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([]);
});