The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { DiscoveredLocalServer } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { mergeServers } from "./useDiscoveredLocalServers"; |
| 5 | |
| 6 | const scannerServer = ( |
| 7 | overrides: Partial<DiscoveredLocalServer & { requestedUrl: string }>, |
| 8 | ): DiscoveredLocalServer & { requestedUrl: string } => ({ |
| 9 | host: "localhost", |
| 10 | port: 5173, |
| 11 | url: "http://localhost:5173", |
| 12 | requestedUrl: overrides.url ?? "http://localhost:5173", |
| 13 | processName: "vite", |
| 14 | pid: 1234, |
| 15 | terminal: null, |
| 16 | ...overrides, |
| 17 | }); |
| 18 | |
| 19 | describe("mergeServers", () => { |
| 20 | it("enriches a configured entry with live process metadata when scanner sees it", () => { |
| 21 | const result = mergeServers({ |
| 22 | scanner: [scannerServer({ port: 5173, processName: "node", pid: 9999 })], |
| 23 | configuredUrls: ["http://localhost:5173"], |
| 24 | }); |
| 25 | expect(result).toHaveLength(1); |
| 26 | expect(result[0]).toMatchObject({ |
| 27 | port: 5173, |
| 28 | source: "configured", |
| 29 | processName: "node", |
| 30 | pid: 9999, |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | it("excludes configured entries that the live scanner doesn't see", () => { |
| 35 | const result = mergeServers({ |
| 36 | scanner: [], |
| 37 | configuredUrls: ["http://localhost:5173"], |
| 38 | }); |
| 39 | expect(result).toHaveLength(0); |
| 40 | }); |
| 41 | |
| 42 | it("ignores non-loopback configured URLs", () => { |
| 43 | const result = mergeServers({ |
| 44 | scanner: [scannerServer({})], |
| 45 | configuredUrls: ["https://example.com", "ws://localhost:5173"], |
| 46 | }); |
| 47 | expect(result).toHaveLength(1); |
| 48 | expect(result[0]?.source).toBe("scanner"); |
| 49 | }); |
| 50 | |
| 51 | it.each(["127.0.0.1", "0.0.0.0", "[::1]"])( |
| 52 | "matches configured loopback alias %s to a live localhost server", |
| 53 | (host) => { |
| 54 | const result = mergeServers({ |
| 55 | scanner: [ |
| 56 | scannerServer({ requestedUrl: `http://localhost:5173/dashboard?mode=test#results` }), |
| 57 | ], |
| 58 | configuredUrls: [`http://${host}:5173/dashboard?mode=test#results`], |
| 59 | }); |
| 60 | expect(result).toHaveLength(1); |
| 61 | expect(result[0]?.source).toBe("configured"); |
| 62 | expect(result[0]?.requestedUrl).toBe("http://localhost:5173/dashboard?mode=test#results"); |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | it("keeps the scanner-verified path and protocol", () => { |
| 67 | const result = mergeServers({ |
| 68 | scanner: [ |
| 69 | scannerServer({ |
| 70 | url: "https://env-42.example.dev:5173/", |
| 71 | requestedUrl: "http://localhost:5173/dashboard?mode=test#results", |
| 72 | }), |
| 73 | ], |
| 74 | configuredUrls: ["https://localhost:5173/dashboard?mode=test#results"], |
| 75 | }); |
| 76 | expect(result[0]?.url).toBe("https://env-42.example.dev:5173/"); |
| 77 | expect(result[0]?.requestedUrl).toBe("http://localhost:5173/dashboard?mode=test#results"); |
| 78 | }); |
| 79 | |
| 80 | it("overlays a configured path when an older server does not advertise path probing", () => { |
| 81 | const result = mergeServers({ |
| 82 | scanner: [scannerServer({ requestedUrl: "http://localhost:5173/" })], |
| 83 | configuredUrls: ["https://localhost:5173/docs?mode=test#results"], |
| 84 | configuredUrlProbing: false, |
| 85 | }); |
| 86 | |
| 87 | expect(result[0]?.requestedUrl).toBe("https://localhost:5173/docs?mode=test#results"); |
| 88 | }); |
| 89 | |
| 90 | it("does not overlay an unverified configured path when the server probes paths", () => { |
| 91 | const result = mergeServers({ |
| 92 | scanner: [scannerServer({ requestedUrl: "http://localhost:5173/" })], |
| 93 | configuredUrls: ["http://localhost:5173/docs"], |
| 94 | configuredUrlProbing: true, |
| 95 | }); |
| 96 | |
| 97 | expect(result[0]?.requestedUrl).toBe("http://localhost:5173/"); |
| 98 | }); |
| 99 | }); |