214 lines · 8.8 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import type { Database } from "bun:sqlite";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";
import { seedJob, seedRow, seedRun } from "./ci-fixtures.test-helper";

let d1 = fakeD1();
let scopes = memoryScopes();
let state: Database = scopes.raw("repo", "r1");
const runnerCancels: string[] = [];
let runnerCancelError: Error | null = null;
const metered: string[] = [];
const released: string[] = [];
const statuses: string[] = [];
const deployments: string[] = [];
const repoTokens: string[] = [];
const signals: string[] = [];
let statusFailures = 0;
const fakeEnv: Record<string, unknown> = {
  get DB() {
    return d1.binding;
  },
  PUBLIC_URL: "https://forge.test",
  CI_WORKFLOW: {
    get: async (runId: string) => ({ sendEvent: async () => void signals.push(runId) }),
  },
  RUNNER: {
    idFromName: (name: string) => name,
    get: (id: string) => ({
      announce: async () => undefined,
      cancel: async () => {
        runnerCancels.push(id);
        if (runnerCancelError) throw runnerCancelError;
      },
    }),
  },
};
mock.module("cloudflare:workers", () => ({ DurableObject: class {}, env: fakeEnv, waitUntil: () => undefined }));
mock.module("../scoped-db", () => ({
  repoDb: (id: string) => scopes.repoDb(id),