A3: derive the pinned command and catalog key lists in tests

Each command module keeps its own COMMAND_IDS next to its commands; the
engine listing test checks their union against the registry, so a missing
or extra command still fails without one shared list. The menu and palette
tests derive their expectation from the listing and check grouping and
ordering rules; the catalog's runtime keys are scanned from the sources.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-30 18:38:41 +01:00
parent da58c83158
commit 95fd9b926f
28 changed files with 329 additions and 262 deletions

View file

@ -38,108 +38,85 @@ const usedKeys = sourceFiles(SRC).flatMap((file) =>
})),
);
/** Root of the repository. */
const REPO = join(SRC, "..", "..");
/**
* Lists the Rust files below a directory, leaving out `tests` directories
* and build output.
*
* @param dir - the directory.
*/
function rustFiles(dir: string): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const path = join(dir, entry.name);
if (entry.isDirectory()) {
return ["tests", "target", "node_modules"].includes(entry.name) ? [] : rustFiles(path);
}
return entry.name.endsWith(".rs") ? [path] : [];
});
}
/**
* The shipped part of a Rust file: everything before its unit test module.
*
* @param file - the path.
*/
function shippedRust(file: string): string {
return readFileSync(file, "utf8").replace(/\r\n/g, "\n").split("\n#[cfg(test)]\nmod tests")[0] ?? "";
}
/**
* Turns a settings enum value into its key segment (`lutra-dark` →
* `lutraDark`).
*
* @param value - the value as the engine stores it.
*/
function camel(value: string): string {
return value.replace(/-([a-z0-9])/g, (_, c: string) => c.toUpperCase());
}
/**
* Keys the source never passes to `t` literally because they arrive at
* runtime: theme labels from `theme.list`, the settings' `labelKey`,
* `descriptionKey`, section names and enum value names from
* `settings.list`, the `messageKey` of
* `notification` events (engine and desktop host), the keys the
* notifications store passes through a constant, and the Changes
* window's commit toasts (M4.2), the branch toasts (M4.5), the merge
* toasts (M4.8) and the remote toasts (M4.10), passed as
* `messageKey`.
* runtime, derived from the source so new ones need no list here:
*
* - any `notification.*` literal: the `messageKey` of `notification`
* events (engine and desktop host) and of the toasts the frontend raises;
* - Rust `*_key: "…"` fields: theme labels and the settings' `labelKey`
* and `descriptionKey`;
* - per settings definition, its section name (`settings.section.<s>`) and
* each enum value (`settings.<key>.value.<value>`).
*/
const RUNTIME_KEYS = new Set([
"notification.command.failed",
"notification.lsp.failed",
"notification.lsp.message",
"notification.lsp.renamed",
"notification.project.notLocal",
"notification.project.openFailed",
"notification.project.unsavedChanges",
"notification.recovery.corrupt",
"notification.recovery.unavailable",
"notification.recovery.writeFailed",
"notification.save.failed",
"notification.vcs.branchCreated",
"notification.vcs.checkedOut",
"notification.vcs.checkoutFailed",
"notification.vcs.checkoutNotSaved",
"notification.vcs.historyNoFile",
"notification.vcs.commitNotSaved",
"notification.vcs.committed",
"notification.vcs.mergeAborted",
"notification.vcs.mergeConflicts",
"notification.vcs.mergeFailed",
"notification.vcs.mergeNotSaved",
"notification.vcs.mergeUpToDate",
"notification.vcs.merged",
"notification.vcs.newBranchFailed",
"notification.vcs.cloned",
"notification.vcs.fetchFailed",
"notification.vcs.fetched",
"notification.vcs.pullFailed",
"notification.vcs.pullNotSaved",
"notification.vcs.pulled",
"notification.vcs.pushFailed",
"notification.vcs.pushed",
"notification.vcs.pushedUpstream",
"notification.vcs.remoteCancelled",
"notification.vcs.resolved",
"notification.vcs.stashApplied",
"notification.vcs.stashDropped",
"notification.vcs.stashPopped",
"notification.vcs.stashed",
"notification.settings.corrupt",
"notification.settings.corruptNotMoved",
"notification.settings.unavailable",
"notification.toolchain.downloadDone",
"notification.toolchain.downloadFailed",
"notification.toolchain.downloadStarted",
"notification.toolchain.phpMissing",
"notification.vfs.pumpFailed",
"notification.watch.failed",
"settings.appearance.theme.description",
"settings.appearance.theme.label",
"settings.appearance.theme.value.lutraDark",
"settings.appearance.theme.value.lutraLight",
"settings.appearance.theme.value.system",
"settings.editor.autosave.idleSeconds.description",
"settings.editor.autosave.idleSeconds.label",
"settings.editor.autosave.onBlur.description",
"settings.editor.autosave.onBlur.label",
"settings.editor.autosave.onTabSwitch.description",
"settings.editor.autosave.onTabSwitch.label",
"settings.editor.completion.autoPopup.description",
"settings.editor.completion.autoPopup.label",
"settings.editor.completion.delayMs.description",
"settings.editor.completion.delayMs.label",
"settings.editor.fontSize.description",
"settings.editor.fontSize.label",
"settings.editor.hover.delayMs.description",
"settings.editor.hover.delayMs.label",
"settings.git.executable.description",
"settings.git.executable.label",
"settings.git.pull.mode.description",
"settings.git.pull.mode.label",
"settings.git.pull.mode.value.merge",
"settings.git.pull.mode.value.rebase",
"settings.php.executable.description",
"settings.php.executable.label",
"settings.phpactor.autoDownload.description",
"settings.phpactor.autoDownload.label",
"settings.phpactor.path.description",
"settings.phpactor.path.label",
"settings.section.appearance",
"settings.section.editor",
"settings.section.git",
"settings.section.php",
"settings.ui.language.description",
"settings.ui.language.label",
"settings.ui.language.value.en",
"theme.lutraDark",
"theme.lutraLight",
]);
const RUNTIME_KEYS = (() => {
const keys = new Set<string>();
const add = (text: string, pattern: RegExp): void => {
for (const m of text.matchAll(pattern)) {
keys.add(m[1] ?? "");
}
};
for (const file of sourceFiles(SRC)) {
add(readFileSync(file, "utf8"), /["'`](notification\.[A-Za-z0-9.]+)["'`]/g);
}
for (const file of [...rustFiles(join(REPO, "crates")), ...rustFiles(join(REPO, "apps"))]) {
const text = shippedRust(file);
add(text, /"(notification\.[A-Za-z0-9.]+)"/g);
add(text, /\b[a-z]+_key:\s*"([a-z][A-Za-z0-9.]*)"/g);
for (const def of text.matchAll(/\bDef\s*\{([\s\S]*?)\n\s*\},?\n/g)) {
const body = def[1] ?? "";
const key = /\bkey:\s*"([^"]+)"/.exec(body)?.[1];
const section = /\bsection:\s*"([^"]+)"/.exec(body)?.[1];
if (section !== undefined) {
keys.add(`settings.section.${section}`);
}
const values = /Kind::Enum\(&\[([^\]]*)\]\)/.exec(body)?.[1] ?? "";
for (const value of values.matchAll(/"([^"]+)"/g)) {
keys.add(`settings.${key ?? ""}.value.${camel(value[1] ?? "")}`);
}
}
}
keys.delete("");
return keys;
})();
afterEach(() => {
vi.restoreAllMocks();
@ -206,6 +183,19 @@ describe("en.json", () => {
expect(missing).toEqual([]);
});
it("finds the runtime keys of every source", () => {
for (const key of [
"notification.vcs.committed",
"notification.project.unsavedChanges",
"settings.editor.fontSize.label",
"settings.section.git",
"settings.appearance.theme.value.lutraDark",
"theme.lutraLight",
]) {
expect(RUNTIME_KEYS.has(key), key).toBe(true);
}
});
it("has every runtime key", () => {
expect([...RUNTIME_KEYS].filter((key) => !hasMessage(key))).toEqual([]);
});

View file

@ -255,49 +255,32 @@ describe("AppMenu", () => {
expect(menu.find("[data-test=menu-button]").attributes("aria-expanded")).toBe("true");
const headings = [...document.querySelectorAll("[data-test=menu-group]")].map((g) => g.querySelector(".app-menu__heading")?.textContent.trim());
expect(headings).toEqual(["File", "Edit", "View", "Go", "Git", "Help"]);
expect(itemIds()).toEqual([
const listed = listing() as { id: string; menuPath: string | null }[];
const withPath = listed.filter((c) => c.menuPath !== null);
expect([...itemIds()].sort()).toEqual(withPath.map((c) => c.id).sort());
const groups = [...document.querySelectorAll("[data-test=menu-group]")].map((g) => ({
heading: g.querySelector(".app-menu__heading")?.textContent.trim() ?? "",
ids: [...g.querySelectorAll<HTMLElement>("[data-test=menu-item]")].map((el) => el.dataset.command ?? ""),
labels: [...g.querySelectorAll<HTMLElement>("[data-test=menu-item]")].map((el) => el.querySelector(".app-menu__label")?.textContent.trim() ?? ""),
}));
for (const group of groups) {
for (const id of group.ids) {
expect(withPath.find((c) => c.id === id)?.menuPath?.split("/")[0], id).toBe(group.heading);
}
}
expect(groups.find((g) => g.heading === "File")?.ids).toEqual([
"project.openDialog",
"file.save",
"file.saveAll",
"buffer.close",
"project.close",
"app.quit",
"editor.complete",
"editor.parameterInfo",
"editor.quickDocumentation",
"palette.open",
"view.toggleProjectTree",
"view.toggleStructure",
"find.open",
"terminal.open",
"problems.open",
"settings.open",
"goto.fileOpen",
"goto.classOpen",
"goto.symbolOpen",
"vcs.abortMerge",
"vcs.toggleBlame",
"vcs.branches",
"vcs.openChanges",
"vcs.clone",
"vcs.commit",
"vcs.fetch",
"vcs.showLog",
"vcs.manageRemotes",
"vcs.merge",
"vcs.newBranch",
"vcs.nextChange",
"vcs.previousChange",
"vcs.pull",
"vcs.push",
"vcs.refresh",
"vcs.resolveConflicts",
"vcs.showDiff",
"vcs.showHistory",
"vcs.stash",
"vcs.showStashes",
"app.about",
]);
expect(groups.find((g) => g.heading === "Go")?.ids).toEqual(["goto.fileOpen", "goto.classOpen", "goto.symbolOpen"]);
const git = groups.find((g) => g.heading === "Git")?.labels ?? [];
expect(git.length).toBeGreaterThan(1);
expect(git).toEqual([...git].sort((x, y) => x.localeCompare(y)));
expect(itemIds().at(-1)).toBe("app.about");
const close = document.querySelector("[data-command='buffer.close']");
expect(close?.textContent).toContain("Close Tab");
expect(close?.querySelector("[data-test=menu-shortcut]")?.textContent).toBe("Ctrl+W");

View file

@ -257,49 +257,14 @@ describe("CommandPalette", () => {
await press(PALETTE_KEY);
expect(wrapper.find("[data-test=palette]").exists()).toBe(true);
expect(document.activeElement).toBe(wrapper.find("[data-test=palette-input]").element);
expect(rows(wrapper)).toEqual([
"vcs.abortMerge",
"vcs.toggleBlame",
"vcs.branches",
"vcs.openChanges",
"vcs.checkout",
"vcs.clone",
"project.close",
"palette.open",
"vcs.commit",
"editor.complete",
"vcs.fetch",
"find.open",
"goto.classOpen",
"goto.fileOpen",
"goto.symbolOpen",
"vcs.showLog",
"vcs.manageRemotes",
"vcs.merge",
"vcs.newBranch",
"vcs.nextChange",
"project.openDialog",
"menu.open",
"editor.parameterInfo",
"vcs.previousChange",
"problems.open",
"vcs.pull",
"vcs.push",
"editor.quickDocumentation",
"app.quit",
"vcs.refresh",
"vcs.resolveConflicts",
"editor.reveal",
"settings.open",
"vcs.showCommit",
"vcs.showDiff",
"vcs.showHistory",
"view.toggleProjectTree",
"view.toggleStructure",
"vcs.stash",
"vcs.showStashes",
"terminal.open",
]);
const offered = (listing() as { id: string; menuPath: string | null; defaultShortcut: string | null; ui: boolean }[])
.filter((c) => c.ui || c.menuPath !== null || c.defaultShortcut !== null)
.map((c) => c.id);
expect(offered).toContain("palette.open");
expect(offered).not.toContain("buffer.edit");
expect([...rows(wrapper)].sort()).toEqual([...offered].sort());
const labels = wrapper.findAll("[data-test=palette-row] .palette__label").map((label) => label.text());
expect(labels).toEqual([...labels].sort((x, y) => x.localeCompare(y)));
const quit = wrapper.find("[data-command='app.quit']");
expect(quit.find("[data-test=palette-shortcut]").text()).toBe("Ctrl+Q");
expect(quit.text()).toContain("Quit");