import { beforeEach, describe, expect, mock, test } from "bun:test";
import { scopedTestState } from "./scoped.test-helper";
const kv = new Map<string, string>();
mock.module("cloudflare:workers", () => ({
env: {
KV: {
get: async (key: string) => kv.get(key) ?? null,
put: async (key: string, value: string) => void kv.set(key, value),
},
},
waitUntil: () => undefined,
}));
mock.module("./directory", () => ({ forgetSiteDirectory: async () => undefined }));
const repos: Record<string, string[]> = { u1: ["r1", "r2"] };
mock.module("../db", () => ({
all: async (_sql: string, ownerId: string) => (repos[ownerId] ?? []).map((id) => ({ id })),
}));
const scoped = scopedTestState();
scoped.install();
const { backfillDomainSites, backfillOwnedSites, domainSites, forgetSite, ownerSiteScopes, registerOwnedSite } =
await import("./store");
beforeEach(() => {
kv.clear();
scoped.reset();
scoped.site({ id: "s1", repo_id: "r1", owner_id: "u1" });
scoped.site({ id: "s2", owner_id: "u1" });
scoped.site({ id: "s3", owner_id: "u2" });
scoped.site({ id: "s4" });
});
describe("owner site scopes", () => {
test("before the backfill every repo-less site scope is a candidate", async () => {
expect((await ownerSiteScopes("u1")).sort()).toEqual(["r1", "site:s2", "site:s3", "site:s4"]);
});
test("after the backfill only the owner's claimed sites and repositories holding a site", async () => {
await backfillOwnedSites();
expect(kv.get("backfill:owner-site-locators")).toBe("done");