mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 19:38:28 +00:00
refactor(test): dedupe command config and model test fixtures
This commit is contained in:
@@ -55,81 +55,71 @@ function makeRuntime(): RuntimeEnv {
|
||||
};
|
||||
}
|
||||
|
||||
async function runTrustedProxyPrompt(textQueue: Array<string | undefined>) {
|
||||
async function runGatewayPrompt(params: {
|
||||
selectQueue: string[];
|
||||
textQueue: Array<string | undefined>;
|
||||
randomToken?: string;
|
||||
confirmResult?: boolean;
|
||||
authConfigFactory?: (input: Record<string, unknown>) => Record<string, unknown>;
|
||||
}) {
|
||||
vi.clearAllMocks();
|
||||
mocks.resolveGatewayPort.mockReturnValue(18789);
|
||||
const selectQueue = ["loopback", "trusted-proxy", "off"];
|
||||
mocks.select.mockImplementation(async () => selectQueue.shift());
|
||||
mocks.text.mockImplementation(async () => textQueue.shift());
|
||||
mocks.buildGatewayAuthConfig.mockImplementation(({ mode, trustedProxy }) => ({
|
||||
mode,
|
||||
trustedProxy,
|
||||
}));
|
||||
mocks.select.mockImplementation(async () => params.selectQueue.shift());
|
||||
mocks.text.mockImplementation(async () => params.textQueue.shift());
|
||||
mocks.randomToken.mockReturnValue(params.randomToken ?? "generated-token");
|
||||
mocks.confirm.mockResolvedValue(params.confirmResult ?? true);
|
||||
mocks.buildGatewayAuthConfig.mockImplementation((input) =>
|
||||
params.authConfigFactory ? params.authConfigFactory(input as Record<string, unknown>) : input,
|
||||
);
|
||||
|
||||
const result = await promptGatewayConfig({}, makeRuntime());
|
||||
const call = mocks.buildGatewayAuthConfig.mock.calls[0]?.[0];
|
||||
return { result, call };
|
||||
}
|
||||
|
||||
async function runTrustedProxyPrompt(params: {
|
||||
textQueue: Array<string | undefined>;
|
||||
tailscaleMode?: "off" | "serve";
|
||||
}) {
|
||||
return runGatewayPrompt({
|
||||
selectQueue: ["loopback", "trusted-proxy", params.tailscaleMode ?? "off"],
|
||||
textQueue: params.textQueue,
|
||||
authConfigFactory: ({ mode, trustedProxy }) => ({ mode, trustedProxy }),
|
||||
});
|
||||
}
|
||||
|
||||
describe("promptGatewayConfig", () => {
|
||||
it("generates a token when the prompt returns undefined", async () => {
|
||||
mocks.resolveGatewayPort.mockReturnValue(18789);
|
||||
const selectQueue = ["loopback", "token", "off"];
|
||||
mocks.select.mockImplementation(async () => selectQueue.shift());
|
||||
const textQueue = ["18789", undefined];
|
||||
mocks.text.mockImplementation(async () => textQueue.shift());
|
||||
mocks.randomToken.mockReturnValue("generated-token");
|
||||
mocks.buildGatewayAuthConfig.mockImplementation(({ mode, token, password }) => ({
|
||||
mode,
|
||||
token,
|
||||
password,
|
||||
}));
|
||||
|
||||
const runtime: RuntimeEnv = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
};
|
||||
|
||||
const result = await promptGatewayConfig({}, runtime);
|
||||
const { result } = await runGatewayPrompt({
|
||||
selectQueue: ["loopback", "token", "off"],
|
||||
textQueue: ["18789", undefined],
|
||||
randomToken: "generated-token",
|
||||
authConfigFactory: ({ mode, token, password }) => ({ mode, token, password }),
|
||||
});
|
||||
expect(result.token).toBe("generated-token");
|
||||
});
|
||||
|
||||
it("does not set password to literal 'undefined' when prompt returns undefined", async () => {
|
||||
vi.clearAllMocks();
|
||||
mocks.resolveGatewayPort.mockReturnValue(18789);
|
||||
// Flow: loopback bind → password auth → tailscale off
|
||||
const selectQueue = ["loopback", "password", "off"];
|
||||
mocks.select.mockImplementation(async () => selectQueue.shift());
|
||||
// Port prompt → OK, then password prompt → returns undefined (simulating prompter edge case)
|
||||
const textQueue = ["18789", undefined];
|
||||
mocks.text.mockImplementation(async () => textQueue.shift());
|
||||
mocks.randomToken.mockReturnValue("unused");
|
||||
mocks.buildGatewayAuthConfig.mockImplementation(({ mode, token, password }) => ({
|
||||
mode,
|
||||
token,
|
||||
password,
|
||||
}));
|
||||
|
||||
const runtime: RuntimeEnv = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
};
|
||||
|
||||
await promptGatewayConfig({}, runtime);
|
||||
const call = mocks.buildGatewayAuthConfig.mock.calls[0]?.[0];
|
||||
const { call } = await runGatewayPrompt({
|
||||
selectQueue: ["loopback", "password", "off"],
|
||||
textQueue: ["18789", undefined],
|
||||
randomToken: "unused",
|
||||
authConfigFactory: ({ mode, token, password }) => ({ mode, token, password }),
|
||||
});
|
||||
expect(call?.password).not.toBe("undefined");
|
||||
expect(call?.password).toBe("");
|
||||
});
|
||||
|
||||
it("prompts for trusted-proxy configuration when trusted-proxy mode selected", async () => {
|
||||
const { result, call } = await runTrustedProxyPrompt([
|
||||
"18789",
|
||||
"x-forwarded-user",
|
||||
"x-forwarded-proto,x-forwarded-host",
|
||||
"nick@example.com",
|
||||
"10.0.1.10,192.168.1.5",
|
||||
]);
|
||||
const { result, call } = await runTrustedProxyPrompt({
|
||||
textQueue: [
|
||||
"18789",
|
||||
"x-forwarded-user",
|
||||
"x-forwarded-proto,x-forwarded-host",
|
||||
"nick@example.com",
|
||||
"10.0.1.10,192.168.1.5",
|
||||
],
|
||||
});
|
||||
|
||||
expect(call?.mode).toBe("trusted-proxy");
|
||||
expect(call?.trustedProxy).toEqual({
|
||||
@@ -142,13 +132,9 @@ describe("promptGatewayConfig", () => {
|
||||
});
|
||||
|
||||
it("handles trusted-proxy with no optional fields", async () => {
|
||||
const { result, call } = await runTrustedProxyPrompt([
|
||||
"18789",
|
||||
"x-remote-user",
|
||||
"",
|
||||
"",
|
||||
"10.0.0.1",
|
||||
]);
|
||||
const { result, call } = await runTrustedProxyPrompt({
|
||||
textQueue: ["18789", "x-remote-user", "", "", "10.0.0.1"],
|
||||
});
|
||||
|
||||
expect(call?.mode).toBe("trusted-proxy");
|
||||
expect(call?.trustedProxy).toEqual({
|
||||
@@ -160,25 +146,10 @@ describe("promptGatewayConfig", () => {
|
||||
});
|
||||
|
||||
it("forces tailscale off when trusted-proxy is selected", async () => {
|
||||
vi.clearAllMocks();
|
||||
mocks.resolveGatewayPort.mockReturnValue(18789);
|
||||
const selectQueue = ["loopback", "trusted-proxy", "serve"];
|
||||
mocks.select.mockImplementation(async () => selectQueue.shift());
|
||||
const textQueue = ["18789", "x-forwarded-user", "", "", "10.0.0.1"];
|
||||
mocks.text.mockImplementation(async () => textQueue.shift());
|
||||
mocks.confirm.mockResolvedValue(true);
|
||||
mocks.buildGatewayAuthConfig.mockImplementation(({ mode, trustedProxy }) => ({
|
||||
mode,
|
||||
trustedProxy,
|
||||
}));
|
||||
|
||||
const runtime: RuntimeEnv = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
};
|
||||
|
||||
const result = await promptGatewayConfig({}, runtime);
|
||||
const { result } = await runTrustedProxyPrompt({
|
||||
tailscaleMode: "serve",
|
||||
textQueue: ["18789", "x-forwarded-user", "", "", "10.0.0.1"],
|
||||
});
|
||||
expect(result.config.gateway?.bind).toBe("lan");
|
||||
expect(result.config.gateway?.tailscale?.mode).toBe("off");
|
||||
expect(result.config.gateway?.tailscale?.resetOnExit).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user