import { flush } from "solid-js"; import { beforeEach, describe, expect, test, vi } from "vitest"; import { createRefs, filterRefs, groupRefs, highlight } from "../../src/lib/refs"; import { reference } from "../fixtures/rows"; const listReferences = vi.fn(async () => [reference("main"), reference("v1.0.0", "tag")]); vi.mock("../../src/lib/api", () => ({ listReferences: (...args: unknown[]) => listReferences(...(args as [])), })); beforeEach(() => { listReferences.mockClear(); listReferences.mockImplementation(async () => [reference("main"), reference("v1.0.0", "tag")]); }); describe("groupRefs", () => { test("splits refs by what their namespace says they are", () => { const grouped = groupRefs([ reference("main"), reference("origin/main", "remoteBranch"), reference("v1.0.0", "tag"), ]); expect(grouped.branches.map((entry) => entry.shorthand)).toEqual(["main"]); expect(grouped.remotes.map((entry) => entry.shorthand)).toEqual(["origin/main"]); expect(grouped.tags.map((entry) => entry.shorthand)).toEqual(["v1.0.0"]); }); test("drops refs that are machinery rather than navigation", () => { // Notes, refs/stash and replacements would bury the branches. const grouped = groupRefs([ reference("main"), reference("stash", "other"), reference("notes/commits", "other"), ]); expect(grouped.branches).toHaveLength(1); expect(grouped.remotes).toHaveLength(0); expect(grouped.tags).toHaveLength(0); }); test("sorts names the way a person reads them", () => { // Sorting as plain strings would put v10 before v9. const grouped = groupRefs([ reference("v9.0.0", "tag"), reference("v10.0.0", "tag"), reference("v2.0.0", "tag"), ]); expect(grouped.tags.map((entry) => entry.shorthand)).toEqual(["v2.0.0", "v9.0.0", "v10.0.0"]); }); test("sorts branches regardless of the order they arrived in", () => { const grouped = groupRefs([reference("zebra"), reference("alpha"), reference("feature/thing")]); expect(grouped.branches.map((entry) => entry.shorthand)).toEqual([ "alpha", "feature/thing", "zebra", ]); }); test("does not mutate what it was given", () => { const entries = [reference("zebra"), reference("alpha")]; groupRefs(entries); expect(entries.map((entry) => entry.shorthand)).toEqual(["zebra", "alpha"]); }); test("copes with a repository that has no refs at all", () => { const grouped = groupRefs([]); expect(grouped).toEqual({ branches: [], remotes: [], tags: [] }); }); }); describe("filterRefs", () => { const REFS = [ reference("main"), reference("feature/graph"), reference("origin/main", "remoteBranch"), reference("v1.0.0", "tag"), ]; test("keeps everything when nothing was typed", () => { expect(filterRefs(REFS, "")).toHaveLength(4); expect(filterRefs(REFS, " ")).toHaveLength(4); }); test("keeps refs whose name contains what was typed", () => { expect(filterRefs(REFS, "main").map((entry) => entry.shorthand)).toEqual([ "main", "origin/main", ]); }); test("ignores case, because nobody wants to match a branch name exactly", () => { expect(filterRefs(REFS, "MAIN")).toHaveLength(2); expect(filterRefs([reference("Feature/Thing")], "feature")).toHaveLength(1); }); test("ignores surrounding space, which is easy to type by accident", () => { expect(filterRefs(REFS, " main ")).toHaveLength(2); }); test("matches anywhere in the name, not only at the start", () => { expect(filterRefs(REFS, "graph").map((entry) => entry.shorthand)).toEqual(["feature/graph"]); }); test("treats what was typed as text, not as a pattern", () => { // A regular expression would throw on this, or match far too much. const refs = [reference("release/v1.0"), reference("release/v1x0")]; expect(filterRefs(refs, "v1.0").map((entry) => entry.shorthand)).toEqual(["release/v1.0"]); expect(filterRefs(refs, "*")).toHaveLength(0); expect(filterRefs(refs, "(")).toHaveLength(0); }); test("keeps nothing when nothing contains it", () => { expect(filterRefs(REFS, "nowhere")).toEqual([]); }); test("does not mutate what it was given", () => { const refs = [reference("zebra"), reference("alpha")]; filterRefs(refs, "a"); expect(refs).toHaveLength(2); }); }); describe("highlight", () => { test("returns the text untouched when nothing is being filtered", () => { expect(highlight("main", "")).toEqual([{ text: "main", matched: false }]); }); test("splits around a match", () => { expect(highlight("feature/graph", "graph")).toEqual([ { text: "feature/", matched: false }, { text: "graph", matched: true }, ]); }); test("keeps the original casing of what it marks", () => { // Matching ignores case, but the name must still read as it is stored. expect(highlight("Feature/Graph", "graph")).toEqual([ { text: "Feature/", matched: false }, { text: "Graph", matched: true }, ]); }); test("marks every occurrence", () => { expect(highlight("release/release-notes", "release")).toEqual([ { text: "release", matched: true }, { text: "/", matched: false }, { text: "release", matched: true }, { text: "-notes", matched: false }, ]); }); test("marks a match at the very start and the very end", () => { expect(highlight("main", "main")).toEqual([{ text: "main", matched: true }]); expect(highlight("origin/main", "main")).toEqual([ { text: "origin/", matched: false }, { text: "main", matched: true }, ]); }); test("leaves text alone when there is no match in it", () => { expect(highlight("main", "zzz")).toEqual([{ text: "main", matched: false }]); }); test("handles text that is entirely one repeated match", () => { expect(highlight("aaa", "a")).toHaveLength(3); }); test("puts the text back together exactly as it was", () => { for (const query of ["a", "e", "feature", "/", "zzz", ""]) { const rebuilt = highlight("feature/a-feature", query) .map((segment) => segment.text) .join(""); expect(rebuilt).toBe("feature/a-feature"); } }); }); describe("createRefs", () => { test("starts empty and asks for nothing", () => { const refs = createRefs(); expect(refs.entries()).toEqual([]); expect(listReferences).not.toHaveBeenCalled(); }); test("loads a repository's refs", async () => { const refs = createRefs(); await refs.show("/demo/.git"); await flush(); expect(listReferences).toHaveBeenCalledWith("/demo/.git"); expect(refs.grouped().branches).toHaveLength(1); expect(refs.grouped().tags).toHaveLength(1); }); test("replaces what was shown when another repository opens", async () => { const refs = createRefs(); await refs.show("/first/.git"); await flush(); listReferences.mockResolvedValueOnce([reference("develop")]); await refs.show("/second/.git"); await flush(); expect(refs.grouped().branches.map((entry) => entry.shorthand)).toEqual(["develop"]); expect(refs.grouped().tags).toHaveLength(0); }); test("empties itself when asked to clear", async () => { const refs = createRefs(); await refs.show("/demo/.git"); await flush(); refs.clear(); await flush(); expect(refs.entries()).toEqual([]); }); test("forgets the filter when another repository opens", async () => { // A filter written for one repository rarely means anything in the next, // and leaving it on would make a full sidebar look empty. const refs = createRefs(); await refs.show("/first/.git"); refs.setQuery("feature"); await flush(); await refs.show("/second/.git"); await flush(); expect(refs.query()).toBe(""); expect(refs.isFiltering()).toBe(false); }); test("counts what matched against what there is", async () => { const refs = createRefs(); await refs.show("/demo/.git"); refs.setQuery("main"); await flush(); expect(refs.counts()).toEqual({ matched: 1, total: 2 }); }); test("reports a failure without leaving stale refs on screen", async () => { // Showing another repository's branches would be worse than showing none. const refs = createRefs(); await refs.show("/first/.git"); await flush(); listReferences.mockRejectedValueOnce({ kind: "git", message: "could not list references" }); await refs.show("/second/.git"); await flush(); expect(refs.entries()).toEqual([]); expect(refs.message()).toBe("could not list references"); }); });