Files
radicle-explorer/tests/unit/revisionDescription.test.ts
T
Rūdolfs Ošiņš 9ff5529c9f Move revision description helpers into lib
The commit-list check was written twice, and revision titles and the
commit group collapse rule were untested inside components.
2026-10-01 18:20:24 +02:00

119 lines
3.4 KiB
TypeScript

import type { Commit } from "@bindings/repo/Commit";
import { describe, expect, test } from "vitest";
import {
groupCommitsByAuthor,
isCommitListDescription,
revisionTitle,
splitDescription,
visibleCommitsOf,
} from "@app/lib/revisionDescription";
import { revision } from "./support/cobs";
function commit(id: string, summary = id, name = "Alice"): Commit {
const person = { name, email: `${name}@x`, time: 0 };
return {
id,
author: person,
committer: person,
summary,
message: summary,
parents: [],
};
}
describe("splitDescription", () => {
test.each([
["", {}],
[" ", {}],
["Subject", { subject: "Subject" }],
[" Subject \n\n Body text ", { subject: "Subject", body: "Body text" }],
["Subject\n", { subject: "Subject" }],
])("splits %j into %j", (text, expected) => {
expect(splitDescription(text)).toEqual(expected);
});
});
test("revisionTitle is the first line of the current description", () => {
const edit = (body: string) => ({
author: revision().author,
timestamp: 0,
body,
});
expect(
revisionTitle(revision({ description: [edit("Old"), edit("New\nbody")] })),
).toBe("New");
expect(revisionTitle(revision({ description: [] }))).toBeUndefined();
expect(
revisionTitle(revision({ description: [edit(" \n ")] })),
).toBeUndefined();
});
describe("isCommitListDescription", () => {
const commits = [commit("a", "Add a"), commit("b", "Fix b")];
test("matches a description that lists the commit summaries", () => {
expect(isCommitListDescription("Add a\nFix b", commits)).toBe(true);
expect(isCommitListDescription(" Fix b\n\nAdd a ", commits)).toBe(true);
});
test.each([
["Add a", "a line is missing"],
["Add a\nFix b\nMore", "there is an extra line"],
["Add a\nSomething else", "a line isn't a summary"],
])("doesn't match %j because %s", description => {
expect(isCommitListDescription(description, commits)).toBe(false);
});
test("doesn't match without commits", () => {
expect(isCommitListDescription("", [])).toBe(false);
expect(isCommitListDescription("Add a", undefined)).toBe(false);
});
});
test("groupCommitsByAuthor groups consecutive commits by author name", () => {
const groups = groupCommitsByAuthor([
commit("1", "1", "Alice"),
commit("2", "2", "Alice"),
commit("3", "3", "Bob"),
commit("4", "4", "Alice"),
]);
expect(groups.map(g => g.map(c => c.id))).toEqual([["1", "2"], ["3"], ["4"]]);
});
describe("visibleCommitsOf", () => {
const group = (n: number) =>
Array.from({ length: n }, (_, i) => commit(`${i}`));
test("shows a short group in full", () => {
expect(visibleCommitsOf(group(5), false)).toMatchObject({
collapsed: false,
hiddenCount: 0,
});
expect(visibleCommitsOf(group(5), false).visible).toHaveLength(5);
});
test("collapses a long group to its first three", () => {
const { collapsed, visible, hiddenCount } = visibleCommitsOf(
group(6),
false,
);
expect(collapsed).toBe(true);
expect(visible.map(c => c.id)).toEqual(["0", "1", "2"]);
expect(hiddenCount).toBe(3);
});
test("shows a long group in full once expanded", () => {
expect(visibleCommitsOf(group(6), true)).toMatchObject({
collapsed: false,
hiddenCount: 0,
});
expect(visibleCommitsOf(group(6), true).visible).toHaveLength(6);
});
});