// 临时自测:验证 networkPath.ts 的纯函数行为 // 跑法:node scripts/network-path-selftest.mjs import { isNetworkPath, extractHostName, buildNetworkUrl, buildNetInfo } from '../src/renderer/src/utils/networkPath.ts' const cases = [] function eq(name, actual, expected) { // 两边按字面字符串比较(不走 JSON.stringify 避免转义歧义) const ok = actual === expected cases.push({ name, ok, actual, expected }) if (!ok) { console.error(`FAIL ${name}`) console.error(` expected: ${JSON.stringify(expected)}`) console.error(` actual: ${JSON.stringify(actual)}`) } } // isNetworkPath eq('isNetworkPath \\host', isNetworkPath('\\\\192.168.1.100\\share'), true) eq('isNetworkPath //host', isNetworkPath('//nas/share'), true) eq('isNetworkPath D:\\a (应 false)', isNetworkPath('D:\\data'), false) eq('isNetworkPath empty', isNetworkPath(''), false) // extractHostName const unc = '\\\\192.168.1.100\\share\\a.pdf' // 实际 \\192.168.1.100\share\a.pdf eq('extractHostName \\host\\share', extractHostName(unc), '192.168.1.100') eq('extractHostName //host/share', extractHostName('//nas/share'), 'nas') eq('extractHostName local', extractHostName('D:\\data'), '') eq('extractHostName empty', extractHostName(''), '') // buildNetworkUrl // 期望: \\192.168.1.100 -> 字面 '\\\\192.168.1.100' eq('buildNetworkUrl bare', buildNetworkUrl('192.168.1.100', ''), '\\\\192.168.1.100') // 期望: \\192.168.1.100\share -> 字面 '\\\\192.168.1.100\\share' eq('buildNetworkUrl share', buildNetworkUrl('192.168.1.100', 'share'), '\\\\192.168.1.100\\share') eq('buildNetworkUrl slashes stripped', buildNetworkUrl('host', '/share/'), '\\\\host\\share') // buildNetInfo // 期望产物是 JSON 文本(JSON 字符串里 \\ 表示 1 个 \ 字符) // 反序列化后 host_name 值是 2 个 \ 字符 -> JSON 文本中需要 4 个 \ 字符 // 4 个 \ 字符 = JS 字面 '\\\\\\\\' (8 个 \) const stored = new Map([['192.168.1.100', { userName: 'admin', password: 'pass', lastUsed: '' }]]) const r1 = buildNetInfo( [{ hostName: '192.168.1.100' }], (h) => stored.get(h) || null ) eq('buildNetInfo single', r1, '[{"host_name":"\\\\\\\\192.168.1.100","user_name":"admin","password":"pass"}]') // dedup const r2 = buildNetInfo( [ { hostName: '192.168.1.100', userName: 'a', password: 'p' }, { hostName: '192.168.1.100', userName: 'b', password: 'q' } ], () => null ) eq('buildNetInfo dedup', r2, '[{"host_name":"\\\\\\\\192.168.1.100","user_name":"a","password":"p"}]') // missing throws let threw = false try { buildNetInfo([{ hostName: 'h1' }], () => null) } catch (e) { threw = e.message.includes('缺少网络凭据') } eq('buildNetInfo missing throws', threw, true) // empty -> empty string eq('buildNetInfo empty list', buildNetInfo([], () => null), '') const pass = cases.filter((c) => c.ok).length const fail = cases.length - pass console.log(`PASS ${pass} / ${cases.length}`) if (fail > 0) process.exit(1)