import { beforeEach, describe, expect, mock, test } from "bun:test";
import { memoryScopes } from "../scoped-db/testing";
let scopes = memoryScopes();
const fakeEnv: Record<string, unknown> = { CACHE_BYTES_PER_REPO: "1000" };
mock.module("cloudflare:workers", () => ({ DurableObject: class {}, env: fakeEnv, waitUntil: () => undefined }));
mock.module("../scoped-db", () => ({
repoDb: (id: string) => scopes.repoDb(id),
accountDb: (id: string) => scopes.accountDb(id),
stmt: scopes.module.stmt,
}));
const { assertCacheFits } = await import("./cache-api");
function insert(id: string, size: number): void {
scopes
.raw("repo", "r1")
.query(
`INSERT INTO action_caches (id, repo_id, ref, key, version, size, r2_key, created_at, last_used_at)
VALUES (?, 'r1', 'refs/heads/main', ?, 'v', ?, ?, 0, 0)`,
)
.run(id, id, size, `k/${id}`);
}
beforeEach(() => {
scopes = memoryScopes();
});
describe("cache quota", () => {
test("in-flight reservations count, so concurrent saves can't overshoot", async () => {
insert("a", 600);
insert("b", -300);
await assertCacheFits("r1");
insert("c", -200);
await expect(assertCacheFits("r1")).rejects.toMatchObject({ status: 413 });
});
});