Files
RuneStone/tests/smoke/features/git.smoke.mjs
T
2026-11-16 04:21:21 -07:00

116 lines
7.5 KiB
JavaScript

// The native Git feature: the module loads only at first need, the Source control pane stages, unstages, and commits,
// the diff and graph tabs open, errors show in the pane, and Off removes the feature at once.
// It replaces plugins/gitToolbar, which loaded the old Git plugin from its sibling checkout.
import assert from "node:assert/strict";
import path from "node:path";
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("features/git", async ({ page }) => {
const output = await outputDir("features/git");
await mockTauri(page, {
commands: test => {
// The handlers run in the page, so the history is defined here.
const commits = [
{ id: "c3".padEnd(40, "3"), parents: ["c1".padEnd(40, "1"), "c2".padEnd(40, "2")], refs: ["HEAD -> main"], summary: "Merge the feature", author: "Ada", date: "2026-10-29" },
{ id: "c2".padEnd(40, "2"), parents: ["c1".padEnd(40, "1")], refs: ["feature"], summary: "Add the feature", author: "Ada", date: "2026-10-28" },
{ id: "c1".padEnd(40, "1"), parents: [], refs: [], summary: "First commit", author: "Ada", date: "2026-10-27" },
];
test.git = { calls: [], changes: [
{ path: "staged.md", index: "M", worktree: " " },
{ path: "changed.md", index: " ", worktree: "M" },
{ path: "new.md", index: "?", worktree: "?" },
] };
const record = (command, value) => { test.git.calls.push([command, value]); return null; };
return {
...test.workspace({ root: "C:\\git-feature", files: { "staged.md": "A\n", "changed.md": "B\n", "new.md": "C\n" } }),
plugins_snapshot: () => ({ root: "C:/plugins", plugins: [] }),
spelling_menu_install: () => null,
"plugin:window|is_maximized": () => false,
git_status: () => ({ available: true, repository: true, branch: "main", changes: test.git.changes }),
git_diff: ({ path, staged }) => `diff --git a/${path} b/${path}\n@@ -1 +1 @@\n-old line\n+new line${staged ? " (staged)" : ""}`,
git_stage: ({ paths }) => record("stage", paths),
git_unstage: ({ paths }) => record("unstage", paths),
git_commit: ({ message }) => record("commit", message),
git_push: () => { throw "The branch has no upstream branch."; },
git_pull: () => record("pull", null),
git_graph: () => ({ commits }),
git_commit_details: ({ commit }) => ({ id: commit, parents: commits.find(item => item.id === commit).parents, author: "Ada", date: "2026-10-29", message: "Details", files: [{ path: "one.md", status: "M" }, { path: "two.md", status: "A" }] }),
git_commit_diff: ({ path }) => `@@ -0,0 +1 @@\n+${path}`,
};
},
});
await page.goto(smokeUrl);
await page.locator('[data-tree-rel="staged.md"]').waitFor();
const loaded = () => page.evaluate(() => performance.getEntriesByType("resource").some(entry => new URL(entry.name).pathname === "/src/lib/features/git/index.ts"));
assert.equal(await loaded(), false, "The Git module does not load before first need");
const sourceControl = page.locator(".header-left").getByRole("button", { name: "Source control", exact: true });
await sourceControl.click();
const pane = page.locator(".source-control");
await pane.getByText("main", { exact: true }).waitFor();
assert.equal(await loaded(), true, "The Source control pane loads the Git module");
await pane.getByRole("heading", { name: "Staged changes (1)", exact: true }).waitFor();
await pane.getByRole("heading", { name: "Unstaged changes (2)", exact: true }).waitFor();
assert.equal(await pane.locator("summary").textContent(), "Changes (3)", "The section title counts all changes");
assert.equal(await pane.getByRole("heading", { name: "Changes (2)", exact: true }).count(), 0, "No group title repeats the section title");
await pane.getByRole("button", { name: "Stage new.md", exact: true }).click();
await page.waitForFunction(() => window.__TEST__.git.calls.some(([command]) => command === "stage"));
assert.deepEqual(await page.evaluate(() => window.__TEST__.git.calls.at(-1)), ["stage", ["new.md"]]);
await pane.getByRole("button", { name: "Unstage all", exact: true }).click();
await page.waitForFunction(() => window.__TEST__.git.calls.some(([command]) => command === "unstage"));
assert.deepEqual(await page.evaluate(() => window.__TEST__.git.calls.at(-1)), ["unstage", ["staged.md"]]);
const message = pane.getByRole("textbox", { name: "Commit message", exact: true });
const commit = pane.getByRole("button", { name: "Commit staged", exact: true });
assert.equal(await commit.isDisabled(), true, "Commit waits for a message");
await message.fill("Write the notes");
await message.press("Control+Enter");
await pane.getByText("Commit created.", { exact: true }).waitFor();
assert.deepEqual(await page.evaluate(() => window.__TEST__.git.calls.at(-1)), ["commit", "Write the notes"]);
await pane.getByRole("button", { name: "Push to the upstream branch", exact: true }).click();
await pane.locator(".notice.error", { hasText: "no upstream branch" }).waitFor();
for (const theme of ["dark", "light"]) {
await page.evaluate(async mode => { const { saveAppearance } = await import("/src/lib/theme/appearance.ts"); saveAppearance({ theme: "default", mode }); }, theme);
await page.waitForTimeout(100);
await pane.screenshot({ path: path.join(output, `source-control-${theme}.png`) });
}
await pane.getByRole("button", { name: /^changed\.md — Modified$/ }).click();
const diff = page.locator(".git-diff");
await diff.getByText("changed.md · Working tree", { exact: true }).waitFor();
await diff.locator(".line.add", { hasText: "+new line" }).waitFor();
assert.equal(await diff.locator(".line.remove").count(), 1);
await page.screenshot({ path: path.join(output, "diff-tab.png") });
await pane.getByRole("button", { name: "Show Git graph", exact: true }).click();
const graph = page.locator(".git-graph");
const options = graph.getByRole("option");
await options.first().waitFor();
assert.equal(await options.count(), 3);
assert.equal(await options.first().getAttribute("aria-selected"), "true");
await graph.getByRole("button", { name: "one.md", exact: false }).first().waitFor();
await options.first().focus();
await page.keyboard.press("ArrowDown");
await page.waitForFunction(() => document.activeElement?.getAttribute("aria-selected") === "true" && document.activeElement?.textContent?.includes("Add the feature"));
await graph.locator(".line.add", { hasText: "+one.md" }).waitFor();
assert.equal(await graph.locator("path.boundary").count(), 0, "The full history has no dashed boundary line");
await page.screenshot({ path: path.join(output, "graph-tab.png") });
await page.setViewportSize({ width: 640, height: 720 });
await page.screenshot({ path: path.join(output, "graph-tab-640.png") });
await page.setViewportSize({ width: 1280, height: 800 });
// Off closes the Git tabs and the pane, and removes the button. The feature can start again.
await page.evaluate(() => window.__TEST__.writeSettings("features", { git: false }));
await sourceControl.waitFor({ state: "detached" });
await graph.waitFor({ state: "detached" });
await diff.waitFor({ state: "detached" });
await pane.waitFor({ state: "detached" });
await page.evaluate(() => window.__TEST__.writeSettings("features", { git: true }));
await sourceControl.click();
await pane.getByText("main", { exact: true }).waitFor();
console.log("PASS: Git loads at first need, stages, unstages, commits, shows errors, diffs, and the graph, and Off removes it");
}, { timeout: 60000 });