Make Git a native feature with an off switch

- Add the native feature runtime in src/lib/features/: the catalog, the switches, the first-start migration, the loader, and the feature sessions.
- Add Settings → Features and the features settings section.
- Add features_legacy. It reads the old plugin packages and changes nothing.
- Move Git from the plugin into src/lib/features/git/, on host internals.
- Remove api.git, the git contribution key, and the SDK Git types.
- Add pnpm check:features. It fails when a feature module loads at start.
- Update the tests, the menu inventory, the docs, and UI-055.
This commit is contained in:
Knotty Wyvern
2026-10-29 15:51:21 -07:00
parent 58f77b2e9f
commit d3218b2501
68 changed files with 2068 additions and 324 deletions
+138
View File
@@ -0,0 +1,138 @@
// The native feature runtime: the catalog, the switches, the migration from old plugin packages, the loader, and the session.
import { test } from "node:test";
import assert from "node:assert/strict";
import { FEATURES, featureEntry, featureOfOwner } from "../../../src/lib/features/catalog.ts";
import { storedSwitches, switchesFrom } from "../../../src/lib/features/state.ts";
import { legacySwitch, migrateFeatures, type LegacyPlugin } from "../../../src/lib/features/migration.ts";
import { featureErrors, featureRequested, loadFeature } from "../../../src/lib/features/loader.ts";
import { FeatureSession } from "../../../src/lib/features/session.ts";
import type { FeatureContext, FeatureInstance, FeatureModule } from "../../../src/lib/features/types.ts";
import { get } from "svelte/store";
test("each feature keeps the ID of its old plugin package as its owner ID", () => {
assert.equal(featureEntry("git").owner, "runestone.git");
assert.equal(featureOfOwner("runestone.git")?.id, "git");
assert.equal(featureOfOwner("runestone.csv-viewer"), undefined);
assert.equal(new Set(FEATURES.map(feature => feature.owner)).size, FEATURES.length);
const git = featureEntry("git");
assert.deepEqual(git.views.map(view => [view.id, view.placement]), [["source-control", "sidebar"], ["diff", "editor"], ["graph", "editor"]]);
assert.equal(git.views[0].title(), "Source control");
assert.deepEqual(git.commands.map(command => [command.id, command.view]), [["graph", "graph"]]);
});
test("switches keep known booleans only, and missing keys take the default", () => {
assert.deepEqual(storedSwitches({ git: false, terminal: true, other: "yes" }), { git: false });
assert.deepEqual(storedSwitches(null), {});
assert.deepEqual(storedSwitches(["git"]), {});
assert.deepEqual(switchesFrom(undefined), { git: true });
assert.deepEqual(switchesFrom({ git: false }), { git: false });
assert.deepEqual(switchesFrom({ git: "false" }), { git: true });
});
test("an installed package gives its state, and without a package the default applies", () => {
const git = featureEntry("git");
const legacy = (installed: boolean, enabled: boolean): LegacyPlugin => ({ id: "runestone.git", installed, enabled, settings: null });
assert.equal(legacySwitch(git, undefined), true);
assert.equal(legacySwitch(git, legacy(false, false)), true);
assert.equal(legacySwitch(git, legacy(true, false)), false);
assert.equal(legacySwitch(git, legacy(true, true)), true);
});
test("the migration asks only for features without a switch, and keeps other keys", async () => {
const writes: unknown[] = [];
const asked: string[][] = [];
const host = (stored: unknown, packages: LegacyPlugin[]) => ({
read: () => stored,
write: async (value: unknown) => { writes.push(value); },
legacy: async (ids: string[]) => { asked.push(ids); return packages; },
});
assert.deepEqual(await migrateFeatures(host({ future: 1 }, [{ id: "runestone.git", installed: true, enabled: false, settings: null }])), ["git"]);
assert.deepEqual(asked, [["runestone.git"]]);
assert.deepEqual(writes, [{ future: 1, git: false }]);
assert.deepEqual(await migrateFeatures(host({ git: true }, [])), []);
assert.equal(asked.length, 1, "A stored switch needs no package read.");
assert.equal(writes.length, 1);
});
test("a failed package read writes nothing, so the next start tries again", async () => {
const writes: unknown[] = [];
const error = console.error;
console.error = () => {};
try {
const result = await migrateFeatures({ read: () => undefined, write: async value => { writes.push(value); }, legacy: async () => { throw new Error("no host"); } });
assert.deepEqual(result, []);
assert.deepEqual(writes, []);
} finally { console.error = error; }
});
test("the loader imports a feature one time, reports a failure, and tries again after it", async () => {
let calls = 0;
const module: FeatureModule = { start: () => ({ mount: () => () => {}, dispose() {} }) };
await assert.rejects(loadFeature("git", async () => { calls++; throw new Error("chunk missing"); }), /chunk missing/);
assert.match(get(featureErrors).git ?? "", /chunk missing/);
assert.equal(featureRequested("git"), false);
const first = loadFeature("git", async () => { calls++; return module; });
const second = loadFeature("git", async () => { calls++; return module; });
assert.equal(await first, module);
assert.equal(await second, module);
assert.equal(calls, 2);
assert.equal(featureRequested("git"), true);
assert.equal(get(featureErrors).git, undefined);
});
function fakeContext(opened: string[] = []): FeatureContext {
return { openView: id => { opened.push(id); }, openFile: async () => {}, onWorkspaceChange: () => () => {} };
}
test("a feature session shows its views and commands without loading the feature", async () => {
let loads = 0;
const opened: string[] = [];
const session = new FeatureSession(featureEntry("git"), fakeContext(opened), async () => { loads++; throw new Error("not loaded"); });
assert.deepEqual([...session.views.keys()], ["source-control", "diff", "graph"]);
assert.equal(session.views.get("source-control")?.navigation, true);
assert.equal(session.views.get("diff")?.launcher, false);
assert.equal(session.views.get("graph")?.title, "Git graph");
assert.equal(session.views.get("diff")?.saveTabState?.(), undefined);
await session.run("graph");
await session.launch("source-control");
assert.deepEqual(opened, ["graph", "source-control"]);
assert.equal(loads, 0, "Commands and navigation do not load the feature.");
await session.stop();
});
test("the first mount starts one instance, and stop disposes it", async () => {
const events: string[] = [];
let starts = 0;
const instance: FeatureInstance = {
mount: (view, element) => { events.push(`mount ${view}`); element.textContent = view; return () => { events.push(`unmount ${view}`); }; },
saveTabState: view => ({ view }),
restoreTabState: (view, state) => { events.push(`restore ${view} ${JSON.stringify(state)}`); },
dispose: () => { events.push("dispose"); },
};
const session = new FeatureSession(featureEntry("git"), fakeContext(), async () => ({ start: () => { starts++; return instance; } }));
const element = { textContent: "", replaceChildren(this: { textContent: string }) { this.textContent = ""; } } as unknown as HTMLElement;
const dispose = await session.mount("diff", element);
await session.views.get("graph")!.restoreTabState!({ selectedId: "a" });
assert.equal(starts, 1);
assert.deepEqual(session.views.get("diff")!.saveTabState!(), { view: "diff" });
await dispose();
assert.equal(element.textContent, "");
await session.stop();
assert.deepEqual(events, ["mount diff", 'restore graph {"selectedId":"a"}', "unmount diff", "dispose"]);
await assert.rejects(session.mount("diff", element), /This feature is off/);
});
test("a failed load leaves no instance, so a later mount tries again", async () => {
let attempt = 0;
const instance: FeatureInstance = { mount: () => () => {}, dispose() {} };
const session = new FeatureSession(featureEntry("git"), fakeContext(), async () => {
if (++attempt === 1) throw new Error("offline");
return { start: () => instance };
});
const element = { replaceChildren() {} } as unknown as HTMLElement;
await assert.rejects(session.mount("graph", element), /offline/);
await session.mount("graph", element);
assert.equal(attempt, 2);
await assert.rejects(session.mount("missing", element), /This view is unavailable/);
await session.stop();
});
+67
View File
@@ -0,0 +1,67 @@
// The Git feature model. These tests come from the Git plugin (`RuneStone_Git/test/model.test.mjs`).
import { test } from "node:test";
import assert from "node:assert/strict";
import { changeLabel, diffLineKind, graphLayout, groups, isConflict, pathsFor } from "../../../src/lib/features/git/model.ts";
import type { GitChange, GitGraphCommit } from "../../../src/lib/git/contracts.ts";
const commit = (id: string, parents: string[]): GitGraphCommit => ({ id, parents, refs: [], summary: id, author: "Test", date: "2026-10-29" });
test("partially staged files occur in both groups, with conflicts only in working files", () => {
const changes: GitChange[] = [
{ path: "both.txt", index: "M", worktree: "M" },
{ path: "new.txt", index: "?", worktree: "?" },
{ path: "conflict.txt", index: "U", worktree: "U" },
{ path: "deleted.txt", index: "D", worktree: " " },
];
assert.deepEqual(groups(changes).staged.map(change => change.path), ["both.txt", "deleted.txt"]);
assert.deepEqual(groups(changes).working.map(change => change.path), ["both.txt", "new.txt", "conflict.txt"]);
assert.equal(isConflict({ path: "a", index: "A", worktree: "A" }), true);
assert.equal(changeLabel(changes[2], false), "Conflict");
assert.equal(changeLabel(changes[1], false), "New");
assert.equal(changeLabel(changes[3], true), "Deleted");
assert.equal(changeLabel({ path: "x", index: "X", worktree: " " }, true), "X");
});
test("rename actions include old and new paths exactly once", () => {
assert.deepEqual(pathsFor([{ path: "new.txt", originalPath: "old.txt", index: "R", worktree: " " }, { path: "old.txt", index: "M", worktree: " " }]), ["old.txt", "new.txt"]);
});
test("diff lines get their kind from the first character", () => {
assert.deepEqual(["+add", "-remove", "+++ b/file", "--- a/file", "@@ -1 +1 @@", " same", ""].map(diffLineKind),
["add", "remove", "context", "context", "range", "context", "context"]);
});
test("graph lanes preserve merge parents, forks, and truncated history", () => {
const layout = graphLayout({ commits: [commit("merge", ["main", "feature"]), commit("feature", ["root"]), commit("main", ["root"]), commit("root", ["older"])] });
assert.deepEqual(layout.nodes.map(node => node.lane), [0, 1, 0, 1]);
assert.equal(layout.edges.length, 5);
assert.deepEqual(layout.edges.filter(edge => edge.from === "merge").map(edge => [edge.to, edge.endRow]), [["main", 2], ["feature", 1]]);
assert.equal(layout.edges.at(-1)!.outside, true);
assert.equal(layout.edges.at(-1)!.endRow, 4);
assert.deepEqual(graphLayout({ commits: [] }).nodes, []);
assert.deepEqual(graphLayout(null).nodes, []);
});
test("graph track colors survive lane changes, joins, and history boundaries", () => {
const history = { commits: [commit("merge", ["main", "feature"]), commit("feature", ["feature-old"]), commit("main", ["root"]), commit("feature-old", ["root"]), commit("root", ["older"])] };
const layout = graphLayout(history);
const node = (id: string) => layout.nodes.find(item => item.id === id)!;
const edge = (from: string, to: string) => layout.edges.find(item => item.from === from && item.to === to)!;
assert.notEqual(node("main").color, node("feature").color);
assert.equal(node("merge").color, node("main").color);
assert.equal(node("feature").color, node("feature-old").color);
assert.equal(edge("merge", "feature").color, node("feature").color);
assert.equal(edge("feature-old", "root").color, node("feature-old").color);
assert.equal(edge("root", "older").color, node("root").color);
assert.deepEqual(graphLayout(history), layout);
const shifted = graphLayout({ commits: [commit("merge", ["main", "feature"]), commit("main", []), commit("feature", ["feature-old"]), commit("feature-old", [])] });
assert.notEqual(shifted.nodes[2].lane, shifted.nodes[3].lane);
assert.equal(shifted.nodes[2].color, shifted.nodes[3].color);
});
test("six concurrent graph tracks use six colors before palette reuse", () => {
const parents = ["a", "b", "c", "d", "e", "f"];
const layout = graphLayout({ commits: [commit("merge", parents), ...parents.map(id => commit(id, []))] });
assert.equal(new Set(layout.nodes.slice(1).map(node => node.color)).size, 6);
assert.equal(new Set(layout.edges.map(edge => edge.color)).size, 6);
});