import { beforeEach, describe, expect, mock, test } from "bun:test";
const env = { EMAIL_REPLY_DOMAIN: "gitfor.dev", SECRETS_KEY: "k".repeat(32), PUBLIC_URL: "https://gitfor.dev" };
mock.module("cloudflare:workers", () => ({ env, waitUntil: () => undefined }));
type Issue = { id: string; repo_id: string; is_pull: number; locked: number; number: number; title: string };
const users = new Map([["u1aaaaaaaaaaaaaaaaaa", { account_id: "u1aaaaaaaaaaaaaaaaaa", name: "alice" }]]);
const emails = new Map([["u1aaaaaaaaaaaaaaaaaa", [{ email: "Alice@Example.com", verified: true, primary: true }]]]);
let issues = new Map<string, Issue>();
let access: "read" | "none" = "read";
let comments: Array<{ issueId: string; userId: string; body: string }> = [];
mock.module("./auth", () => ({
loadUserById: async (id: string) => users.get(id) ?? null,
toViewer: (user: { account_id: string; name: string }) => ({
id: user.account_id,
name: user.name,
displayName: null,
email: "",
isAdmin: false,
}),
}));
mock.module("./db", () => ({ first: async (_sql: string, id: string) => issues.get(id) ?? null }));
mock.module("./user-emails", () => ({ listAccountEmails: async (id: string) => emails.get(id) ?? [] }));
mock.module("./limits", () => ({ limitWrite: async () => undefined }));
mock.module("./repos", () => ({
loadRepoById: async (id: string) => ({ id, owner_name: "acme", name: "web", has_issues: 1 }),
requireRepoMutation: async () => {
if (access === "none") {
const { notFoundError } = await import("./errors");
throw notFoundError("Repository not found");
}
return { repo: { id: "r1" }, permission: "read" };
},
}));
mock.module("./issue-comments", () => ({
createIssueComment: async (_access: unknown, issue: Issue, viewer: { id: string }, body: string) => {
comments.push({ issueId: issue.id, userId: viewer.id, body });
},
}));