Files
radicle-explorer/desktop/tests/unit/newerCommits.test.ts
T
Rūdolfs Ošiņš 78e56dc9ac desktop: Add new commits to the commits list
When the branch moves on, its new commits are added at the top of the
list, without moving the rows in view, instead of the list going stale
until the next visit. Rewritten history offers to reload the list.
2026-11-20 19:32:17 +01:00

65 lines
2.0 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { newerCommits } from "@app/lib/newerCommits";
function commit(id: string, parents = ["p"]) {
return { id, parents };
}
// A branch's history from its head, newest first, paged like the backend.
function history(...commits: { id: string; parents: string[] }[]) {
return async (skip: number, take: number) => ({
cursor: skip,
content: commits.slice(skip, skip + take),
more: skip + take < commits.length,
});
}
describe("newerCommits", () => {
test("returns the commits before the old head", async () => {
const fetch = history(commit("c"), commit("b"), commit("a"));
expect(await newerCommits(fetch, "a", 100, 10)).toEqual({
type: "added",
commits: [commit("c"), commit("b")],
merge: false,
});
});
test("follows pages until it finds the old head", async () => {
const fetch = history(commit("d"), commit("c"), commit("b"), commit("a"));
const result = await newerCommits(fetch, "a", 100, 2);
expect(result).toEqual({
type: "added",
commits: [commit("d"), commit("c"), commit("b")],
merge: false,
});
});
test("says when a merge brought in older commits", async () => {
const fetch = history(commit("m", ["a", "x"]), commit("a"));
const result = await newerCommits(fetch, "a", 100, 10);
expect(result).toMatchObject({ type: "added", merge: true });
});
test("an unmoved head adds nothing", async () => {
const fetch = history(commit("a"));
expect(await newerCommits(fetch, "a", 100, 10)).toEqual({
type: "added",
commits: [],
merge: false,
});
});
test("history without the old head was rewritten", async () => {
const fetch = history(commit("c"), commit("b"));
expect(await newerCommits(fetch, "a", 100, 10)).toEqual({
type: "rewritten",
});
});
test("gives up past the limit", async () => {
const fetch = history(commit("d"), commit("c"), commit("b"), commit("a"));
expect(await newerCommits(fetch, "a", 2, 2)).toEqual({ type: "rewritten" });
});
});