|
| 1 | +// Tests for node.pair.approve unknown requestId hint (fixes #94040). |
| 2 | +import { Command } from "commander"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +const mockCallPairApprovalGateway = vi.fn(); |
| 6 | +const mockParsePairingList = vi.fn(() => ({ pending: [], paired: [] })); |
| 7 | +const mockError = vi.fn(); |
| 8 | +const mockExit = vi.fn(); |
| 9 | +const mockWriteJson = vi.fn(); |
| 10 | +const mockWriteStdout = vi.fn(); |
| 11 | +const mockLog = vi.fn(); |
| 12 | + |
| 13 | +vi.mock("./rpc.js", () => ({ |
| 14 | + callGatewayCli: vi.fn(), |
| 15 | + callNodePairApprovalGatewayCli: (...args: unknown[]) => |
| 16 | + mockCallPairApprovalGateway(...args), |
| 17 | + nodesCallOpts: (cmd: Command) => cmd, |
| 18 | + resolveNodeId: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("./format.js", () => ({ |
| 22 | + parsePairingList: (...args: unknown[]) => mockParsePairingList(...args), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("../../runtime.js", () => ({ |
| 26 | + defaultRuntime: { |
| 27 | + log: (...args: unknown[]) => mockLog(...args), |
| 28 | + error: (...args: unknown[]) => mockError(...args), |
| 29 | + writeJson: (...args: unknown[]) => mockWriteJson(...args), |
| 30 | + writeStdout: (...args: unknown[]) => mockWriteStdout(...args), |
| 31 | + exit: (...args: unknown[]) => mockExit(...args), |
| 32 | + }, |
| 33 | +})); |
| 34 | + |
| 35 | +const { registerNodesPairingCommands } = |
| 36 | + await import("./register.pairing.js"); |
| 37 | + |
| 38 | +function createApproveProgram() { |
| 39 | + const program = new Command(); |
| 40 | + program.name("test"); |
| 41 | + program.exitOverride(); |
| 42 | + const nodes = program.command("nodes"); |
| 43 | + registerNodesPairingCommands(nodes); |
| 44 | + return program; |
| 45 | +} |
| 46 | + |
| 47 | +function createGatewayUnknownRequestIdError(): Error { |
| 48 | + const error = new Error("unknown requestId"); |
| 49 | + error.name = "GatewayClientRequestError"; |
| 50 | + (error as Error & { gatewayCode: string }).gatewayCode = "INVALID_REQUEST"; |
| 51 | + return error; |
| 52 | +} |
| 53 | + |
| 54 | +describe("nodes approve unknown requestId hint", () => { |
| 55 | + beforeEach(() => { |
| 56 | + mockCallPairApprovalGateway.mockReset(); |
| 57 | + mockParsePairingList.mockReset(); |
| 58 | + mockParsePairingList.mockReturnValue({ pending: [], paired: [] }); |
| 59 | + mockError.mockReset(); |
| 60 | + mockExit.mockReset(); |
| 61 | + mockWriteJson.mockReset(); |
| 62 | + mockWriteStdout.mockReset(); |
| 63 | + mockLog.mockReset(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("shows pending requestIds when approve fails with unknown requestId", async () => { |
| 67 | + // First call: resolveApproveScopesForRequest → node.pair.list |
| 68 | + mockCallPairApprovalGateway.mockResolvedValueOnce({}); |
| 69 | + mockParsePairingList.mockReturnValueOnce({ |
| 70 | + pending: [ |
| 71 | + { requestId: "req-abc", nodeId: "n1" }, |
| 72 | + { requestId: "req-def", nodeId: "n2" }, |
| 73 | + ], |
| 74 | + paired: [], |
| 75 | + }); |
| 76 | + // Second call: approve throws unknown requestId |
| 77 | + mockCallPairApprovalGateway.mockRejectedValueOnce( |
| 78 | + createGatewayUnknownRequestIdError(), |
| 79 | + ); |
| 80 | + // Third call: buildUnknownRequestIdHint → node.pair.list |
| 81 | + mockCallPairApprovalGateway.mockResolvedValueOnce({}); |
| 82 | + mockParsePairingList.mockReturnValueOnce({ |
| 83 | + pending: [ |
| 84 | + { requestId: "req-abc", nodeId: "n1" }, |
| 85 | + { requestId: "req-def", nodeId: "n2" }, |
| 86 | + ], |
| 87 | + paired: [], |
| 88 | + }); |
| 89 | + |
| 90 | + const program = createApproveProgram(); |
| 91 | + try { |
| 92 | + await program.parseAsync(["nodes", "approve", "stale-request"], { |
| 93 | + from: "user", |
| 94 | + }); |
| 95 | + } catch { |
| 96 | + // exitOverride converts exit(1) to CommanderError |
| 97 | + } |
| 98 | + |
| 99 | + expect(mockError).toHaveBeenCalled(); |
| 100 | + const errorCalls = mockError.mock.calls.flat() as string[]; |
| 101 | + const errorText = errorCalls.join(" "); |
| 102 | + expect(errorText).toContain( |
| 103 | + "Unknown node pairing requestId: stale-request", |
| 104 | + ); |
| 105 | + expect(errorText).toContain("req-abc"); |
| 106 | + expect(errorText).toContain("req-def"); |
| 107 | + expect(errorText).toContain("openclaw nodes pending"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("shows fallback when list fails during hint building", async () => { |
| 111 | + // resolveApproveScopesForRequest list fails silently |
| 112 | + mockCallPairApprovalGateway.mockRejectedValueOnce(new Error("gateway down")); |
| 113 | + // approve fails with unknown requestId |
| 114 | + mockCallPairApprovalGateway.mockRejectedValueOnce( |
| 115 | + createGatewayUnknownRequestIdError(), |
| 116 | + ); |
| 117 | + // buildUnknownRequestIdHint list also fails |
| 118 | + mockCallPairApprovalGateway.mockRejectedValueOnce(new Error("gateway down")); |
| 119 | + |
| 120 | + const program = createApproveProgram(); |
| 121 | + try { |
| 122 | + await program.parseAsync(["nodes", "approve", "orphan-request"], { |
| 123 | + from: "user", |
| 124 | + }); |
| 125 | + } catch { |
| 126 | + // exitOverride |
| 127 | + } |
| 128 | + |
| 129 | + expect(mockError).toHaveBeenCalled(); |
| 130 | + const errorCalls = mockError.mock.calls.flat() as string[]; |
| 131 | + const errorText = errorCalls.join(" "); |
| 132 | + expect(errorText).toContain( |
| 133 | + "Unknown node pairing requestId: orphan-request", |
| 134 | + ); |
| 135 | + expect(errorText).toContain( |
| 136 | + "No pending node pairing requests are currently visible", |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it("does not intercept non-unknown-requestId errors", async () => { |
| 141 | + mockCallPairApprovalGateway.mockResolvedValueOnce({}); |
| 142 | + mockParsePairingList.mockReturnValueOnce({ pending: [], paired: [] }); |
| 143 | + mockCallPairApprovalGateway.mockRejectedValueOnce( |
| 144 | + new Error("some other error"), |
| 145 | + ); |
| 146 | + |
| 147 | + const program = createApproveProgram(); |
| 148 | + try { |
| 149 | + await program.parseAsync(["nodes", "approve", "req-1"], { |
| 150 | + from: "user", |
| 151 | + }); |
| 152 | + } catch { |
| 153 | + // exitOverride |
| 154 | + } |
| 155 | + |
| 156 | + expect(mockError).toHaveBeenCalled(); |
| 157 | + const errorCalls = mockError.mock.calls.flat() as string[]; |
| 158 | + const errorText = errorCalls.join(" "); |
| 159 | + expect(errorText).toContain("some other error"); |
| 160 | + expect(errorText).not.toContain("openclaw nodes pending"); |
| 161 | + }); |
| 162 | +}); |
0 commit comments