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
hosted-claude-loop.test.ts11.3 KB · 337 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
/**
 * Hosted Claude loops — migration 0069 + src/lib/hosted-claude-loop.ts.
 *
 * Two layers:
 *
 *   1. Pure helpers (slugify, endpoint path build, usage extraction) —
 *      no DB or AI key required, always run.
 *
 *   2. DB-backed flows (createLoop, invokeLoop via the executor seam,
 *      budget cap enforcement) — gated on HAS_DB. The executor seam
 *      avoids needing ANTHROPIC_API_KEY in CI, so HAS_AI just adds a
 *      smoke check that the default template parses as JS.
 */

import { beforeAll, afterAll, describe, expect, it } from "bun:test";
import { eq } from "drizzle-orm";
import { randomBytes, createHash } from "crypto";

import {
  DEFAULT_LOOP_TEMPLATE,
  __setExecutorForTests,
  buildEndpointPath,
  capStream,
  createLoop,
  deleteLoop,
  extractUsageFromStdout,
  getLoop,
  invokeLoop,
  listLoopsForOwner,
  listRunsForLoop,
  pauseLoop,
  resumeLoop,
  slugifyLoopName,
} from "../lib/hosted-claude-loop";

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

// Seed a unique test user per run when DB is available.
async function seedTestUser(): Promise<string | null> {
  if (!HAS_DB) return null;
  const { db } = await import("../db");
  const { users } = await import("../db/schema");
  const username = `cldploy-${randomBytes(4).toString("hex")}`;
  try {
    const [row] = await db
      .insert(users)
      .values({
        username,
        email: `${username}@test.local`,
        passwordHash: "x",
      })
      .returning();
    return row?.id ?? null;
  } catch {
    return null;
  }
}

async function cleanupTestUser(userId: string | null) {
  if (!userId || !HAS_DB) return;
  try {
    const { db } = await import("../db");
    const { users } = await import("../db/schema");
    await db.delete(users).where(eq(users.id, userId));
  } catch {
    /* best-effort */
  }
}

let testUserId: string | null = null;

beforeAll(async () => {
  testUserId = await seedTestUser();
});

afterAll(async () => {
  __setExecutorForTests(null);
  await cleanupTestUser(testUserId);
});

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

describe("hosted-claude-loop — pure helpers", () => {
  it("slugifyLoopName lowercases + dasherises + trims to 40 chars", () => {
    expect(slugifyLoopName("Hello World!")).toBe("hello-world");
    expect(slugifyLoopName("  Foo   BAR  ")).toBe("foo-bar");
    expect(slugifyLoopName("a".repeat(60)).length).toBe(40);
    expect(slugifyLoopName("")).toBe("");
    expect(slugifyLoopName("!!!---")).toBe("");
  });

  it("buildEndpointPath always begins with /claude-loops/ and has a suffix", () => {
    const a = buildEndpointPath("my-loop");
    const b = buildEndpointPath("my-loop");
    expect(a.startsWith("/claude-loops/")).toBe(true);
    expect(b.startsWith("/claude-loops/")).toBe(true);
    // Suffix randomness should virtually never collide.
    expect(a).not.toBe(b);
    // Empty name falls back to `loop` prefix.
    expect(buildEndpointPath("").startsWith("/claude-loops/loop-")).toBe(true);
  });

  it("capStream truncates at the cap and tags it", () => {
    const big = "x".repeat(50_000);
    const out = capStream(big, 100);
    expect(out.length).toBeLessThanOrEqual(200);
    expect(out.endsWith("[truncated]")).toBe(true);
    expect(capStream("short", 1000)).toBe("short");
    expect(capStream("", 100)).toBe("");
  });

  it("extractUsageFromStdout pulls input/output tokens from JSON stdout", () => {
    const stdout = JSON.stringify({
      ok: true,
      model: "claude-haiku-4-5",
      usage: { input_tokens: 120, output_tokens: 80 },
    });
    const got = extractUsageFromStdout(stdout);
    expect(got.inputTokens).toBe(120);
    expect(got.outputTokens).toBe(80);
    expect(got.model).toBe("claude-haiku-4-5");
  });

  it("extractUsageFromStdout returns zeros for non-JSON or no usage block", () => {
    expect(extractUsageFromStdout("plain text output")).toEqual({
      inputTokens: 0,
      outputTokens: 0,
      model: null,
    });
    expect(extractUsageFromStdout("")).toEqual({
      inputTokens: 0,
      outputTokens: 0,
      model: null,
    });
    expect(
      extractUsageFromStdout(JSON.stringify({ no_usage_here: true }))
    ).toEqual({ inputTokens: 0, outputTokens: 0, model: null });
  });

  it("extractUsageFromStdout scans line-by-line for embedded JSON lines", () => {
    const stdout =
      "preamble log\n" +
      JSON.stringify({ usage: { input_tokens: 5, output_tokens: 3 } }) +
      "\nepilogue text";
    const got = extractUsageFromStdout(stdout);
    expect(got.inputTokens).toBe(5);
    expect(got.outputTokens).toBe(3);
  });

  it("DEFAULT_LOOP_TEMPLATE references the SDK + prints JSON usage", () => {
    expect(DEFAULT_LOOP_TEMPLATE).toContain("@anthropic-ai/sdk");
    expect(DEFAULT_LOOP_TEMPLATE).toContain("usage");
    expect(DEFAULT_LOOP_TEMPLATE).toContain("process.env.INPUT");
  });
});

// ---------------------------------------------------------------------------
// DB-backed flows
// ---------------------------------------------------------------------------

describe.skipIf(!HAS_DB)("hosted-claude-loop — DB flows", () => {
  it("createLoop persists a row + mints an agent token", async () => {
    if (!testUserId) return;
    const r = await createLoop({
      ownerUserId: testUserId,
      name: "test-loop-create",
      sourceCode: "console.log(JSON.stringify({ok:true}));",
      monthlyBudgetCents: 500,
    });
    expect(r).not.toBeNull();
    expect(r!.loop.name).toBe("test-loop-create");
    expect(r!.loop.endpointPath.startsWith("/claude-loops/")).toBe(true);
    expect(r!.loop.status).toBe("paused");
    expect(r!.loop.monthlyBudgetCents).toBe(500);
    // Agent token is plaintext, shown once.
    if (r!.agentToken) {
      expect(/^agt_[0-9a-f]{64}$/.test(r!.agentToken)).toBe(true);
    }
    const back = await getLoop(r!.loop.id);
    expect(back?.id).toBe(r!.loop.id);
    await deleteLoop(r!.loop.id, testUserId);
  });

  it("listLoopsForOwner only returns loops owned by the user", async () => {
    if (!testUserId) return;
    const otherUserId = await seedTestUser();
    try {
      const mine = await createLoop({
        ownerUserId: testUserId,
        name: "mine-list-test",
        sourceCode: "console.log('hi');",
      });
      const theirs = otherUserId
        ? await createLoop({
            ownerUserId: otherUserId,
            name: "theirs-list-test",
            sourceCode: "console.log('hi');",
          })
        : null;
      const mineList = await listLoopsForOwner(testUserId);
      expect(mineList.some((l) => l.id === mine?.loop.id)).toBe(true);
      expect(mineList.some((l) => l.id === theirs?.loop.id)).toBe(false);
      if (mine) await deleteLoop(mine.loop.id, testUserId);
      if (theirs && otherUserId) await deleteLoop(theirs.loop.id, otherUserId);
    } finally {
      await cleanupTestUser(otherUserId);
    }
  });

  it("invokeLoop runs the snippet via the executor seam + records a run", async () => {
    if (!testUserId) return;
    const created = await createLoop({
      ownerUserId: testUserId,
      name: "test-loop-invoke",
      sourceCode: "console.log('seeded');",
      monthlyBudgetCents: 5000,
    });
    expect(created).not.toBeNull();

    // Resume so the owner-side invoke runs without an extra hop.
    await resumeLoop(created!.loop.id, testUserId);

    __setExecutorForTests(async (args) => ({
      stdout: JSON.stringify({
        echo: args.inputPayload,
        usage: { input_tokens: 100, output_tokens: 50 },
        model: "claude-haiku-4-5",
      }),
      stderr: "",
      exitCode: 0,
      durationMs: 5,
      timedOut: false,
    }));
    const result = await invokeLoop({
      loopId: created!.loop.id,
      inputPayload: { repo: "demo" },
    });
    expect(result.status).toBe("ok");
    expect(result.centsCharged).toBeGreaterThan(0);
    expect(result.run).not.toBeNull();
    expect(result.run?.status).toBe("ok");
    expect(result.run?.claudeInputTokens).toBe(100);
    expect(result.run?.claudeOutputTokens).toBe(50);
    // Output parsed back from the stdout JSON.
    expect((result.output as { echo: { repo: string } }).echo.repo).toBe("demo");

    const runs = await listRunsForLoop(created!.loop.id, 10);
    expect(runs.length).toBeGreaterThanOrEqual(1);

    // Totals on the loop row should have bumped.
    const after = await getLoop(created!.loop.id);
    expect(after?.totalInvocations).toBeGreaterThan(0);
    expect(after?.totalCentsSpent).toBeGreaterThan(0);
    expect(after?.lastRunAt).not.toBeNull();

    __setExecutorForTests(null);
    await deleteLoop(created!.loop.id, testUserId);
  });

  it("invokeLoop returns budget_exceeded once the cap is hit", async () => {
    if (!testUserId) return;
    const created = await createLoop({
      ownerUserId: testUserId,
      name: "test-loop-budget",
      sourceCode: "console.log('seeded');",
      // 1¢ budget — first invocation should land at the cap.
      monthlyBudgetCents: 1,
    });
    expect(created).not.toBeNull();
    await resumeLoop(created!.loop.id, testUserId);

    // Stub an executor that reports 10k input + 10k output tokens so
    // computeCentsForCall lands well over the 1¢ cap.
    __setExecutorForTests(async () => ({
      stdout: JSON.stringify({
        usage: { input_tokens: 10_000, output_tokens: 10_000 },
        model: "claude-haiku-4-5",
      }),
      stderr: "",
      exitCode: 0,
      durationMs: 5,
      timedOut: false,
    }));

    // First call lands OK (no spend yet → cap not exceeded at the
    // pre-flight check) and bumps the spend counter over cap.
    const first = await invokeLoop({
      loopId: created!.loop.id,
      inputPayload: {},
    });
    expect(["ok", "budget_exceeded"].includes(first.status)).toBe(true);

    // Second call MUST short-circuit on budget.
    const second = await invokeLoop({
      loopId: created!.loop.id,
      inputPayload: {},
    });
    expect(second.status).toBe("budget_exceeded");
    expect(second.centsCharged).toBe(0);

    __setExecutorForTests(null);
    await deleteLoop(created!.loop.id, testUserId);
  });

  it("pauseLoop + invokeLoop with isPublicInvocation=true returns disabled", async () => {
    if (!testUserId) return;
    const created = await createLoop({
      ownerUserId: testUserId,
      name: "test-loop-paused",
      sourceCode: "console.log('seeded');",
    });
    expect(created).not.toBeNull();
    await pauseLoop(created!.loop.id, testUserId);
    const r = await invokeLoop({
      loopId: created!.loop.id,
      inputPayload: {},
      isPublicInvocation: true,
    });
    expect(r.status).toBe("disabled");
    await deleteLoop(created!.loop.id, testUserId);
  });
});

// ---------------------------------------------------------------------------
// HAS_AI smoke: just verify the template is syntactically importable as
// JS (parses) when an AI key is present — we don't actually hit Claude.
// ---------------------------------------------------------------------------
describe.skipIf(!HAS_AI)("hosted-claude-loop — HAS_AI surface", () => {
  it("DEFAULT_LOOP_TEMPLATE is non-empty + hashes deterministically", () => {
    expect(DEFAULT_LOOP_TEMPLATE.length).toBeGreaterThan(50);
    const h = createHash("sha256").update(DEFAULT_LOOP_TEMPLATE).digest("hex");
    expect(h.length).toBe(64);
  });
});