Skip to content

Commit 139e672

Browse files
fix(test): evaluate Windows platform check lazily for CI portability
isWindows was a module-level constant evaluated at import time, so vi.stubGlobal('navigator', ...) in tests took effect too late and the value froze to the host platform - breaking the Windows path normalization tests on Linux/macOS CI runners. - replace the isWindows constant with an isWindowsPlatform() function evaluated per call - anchor the platform regex (^win) so 'Darwin' is not misdetected - give the test navigator stub a realistic platform + userAgent pair Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 6ca2d2c commit 139e672

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/utils/path.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ const WINDOWS_VERBATIM_UNC_RE = /^\\\\\?\\UNC\\/
22
const WINDOWS_VERBATIM_RE = /^\\\\\?\\/
33
const TRAILING_SLASH_RE = /\/+$/
44

5-
/** 当前是否运行在 Windows 上(路径比较时大小写不敏感)。 */
6-
export const isWindows = (() => {
7-
// 优先使用 userAgent/platform(Tauri WebView 下可靠)
5+
/**
6+
* 当前是否运行在 Windows 上(路径比较时大小写不敏感)。
7+
* 必须在调用时求值而非模块加载时缓存:测试通过 stubGlobal 替换
8+
* navigator,模块级常量会在 stub 生效前被锁死为宿主平台的值。
9+
*/
10+
export function isWindowsPlatform(): boolean {
11+
// 优先使用 platform/userAgent(Tauri WebView 下可靠)。
12+
// 用 ^win 锚定避免 'Darwin' 误匹配。
813
if (typeof navigator !== 'undefined') {
9-
if (navigator.platform && /win/i.test(navigator.platform))
14+
if (navigator.platform && /^win/i.test(navigator.platform))
1015
return true
1116
if (/Windows/.test(navigator.userAgent))
1217
return true
@@ -20,7 +25,7 @@ export const isWindows = (() => {
2025
catch {
2126
return false
2227
}
23-
})()
28+
}
2429

2530
/** 去除 Windows canonicalize 产生的 verbatim 前缀(`\\?\` / `\\?\UNC\`)。 */
2631
export function stripWindowsVerbatimPrefix(path: string) {
@@ -35,5 +40,5 @@ export function stripWindowsVerbatimPrefix(path: string) {
3540
*/
3641
export function normalizePathKey(path: string) {
3742
const normalized = stripWindowsVerbatimPrefix(path).trim().replaceAll('\\', '/').replace(TRAILING_SLASH_RE, '')
38-
return isWindows ? normalized.toLowerCase() : normalized
43+
return isWindowsPlatform() ? normalized.toLowerCase() : normalized
3944
}

tests/projectsStore.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ function createMemoryStorage() {
1818
describe('projectsStore', () => {
1919
beforeEach(() => {
2020
setActivePinia(createPinia())
21-
vi.stubGlobal('navigator', { userAgent: 'Windows' })
21+
vi.stubGlobal('navigator', {
22+
platform: 'Win32',
23+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
24+
})
2225
vi.stubGlobal('window', {
2326
localStorage: createMemoryStorage(),
2427
api: {

0 commit comments

Comments
 (0)