Lutra/ui/tests/vcs/vcsStatus.test.ts
danny fc8ae6c2e3 M4.4: Git gutter change markers and status colours in tree and tabs
- git.lineChanges {id} -> {ranges: [{kind, startLine, endLine}], version}:
  the buffer against its index blob, line diff with similar (Myers +
  Replace, no Compact) off the lock, CRLF-safe; index texts cached per
  path and dropped on gitChanged (State::emit -> git_lines::observe)
- editor gutter markers for added, modified and deleted lines, queried
  150 ms after edits, on show and on gitChanged, mapped through edits
- vcsStatus store (git.status on projectOpened and gitChanged, no
  polling) colouring file names in the project tree and editor tabs with
  the --lt-vcs-* tokens; contrast also checked on the active tab
- tests: unit and property tests, real-repo engine tests, Vitest
- docs: architecture, parity, gaps, roadmap, ui-parity, ROADMAP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 17:40:30 +01:00

155 lines
6.4 KiB
TypeScript

/**
* Tests of the Git status colours (M4.4, `stores/vcsStatus.ts`): folding
* index and work tree states into a colour, reading untrusted
* `git.status` results, one query at a time with one more after it,
* reacting to `projectOpened`, `projectClosed` and `gitChanged` (which
* also refreshes the gutters), and coloured tab names.
*/
import { flushPromises, mount } from "@vue/test-utils";
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
const refreshGitGutters = vi.hoisted(() => vi.fn());
vi.mock("../../src/components/editor/gitGutter", () => ({ refreshGitGutters }));
import EditorTabs from "../../src/components/editor/EditorTabs.vue";
import { useEditorStore } from "../../src/stores/editor";
import { readStatus, statusColour, useVcsStatusStore, type VcsStatusTransport } from "../../src/stores/vcsStatus";
/** A `git.status` result with `files`. */
function status(files: unknown[]): unknown {
return { available: true, repo: { root: "/r", branch: "main", detached: false }, files };
}
/** A transport whose answers the test releases one by one. */
class HeldTransport implements VcsStatusTransport {
/** Queries waiting for an answer. */
readonly waiting: ((value: unknown) => void)[] = [];
status(): Promise<{ ok: true; value: unknown }> {
return new Promise((resolve) => {
this.waiting.push((value) => {
resolve({ ok: true, value });
});
});
}
}
beforeEach(() => {
setActivePinia(createPinia());
refreshGitGutters.mockReset();
});
describe("statusColour", () => {
it("puts conflicts first, then untracked, added, deleted and other changes", () => {
expect(statusColour("conflicted", "conflicted")).toBe("conflicted");
expect(statusColour("untracked", "untracked")).toBe("untracked");
expect(statusColour("added", "unmodified")).toBe("added");
expect(statusColour("added", "modified")).toBe("added");
expect(statusColour("added", "deleted")).toBeNull();
expect(statusColour("deleted", "unmodified")).toBe("deleted");
expect(statusColour("unmodified", "deleted")).toBe("deleted");
expect(statusColour("modified", "unmodified")).toBe("modified");
expect(statusColour("renamed", "modified")).toBe("modified");
expect(statusColour("unmodified", "unmodified")).toBeNull();
});
});
describe("readStatus", () => {
it("skips malformed files and notes whether a repository is open", () => {
const read = readStatus(
status([
{ path: "a.php", index: "unmodified", worktree: "modified" },
{ path: "b.php", index: "bogus", worktree: "modified" },
{ path: 3, index: "added", worktree: "unmodified" },
"x",
]),
);
expect(read?.repo).toBe(true);
expect([...(read?.colours ?? [])]).toEqual([["a.php", "modified"]]);
expect(readStatus({ available: true, repo: null, files: [] })?.repo).toBe(false);
expect(readStatus({ available: false, repo: null, files: [] })?.repo).toBe(false);
expect(readStatus("nope")).toBeNull();
});
});
describe("the store", () => {
it("runs one query at a time and one more for requests made meanwhile", async () => {
const transport = new HeldTransport();
const store = useVcsStatusStore();
store.configure(transport);
const first = store.refresh();
void store.refresh();
void store.refresh();
expect(transport.waiting).toHaveLength(1);
transport.waiting.shift()?.(status([{ path: "a.php", index: "unmodified", worktree: "modified" }]));
await flushPromises();
expect(store.colourOf("a.php")).toBe("modified");
expect(transport.waiting).toHaveLength(1);
transport.waiting.shift()?.(status([]));
await first;
expect(store.colourOf("a.php")).toBeNull();
expect(transport.waiting).toHaveLength(0);
});
it("refreshes on projectOpened and gitChanged, clears on projectClosed", async () => {
let answer = status([{ path: "n.php", index: "untracked", worktree: "untracked" }]);
const store = useVcsStatusStore();
const calls = vi.fn(() => Promise.resolve({ ok: true as const, value: answer }));
store.configure({ status: calls });
store.handle({ kind: "projectOpened", root: "/r", name: "r" });
await flushPromises();
expect(store.colourOf("n.php")).toBe("untracked");
expect(store.repo).toBe(true);
answer = status([{ path: "n.php", index: "added", worktree: "unmodified" }]);
store.handle({ kind: "gitChanged", available: true, repo: {}, filesChanged: true });
await flushPromises();
expect(store.colourOf("n.php")).toBe("added");
expect(refreshGitGutters).toHaveBeenCalledTimes(1);
store.handle({ kind: "projectClosed" });
expect(store.colourOf("n.php")).toBeNull();
expect(store.repo).toBe(false);
expect(calls).toHaveBeenCalledTimes(2);
});
it("ignores an answer for a project that closed meanwhile and keeps the state on failure", async () => {
const transport = new HeldTransport();
const store = useVcsStatusStore();
store.configure(transport);
void store.refresh();
store.clear();
transport.waiting.shift()?.(status([{ path: "a.php", index: "modified", worktree: "unmodified" }]));
await flushPromises();
expect(store.colourOf("a.php")).toBeNull();
store.configure({ status: () => Promise.resolve({ ok: true, value: status([{ path: "b.php", index: "deleted", worktree: "unmodified" }]) }) });
await store.refresh();
store.configure({ status: () => Promise.resolve({ ok: false }) });
await store.refresh();
expect(store.colourOf("b.php")).toBe("deleted");
});
});
describe("tabs", () => {
it("colour the names of changed files", async () => {
const store = useVcsStatusStore();
store.configure({
status: () => Promise.resolve({ ok: true, value: status([{ path: "src/A.php", index: "unmodified", worktree: "modified" }]) }),
});
await store.refresh();
const editor = useEditorStore();
editor.tabs = [
{ id: 1, path: "src/A.php", name: "A.php", dirty: false, notice: null },
{ id: 2, path: "src/B.php", name: "B.php", dirty: false, notice: null },
];
const wrapper = mount(EditorTabs);
await flushPromises();
const names = wrapper.findAll(".tabs__name");
expect(names[0]?.attributes("data-vcs")).toBe("modified");
expect(names[0]?.classes()).toContain("tabs__name--vcs-modified");
expect(names[1]?.attributes("data-vcs")).toBeUndefined();
wrapper.unmount();
});
});