|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | | -import { formatDateTime } from '../../lib/datetime'; |
| 2 | +import dayjs from 'dayjs'; |
| 3 | +import timezone from 'dayjs/plugin/timezone'; |
| 4 | +import utc from 'dayjs/plugin/utc'; |
| 5 | +import { APP_TIME_ZONE, formatDateTime } from '../../lib/datetime'; |
3 | 6 | import { contentArchiveDir, contentUrl } from './cms-urls'; |
4 | 7 |
|
5 | | -const AT = new Date(2026, 6, 5); // 2026-07-05(月/日单位数,用于验证补零差异) |
| 8 | +dayjs.extend(utc); |
| 9 | +dayjs.extend(timezone); |
| 10 | + |
| 11 | +// 固定在 APP_TIME_ZONE 的正午构造,避开任何 runner 时区的日界,使字面断言与 TZ 无关 |
| 12 | +// (用 new Date(2026, 6, 5) 会随 OS 时区漂移,CI 与本地结果不一致) |
| 13 | +const AT = dayjs.tz('2026-07-05 12:00:00', 'YYYY-MM-DD HH:mm:ss', APP_TIME_ZONE).toDate(); |
6 | 14 |
|
7 | 15 | function content(overrides: Partial<Parameters<typeof contentUrl>[2]> = {}) { |
8 | 16 | return { id: 42, slug: null, publishedAt: AT, createdAt: AT, ...overrides }; |
@@ -36,8 +44,8 @@ describe('contentArchiveDir', () => { |
36 | 44 | expect(contentArchiveDir('year', content({ publishedAt: null, createdAt: null }))).toBe(''); |
37 | 45 | }); |
38 | 46 |
|
39 | | - it('取值口径与 formatDateTime 展示一致(同为 dayjs 本地时区)', () => { |
40 | | - // 存的是 UTC 2023-12-31T16:00Z,在 UTC+8 下展示为 2024-01-01,归档目录须同为 2024 |
| 47 | + it('取值口径与 formatDateTime 展示一致(同为 APP_TIME_ZONE,而非 OS 本地时区)', () => { |
| 48 | + // APP_TIME_ZONE 默认 Asia/Shanghai,部署机 TZ 未设置时裸 dayjs 会按 UTC 算,两者差一年 |
41 | 49 | const utcBoundary = new Date('2023-12-31T16:00:00.000Z'); |
42 | 50 | const displayedYear = Number(formatDateTime(utcBoundary).slice(0, 4)); |
43 | 51 | expect(contentArchiveDir('year', content({ publishedAt: utcBoundary }))).toBe(`${displayedYear}/`); |
@@ -69,7 +77,7 @@ describe('contentUrl 与归档规则', () => { |
69 | 77 | expect(contentUrl('', channel, c, 3)).toBe('/topic/2026/special_3.html'); |
70 | 78 | }); |
71 | 79 |
|
72 | | - it('baseUrl 前缀参与拼接(预览 / 通道子树场景)', () => { |
| 80 | + it('baseUrl 前缀参与拼接(预览场景)', () => { |
73 | 81 | expect(contentUrl('/__cms/main', channel, content())).toBe('/__cms/main/news/2026/42.html'); |
74 | 82 | }); |
75 | 83 |
|
|
0 commit comments