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 | /**
* Block K — Crontech client tests.
*
* Exercises config toggle, auth headers, and offline short-circuits. When a
* key is present, we mock globalThis.fetch to confirm graceful degradation.
*/
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
import {
buildAuthHeaders,
getDeploymentForCommit,
isConfigured,
rollbackDeployment,
triggerRedeploy,
watchDeployment,
} from "../lib/crontech-client";
const ENV_KEYS = ["CRONTECH_API_KEY", "CRONTECH_BASE_URL"] as const;
let savedEnv: Record<string, string | undefined> = {};
let savedFetch: typeof fetch;
beforeEach(() => {
savedEnv = {};
for (const k of ENV_KEYS) savedEnv[k] = process.env[k];
for (const k of ENV_KEYS) delete process.env[k];
savedFetch = globalThis.fetch;
});
afterEach(() => {
for (const k of ENV_KEYS) {
const v = savedEnv[k];
if (v === undefined) delete process.env[k];
else process.env[k] = v;
}
globalThis.fetch = savedFetch;
});
// ---------------------------------------------------------------------------
// isConfigured + buildAuthHeaders
// ---------------------------------------------------------------------------
describe("crontech-client — config", () => {
it("isConfigured is false when no API key is set", () => {
expect(isConfigured()).toBe(false);
});
it("isConfigured flips true once CRONTECH_API_KEY is set", () => {
process.env.CRONTECH_API_KEY = "ct-test";
expect(isConfigured()).toBe(true);
});
it("buildAuthHeaders omits Authorization when no key is present", () => {
const h = buildAuthHeaders();
expect(h["Content-Type"]).toBe("application/json");
expect(h["Authorization"]).toBeUndefined();
});
it("buildAuthHeaders includes a Bearer token when the key is present", () => {
process.env.CRONTECH_API_KEY = "ct-live";
const h = buildAuthHeaders();
expect(h["Authorization"]).toBe("Bearer ct-live");
});
});
// ---------------------------------------------------------------------------
// getDeploymentForCommit
// ---------------------------------------------------------------------------
describe("crontech-client — getDeploymentForCommit", () => {
it("returns null in offline mode (no key)", async () => {
globalThis.fetch = (() => {
throw new Error("fetch must not run offline");
}) as unknown as typeof fetch;
const result = await getDeploymentForCommit({
repo: "o/r",
commitSha: "deadbeef",
});
expect(result).toBeNull();
});
it("returns null on a 500 response", async () => {
process.env.CRONTECH_API_KEY = "ct";
globalThis.fetch = (async () =>
new Response("", { status: 500 })) as unknown as typeof fetch;
const result = await getDeploymentForCommit({
repo: "o/r",
commitSha: "deadbeef",
});
expect(result).toBeNull();
});
it("parses a valid deployment body on 200", async () => {
process.env.CRONTECH_API_KEY = "ct";
globalThis.fetch = (async () =>
new Response(
JSON.stringify({
deployId: "dep_1",
commitSha: "deadbeef",
status: "live",
environment: "production",
startedAt: "2026-01-01T00:00:00Z",
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)) as unknown as typeof fetch;
const result = await getDeploymentForCommit({
repo: "o/r",
commitSha: "deadbeef",
});
expect(result).not.toBeNull();
expect(result?.deployId).toBe("dep_1");
expect(result?.status).toBe("live");
});
});
// ---------------------------------------------------------------------------
// triggerRedeploy
// ---------------------------------------------------------------------------
describe("crontech-client — triggerRedeploy", () => {
it("returns null in offline mode (no key)", async () => {
const result = await triggerRedeploy({
repo: "o/r",
commitSha: "cafef00d",
});
expect(result).toBeNull();
});
it("returns null on a 500 response", async () => {
process.env.CRONTECH_API_KEY = "ct";
globalThis.fetch = (async () =>
new Response("", { status: 500 })) as unknown as typeof fetch;
const result = await triggerRedeploy({
repo: "o/r",
commitSha: "cafef00d",
environment: "staging",
});
expect(result).toBeNull();
});
});
// ---------------------------------------------------------------------------
// rollbackDeployment
// ---------------------------------------------------------------------------
describe("crontech-client — rollbackDeployment", () => {
it("returns false in offline mode (no key)", async () => {
const ok = await rollbackDeployment({ repo: "o/r", deployId: "dep_1" });
expect(ok).toBe(false);
});
it("returns false on a 500 response", async () => {
process.env.CRONTECH_API_KEY = "ct";
globalThis.fetch = (async () =>
new Response("", { status: 500 })) as unknown as typeof fetch;
const ok = await rollbackDeployment({ repo: "o/r", deployId: "dep_1" });
expect(ok).toBe(false);
});
it("returns false when deployId is empty even with a key", async () => {
process.env.CRONTECH_API_KEY = "ct";
const ok = await rollbackDeployment({ repo: "o/r", deployId: "" });
expect(ok).toBe(false);
});
});
// ---------------------------------------------------------------------------
// watchDeployment
// ---------------------------------------------------------------------------
describe("crontech-client — watchDeployment", () => {
it("returns offline:true immediately when no key is set", async () => {
globalThis.fetch = (() => {
throw new Error("fetch must not run offline");
}) as unknown as typeof fetch;
const start = Date.now();
const result = await watchDeployment({
repo: "o/r",
deployId: "dep_1",
maxWaitMs: 60_000,
});
const elapsed = Date.now() - start;
expect(result.offline).toBe(true);
expect(result.finalStatus).toBe("failed");
expect(result.errors).toEqual([]);
// Should short-circuit in well under a second.
expect(elapsed).toBeLessThan(500);
});
it("resolves with finalStatus=live when the poll returns terminal status", async () => {
process.env.CRONTECH_API_KEY = "ct";
let call = 0;
globalThis.fetch = (async (input: unknown) => {
call++;
const url = String(input);
if (url.endsWith("/status")) {
return new Response(
JSON.stringify({ status: "live" }),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
}
if (url.endsWith("/errors")) {
return new Response(
JSON.stringify({ errors: [] }),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
}
return new Response("", { status: 404 });
}) as unknown as typeof fetch;
const result = await watchDeployment({
repo: "o/r",
deployId: "dep_1",
maxWaitMs: 5_000,
pollIntervalMs: 50,
});
expect(result.offline).toBe(false);
expect(result.finalStatus).toBe("live");
expect(call).toBeGreaterThanOrEqual(2); // status + errors
});
it("bails to offline after repeated 500 status polls", async () => {
process.env.CRONTECH_API_KEY = "ct";
globalThis.fetch = (async () =>
new Response("", { status: 500 })) as unknown as typeof fetch;
const result = await watchDeployment({
repo: "o/r",
deployId: "dep_1",
maxWaitMs: 5_000,
pollIntervalMs: 10,
});
expect(result.offline).toBe(true);
expect(result.finalStatus).toBe("failed");
});
});
|