import { describe, expect, it, vi } from "vitest"; import { assertDeliveryWriteAllowed, canRepairFeatureDelivery } from "../src/services/featureDeliveryFlow.js"; import type { FeatureDeliveryDto } from "../src/repositories/featureDeliveryRepository.js"; import type { GiteaClient } from "../src/integrations/gitea/client.js"; describe("repairing a delivery after PR validation fails", () => { const repository = { owner: "owner", repository: "repo", defaultBranch: "main" }; const delivery = { approvalState: "approved", spec: { status: "pull_request_open", branch: "chatgpt/existing", pullRequest: { number: 12 } } } as FeatureDeliveryDto; it("allows repair on the existing open PR, while terminal deliveries stay immutable", async () => { const getPullRequest = vi.fn().mockResolvedValue({ state: "open", merged: false, head: { ref: "chatgpt/existing" }, base: { ref: "main" } }); await expect(assertDeliveryWriteAllowed(delivery, "chatgpt/existing", { getPullRequest } as unknown as GiteaClient, repository)).resolves.toBeUndefined(); expect(getPullRequest).toHaveBeenCalledWith("owner", "repo", 12); expect(canRepairFeatureDelivery("merged")).toBe(false); expect(canRepairFeatureDelivery("failed")).toBe(false); }); it("refuses writes if Gitea merged the PR before ProjectBrain saw its webhook", async () => { const getPullRequest = vi.fn().mockResolvedValue({ state: "closed", merged: true }); await expect(assertDeliveryWriteAllowed(delivery, "chatgpt/existing", { getPullRequest } as unknown as GiteaClient, repository)).rejects.toThrow("FEATURE_DELIVERY_PULL_REQUEST_NOT_WRITABLE"); }); });