import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { scopedTestState } from "../sites/scoped.test-helper";
let d1 = fakeD1();
mock.module("cloudflare:workers", () => ({
env: {
get DB() {
return d1.binding;
},
},
waitUntil: () => undefined,
}));
const scoped = scopedTestState();
scoped.install();
const { linkForkObjects, linkObjects, purgeRepoLfs, rebillRepoLfs } = await import("./objects");
const { billableBytes, lfsUsage } = await import("./quota");
const refs = (oid: string) =>
(d1.db.query("SELECT refs FROM lfs_blobs WHERE oid = ?").get(oid) as { refs: number } | null)?.refs;
beforeEach(() => {
d1 = fakeD1();
scoped.reset();
d1.db.run(`CREATE TABLE lfs_blobs (oid TEXT PRIMARY KEY, size INTEGER NOT NULL, r2_key TEXT NOT NULL,
refs INTEGER NOT NULL DEFAULT 0, orphaned_at INTEGER, created_at INTEGER NOT NULL)`);
d1.db.run(
"INSERT INTO lfs_blobs VALUES ('o1', 10, 'k1', 1, NULL, 0), ('o2', 20, 'k2', 1, NULL, 0), ('o3', 5, 'k3', 0, 1, 0)",
);
});
describe("LFS links across scopes", () => {
test("bills an owner once per oid however many of its repositories link it", async () => {
await linkObjects({ id: "r1", owner_id: "u1" }, [
{ oid: "o1", size: 10 },
{ oid: "o2", size: 20 },
]);
await linkObjects({ id: "r2", owner_id: "u1" }, [{ oid: "o1", size: 10 }]);
expect((await lfsUsage("u1")).storageBytes).toBe(30);