|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +// chat-action-registry → governor-chokepoint → governor-ledger → node:sqlite. jsdom/Vite cannot bundle that |
| 4 | +// builtin (#6837); keep a client-safe registry twin so the real registration module loads. Same pattern as |
| 5 | +// chat-governor-actions.test.tsx. |
| 6 | +vi.mock("../../../../packages/loopover-miner/lib/chat-action-registry.js", () => { |
| 7 | + const GOVERNOR_GATED = Symbol("loopover.chat-action.governor-gated"); |
| 8 | + |
| 9 | + function isGovernorGatedHandler(handler: unknown): boolean { |
| 10 | + return typeof handler === "function" && (handler as unknown as { [k: symbol]: unknown })[GOVERNOR_GATED] === true; |
| 11 | + } |
| 12 | + |
| 13 | + function governorGatedHandler( |
| 14 | + run: (request: unknown, gate: unknown) => unknown, |
| 15 | + options: { evaluateGate?: (input?: unknown) => { decision: { stage: string } } } = {}, |
| 16 | + ) { |
| 17 | + const evaluateGate = options.evaluateGate ?? (() => ({ decision: { stage: "allow" } })); |
| 18 | + const handler = async (request: { governorInput?: unknown }) => { |
| 19 | + const gate = evaluateGate(request?.governorInput); |
| 20 | + if (gate?.decision?.stage !== "allow") { |
| 21 | + return { ok: false, status: "gated", decision: gate?.decision ?? null }; |
| 22 | + } |
| 23 | + const result = await run(request, gate); |
| 24 | + return { ok: true, status: "executed", decision: gate.decision, result }; |
| 25 | + }; |
| 26 | + Object.defineProperty(handler, GOVERNOR_GATED, { value: true }); |
| 27 | + return handler; |
| 28 | + } |
| 29 | + |
| 30 | + function createChatActionRegistry() { |
| 31 | + const actions = new Map< |
| 32 | + string, |
| 33 | + { paramsValidator: (params: unknown) => boolean; handler: (request: unknown) => Promise<unknown> } |
| 34 | + >(); |
| 35 | + return { |
| 36 | + register( |
| 37 | + name: string, |
| 38 | + definition: { |
| 39 | + paramsValidator: (params: unknown) => boolean; |
| 40 | + handler: (request: unknown) => Promise<unknown>; |
| 41 | + }, |
| 42 | + ) { |
| 43 | + if (!isGovernorGatedHandler(definition.handler)) { |
| 44 | + throw new Error(`registerChatAction("${name}"): handler must be produced by governorGatedHandler()`); |
| 45 | + } |
| 46 | + actions.set(name, definition); |
| 47 | + return definition; |
| 48 | + }, |
| 49 | + get: (name: string) => actions.get(name), |
| 50 | + has: (name: string) => actions.has(name), |
| 51 | + names: () => [...actions.keys()], |
| 52 | + get size() { |
| 53 | + return actions.size; |
| 54 | + }, |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + createChatActionRegistry, |
| 60 | + governorGatedHandler, |
| 61 | + isGovernorGatedHandler, |
| 62 | + chatActionRegistry: createChatActionRegistry(), |
| 63 | + registerChatAction: () => { |
| 64 | + throw new Error("tests use an injected isolated registry"); |
| 65 | + }, |
| 66 | + }; |
| 67 | +}); |
| 68 | + |
| 69 | +import { |
| 70 | + CHAT_ACTION_DISPATCH_ENABLE_VALUE, |
| 71 | + CHAT_ACTION_DISPATCH_FLAG, |
| 72 | + dispatchChatAction, |
| 73 | +} from "../../../../packages/loopover-miner/lib/chat-action-dispatch.js"; |
| 74 | +import { createChatActionRegistry } from "../../../../packages/loopover-miner/lib/chat-action-registry.js"; |
| 75 | +import { |
| 76 | + ATTEMPT_CHAT_ACTION, |
| 77 | + DISCOVER_CHAT_ACTION, |
| 78 | + registerDiscoverAttemptChatActions, |
| 79 | +} from "./chat-discover-attempt-actions"; |
| 80 | +import { requestAttempt } from "./attempt"; |
| 81 | +import { requestDiscover } from "./discover"; |
| 82 | + |
| 83 | +const enabledEnv = { [CHAT_ACTION_DISPATCH_FLAG]: CHAT_ACTION_DISPATCH_ENABLE_VALUE }; |
| 84 | + |
| 85 | +const discoverOk = { ok: true as const, result: { enqueued: 2 }, exitCode: 0 }; |
| 86 | +const attemptOk = { ok: true as const, result: { outcome: "submitted" }, exitCode: 0 }; |
| 87 | + |
| 88 | +const validAttemptParams = { repoFullName: "acme/widgets", issueNumber: 12, minerLogin: "octocat" }; |
| 89 | + |
| 90 | +describe("registerDiscoverAttemptChatActions (#6837)", () => { |
| 91 | + it("registers discover and attempt into the supplied registry", () => { |
| 92 | + const registry = createChatActionRegistry(); |
| 93 | + registerDiscoverAttemptChatActions({ registry }); |
| 94 | + expect(registry.has(DISCOVER_CHAT_ACTION)).toBe(true); |
| 95 | + expect(registry.has(ATTEMPT_CHAT_ACTION)).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it("routes a dispatched discover only through requestDiscover, never attempt", async () => { |
| 99 | + const registry = createChatActionRegistry(); |
| 100 | + const requestDiscoverFn = vi.fn(async () => discoverOk); |
| 101 | + const requestAttemptFn = vi.fn(async () => attemptOk); |
| 102 | + registerDiscoverAttemptChatActions({ registry, requestDiscoverFn, requestAttemptFn }); |
| 103 | + |
| 104 | + const dispatch = await dispatchChatAction( |
| 105 | + { action: DISCOVER_CHAT_ACTION, params: { search: "typescript", dryRun: true } }, |
| 106 | + { env: enabledEnv, registry }, |
| 107 | + ); |
| 108 | + |
| 109 | + expect(dispatch).toMatchObject({ ok: true, status: "dispatched" }); |
| 110 | + expect(dispatch.result).toMatchObject({ status: "executed", result: discoverOk }); |
| 111 | + expect(requestDiscoverFn).toHaveBeenCalledWith({ search: "typescript", dryRun: true }); |
| 112 | + expect(requestAttemptFn).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("forwards nullish discover params to requestDiscover as an empty object", async () => { |
| 116 | + const registry = createChatActionRegistry(); |
| 117 | + const requestDiscoverFn = vi.fn(async () => discoverOk); |
| 118 | + registerDiscoverAttemptChatActions({ registry, requestDiscoverFn, requestAttemptFn: vi.fn(async () => attemptOk) }); |
| 119 | + |
| 120 | + await dispatchChatAction({ action: DISCOVER_CHAT_ACTION }, { env: enabledEnv, registry }); |
| 121 | + |
| 122 | + expect(requestDiscoverFn).toHaveBeenCalledWith({}); |
| 123 | + }); |
| 124 | + |
| 125 | + it("routes a dispatched attempt only through requestAttempt, never discover", async () => { |
| 126 | + const registry = createChatActionRegistry(); |
| 127 | + const requestDiscoverFn = vi.fn(async () => discoverOk); |
| 128 | + const requestAttemptFn = vi.fn(async () => attemptOk); |
| 129 | + registerDiscoverAttemptChatActions({ registry, requestDiscoverFn, requestAttemptFn }); |
| 130 | + |
| 131 | + const dispatch = await dispatchChatAction( |
| 132 | + { action: ATTEMPT_CHAT_ACTION, params: validAttemptParams }, |
| 133 | + { env: enabledEnv, registry }, |
| 134 | + ); |
| 135 | + |
| 136 | + expect(dispatch).toMatchObject({ ok: true, status: "dispatched" }); |
| 137 | + expect(dispatch.result).toMatchObject({ status: "executed", result: attemptOk }); |
| 138 | + expect(requestAttemptFn).toHaveBeenCalledWith(validAttemptParams); |
| 139 | + expect(requestDiscoverFn).not.toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it("rejects an attempt with missing required params before reaching the client", async () => { |
| 143 | + const registry = createChatActionRegistry(); |
| 144 | + const requestAttemptFn = vi.fn(async () => attemptOk); |
| 145 | + registerDiscoverAttemptChatActions({ |
| 146 | + registry, |
| 147 | + requestDiscoverFn: vi.fn(async () => discoverOk), |
| 148 | + requestAttemptFn, |
| 149 | + }); |
| 150 | + |
| 151 | + const dispatch = await dispatchChatAction( |
| 152 | + { action: ATTEMPT_CHAT_ACTION, params: { repoFullName: "acme/widgets" } }, |
| 153 | + { env: enabledEnv, registry }, |
| 154 | + ); |
| 155 | + |
| 156 | + expect(dispatch).toMatchObject({ ok: false, status: "invalid_params" }); |
| 157 | + expect(requestAttemptFn).not.toHaveBeenCalled(); |
| 158 | + }); |
| 159 | + |
| 160 | + it("does not run the client when the injected gate denies the write", async () => { |
| 161 | + const registry = createChatActionRegistry(); |
| 162 | + const requestAttemptFn = vi.fn(async () => attemptOk); |
| 163 | + registerDiscoverAttemptChatActions({ |
| 164 | + registry, |
| 165 | + requestDiscoverFn: vi.fn(async () => discoverOk), |
| 166 | + requestAttemptFn, |
| 167 | + evaluateGate: () => ({ decision: { stage: "block" } }), |
| 168 | + }); |
| 169 | + |
| 170 | + const dispatch = await dispatchChatAction( |
| 171 | + { action: ATTEMPT_CHAT_ACTION, params: validAttemptParams }, |
| 172 | + { env: enabledEnv, registry }, |
| 173 | + ); |
| 174 | + |
| 175 | + expect(dispatch).toMatchObject({ ok: true, status: "dispatched" }); |
| 176 | + expect(dispatch.result).toMatchObject({ status: "gated" }); |
| 177 | + expect(requestAttemptFn).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + |
| 180 | + it("regression: default wiring binds the exported requestDiscover/requestAttempt clients", async () => { |
| 181 | + const discoverModule = await import("./discover"); |
| 182 | + const attemptModule = await import("./attempt"); |
| 183 | + const discoverSpy = vi.spyOn(discoverModule, "requestDiscover").mockResolvedValue(discoverOk); |
| 184 | + const attemptSpy = vi.spyOn(attemptModule, "requestAttempt").mockResolvedValue(attemptOk); |
| 185 | + |
| 186 | + const registry = createChatActionRegistry(); |
| 187 | + registerDiscoverAttemptChatActions({ registry }); |
| 188 | + |
| 189 | + await dispatchChatAction( |
| 190 | + { action: DISCOVER_CHAT_ACTION, params: { search: "rust" } }, |
| 191 | + { env: enabledEnv, registry }, |
| 192 | + ); |
| 193 | + expect(discoverSpy).toHaveBeenCalledWith({ search: "rust" }); |
| 194 | + |
| 195 | + await dispatchChatAction( |
| 196 | + { action: ATTEMPT_CHAT_ACTION, params: validAttemptParams }, |
| 197 | + { env: enabledEnv, registry }, |
| 198 | + ); |
| 199 | + expect(attemptSpy).toHaveBeenCalledWith(validAttemptParams); |
| 200 | + |
| 201 | + discoverSpy.mockRestore(); |
| 202 | + attemptSpy.mockRestore(); |
| 203 | + }); |
| 204 | + |
| 205 | + it("defaults registration to the real ./discover and ./attempt module exports", () => { |
| 206 | + // Structural pin: the wire module's default path is `requestDiscover` / `requestAttempt`. |
| 207 | + expect(typeof requestDiscover).toBe("function"); |
| 208 | + expect(typeof requestAttempt).toBe("function"); |
| 209 | + }); |
| 210 | +}); |
| 211 | + |
| 212 | +describe("chatDiscoverAttemptActionsPlugin (#6837)", () => { |
| 213 | + it("registers discover/attempt into the shared registry on configureServer", async () => { |
| 214 | + const { chatDiscoverAttemptActionsPlugin } = await import("../../vite-chat-discover-attempt-actions"); |
| 215 | + const { chatActionRegistry } = await import("../../../../packages/loopover-miner/lib/chat-action-registry.js"); |
| 216 | + |
| 217 | + const plugin = chatDiscoverAttemptActionsPlugin(); |
| 218 | + expect(plugin.name).toBe("loopover-miner-chat-discover-attempt-actions"); |
| 219 | + |
| 220 | + (plugin.configureServer as () => void)(); |
| 221 | + |
| 222 | + // configureServer fires import().then(register); wait for the shared registration to settle. |
| 223 | + await vi.waitFor(() => { |
| 224 | + expect(chatActionRegistry.has(DISCOVER_CHAT_ACTION)).toBe(true); |
| 225 | + expect(chatActionRegistry.has(ATTEMPT_CHAT_ACTION)).toBe(true); |
| 226 | + }); |
| 227 | + }); |
| 228 | +}); |
0 commit comments