Skip to content

Commit ddaf9ff

Browse files
committed
refactor(chain): keep the fake GitHub's pull and write helpers beside it
1 parent 5ef7442 commit ddaf9ff

14 files changed

Lines changed: 45 additions & 35 deletions

‎tests/chain/always.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// A held Do the inbox's "Always, in this repository" lets Triage do on its own.
22
import { expect, test } from "bun:test";
33
import { allowTriageAutomation, approveHeldDo } from "../../apps/web/src/lib/hub-api.ts";
4-
import type { Write } from "./github.ts";
5-
import { chainStack, ALWAYS, sha, pullOf, once } from "./chain.ts";
4+
import { chainStack, ALWAYS, once } from "./chain.ts";
5+
import { sha, pullOf, type Write } from "./github.ts";
66

77
const chain = chainStack();
88
const { github, pushed, until, settled, verdictsOf } = chain;

‎tests/chain/chain.ts‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createDB } from "@intx/db";
1010
import { listWorkflowDeployments, listWorkflowRuns, readWorkflowRunEvents } from "@intx/hub-client";
1111
import { parseTriageState, triageStateName, type PrTriageRow } from "@corbits/triage-contracts";
1212
import { saveAutomationMode } from "../../apps/web/src/lib/hub-api.ts";
13-
import { fakeGithub, OWNER, type Pull, type Write } from "./github.ts";
13+
import { fakeGithub, OWNER, type Pull } from "./github.ts";
1414
import { stubModel, type Answer, type Question } from "./model.ts";
1515
import { buildPortal, createDatabase, deliver, dropDatabase, listenerStarted, POLL_MS, setUpWorkspace, signUp, startHub, type Database, type Hub, type Workspace } from "./stack.ts";
1616

@@ -56,18 +56,6 @@ function decide(id: string, question: Question): Answer {
5656
return { type: "choice", choice: first!, probabilities: { [first!]: 0.96, ...Object.fromEntries(rest.map((option) => [option, 0.04 / rest.length])) }, confidence: 0.96 };
5757
}
5858

59-
export function sha(seed: string): string {
60-
return new Bun.CryptoHasher("sha1").update(seed).digest("hex");
61-
}
62-
63-
export function pullOf(number: number, head: string): Pull {
64-
return {
65-
number, title: "fix: widget sizing", body: "Fixes the widget's size.", author: "octocat", head,
66-
files: [{ filename: "src/widget.ts", status: "modified", additions: 1, deletions: 1, patch: "@@ -1 +1 @@\n-export const size = 1;\n+export const size = 2;" }],
67-
commits: ["fix: widget sizing"], labels: [], comments: [],
68-
};
69-
}
70-
7159
export type Claim = { status: "live" | "ended"; runId?: string; history: { head: string; runId: string; doneAt: string }[] };
7260

7361
/** This test file's stack, started before its tests and stopped after them; call it once, at the file's top level. `env` overrides the hub's. */
@@ -228,14 +216,6 @@ export function chainStack(env: Record<string, string> = {}) {
228216
};
229217
}
230218

231-
export function comments(writes: readonly Write[]): string[] {
232-
return writes.filter((write) => write.method === "POST" && write.path.endsWith("/comments")).map((write) => String((write.body as { body: string }).body));
233-
}
234-
235-
export function labelled(writes: readonly Write[]): string[] {
236-
return writes.filter((write) => write.method === "POST" && write.path.endsWith("/labels")).flatMap((write) => (write.body as { labels: string[] }).labels);
237-
}
238-
239219
/** Runs `scenario` once, on the first test that asks for it. */
240220
export function once<T>(scenario: () => Promise<T>): () => Promise<T> {
241221
let started: Promise<T> | undefined;

‎tests/chain/deterministic.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// A policy of only deterministic rules, settled on the pass the pull request opens.
22
import { expect, test } from "bun:test";
33
import { VERDICT_LABELS } from "@corbits/triage-contracts";
4-
import { chainStack, DETERMINISTIC, isLiveDeployment, sha, pullOf, comments, labelled, once } from "./chain.ts";
4+
import { chainStack, DETERMINISTIC, isLiveDeployment, once } from "./chain.ts";
5+
import { sha, pullOf, comments, labelled } from "./github.ts";
56

67
const { github, model, pushed, managers, settled, verdictsOf } = chainStack();
78

‎tests/chain/disconnected.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { expect, test } from "bun:test";
33
import { TRIAGE_LISTENERS, VERDICT_LABELS } from "@corbits/triage-contracts";
44
import { TIMINGS } from "./stack.ts";
5-
import { chainStack, SETTLE_MS, DISCONNECTED, isLiveDeployment, sha, pullOf, comments, labelled, once } from "./chain.ts";
5+
import { chainStack, SETTLE_MS, DISCONNECTED, isLiveDeployment, once } from "./chain.ts";
6+
import { sha, pullOf, comments, labelled } from "./github.ts";
67

78
const chain = chainStack();
89
const { github, pushed, until, rows, settled } = chain;

‎tests/chain/github.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ export type Pull = {
2424
};
2525
export type Write = { method: string; path: string; body: unknown };
2626

27+
export function sha(seed: string): string {
28+
return new Bun.CryptoHasher("sha1").update(seed).digest("hex");
29+
}
30+
31+
export function pullOf(number: number, head: string): Pull {
32+
return {
33+
number, title: "fix: widget sizing", body: "Fixes the widget's size.", author: "octocat", head,
34+
files: [{ filename: "src/widget.ts", status: "modified", additions: 1, deletions: 1, patch: "@@ -1 +1 @@\n-export const size = 1;\n+export const size = 2;" }],
35+
commits: ["fix: widget sizing"], labels: [], comments: [],
36+
};
37+
}
38+
39+
export function comments(writes: readonly Write[]): string[] {
40+
return writes.filter((write) => write.method === "POST" && write.path.endsWith("/comments")).map((write) => String((write.body as { body: string }).body));
41+
}
42+
43+
export function labelled(writes: readonly Write[]): string[] {
44+
return writes.filter((write) => write.method === "POST" && write.path.endsWith("/labels")).flatMap((write) => (write.body as { labels: string[] }).labels);
45+
}
46+
2747
/** A null `policy` is a repository with no policy file. */
2848
type Repo = { name: string; policy: string | null; pulls: Map<number, Pull>; checks: Map<string, CheckRun[]> };
2949

‎tests/chain/held.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { expect, test } from "bun:test";
33
import { TRIAGE_ROLE, VERDICT_LABELS } from "@corbits/triage-contracts";
44
import { approveHeldDo, saveAutomationMode, saveRepoSettings } from "../../apps/web/src/lib/hub-api.ts";
5-
import { chainStack, HELD, sha, pullOf, labelled, once } from "./chain.ts";
5+
import { chainStack, HELD, once } from "./chain.ts";
6+
import { sha, pullOf, labelled } from "./github.ts";
67

78
const chain = chainStack();
89
const { github, pushed, until, rows, settled, verdictsOf } = chain;

‎tests/chain/judged.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// A policy whose rules need the judge.
22
import { expect, test } from "bun:test";
33
import { VERDICT_LABELS } from "@corbits/triage-contracts";
4-
import { chainStack, JUDGED, sha, pullOf, comments, labelled, once } from "./chain.ts";
4+
import { chainStack, JUDGED, once } from "./chain.ts";
5+
import { sha, pullOf, comments, labelled } from "./github.ts";
56

67
const { github, model, pushed, settled, verdictsOf } = chainStack();
78

‎tests/chain/pushed.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Pushes while the manager is up and after it stopped.
22
import { expect, test } from "bun:test";
3-
import { chainStack, PUSHED, sha, pullOf, once } from "./chain.ts";
3+
import { chainStack, PUSHED, once } from "./chain.ts";
4+
import { sha, pullOf } from "./github.ts";
45

56
const { github, pushed, until, rows, claimOf, managers, settled } = chainStack();
67

‎tests/chain/reread.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { expect, test } from "bun:test";
33
import { VERDICT_LABELS } from "@corbits/triage-contracts";
44
import { TIMINGS } from "./stack.ts";
5-
import { chainStack, REREAD, SETTLE_MS, isLiveDeployment, sha, pullOf, once } from "./chain.ts";
5+
import { chainStack, REREAD, SETTLE_MS, isLiveDeployment, once } from "./chain.ts";
6+
import { sha, pullOf } from "./github.ts";
67

78
const { github, pushed, until, managers, settled, verdictsOf } = chainStack();
89

‎tests/chain/unrouted.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// A pull request opened while its listener is being replaced.
22
import { expect, test } from "bun:test";
33
import { TRIAGE_LISTENERS, VERDICT_LABELS } from "@corbits/triage-contracts";
4-
import { chainStack, UNROUTED, isLiveDeployment, sha, pullOf, comments, labelled, once } from "./chain.ts";
4+
import { chainStack, UNROUTED, isLiveDeployment, once } from "./chain.ts";
5+
import { sha, pullOf, comments, labelled } from "./github.ts";
56

67
// Longer than the scenario runs, so only the hub's short retry and its pass on a listener starting can mail the head.
78
const RECONCILE_INTERVAL_MS = 60 * 60_000;

0 commit comments

Comments
 (0)