openclaw/src/infra/tailscale.test.ts
google-labs-jules[bot] 29f0463f65 feat: use sudo for tailscale configuration commands
To avoid permission denied errors when modifying Tailscale configuration (serve/funnel),
we now prepend `sudo -n` to these commands. This ensures that if the user has appropriate
sudo privileges (specifically passwordless for these commands or generally), the operation
succeeds. If sudo fails (e.g. requires password non-interactively), it will throw an error
which is caught and logged as a warning, preserving existing behavior but attempting to escalate privileges first.

- Updated `ensureFunnel` to use `sudo -n` for the enabling step.
- Updated `enableTailscaleServe`, `disableTailscaleServe`, `enableTailscaleFunnel`, `disableTailscaleFunnel` to use `sudo -n`.
2026-01-24 00:17:20 +00:00

52 lines
1.9 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { ensureGoInstalled, ensureTailscaledInstalled, getTailnetHostname } from "./tailscale.js";
describe("tailscale helpers", () => {
it("parses DNS name from tailscale status", async () => {
const exec = vi.fn().mockResolvedValue({
stdout: JSON.stringify({
Self: { DNSName: "host.tailnet.ts.net.", TailscaleIPs: ["100.1.1.1"] },
}),
});
const host = await getTailnetHostname(exec);
expect(host).toBe("host.tailnet.ts.net");
});
it("falls back to IP when DNS missing", async () => {
const exec = vi.fn().mockResolvedValue({
stdout: JSON.stringify({ Self: { TailscaleIPs: ["100.2.2.2"] } }),
});
const host = await getTailnetHostname(exec);
expect(host).toBe("100.2.2.2");
});
it("ensureGoInstalled installs when missing and user agrees", async () => {
const exec = vi.fn().mockRejectedValueOnce(new Error("no go")).mockResolvedValue({}); // brew install go
const prompt = vi.fn().mockResolvedValue(true);
const runtime = {
error: vi.fn(),
log: vi.fn(),
exit: ((code: number) => {
throw new Error(`exit ${code}`);
}) as (code: number) => never,
};
await ensureGoInstalled(exec as never, prompt, runtime);
expect(exec).toHaveBeenCalledWith("brew", ["install", "go"]);
});
it("ensureTailscaledInstalled installs when missing and user agrees", async () => {
const exec = vi.fn().mockRejectedValueOnce(new Error("missing")).mockResolvedValue({});
const prompt = vi.fn().mockResolvedValue(true);
const runtime = {
error: vi.fn(),
log: vi.fn(),
exit: ((code: number) => {
throw new Error(`exit ${code}`);
}) as (code: number) => never,
};
await ensureTailscaledInstalled(exec as never, prompt, runtime);
expect(exec).toHaveBeenCalledWith("brew", ["install", "tailscale"]);
});
});