|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createMinerOpsActions } from "../../packages/loopover-miner/lib/miner-ops-actions"; |
| 3 | + |
| 4 | +// #9523: the store operations behind the miner's mutating MCP tools. Every seam is injected here, so these |
| 5 | +// assert the wiring — which store call each action makes, and that every store it opens is closed — without |
| 6 | +// touching disk. The governor gate in front of them is covered by miner-mcp-governor-gating.test.ts. |
| 7 | + |
| 8 | +function fakeQueue(overrides: Record<string, unknown> = {}) { |
| 9 | + const closed = { count: 0 }; |
| 10 | + const store = { |
| 11 | + reclaimStuckItem: vi.fn(() => ({ id: "entry" })), |
| 12 | + requeueItem: vi.fn(() => ({ id: "entry" })), |
| 13 | + close: vi.fn(() => { |
| 14 | + closed.count += 1; |
| 15 | + }), |
| 16 | + ...overrides, |
| 17 | + }; |
| 18 | + return { store, closed }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("releaseQueueItem", () => { |
| 22 | + it("reclaims the lease by the item's string identifier and closes the store", () => { |
| 23 | + const { store, closed } = fakeQueue(); |
| 24 | + const actions = createMinerOpsActions({ initPortfolioQueue: () => store as never }); |
| 25 | + expect(actions.releaseQueueItem({ repoFullName: "owner/repo", issueNumber: 12 })).toEqual({ released: true, entry: { id: "entry" } }); |
| 26 | + // The queue keys items by a STRING identifier; an issue number must be stringified, not passed raw. |
| 27 | + expect(store.reclaimStuckItem).toHaveBeenCalledWith("owner/repo", "12"); |
| 28 | + expect(closed.count).toBe(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it("reports released=false when the item was not held, and still closes", () => { |
| 32 | + const { store, closed } = fakeQueue({ reclaimStuckItem: vi.fn(() => null) }); |
| 33 | + const actions = createMinerOpsActions({ initPortfolioQueue: () => store as never }); |
| 34 | + expect(actions.releaseQueueItem({ repoFullName: "owner/repo", issueNumber: 1 })).toEqual({ released: false, entry: null }); |
| 35 | + expect(closed.count).toBe(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it("closes the store even when the operation throws", () => { |
| 39 | + const { store, closed } = fakeQueue({ |
| 40 | + reclaimStuckItem: vi.fn(() => { |
| 41 | + throw new Error("db locked"); |
| 42 | + }), |
| 43 | + }); |
| 44 | + const actions = createMinerOpsActions({ initPortfolioQueue: () => store as never }); |
| 45 | + expect(() => actions.releaseQueueItem({ repoFullName: "owner/repo", issueNumber: 1 })).toThrow("db locked"); |
| 46 | + expect(closed.count, "a thrown operation must not leak the handle").toBe(1); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("requeueQueueItem", () => { |
| 51 | + it("requeues by identifier and closes", () => { |
| 52 | + const { store, closed } = fakeQueue(); |
| 53 | + const actions = createMinerOpsActions({ initPortfolioQueue: () => store as never }); |
| 54 | + expect(actions.requeueQueueItem({ repoFullName: "owner/repo", issueNumber: 7 })).toEqual({ requeued: true, entry: { id: "entry" } }); |
| 55 | + expect(store.requeueItem).toHaveBeenCalledWith("owner/repo", "7"); |
| 56 | + expect(closed.count).toBe(1); |
| 57 | + }); |
| 58 | + |
| 59 | + it("reports requeued=false for an unknown item", () => { |
| 60 | + const { store } = fakeQueue({ requeueItem: vi.fn(() => null) }); |
| 61 | + const actions = createMinerOpsActions({ initPortfolioQueue: () => store as never }); |
| 62 | + expect(actions.requeueQueueItem({ repoFullName: "owner/repo", issueNumber: 7 })).toEqual({ requeued: false, entry: null }); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("releaseClaim", () => { |
| 67 | + it("releases the claim by NUMBER — the claim ledger keys by issue number, unlike the queue", () => { |
| 68 | + const close = vi.fn(); |
| 69 | + const releaseClaim = vi.fn(() => ({ status: "released" })); |
| 70 | + const actions = createMinerOpsActions({ openClaims: () => ({ releaseClaim, close }) as never }); |
| 71 | + expect(actions.releaseClaim({ repoFullName: "owner/repo", issueNumber: 3 })).toEqual({ released: true, entry: { status: "released" } }); |
| 72 | + expect(releaseClaim).toHaveBeenCalledWith("owner/repo", 3); |
| 73 | + expect(close).toHaveBeenCalledOnce(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("reports released=false when there was no claim", () => { |
| 77 | + const actions = createMinerOpsActions({ openClaims: () => ({ releaseClaim: () => null, close: vi.fn() }) as never }); |
| 78 | + expect(actions.releaseClaim({ repoFullName: "owner/repo", issueNumber: 3 })).toEqual({ released: false, entry: null }); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("decideDenyHook", () => { |
| 83 | + it("approves a proposal it can find in that repo", () => { |
| 84 | + const setProposalStatus = vi.fn(); |
| 85 | + const close = vi.fn(); |
| 86 | + const actions = createMinerOpsActions({ |
| 87 | + openDenyHooks: () => ({ listProposals: () => [{ id: "hook-1" }], setProposalStatus, close }) as never, |
| 88 | + }); |
| 89 | + expect(actions.decideDenyHook({ repoFullName: "owner/repo", hookId: "hook-1", decision: "approve" })).toEqual({ |
| 90 | + decided: true, |
| 91 | + hookId: "hook-1", |
| 92 | + status: "approved", |
| 93 | + }); |
| 94 | + expect(setProposalStatus).toHaveBeenCalledWith("owner/repo", "hook-1", "approved"); |
| 95 | + expect(close).toHaveBeenCalledOnce(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("maps reject to the rejected status", () => { |
| 99 | + const setProposalStatus = vi.fn(); |
| 100 | + const actions = createMinerOpsActions({ |
| 101 | + openDenyHooks: () => ({ listProposals: () => [{ id: "hook-2" }], setProposalStatus, close: vi.fn() }) as never, |
| 102 | + }); |
| 103 | + expect(actions.decideDenyHook({ repoFullName: "owner/repo", hookId: "hook-2", decision: "reject" })).toMatchObject({ status: "rejected" }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("reports notFound WITHOUT writing when the proposal is not in that repo", () => { |
| 107 | + const setProposalStatus = vi.fn(); |
| 108 | + const actions = createMinerOpsActions({ |
| 109 | + openDenyHooks: () => ({ listProposals: () => [{ id: "other" }], setProposalStatus, close: vi.fn() }) as never, |
| 110 | + }); |
| 111 | + expect(actions.decideDenyHook({ repoFullName: "owner/repo", hookId: "missing", decision: "approve" })).toEqual({ |
| 112 | + decided: false, |
| 113 | + notFound: true, |
| 114 | + hookId: "missing", |
| 115 | + }); |
| 116 | + expect(setProposalStatus, "an unknown proposal must not be written").not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe("runMigrations", () => { |
| 121 | + it("reports ok when every store migrated cleanly", () => { |
| 122 | + const actions = createMinerOpsActions({ |
| 123 | + migrate: () => [ |
| 124 | + { name: "a", ok: true, status: "migrated" }, |
| 125 | + { name: "b", ok: true, status: "up-to-date" }, |
| 126 | + ] as never, |
| 127 | + }); |
| 128 | + expect(actions.runMigrations()).toMatchObject({ ok: true }); |
| 129 | + }); |
| 130 | + |
| 131 | + it("reports ok=false when ANY store failed — a partial sweep is not a success", () => { |
| 132 | + const actions = createMinerOpsActions({ |
| 133 | + migrate: () => [ |
| 134 | + { name: "a", ok: true, status: "migrated" }, |
| 135 | + { name: "b", ok: false, status: "failed" }, |
| 136 | + ] as never, |
| 137 | + }); |
| 138 | + expect(actions.runMigrations()).toMatchObject({ ok: false }); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe("purgeRepo", () => { |
| 143 | + it("delegates to the CLI's own purge core, so both surfaces cover the same stores", () => { |
| 144 | + const purge = vi.fn(() => ({ outcome: "purged", totalPurged: 4 })); |
| 145 | + const actions = createMinerOpsActions({ purge: purge as never }); |
| 146 | + expect(actions.purgeRepo({ repoFullName: "owner/repo" })).toEqual({ outcome: "purged", totalPurged: 4 }); |
| 147 | + expect(purge).toHaveBeenCalledWith("owner/repo"); |
| 148 | + }); |
| 149 | +}); |
0 commit comments