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

let d1 = fakeD1();
let scopes = memoryScopes();
const state = () => scopes.raw("repo", "r1");
const fakeEnv: Record<string, unknown> = {
  get DB() {
    return d1.binding;
  },
  PUBLIC_URL: "https://forge.test",
  CLOUD_MINUTES_PER_DAY: "600",
};
mock.module("cloudflare:workers", () => ({ DurableObject: class {}, env: fakeEnv, waitUntil: () => undefined }));
const realQuota = await import("../metering/quota");
mock.module("../metering/quota", () => ({ ...realQuota, assertWithinQuota: async () => undefined }));

mock.module("../scoped-db", () => ({
  repoDb: (id: string) => scopes.repoDb(id),
  accountDb: (id: string) => scopes.accountDb(id),
  stmt: scopes.module.stmt,
}));

const { budgetBusy, chargeDispatched, ownerQuota } = await import("./dispatch");

let seq = 0;
function addJob(runId: string, status: string, timeoutMinutes: number): void {
  seq++;
  seedJob(state(), {
    id: `j${seq}`,
    run_id: runId,
    repo_id: "r1",
    spec: JSON.stringify({ timeoutMinutes }),
    status,
    started_at: status === "pending" ? null : 1,
  });
}