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
ai-patch-generator.test.ts15.9 KB · 500 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
/**
 * Tests for src/lib/ai-patch-generator.ts.
 *
 * Two layers of coverage:
 *
 *   1. Pure helpers — no DB / no Claude. These always run.
 *      - findingShortId is deterministic for the same finding.
 *      - patchBranchName respects an override and otherwise embeds the
 *        finding's short id + timestamp.
 *      - buildPatchPrompt + renderPatchPrBody embed the finding's
 *        context so PR reviewers can trace the trail.
 *      - severityAtOrAboveMedium gates correctly.
 *
 *   2. End-to-end with an injected fake Claude client + a real bare
 *      repo on disk. DB-backed steps (PR insert, label upsert, audit)
 *      are gated on DATABASE_URL — without it we still assert the
 *      git-side effect (branch + commit landed) and that an empty
 *      patches array short-circuits to `null`.
 *
 * The Anthropic client is faked via the public `client` option so we
 * never touch the network or require an API key.
 */

import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { join } from "path";
import { rm, mkdir } from "fs/promises";
import {
  AI_PATCH_LABEL,
  AI_PATCH_MARKER,
  __test,
  buildPatchPrompt,
  findingShortId,
  generatePatchForGateTestFinding,
  patchBranchName,
  renderPatchPrBody,
  severityAtOrAboveMedium,
} from "../lib/ai-patch-generator";
import {
  initBareRepo,
  createOrUpdateFileOnBranch,
  refExists,
  resolveRef,
  getBlob,
} from "../git/repository";
import { db } from "../db";
import { eq } from "drizzle-orm";
import {
  pullRequests,
  repositories,
  users,
} from "../db/schema";

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

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

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

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

// ---------------------------------------------------------------------------
// Pure helpers
// ---------------------------------------------------------------------------

describe("findingShortId", () => {
  it("uses the scanner-provided id when it's safe", () => {
    expect(findingShortId({ id: "RULE-123" })).toBe("RULE-123");
  });

  it("derives a deterministic hash when no id is provided", () => {
    const a = findingShortId({
      ruleId: "no-eval",
      path: "src/x.ts",
      line: 7,
      title: "eval usage",
      description: "Avoid eval",
    });
    const b = findingShortId({
      ruleId: "no-eval",
      path: "src/x.ts",
      line: 7,
      title: "eval usage",
      description: "Avoid eval",
    });
    expect(a).toBe(b);
    expect(a.length).toBe(12);
    expect(/^[0-9a-f]{12}$/.test(a)).toBe(true);
  });

  it("yields different ids for different findings", () => {
    const a = findingShortId({ ruleId: "x", path: "a.ts" });
    const b = findingShortId({ ruleId: "y", path: "a.ts" });
    expect(a).not.toBe(b);
  });
});

describe("patchBranchName", () => {
  it("honours an override", () => {
    expect(patchBranchName({ id: "RULE-1" }, "custom/branch")).toBe(
      "custom/branch"
    );
  });

  it("uses ai-patch/<id>-<timestamp> by default", () => {
    const name = patchBranchName({ id: "RULE-1" });
    expect(name.startsWith("ai-patch/RULE-1-")).toBe(true);
    // tail must be a numeric timestamp
    const ts = name.split("-").pop()!;
    expect(/^\d+$/.test(ts)).toBe(true);
  });
});

describe("severityAtOrAboveMedium", () => {
  it("accepts medium/high/critical (case-insensitive)", () => {
    expect(severityAtOrAboveMedium("medium")).toBe(true);
    expect(severityAtOrAboveMedium("High")).toBe(true);
    expect(severityAtOrAboveMedium("CRITICAL")).toBe(true);
  });

  it("rejects low / info / missing", () => {
    expect(severityAtOrAboveMedium("low")).toBe(false);
    expect(severityAtOrAboveMedium("info")).toBe(false);
    expect(severityAtOrAboveMedium(undefined)).toBe(false);
    expect(severityAtOrAboveMedium(null)).toBe(false);
    expect(severityAtOrAboveMedium("")).toBe(false);
  });
});

describe("buildPatchPrompt", () => {
  it("embeds path, severity, description, and the file contents", () => {
    const out = buildPatchPrompt(
      {
        ruleId: "hardcoded-secret",
        severity: "high",
        line: 12,
        description: "API key in source",
      },
      "src/config.ts",
      "export const KEY = 'sk_live_xxx';"
    );
    expect(out).toContain("src/config.ts");
    expect(out).toContain("line 12");
    expect(out).toContain("API key in source");
    expect(out).toContain("hardcoded-secret");
    expect(out).toContain("sk_live_xxx");
    // JSON schema cue must be present so the model returns parseable output
    expect(out).toContain("\"patches\"");
    expect(out).toContain("\"new_content\"");
  });
});

describe("renderPatchPrBody", () => {
  it("includes the marker, label tag, citation, and file list", () => {
    const body = renderPatchPrBody({
      finding: {
        ruleId: "hardcoded-secret",
        severity: "high",
        line: 12,
        title: "Hardcoded secret",
        description: "API key in source",
      },
      filePath: "src/config.ts",
      explanation: "Replaced literal with env var read.",
      reportUrl: "https://gatetest.example/run/42",
      patchPaths: ["src/config.ts"],
    });
    expect(body).toContain(AI_PATCH_MARKER);
    expect(body).toContain(AI_PATCH_LABEL);
    expect(body).toContain("https://gatetest.example/run/42");
    expect(body).toContain("src/config.ts");
    expect(body).toContain("Replaced literal with env var read.");
  });

  it("notes the missing citation when reportUrl is absent", () => {
    const body = renderPatchPrBody({
      finding: { ruleId: "x" },
      filePath: "a.ts",
      explanation: "",
      patchPaths: ["a.ts"],
    });
    expect(body).toContain("(not provided)");
  });
});

// ---------------------------------------------------------------------------
// Injected-Claude end-to-end. The DB-touching steps only run with HAS_DB;
// without it we still verify the git side effects and the empty-patches
// short-circuit.
// ---------------------------------------------------------------------------

/**
 * Build a fake Anthropic client that returns a canned JSON envelope.
 * Mirrors the shape `extractText` + `parseJsonResponse` consume.
 */
function fakeClient(responseText: string) {
  return {
    messages: {
      // The real SDK exposes much more here — we narrow to `create`
      // because that's all `askClaudeForPatch` calls.
      create: async () => ({
        content: [{ type: "text" as const, text: responseText }],
      }),
    },
  } as any;
}

const OWNER = "ai-patch-test-" + Date.now().toString(36);
const REPO = "subject";

async function seedRepo(): Promise<{ baseSha: string }> {
  await initBareRepo(OWNER, REPO);
  // Seed one initial commit on `main` so we have a valid base sha for
  // the patch generator to branch from.
  const res = await createOrUpdateFileOnBranch({
    owner: OWNER,
    name: REPO,
    branch: "main",
    filePath: "src/config.ts",
    bytes: new TextEncoder().encode(
      "export const KEY = 'sk_live_LEAKED';\n"
    ),
    message: "seed",
    authorName: "Seeder",
    authorEmail: "seed@example.com",
  });
  if ("error" in res) {
    throw new Error(`seed failed: ${res.error}`);
  }
  return { baseSha: res.commitSha };
}

describe("generatePatchForGateTestFinding — end-to-end with fake Claude", () => {
  it("returns null when findings array is empty", async () => {
    const out = await generatePatchForGateTestFinding({
      repositoryId: "00000000-0000-0000-0000-000000000000",
      baseSha: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
      findings: [],
      client: fakeClient("{}"),
    });
    expect(out).toBeNull();
  });

  it("returns null when Claude returns zero patches", async () => {
    // No DB needed — we short-circuit before any insert when patches
    // array is empty. Still, we need a resolvable repo row to even
    // reach that branch, so this assertion only fires under HAS_DB.
    if (!HAS_DB) {
      // Without DB the resolver returns null first → still null. Either
      // way, the assertion holds.
      const out = await generatePatchForGateTestFinding({
        repositoryId: "00000000-0000-0000-0000-000000000000",
        baseSha: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
        findings: [
          { id: "f1", path: "src/config.ts", severity: "high", description: "x" },
        ],
        client: fakeClient('{"explanation":"nothing to fix","patches":[]}'),
      });
      expect(out).toBeNull();
      return;
    }

    // HAS_DB path: insert a real repo row + user, then run the generator.
    const username = `aipatch_${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 [r] = await db
      .insert(repositories)
      .values({
        ownerId: u.id,
        name: `empty_${Date.now()}`,
        diskPath: `/tmp/${username}/empty`,
        defaultBranch: "main",
      })
      .returning({ id: repositories.id, name: repositories.name });

    // Need a bare repo on disk so getBlob succeeds.
    await initBareRepo(username, r.name);
    const seeded = await createOrUpdateFileOnBranch({
      owner: username,
      name: r.name,
      branch: "main",
      filePath: "src/config.ts",
      bytes: new TextEncoder().encode("x\n"),
      message: "seed",
      authorName: "Seeder",
      authorEmail: "s@e.com",
    });
    if ("error" in seeded) throw new Error("seed failed");

    const out = await generatePatchForGateTestFinding({
      repositoryId: r.id,
      baseSha: seeded.commitSha,
      findings: [
        { id: "f1", path: "src/config.ts", severity: "high", description: "x" },
      ],
      client: fakeClient('{"explanation":"nothing to fix","patches":[]}'),
    });
    expect(out).toBeNull();

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

  it(
    "creates branch + commit + PR when Claude returns a real patch",
    async () => {
      if (!HAS_DB) {
        // Without a DB we can't open a PR, but we can still drive the
        // git-side of the generator by asserting the helper used by the
        // generator (`createOrUpdateFileOnBranch`) lands the patch on a
        // disposable branch. This keeps signal in the DB-less sandbox.
        const { baseSha } = await seedRepo();
        const res = await createOrUpdateFileOnBranch({
          owner: OWNER,
          name: REPO,
          branch: "ai-patch/manual-1",
          filePath: "src/config.ts",
          bytes: new TextEncoder().encode(
            "export const KEY = process.env.API_KEY ?? '';\n"
          ),
          message: "fix",
          authorName: "GlueCron AI",
          authorEmail: "ai@gluecron.com",
        });
        expect("commitSha" in res).toBe(true);
        expect(await refExists(OWNER, REPO, "refs/heads/ai-patch/manual-1")).toBe(
          true
        );
        // The base sha must still exist on main.
        expect(typeof baseSha).toBe("string");
        return;
      }

      // HAS_DB path: full E2E.
      const username = `aipatch_${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 seeded = await createOrUpdateFileOnBranch({
        owner: username,
        name: repoName,
        branch: "main",
        filePath: "src/config.ts",
        bytes: new TextEncoder().encode(
          "export const KEY = 'sk_live_LEAKED';\n"
        ),
        message: "seed",
        authorName: "Seeder",
        authorEmail: "s@e.com",
      });
      if ("error" in seeded) throw new Error("seed failed");

      const cannedPatch = JSON.stringify({
        explanation:
          "Replaced the literal credential with an env-var read so the secret no longer lives in source.",
        patches: [
          {
            path: "src/config.ts",
            new_content:
              "export const KEY = process.env.API_KEY ?? '';\n",
          },
        ],
      });

      const branchOverride = `ai-patch/test-${Date.now()}`;
      const out = await generatePatchForGateTestFinding({
        repositoryId: r.id,
        baseSha: seeded.commitSha,
        findings: [
          {
            id: "hardcoded-secret",
            ruleId: "hardcoded-secret",
            path: "src/config.ts",
            severity: "high",
            line: 1,
            title: "Hardcoded secret",
            description: "API key in source",
          },
        ],
        client: fakeClient(cannedPatch),
        reportUrl: "https://gatetest.example/run/test",
        branchOverride,
      });

      expect(out).not.toBeNull();
      expect(out!.branch).toBe(branchOverride);
      expect(typeof out!.prNumber).toBe("number");

      // Branch exists in the bare repo.
      expect(
        await refExists(username, repoName, `refs/heads/${branchOverride}`)
      ).toBe(true);

      // Commit on the branch contains the new content.
      const branchSha = await resolveRef(
        username,
        repoName,
        branchOverride
      );
      expect(branchSha).not.toBeNull();
      const blob = await getBlob(
        username,
        repoName,
        branchOverride,
        "src/config.ts"
      );
      expect(blob).not.toBeNull();
      expect(blob!.content).toContain("process.env.API_KEY");
      expect(blob!.content).not.toContain("sk_live_LEAKED");

      // PR row exists with the right base/head.
      const [pr] = await db
        .select({
          number: pullRequests.number,
          headBranch: pullRequests.headBranch,
          baseBranch: pullRequests.baseBranch,
          body: pullRequests.body,
        })
        .from(pullRequests)
        .where(eq(pullRequests.repositoryId, r.id))
        .limit(1);
      expect(pr).toBeTruthy();
      expect(pr!.headBranch).toBe(branchOverride);
      expect(pr!.baseBranch).toBe("main");
      expect(pr!.body).toContain(AI_PATCH_MARKER);
      expect(pr!.body).toContain(AI_PATCH_LABEL);
    },
    20000
  );
});

// ---------------------------------------------------------------------------
// Internal helpers (sanity checks — they should resolve without throwing
// against bogus inputs).
// ---------------------------------------------------------------------------

describe("__test internals", () => {
  it("exports the documented helpers", () => {
    expect(typeof __test.findingPath).toBe("function");
    expect(typeof __test.findingDescription).toBe("function");
    expect(typeof __test.resolveOwnerName).toBe("function");
    expect(typeof __test.askClaudeForPatch).toBe("function");
    expect(typeof __test.seedBranchFromBase).toBe("function");
  });

  it("findingPath returns null when neither path nor file is set", () => {
    expect(__test.findingPath({} as any)).toBeNull();
    expect(__test.findingPath({ path: "  " } as any)).toBeNull();
    expect(__test.findingPath({ file: "x.ts" } as any)).toBe("x.ts");
  });

  it("askClaudeForPatch tolerates an invalid JSON envelope", async () => {
    const out = await __test.askClaudeForPatch(
      fakeClient("not json at all"),
      { ruleId: "x", path: "a.ts" },
      "a.ts",
      "content"
    );
    expect(out).toBeNull();
  });
});