CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 | /**
* Signal Bus P1 — inbound deploy-event receiver tests (E3/E4).
*
* Exercises `src/routes/events.ts` directly via its default Hono sub-app so
* the suite is hermetic: no need to mount on the main app (which is locked)
* and no live DB required. Tests that assert DB-backed side-effects run only
* when `DATABASE_URL` is present; otherwise they assert graceful degradation.
*/
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
} from "bun:test";
import events, { __test } from "../routes/events";
const VALID_EVENT_ID_A = "11111111-1111-4111-8111-111111111111";
const VALID_EVENT_ID_B = "22222222-2222-4222-8222-222222222222";
const VALID_SHA = "a".repeat(40);
const origToken = process.env.CRONTECH_EVENT_TOKEN;
function makePayload(
overrides: Partial<Record<string, unknown>> = {}
): Record<string, unknown> {
return {
event: "deploy.succeeded",
eventId: VALID_EVENT_ID_A,
repository: "alice/widgets",
sha: VALID_SHA,
environment: "production",
deploymentId: "crontech-dep-123",
timestamp: "2025-06-01T12:00:00.000Z",
...overrides,
};
}
async function post(
body: unknown,
headers: Record<string, string> = {}
): Promise<Response> {
return events.request("/deploy", {
method: "POST",
headers: {
"Content-Type": "application/json",
...headers,
},
body: typeof body === "string" ? body : JSON.stringify(body),
});
}
beforeAll(() => {
process.env.CRONTECH_EVENT_TOKEN = "unit-bearer-fixture";
});
afterAll(() => {
if (origToken === undefined) delete process.env.CRONTECH_EVENT_TOKEN;
else process.env.CRONTECH_EVENT_TOKEN = origToken;
});
// ---------------------------------------------------------------------------
// Bearer auth
// ---------------------------------------------------------------------------
describe("events/deploy — bearer auth", () => {
it("rejects with 401 when Authorization header is missing", async () => {
const res = await post(makePayload());
expect(res.status).toBe(401);
const body = await res.json();
expect(body.ok).toBe(false);
expect(String(body.error).toLowerCase()).toContain("bearer");
});
it("rejects with 401 when Bearer token is wrong", async () => {
const res = await post(makePayload(), {
authorization: "Bearer not-the-real-token",
});
expect(res.status).toBe(401);
const body = await res.json();
expect(body.ok).toBe(false);
});
it("rejects with 401 when CRONTECH_EVENT_TOKEN is unset (refuse-by-default)", async () => {
const saved = process.env.CRONTECH_EVENT_TOKEN;
delete process.env.CRONTECH_EVENT_TOKEN;
try {
const res = await post(makePayload(), {
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.CRONTECH_EVENT_TOKEN = saved;
}
});
});
// ---------------------------------------------------------------------------
// Payload validation
// ---------------------------------------------------------------------------
describe("events/deploy — payload validation", () => {
const authHeader = { authorization: "Bearer unit-bearer-fixture" };
it("rejects malformed JSON with 400", async () => {
const res = await post("{not-json", authHeader);
expect(res.status).toBe(400);
const body = await res.json();
expect(String(body.error).toLowerCase()).toContain("json");
});
it("rejects unknown event type with 400", async () => {
const res = await post(makePayload({ event: "deploy.canceled" }), authHeader);
expect(res.status).toBe(400);
});
it("rejects non-uuid eventId with 400", async () => {
const res = await post(makePayload({ eventId: "not-a-uuid" }), authHeader);
expect(res.status).toBe(400);
const body = await res.json();
expect(String(body.error).toLowerCase()).toContain("eventid");
});
it("rejects invalid sha with 400", async () => {
const res = await post(makePayload({ sha: "xyz" }), authHeader);
expect(res.status).toBe(400);
});
it("rejects deploy.failed without errorCategory + errorSummary", async () => {
const res = await post(
makePayload({
event: "deploy.failed",
eventId: VALID_EVENT_ID_B,
}),
authHeader
);
expect(res.status).toBe(400);
const body = await res.json();
expect(String(body.error).toLowerCase()).toMatch(/errorcategory|errorsummary/);
});
it("rejects errorSummary over 500 chars on deploy.failed", async () => {
const res = await post(
makePayload({
event: "deploy.failed",
eventId: VALID_EVENT_ID_B,
errorCategory: "build",
errorSummary: "x".repeat(501),
}),
authHeader
);
expect(res.status).toBe(400);
});
});
// ---------------------------------------------------------------------------
// Pure validator (no HTTP) — exercises __test.validatePayload.
// ---------------------------------------------------------------------------
describe("events/deploy — validatePayload helper", () => {
it("accepts a well-formed deploy.succeeded payload", () => {
const result = __test.validatePayload(makePayload());
expect(result.ok).toBe(true);
});
it("accepts a well-formed deploy.failed payload with required error fields", () => {
const result = __test.validatePayload(
makePayload({
event: "deploy.failed",
errorCategory: "runtime",
errorSummary: "Container OOM-killed after 42s",
})
);
expect(result.ok).toBe(true);
});
it("rejects non-object bodies", () => {
expect(__test.validatePayload(null).ok).toBe(false);
expect(__test.validatePayload("hello").ok).toBe(false);
expect(__test.validatePayload(42).ok).toBe(false);
});
it("rejects unknown errorCategory on deploy.failed", () => {
const result = __test.validatePayload(
makePayload({
event: "deploy.failed",
errorCategory: "nuclear",
errorSummary: "boom",
})
);
expect(result.ok).toBe(false);
});
});
// ---------------------------------------------------------------------------
// Side-effect paths — these hit the DB. Without a DATABASE_URL the handler
// degrades gracefully (idempotency lookup swallows, insert returns 500, etc.).
// We run a relaxed assertion in no-DB mode and a strict one with DB.
// ---------------------------------------------------------------------------
const HAS_DB = Boolean(process.env.DATABASE_URL);
describe("events/deploy — idempotency + update (DB-aware)", () => {
const authHeader = { authorization: "Bearer unit-bearer-fixture" };
beforeEach(() => {
process.env.CRONTECH_EVENT_TOKEN = "unit-bearer-fixture";
});
afterEach(() => {
// no-op — env restored by afterAll
});
it("E3 deploy.succeeded: returns ok + duplicate:false on first delivery (or 500 without DB)", async () => {
const res = await post(
makePayload({ eventId: VALID_EVENT_ID_A }),
authHeader
);
if (HAS_DB) {
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ok).toBe(true);
expect(body.duplicate).toBe(false);
} else {
// Without a DB the insert into processed_events will throw; handler
// returns 500 with {ok:false}. This is the "graceful no-DB" contract.
expect([200, 500]).toContain(res.status);
}
});
it("replaying the same eventId returns duplicate:true and does not double-side-effect", async () => {
const payload = makePayload({ eventId: VALID_EVENT_ID_A });
const first = await post(payload, authHeader);
const second = await post(payload, authHeader);
if (HAS_DB) {
expect(first.status).toBe(200);
expect(second.status).toBe(200);
const firstBody = await first.json();
const secondBody = await second.json();
// Whichever call loses the race is duplicate. At most one is false.
const duplicates = [firstBody.duplicate, secondBody.duplicate];
expect(duplicates.filter(Boolean).length).toBeGreaterThanOrEqual(1);
} else {
// Without DB, both attempts fail the insert; we only assert that both
// return a JSON body rather than crashing.
expect([200, 500]).toContain(first.status);
expect([200, 500]).toContain(second.status);
}
});
it("E4 deploy.failed is accepted and returns JSON (DB-aware)", async () => {
const res = await post(
makePayload({
event: "deploy.failed",
eventId: VALID_EVENT_ID_B,
errorCategory: "build",
errorSummary: "npm install exited 1",
logsUrl: "https://crontech.ai/logs/xyz",
}),
authHeader
);
if (HAS_DB) {
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ok).toBe(true);
} else {
expect([200, 500]).toContain(res.status);
}
});
});
|