1import { assert, expect, it } from "@effect/vitest";
2
3import {
4 buildPairingUrl,
5 formatHeadlessServeOutput,
6 renderTerminalQrCode,
7 resolveHeadlessConnectionHost,
8 resolveHeadlessConnectionString,
9 resolveListeningPort,
10} from "./startupAccess.ts";
11
12it("prefers localhost when no explicit host is configured", () => {
13 expect(resolveHeadlessConnectionHost(undefined)).toBe("localhost");
14 expect(resolveHeadlessConnectionString(undefined, 3773)).toBe("http://localhost:3773");
15});
16
17it("keeps explicit bind hosts in the connection string", () => {
18 expect(resolveHeadlessConnectionString("127.0.0.1", 3773)).toBe("http://127.0.0.1:3773");
19 expect(resolveHeadlessConnectionString("::1", 3773)).toBe("http://[::1]:3773");
20});
21
22it("resolves wildcard hosts to a concrete external interface when one is available", () => {
23 const connectionString = resolveHeadlessConnectionString("0.0.0.0", 3773, {
24 en0: [
25 {
26 address: "192.168.1.42",
27 netmask: "255.255.255.0",
28 family: "IPv4",
29 mac: "00:00:00:00:00:00",
30 internal: false,
31 cidr: "192.168.1.42/24",
32 },
33 ],
34 lo0: [
35 {
36 address: "127.0.0.1",
37 netmask: "255.0.0.0",
38 family: "IPv4",
39 mac: "00:00:00:00:00:00",
40 internal: true,
41 cidr: "127.0.0.1/8",
42 },
43 ],
44 });
45
46 expect(connectionString).toBe("http://192.168.1.42:3773");
47});
48
49it("prefers the actual bound port when an http server address is available", () => {
50 expect(resolveListeningPort({ port: 4123 }, 3773)).toBe(4123);
51 expect(resolveListeningPort("pipe", 3773)).toBe(3773);
52 expect(resolveListeningPort(null, 3773)).toBe(3773);
53});
54
55it("builds a pairing URL that embeds the token in the hash", () => {
56 expect(buildPairingUrl("http://192.168.1.42:3773", "PAIRCODE")).toBe(
57 "http://192.168.1.42:3773/pair#token=PAIRCODE",
58 );
59});
60
61it("renders terminal QR codes as a multi-line unicode block grid", () => {
62 const qrCode = renderTerminalQrCode("http://192.168.1.42:3773/pair#token=PAIRCODE");
63
64 assert.isTrue(qrCode.includes("█"));
65 assert.isTrue(qrCode.split("\n").length > 10);
66});
67
68it("formats headless serve output with the connection string, token, pairing url, and qr code", () => {
69 const output = formatHeadlessServeOutput({
70 connectionString: "http://192.168.1.42:3773",
71 token: "PAIRCODE",
72 pairingUrl: "http://192.168.1.42:3773/pair#token=PAIRCODE",
73 });
74
75 expect(output).toContain("Connection string: http://192.168.1.42:3773");
76 expect(output).toContain("Token: PAIRCODE");
77 expect(output).toContain("Pairing URL: http://192.168.1.42:3773/pair#token=PAIRCODE");
78 assert.isTrue(output.includes("█") || output.includes("▀") || output.includes("▄"));
79});