


























@@ -12,7 +12,7 @@ const {
1212 ensureFunnel,
1313 tailscaleFunnelStatusCoversPort,
1414} = tailscale;
15-const tailscaleBin = expect.stringMatching(/tailscale$/i);
15+const tailscaleBin = "tailscale";
16161717function createRuntimeWithExitError() {
1818return {
@@ -24,11 +24,23 @@ function createRuntimeWithExitError() {
2424};
2525}
262627-function expectServeFallbackCommand(params: { callArgs: string[]; sudoArgs: string[] }) {
28-return [
29-[tailscaleBin, expect.arrayContaining(params.callArgs)],
30-["sudo", expect.arrayContaining(["-n", tailscaleBin, ...params.sudoArgs])],
31-];
27+function expectExecCall(
28+exec: ReturnType<typeof vi.fn>,
29+callNumber: number,
30+command: string,
31+args: readonly string[],
32+options?: Record<string, unknown>,
33+) {
34+const call = exec.mock.calls[callNumber - 1];
35+expect(call, `call ${callNumber}`).toBeDefined();
36+expect(call[0]).toBe(command);
37+expect(call[1]).toEqual(args);
38+if (options) {
39+expect(call).toHaveLength(3);
40+expect(call[2]).toEqual(options);
41+} else {
42+expect(call).toHaveLength(2);
43+}
3244}
33453446describe("tailscale helpers", () => {
@@ -142,12 +154,15 @@ describe("tailscale helpers", () => {
142154143155await enableTailscaleServe(3000, exec as never);
144156145-const [firstCall, secondCall] = expectServeFallbackCommand({
146-callArgs: ["serve", "--bg", "--yes", "3000"],
147-sudoArgs: ["serve", "--bg", "--yes", "3000"],
157+expect(exec).toHaveBeenCalledTimes(2);
158+expectExecCall(exec, 1, tailscaleBin, ["serve", "--bg", "--yes", "3000"], {
159+maxBuffer: 200_000,
160+timeoutMs: 15_000,
161+});
162+expectExecCall(exec, 2, "sudo", ["-n", tailscaleBin, "serve", "--bg", "--yes", "3000"], {
163+maxBuffer: 200_000,
164+timeoutMs: 15_000,
148165});
149-expect(exec).toHaveBeenNthCalledWith(1, firstCall[0], firstCall[1], expect.any(Object));
150-expect(exec).toHaveBeenNthCalledWith(2, secondCall[0], secondCall[1], expect.any(Object));
151166});
152167153168it("enableTailscaleServe does NOT use sudo if first attempt succeeds", async () => {
@@ -156,11 +171,10 @@ describe("tailscale helpers", () => {
156171await enableTailscaleServe(3000, exec as never);
157172158173expect(exec).toHaveBeenCalledTimes(1);
159-expect(exec).toHaveBeenCalledWith(
160-tailscaleBin,
161-expect.arrayContaining(["serve", "--bg", "--yes", "3000"]),
162-expect.any(Object),
163-);
174+expectExecCall(exec, 1, tailscaleBin, ["serve", "--bg", "--yes", "3000"], {
175+maxBuffer: 200_000,
176+timeoutMs: 15_000,
177+});
164178});
165179166180it("disableTailscaleServe uses fallback", async () => {
@@ -172,12 +186,10 @@ describe("tailscale helpers", () => {
172186await disableTailscaleServe(exec as never);
173187174188expect(exec).toHaveBeenCalledTimes(2);
175-expect(exec).toHaveBeenNthCalledWith(
176-2,
177-"sudo",
178-expect.arrayContaining(["-n", tailscaleBin, "serve", "reset"]),
179-expect.any(Object),
180-);
189+expectExecCall(exec, 2, "sudo", ["-n", tailscaleBin, "serve", "reset"], {
190+maxBuffer: 200_000,
191+timeoutMs: 15_000,
192+});
181193});
182194183195it("ensureFunnel uses fallback for enabling", async () => {
@@ -196,23 +208,16 @@ describe("tailscale helpers", () => {
196208197209await ensureFunnel(8080, exec as never, runtime, prompt);
198210199-expect(exec).toHaveBeenNthCalledWith(
200-1,
201-tailscaleBin,
202-expect.arrayContaining(["funnel", "status", "--json"]),
203-);
204-expect(exec).toHaveBeenNthCalledWith(
205-2,
206-tailscaleBin,
207-expect.arrayContaining(["funnel", "--yes", "--bg", "8080"]),
208-expect.any(Object),
209-);
210-expect(exec).toHaveBeenNthCalledWith(
211-3,
212-"sudo",
213-expect.arrayContaining(["-n", tailscaleBin, "funnel", "--yes", "--bg", "8080"]),
214-expect.any(Object),
215-);
211+expect(exec).toHaveBeenCalledTimes(3);
212+expectExecCall(exec, 1, tailscaleBin, ["funnel", "status", "--json"]);
213+expectExecCall(exec, 2, tailscaleBin, ["funnel", "--yes", "--bg", "8080"], {
214+maxBuffer: 200_000,
215+timeoutMs: 15_000,
216+});
217+expectExecCall(exec, 3, "sudo", ["-n", tailscaleBin, "funnel", "--yes", "--bg", "8080"], {
218+maxBuffer: 200_000,
219+timeoutMs: 15_000,
220+});
216221});
217222218223it("enableTailscaleServe skips sudo on non-permission errors", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。