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

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

const grants = new Map<string, Permission>();
const orgAdmins = new Set<string>();
const orgMembers = new Set<string>();

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: Viewer) =>
    grants.get(`${repo.id}:${viewer.id}`) ?? (repo.visibility === "public" ? "read" : "none"),
  canAdministerAccount: async (accountId: string, viewer: Viewer) =>
    accountId === viewer.id || orgAdmins.has(`${accountId}:${viewer.id}`),
  canCreateInAccount: async (accountId: string, viewer: Viewer) =>
    accountId === viewer.id ||
    orgAdmins.has(`${accountId}:${viewer.id}`) ||
    orgMembers.has(`${accountId}:${viewer.id}`),
}));

const { assertCanFork, assertCanTransfer } = await import("./fork-policy");

const bob: Viewer = { id: "u-bob", name: "bob", displayName: null, email: "b@x", isAdmin: false, emailVerified: true };
const privateRepo = { id: "A", visibility: "private", allow_forking: 0 } as RepoRow;
const publicRepo = { id: "P", visibility: "public", allow_forking: 0 } as RepoRow;

async function status(promise: Promise<unknown>): Promise<number | "ok"> {
  try {
    await promise;
    return "ok";