From 0eedbfd5b2331bccdf7687f14ee1904fdc78ce20 Mon Sep 17 00:00:00 2001 From: Rekty Date: Tue, 25 Aug 2026 14:08:39 +0700 Subject: [PATCH] test(common): pin the Beijing weekend shift with edge instants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1090 The two weekend tests sat at 02:00Z, where the +8h shift and a plain getUTCDay() agree — deleting the shift left the suite green. Export isBeijingWeekend and pin its four edges (Fri 15:00Z/16:00Z, Sun 15:00Z/16:00Z), the only instants where the calendars disagree, so the predicate can no longer silently regress to UTC days. --- .../src/__tests__/freebuff-peak-hours.test.ts | 18 ++++++++++++++++++ common/src/constants/freebuff-peak-hours.ts | 9 ++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/common/src/__tests__/freebuff-peak-hours.test.ts b/common/src/__tests__/freebuff-peak-hours.test.ts index 72abc8f97d..527c227e63 100644 --- a/common/src/__tests__/freebuff-peak-hours.test.ts +++ b/common/src/__tests__/freebuff-peak-hours.test.ts @@ -4,6 +4,7 @@ import { DEEPSEEK_EXPENSIVE_WINDOW_UTC, deepSeekExpensiveWindowEndsAt, deepseekPricingWindow, + isBeijingWeekend, isDeepSeekExpensiveWindow, } from '../constants/freebuff-peak-hours' @@ -72,3 +73,20 @@ test.each(['2026-08-29T02:00:00Z', '2026-08-30T02:00:00Z'])( expect(isDeepSeekExpensiveWindow(date)).toBe(false) }, ) + +describe('the Beijing weekend shift', () => { + // The Beijing weekend runs Fri 16:00Z -> Sun 16:00Z, a band where every UTC + // hour is off-peak anyway — so the window functions cannot see the +8h shift + // and neither can the weekend tests above (they sit at 02:00Z, inside the + // agreement band of both calendars). These instants sit exactly on the four + // edges, where the shift and a plain getUTCDay() disagree; only the + // predicate itself can tell them apart. + test.each([ + ['2026-08-28T15:00:00Z', false], // Fri 23:00 Beijing — last weekday hour + ['2026-08-28T16:00:00Z', true], // Sat 00:00 Beijing — weekend opens + ['2026-08-30T15:00:00Z', true], // Sun 23:00 Beijing — still weekend + ['2026-08-30T16:00:00Z', false], // Mon 00:00 Beijing — weekend closed + ])('%s is weekend=%p in Beijing', (instant, weekend) => { + expect(isBeijingWeekend(new Date(instant))).toBe(weekend as boolean) + }) +}) diff --git a/common/src/constants/freebuff-peak-hours.ts b/common/src/constants/freebuff-peak-hours.ts index 99c92a6ec5..6b657b07f0 100644 --- a/common/src/constants/freebuff-peak-hours.ts +++ b/common/src/constants/freebuff-peak-hours.ts @@ -29,7 +29,14 @@ export const DEEPSEEK_PEAK_HOUR_RANGES_UTC: ReadonlyArray< export type DeepSeekPricingWindow = 'peak' | 'off-peak' -function isBeijingWeekend(at: Date): boolean { +/** + * Whether `at` falls on a Saturday or Sunday in Beijing time (UTC+8). + * + * Exported for tests: the Beijing weekend runs Fri 16:00Z -> Sun 16:00Z, a + * band the peak-hour windows never touch, so only this predicate can pin the + * shift directly. + */ +export function isBeijingWeekend(at: Date): boolean { const beijingDay = new Date(at.getTime() + 8 * 60 * 60 * 1000).getUTCDay() return beijingDay === 0 || beijingDay === 6 }