| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { freeSlot, IN_FLIGHT_LEASE_MS, MAX_IN_FLIGHT, takeSlot } from "../../src/agentic/inflight.js"; |
| 3 | import { testD1 } from "./stubs/d1.js"; |
| 4 | |
| 5 | describe("model requests at once (phase 5b)", () => { |
| 6 | it("gives a scope at most MAX_IN_FLIGHT slots, frees them, and lets an expired lease stop counting", async () => { |
| 7 | const env = { DB: testD1() }; |
| 8 | const at = Date.parse("2026-10-14T09:00:00Z"); |
| 9 | const ids = []; |
| 10 | for (let i = 0; i < MAX_IN_FLIGHT; i++) ids.push(await takeSlot(env, "org:acme:mia", at)); |
| 11 | expect(ids.every((x) => typeof x === "string")).toBe(true); |
| 12 | expect(await takeSlot(env, "org:acme:mia", at)).toBeNull(); |
| 13 | // Other scopes have their own. |
| 14 | expect(await takeSlot(env, "org:acme:otto", at)).toBeTypeOf("string"); |
| 15 | await freeSlot(env, ids[0]!); |
| 16 | expect(await takeSlot(env, "org:acme:mia", at)).toBeTypeOf("string"); |
| 17 | expect(await takeSlot(env, "org:acme:mia", at)).toBeNull(); |
| 18 | // A lease whose request never settled (its Worker died) counts until it expires. |
| 19 | expect(await takeSlot(env, "org:acme:mia", at + IN_FLIGHT_LEASE_MS - 1)).toBeNull(); |
| 20 | expect(await takeSlot(env, "org:acme:mia", at + IN_FLIGHT_LEASE_MS)).toBeTypeOf("string"); |
| 21 | }); |
| 22 | }); |