72 lines · 2.8 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { applyCombinedSchema } from "../test-schema.test-helper";
import type { RunnerRow } from "./runner-registration";
import { memoryScopes } from "../scoped-db/testing";

class StubEntrypoint {}
mock.module("cloudflare:workers", () => ({
  env: { PUBLIC_URL: "http://localhost:8787", SECRETS_KEY: "test-key" },
  waitUntil: () => undefined,
  WorkflowEntrypoint: StubEntrypoint,
  DurableObject: StubEntrypoint,
  WorkerEntrypoint: StubEntrypoint,
}));

const db = new Database(":memory:");
applyCombinedSchema(db);

const actualDb = await import("../db");
mock.module("../db", () => ({
  ...actualDb,
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...(params as never[])) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...(params as never[])),
  run: async (sql: string, ...params: unknown[]) => ({
    meta: { changes: db.query(sql).run(...(params as never[])).changes },
  }),
}));

const scopes = memoryScopes();
mock.module("../scoped-db", () => scopes.module);
const repoState = scopes.raw("repo", "repo");

const { claimOnce } = await import("./runner-claim");

function runner(labels: string[]): RunnerRow {
  return {
    id: `r-${labels.join("-")}`,
    name: "r",
    labels: JSON.stringify(labels),
    owner_id: "org",