forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailscale-status.test.ts
More file actions
85 lines (71 loc) · 3.01 KB
/
Copy pathtailscale-status.test.ts
File metadata and controls
85 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { describe, expect, it, vi } from "vitest";
import { resolveTailnetHostWithRunner } from "./tailscale-status.js";
describe("shared/tailscale-status", () => {
it("returns null when no runner is provided", async () => {
await expect(resolveTailnetHostWithRunner()).resolves.toBeNull();
});
it("prefers DNS names and trims trailing dots from status json", async () => {
const run = vi.fn().mockResolvedValue({
code: 0,
stdout: 'noise\n{"Self":{"DNSName":"mac.tail123.ts.net.","TailscaleIPs":["100.64.0.8"]}}',
});
await expect(resolveTailnetHostWithRunner(run)).resolves.toBe("mac.tail123.ts.net");
expect(run).toHaveBeenCalledWith(["tailscale", "status", "--json"], { timeoutMs: 5000 });
});
it("falls back across command candidates and then to the first tailscale ip", async () => {
const run = vi.fn().mockRejectedValueOnce(new Error("missing binary")).mockResolvedValueOnce({
code: 0,
stdout: '{"Self":{"TailscaleIPs":["100.64.0.9","fd7a::1"]}}',
});
await expect(resolveTailnetHostWithRunner(run)).resolves.toBe("100.64.0.9");
expect(run).toHaveBeenNthCalledWith(
2,
["/Applications/Tailscale.app/Contents/MacOS/Tailscale", "status", "--json"],
{
timeoutMs: 5000,
},
);
});
it("falls back to the first tailscale ip when DNSName is blank", async () => {
const run = vi.fn().mockResolvedValue({
code: 0,
stdout: '{"Self":{"DNSName":"","TailscaleIPs":["100.64.0.10","fd7a::2"]}}',
});
await expect(resolveTailnetHostWithRunner(run)).resolves.toBe("100.64.0.10");
});
it("continues to later command candidates when earlier output has no usable host", async () => {
const run = vi
.fn()
.mockResolvedValueOnce({ code: 0, stdout: '{"Self":{}}' })
.mockResolvedValueOnce({
code: 0,
stdout: '{"Self":{"DNSName":"backup.tail.ts.net."}}',
});
await expect(resolveTailnetHostWithRunner(run)).resolves.toBe("backup.tail.ts.net");
expect(run).toHaveBeenCalledTimes(2);
});
it("continues when the first candidate returns success but malformed Self data", async () => {
const run = vi
.fn()
.mockResolvedValueOnce({ code: 0, stdout: '{"Self":"bad"}' })
.mockResolvedValueOnce({
code: 0,
stdout: 'prefix {"Self":{"TailscaleIPs":["100.64.0.11"]}} suffix',
});
await expect(resolveTailnetHostWithRunner(run)).resolves.toBe("100.64.0.11");
expect(run).toHaveBeenCalledTimes(2);
});
it("returns null for non-zero exits, blank output, or invalid json", async () => {
const run = vi
.fn()
.mockResolvedValueOnce({ code: null, stdout: "boom" })
.mockResolvedValueOnce({ code: 1, stdout: "boom" })
.mockResolvedValueOnce({ code: 0, stdout: " " });
await expect(resolveTailnetHostWithRunner(run)).resolves.toBeNull();
const invalid = vi.fn().mockResolvedValue({
code: 0,
stdout: "not-json",
});
await expect(resolveTailnetHostWithRunner(invalid)).resolves.toBeNull();
});
});