43 lines
2.3 KiB
TypeScript
43 lines
2.3 KiB
TypeScript
import assert from 'node:assert/strict'
|
||
import { test } from 'node:test'
|
||
import { formatGameDuration } from '../src/utils/gameDuration.js'
|
||
|
||
const start = '2026-10-27T08:00:00Z'
|
||
const now = Date.parse(start)
|
||
const actualNow = now + new Date(now).getTimezoneOffset() * 60000
|
||
|
||
test('длительность игры форматируется на границах единиц времени', () => {
|
||
for (const [seconds, expected] of [
|
||
[0, '0с'], [59, '59с'], [60, '1м 0с'], [3599, '59м 59с'],
|
||
[3600, '1ч 0м'], [86399, '23ч 59м'], [86400, '1д 0ч'],
|
||
[30 * 86400, '1мес 0д'], [365 * 86400, '1г 0д'],
|
||
] as const) {
|
||
assert.equal(formatGameDuration(start, '', actualNow + seconds * 1000), expected)
|
||
}
|
||
assert.equal(formatGameDuration('2026-10-27T11:00:00+03:00', '', actualNow + 60000), '1м 0с')
|
||
})
|
||
|
||
test('начало, завершение и некорректные даты обрабатываются явно', () => {
|
||
assert.equal(formatGameDuration('', '', now), 'Игра ещё не началась')
|
||
assert.equal(formatGameDuration(start, '', actualNow - 1), 'Игра ещё не началась')
|
||
assert.equal(formatGameDuration(start, '2026-10-27T09:12:00Z', now + 86400000), '1ч 12м')
|
||
assert.equal(formatGameDuration('invalid', '', now), 'Время игры недоступно')
|
||
assert.equal(formatGameDuration(start, 'invalid', now), 'Время игры недоступно')
|
||
})
|
||
|
||
test('идущая игра считает местные часы сервера как вкладка «Игра»', (t) => {
|
||
const previousTimezone = process.env.TZ
|
||
t.after(() => {
|
||
if (previousTimezone === undefined) delete process.env.TZ
|
||
else process.env.TZ = previousTimezone
|
||
})
|
||
process.env.TZ = 'Asia/Barnaul'
|
||
const now = Date.parse('2026-10-27T08:12:00Z')
|
||
// В Барнауле игра началась в 15:00; API возвращает эти часы с суффиксом Z.
|
||
assert.equal(formatGameDuration('2026-10-27T15:00:00Z', '', now), '12м 0с')
|
||
assert.equal(formatGameDuration('2026-10-27T15:00:00Z', '', now + 1000), '12м 1с')
|
||
assert.equal(formatGameDuration('2026-10-27T15:00:00Z', '2026-10-27T15:05:00Z', now), '5м 0с')
|
||
process.env.TZ = 'UTC'
|
||
assert.equal(formatGameDuration('2026-10-27T08:00:00Z', '', now), '12м 0с')
|
||
})
|