72 lines · 2.9 KB
import { describe, expect, mock, test } from "bun:test";

const owners = new Map<string, string>([["repo", "alice"]]);
mock.module("cloudflare:workers", () => ({ env: {}, waitUntil: () => undefined }));
mock.module("../db", () => ({
  first: async (_sql: string, repoId: string) => (owners.has(repoId) ? { owner_id: owners.get(repoId) } : null),
  all: async () => [],
  run: async () => ({ meta: { changes: 0 } }),
}));
mock.module("./directory", () => ({
  noteScratchAccount: async () => undefined,
  claimLocator: async (_kind: string, _key: string, scope: string) => scope,
}));
mock.module("../scoped-db/locators", () => ({
  locate: async () => null,
  locatedScopes: async () => [],
  forgetLocator: async () => undefined,
  forgetScope: async () => undefined,
  registerLocator: async () => undefined,
}));
const { memoryScopes } = await import("../scoped-db/testing");
const scopes = memoryScopes();
mock.module("../scoped-db", () => scopes.module);

const { publishScopeGauges, retractScopeGauges, sumScopeGauges } = await import("./scope-gauges");

function sqlOf(accountId: string) {
  const db = scopes.raw("account", accountId);
  return {
    exec: (query: string, ...params: Array<string | number | null>) => {
      const rows = db.query(query).all(...params) as never[];
      return { toArray: () => rows };
    },
  };
}

function gauges(accountId: string): Record<string, number> {
  const rows = scopes.raw("account", accountId).query("SELECT meter, value FROM scope_gauges").all() as Array<{
    meter: string;
    value: number;