openclaw/src/shared/gateway-bind-url.test.ts
2026-03-13 20:21:43 +00:00

95 lines
2.3 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { resolveGatewayBindUrl } from "./gateway-bind-url.js";
describe("shared/gateway-bind-url", () => {
it("returns null for loopback/default binds", () => {
expect(
resolveGatewayBindUrl({
scheme: "ws",
port: 18789,
pickTailnetHost: () => "100.64.0.1",
pickLanHost: () => "192.168.1.2",
}),
).toBeNull();
});
it("resolves custom binds only when custom host is present after trimming", () => {
expect(
resolveGatewayBindUrl({
bind: "custom",
customBindHost: " gateway.local ",
scheme: "wss",
port: 443,
pickTailnetHost: vi.fn(),
pickLanHost: vi.fn(),
}),
).toEqual({
url: "wss://gateway.local:443",
source: "gateway.bind=custom",
});
expect(
resolveGatewayBindUrl({
bind: "custom",
customBindHost: " ",
scheme: "ws",
port: 18789,
pickTailnetHost: vi.fn(),
pickLanHost: vi.fn(),
}),
).toEqual({
error: "gateway.bind=custom requires gateway.customBindHost.",
});
});
it("resolves tailnet and lan binds or returns clear errors", () => {
expect(
resolveGatewayBindUrl({
bind: "tailnet",
scheme: "ws",
port: 18789,
pickTailnetHost: () => "100.64.0.1",
pickLanHost: vi.fn(),
}),
).toEqual({
url: "ws://100.64.0.1:18789",
source: "gateway.bind=tailnet",
});
expect(
resolveGatewayBindUrl({
bind: "tailnet",
scheme: "ws",
port: 18789,
pickTailnetHost: () => null,
pickLanHost: vi.fn(),
}),
).toEqual({
error: "gateway.bind=tailnet set, but no tailnet IP was found.",
});
expect(
resolveGatewayBindUrl({
bind: "lan",
scheme: "wss",
port: 8443,
pickTailnetHost: vi.fn(),
pickLanHost: () => "192.168.1.2",
}),
).toEqual({
url: "wss://192.168.1.2:8443",
source: "gateway.bind=lan",
});
expect(
resolveGatewayBindUrl({
bind: "lan",
scheme: "ws",
port: 18789,
pickTailnetHost: vi.fn(),
pickLanHost: () => null,
}),
).toEqual({
error: "gateway.bind=lan set, but no private LAN IP was found.",
});
});
});