135 lines · 5.3 KB
import { describe, expect, mock, test } from "bun:test";
import type { CommitSummary } from "~/lib/types";

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

type Entry = { name: string; mode: string; hash: string };
const commits = new Map<string, CommitSummary>();
const trees = new Map<string, Entry[]>();
const blobs = new Map<string, Uint8Array>();

function ancestors(sha: string): Set<string> {
  const seen = new Set<string>();
  const stack = [sha];
  while (stack.length > 0) {
    const next = stack.pop() as string;
    if (seen.has(next)) continue;
    seen.add(next);
    stack.push(...(commits.get(next)?.parents ?? []));
  }
  return seen;
}

mock.module("./git/history", () => ({
  async compareCommits(_base: unknown, baseSha: string, _head: unknown, headSha: string) {
    const fromBase = ancestors(baseSha);
    const ahead: CommitSummary[] = [];
    let mergeBase: string | null = null;
    let cursor: string | undefined = headSha;
    while (cursor) {
      if (fromBase.has(cursor)) {
        mergeBase = cursor;
        break;
      }
      const commit = commits.get(cursor);
      if (!commit) break;
      ahead.push(commit);
      cursor = commit.parents[0];
    }
    return { mergeBase, commits: ahead, behindBy: 0, truncated: false };
  },