9352f10d0d
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
249 lines
8.3 KiB
TypeScript
249 lines
8.3 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { spawnSync } from "node:child_process";
|
|
import { resolve } from "node:path";
|
|
|
|
const evaluator = resolve(import.meta.dir, "platform_infra_gitea_status_evaluator.py");
|
|
|
|
describe("Gitea source authority status evaluator", () => {
|
|
test("selects only the exact branch-derived immutable snapshot", () => {
|
|
const head = "b".repeat(40);
|
|
const result = evaluate({
|
|
action: "select-snapshot",
|
|
branchCommit: head,
|
|
snapshotPrefix: "refs/snapshots/source",
|
|
refs: {
|
|
"refs/snapshots/source/zzzz": "f".repeat(40),
|
|
[`refs/snapshots/source/${head}`]: head,
|
|
},
|
|
});
|
|
expect(result).toEqual({
|
|
expectedSnapshotRef: `refs/snapshots/source/${head}`,
|
|
snapshotRef: `refs/snapshots/source/${head}`,
|
|
snapshotCommit: head,
|
|
sourceBundleReady: true,
|
|
});
|
|
|
|
const missing = evaluate({
|
|
action: "select-snapshot",
|
|
branchCommit: head,
|
|
snapshotPrefix: "refs/snapshots/source",
|
|
refs: { "refs/snapshots/source/zzzz": "f".repeat(40) },
|
|
});
|
|
expect(missing.snapshotRef).toBeNull();
|
|
expect(missing.sourceBundleReady).toBe(false);
|
|
});
|
|
|
|
test("latest failed delivery cannot be hidden by an older successful bridge event", () => {
|
|
const head = "b".repeat(40);
|
|
const row = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: "new-delivery",
|
|
statusCode: 502,
|
|
bridgeEvents: [terminalEvent("old-delivery", head)],
|
|
inboxRecords: [committedInbox("old-delivery", head)],
|
|
});
|
|
expect(row.authorityMatches).toBe(true);
|
|
expect(row.deliveryAccepted).toBe(false);
|
|
expect(row.correlatedBridgeEvent).toBeNull();
|
|
expect(row.bridgeEventStale).toBe(true);
|
|
expect(row.staleReason).toBe("latest-push-delivery-failed-or-missing");
|
|
});
|
|
|
|
test("MATCH=false is always STALE=true and exact delivery correlation is repo isolated", () => {
|
|
const head = "c".repeat(40);
|
|
const old = "b".repeat(40);
|
|
const row = evaluateRow({
|
|
head,
|
|
branchCommit: old,
|
|
snapshotCommit: old,
|
|
deliveryId: "3830578865896423400",
|
|
statusCode: 202,
|
|
bridgeEvents: [
|
|
{ ...terminalEvent("3830578865896423400", head), repo: "other" },
|
|
terminalEvent("3830578865896423400", head),
|
|
],
|
|
});
|
|
expect(row.deliveryId).toBe("3830578865896423400");
|
|
expect(row.correlatedBridgeEvent.repo).toBe("fixture");
|
|
expect(row.authorityMatches).toBe(false);
|
|
expect(row.bridgeEventStale).toBe(true);
|
|
expect(row.staleReason).toBe("gitea-branch-behind-github");
|
|
});
|
|
|
|
test("a correlated terminal event and exact refs prove current authority", () => {
|
|
const head = "d".repeat(40);
|
|
const row = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: "delivery-ok",
|
|
statusCode: 202,
|
|
bridgeEvents: [terminalEvent("delivery-ok", head)],
|
|
});
|
|
expect(row.authorityMatches).toBe(true);
|
|
expect(row.bridgeEventStale).toBe(false);
|
|
expect(row.staleReason).toBeNull();
|
|
});
|
|
|
|
test("an out-of-order delivery may prove a newer authority without rolling it back", () => {
|
|
const head = "e".repeat(40);
|
|
const older = "d".repeat(40);
|
|
const event = terminalEvent("delivery-old", older);
|
|
event.authorityCommit = head;
|
|
event.disposition = "superseded-with-immutable-snapshot";
|
|
const row = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: "delivery-old",
|
|
statusCode: 202,
|
|
bridgeEvents: [event],
|
|
inboxRecords: [committedInbox("delivery-old", older, head, "superseded-with-immutable-snapshot")],
|
|
});
|
|
expect(row.authorityMatches).toBe(true);
|
|
expect(row.bridgeEventStale).toBe(false);
|
|
});
|
|
|
|
test("accepted, processing and failed inbox states never masquerade as committed refs", () => {
|
|
const head = "f".repeat(40);
|
|
for (const state of ["accepted", "processing", "failed"]) {
|
|
const row = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: `delivery-${state}`,
|
|
statusCode: 202,
|
|
bridgeEvents: [],
|
|
inboxRecords: [{ ...committedInbox(`delivery-${state}`, head), state, result: null }],
|
|
});
|
|
expect(row.authorityMatches).toBe(true);
|
|
expect(row.durableInboxState).toBe(state);
|
|
expect(row.bridgeEventStale).toBe(true);
|
|
expect(row.staleReason).toBe(`durable-inbox-${state}-not-committed`);
|
|
}
|
|
});
|
|
|
|
test("missing committed watermark or mismatched proof remains stale", () => {
|
|
const head = "a".repeat(40);
|
|
const missing = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: "delivery-missing",
|
|
statusCode: 202,
|
|
bridgeEvents: [],
|
|
inboxRecords: [],
|
|
});
|
|
expect(missing.staleReason).toBe("no-correlated-durable-inbox-record");
|
|
|
|
const wrong = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: "delivery-wrong",
|
|
statusCode: 202,
|
|
bridgeEvents: [],
|
|
inboxRecords: [committedInbox("delivery-wrong", "b".repeat(40))],
|
|
});
|
|
expect(wrong.staleReason).toBe("correlated-inbox-proof-head-mismatch");
|
|
});
|
|
|
|
test("a delivery older than the persistent journal is marked pre-durable-bootstrap without faking committed", () => {
|
|
const head = "9".repeat(40);
|
|
const row = evaluateRow({
|
|
head,
|
|
branchCommit: head,
|
|
snapshotCommit: head,
|
|
deliveryId: "bootstrap-delivery",
|
|
statusCode: 202,
|
|
deliveredAt: "2026-07-11T06:00:00.000Z",
|
|
journalCreatedAt: "2026-07-11T07:00:00.000Z",
|
|
bridgeEvents: [],
|
|
inboxRecords: [],
|
|
});
|
|
expect(row.authorityMatches).toBe(true);
|
|
expect(row.durableInboxState).toBe("pre-durable-bootstrap");
|
|
expect(row.preDurableBootstrap).toBe(true);
|
|
expect(row.durableInboxCommitted).toBe(false);
|
|
expect(row.bridgeEventStale).toBe(false);
|
|
expect(row.staleReason).toBeNull();
|
|
});
|
|
});
|
|
|
|
function evaluateRow(input: {
|
|
head: string;
|
|
branchCommit: string;
|
|
snapshotCommit: string;
|
|
deliveryId: string;
|
|
statusCode: number;
|
|
bridgeEvents: Array<Record<string, unknown>>;
|
|
inboxRecords?: Array<Record<string, unknown>>;
|
|
deliveredAt?: string;
|
|
journalCreatedAt?: string;
|
|
}): Record<string, any> {
|
|
return evaluate({
|
|
action: "evaluate-repository",
|
|
repo: {
|
|
key: "fixture",
|
|
upstream: { repository: "pikasTech/fixture", branch: "main" },
|
|
snapshot: { prefix: "refs/snapshots/source" },
|
|
},
|
|
hook: {
|
|
hookId: 1,
|
|
hookReady: true,
|
|
active: true,
|
|
status: 200,
|
|
latest: { deliveryId: input.deliveryId, event: "push", statusCode: input.statusCode, deliveredAt: input.deliveredAt },
|
|
latestPush: { deliveryId: input.deliveryId, event: "push", statusCode: input.statusCode, deliveredAt: input.deliveredAt },
|
|
},
|
|
head: { ok: true, sha: input.head },
|
|
mirror: {
|
|
branchCommit: input.branchCommit,
|
|
snapshotCommit: input.snapshotCommit,
|
|
snapshotRef: `refs/snapshots/source/${input.snapshotCommit}`,
|
|
},
|
|
bridgeEvents: input.bridgeEvents,
|
|
inboxRecords: input.inboxRecords ?? [committedInbox(input.deliveryId, input.head)],
|
|
journal: input.journalCreatedAt === undefined ? undefined : { version: 1, kind: "GiteaGithubSyncDurableInbox", createdAt: input.journalCreatedAt },
|
|
});
|
|
}
|
|
|
|
function committedInbox(deliveryId: string, sourceCommit: string, authorityCommit = sourceCommit, disposition = "atomic-source-authority-committed"): Record<string, unknown> {
|
|
return {
|
|
deliveryId,
|
|
repo: "fixture",
|
|
state: "committed",
|
|
result: {
|
|
sourceCommit,
|
|
authorityCommit,
|
|
snapshotRef: `refs/snapshots/source/${sourceCommit}`,
|
|
disposition,
|
|
},
|
|
};
|
|
}
|
|
|
|
function terminalEvent(deliveryId: string, sourceCommit: string): Record<string, unknown> {
|
|
return {
|
|
event: "github-to-gitea-sync",
|
|
repo: "fixture",
|
|
deliveryId,
|
|
ok: true,
|
|
terminal: true,
|
|
sourceCommit,
|
|
authorityCommit: sourceCommit,
|
|
disposition: "atomic-source-authority-committed",
|
|
};
|
|
}
|
|
|
|
function evaluate(payload: Record<string, unknown>): Record<string, any> {
|
|
const result = spawnSync("python3", [evaluator], {
|
|
input: JSON.stringify(payload),
|
|
encoding: "utf8",
|
|
env: { ...process.env, PYTHONDONTWRITEBYTECODE: "1" },
|
|
});
|
|
expect(result.status).toBe(0);
|
|
return JSON.parse(result.stdout);
|
|
}
|