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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | /**
* Block K3 — autopilot AI-driver task tests.
*
* Uses the dependency-injection seams added in `src/lib/autopilot.ts`
* (`runAutoMergeSweep`) and `src/lib/ai-build-tasks.ts`
* (`runAiBuildTaskOnce`) so we never hit the DB or the AI client.
*
* Covers the contract called out in the K3 spec:
* - auto-merge-sweep skips drafts
* - auto-merge-sweep skips archived repos
* - auto-merge-sweep invokes merge exactly once per `merge:true` PR
* - ai-build dispatches against `ai:build`-labelled issues
* - ai-build skips an issue with a marker comment already present
* - both tasks no-op cleanly when AUTOPILOT_DISABLED=1
*/
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
import {
startAutopilot,
runAutoMergeSweep,
type AutoMergeSweepDeps,
type AutopilotTask,
} from "../lib/autopilot";
import {
runAiBuildTaskOnce,
AI_BUILD_MARKER,
type AiBuildCandidate,
} from "../lib/ai-build-tasks";
import type {
AutoMergeContext,
AutoMergeDecision,
} from "../lib/auto-merge";
import type { PerformMergeResult } from "../lib/pr-merge";
// ---------------------------------------------------------------------------
// Test fixtures
// ---------------------------------------------------------------------------
type Candidate = Parameters<NonNullable<AutoMergeSweepDeps["merge"]>>[0];
function makeCandidate(overrides: Partial<Candidate> = {}): Candidate {
return {
prId: "pr-1",
prNumber: 42,
prTitle: "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 okMerge: PerformMergeResult = {
ok: true,
closedIssueNumbers: [],
resolvedFiles: [],
};
const allowDecision: AutoMergeDecision = {
merge: true,
reason: "All auto-merge conditions met for 'main'.",
};
const blockDecision: AutoMergeDecision = {
merge: false,
reason: "blocked",
blocking: ["draft"],
};
// ---------------------------------------------------------------------------
// auto-merge-sweep
// ---------------------------------------------------------------------------
describe("auto-merge-sweep", () => {
it("skips drafts at the candidate-finder layer (drafts must never enter the loop)", async () => {
// The default findCandidates filters drafts via SQL. We model that
// contract here: when the caller supplies drafts, the sweep STILL
// delegates the merge() call ONLY for non-draft PRs whose decision is
// merge:true. Drafts that somehow leak through must surface as blocked
// (decideAutoMerge will reject them).
const draft = makeCandidate({ prId: "draft-1", isDraft: true });
const open = makeCandidate({ prId: "open-1", isDraft: false });
const mergeCalls: string[] = [];
const summary = await runAutoMergeSweep({
findCandidates: async () => [draft, open],
// Simulate evaluateAutoMerge: drafts get blocked, opens get allowed.
evaluate: async (ctx: AutoMergeContext) =>
ctx.isDraft ? blockDecision : allowDecision,
merge: async (cand) => {
mergeCalls.push(cand.prId);
return okMerge;
},
recordAttempt: async () => {},
onMerged: async () => {},
onMergeFailed: async () => {},
shouldShortCircuitAi: async () => false,
});
expect(mergeCalls).toEqual(["open-1"]);
expect(summary.evaluated).toBe(2);
expect(summary.merged).toBe(1);
expect(summary.blocked).toBe(1);
});
it("skips archived repos (no candidates surfaced from the finder)", async () => {
// The default finder excludes archived repos in SQL. We simulate by
// supplying an empty result and confirming a clean no-op summary.
let findCalled = false;
let mergeCalled = 0;
const summary = await runAutoMergeSweep({
findCandidates: async () => {
findCalled = true;
return [];
},
evaluate: async () => allowDecision,
merge: async () => {
mergeCalled += 1;
return okMerge;
},
recordAttempt: async () => {},
onMerged: async () => {},
onMergeFailed: async () => {},
shouldShortCircuitAi: async () => false,
});
expect(findCalled).toBe(true);
expect(mergeCalled).toBe(0);
expect(summary).toEqual({ evaluated: 0, merged: 0, blocked: 0 });
});
it("invokes the merge function exactly once per merge:true PR and records an evaluated audit per PR", async () => {
const candidates = [
makeCandidate({ prId: "a" }),
makeCandidate({ prId: "b" }),
makeCandidate({ prId: "c" }),
];
const mergeCalls: string[] = [];
const evaluatedAuditCalls: string[] = [];
const mergedSuccessCalls: string[] = [];
const summary = await runAutoMergeSweep({
findCandidates: async () => candidates,
// 'a' and 'c' are allowed, 'b' is blocked.
evaluate: async (ctx) =>
ctx.pullRequestId === "b" ? blockDecision : allowDecision,
merge: async (cand) => {
mergeCalls.push(cand.prId);
return okMerge;
},
recordAttempt: async (_repoId, prId) => {
evaluatedAuditCalls.push(prId);
},
onMerged: async (cand) => {
mergedSuccessCalls.push(cand.prId);
},
onMergeFailed: async () => {},
shouldShortCircuitAi: async () => false,
});
expect(mergeCalls).toEqual(["a", "c"]);
expect(mergedSuccessCalls).toEqual(["a", "c"]);
// recordAttempt fires for EVERY evaluation, not just successes.
expect(evaluatedAuditCalls.sort()).toEqual(["a", "b", "c"]);
expect(summary).toEqual({ evaluated: 3, merged: 2, blocked: 1 });
});
it("emits the merge_failed audit when performMerge returns ok=false (and does NOT emit merged audit)", async () => {
const cand = makeCandidate({ prId: "x" });
let mergedHits = 0;
let failedHits = 0;
const summary = await runAutoMergeSweep({
findCandidates: async () => [cand],
evaluate: async () => allowDecision,
merge: async () => ({
ok: false,
error: "git update-ref failed: not a fast-forward",
closedIssueNumbers: [],
resolvedFiles: [],
}),
recordAttempt: async () => {},
onMerged: async () => {
mergedHits += 1;
},
onMergeFailed: async () => {
failedHits += 1;
},
shouldShortCircuitAi: async () => false,
});
expect(mergedHits).toBe(0);
expect(failedHits).toBe(1);
expect(summary).toEqual({ evaluated: 1, merged: 0, blocked: 1 });
});
it("short-circuits AI-required rules when ANTHROPIC_API_KEY is unset (logged as blocked, not error)", async () => {
const cand = makeCandidate({ prId: "ai-1" });
let evaluateHits = 0;
let mergeHits = 0;
const summary = await runAutoMergeSweep({
findCandidates: async () => [cand],
shouldShortCircuitAi: async () => true,
evaluate: async () => {
evaluateHits += 1;
return allowDecision;
},
merge: async () => {
mergeHits += 1;
return okMerge;
},
recordAttempt: async () => {},
onMerged: async () => {},
onMergeFailed: async () => {},
});
expect(evaluateHits).toBe(0); // never invoked
expect(mergeHits).toBe(0); // never invoked
expect(summary).toEqual({ evaluated: 1, merged: 0, blocked: 1 });
});
it("never throws even when the candidate-finder throws — returns zero summary", async () => {
const summary = await runAutoMergeSweep({
findCandidates: async () => {
throw new Error("db blew up");
},
evaluate: async () => allowDecision,
merge: async () => okMerge,
recordAttempt: async () => {},
onMerged: async () => {},
onMergeFailed: async () => {},
shouldShortCircuitAi: async () => false,
});
expect(summary).toEqual({ evaluated: 0, merged: 0, blocked: 0 });
});
it("isolates per-PR failures — a thrown merge() doesn't stop later PRs", async () => {
const candidates = [
makeCandidate({ prId: "first" }),
makeCandidate({ prId: "second" }),
];
const mergeCalls: string[] = [];
const summary = await runAutoMergeSweep({
findCandidates: async () => candidates,
evaluate: async () => allowDecision,
merge: async (cand) => {
mergeCalls.push(cand.prId);
if (cand.prId === "first") throw new Error("kaboom");
return okMerge;
},
recordAttempt: async () => {},
onMerged: async () => {},
onMergeFailed: async () => {},
shouldShortCircuitAi: async () => false,
});
expect(mergeCalls).toEqual(["first", "second"]);
expect(summary.evaluated).toBe(2);
expect(summary.merged).toBe(1);
expect(summary.blocked).toBe(1);
});
});
// ---------------------------------------------------------------------------
// ai-build-from-issues
// ---------------------------------------------------------------------------
function makeAiBuildCandidate(
overrides: Partial<AiBuildCandidate> = {}
): AiBuildCandidate {
return {
issueId: "issue-1",
issueNumber: 7,
issueTitle: "Add a sparkle button",
issueBody: "Should sparkle when clicked.",
repositoryId: "repo-1",
authorUserId: "user-1",
ownerUsername: "alice",
repoName: "demo",
defaultBranch: "main",
...overrides,
};
}
describe("ai-build-from-issues", () => {
it("dispatches the spec-to-PR pipeline for an ai:build-labelled issue", async () => {
const dispatched: Array<{ repoId: string; spec: string; baseRef: string }> =
[];
const markersPosted: string[] = [];
const summary = await runAiBuildTaskOnce({
findCandidates: async () => [makeAiBuildCandidate()],
hasDispatchMarker: async () => false,
hasOpenLinkedPr: async () => false,
dispatcher: async (args) => {
dispatched.push({
repoId: args.repoId,
spec: args.spec,
baseRef: args.baseRef,
});
return { ok: true, prNumber: 123 };
},
postMarkerComment: async (issueId) => {
markersPosted.push(issueId);
},
});
expect(dispatched.length).toBe(1);
expect(dispatched[0].repoId).toBe("repo-1");
expect(dispatched[0].baseRef).toBe("main");
// buildSpecFromIssue prefixes with "Implement:" and includes "Closes #N".
expect(dispatched[0].spec).toContain("Implement: Add a sparkle button");
expect(dispatched[0].spec).toContain("Closes #7");
expect(markersPosted).toEqual(["issue-1"]);
expect(summary).toEqual({ queued: 1, skipped: 0 });
});
it("skips an issue that already has the dispatch marker comment", async () => {
const dispatched: string[] = [];
const markersPosted: string[] = [];
const summary = await runAiBuildTaskOnce({
findCandidates: async () => [makeAiBuildCandidate({ issueId: "i-already" })],
hasDispatchMarker: async (id) => id === "i-already",
hasOpenLinkedPr: async () => false,
dispatcher: async (args) => {
dispatched.push(args.repoId);
return { ok: true, prNumber: 1 };
},
postMarkerComment: async (id) => {
markersPosted.push(id);
},
});
expect(dispatched).toEqual([]);
expect(markersPosted).toEqual([]);
expect(summary).toEqual({ queued: 0, skipped: 1 });
});
it("skips an issue that already has an open PR closing it via close-keywords", async () => {
let dispatcherHits = 0;
const summary = await runAiBuildTaskOnce({
findCandidates: async () => [makeAiBuildCandidate({ issueNumber: 42 })],
hasDispatchMarker: async () => false,
hasOpenLinkedPr: async (_repoId, n) => n === 42,
dispatcher: async () => {
dispatcherHits += 1;
return { ok: true, prNumber: 1 };
},
postMarkerComment: async () => {},
});
expect(dispatcherHits).toBe(0);
expect(summary).toEqual({ queued: 0, skipped: 1 });
});
it("swallows dispatcher failures without crashing the tick", async () => {
let markerPosted = false;
const summary = await runAiBuildTaskOnce({
findCandidates: async () => [makeAiBuildCandidate()],
hasDispatchMarker: async () => false,
hasOpenLinkedPr: async () => false,
dispatcher: async () => {
throw new Error("anthropic 500");
},
postMarkerComment: async () => {
markerPosted = true;
},
});
// Marker still posted (idempotency takes priority over success).
expect(markerPosted).toBe(true);
// The issue counts as queued because we did our part — we don't retry.
expect(summary.queued).toBe(1);
});
it("returns zero summary if findCandidates throws", async () => {
const summary = await runAiBuildTaskOnce({
findCandidates: async () => {
throw new Error("db down");
},
});
expect(summary).toEqual({ queued: 0, skipped: 0 });
});
it("uses the AI_BUILD_MARKER constant in the marker body", async () => {
let receivedBody = "";
await runAiBuildTaskOnce({
findCandidates: async () => [makeAiBuildCandidate()],
hasDispatchMarker: async () => false,
hasOpenLinkedPr: async () => false,
dispatcher: async () => ({ ok: true, prNumber: 1 }),
postMarkerComment: async (_id, _author, body) => {
receivedBody = body;
},
});
expect(receivedBody.includes(AI_BUILD_MARKER)).toBe(true);
});
});
// ---------------------------------------------------------------------------
// AUTOPILOT_DISABLED=1 — both tasks no-op when the parent loop never runs.
// ---------------------------------------------------------------------------
describe("autopilot disabled — neither AI task fires", () => {
const originalDisabled = process.env.AUTOPILOT_DISABLED;
afterEach(() => {
if (originalDisabled === undefined) delete process.env.AUTOPILOT_DISABLED;
else process.env.AUTOPILOT_DISABLED = originalDisabled;
});
it("startAutopilot with AUTOPILOT_DISABLED=1 doesn't run injected AI tasks", async () => {
process.env.AUTOPILOT_DISABLED = "1";
let autoMergeRan = 0;
let aiBuildRan = 0;
const tasks: AutopilotTask[] = [
{
name: "auto-merge-sweep",
run: async () => {
autoMergeRan += 1;
},
},
{
name: "ai-build-from-issues",
run: async () => {
aiBuildRan += 1;
},
},
];
const { stop } = startAutopilot({ intervalMs: 5, tasks });
await new Promise((r) => setTimeout(r, 40));
stop();
expect(autoMergeRan).toBe(0);
expect(aiBuildRan).toBe(0);
});
});
|