25 lines · 1.0 KB
import { describe, expect, test } from "bun:test";
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join, relative } from "node:path";

const SRC = join(import.meta.dir, "..");
const WRITE = /(INSERT\s+INTO|UPDATE|DELETE\s+FROM|REPLACE\s+INTO)\s+repo_storage_estimates\b/i;

function sourceFiles(dir: string): string[] {
  return readdirSync(dir).flatMap((name) => {
    const path = join(dir, name);
    if (statSync(path).isDirectory()) return sourceFiles(path);
    return /\.tsx?$/.test(name) && !name.endsWith(".test.ts") ? [path] : [];
  });
}

describe("repo_storage_estimates.pack_bytes", () => {
  test("is written only by the repo-counts counter, whose absolute flush would overwrite any other writer", () => {
    const writers = sourceFiles(SRC)
      .filter((file) => WRITE.test(readFileSync(file, "utf8")))
      .map((file) => relative(SRC, file));
    // A new writer (recompute, sweep, transfer) must go through the counter or call Counters.reset afterwards.
    expect(writers).toEqual(["server/counters-do.ts"]);
  });
});