Blame · Line-by-line history
crontech-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.
| 43cf9b0 | 1 | /** |
| 2 | * Crontech deploy-webhook sender — Finding 1. | |
| 3 | * | |
| 4 | * Asserts that the outbound `triggerCrontechDeploy` call sent from | |
| 5 | * `src/hooks/post-receive.ts` matches the wire contract documented at the | |
| 6 | * top of that helper: | |
| 7 | * | |
| 8 | * POST https://crontech.ai/api/hooks/gluecron/push | |
| 9 | * Authorization: Bearer ${GLUECRON_WEBHOOK_SECRET} | |
| 10 | * Content-Type: application/json | |
| 11 | * body = { repository, sha, branch, ref, source, timestamp } | |
| 12 | * | |
| 13 | * The helper swallows DB errors, so these tests work without a real DB. | |
| 14 | */ | |
| 15 | ||
| 16 | import { afterEach, beforeEach, describe, expect, it } from "bun:test"; | |
| 17 | import { __test } from "../hooks/post-receive"; | |
| 18 | ||
| 19 | const { triggerCrontechDeploy } = __test; | |
| 20 | ||
| 21 | interface CapturedCall { | |
| 22 | url: string; | |
| 23 | init: RequestInit; | |
| 24 | } | |
| 25 | ||
| 26 | const origFetch = globalThis.fetch; | |
| 27 | const origSecret = process.env.GLUECRON_WEBHOOK_SECRET; | |
| 28 | const origUrl = process.env.CRONTECH_DEPLOY_URL; | |
| 29 | ||
| 30 | function installFetchCapture( | |
| 31 | respond: () => Response = () => | |
| 32 | new Response( | |
| 33 | JSON.stringify({ ok: true, deploymentId: "d1", status: "queued" }), | |
| 34 | { status: 200, headers: { "Content-Type": "application/json" } } | |
| 35 | ) | |
| 36 | ): CapturedCall[] { | |
| 37 | const calls: CapturedCall[] = []; | |
| 38 | // @ts-expect-error — override global fetch for test | |
| 39 | globalThis.fetch = async ( | |
| 40 | input: RequestInfo | URL, | |
| 41 | init: RequestInit = {} | |
| 42 | ): Promise<Response> => { | |
| 43 | calls.push({ url: String(input), init }); | |
| 44 | return respond(); | |
| 45 | }; | |
| 46 | return calls; | |
| 47 | } | |
| 48 | ||
| 49 | function restoreFetch(): void { | |
| 50 | globalThis.fetch = origFetch; | |
| 51 | } | |
| 52 | ||
| 53 | describe("hooks/post-receive — triggerCrontechDeploy (Finding 1 sender)", () => { | |
| 54 | beforeEach(() => { | |
| 55 | // Unset overrides so each test owns its env state. | |
| 56 | delete process.env.GLUECRON_WEBHOOK_SECRET; | |
| 57 | delete process.env.CRONTECH_DEPLOY_URL; | |
| 58 | }); | |
| 59 | ||
| 60 | afterEach(() => { | |
| 61 | restoreFetch(); | |
| 62 | if (origSecret === undefined) delete process.env.GLUECRON_WEBHOOK_SECRET; | |
| 63 | else process.env.GLUECRON_WEBHOOK_SECRET = origSecret; | |
| 64 | if (origUrl === undefined) delete process.env.CRONTECH_DEPLOY_URL; | |
| 65 | else process.env.CRONTECH_DEPLOY_URL = origUrl; | |
| 66 | }); | |
| 67 | ||
| 68 | it("is exported from __test", () => { | |
| 69 | expect(typeof triggerCrontechDeploy).toBe("function"); | |
| 70 | }); | |
| 71 | ||
| 72 | it("POSTs to the new Crontech hooks endpoint (not the old tRPC URL)", async () => { | |
| 73 | const calls = installFetchCapture(); | |
| 74 | ||
| 75 | await triggerCrontechDeploy( | |
| 76 | "alice", | |
| 77 | "widgets", | |
| 78 | "a".repeat(40), | |
| 79 | "00000000-0000-0000-0000-000000000000" | |
| 80 | ); | |
| 81 | ||
| 82 | expect(calls.length).toBe(1); | |
| 83 | expect(calls[0]!.url).toBe( | |
| 84 | "https://crontech.ai/api/hooks/gluecron/push" | |
| 85 | ); | |
| 86 | expect(calls[0]!.url).not.toContain("/api/trpc/tenant.deploy"); | |
| 87 | expect(calls[0]!.init.method).toBe("POST"); | |
| 88 | }); | |
| 89 | ||
| 90 | it("sends Authorization: Bearer <secret> when GLUECRON_WEBHOOK_SECRET is set", async () => { | |
| 91 | process.env.GLUECRON_WEBHOOK_SECRET = "s3cret-token"; | |
| 92 | const calls = installFetchCapture(); | |
| 93 | ||
| 94 | await triggerCrontechDeploy( | |
| 95 | "alice", | |
| 96 | "widgets", | |
| 97 | "b".repeat(40), | |
| 98 | "00000000-0000-0000-0000-000000000000" | |
| 99 | ); | |
| 100 | ||
| 101 | expect(calls.length).toBe(1); | |
| 102 | const headers = calls[0]!.init.headers as Record<string, string>; | |
| 103 | expect(headers["Authorization"]).toBe("Bearer s3cret-token"); | |
| 104 | expect(headers["Content-Type"]).toBe("application/json"); | |
| 105 | }); | |
| 106 | ||
| 107 | it("omits the Authorization header when no secret is configured", async () => { | |
| 108 | // GLUECRON_WEBHOOK_SECRET deliberately unset by beforeEach. | |
| 109 | const calls = installFetchCapture(); | |
| 110 | ||
| 111 | await triggerCrontechDeploy( | |
| 112 | "alice", | |
| 113 | "widgets", | |
| 114 | "c".repeat(40), | |
| 115 | "00000000-0000-0000-0000-000000000000" | |
| 116 | ); | |
| 117 | ||
| 118 | expect(calls.length).toBe(1); | |
| 119 | const headers = calls[0]!.init.headers as Record<string, string>; | |
| 120 | expect(headers["Authorization"]).toBeUndefined(); | |
| 121 | expect(headers["Content-Type"]).toBe("application/json"); | |
| 122 | }); | |
| 123 | ||
| 124 | it("sends the wire-contract body shape (repository, sha, branch, ref, source, timestamp)", async () => { | |
| 125 | const calls = installFetchCapture(); | |
| 126 | const sha = "d".repeat(40); | |
| 127 | ||
| 128 | await triggerCrontechDeploy( | |
| 129 | "acme", | |
| 130 | "api", | |
| 131 | sha, | |
| 132 | "00000000-0000-0000-0000-000000000000" | |
| 133 | ); | |
| 134 | ||
| 135 | expect(calls.length).toBe(1); | |
| 136 | const body = JSON.parse(String(calls[0]!.init.body)); | |
| 137 | expect(body.repository).toBe("acme/api"); | |
| 138 | expect(body.sha).toBe(sha); | |
| 139 | expect(body.branch).toBe("main"); | |
| 140 | expect(body.ref).toBe("refs/heads/main"); | |
| 141 | expect(body.source).toBe("gluecron"); | |
| 142 | expect(typeof body.timestamp).toBe("string"); | |
| 143 | // ISO-8601 sanity check | |
| 144 | expect(new Date(body.timestamp).toString()).not.toBe("Invalid Date"); | |
| 145 | }); | |
| 146 | ||
| 147 | it("respects CRONTECH_DEPLOY_URL override", async () => { | |
| 148 | process.env.CRONTECH_DEPLOY_URL = | |
| 149 | "https://staging.crontech.ai/api/hooks/gluecron/push"; | |
| 150 | const calls = installFetchCapture(); | |
| 151 | ||
| 152 | await triggerCrontechDeploy( | |
| 153 | "alice", | |
| 154 | "widgets", | |
| 155 | "e".repeat(40), | |
| 156 | "00000000-0000-0000-0000-000000000000" | |
| 157 | ); | |
| 158 | ||
| 159 | expect(calls.length).toBe(1); | |
| 160 | expect(calls[0]!.url).toBe( | |
| 161 | "https://staging.crontech.ai/api/hooks/gluecron/push" | |
| 162 | ); | |
| 163 | }); | |
| 164 | ||
| 165 | it("does not throw when Crontech responds 401 (unset secret path)", async () => { | |
| 166 | const calls = installFetchCapture(() => new Response("", { status: 401 })); | |
| 167 | await expect( | |
| 168 | triggerCrontechDeploy( | |
| 169 | "alice", | |
| 170 | "widgets", | |
| 171 | "f".repeat(40), | |
| 172 | "00000000-0000-0000-0000-000000000000" | |
| 173 | ) | |
| 174 | ).resolves.toBeUndefined(); | |
| 175 | expect(calls.length).toBe(1); | |
| 176 | }); | |
| 177 | }); |