import { isNetworkPath, extractHostName, extractDriveLetter, buildNetworkUrl, collectHostsForCopyPaths, buildNetInfo } from '../src/shared/network-host.ts' const cases = [] function eq(name, actual, expected) { const ok = Array.isArray(expected) ? JSON.stringify(actual) === JSON.stringify(expected) : 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)}`) } } eq('isNetworkPath \\host', isNetworkPath('\\\\192.168.1.100\\share'), true) eq('isNetworkPath //host', isNetworkPath('//nas/share'), true) eq('isNetworkPath D:\\a', isNetworkPath('D:\\data'), false) eq('isNetworkPath empty', isNetworkPath(''), false) const unc = '\\\\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(''), '') eq('extractDriveLetter Z', extractDriveLetter('Z:\\folder'), 'Z') eq('extractDriveLetter C', extractDriveLetter('C:\\data\\sub'), 'C') eq('extractDriveLetter unc', extractDriveLetter(unc), '') eq('buildNetworkUrl bare', buildNetworkUrl('192.168.1.100', ''), '\\\\192.168.1.100') eq('buildNetworkUrl share', buildNetworkUrl('192.168.1.100', 'share'), '\\\\192.168.1.100\\share') eq('buildNetworkUrl slashes stripped', buildNetworkUrl('host', '/share/'), '\\\\host\\share') eq( 'collectHosts unc + drive', collectHostsForCopyPaths(['\\\\192.168.1.100\\share\\a', 'Z:\\data'], { Z: '192.168.1.200' }), ['192.168.1.100', '192.168.1.200'] ) eq('collectHosts local only', collectHostsForCopyPaths(['E:\\data'], {}), []) 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"}]') 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"}]') let threw = false try { buildNetInfo([{ hostName: 'h1' }], () => null) } catch (e) { threw = e.message.includes('缺少网络凭据') } eq('buildNetInfo missing throws', threw, true) 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)