Skip to content

Commit 43861e6

Browse files
fix(common): honor DeepSeek Beijing weekend boundaries
1 parent 366311e commit 43861e6

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

common/src/__tests__/freebuff-peak-hours.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { describe, expect, test } from 'bun:test'
22

33
import {
44
DEEPSEEK_EXPENSIVE_WINDOW_UTC,
5+
DEEPSEEK_WEEKEND_OFFPEAK_EFFECTIVE_AT_UTC,
56
deepSeekExpensiveWindowEndsAt,
67
deepseekPricingWindow,
8+
isBeijingWeekend,
79
isDeepSeekExpensiveWindow,
810
} from '../constants/freebuff-peak-hours'
911

@@ -64,6 +66,38 @@ describe('the expensive window', () => {
6466
})
6567
})
6668

69+
describe('the Beijing weekend boundary', () => {
70+
test.each([
71+
['2026-08-28T15:00:00Z', false], // Friday 23:00 in Beijing
72+
['2026-08-28T16:00:00Z', true], // Saturday 00:00 in Beijing
73+
['2026-08-30T15:00:00Z', true], // Sunday 23:00 in Beijing
74+
['2026-08-30T16:00:00Z', false], // Monday 00:00 in Beijing
75+
])('%s isBeijingWeekend=%p', (instant, weekend) => {
76+
expect(isBeijingWeekend(new Date(instant as string))).toBe(
77+
weekend as boolean,
78+
)
79+
})
80+
81+
test('does not apply the weekend rule before its effective instant', () => {
82+
expect(DEEPSEEK_WEEKEND_OFFPEAK_EFFECTIVE_AT_UTC).toBe(
83+
Date.parse('2026-08-22T16:00:00Z'),
84+
)
85+
expect(deepseekPricingWindow(new Date('2026-08-22T09:59:59Z'))).toBe('peak')
86+
expect(isDeepSeekExpensiveWindow(new Date('2026-08-22T02:00:00Z'))).toBe(
87+
true,
88+
)
89+
})
90+
91+
test('applies the weekend rule from its effective instant onward', () => {
92+
expect(deepseekPricingWindow(new Date('2026-08-23T01:30:00Z'))).toBe(
93+
'off-peak',
94+
)
95+
expect(isDeepSeekExpensiveWindow(new Date('2026-08-23T02:00:00Z'))).toBe(
96+
false,
97+
)
98+
})
99+
})
100+
67101
test.each(['2026-08-29T02:00:00Z', '2026-08-30T02:00:00Z'])(
68102
'%s uses weekend rates in Beijing',
69103
(instant) => {

common/src/constants/freebuff-peak-hours.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
/**
1414
* Peak hours, from api-docs.deepseek.com/quick_start/pricing (read 2026-08-16):
1515
* "Peak hours are 01:00 - 04:00 and 06:00 - 10:00 UTC (all other hours are
16-
* off-peak)." These windows apply Monday-Friday Beijing time; weekends are
17-
* always off-peak.
16+
* off-peak)." These windows apply Monday-Friday Beijing time; from
17+
* `DEEPSEEK_WEEKEND_OFFPEAK_EFFECTIVE_AT_UTC` onward, weekends are off-peak.
1818
*
1919
* Half-open [start, end): 04:00:00 UTC itself is already off-peak. TWO
2020
* disjoint windows, not one — the 04:00-06:00 gap between them is exactly what
@@ -29,7 +29,17 @@ export const DEEPSEEK_PEAK_HOUR_RANGES_UTC: ReadonlyArray<
2929

3030
export type DeepSeekPricingWindow = 'peak' | 'off-peak'
3131

32-
function isBeijingWeekend(at: Date): boolean {
32+
/**
33+
* The weekend pricing change took effect at 00:00 Beijing time on 2026-08-23.
34+
* Keep this as an instant rather than a local date so historical billing and
35+
* availability checks use the same boundary in every runtime timezone.
36+
*/
37+
export const DEEPSEEK_WEEKEND_OFFPEAK_EFFECTIVE_AT_UTC = Date.parse(
38+
'2026-08-22T16:00:00Z',
39+
)
40+
41+
/** Whether `at` falls on Saturday or Sunday in Beijing time. */
42+
export function isBeijingWeekend(at: Date): boolean {
3343
const beijingDay = new Date(at.getTime() + 8 * 60 * 60 * 1000).getUTCDay()
3444
return beijingDay === 0 || beijingDay === 6
3545
}
@@ -42,7 +52,12 @@ function isBeijingWeekend(at: Date): boolean {
4252
* ceiling), and a hidden `new Date()` would make both untestable.
4353
*/
4454
export function deepseekPricingWindow(at: Date): DeepSeekPricingWindow {
45-
if (isBeijingWeekend(at)) return 'off-peak'
55+
if (
56+
at.getTime() >= DEEPSEEK_WEEKEND_OFFPEAK_EFFECTIVE_AT_UTC &&
57+
isBeijingWeekend(at)
58+
) {
59+
return 'off-peak'
60+
}
4661
const hour = at.getUTCHours()
4762
const peak = DEEPSEEK_PEAK_HOUR_RANGES_UTC.some(
4863
([startHour, endHour]) => hour >= startHour && hour < endHour,
@@ -86,7 +101,12 @@ export const DEEPSEEK_EXPENSIVE_WINDOW_UTC: readonly [number, number] = [
86101
/** Whether `at` falls in the window above. Half-open like the peak check, so
87102
* the closing hour is already outside it. */
88103
export function isDeepSeekExpensiveWindow(at: Date): boolean {
89-
if (isBeijingWeekend(at)) return false
104+
if (
105+
at.getTime() >= DEEPSEEK_WEEKEND_OFFPEAK_EFFECTIVE_AT_UTC &&
106+
isBeijingWeekend(at)
107+
) {
108+
return false
109+
}
90110
const [start, end] = DEEPSEEK_EXPENSIVE_WINDOW_UTC
91111
const hour = at.getUTCHours()
92112
return hour >= start && hour < end

0 commit comments

Comments
 (0)