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
Blame · Line-by-line history

vapron-deploy.test.ts

Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.

vapron-deploy.test.tsBlame400 lines · 2 contributors
43cf9b0Claude1/**
9ecf5a4Claude2 * BLK-016 — Vapron deploy webhook sender.
43cf9b0Claude3 *
9ecf5a4Claude4 * Asserts that `triggerVapronDeploy` (in `src/hooks/post-receive.ts`)
ba93444Claude5 * matches the wire contract documented at the top of that helper, which
9ecf5a4Claude6 * is the inbound contract for Vapron's
ba93444Claude7 * `apps/api/src/webhooks/gluecron-push.ts` receiver:
43cf9b0Claude8 *
9ecf5a4Claude9 * POST https://vapron.ai/api/webhooks/gluecron-push
43cf9b0Claude10 * Content-Type: application/json
ba93444Claude11 * X-Gluecron-Signature: sha256=<hex(hmac-sha256(body, secret))>
12 *
13 * body = {
14 * event: "push",
15 * repository: { full_name },
16 * ref, after, before,
17 * pusher: { name, email },
18 * commits: [...]
19 * }
20 *
21 * Plus at-least-once delivery: 5 attempts on 5xx with exponential backoff,
22 * stop on first 2xx or unrecoverable 4xx.
43cf9b0Claude23 *
24 * The helper swallows DB errors, so these tests work without a real DB.
25 */
26
27import { afterEach, beforeEach, describe, expect, it } from "bun:test";
ba93444Claude28import { createHmac } from "crypto";
43cf9b0Claude29import { __test } from "../hooks/post-receive";
9ecf5a4Claude30import { config } from "../lib/config";
43cf9b0Claude31
9ecf5a4Claude32const { triggerVapronDeploy, signBody } = __test;
43cf9b0Claude33
34interface CapturedCall {
35 url: string;
36 init: RequestInit;
37}
38
39const origSecret = process.env.GLUECRON_WEBHOOK_SECRET;
9ecf5a4Claude40const origUrl = process.env.VAPRON_DEPLOY_URL;
41const origRepo = process.env.VAPRON_REPO;
ba93444Claude42
43const NULL_REPO_ID = "00000000-0000-0000-0000-000000000000";
44const ZERO_SHA = "0000000000000000000000000000000000000000";
43cf9b0Claude45
ba93444Claude46function makeArgs(overrides: Partial<{
47 owner: string;
48 repo: string;
49 before: string;
50 after: string;
51 ref: string;
52 branch: string;
53 repositoryId: string;
54}> = {}) {
55 return {
56 owner: "ccantynz-alt",
9ecf5a4Claude57 repo: "vapron",
ba93444Claude58 before: ZERO_SHA,
59 after: "a".repeat(40),
60 ref: "refs/heads/Main",
61 branch: "Main",
62 repositoryId: NULL_REPO_ID,
63 ...overrides,
64 };
65}
43cf9b0Claude66
ba93444Claude67function captureFetch(
68 responder: (callIdx: number) => Response | Promise<Response> = () =>
43cf9b0Claude69 new Response(
ba93444Claude70 JSON.stringify({ ok: true, deploymentId: "d1" }),
43cf9b0Claude71 { status: 200, headers: { "Content-Type": "application/json" } }
72 )
ba93444Claude73): { calls: CapturedCall[]; fn: typeof fetch } {
43cf9b0Claude74 const calls: CapturedCall[] = [];
ba93444Claude75 const fn = (async (
43cf9b0Claude76 input: RequestInfo | URL,
77 init: RequestInit = {}
78 ): Promise<Response> => {
ba93444Claude79 const i = calls.length;
43cf9b0Claude80 calls.push({ url: String(input), init });
ba93444Claude81 return responder(i);
82 }) as unknown as typeof fetch;
83 return { calls, fn };
43cf9b0Claude84}
85
ba93444Claude86const noSleep = async (_ms: number) => {};
87
88describe("hooks/post-receive — signBody", () => {
89 it("returns null when no secret", () => {
90 expect(signBody("any body", "")).toBeNull();
91 });
92
93 it("produces sha256=<hex hmac>", () => {
94 const body = '{"event":"push"}';
95 const secret = "topsecret";
96 const expected =
97 "sha256=" + createHmac("sha256", secret).update(body).digest("hex");
98 expect(signBody(body, secret)).toBe(expected);
99 });
100
101 it("is deterministic for the same input", () => {
102 const a = signBody("body", "k");
103 const b = signBody("body", "k");
104 expect(a).toBe(b);
105 });
43cf9b0Claude106
ba93444Claude107 it("changes when the body changes", () => {
108 const a = signBody("body1", "k");
109 const b = signBody("body2", "k");
110 expect(a).not.toBe(b);
111 });
112});
43cf9b0Claude113
9ecf5a4Claude114describe("hooks/post-receive — triggerVapronDeploy (BLK-016 sender)", () => {
43cf9b0Claude115 beforeEach(() => {
116 delete process.env.GLUECRON_WEBHOOK_SECRET;
9ecf5a4Claude117 delete process.env.VAPRON_DEPLOY_URL;
118 delete process.env.VAPRON_REPO;
119 delete process.env.VAPRON_HMAC_SECRET;
120 // legacy names must not leak into the default-URL assertions
43cf9b0Claude121 delete process.env.CRONTECH_DEPLOY_URL;
ba93444Claude122 delete process.env.CRONTECH_REPO;
9ecf5a4Claude123 delete process.env.CRONTECH_HMAC_SECRET;
43cf9b0Claude124 });
125
126 afterEach(() => {
127 if (origSecret === undefined) delete process.env.GLUECRON_WEBHOOK_SECRET;
128 else process.env.GLUECRON_WEBHOOK_SECRET = origSecret;
9ecf5a4Claude129 if (origUrl === undefined) delete process.env.VAPRON_DEPLOY_URL;
130 else process.env.VAPRON_DEPLOY_URL = origUrl;
131 if (origRepo === undefined) delete process.env.VAPRON_REPO;
132 else process.env.VAPRON_REPO = origRepo;
43cf9b0Claude133 });
134
135 it("is exported from __test", () => {
9ecf5a4Claude136 expect(typeof triggerVapronDeploy).toBe("function");
43cf9b0Claude137 });
138
9ecf5a4Claude139 it("POSTs to /api/webhooks/gluecron-push (matches Vapron receiver path)", async () => {
ba93444Claude140 const { calls, fn } = captureFetch();
43cf9b0Claude141
9ecf5a4Claude142 await triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep });
43cf9b0Claude143
144 expect(calls.length).toBe(1);
145 expect(calls[0]!.url).toBe(
9ecf5a4Claude146 "https://vapron.ai/api/webhooks/gluecron-push"
43cf9b0Claude147 );
ba93444Claude148 expect(calls[0]!.url).not.toContain("/api/hooks/gluecron/push");
43cf9b0Claude149 expect(calls[0]!.init.method).toBe("POST");
150 });
151
5164fabClaude152 it("posts a GitHub-shaped push payload (event, repository, ref, before/after, pusher, commits, sent_at, source)", async () => {
153 const after = "b".repeat(40);
154 const before = "c".repeat(40);
155 const { calls, fn } = captureFetch();
43cf9b0Claude156
9ecf5a4Claude157 await triggerVapronDeploy(
ba93444Claude158 makeArgs({
159 owner: "acme",
160 repo: "api",
161 after,
162 before,
163 ref: "refs/heads/Main",
164 branch: "Main",
165 }),
166 { fetchImpl: fn, sleep: noSleep }
43cf9b0Claude167 );
168
ba93444Claude169 const body = JSON.parse(String(calls[0]!.init.body));
170 expect(body.event).toBe("push");
171 expect(body.repository).toEqual({ full_name: "acme/api" });
172 expect(body.ref).toBe("refs/heads/Main");
173 expect(body.after).toBe(after);
174 expect(body.before).toBe(before);
175 expect(body.pusher).toBeDefined();
176 expect(typeof body.pusher.name).toBe("string");
177 expect(typeof body.pusher.email).toBe("string");
178 expect(Array.isArray(body.commits)).toBe(true);
179 expect(typeof body.sent_at).toBe("string");
180 expect(new Date(body.sent_at).toString()).not.toBe("Invalid Date");
181 expect(body.source).toBe("gluecron");
182 });
183
184 it("signs the body with HMAC-SHA256 in X-Gluecron-Signature when secret is set", async () => {
185 process.env.GLUECRON_WEBHOOK_SECRET = "shared-vultr-secret";
186 const { calls, fn } = captureFetch();
187
9ecf5a4Claude188 await triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep });
ba93444Claude189
43cf9b0Claude190 const headers = calls[0]!.init.headers as Record<string, string>;
ba93444Claude191 const sentBody = String(calls[0]!.init.body);
192 const expected =
193 "sha256=" +
194 createHmac("sha256", "shared-vultr-secret")
195 .update(sentBody)
196 .digest("hex");
197 expect(headers["X-Gluecron-Signature"]).toBe(expected);
43cf9b0Claude198 expect(headers["Content-Type"]).toBe("application/json");
199 });
200
ba93444Claude201 it("omits X-Gluecron-Signature when no secret is configured", async () => {
202 const { calls, fn } = captureFetch();
43cf9b0Claude203
9ecf5a4Claude204 await triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep });
43cf9b0Claude205
206 const headers = calls[0]!.init.headers as Record<string, string>;
ba93444Claude207 expect(headers["X-Gluecron-Signature"]).toBeUndefined();
43cf9b0Claude208 });
209
e61e6cdccanty labs210 it("sends Authorization: Bearer <VAPRON_API_KEY> when the tenant key is set", async () => {
211 process.env.VAPRON_API_KEY = "vap_tenant_key_123";
212 try {
213 const { calls, fn } = captureFetch();
214
215 await triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep });
216
217 const headers = calls[0]!.init.headers as Record<string, string>;
218 expect(headers["Authorization"]).toBe("Bearer vap_tenant_key_123");
219 } finally {
220 delete process.env.VAPRON_API_KEY;
221 }
222 });
223
224 it("omits Authorization when no VAPRON_API_KEY is configured", async () => {
225 delete process.env.VAPRON_API_KEY;
226 const { calls, fn } = captureFetch();
227
228 await triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep });
229
230 const headers = calls[0]!.init.headers as Record<string, string>;
231 expect(headers["Authorization"]).toBeUndefined();
232 });
233
ba93444Claude234 it("attaches X-Gluecron-Event=push and a non-empty X-Gluecron-Delivery id", async () => {
235 const { calls, fn } = captureFetch();
236
9ecf5a4Claude237 await triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep });
ba93444Claude238
239 const headers = calls[0]!.init.headers as Record<string, string>;
240 expect(headers["X-Gluecron-Event"]).toBe("push");
241 expect(headers["X-Gluecron-Delivery"]).toBeDefined();
242 expect(headers["X-Gluecron-Delivery"]!.length).toBeGreaterThan(0);
243 });
244
245 it("ref carries the actual case of the branch (Main, not main)", async () => {
246 const { calls, fn } = captureFetch();
43cf9b0Claude247
9ecf5a4Claude248 await triggerVapronDeploy(
ba93444Claude249 makeArgs({ ref: "refs/heads/Main", branch: "Main" }),
250 { fetchImpl: fn, sleep: noSleep }
43cf9b0Claude251 );
252
253 const body = JSON.parse(String(calls[0]!.init.body));
ba93444Claude254 expect(body.ref).toBe("refs/heads/Main");
255 expect(body.ref).not.toBe("refs/heads/main");
43cf9b0Claude256 });
257
ba93444Claude258 it("retries on 5xx with provided backoff schedule, stops on first 2xx", async () => {
259 const responses = [
260 new Response("", { status: 502 }),
261 new Response("", { status: 503 }),
262 new Response("", { status: 200 }),
263 ];
264 const { calls, fn } = captureFetch((i) => responses[i]!);
265 const sleeps: number[] = [];
43cf9b0Claude266
9ecf5a4Claude267 await triggerVapronDeploy(makeArgs(), {
ba93444Claude268 fetchImpl: fn,
269 sleep: async (ms) => { sleeps.push(ms); },
270 retryDelaysMs: [10, 20, 30, 40, 50],
271 });
272
273 expect(calls.length).toBe(3);
274 // Two waits — between attempt 1→2 and 2→3. None after the successful 3rd.
275 expect(sleeps).toEqual([10, 20]);
276 });
277
278 it("gives up after the configured number of attempts on persistent 5xx", async () => {
279 const { calls, fn } = captureFetch(() => new Response("", { status: 500 }));
280 const sleeps: number[] = [];
281
9ecf5a4Claude282 await triggerVapronDeploy(makeArgs(), {
ba93444Claude283 fetchImpl: fn,
284 sleep: async (ms) => { sleeps.push(ms); },
285 retryDelaysMs: [1, 2, 3, 4, 5],
286 });
287
288 // 5 delays + 1 initial = 6 total attempts (consistent with at-least-once).
289 expect(calls.length).toBe(6);
290 expect(sleeps).toEqual([1, 2, 3, 4, 5]);
291 });
292
293 it("does not retry on unrecoverable 4xx (e.g. 401 invalid signature)", async () => {
294 const { calls, fn } = captureFetch(() => new Response("", { status: 401 }));
295 const sleeps: number[] = [];
296
9ecf5a4Claude297 await triggerVapronDeploy(makeArgs(), {
ba93444Claude298 fetchImpl: fn,
299 sleep: async (ms) => { sleeps.push(ms); },
300 retryDelaysMs: [1, 2, 3, 4, 5],
301 });
43cf9b0Claude302
303 expect(calls.length).toBe(1);
ba93444Claude304 expect(sleeps).toEqual([]);
43cf9b0Claude305 });
306
ba93444Claude307 it("does retry 408 (request timeout) and 429 (rate limit)", async () => {
308 const responses = [
309 new Response("", { status: 429 }),
310 new Response("", { status: 408 }),
311 new Response("", { status: 200 }),
312 ];
313 const { calls, fn } = captureFetch((i) => responses[i]!);
314
9ecf5a4Claude315 await triggerVapronDeploy(makeArgs(), {
ba93444Claude316 fetchImpl: fn,
317 sleep: noSleep,
318 retryDelaysMs: [1, 2, 3, 4, 5],
319 });
320
321 expect(calls.length).toBe(3);
322 });
323
324 it("retries on network errors (fetch throws)", async () => {
325 let callCount = 0;
326 const fn = (async () => {
327 callCount++;
328 if (callCount < 3) throw new Error("ECONNREFUSED");
329 return new Response("", { status: 200 });
330 }) as unknown as typeof fetch;
331
9ecf5a4Claude332 await triggerVapronDeploy(makeArgs(), {
ba93444Claude333 fetchImpl: fn,
334 sleep: noSleep,
335 retryDelaysMs: [1, 2, 3, 4, 5],
336 });
337
338 expect(callCount).toBe(3);
43cf9b0Claude339 });
340
ba93444Claude341 it("does not throw when receiver responds 401 (unconfigured-secret path)", async () => {
342 const { fn } = captureFetch(() => new Response("", { status: 401 }));
43cf9b0Claude343 await expect(
9ecf5a4Claude344 triggerVapronDeploy(makeArgs(), { fetchImpl: fn, sleep: noSleep })
43cf9b0Claude345 ).resolves.toBeUndefined();
ba93444Claude346 });
347
348 it("uses a default exponential-backoff schedule of 1s/4s/16s/64s/256s", () => {
349 expect(__test.RETRY_DELAYS_MS).toEqual([1_000, 4_000, 16_000, 64_000, 256_000]);
43cf9b0Claude350 });
351});
9ecf5a4Claude352
353describe("vapron config — legacy CRONTECH_* env fallback", () => {
354 const KEYS = [
355 "VAPRON_DEPLOY_URL", "CRONTECH_DEPLOY_URL",
356 "VAPRON_REPO", "CRONTECH_REPO",
357 "VAPRON_HMAC_SECRET", "CRONTECH_HMAC_SECRET", "GLUECRON_WEBHOOK_SECRET",
358 ] as const;
359 const saved: Record<string, string | undefined> = {};
360 beforeEach(() => {
361 for (const k of KEYS) { saved[k] = process.env[k]; delete process.env[k]; }
362 });
363 afterEach(() => {
364 for (const k of KEYS) {
365 if (saved[k] === undefined) delete process.env[k];
366 else process.env[k] = saved[k]!;
367 }
368 });
369
370 it("defaults to the vapron.ai webhook URL and ccantynz-alt/vapron repo", () => {
371 expect(config.vapronDeployUrl).toBe("https://vapron.ai/api/webhooks/gluecron-push");
372 expect(config.vapronRepo).toBe("ccantynz-alt/vapron");
373 });
374
375 it("VAPRON_* wins over legacy CRONTECH_*", () => {
376 process.env.VAPRON_DEPLOY_URL = "https://vapron.ai/hook-a";
377 process.env.CRONTECH_DEPLOY_URL = "https://crontech.ai/hook-b";
378 process.env.VAPRON_REPO = "o/new";
379 process.env.CRONTECH_REPO = "o/old";
380 process.env.VAPRON_HMAC_SECRET = "new-secret";
381 process.env.CRONTECH_HMAC_SECRET = "old-secret";
382 expect(config.vapronDeployUrl).toBe("https://vapron.ai/hook-a");
383 expect(config.vapronRepo).toBe("o/new");
384 expect(config.vapronHmacSecret).toBe("new-secret");
385 });
386
387 it("legacy CRONTECH_* still works when VAPRON_* is unset", () => {
388 process.env.CRONTECH_DEPLOY_URL = "https://crontech.ai/hook-b";
389 process.env.CRONTECH_REPO = "o/old";
390 process.env.CRONTECH_HMAC_SECRET = "old-secret";
391 expect(config.vapronDeployUrl).toBe("https://crontech.ai/hook-b");
392 expect(config.vapronRepo).toBe("o/old");
393 expect(config.vapronHmacSecret).toBe("old-secret");
394 });
395
396 it("HMAC secret falls back to GLUECRON_WEBHOOK_SECRET last", () => {
397 process.env.GLUECRON_WEBHOOK_SECRET = "oldest-secret";
398 expect(config.vapronHmacSecret).toBe("oldest-secret");
399 });
400});