112 lines · 4.6 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { scopedTestState } from "./scoped.test-helper";

let d1 = fakeD1();
const scoped = scopedTestState();
scoped.install();
const kv = new Map<string, string>();
const fakeEnv = {
  get DB() {
    return d1.binding;
  },
  KV: {
    get: async (key: string, type?: string) => {
      const value = kv.get(key) ?? null;
      return value !== null && type === "json" ? JSON.parse(value) : value;
    },
    put: async (key: string, value: string) => void kv.set(key, value),
    delete: async (key: string) => void kv.delete(key),
  },
};
mock.module("cloudflare:workers", () => ({ env: fakeEnv, waitUntil: () => undefined }));

const { dropAliasesForPreview, dropPullAliasesForIssue, syncPullAliases, sweepClosedPullAliasesForScope } =
  await import("./pr-aliases");

function addPull(id: string, number: number, headRepo: string): void {
  scoped.insert("r1", "issues", { id, number });
  scoped.insert("r1", "pull_requests", { issue_id: id, head_repo_id: headRepo, head_ref: "feature" });
  scoped.register("issue", id, "r1");
}

function setup(): void {
  d1 = fakeD1();
  kv.clear();
  scoped.reset();
  scoped.site({ id: "s1", owner_id: "u1", repo_id: "r1", label: "blog--alice" });
  addPull("i7", 7, "r1");
  addPull("i8", 8, "fork");
}