Skip to content

Commit 938b536

Browse files
committed
test(voice-call): cover tunnel startup
1 parent 2b87d9f commit 938b536

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { EventEmitter } from "node:events";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
4+
class FakeChildProcess extends EventEmitter {
5+
readonly stdout = new EventEmitter();
6+
readonly stderr = new EventEmitter();
7+
killedWith: NodeJS.Signals | null = null;
8+
9+
kill(signal: NodeJS.Signals = "SIGTERM"): boolean {
10+
this.killedWith = signal;
11+
queueMicrotask(() => this.emit("close", null));
12+
return true;
13+
}
14+
15+
close(code: number | null = 0): void {
16+
this.emit("close", code);
17+
}
18+
19+
fail(error: Error): void {
20+
this.emit("error", error);
21+
}
22+
}
23+
24+
const mocks = vi.hoisted(() => ({
25+
spawn: vi.fn(),
26+
getTailscaleDnsName: vi.fn(),
27+
}));
28+
29+
vi.mock("node:child_process", () => ({
30+
spawn: mocks.spawn,
31+
}));
32+
33+
vi.mock("./webhook/tailscale.js", () => ({
34+
getTailscaleDnsName: mocks.getTailscaleDnsName,
35+
}));
36+
37+
import { isNgrokAvailable, startNgrokTunnel, startTailscaleTunnel, startTunnel } from "./tunnel.js";
38+
39+
function nextProcess(): FakeChildProcess {
40+
const proc = new FakeChildProcess();
41+
mocks.spawn.mockReturnValueOnce(proc as never);
42+
return proc;
43+
}
44+
45+
function emitNgrokUrl(proc: FakeChildProcess, url: string): void {
46+
proc.stdout.emit("data", Buffer.from(`${JSON.stringify({ msg: "started tunnel", url })}\n`));
47+
}
48+
49+
describe("voice-call tunnels", () => {
50+
beforeEach(() => {
51+
vi.clearAllMocks();
52+
mocks.getTailscaleDnsName.mockReset();
53+
});
54+
55+
it("checks ngrok availability from the version command exit code", async () => {
56+
const proc = nextProcess();
57+
const result = isNgrokAvailable();
58+
proc.close(0);
59+
60+
await expect(result).resolves.toBe(true);
61+
expect(mocks.spawn).toHaveBeenCalledWith("ngrok", ["version"], expect.any(Object));
62+
});
63+
64+
it("treats ngrok spawn failures as unavailable", async () => {
65+
const proc = nextProcess();
66+
const result = isNgrokAvailable();
67+
proc.fail(new Error("spawn ngrok ENOENT"));
68+
69+
await expect(result).resolves.toBe(false);
70+
});
71+
72+
it("starts ngrok and appends the webhook path to the public URL", async () => {
73+
const proc = nextProcess();
74+
const result = startNgrokTunnel({ port: 3334, path: "/voice/webhook" });
75+
76+
emitNgrokUrl(proc, "https://abc.ngrok.io");
77+
78+
await expect(result).resolves.toMatchObject({
79+
publicUrl: "https://abc.ngrok.io/voice/webhook",
80+
provider: "ngrok",
81+
});
82+
expect(mocks.spawn).toHaveBeenCalledWith(
83+
"ngrok",
84+
expect.arrayContaining(["http", "3334"]),
85+
expect.any(Object),
86+
);
87+
});
88+
89+
it("sets ngrok auth token before starting the tunnel", async () => {
90+
const authProc = nextProcess();
91+
const tunnelProc = nextProcess();
92+
const result = startNgrokTunnel({
93+
port: 3334,
94+
path: "/hook",
95+
authToken: "token",
96+
});
97+
98+
authProc.close(0);
99+
await vi.waitFor(() => expect(mocks.spawn).toHaveBeenCalledTimes(2));
100+
emitNgrokUrl(tunnelProc, "https://auth.ngrok.io");
101+
102+
await expect(result).resolves.toMatchObject({
103+
publicUrl: "https://auth.ngrok.io/hook",
104+
});
105+
expect(mocks.spawn).toHaveBeenNthCalledWith(
106+
1,
107+
"ngrok",
108+
["config", "add-authtoken", "token"],
109+
expect.any(Object),
110+
);
111+
});
112+
113+
it("rejects ngrok startup errors from stderr", async () => {
114+
const proc = nextProcess();
115+
const result = startNgrokTunnel({ port: 3334, path: "/hook" });
116+
117+
proc.stderr.emit("data", Buffer.from("ERR_NGROK_3200: invalid auth token"));
118+
119+
await expect(result).rejects.toThrow("ngrok error:");
120+
});
121+
122+
it("starts Tailscale serve using the resolved tailnet DNS name", async () => {
123+
mocks.getTailscaleDnsName.mockResolvedValue("host.tailnet.ts.net");
124+
const proc = nextProcess();
125+
const result = startTailscaleTunnel({
126+
mode: "serve",
127+
port: 3334,
128+
path: "voice/webhook",
129+
});
130+
131+
await vi.waitFor(() => expect(mocks.spawn).toHaveBeenCalled());
132+
proc.close(0);
133+
134+
await expect(result).resolves.toMatchObject({
135+
publicUrl: "https://host.tailnet.ts.net/voice/webhook",
136+
provider: "tailscale-serve",
137+
});
138+
expect(mocks.spawn).toHaveBeenCalledWith(
139+
"tailscale",
140+
expect.arrayContaining(["serve", "--set-path", "/voice/webhook"]),
141+
expect.any(Object),
142+
);
143+
});
144+
145+
it("rejects Tailscale tunnel startup when the DNS name is unavailable", async () => {
146+
mocks.getTailscaleDnsName.mockResolvedValue(null);
147+
148+
await expect(
149+
startTailscaleTunnel({ mode: "funnel", port: 3334, path: "/hook" }),
150+
).rejects.toThrow("Could not get Tailscale DNS name");
151+
expect(mocks.spawn).not.toHaveBeenCalled();
152+
});
153+
154+
it("dispatches tunnel providers from config", async () => {
155+
await expect(startTunnel({ provider: "none", port: 3334, path: "/hook" })).resolves.toBeNull();
156+
157+
const proc = nextProcess();
158+
const result = startTunnel({ provider: "ngrok", port: 3334, path: "/hook" });
159+
emitNgrokUrl(proc, "https://dispatch.ngrok.io");
160+
161+
await expect(result).resolves.toMatchObject({
162+
publicUrl: "https://dispatch.ngrok.io/hook",
163+
provider: "ngrok",
164+
});
165+
});
166+
});

0 commit comments

Comments
 (0)