|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + getLatestAdvisoryForPullRequest, |
| 4 | + upsertInstallation, |
| 5 | + upsertPullRequestFromGitHub, |
| 6 | + upsertRepositoryFromGitHub, |
| 7 | + upsertRepositorySettings, |
| 8 | +} from "../../src/db/repositories"; |
| 9 | +import { reReviewStoredPullRequest } from "../../src/queue/processors"; |
| 10 | +import { normalizeRegistryPayload } from "../../src/registry/normalize"; |
| 11 | +import { persistRegistrySnapshot } from "../../src/registry/sync"; |
| 12 | +import { asCloudEnv, createTestEnv } from "../helpers/d1"; |
| 13 | +import { upsertRepoFocusManifest } from "../../src/signals/focus-manifest-loader"; |
| 14 | +import { generatePrivateKeyPem } from "../helpers/github-app-key"; |
| 15 | + |
| 16 | +// #10168 end-to-end: the supersession split, driven through a real gate-evaluating entry point rather than |
| 17 | +// against buildPullRequestAdvisory directly, so the whole seam is proven -- the linked-issue verification pass |
| 18 | +// carrying closure facts out, the bounded merged-rival read, the flag, and the finding the contributor sees. |
| 19 | +// |
| 20 | +// The collision reproduced here is the real one from the Orb: |
| 21 | +// PR 8886 opened 09:22:36 citing issue 8829 (the issue was OPEN at that moment) |
| 22 | +// PR 8881 merged 09:30:24 citing the same issue |
| 23 | +// issue 8829 closed 09:30:25, one second later, as a side effect of that merge |
| 24 | + |
| 25 | +const REPO = "JSONbored/gittensory"; |
| 26 | +const PR_CREATED = "2026-07-31T09:22:36Z"; |
| 27 | +const RIVAL_MERGED = "2026-07-31T09:30:24Z"; |
| 28 | +const ISSUE_CLOSED = "2026-07-31T09:30:25Z"; |
| 29 | + |
| 30 | +async function seedRepo(env: ReturnType<typeof createTestEnv>) { |
| 31 | + await persistRegistrySnapshot( |
| 32 | + asCloudEnv(env), |
| 33 | + normalizeRegistryPayload({ [REPO]: { emission_share: 0.01, issue_discovery_share: 0 } }, { kind: "raw-github", url: "https://example.test" }, "2026-05-23T00:00:00.000Z"), |
| 34 | + ); |
| 35 | + await upsertInstallation(env, { |
| 36 | + action: "created", |
| 37 | + installation: { id: 123, account: { login: "JSONbored", id: 1, type: "User" }, target_type: "User", repository_selection: "selected", permissions: {}, events: [] }, |
| 38 | + }); |
| 39 | + await upsertRepositoryFromGitHub(env, { name: "gittensory", full_name: REPO, private: false, owner: { login: "JSONbored" } }, 123); |
| 40 | + await upsertRepositorySettings(env, { |
| 41 | + repoFullName: REPO, |
| 42 | + autoLabelEnabled: false, |
| 43 | + gatePack: "oss-anti-slop", |
| 44 | + // Only `label` acts, so maintenance never attempts a live merge/approve. |
| 45 | + autonomy: { label: "auto" }, |
| 46 | + }); |
| 47 | + // linkedIssueGateMode is CONFIG-AS-CODE only (loopover#6442) -- upsertRepositorySettings silently drops it, |
| 48 | + // so it has to arrive through the manifest's `gate:` block. "block" is the only mode in which the |
| 49 | + // open-reference check runs at all, and that is the pass the closure facts ride out of. |
| 50 | + await upsertRepoFocusManifest(env, REPO, { |
| 51 | + gate: { linkedIssue: "block" }, |
| 52 | + settings: { commentMode: "all_prs", publicSurface: "comment_only", checkRunMode: "off", reviewCheckMode: "required", aiReviewMode: "off" }, |
| 53 | + }); |
| 54 | + // The merged rival, and the PR it superseded. |
| 55 | + await upsertPullRequestFromGitHub(env, REPO, { |
| 56 | + number: 8881, title: "rival", state: "closed", user: { login: "rival" }, head: { sha: "shaRival" }, |
| 57 | + labels: [], body: "Closes #8829", created_at: "2026-07-31T09:04:07Z", merged_at: RIVAL_MERGED, |
| 58 | + } as never); |
| 59 | + await upsertPullRequestFromGitHub(env, REPO, { |
| 60 | + number: 8886, title: "Fix the thing", state: "open", user: { login: "contributor" }, author_association: "CONTRIBUTOR", |
| 61 | + head: { sha: "sha8886" }, base: { ref: "main" }, labels: [], body: "Closes #8829", created_at: PR_CREATED, |
| 62 | + } as never); |
| 63 | +} |
| 64 | + |
| 65 | +function stubGitHub() { |
| 66 | + vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => { |
| 67 | + const url = input.toString(); |
| 68 | + const method = init?.method ?? "GET"; |
| 69 | + if (url.includes("/access_tokens")) return Response.json({ token: "fake-installation-token" }); |
| 70 | + if (url.includes("/pulls/8886/files")) return Response.json([{ filename: "src/a.ts", status: "modified", additions: 1, deletions: 0, changes: 1, patch: "@@\n+export const ok = true;" }]); |
| 71 | + if (url.endsWith("/pulls/8886")) { |
| 72 | + return Response.json({ number: 8886, title: "Fix the thing", state: "open", user: { login: "contributor" }, head: { sha: "sha8886" }, labels: [], body: "Closes #8829", created_at: PR_CREATED, mergeable_state: "dirty" }); |
| 73 | + } |
| 74 | + if (url.includes("/commits/sha8886/check-runs")) return Response.json({ total_count: 1, check_runs: [{ name: "test", status: "completed", conclusion: "success", app: { slug: "github-actions" } }] }); |
| 75 | + if (url.includes("/commits/sha8886/status")) return Response.json({ state: "success", statuses: [] }); |
| 76 | + // The issue the contributor correctly linked -- closed, by the rival's merge, one second after it landed. |
| 77 | + if (url.includes("/issues/8829")) return Response.json({ number: 8829, title: "The issue", state: "closed", closed_at: ISSUE_CLOSED, labels: [], assignees: [], user: { login: "reporter" } }); |
| 78 | + if (url.includes("/issues/8886/comments") && (method === "POST" || method === "PATCH")) return Response.json({ id: 1 }, { status: 201 }); |
| 79 | + if (url.includes("/issues/8886/comments")) return Response.json([]); |
| 80 | + if (url.includes("/branches/")) return Response.json({ protected: false, protection: { required_status_checks: { contexts: [] } } }); |
| 81 | + return Response.json({}); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +async function runReview(supersededCloseFlag: string | undefined) { |
| 86 | + const env = createTestEnv({ |
| 87 | + GITHUB_APP_PRIVATE_KEY: await generatePrivateKeyPem(), |
| 88 | + ...(supersededCloseFlag === undefined ? {} : { LOOPOVER_SUPERSEDED_CLOSE: supersededCloseFlag }), |
| 89 | + }); |
| 90 | + await seedRepo(env); |
| 91 | + stubGitHub(); |
| 92 | + await reReviewStoredPullRequest(env, "superseded-wire", 123, REPO, 8886); |
| 93 | + const advisory = await getLatestAdvisoryForPullRequest(env, REPO, 8886); |
| 94 | + return (advisory?.findings ?? []).map((finding) => finding.code); |
| 95 | +} |
| 96 | + |
| 97 | +describe("superseded linked issue, wired end to end (#10168)", () => { |
| 98 | + afterEach(() => { |
| 99 | + vi.unstubAllGlobals(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("reports the supersession instead of 'No linked issue detected' once the flag is on", async () => { |
| 103 | + const codes = await runReview("true"); |
| 104 | + expect(codes).toContain("linked_issue_superseded"); |
| 105 | + expect(codes).not.toContain("missing_linked_issue"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("is byte-identical to today's behaviour while the flag is off", async () => { |
| 109 | + // The whole feature ships dark: same finding, same message, same disposition as before it existed. |
| 110 | + for (const flag of [undefined, "false"]) { |
| 111 | + const codes = await runReview(flag); |
| 112 | + expect(codes, String(flag)).toContain("missing_linked_issue"); |
| 113 | + expect(codes, String(flag)).not.toContain("linked_issue_superseded"); |
| 114 | + } |
| 115 | + }); |
| 116 | +}); |
0 commit comments