76 lines · 2.6 KB
import { describe, expect, test } from "bun:test";
import { fastForward } from "./fast-forward";
import type { CommitSummary } from "~/lib/types";

type Graph = Record<string, string[]>;

function storeFor(graph: Graph) {
  return {
    async prefetchHistory() {},
    async commit(sha: string): Promise<CommitSummary | null> {
      const parents = graph[sha];
      if (!parents) return null;
      return {
        sha,
        shortSha: sha.slice(0, 7),
        subject: sha,
        message: sha,
        authorName: "A",
        authorEmail: "a@example.com",
        committerName: "A",
        authoredAt: 0,
        committedAt: 0,
        parents,
        treeSha: "t",
      };
    },
  };
}

/** a <- b <- c is indexed with head c. */
const base: Graph = { a: [], b: ["a"], c: ["b"] };
const indexed = (sha: string) => ["a", "b", "c"].includes(sha);
const shas = (commits: Array<{ sha: string }> | null) => commits?.map((commit) => commit.sha).sort() ?? null;

describe("fastForward", () => {
  test("linear fast-forward returns only the new commits", async () => {
    const graph = { ...base, d: ["c"], e: ["d"] };
    expect(shas(await fastForward(storeFor(graph), indexed, "c", "e"))).toEqual(["d", "e"]);
  });