|
| 1 | +// Windows-native proof that PowerShell preserves localized adapter aliases. |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { runCommandWithTimeout } from "../process/exec.js"; |
| 4 | +import { resolveAdvertisedLanHostCore } from "./advertised-lan-host.js"; |
| 5 | +import type { NetworkInterfacesSnapshot } from "./network-interfaces.js"; |
| 6 | + |
| 7 | +type ResolveOptions = NonNullable<Parameters<typeof resolveAdvertisedLanHostCore>[0]>; |
| 8 | +type RouteRunner = NonNullable<ResolveOptions["runCommandWithTimeout"]>; |
| 9 | + |
| 10 | +function ipv4(address: string) { |
| 11 | + return { |
| 12 | + address, |
| 13 | + family: "IPv4" as const, |
| 14 | + internal: false, |
| 15 | + netmask: "255.255.255.0", |
| 16 | + mac: "00:00:00:00:00:00", |
| 17 | + cidr: `${address}/24`, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe.runIf(process.platform === "win32")("advertised LAN host PowerShell contract", () => { |
| 22 | + it("round-trips a localized route alias through the production command prefix", async () => { |
| 23 | + let capturedArgv: string[] | undefined; |
| 24 | + const captureRunner: RouteRunner = vi.fn(async (argv) => { |
| 25 | + capturedArgv = argv; |
| 26 | + return { code: 0, stdout: "", stderr: "" }; |
| 27 | + }); |
| 28 | + const interfaces = { |
| 29 | + "vEthernet (Default Switch)": [ipv4("10.37.129.4")], |
| 30 | + "réseau-网卡": [ipv4("192.168.1.20")], |
| 31 | + } as NetworkInterfacesSnapshot; |
| 32 | + |
| 33 | + await resolveAdvertisedLanHostCore({ |
| 34 | + platform: "win32", |
| 35 | + networkInterfaces: () => interfaces, |
| 36 | + runCommandWithTimeout: captureRunner, |
| 37 | + }); |
| 38 | + |
| 39 | + const command = capturedArgv?.at(-1); |
| 40 | + const routeCommandIndex = command?.indexOf("Get-NetRoute") ?? -1; |
| 41 | + const outputPrefix = routeCommandIndex > 0 ? (command?.slice(0, routeCommandIndex) ?? "") : ""; |
| 42 | + const result = await runCommandWithTimeout( |
| 43 | + [ |
| 44 | + "powershell.exe", |
| 45 | + "-NoProfile", |
| 46 | + "-ExecutionPolicy", |
| 47 | + "Bypass", |
| 48 | + "-Command", |
| 49 | + `[Console]::OutputEncoding=[Text.Encoding]::GetEncoding(437); ${outputPrefix}[pscustomobject]@{InterfaceAlias='réseau-网卡';RouteMetric=1;InterfaceMetric=1} | ConvertTo-Json -Compress`, |
| 50 | + ], |
| 51 | + { timeoutMs: 3_000, maxOutputBytes: 16 * 1024 }, |
| 52 | + ); |
| 53 | + expect(result).toMatchObject({ code: 0 }); |
| 54 | + expect(JSON.parse(result.stdout)).toMatchObject({ InterfaceAlias: "réseau-网卡" }); |
| 55 | + expect(command).toMatch( |
| 56 | + /^\[Console\]::OutputEncoding=\[Text\.UTF8Encoding\]::new\(\$false\); Get-NetRoute/, |
| 57 | + ); |
| 58 | + expect(routeCommandIndex).toBeGreaterThan(0); |
| 59 | + |
| 60 | + await expect( |
| 61 | + resolveAdvertisedLanHostCore({ |
| 62 | + platform: "win32", |
| 63 | + networkInterfaces: () => interfaces, |
| 64 | + runCommandWithTimeout: vi.fn(async () => result), |
| 65 | + }), |
| 66 | + ).resolves.toBe("192.168.1.20"); |
| 67 | + }); |
| 68 | +}); |
0 commit comments