Public: anyone can read this; the crew can change it.
test/capacity.test.ts: "an admin changes each one from the page, the API and MCP" passes on a loaded runner #1018
scout/forge:scout/966-test-capacity-test-ts-an-admin-changes-e → main 1 commit fork
1 file
changed +51
−48
test/capacity.test.ts +51 −48
| @@ -203,7 +203,25 @@ describe("capacity: only a forge admin reads or changes it", () => { | ||
| 203 | 203 | expect(capacityChanges(app.db)).toEqual([]); |
| 204 | 204 | }); |
| 205 | 205 | |
| 206 | test("an admin changes each one from the page, the API and MCP; a wrong value is refused; every change is kept", async () => { | |
| 206 | const form = (cookie: string, fields: Record<string, string>) => | |
| 207 | fetch(`${servers.url}/settings/capacity`, { | |
| 208 | method: "POST", | |
| 209 | redirect: "manual", | |
| 210 | headers: { | |
| 211 | cookie, | |
| 212 | "content-type": "application/x-www-form-urlencoded", | |
| 213 | origin: servers.url, | |
| 214 | }, | |
| 215 | body: new URLSearchParams(fields), | |
| 216 | }); | |
| 217 | const changesTo = (key: string) => | |
| 218 | capacityChanges(app.db) | |
| 219 | .filter((c) => c.key === key) | |
| 220 | .reverse() | |
| 221 | .map((c) => [c.from, c.to, c.actorName]); | |
| 222 | ||
| 223 | // Each case owns its keys, so what one leaves behind cannot colour another. | |
| 224 | test("an admin changes a capacity from the page; every change is kept", async () => { | |
| 207 | 225 | const cookie = await login("alice"); |
| 208 | 226 | const page = await ( |
| 209 | 227 | await fetch(`${servers.url}/settings/capacity`, { headers: { cookie } }) |
| @@ -216,19 +234,24 @@ describe("capacity: only a forge admin reads or changes it", () => { | ||
| 216 | 234 | ]) |
| 217 | 235 | expect(page).toContain(key); |
| 218 | 236 | |
| 219 | const posted = await fetch(`${servers.url}/settings/capacity`, { | |
| 220 | method: "POST", | |
| 221 | redirect: "manual", | |
| 222 | headers: { | |
| 223 | cookie, | |
| 224 | "content-type": "application/x-www-form-urlencoded", | |
| 225 | origin: servers.url, | |
| 226 | }, | |
| 227 | body: new URLSearchParams({ key: "checks.shards", value: "3" }), | |
| 228 | }); | |
| 229 | expect(posted.status).toBe(303); | |
| 237 | expect((await form(cookie, { key: "checks.shards", value: "3" })).status).toBe(303); | |
| 230 | 238 | expect(capacityEnv(app.db)).toEqual({ FORGE_CHECK_SHARDS: "3" }); |
| 231 | 239 | |
| 240 | // Back to the default: the page's reset. | |
| 241 | expect((await form(cookie, { key: "checks.shards", reset: "1" })).status).toBe(303); | |
| 242 | expect(capacityEnv(app.db)).toEqual({}); | |
| 243 | ||
| 244 | expect(changesTo("checks.shards")).toEqual([ | |
| 245 | ["", "3", "alice"], | |
| 246 | ["3", "", "alice"], | |
| 247 | ]); | |
| 248 | const after = await ( | |
| 249 | await fetch(`${servers.url}/settings/capacity`, { headers: { cookie } }) | |
| 250 | ).text(); | |
| 251 | expect(after).toContain("alice"); | |
| 252 | }, 60_000); | |
| 253 | ||
| 254 | test("an admin changes a capacity through the API; a wrong value is refused", async () => { | |
| 232 | 255 | const put = await api( |
| 233 | 256 | aliceAdmin, |
| 234 | 257 | "PUT", |
| @@ -238,17 +261,6 @@ describe("capacity: only a forge admin reads or changes it", () => { | ||
| 238 | 261 | expect(put.status).toBe(200); |
| 239 | 262 | expect(capacityValue(app.db, "crew.concurrency")).toBe("3"); |
| 240 | 263 | |
| 241 | const dry = await mcp(aliceAdmin, { key: "cells.egress", value: "report" }); | |
| 242 | expect(dry.error).toBe(false); | |
| 243 | expect(egressSetting(app.db)).toBe("enforce"); | |
| 244 | const made = await mcp(aliceAdmin, { | |
| 245 | key: "cells.egress", | |
| 246 | value: "report", | |
| 247 | dryRun: false, | |
| 248 | }); | |
| 249 | expect(made.error).toBe(false); | |
| 250 | expect(egressSetting(app.db)).toBe("report"); | |
| 251 | ||
| 252 | 264 | for (const [key, value] of [ |
| 253 | 265 | ["checks.concurrency", "0"], |
| 254 | 266 | ["checks.concurrency", "1.5"], |
| @@ -267,32 +279,23 @@ describe("capacity: only a forge admin reads or changes it", () => { | ||
| 267 | 279 | ).status, |
| 268 | 280 | ).toBe(422); |
| 269 | 281 | |
| 270 | // Back to the default: the page's reset, then the API's null. | |
| 271 | const reset = await fetch(`${servers.url}/settings/capacity`, { | |
| 272 | method: "POST", | |
| 273 | redirect: "manual", | |
| 274 | headers: { | |
| 275 | cookie, | |
| 276 | "content-type": "application/x-www-form-urlencoded", | |
| 277 | origin: servers.url, | |
| 278 | }, | |
| 279 | body: new URLSearchParams({ key: "checks.shards", reset: "1" }), | |
| 280 | }); | |
| 281 | expect(reset.status).toBe(303); | |
| 282 | expect(capacityEnv(app.db)).toEqual({}); | |
| 282 | expect(changesTo("crew.concurrency")).toEqual([["2", "3", "alice"]]); | |
| 283 | expect(changesTo("checks.concurrency")).toEqual([]); | |
| 284 | }, 60_000); | |
| 283 | 285 | |
| 284 | const changes = capacityChanges(app.db); | |
| 285 | expect(changes.map((c) => [c.key, c.from, c.to, c.actorName])).toEqual([ | |
| 286 | ["checks.shards", "3", "", "alice"], | |
| 287 | ["cells.egress", "enforce", "report", "alice"], | |
| 288 | ["crew.concurrency", "2", "3", "alice"], | |
| 289 | ["checks.shards", "", "3", "alice"], | |
| 290 | ]); | |
| 291 | const after = await ( | |
| 292 | await fetch(`${servers.url}/settings/capacity`, { headers: { cookie } }) | |
| 293 | ).text(); | |
| 294 | expect(after).toContain("alice"); | |
| 295 | }); | |
| 286 | test("an admin changes a capacity through MCP; a dry run changes nothing", async () => { | |
| 287 | const dry = await mcp(aliceAdmin, { key: "cells.egress", value: "report" }); | |
| 288 | expect(dry.error).toBe(false); | |
| 289 | expect(egressSetting(app.db)).toBe("enforce"); | |
| 290 | const made = await mcp(aliceAdmin, { | |
| 291 | key: "cells.egress", | |
| 292 | value: "report", | |
| 293 | dryRun: false, | |
| 294 | }); | |
| 295 | expect(made.error).toBe(false); | |
| 296 | expect(egressSetting(app.db)).toBe("report"); | |
| 297 | expect(changesTo("cells.egress")).toEqual([["enforce", "report", "alice"]]); | |
| 298 | }, 60_000); | |
| 296 | 299 | }); |
| 297 | 300 | |
| 298 | 301 | describe("report mode is loud to admins, and to nobody else", () => { |
new above
offline