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

class StubEntrypoint {}
mock.module("cloudflare:workers", () => ({
  env: {},
  waitUntil: () => undefined,
  WorkflowEntrypoint: StubEntrypoint,
  DurableObject: StubEntrypoint,
  WorkerEntrypoint: StubEntrypoint,
}));
mock.module("./db", () => ({
  all: async () => [],
  first: async () => null,
  run: async () => ({ meta: { changes: 0 } }),
  batch: async () => [],
  stmt: () => ({}),
  placeholders: () => "",
  chunks: <T>(items: T[]) => [items],
  MAX_IN_LIST: 90,
}));
mock.module("./merge-automation", () => ({ onCommitStatus: async () => undefined }));

/** Workflow file content by commit sha; the "store" exposes one file at `.gitfor/workflows/ci.yml`. */
const WORKFLOW_PATH = ".gitfor/workflows/ci.yml";
const OLD_SHA = "a".repeat(40);
const TIP_SHA = "b".repeat(40);
const OLD_SOURCE = "name: CI\non: pull_request\njobs:\n  test:\n    steps:\n      - run: echo ok\n";
const TIP_SOURCE = "name: CI\non: pull_request\njobs:\n  test:\n    steps:\n      - run: npm test\n";
const filesAt: Record<string, string> = { [OLD_SHA]: OLD_SOURCE, [TIP_SHA]: TIP_SOURCE };

mock.module("./git/store", () => ({
  ZERO_SHA: "0".repeat(40),
  withStore: async (_name: string, fn: (store: unknown) => Promise<unknown>) =>
    fn({
      commit: async (sha: string) => (filesAt[sha] ? { treeSha: `tree:${sha}` } : null),
      entryAtPath: async (treeSha: string, path: string) =>
        path === WORKFLOW_PATH ? { type: "blob", sha: `blob:${treeSha.slice("tree:".length)}` } : null,
      blob: async (sha: string) => new TextEncoder().encode(filesAt[sha.slice("blob:".length)] ?? ""),
    }),
}));