83 lines · 3.2 KB
import { describe, expect, mock, test } from "bun:test";
import type { Permission, RepoRow } from "~/lib/types";

mock.module("cloudflare:workers", () => ({
  env: {},
  waitUntil: () => undefined,
  WorkflowEntrypoint: class {},
  DurableObject: class {},
  WorkerEntrypoint: class {},
}));

const grants = new Map<string, Permission>();
const users = new Set(["u-bob"]);

mock.module("../auth", () => ({
  loadUserById: async (id: string) => (users.has(id) ? { id, name: id, email: `${id}@x`, is_admin: 0 } : null),
  toViewer: (user: { id: string; name: string; email: string }) => ({ ...user, displayName: null, isAdmin: false }),
}));
mock.module("../repos", () => ({
  hasPermission: (actual: Permission, needed: Permission) => {
    const rank = { none: 0, read: 1, write: 2, admin: 3 };
    return rank[actual] >= rank[needed];
  },
  repoPermission: async (repo: RepoRow, viewer: { id: string } | null) =>
    viewer ? (grants.get(`${repo.id}:${viewer.id}`) ?? (repo.visibility === "public" ? "read" : "none")) : "none",
}));

const { canBridge, isPullHeadRepo, pullHeadAccessible } = await import("./bridge-policy");

function repo(overrides: Partial<RepoRow>): RepoRow {
  return {
    id: "r",
    owner_id: "o",
    visibility: "private",
    fork_of_id: null,
    detached: 0,
    ...overrides,
  } as RepoRow;
}