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
branch-previews.test.ts12.7 KB · 409 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
/**
 * Tests for src/lib/branch-previews.ts — per-branch preview URLs.
 *
 * Two layers:
 *
 *   1. Pure helpers — URL slugging, expires-in formatting, status
 *      label mapping. No DB, no network. Always run.
 *
 *   2. DB-backed pipeline — gated on HAS_DB so the suite stays green
 *      on machines without Postgres. Covers:
 *        - enqueue creates a row
 *        - re-push to the same branch DEDUPES (replaces commit_sha + URL,
 *          resets status to 'building')
 *        - markPreviewReady / markPreviewFailed flip status as expected
 *        - expireOldPreviews flips expired rows to 'expired'
 *        - listPreviewsForRepo / getPreviewForBranch round-trip
 */

import { afterEach, describe, expect, it } from "bun:test";
import {
  buildPreviewUrl,
  enqueuePreviewBuild,
  expireOldPreviews,
  formatExpiresIn,
  getPreviewForBranch,
  listPreviewsForRepo,
  markPreviewFailed,
  markPreviewReady,
  previewStatusLabel,
  slugifyForUrl,
} from "../lib/branch-previews";
import { db } from "../db";
import { branchPreviews, repositories, users } from "../db/schema";
import { eq } from "drizzle-orm";

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

// ---------------------------------------------------------------------------
// 1. Pure helpers
// ---------------------------------------------------------------------------

describe("branch-previews — slugifyForUrl", () => {
  it("lowercases and replaces non-alphanumerics with dashes", () => {
    expect(slugifyForUrl("Feature/Branch_42")).toBe("feature-branch-42");
  });

  it("strips leading + trailing dashes", () => {
    expect(slugifyForUrl("--foo--")).toBe("foo");
  });

  it("clips to 50 characters", () => {
    const long = "a".repeat(200);
    expect(slugifyForUrl(long).length).toBe(50);
  });

  it("returns empty string for empty input", () => {
    expect(slugifyForUrl("")).toBe("");
  });
});

describe("branch-previews — buildPreviewUrl", () => {
  it("produces a wildcard subdomain URL with default domain", () => {
    const prev = process.env.PREVIEW_DOMAIN;
    delete process.env.PREVIEW_DOMAIN;
    try {
      const url = buildPreviewUrl("alice", "site", "feat/new");
      expect(url).toBe("https://feat-new-alice-site.preview.gluecron.com");
    } finally {
      if (prev !== undefined) process.env.PREVIEW_DOMAIN = prev;
    }
  });

  it("honours a custom PREVIEW_DOMAIN env var", () => {
    const prev = process.env.PREVIEW_DOMAIN;
    process.env.PREVIEW_DOMAIN = "preview.acme.dev";
    try {
      const url = buildPreviewUrl("a", "b", "main-thing");
      expect(url).toBe("https://main-thing-a-b.preview.acme.dev");
    } finally {
      if (prev === undefined) delete process.env.PREVIEW_DOMAIN;
      else process.env.PREVIEW_DOMAIN = prev;
    }
  });

  it("falls back to 'branch' when branch slug is empty", () => {
    const url = buildPreviewUrl("a", "b", "///");
    expect(url).toContain("branch-");
  });

  it("strips scheme from PREVIEW_DOMAIN if accidentally included", () => {
    const prev = process.env.PREVIEW_DOMAIN;
    process.env.PREVIEW_DOMAIN = "https://preview.foo.com";
    try {
      const url = buildPreviewUrl("a", "b", "c");
      expect(url).toBe("https://c-a-b.preview.foo.com");
    } finally {
      if (prev === undefined) delete process.env.PREVIEW_DOMAIN;
      else process.env.PREVIEW_DOMAIN = prev;
    }
  });
});

describe("branch-previews — formatExpiresIn", () => {
  it("returns 'expired' for past timestamps", () => {
    const now = new Date("2026-01-01T12:00:00Z");
    const past = new Date("2026-01-01T11:00:00Z");
    expect(formatExpiresIn(past, now)).toBe("expired");
  });

  it("returns 'less than a minute' for sub-minute futures", () => {
    const now = new Date("2026-01-01T12:00:00Z");
    const soon = new Date("2026-01-01T12:00:30Z");
    expect(formatExpiresIn(soon, now)).toBe("less than a minute");
  });

  it("returns just minutes when under an hour away", () => {
    const now = new Date("2026-01-01T12:00:00Z");
    const future = new Date("2026-01-01T12:42:00Z");
    expect(formatExpiresIn(future, now)).toBe("42m");
  });

  it("returns hours + minutes when over an hour away", () => {
    const now = new Date("2026-01-01T12:00:00Z");
    const future = new Date("2026-01-02T08:30:00Z");
    expect(formatExpiresIn(future, now)).toBe("20h 30m");
  });

  it("returns em-dash for null", () => {
    expect(formatExpiresIn(null)).toBe("—");
  });
});

describe("branch-previews — previewStatusLabel", () => {
  it("maps known statuses to human labels", () => {
    expect(previewStatusLabel("building")).toBe("Building");
    expect(previewStatusLabel("ready")).toBe("Ready");
    expect(previewStatusLabel("failed")).toBe("Failed");
    expect(previewStatusLabel("expired")).toBe("Expired");
  });

  it("passes through unknown statuses unchanged", () => {
    expect(previewStatusLabel("weird")).toBe("weird");
  });
});

// ---------------------------------------------------------------------------
// 2. Graceful no-ops without DB — must not throw on empty/missing inputs
// ---------------------------------------------------------------------------

describe("branch-previews — graceful no-ops", () => {
  it("enqueuePreviewBuild returns null for missing required fields", async () => {
    expect(
      await enqueuePreviewBuild({
        repositoryId: "",
        ownerName: "a",
        repoName: "b",
        branchName: "c",
        commitSha: "d",
      })
    ).toBeNull();
    expect(
      await enqueuePreviewBuild({
        repositoryId: "x",
        ownerName: "a",
        repoName: "b",
        branchName: "",
        commitSha: "d",
      })
    ).toBeNull();
    expect(
      await enqueuePreviewBuild({
        repositoryId: "x",
        ownerName: "a",
        repoName: "b",
        branchName: "c",
        commitSha: "",
      })
    ).toBeNull();
  });

  it("getPreviewForBranch returns null for empty inputs", async () => {
    expect(await getPreviewForBranch("", "main")).toBeNull();
    expect(await getPreviewForBranch("repo-id", "")).toBeNull();
  });

  it("listPreviewsForRepo returns [] for empty repository id", async () => {
    expect(await listPreviewsForRepo("")).toEqual([]);
  });

  it("markPreviewReady / markPreviewFailed swallow empty ids", async () => {
    await markPreviewReady("");
    await markPreviewFailed("", "err");
    // No throw = pass.
    expect(true).toBe(true);
  });
});

// ---------------------------------------------------------------------------
// 3. DB-backed pipeline
// ---------------------------------------------------------------------------

const TEST_USER_PREFIX = "previewtest_";

async function seedRepo(): Promise<{ userId: string; repoId: string }> {
  const username =
    TEST_USER_PREFIX + Math.random().toString(36).slice(2, 10);
  const [user] = await db
    .insert(users)
    .values({
      username,
      email: `${username}@previewtest.local`,
      passwordHash: "$2b$10$" + "x".repeat(53),
    })
    .returning();
  const [repo] = await db
    .insert(repositories)
    .values({
      name: "previews-test-" + Math.random().toString(36).slice(2, 8),
      ownerId: user!.id,
      diskPath: "/tmp/previews-test-" + Math.random().toString(36).slice(2, 8),
    })
    .returning();
  return { userId: user!.id, repoId: repo!.id };
}

async function cleanupRepo(userId: string): Promise<void> {
  try {
    await db.delete(repositories).where(eq(repositories.ownerId, userId));
    await db.delete(users).where(eq(users.id, userId));
  } catch {
    /* best effort */
  }
}

describe.skipIf(!HAS_DB)("branch-previews — DB pipeline", () => {
  let userId = "";
  let repoId = "";

  afterEach(async () => {
    if (userId) await cleanupRepo(userId);
    userId = "";
    repoId = "";
  });

  it("enqueue creates a building row with a computed preview URL", async () => {
    ({ userId, repoId } = await seedRepo());

    const row = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "alice",
      repoName: "site",
      branchName: "feat/a",
      commitSha: "abc1234567890",
    });
    expect(row).not.toBeNull();
    expect(row!.status).toBe("building");
    expect(row!.branchName).toBe("feat/a");
    expect(row!.commitSha).toBe("abc1234567890");
    expect(row!.previewUrl).toContain("feat-a-");
    expect(row!.previewUrl.startsWith("https://")).toBe(true);
    expect(row!.expiresAt instanceof Date).toBe(true);
    expect(row!.expiresAt.getTime()).toBeGreaterThan(Date.now());
  });

  it("re-pushing the same branch DEDUPES — same id, new SHA, status='building'", async () => {
    ({ userId, repoId } = await seedRepo());

    const first = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "alice",
      repoName: "site",
      branchName: "feat/b",
      commitSha: "sha-one",
    });
    expect(first).not.toBeNull();

    // Flip to ready so we can verify the upsert resets it.
    await markPreviewReady(first!.id);
    let row = await getPreviewForBranch(repoId, "feat/b");
    expect(row!.status).toBe("ready");

    // Second push to the same branch → upsert path.
    const second = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "alice",
      repoName: "site",
      branchName: "feat/b",
      commitSha: "sha-two",
    });
    expect(second).not.toBeNull();
    expect(second!.id).toBe(first!.id); // same row
    expect(second!.commitSha).toBe("sha-two");
    expect(second!.status).toBe("building");
    expect(second!.buildCompletedAt).toBeNull();

    // And only ONE row exists for the (repo, branch) pair.
    const all = await listPreviewsForRepo(repoId);
    const matching = all.filter((p) => p.branchName === "feat/b");
    expect(matching.length).toBe(1);
  });

  it("markPreviewReady flips status to 'ready' with completed_at", async () => {
    ({ userId, repoId } = await seedRepo());

    const row = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "a",
      repoName: "b",
      branchName: "x",
      commitSha: "abc",
    });
    await markPreviewReady(row!.id);
    const after = await getPreviewForBranch(repoId, "x");
    expect(after!.status).toBe("ready");
    expect(after!.buildCompletedAt).not.toBeNull();
  });

  it("markPreviewFailed records error_message + truncates", async () => {
    ({ userId, repoId } = await seedRepo());

    const row = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "a",
      repoName: "b",
      branchName: "y",
      commitSha: "def",
    });
    const longError = "boom\n".repeat(10_000);
    await markPreviewFailed(row!.id, longError);
    const after = await getPreviewForBranch(repoId, "y");
    expect(after!.status).toBe("failed");
    expect(after!.errorMessage).not.toBeNull();
    expect(after!.errorMessage!.length).toBeLessThanOrEqual(2_000);
  });

  it("expireOldPreviews transitions ready rows past expires_at to 'expired'", async () => {
    ({ userId, repoId } = await seedRepo());

    const row = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "a",
      repoName: "b",
      branchName: "stale",
      commitSha: "old",
    });
    expect(row).not.toBeNull();
    await markPreviewReady(row!.id);

    // Force expires_at into the past.
    await db
      .update(branchPreviews)
      .set({ expiresAt: new Date(Date.now() - 60_000) })
      .where(eq(branchPreviews.id, row!.id));

    const flipped = await expireOldPreviews();
    expect(flipped).toBeGreaterThanOrEqual(1);

    const after = await getPreviewForBranch(repoId, "stale");
    expect(after!.status).toBe("expired");
  });

  it("expireOldPreviews leaves fresh rows alone", async () => {
    ({ userId, repoId } = await seedRepo());

    const row = await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "a",
      repoName: "b",
      branchName: "fresh",
      commitSha: "new",
    });
    expect(row).not.toBeNull();
    await markPreviewReady(row!.id);

    // Default expiresAt is now+24h → expire pass should ignore it.
    await expireOldPreviews();
    const after = await getPreviewForBranch(repoId, "fresh");
    expect(after!.status).toBe("ready");
  });

  it("listPreviewsForRepo orders by build_started_at descending", async () => {
    ({ userId, repoId } = await seedRepo());

    const oldNow = () => new Date(Date.now() - 60_000);
    const newNow = () => new Date();

    await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "a",
      repoName: "b",
      branchName: "old-branch",
      commitSha: "1",
      now: oldNow,
    });
    await enqueuePreviewBuild({
      repositoryId: repoId,
      ownerName: "a",
      repoName: "b",
      branchName: "new-branch",
      commitSha: "2",
      now: newNow,
    });

    const list = await listPreviewsForRepo(repoId);
    expect(list.length).toBe(2);
    expect(list[0]!.branchName).toBe("new-branch");
    expect(list[1]!.branchName).toBe("old-branch");
  });
});