84 lines · 3.1 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { readFileSync } from "node:fs";
import { fakeD1 } from "../metering/fake-d1.test-helper";

let d1 = fakeD1();
const kv = new Map<string, string>();
mock.module("cloudflare:workers", () => ({
  env: {
    get DB() {
      return d1.binding;
    },
    KV: {
      get: async (key: string) => kv.get(key) ?? null,
      put: async (key: string, value: string) => void kv.set(key, value),
    },
  },
  waitUntil: () => undefined,
}));
const { REPO_SCHEMA } = await import("../scoped-db/schema.gen");
const { forgetSiteDirectory, publishSiteDirectory, siteDirectoryStats } = await import("./directory");

const NOW = 1_000_000_000_000;
const WEEK = NOW - 7 * 24 * 3600_000;

function scope() {
  const db = new Database(":memory:");
  for (const statement of REPO_SCHEMA) db.run(statement);
  const sql = {
    exec: (query: string, ...params: Array<string | number | null>) => {
      const rows = db.query(query).all(...params) as never[];
      return { toArray: () => rows };
    },
  };
  const site = (id: string, fields: Record<string, string | number | null>) => {
    const row = { id, label: id, source: "branch", created_at: 0, updated_at: 0, ...fields };
    db.query(
      `INSERT INTO sites (${Object.keys(row).join(", ")}) VALUES (${Object.keys(row)
        .map(() => "?")
        .join(", ")})`,