import { beforeEach, describe, expect, mock, test } from "bun:test"; import { fakeD1 } from "../metering/fake-d1.test-helper"; import { memoryScopes } from "../scoped-db/testing"; import { seedJob, seedRow, seedRun } from "./ci-fixtures.test-helper"; let d1 = fakeD1(); let scopes = memoryScopes(); mock.module("cloudflare:workers", () => ({ DurableObject: class {}, env: { get DB() { return d1.binding; }, PUBLIC_URL: "https://forge.test", }, waitUntil: () => undefined, })); mock.module("../scoped-db", () => ({ repoDb: (id: string) => scopes.repoDb(id), accountDb: (id: string) => scopes.accountDb(id), stmt: scopes.module.stmt, })); const { settleDeployment } = await import("./deployments"); function deployment(): { status: string; url: string | null; updated_at: number } { return scopes .raw("repo", "r1") .query("SELECT status, url, updated_at FROM deployments WHERE job_id = 'j1'") .get() as never; } beforeEach(() => { d1 = fakeD1(); scopes = memoryScopes(); d1.db.exec("INSERT INTO scope_locators VALUES ('workflow_job', 'j1', 'r1')"); const repo = scopes.raw("repo", "r1"); seedRun(repo, { id: "w1", repo_id: "r1" }); seedJob(repo, { id: "j1", run_id: "w1", repo_id: "r1" }); seedRow(repo, "environments", { id: "e1", repo_id: "r1", name: "prod", created_at: 0 }); seedRow(repo, "deployments", { id: "d1", repo_id: "r1", environment_id: "e1", run_id: "w1", job_id: "j1", ref: "refs/heads/main", sha: "a", status: "in_progress", created_at: 0, updated_at: 0, }); }); describe("settleDeployment under duplicate completions", () => { test("a repeat or late call with another conclusion leaves the settled deployment alone", async () => { await settleDeployment("j1", "cancelled"); const settled = deployment(); expect(settled.status).toBe("cancelled"); scopes .raw("repo", "r1") .exec(`UPDATE workflow_jobs SET outputs = '{"__gitfor_environment_url":"https://late.example"}'`); await settleDeployment("j1", "success"); await settleDeployment("j1", "cancelled"); expect(deployment()).toEqual(settled); }); });