Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
spec-to-pr.test.ts19.3 KB · 596 lines
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/**
 * Tests for src/lib/spec-to-pr.ts.
 *
 * Two layers:
 *
 *   1. Pure helpers — no DB, no Claude. Always run.
 *      - parseFrontMatter / serialiseSpec round-trip.
 *      - specBasename derivation.
 *      - createSpecPR's fail-fast guards (no API key, empty spec).
 *
 *   2. End-to-end runSpecToPr — exercises the autopilot-driven flow with
 *      a fake AI client + a real bare repo on disk. Gated on HAS_DB so the
 *      DB-less sandbox still gets signal from the git-side assertions.
 */

import { describe, it, expect, beforeAll, afterAll, afterEach } from "bun:test";
import { join } from "path";
import { rm, mkdir } from "fs/promises";
import { eq } from "drizzle-orm";
import {
  createSpecPR,
  runSpecToPr,
  parseFrontMatter,
  serialiseSpec,
  specBasename,
  markSpecShipped,
  AI_SPEC_LABEL,
  AI_SPEC_PR_MARKER,
} from "../lib/spec-to-pr";
import {
  initBareRepo,
  createOrUpdateFileOnBranch,
  getBlob,
  refExists,
} from "../git/repository";
import {
  runSpecToPrTaskOnce,
  type SpecToPrCandidate,
} from "../lib/autopilot-spec-to-pr";
import { db } from "../db";
import {
  pullRequests,
  repositories,
  users,
} from "../db/schema";
import { _resetClientForTests as resetSpecAiClient } from "../lib/spec-ai";

const HAS_DB = Boolean(process.env.DATABASE_URL);

const TEST_REPOS = join(
  import.meta.dir,
  "../../.test-repos-spec-to-pr-" + Date.now()
);

beforeAll(async () => {
  process.env.GIT_REPOS_PATH = TEST_REPOS;
  await rm(TEST_REPOS, { recursive: true, force: true });
  await mkdir(TEST_REPOS, { recursive: true });
});

afterAll(async () => {
  await rm(TEST_REPOS, { recursive: true, force: true });
});

// ---------------------------------------------------------------------------
// createSpecPR — fail-fast guards (DB-less)
// ---------------------------------------------------------------------------

describe("createSpecPR — guards", () => {
  const originalKey = process.env.ANTHROPIC_API_KEY;

  afterEach(() => {
    if (originalKey === undefined) delete process.env.ANTHROPIC_API_KEY;
    else process.env.ANTHROPIC_API_KEY = originalKey;
  });

  it("returns ok:false when ANTHROPIC_API_KEY is missing", async () => {
    delete process.env.ANTHROPIC_API_KEY;
    const result = await createSpecPR({
      repoId: "00000000-0000-0000-0000-000000000000",
      spec: "test",
      userId: "00000000-0000-0000-0000-000000000000",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toContain("ANTHROPIC_API_KEY");
    }
  });

  it("returns ok:false when spec is empty", async () => {
    process.env.ANTHROPIC_API_KEY = "anthropic-test-placeholder";
    const result = await createSpecPR({
      repoId: "00000000-0000-0000-0000-000000000000",
      spec: "   ",
      userId: "00000000-0000-0000-0000-000000000000",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toContain("empty");
    }
  });
});

// ---------------------------------------------------------------------------
// Pure front-matter helpers
// ---------------------------------------------------------------------------

describe("parseFrontMatter", () => {
  it("returns empty front-matter when the document has none", () => {
    const out = parseFrontMatter("just a body");
    expect(out.hasFrontMatter).toBe(false);
    expect(out.body).toBe("just a body");
    expect(out.frontMatter).toEqual({});
  });

  it("parses simple key: value pairs", () => {
    const out = parseFrontMatter("---\ntitle: Add dark mode\nstatus: ready\n---\nbody here");
    expect(out.hasFrontMatter).toBe(true);
    expect(out.frontMatter.title).toBe("Add dark mode");
    expect(out.frontMatter.status).toBe("ready");
    expect(out.body).toBe("body here");
  });

  it("strips matched surrounding quotes", () => {
    const out = parseFrontMatter('---\ntitle: "Quoted: title"\n---\nbody');
    expect(out.frontMatter.title).toBe("Quoted: title");
  });

  it("tolerates CRLF line endings", () => {
    const out = parseFrontMatter("---\r\ntitle: T\r\nstatus: ready\r\n---\r\nbody");
    expect(out.frontMatter.title).toBe("T");
    expect(out.frontMatter.status).toBe("ready");
  });
});

describe("serialiseSpec", () => {
  it("round-trips parsed front-matter", () => {
    const raw = "---\ntitle: Add dark mode\nstatus: ready\n---\nbody here\n";
    const parsed = parseFrontMatter(raw);
    const out = serialiseSpec(parsed.frontMatter, parsed.body);
    const reparsed = parseFrontMatter(out);
    expect(reparsed.frontMatter.title).toBe("Add dark mode");
    expect(reparsed.frontMatter.status).toBe("ready");
    expect(reparsed.body.trim()).toBe("body here");
  });

  it("quotes values containing colons", () => {
    const out = serialiseSpec({ title: "Foo: bar" }, "body");
    // The quoted form must round-trip back to the same string.
    const reparsed = parseFrontMatter(out);
    expect(reparsed.frontMatter.title).toBe("Foo: bar");
  });
});

describe("specBasename", () => {
  it("strips the directory and .md extension", () => {
    expect(specBasename(".gluecron/specs/foo-bar.md")).toBe("foo-bar");
  });

  it("falls back to 'spec' for unusable input", () => {
    expect(specBasename(".gluecron/specs/!!!.md")).toBe("spec");
  });
});

// ---------------------------------------------------------------------------
// runSpecToPr — guard short-circuits (DB-less)
// ---------------------------------------------------------------------------

describe("runSpecToPr — guards", () => {
  const originalKey = process.env.ANTHROPIC_API_KEY;
  const originalDisabled = process.env.AUTOPILOT_DISABLED;

  afterEach(() => {
    if (originalKey === undefined) delete process.env.ANTHROPIC_API_KEY;
    else process.env.ANTHROPIC_API_KEY = originalKey;
    if (originalDisabled === undefined) delete process.env.AUTOPILOT_DISABLED;
    else process.env.AUTOPILOT_DISABLED = originalDisabled;
  });

  it("short-circuits when AUTOPILOT_DISABLED=1", async () => {
    process.env.AUTOPILOT_DISABLED = "1";
    process.env.ANTHROPIC_API_KEY = "x";
    const result = await runSpecToPr({
      repositoryId: "00000000-0000-0000-0000-000000000000",
      specPath: ".gluecron/specs/foo.md",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toContain("AUTOPILOT_DISABLED");
  });

  it("short-circuits when ANTHROPIC_API_KEY is missing", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    delete process.env.ANTHROPIC_API_KEY;
    const result = await runSpecToPr({
      repositoryId: "00000000-0000-0000-0000-000000000000",
      specPath: ".gluecron/specs/foo.md",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toContain("ANTHROPIC_API_KEY");
  });

  it("rejects paths outside .gluecron/specs/", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    process.env.ANTHROPIC_API_KEY = "x";
    const result = await runSpecToPr({
      repositoryId: "00000000-0000-0000-0000-000000000000",
      specPath: "src/evil.md",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toContain(".gluecron/specs");
  });

  it("rejects non-.md paths", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    process.env.ANTHROPIC_API_KEY = "x";
    const result = await runSpecToPr({
      repositoryId: "00000000-0000-0000-0000-000000000000",
      specPath: ".gluecron/specs/foo.txt",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toContain(".md");
  });
});

// ---------------------------------------------------------------------------
// Autopilot task — short-circuit + dependency-injection coverage (DB-less)
// ---------------------------------------------------------------------------

describe("runSpecToPrTaskOnce", () => {
  const originalKey = process.env.ANTHROPIC_API_KEY;
  const originalDisabled = process.env.AUTOPILOT_DISABLED;

  afterEach(() => {
    if (originalKey === undefined) delete process.env.ANTHROPIC_API_KEY;
    else process.env.ANTHROPIC_API_KEY = originalKey;
    if (originalDisabled === undefined) delete process.env.AUTOPILOT_DISABLED;
    else process.env.AUTOPILOT_DISABLED = originalDisabled;
  });

  it("no-ops when AUTOPILOT_DISABLED=1", async () => {
    process.env.AUTOPILOT_DISABLED = "1";
    process.env.ANTHROPIC_API_KEY = "x";
    let called = 0;
    const out = await runSpecToPrTaskOnce({
      findCandidates: async () => {
        called += 1;
        return [];
      },
    });
    expect(out).toEqual({ considered: 0, dispatched: 0, skipped: 0, failed: 0 });
    expect(called).toBe(0);
  });

  it("no-ops when ANTHROPIC_API_KEY is unset", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    delete process.env.ANTHROPIC_API_KEY;
    let called = 0;
    const out = await runSpecToPrTaskOnce({
      findCandidates: async () => {
        called += 1;
        return [];
      },
    });
    expect(out.dispatched).toBe(0);
    expect(called).toBe(0);
  });

  it("dispatches a ready spec exactly once and counts the result", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    process.env.ANTHROPIC_API_KEY = "x";
    const cand: SpecToPrCandidate = {
      repositoryId: "repo-1",
      ownerName: "alice",
      repoName: "demo",
      defaultBranch: "main",
      specPath: ".gluecron/specs/foo.md",
    };
    let dispatchCount = 0;
    const out = await runSpecToPrTaskOnce({
      findCandidates: async () => [cand],
      hasOpenLinkedPr: async () => false,
      dispatcher: async () => {
        dispatchCount += 1;
        return { ok: true, branch: "ai-spec/foo-1", prNumber: 7, status: "building" };
      },
    });
    expect(dispatchCount).toBe(1);
    expect(out.dispatched).toBe(1);
    expect(out.skipped).toBe(0);
    expect(out.failed).toBe(0);
    expect(out.considered).toBe(1);
  });

  it("skips a spec whose PR already exists", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    process.env.ANTHROPIC_API_KEY = "x";
    let dispatched = 0;
    const out = await runSpecToPrTaskOnce({
      findCandidates: async () => [
        {
          repositoryId: "r",
          ownerName: "alice",
          repoName: "demo",
          defaultBranch: "main",
          specPath: ".gluecron/specs/foo.md",
        },
      ],
      hasOpenLinkedPr: async () => true,
      dispatcher: async () => {
        dispatched += 1;
        return { ok: true, branch: "x", prNumber: 1, status: "building" };
      },
    });
    expect(dispatched).toBe(0);
    expect(out.skipped).toBe(1);
    expect(out.dispatched).toBe(0);
  });

  it("counts dispatcher failures separately from skips", async () => {
    delete process.env.AUTOPILOT_DISABLED;
    process.env.ANTHROPIC_API_KEY = "x";
    const out = await runSpecToPrTaskOnce({
      findCandidates: async () => [
        {
          repositoryId: "r",
          ownerName: "alice",
          repoName: "demo",
          defaultBranch: "main",
          specPath: ".gluecron/specs/foo.md",
        },
      ],
      hasOpenLinkedPr: async () => false,
      dispatcher: async () => ({ ok: false, error: "boom" }),
    });
    expect(out.failed).toBe(1);
    expect(out.dispatched).toBe(0);
  });
});

// ---------------------------------------------------------------------------
// End-to-end runSpecToPr — fake Claude + real bare repo
// ---------------------------------------------------------------------------

/**
 * Build a fake global `fetch` that returns the canned Claude JSON envelope.
 * The Anthropic SDK calls `fetch` under the hood, so swapping it lets us
 * exercise the full pipeline without an API key or network.
 */
function withFakeAnthropic<T>(
  responseText: string,
  fn: () => Promise<T>
): Promise<T> {
  const originalFetch = globalThis.fetch;
  // Reset the cached Anthropic client so it captures the stubbed fetch.
  resetSpecAiClient();
  (globalThis as any).fetch = async () => {
    return new Response(
      JSON.stringify({
        id: "msg_test",
        type: "message",
        role: "assistant",
        model: "claude-sonnet-4-6",
        content: [{ type: "text", text: responseText }],
        stop_reason: "end_turn",
        usage: { input_tokens: 1, output_tokens: 1 },
      }),
      {
        status: 200,
        headers: { "content-type": "application/json" },
      }
    );
  };
  return fn().finally(() => {
    globalThis.fetch = originalFetch;
    resetSpecAiClient();
  });
}

describe.skipIf(!HAS_DB)("runSpecToPr — end-to-end with fake Claude", () => {
  it(
    "opens a PR, tags it, and rewrites the spec status to building",
    async () => {
      process.env.ANTHROPIC_API_KEY = "anthropic-test-placeholder";
      delete process.env.AUTOPILOT_DISABLED;

      const username = `spectopr_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
      const [u] = await db
        .insert(users)
        .values({
          username,
          email: `${username}@example.com`,
          passwordHash: "x",
        })
        .returning({ id: users.id });

      const repoName = `subject_${Date.now()}`;
      const [r] = await db
        .insert(repositories)
        .values({
          ownerId: u.id,
          name: repoName,
          diskPath: `/tmp/${username}/${repoName}`,
          defaultBranch: "main",
        })
        .returning({ id: repositories.id });

      await initBareRepo(username, repoName);

      // Seed a minimal repo: a source file + a ready spec.
      const seedSrc = await createOrUpdateFileOnBranch({
        owner: username,
        name: repoName,
        branch: "main",
        filePath: "src/app.ts",
        bytes: new TextEncoder().encode("export const hello = 'world';\n"),
        message: "seed src",
        authorName: "Seeder",
        authorEmail: "s@e.com",
      });
      if ("error" in seedSrc) throw new Error("seed src failed");

      const specBody =
        "---\ntitle: Add greeting\nstatus: ready\n---\n\nAdd a `greet()` helper to src/app.ts that returns 'hi'.\n";
      const seedSpec = await createOrUpdateFileOnBranch({
        owner: username,
        name: repoName,
        branch: "main",
        filePath: ".gluecron/specs/greet.md",
        bytes: new TextEncoder().encode(specBody),
        message: "seed spec",
        authorName: "Seeder",
        authorEmail: "s@e.com",
      });
      if ("error" in seedSpec) throw new Error("seed spec failed");

      const cannedEdits = JSON.stringify({
        summary: "Add greet() helper",
        edits: [
          {
            action: "edit",
            path: "src/app.ts",
            content:
              "export const hello = 'world';\nexport function greet(): string { return 'hi'; }\n",
          },
        ],
      });

      const result = await withFakeAnthropic(cannedEdits, () =>
        runSpecToPr({
          repositoryId: r.id,
          specPath: ".gluecron/specs/greet.md",
        })
      );

      expect(result.ok).toBe(true);
      if (!result.ok) return;
      expect(result.branch.startsWith("ai-spec/greet-")).toBe(true);
      expect(typeof result.prNumber).toBe("number");
      expect(result.status).toBe("building");

      // Branch exists and contains the patched file.
      expect(
        await refExists(username, repoName, `refs/heads/${result.branch}`)
      ).toBe(true);
      const blob = await getBlob(username, repoName, result.branch, "src/app.ts");
      expect(blob).not.toBeNull();
      expect(blob!.content).toContain("greet()");

      // PR row exists with the right marker + label citation.
      const [pr] = await db
        .select({
          number: pullRequests.number,
          headBranch: pullRequests.headBranch,
          baseBranch: pullRequests.baseBranch,
          body: pullRequests.body,
          title: pullRequests.title,
        })
        .from(pullRequests)
        .where(eq(pullRequests.repositoryId, r.id))
        .limit(1);
      expect(pr).toBeTruthy();
      expect(pr!.headBranch).toBe(result.branch);
      expect(pr!.baseBranch).toBe("main");
      expect(pr!.title.startsWith("[spec]")).toBe(true);
      expect(pr!.body).toContain(AI_SPEC_PR_MARKER);
      expect(pr!.body).toContain(AI_SPEC_LABEL);
      expect(pr!.body).toContain(".gluecron/specs/greet.md");

      // The spec file should now be `status: building` on main.
      const updatedSpec = await getBlob(
        username,
        repoName,
        "main",
        ".gluecron/specs/greet.md"
      );
      expect(updatedSpec).not.toBeNull();
      const parsed = parseFrontMatter(updatedSpec!.content);
      expect(parsed.frontMatter.status).toBe("building");
      expect(parsed.frontMatter.pr).toBe(String(pr!.number));

      // Re-running against the (now `building`) spec should NOT open another
      // PR — both because the status is not `ready` AND because the dedup
      // query will find the previous PR body referencing the spec path.
      const second = await withFakeAnthropic(cannedEdits, () =>
        runSpecToPr({
          repositoryId: r.id,
          specPath: ".gluecron/specs/greet.md",
        })
      );
      expect(second.ok).toBe(false);
      if (!second.ok) {
        // Either status mismatch OR the dedup short-circuit — both are
        // valid "doesn't re-trigger" signals.
        expect(
          second.error.includes("not ready") ||
            second.error.includes("PR already exists")
        ).toBe(true);
      }

      // Now mark shipped and re-read.
      const shipped = await markSpecShipped({
        ownerName: username,
        repoName,
        defaultBranch: "main",
        specPath: ".gluecron/specs/greet.md",
        prNumber: pr!.number,
      });
      expect(shipped.ok).toBe(true);
      const finalSpec = await getBlob(
        username,
        repoName,
        "main",
        ".gluecron/specs/greet.md"
      );
      const finalParsed = parseFrontMatter(finalSpec!.content);
      expect(finalParsed.frontMatter.status).toBe("shipped");
    },
    25000
  );

  it("refuses a spec whose status is not 'ready'", async () => {
    process.env.ANTHROPIC_API_KEY = "anthropic-test-placeholder";
    delete process.env.AUTOPILOT_DISABLED;

    const username = `spectopr_draft_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
    const [u] = await db
      .insert(users)
      .values({
        username,
        email: `${username}@example.com`,
        passwordHash: "x",
      })
      .returning({ id: users.id });

    const repoName = `subject_${Date.now()}`;
    const [r] = await db
      .insert(repositories)
      .values({
        ownerId: u.id,
        name: repoName,
        diskPath: `/tmp/${username}/${repoName}`,
        defaultBranch: "main",
      })
      .returning({ id: repositories.id });

    await initBareRepo(username, repoName);
    const seed = await createOrUpdateFileOnBranch({
      owner: username,
      name: repoName,
      branch: "main",
      filePath: ".gluecron/specs/draft.md",
      bytes: new TextEncoder().encode(
        "---\ntitle: Draft\nstatus: draft\n---\n\nNot ready yet.\n"
      ),
      message: "seed draft",
      authorName: "Seeder",
      authorEmail: "s@e.com",
    });
    if ("error" in seed) throw new Error("seed failed");

    // Should refuse before calling the AI — so we don't even need a fetch stub.
    const result = await runSpecToPr({
      repositoryId: r.id,
      specPath: ".gluecron/specs/draft.md",
    });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toContain("not ready");

    // No PR should have been opened.
    const prs = await db
      .select({ id: pullRequests.id })
      .from(pullRequests)
      .where(eq(pullRequests.repositoryId, r.id));
    expect(prs.length).toBe(0);
  });
});