50 行
1.6 KiB
TypeScript
50 行
1.6 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import {
|
|
expirationStatus,
|
|
formatNumericDateTime,
|
|
millisecondsUntil,
|
|
toTimestamp,
|
|
} from '../src/date'
|
|
|
|
test('toTimestamp accepts seconds, milliseconds and API date strings', () => {
|
|
assert.equal(toTimestamp(1_700_000_000), 1_700_000_000_000)
|
|
assert.equal(toTimestamp(1_700_000_000_000), 1_700_000_000_000)
|
|
assert.equal(toTimestamp('2026-07-17 12:30:00'), new Date('2026-07-17T12:30:00').getTime())
|
|
assert.equal(toTimestamp('invalid'), null)
|
|
})
|
|
|
|
test('formatNumericDateTime produces stable protocol and display layouts', () => {
|
|
const value = new Date(2026, 6, 18, 9, 5, 4)
|
|
assert.equal(formatNumericDateTime(value), '2026-07-18 09:05:04')
|
|
assert.equal(
|
|
formatNumericDateTime(value, {
|
|
dateSeparator: '/',
|
|
includeTime: false,
|
|
}),
|
|
'2026/07/18',
|
|
)
|
|
assert.equal(
|
|
formatNumericDateTime(value, {
|
|
includeDate: false,
|
|
includeSeconds: false,
|
|
}),
|
|
'09:05',
|
|
)
|
|
assert.equal(formatNumericDateTime('invalid'), null)
|
|
})
|
|
|
|
test('expirationStatus uses one canonical strict end-time decision', () => {
|
|
const now = 1_700_000_000_000
|
|
assert.equal(expirationStatus(null, now), 'missing')
|
|
assert.equal(expirationStatus(now + 1, now), 'valid')
|
|
assert.equal(expirationStatus(now, now), 'expired')
|
|
assert.equal(expirationStatus(now - 1, now), 'expired')
|
|
})
|
|
|
|
test('millisecondsUntil keeps positive and negative durations', () => {
|
|
assert.equal(millisecondsUntil(2_000, 1_000), 1_000_000)
|
|
assert.equal(millisecondsUntil(1_000_000_000_000, 1_000_000_000_500), -500)
|
|
})
|