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 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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | /**
* Block R1 — Tests for /admin/ops, the site-admin operations console.
*
* Coverage:
* - GET /admin/ops requires site-admin (302 for anon, 403 for non-admin,
* 200 HTML for admin)
* - POST /admin/ops/auto-merge/enable calls runEnableAutoMerge with the
* right args and redirects with success
* - POST /admin/ops/auto-merge/disable passes `off:true`
* - POST /admin/ops/deploy/trigger forwards to the N4 handler
* - POST /admin/ops/rollback resolves the previous-successful SHA and
* dispatches a workflow_dispatch with that ref
* - findPreviousSuccessfulDeploy returns null when there's nothing prior
* - triggerRollback maps 401 / 422 into friendly errors
*
* Mock pattern: K1-style `mock.module("../db", ...)` with afterAll
* restoration. The opsRoutes module exposes `__setOpsDepsForTests` so we
* inject the runEnableAutoMerge / triggerRollback / findPreviousSuccessfulDeploy
* spies as actual collaborators — no need to stub Drizzle for the script's
* private queries.
*/
import { describe, it, expect, mock, beforeEach, afterAll } from "bun:test";
// ---------------------------------------------------------------------------
// Helpers shared with cli-deploy.test.ts.
// ---------------------------------------------------------------------------
function jsonRes(status: number, body: any) {
return {
status,
ok: status >= 200 && status < 300,
text: async () => (typeof body === "string" ? body : JSON.stringify(body)),
};
}
function noContent() {
return { status: 204, ok: true, text: async () => "" };
}
// ---------------------------------------------------------------------------
// Spread-from-real `../db` mock. We capture select returns per-test via
// per-table "next row" hooks, identical to cli-deploy.test.ts.
// ---------------------------------------------------------------------------
const _real_db = await import("../db");
const _schema = await import("../db/schema");
const _schemaDeploys = await import("../db/schema-deploys");
let _nextSessionRow: any = null;
let _nextUserRow: any = null;
let _nextAdminRow: any = null;
let _nextBpOwnerRow: any = null; // for readAutoMergeState owner lookup
let _nextRepoRow: any = null;
let _nextBpRow: any = null;
let _nextLatestDeployRow: any = null;
let _lastSelectFrom: any = null;
let _userSelectCount = 0;
const tableName = (t: any): string => {
if (t === _schema.sessions) return "sessions";
if (t === _schema.users) return "users";
if (t === _schema.siteAdmins) return "site_admins";
if (t === _schema.repositories) return "repositories";
if (t === _schema.branchProtection) return "branch_protection";
if (t === _schemaDeploys.platformDeploys) return "platform_deploys";
return "?";
};
const _selectChain: any = {
from: (t: any) => {
_lastSelectFrom = t;
if (tableName(t) === "users") _userSelectCount++;
return _selectChain;
},
innerJoin: () => _selectChain,
leftJoin: () => _selectChain,
where: () => _selectChain,
orderBy: () => _selectChain,
limit: async () => {
const name = tableName(_lastSelectFrom);
if (name === "sessions") return _nextSessionRow ? [_nextSessionRow] : [];
if (name === "users") {
// The softAuth path performs a users-select for the session lookup;
// the page handler then does additional users-selects for the ops
// repo owner. We let the first call be the session user and second+
// calls be the bp owner — the test sets both via setters below.
if (_userSelectCount === 1) return _nextUserRow ? [_nextUserRow] : [];
return _nextBpOwnerRow ? [_nextBpOwnerRow] : [];
}
if (name === "site_admins") return _nextAdminRow ? [_nextAdminRow] : [];
if (name === "repositories") return _nextRepoRow ? [_nextRepoRow] : [];
if (name === "branch_protection")
return _nextBpRow ? [_nextBpRow] : [];
if (name === "platform_deploys")
return _nextLatestDeployRow ? [_nextLatestDeployRow] : [];
return [];
},
then: (resolve: (v: any) => void) => resolve([]),
};
const _fakeDb = {
db: {
select: () => _selectChain,
insert: () => ({
values: () => ({
returning: async () => [],
then: (r: (v: any) => void) => r(undefined),
}),
}),
update: () => ({ set: () => ({ where: () => Promise.resolve() }) }),
delete: () => ({ where: () => Promise.resolve() }),
execute: async () => ({ rows: [{ column_name: "enable_auto_merge" }] }),
},
getDb: () => _fakeDb.db,
};
mock.module("../db", () => ({ ..._real_db, ..._fakeDb }));
// Import the app + ops module AFTER mock.module has installed the fake.
const { default: app } = await import("../app");
const { sessionCache } = await import("../lib/cache");
const opsModule = await import("../routes/admin-ops");
const adminDeploys = await import("../routes/admin-deploys");
const rollbackLib = await import("../lib/rollback-deploy");
// ---------------------------------------------------------------------------
// Fake users + session tokens
// ---------------------------------------------------------------------------
const ADMIN_ID = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const NON_ADMIN_ID = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
const ADMIN_TOKEN = "r1-admin-token";
const NON_ADMIN_TOKEN = "r1-nonadmin-token";
const ADMIN_USER = {
id: ADMIN_ID,
username: "ops_admin",
displayName: "Ops Admin",
email: "ops@example.com",
passwordHash: "x",
createdAt: new Date(),
updatedAt: new Date(),
};
const NON_ADMIN_USER = {
id: NON_ADMIN_ID,
username: "ops_nobody",
displayName: "Nobody",
email: "n@example.com",
passwordHash: "x",
createdAt: new Date(),
updatedAt: new Date(),
};
const SAME_ORIGIN_HEADERS = {
host: "localhost",
origin: "http://localhost",
};
function authedPost(token: string): RequestInit {
return {
method: "POST",
headers: {
...SAME_ORIGIN_HEADERS,
cookie: `session=${token}`,
"content-type": "application/json",
},
body: JSON.stringify({}),
redirect: "manual",
};
}
function authedGet(token: string | null): RequestInit {
const headers: Record<string, string> = { ...SAME_ORIGIN_HEADERS };
if (token) headers.cookie = `session=${token}`;
return { method: "GET", headers, redirect: "manual" };
}
beforeEach(() => {
sessionCache.set(ADMIN_TOKEN, ADMIN_USER as any);
sessionCache.set(NON_ADMIN_TOKEN, NON_ADMIN_USER as any);
_nextSessionRow = null;
_nextUserRow = null;
_nextAdminRow = null;
_nextBpOwnerRow = null;
_nextRepoRow = null;
_nextBpRow = null;
_nextLatestDeployRow = null;
_userSelectCount = 0;
opsModule.__setOpsDepsForTests(null);
});
afterAll(() => {
sessionCache.invalidate(ADMIN_TOKEN);
sessionCache.invalidate(NON_ADMIN_TOKEN);
opsModule.__setOpsDepsForTests(null);
adminDeploys.__setGithubFetchForTests(null);
adminDeploys.__setEnvForTests(null);
mock.module("../db", () => _real_db);
});
// ===========================================================================
// GET /admin/ops gating
// ===========================================================================
describe("GET /admin/ops gating", () => {
it("redirects anonymous users to /login", async () => {
const res = await app.request("/admin/ops", authedGet(null));
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("/login");
});
it("403s an authed non-admin", async () => {
_nextAdminRow = null;
const res = await app.request(
"/admin/ops",
authedGet(NON_ADMIN_TOKEN)
);
expect(res.status).toBe(403);
});
it("renders HTML 200 for a site admin (auto-merge card present)", async () => {
_nextAdminRow = { userId: ADMIN_ID };
// Stub the readiness-friendly helpers so the page renders without
// exercising the autopilot module-load probe.
opsModule.__setOpsDepsForTests({
findPreviousSuccessfulDeploy: async () => null,
});
const res = await app.request("/admin/ops", authedGet(ADMIN_TOKEN));
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("AI auto-merge on main");
expect(html).toContain("Deploy");
expect(html).toContain("Rollback");
});
});
// ===========================================================================
// POST /admin/ops/auto-merge/{enable,disable}
// ===========================================================================
describe("POST /admin/ops/auto-merge/enable", () => {
it("redirects 401-equivalent to login when anonymous", async () => {
const res = await app.request("/admin/ops/auto-merge/enable", {
method: "POST",
headers: SAME_ORIGIN_HEADERS,
redirect: "manual",
});
expect([302, 303]).toContain(res.status);
expect(res.headers.get("location") || "").toContain("/login");
});
it("403s for a non-admin", async () => {
_nextAdminRow = null;
const res = await app.request(
"/admin/ops/auto-merge/enable",
authedPost(NON_ADMIN_TOKEN)
);
expect(res.status).toBe(403);
});
it("calls runEnableAutoMerge with off=false + redirects success", async () => {
_nextAdminRow = { userId: ADMIN_ID };
let captured: any = null;
opsModule.__setOpsDepsForTests({
runEnableAutoMerge: async (_db, args) => {
captured = args;
return {
action: "updated",
before: { enableAutoMerge: false } as any,
after: {
id: "bp-1",
enableAutoMerge: true,
} as any,
auditWritten: true,
};
},
});
const res = await app.request(
"/admin/ops/auto-merge/enable",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("/admin/ops");
expect(loc).toContain("success=");
expect(loc.toLowerCase()).toContain("enabled");
expect(captured).not.toBeNull();
expect(captured.ownerSlash).toBe("ccantynz/Gluecron.com");
expect(captured.pattern).toBe("main");
expect(captured.off).toBe(false);
expect(captured.actorUserId).toBe(ADMIN_ID);
});
it("reports a friendly error when the script throws", async () => {
_nextAdminRow = { userId: ADMIN_ID };
opsModule.__setOpsDepsForTests({
runEnableAutoMerge: async () => {
throw new Error("Repository not found: ccantynz/Gluecron.com.");
},
});
const res = await app.request(
"/admin/ops/auto-merge/enable",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("error=");
expect(decodeURIComponent(loc)).toMatch(/Repository not found/);
});
});
describe("POST /admin/ops/auto-merge/disable", () => {
it("calls the script with off=true + redirects success", async () => {
_nextAdminRow = { userId: ADMIN_ID };
let captured: any = null;
opsModule.__setOpsDepsForTests({
runEnableAutoMerge: async (_db, args) => {
captured = args;
return {
action: "updated",
before: { enableAutoMerge: true } as any,
after: { id: "bp-1", enableAutoMerge: false } as any,
auditWritten: true,
};
},
});
const res = await app.request(
"/admin/ops/auto-merge/disable",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
expect(captured.off).toBe(true);
expect(captured.actorUserId).toBe(ADMIN_ID);
const loc = res.headers.get("location") || "";
expect(loc).toContain("success=");
expect(decodeURIComponent(loc).toLowerCase()).toContain("disabled");
});
});
// ===========================================================================
// POST /admin/ops/deploy/trigger — re-uses N4 internally
// ===========================================================================
describe("POST /admin/ops/deploy/trigger", () => {
it("forwards to N4 and redirects success when GitHub returns 204", async () => {
_nextAdminRow = { userId: ADMIN_ID };
adminDeploys.__setEnvForTests({ GITHUB_TOKEN: "ghp_admin" });
let captured: { url: string; method?: string; body?: string } | null = null;
adminDeploys.__setGithubFetchForTests(async (url, init) => {
captured = { url, method: init?.method, body: init?.body };
return noContent();
});
const res = await app.request(
"/admin/ops/deploy/trigger",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("success=");
expect(captured).not.toBeNull();
expect(captured!.method).toBe("POST");
expect(captured!.url).toContain(
"/actions/workflows/hetzner-deploy.yml/dispatches"
);
});
it("surfaces the N4 error when GitHub rejects the dispatch", async () => {
_nextAdminRow = { userId: ADMIN_ID };
adminDeploys.__setEnvForTests({ GITHUB_TOKEN: "ghp_admin" });
adminDeploys.__setGithubFetchForTests(async () =>
jsonRes(422, { message: "No ref found" })
);
const res = await app.request(
"/admin/ops/deploy/trigger",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("error=");
expect(decodeURIComponent(loc)).toMatch(/422.*No ref found/);
});
});
// ===========================================================================
// POST /admin/ops/rollback
// ===========================================================================
describe("POST /admin/ops/rollback", () => {
it("400-style redirect when there's no previous successful deploy", async () => {
_nextAdminRow = { userId: ADMIN_ID };
opsModule.__setOpsDepsForTests({
findPreviousSuccessfulDeploy: async () => null,
});
const res = await app.request(
"/admin/ops/rollback",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("error=");
expect(decodeURIComponent(loc)).toMatch(/No previous successful deploy/);
});
it("calls triggerRollback with the previous-successful SHA on success", async () => {
_nextAdminRow = { userId: ADMIN_ID };
const PREV_SHA = "def56781234567890abcdef";
let capturedArgs: any = null;
opsModule.__setOpsDepsForTests({
findPreviousSuccessfulDeploy: async () => ({
sha: PREV_SHA,
runId: "9999",
finishedAt: new Date(),
}),
triggerRollback: async (args) => {
capturedArgs = args;
return { ok: true, htmlUrl: "https://github.com/x/y/actions" };
},
});
const res = await app.request(
"/admin/ops/rollback",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
expect(capturedArgs).not.toBeNull();
expect(capturedArgs.targetSha).toBe(PREV_SHA);
expect(capturedArgs.triggeredByUserId).toBe(ADMIN_ID);
const loc = res.headers.get("location") || "";
expect(loc).toContain("success=");
expect(decodeURIComponent(loc)).toContain(PREV_SHA.slice(0, 7));
});
it("redirects with error when triggerRollback returns ok:false", async () => {
_nextAdminRow = { userId: ADMIN_ID };
opsModule.__setOpsDepsForTests({
findPreviousSuccessfulDeploy: async () => ({
sha: "abc1234",
runId: "1",
finishedAt: new Date(),
}),
triggerRollback: async () => ({ ok: false, error: "GitHub auth failed (401)" }),
});
const res = await app.request(
"/admin/ops/rollback",
authedPost(ADMIN_TOKEN)
);
expect([302, 303]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc).toContain("error=");
expect(decodeURIComponent(loc)).toMatch(/GitHub auth failed/);
});
it("403s non-admins", async () => {
_nextAdminRow = null;
const res = await app.request(
"/admin/ops/rollback",
authedPost(NON_ADMIN_TOKEN)
);
expect(res.status).toBe(403);
});
});
// ===========================================================================
// rollback-deploy.ts library helpers
// ===========================================================================
describe("findPreviousSuccessfulDeploy", () => {
it("returns null when the table is empty", async () => {
_nextLatestDeployRow = null;
const r = await rollbackLib.findPreviousSuccessfulDeploy();
expect(r).toBeNull();
});
});
describe("triggerRollback — friendly error mapping", () => {
it("rejects missing targetSha", async () => {
const r = await rollbackLib.triggerRollback({
targetSha: "",
triggeredByUserId: ADMIN_ID,
githubToken: "ghp_x",
fetchImpl: (async () => noContent()) as any,
});
expect(r.ok).toBe(false);
expect(r.error).toMatch(/targetSha is required/);
});
it("rejects missing GITHUB_TOKEN", async () => {
const r = await rollbackLib.triggerRollback({
targetSha: "abc1234",
triggeredByUserId: ADMIN_ID,
githubToken: "",
fetchImpl: (async () => noContent()) as any,
});
expect(r.ok).toBe(false);
expect(r.error).toMatch(/GITHUB_TOKEN/);
});
it("maps 401 → friendly auth error", async () => {
const r = await rollbackLib.triggerRollback({
targetSha: "abc1234",
triggeredByUserId: ADMIN_ID,
githubToken: "ghp_bad",
fetchImpl: (async () =>
jsonRes(401, { message: "Bad credentials" })) as any,
});
expect(r.ok).toBe(false);
expect(r.error).toMatch(/GitHub auth failed \(401\)/);
});
it("maps 422 → friendly ref error", async () => {
const r = await rollbackLib.triggerRollback({
targetSha: "nope",
triggeredByUserId: ADMIN_ID,
githubToken: "ghp_x",
fetchImpl: (async () =>
jsonRes(422, { message: "No ref found for: nope" })) as any,
});
expect(r.ok).toBe(false);
expect(r.error).toMatch(/422.*No ref found/);
});
it("ok:true on a 204 dispatch + POSTs ref=targetSha", async () => {
const calls: Array<{ url: string; body?: string }> = [];
const r = await rollbackLib.triggerRollback({
targetSha: "abc1234def",
triggeredByUserId: ADMIN_ID,
githubToken: "ghp_x",
fetchImpl: (async (url: string, init?: any) => {
calls.push({ url, body: init?.body });
return noContent();
}) as any,
});
expect(r.ok).toBe(true);
expect(calls[0]!.url).toContain("/dispatches");
expect(JSON.parse(calls[0]!.body!).ref).toBe("abc1234def");
});
});
|