102 lines · 3.8 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";

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,
}));

const repoIds = ["repo-a", "repo-b", "repo-c"];
mock.module("./db", () => ({
  all: async (sql: string, ...params: unknown[]) => {
    if (sql.includes("WHERE id > ?")) {
      const [cursor, limit] = params as [string, number];
      return repoIds
        .filter((id) => id > cursor)
        .slice(0, limit)
        .map((id) => ({ id }));
    }
    return repoIds.map((id) => ({ id }));
  },
  chunks: <T>(items: T[]) => [items],
  placeholders: (count: number) => Array.from({ length: count }, () => "?").join(", "),
}));
mock.module("./scoped-db/locators", () => ({
  forgetLocator: async () => undefined,
  forgetScope: async () => undefined,
}));

const { memoryScopes } = await import("./scoped-db/testing");
const scopes = memoryScopes();
const visited: string[] = [];
mock.module("./scoped-db", () => ({
  ...scopes.module,
  repoDb: (repoId: string) => {
    visited.push(repoId);