CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | /**
* Block R3 — Fast-lane auto-merge (PR-create / PR-head-update event path) tests.
*
* Drives `tryAutoMergeNow` through its dependency-injection seam so no DB
* or external module is touched. The lib is loaded via spread-from-real
* import so an `afterAll` can no-op restore neighbouring suites — same K1
* pattern other tests use, even though no `mock.module` override is needed
* for this suite (the public API exposes a `deps` injection point).
*
* Covers the R3 contract:
* - waits for an AI-review comment when none exists yet (polls multiple times)
* - times out after `waitForAiReviewMs` without merging
* - on green decision: calls performMerge once + records auto_merge.merged audit + posts marker
* - on blocked decision: records the failure reason; does NOT call performMerge
* - never throws even when every injected helper throws
* - the PR-create route in pulls.tsx wires `tryAutoMergeNow(pr.id)` fire-and-forget
*/
import { describe, it, expect, beforeEach } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
tryAutoMergeNow,
type AutoMergeContext,
type AutoMergeDecision,
type TryAutoMergeNowDeps,
} from "../lib/auto-merge";
import type { PerformMergeResult } from "../lib/pr-merge";
// ---------------------------------------------------------------------------
// Test fixtures
// ---------------------------------------------------------------------------
type FastLaneCtxArg = Parameters<NonNullable<TryAutoMergeNowDeps["merge"]>>[0];
function makeCtx(overrides: Partial<FastLaneCtxArg> = {}): FastLaneCtxArg {
return {
prId: "pr-1",
prNumber: 7,
prTitle: "Fast lane test PR",
prBody: "Closes #99",
baseBranch: "main",
headBranch: "feature",
isDraft: false,
repositoryId: "repo-1",
authorUserId: "user-1",
ownerUsername: "alice",
repoName: "demo",
state: "open",
...overrides,
};
}
const greenDecision: AutoMergeDecision = {
merge: true,
reason: "All auto-merge conditions met for 'main'.",
};
const blockedDecision: AutoMergeDecision = {
merge: false,
reason: "Branch protection requires AI approval but no approval was found.",
blocking: ["AI approval missing."],
};
const okMerge: PerformMergeResult = {
ok: true,
closedIssueNumbers: [99],
resolvedFiles: [],
};
// A no-op sleep so tests don't actually wait. We still record each call
// so polling assertions can inspect them.
function makeFakeSleep() {
const calls: number[] = [];
const sleep = async (ms: number) => {
calls.push(ms);
};
return { sleep, calls };
}
interface RecordedAttempt {
repositoryId: string;
prId: string;
decision: AutoMergeDecision;
}
interface RecordedMerged {
ctx: FastLaneCtxArg;
result: PerformMergeResult;
}
interface TestHarness {
loadContextCalls: number;
hasAiReviewCalls: number;
evaluateCalls: AutoMergeContext[];
mergeCalls: FastLaneCtxArg[];
recordedAttempts: RecordedAttempt[];
mergedCalls: RecordedMerged[];
sleepCalls: number[];
deps: TryAutoMergeNowDeps;
}
function makeHarness(overrides: {
ctx?: FastLaneCtxArg | null;
/** Return value of hasAiReviewComment, indexed by call count (0-based). */
aiCommentReadyAfterPolls?: number; // -1 means never
decision?: AutoMergeDecision;
mergeResult?: PerformMergeResult;
// Throw-injection hooks (default false everywhere)
loadContextThrows?: boolean;
hasAiReviewThrows?: boolean;
evaluateThrows?: boolean;
mergeThrows?: boolean;
recordAttemptThrows?: boolean;
onMergedThrows?: boolean;
} = {}): TestHarness {
const ctx = overrides.ctx === undefined ? makeCtx() : overrides.ctx;
const decision = overrides.decision ?? greenDecision;
const mergeResult = overrides.mergeResult ?? okMerge;
const aiReadyAfter =
overrides.aiCommentReadyAfterPolls === undefined
? 0 // ready on first probe by default
: overrides.aiCommentReadyAfterPolls;
const h: TestHarness = {
loadContextCalls: 0,
hasAiReviewCalls: 0,
evaluateCalls: [],
mergeCalls: [],
recordedAttempts: [],
mergedCalls: [],
sleepCalls: [],
deps: {},
};
const sleep = async (ms: number) => {
h.sleepCalls.push(ms);
};
h.deps = {
loadContext: async () => {
h.loadContextCalls += 1;
if (overrides.loadContextThrows) throw new Error("loadContext boom");
return ctx;
},
hasAiReviewComment: async () => {
h.hasAiReviewCalls += 1;
if (overrides.hasAiReviewThrows) throw new Error("hasAiReview boom");
if (aiReadyAfter < 0) return false;
return h.hasAiReviewCalls > aiReadyAfter;
},
evaluate: async (ctxArg: AutoMergeContext) => {
h.evaluateCalls.push(ctxArg);
if (overrides.evaluateThrows) throw new Error("evaluate boom");
return decision;
},
merge: async (mctx) => {
h.mergeCalls.push(mctx);
if (overrides.mergeThrows) throw new Error("merge boom");
return mergeResult;
},
recordAttempt: async (repoId, prId, d) => {
h.recordedAttempts.push({ repositoryId: repoId, prId, decision: d });
if (overrides.recordAttemptThrows) throw new Error("record boom");
},
onMerged: async (mctx, result) => {
h.mergedCalls.push({ ctx: mctx, result });
if (overrides.onMergedThrows) throw new Error("onMerged boom");
},
sleep,
};
return h;
}
// ---------------------------------------------------------------------------
// AI-review wait behaviour
// ---------------------------------------------------------------------------
describe("tryAutoMergeNow — AI-review wait", () => {
it("polls multiple times while the AI-review comment is missing, then evaluates once it lands", async () => {
// The probe returns false on calls 1+2 and true on call 3, so the
// outer loop should sleep twice and then evaluate.
const h = makeHarness({ aiCommentReadyAfterPolls: 2 });
await tryAutoMergeNow("pr-1", {
waitForAiReviewMs: 60_000,
aiReviewPollIntervalMs: 5_000,
deps: h.deps,
});
expect(h.hasAiReviewCalls).toBeGreaterThanOrEqual(3);
expect(h.sleepCalls.length).toBeGreaterThanOrEqual(2);
expect(h.evaluateCalls.length).toBe(1);
// Green decision → merge proceeds.
expect(h.mergeCalls.length).toBe(1);
});
it("gives up cleanly when the AI-review wait window elapses without a comment", async () => {
// waitForAiReviewMs=0 ⇒ first check fails immediately and we return
// without evaluating. Models "AI review never landed before the cap".
const h = makeHarness({ aiCommentReadyAfterPolls: -1 });
await tryAutoMergeNow("pr-1", {
waitForAiReviewMs: 0,
aiReviewPollIntervalMs: 1,
deps: h.deps,
});
expect(h.hasAiReviewCalls).toBe(1);
expect(h.evaluateCalls.length).toBe(0);
expect(h.mergeCalls.length).toBe(0);
expect(h.recordedAttempts.length).toBe(0);
});
it("respects skipAiReviewWait=true and decides immediately without probing", async () => {
const h = makeHarness({ aiCommentReadyAfterPolls: -1 });
await tryAutoMergeNow("pr-1", {
skipAiReviewWait: true,
deps: h.deps,
});
expect(h.hasAiReviewCalls).toBe(0);
expect(h.evaluateCalls.length).toBe(1);
expect(h.mergeCalls.length).toBe(1);
});
});
// ---------------------------------------------------------------------------
// Decision branching
// ---------------------------------------------------------------------------
describe("tryAutoMergeNow — green path", () => {
it("calls performMerge once + fires onMerged + records the evaluated audit", async () => {
const h = makeHarness({ decision: greenDecision });
await tryAutoMergeNow("pr-1", {
skipAiReviewWait: true,
deps: h.deps,
});
expect(h.mergeCalls.length).toBe(1);
expect(h.mergedCalls.length).toBe(1);
expect(h.mergedCalls[0].ctx.prId).toBe("pr-1");
expect(h.mergedCalls[0].result.ok).toBe(true);
// recordAttempt fires exactly once with the green decision.
expect(h.recordedAttempts.length).toBe(1);
expect(h.recordedAttempts[0].decision.merge).toBe(true);
});
it("default onMerged side-effect posts the gluecron:auto-merge:v1 marker comment", async () => {
// Verify the actual marker token is exported and starts the comment
// body that defaultOnMerged emits. Pulling from __test rather than
// copy-pasting the constant keeps the assertion honest.
const mod = await import("../lib/auto-merge");
expect(mod.__test.FAST_LANE_AUTO_MERGE_MARKER).toBe(
"<!-- gluecron:auto-merge:v1 -->"
);
});
});
describe("tryAutoMergeNow — blocked path", () => {
it("records the failure reason and does NOT call performMerge", async () => {
const h = makeHarness({ decision: blockedDecision });
await tryAutoMergeNow("pr-1", {
skipAiReviewWait: true,
deps: h.deps,
});
expect(h.mergeCalls.length).toBe(0);
expect(h.mergedCalls.length).toBe(0);
expect(h.recordedAttempts.length).toBe(1);
expect(h.recordedAttempts[0].decision.merge).toBe(false);
expect(h.recordedAttempts[0].decision.reason).toMatch(/AI approval/i);
});
});
// ---------------------------------------------------------------------------
// Never-throws guarantee
// ---------------------------------------------------------------------------
describe("tryAutoMergeNow — never throws", () => {
it("swallows loadContext throws", async () => {
const h = makeHarness({ loadContextThrows: true });
await expect(
tryAutoMergeNow("pr-1", { skipAiReviewWait: true, deps: h.deps })
).resolves.toBeUndefined();
// Nothing else should have run.
expect(h.evaluateCalls.length).toBe(0);
expect(h.mergeCalls.length).toBe(0);
});
it("swallows evaluate throws + still records an audit", async () => {
const h = makeHarness({ evaluateThrows: true });
await expect(
tryAutoMergeNow("pr-1", { skipAiReviewWait: true, deps: h.deps })
).resolves.toBeUndefined();
// We never merged.
expect(h.mergeCalls.length).toBe(0);
// But we still recorded a failure audit so the paper trail survives.
expect(h.recordedAttempts.length).toBe(1);
expect(h.recordedAttempts[0].decision.merge).toBe(false);
});
it("swallows merge throws", async () => {
const h = makeHarness({ mergeThrows: true, decision: greenDecision });
await expect(
tryAutoMergeNow("pr-1", { skipAiReviewWait: true, deps: h.deps })
).resolves.toBeUndefined();
expect(h.mergeCalls.length).toBe(1);
expect(h.mergedCalls.length).toBe(0);
});
it("swallows recordAttempt + onMerged throws", async () => {
const h = makeHarness({
recordAttemptThrows: true,
onMergedThrows: true,
decision: greenDecision,
});
await expect(
tryAutoMergeNow("pr-1", { skipAiReviewWait: true, deps: h.deps })
).resolves.toBeUndefined();
// Merge still attempted; recordAttempt + onMerged both threw silently.
expect(h.mergeCalls.length).toBe(1);
});
it("never throws even when every injected helper throws", async () => {
const exploding: TryAutoMergeNowDeps = {
loadContext: async () => {
throw new Error("load");
},
hasAiReviewComment: async () => {
throw new Error("probe");
},
evaluate: async () => {
throw new Error("eval");
},
merge: async () => {
throw new Error("merge");
},
recordAttempt: async () => {
throw new Error("record");
},
onMerged: async () => {
throw new Error("merged");
},
sleep: async () => {
throw new Error("sleep");
},
};
await expect(
tryAutoMergeNow("pr-1", { skipAiReviewWait: true, deps: exploding })
).resolves.toBeUndefined();
});
it("returns cleanly when loadContext returns null (PR vanished mid-flight)", async () => {
const h = makeHarness({ ctx: null });
await tryAutoMergeNow("pr-1", { skipAiReviewWait: true, deps: h.deps });
expect(h.evaluateCalls.length).toBe(0);
expect(h.mergeCalls.length).toBe(0);
});
});
// ---------------------------------------------------------------------------
// Route wiring smoke check
// ---------------------------------------------------------------------------
describe("pulls.tsx — PR-create hook wiring (R3 fast-lane)", () => {
it("imports auto-merge and calls tryAutoMergeNow(pr.id) fire-and-forget after PR insert", () => {
const src = readFileSync(
join(import.meta.dir, "..", "routes", "pulls.tsx"),
"utf8"
);
// The exact line we added — kept loose enough to allow whitespace
// diffs but strict enough that an accidental delete trips this test.
expect(src).toMatch(/import\(\s*"\.\.\/lib\/auto-merge"\s*\)/);
expect(src).toMatch(/tryAutoMergeNow\s*\(\s*pr\.id\s*\)/);
// R3 marker comment so a casual rebase doesn't quietly drop the hook.
expect(src).toMatch(/R3 .*fast-lane auto-merge/i);
});
});
|