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
platform-deploys.test.ts14.8 KB · 442 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
/**
 * Block N3 — Platform deploy timeline tests.
 *
 * Exercises:
 *   - POST /api/events/deploy/started   (bearer auth + insert + SSE publish)
 *   - POST /api/events/deploy/finished  (update + SSE publish)
 *   - Idempotency on run_id
 *   - GET /admin/deploys + GET /admin/deploys/latest.json gating
 *   - relativeTime / shortSha / formatDuration edge formats
 *
 * The HTTP tests hit `src/routes/events.ts` directly via its default Hono
 * sub-app — no main-app mount needed. SSE publication is observed by
 * subscribing to the broadcaster directly (deterministic in-process).
 *
 * DB-backed assertions degrade gracefully: when DATABASE_URL is unset the
 * insert path returns 500 and we assert {200, 500}-shape contracts. With
 * a real DB they go strict.
 *
 * NO mock.module() calls in this file — we deliberately avoid the
 * spread-from-real mock pattern's global-bleed footgun. The page-render
 * tests exercise the helpers directly + app.request() against the gated
 * routes to confirm 401/403/302 redirects without touching DB rows.
 */

import {
  afterAll,
  afterEach,
  beforeAll,
  beforeEach,
  describe,
  expect,
  it,
} from "bun:test";
import events, { __test as eventsTest } from "../routes/events";
import { __test as pageTest } from "../routes/admin-deploys-page";
import { subscribe, topicSubscriberCount, type SSEEvent } from "../lib/sse";
import app from "../app";

const TOPIC = "platform:deploys";

const VALID_SHA = "a".repeat(40);
const RUN_ID_A = "n3-test-run-aaaa-0001";
const RUN_ID_B = "n3-test-run-bbbb-0002";
const RUN_ID_C = "n3-test-run-cccc-0003";
const RUN_ID_D = "n3-test-run-dddd-0004";

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

async function postStarted(
  body: unknown,
  headers: Record<string, string> = {}
): Promise<Response> {
  return events.request("/deploy/started", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...headers,
    },
    body: typeof body === "string" ? body : JSON.stringify(body),
  });
}

async function postFinished(
  body: unknown,
  headers: Record<string, string> = {}
): Promise<Response> {
  return events.request("/deploy/finished", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...headers,
    },
    body: typeof body === "string" ? body : JSON.stringify(body),
  });
}

beforeAll(() => {
  process.env.DEPLOY_EVENT_TOKEN = "n3-bearer-fixture";
});

afterAll(() => {
  if (origToken === undefined) delete process.env.DEPLOY_EVENT_TOKEN;
  else process.env.DEPLOY_EVENT_TOKEN = origToken;
});

// ---------------------------------------------------------------------------
// /api/events/deploy/started — auth
// ---------------------------------------------------------------------------

describe("POST /api/events/deploy/started — bearer auth", () => {
  it("rejects with 401 when Authorization header is missing", async () => {
    const res = await postStarted({
      sha: VALID_SHA,
      run_id: RUN_ID_A,
      source: "hetzner-deploy",
    });
    expect(res.status).toBe(401);
  });

  it("rejects with 401 when bearer token is wrong", async () => {
    const res = await postStarted(
      { sha: VALID_SHA, run_id: RUN_ID_A, source: "hetzner-deploy" },
      { authorization: "Bearer not-the-real-token" }
    );
    expect(res.status).toBe(401);
  });

  it("rejects with 401 when DEPLOY_EVENT_TOKEN is unset (refuse-by-default)", async () => {
    const saved = process.env.DEPLOY_EVENT_TOKEN;
    delete process.env.DEPLOY_EVENT_TOKEN;
    try {
      const res = await postStarted(
        { sha: VALID_SHA, run_id: RUN_ID_A, source: "hetzner-deploy" },
        { authorization: "Bearer anything" }
      );
      expect(res.status).toBe(401);
      const body = await res.json();
      expect(String(body.error).toLowerCase()).toContain("not configured");
    } finally {
      if (saved !== undefined) process.env.DEPLOY_EVENT_TOKEN = saved;
    }
  });
});

// ---------------------------------------------------------------------------
// /api/events/deploy/started — payload validation
// ---------------------------------------------------------------------------

describe("POST /api/events/deploy/started — payload validation", () => {
  const authHeader = { authorization: "Bearer n3-bearer-fixture" };

  it("rejects malformed JSON with 400", async () => {
    const res = await postStarted("{not-json", authHeader);
    expect(res.status).toBe(400);
  });

  it("rejects missing run_id with 400", async () => {
    const res = await postStarted(
      { sha: VALID_SHA, source: "hetzner-deploy" },
      authHeader
    );
    expect(res.status).toBe(400);
  });

  it("rejects non-hex sha with 400", async () => {
    const res = await postStarted(
      { sha: "xyz!", run_id: RUN_ID_A, source: "hetzner-deploy" },
      authHeader
    );
    expect(res.status).toBe(400);
  });

  it("accepts a 7-char short sha (CI sometimes ships abbrev)", () => {
    const result = eventsTest.validateStarted({
      sha: "a1b2c3d",
      run_id: RUN_ID_A,
      source: "hetzner-deploy",
    });
    expect(result.ok).toBe(true);
  });

  it("rejects an overlong run_id with 400", async () => {
    const res = await postStarted(
      {
        sha: VALID_SHA,
        run_id: "x".repeat(129),
        source: "hetzner-deploy",
      },
      authHeader
    );
    expect(res.status).toBe(400);
  });
});

// ---------------------------------------------------------------------------
// /api/events/deploy/finished — payload validation
// ---------------------------------------------------------------------------

describe("POST /api/events/deploy/finished — payload validation", () => {
  const authHeader = { authorization: "Bearer n3-bearer-fixture" };

  it("rejects unknown status with 400", async () => {
    const res = await postFinished(
      { run_id: RUN_ID_A, status: "weird" },
      authHeader
    );
    expect(res.status).toBe(400);
  });

  it("rejects negative duration_ms with 400", async () => {
    const res = await postFinished(
      { run_id: RUN_ID_A, status: "succeeded", duration_ms: -1 },
      authHeader
    );
    expect(res.status).toBe(400);
  });

  it("accepts succeeded with no error/duration", () => {
    const result = eventsTest.validateFinished({
      run_id: RUN_ID_A,
      status: "succeeded",
    });
    expect(result.ok).toBe(true);
  });

  it("accepts failed with an error string", () => {
    const result = eventsTest.validateFinished({
      run_id: RUN_ID_A,
      status: "failed",
      duration_ms: 42_000,
      error: "smoke test returned 502",
    });
    expect(result.ok).toBe(true);
  });

  it("caps very long error strings to 8 KB on the validator", () => {
    const result = eventsTest.validateFinished({
      run_id: RUN_ID_A,
      status: "failed",
      error: "x".repeat(20_000),
    });
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect((result.payload.error || "").length).toBeLessThanOrEqual(
        8 * 1024
      );
    }
  });
});

// ---------------------------------------------------------------------------
// SSE publication — verified in-process via lib/sse. No DB round-trip needed
// to confirm publish() fires (we always publish on success), but we DO need
// the DB INSERT to succeed for the started handler to reach the publish
// branch. Skipped when DATABASE_URL is unset.
// ---------------------------------------------------------------------------

describe("/api/events/deploy/started — DB-aware insert + publish", () => {
  const authHeader = { authorization: "Bearer n3-bearer-fixture" };

  // Hold every event the broadcaster delivers for `platform:deploys` while
  // a test is running. Unsubscribe in afterEach so the suite leaves the
  // broadcaster's subscriber count at zero — sse.test.ts asserts that
  // contract.
  let received: SSEEvent[] = [];
  let unsubscribe: (() => void) | null = null;

  beforeEach(() => {
    received = [];
    unsubscribe = subscribe(TOPIC, (e) => received.push(e));
  });

  afterEach(() => {
    if (unsubscribe) unsubscribe();
    unsubscribe = null;
    expect(topicSubscriberCount(TOPIC)).toBe(0);
  });

  it("first delivery: inserts a row + publishes deploy.started (DB only)", async () => {
    const res = await postStarted(
      { sha: VALID_SHA, run_id: RUN_ID_B, source: "hetzner-deploy" },
      authHeader
    );
    if (HAS_DB) {
      expect(res.status).toBe(200);
      const body = await res.json();
      expect(body.ok).toBe(true);
      expect(body.duplicate).toBe(false);
      // publish() is synchronous so by the time await returns the event
      // has already been pushed to subscribers.
      expect(received.length).toBe(1);
      expect(received[0]?.event).toBe("deploy.started");
      const data = received[0]?.data as any;
      expect(data.run_id).toBe(RUN_ID_B);
      expect(data.sha).toBe(VALID_SHA);
      expect(data.status).toBe("in_progress");
    } else {
      expect([200, 500]).toContain(res.status);
    }
  });

  it("idempotency: replaying the same run_id returns duplicate:true and does NOT republish", async () => {
    const payload = {
      sha: VALID_SHA,
      run_id: RUN_ID_C,
      source: "hetzner-deploy",
    };
    const first = await postStarted(payload, authHeader);
    const before = received.length;
    const second = await postStarted(payload, authHeader);
    if (HAS_DB) {
      expect(first.status).toBe(200);
      expect(second.status).toBe(200);
      const secondBody = await second.json();
      expect(secondBody.duplicate).toBe(true);
      // No new SSE event from the duplicate.
      expect(received.length).toBe(before);
    } else {
      expect([200, 500]).toContain(first.status);
      expect([200, 500]).toContain(second.status);
    }
  });
});

describe("/api/events/deploy/finished — DB-aware update + publish", () => {
  const authHeader = { authorization: "Bearer n3-bearer-fixture" };

  let received: SSEEvent[] = [];
  let unsubscribe: (() => void) | null = null;

  beforeEach(() => {
    received = [];
    unsubscribe = subscribe(TOPIC, (e) => received.push(e));
  });

  afterEach(() => {
    if (unsubscribe) unsubscribe();
    unsubscribe = null;
    expect(topicSubscriberCount(TOPIC)).toBe(0);
  });

  it("updates the matching row + publishes deploy.finished (DB only)", async () => {
    // Seed via /started so the update path has a row to flip.
    await postStarted(
      { sha: VALID_SHA, run_id: RUN_ID_D, source: "hetzner-deploy" },
      authHeader
    );
    const before = received.length;
    const res = await postFinished(
      {
        run_id: RUN_ID_D,
        status: "succeeded",
        duration_ms: 12_000,
      },
      authHeader
    );
    if (HAS_DB) {
      expect(res.status).toBe(200);
      // After /started we get one event; after /finished we should get a
      // second on top of it.
      expect(received.length).toBeGreaterThan(before);
      const finishedEvent = received[received.length - 1];
      expect(finishedEvent?.event).toBe("deploy.finished");
      const data = finishedEvent?.data as any;
      expect(data.run_id).toBe(RUN_ID_D);
      expect(data.status).toBe("succeeded");
    } else {
      expect([200, 500]).toContain(res.status);
    }
  });
});

// ---------------------------------------------------------------------------
// /admin/deploys gating — hit the real app router to confirm anonymous
// + non-admin paths are blocked.
// ---------------------------------------------------------------------------

describe("/admin/deploys gating", () => {
  it("redirects anonymous users to /login (HTML page)", async () => {
    const res = await app.request("/admin/deploys", { redirect: "manual" });
    // Either 302 to /login (gate redirect) or 403 if the gate flips to
    // forbidden first. Both are non-200 / non-leak.
    expect([302, 303, 401, 403]).toContain(res.status);
  });

  it("returns 401 JSON for anonymous on /admin/deploys/latest.json", async () => {
    const res = await app.request("/admin/deploys/latest.json");
    expect([401, 403]).toContain(res.status);
    const body = await res.json();
    expect(body.ok).toBe(false);
  });

  it("renders a page (200 + HTML) for the site admin", async () => {
    // Without spinning up an authed session this case is hard to verify
    // end-to-end. We assert the route is at least registered + does not
    // 404 — the gating branches above prove the auth contract.
    const res = await app.request("/admin/deploys", { redirect: "manual" });
    expect(res.status).not.toBe(404);
  });
});

// ---------------------------------------------------------------------------
// Status-pill helper formats — pure functions, no I/O.
// ---------------------------------------------------------------------------

describe("admin-deploys-page helpers — relativeTime", () => {
  const { relativeTime, shortSha, formatDuration } = pageTest;
  const ANCHOR = new Date("2026-05-14T12:00:00.000Z");
  const ago = (ms: number) => new Date(ANCHOR.getTime() - ms);

  it("'just now' for <5s", () => {
    expect(relativeTime(ago(0), ANCHOR)).toBe("just now");
    expect(relativeTime(ago(2_000), ANCHOR)).toBe("just now");
    expect(relativeTime(ago(4_999), ANCHOR)).toBe("just now");
  });

  it("'Ns ago' for 5-59 seconds", () => {
    expect(relativeTime(ago(12_000), ANCHOR)).toBe("12s ago");
    expect(relativeTime(ago(59_000), ANCHOR)).toBe("59s ago");
  });

  it("'Nm ago' for 1-59 minutes", () => {
    expect(relativeTime(ago(60_000), ANCHOR)).toBe("1m ago");
    expect(relativeTime(ago(3 * 60_000), ANCHOR)).toBe("3m ago");
    expect(relativeTime(ago(59 * 60_000), ANCHOR)).toBe("59m ago");
  });

  it("'Nh ago' for 1-23 hours", () => {
    expect(relativeTime(ago(60 * 60_000), ANCHOR)).toBe("1h ago");
    expect(relativeTime(ago(2 * 60 * 60_000), ANCHOR)).toBe("2h ago");
    expect(relativeTime(ago(23 * 60 * 60_000), ANCHOR)).toBe("23h ago");
  });

  it("'Nd ago' for ≥1 day", () => {
    expect(relativeTime(ago(24 * 60 * 60_000), ANCHOR)).toBe("1d ago");
    expect(relativeTime(ago(3 * 24 * 60 * 60_000), ANCHOR)).toBe("3d ago");
  });

  it("clamps clock skew (future Date) to 'just now'", () => {
    const future = new Date(ANCHOR.getTime() + 5_000);
    expect(relativeTime(future, ANCHOR)).toBe("just now");
  });

  it("shortSha returns 7 lowercased hex chars", () => {
    expect(shortSha("DEADBEEFCAFE1234")).toBe("deadbee");
    expect(shortSha("abc")).toBe("abc");
    expect(shortSha("")).toBe("");
  });

  it("formatDuration handles seconds + minutes + invalid input", () => {
    expect(formatDuration(0)).toBe("0s");
    expect(formatDuration(12_000)).toBe("12s");
    expect(formatDuration(59_000)).toBe("59s");
    expect(formatDuration(60_000)).toBe("1m");
    expect(formatDuration(74_000)).toBe("1m 14s");
    expect(formatDuration(null)).toBe("—");
    expect(formatDuration(undefined)).toBe("—");
    expect(formatDuration(-5)).toBe("—");
    expect(formatDuration(NaN)).toBe("—");
  });
});