The host publishes picker, command-line and pump errors as notification events (Engine::notify, same id sequence) instead of stderr, keeps notifications and watchFailed published before engine_ready and replays them after the state replay, so startup warnings (corrupt settings, journal entries) reach the UI. ipc.ts announces engine_ready once all subscriptions started together are registered. Frontend: notifications store and NotificationStack (bottom right, info auto-dismissed after 5 s, warnings/errors stay, close button and Escape, at most 5 visible with "+n more", role status/alert); watchFailed maps to a warning toast; failed commands without their own UI raise an error toast. Catalog keys notification.settings.*, project.*, vfs.*, watch.*, command.* plus the runtime allow-list. Docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
4.3 KiB
TypeScript
134 lines
4.3 KiB
TypeScript
/**
|
|
* Tests of the message catalog: the `t` helper (lookup, interpolation,
|
|
* missing keys) and a scan of the frontend source that every literal key
|
|
* passed to `t` exists in `en.json`.
|
|
*/
|
|
import { readdirSync, readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import en from "../../src/i18n/en.json";
|
|
import { hasMessage, t } from "../../src/i18n";
|
|
|
|
/** Root of the frontend source tree. */
|
|
const SRC = join(import.meta.dirname, "..", "..", "src");
|
|
|
|
/**
|
|
* Lists the `.ts` and `.vue` files below a directory.
|
|
*
|
|
* @param dir - the directory.
|
|
*/
|
|
function sourceFiles(dir: string): string[] {
|
|
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
const path = join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
return sourceFiles(path);
|
|
}
|
|
return /\.(ts|vue)$/.test(entry.name) ? [path] : [];
|
|
});
|
|
}
|
|
|
|
/** Every call of `t` with a literal first argument, by file. */
|
|
const usedKeys = sourceFiles(SRC).flatMap((file) =>
|
|
[...readFileSync(file, "utf8").matchAll(/\bt\(\s*(["'`])([^"'`]*)\1/g)].map((m) => ({
|
|
file,
|
|
key: m[2] ?? "",
|
|
})),
|
|
);
|
|
|
|
/**
|
|
* Keys the source never passes to `t` literally because they arrive at
|
|
* runtime: theme labels from `theme.list`, the `messageKey` of
|
|
* `notification` events (engine and desktop host), and the keys the
|
|
* notifications store passes through a constant.
|
|
*/
|
|
const RUNTIME_KEYS = new Set([
|
|
"notification.command.failed",
|
|
"notification.project.notLocal",
|
|
"notification.project.openFailed",
|
|
"notification.project.unsavedChanges",
|
|
"notification.recovery.corrupt",
|
|
"notification.recovery.unavailable",
|
|
"notification.recovery.writeFailed",
|
|
"notification.settings.corrupt",
|
|
"notification.settings.corruptNotMoved",
|
|
"notification.settings.unavailable",
|
|
"notification.vfs.pumpFailed",
|
|
"notification.watch.failed",
|
|
"theme.lutraDark",
|
|
"theme.lutraLight",
|
|
]);
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("t", () => {
|
|
it("returns the message of a key", () => {
|
|
expect(t("common.cancel")).toBe("Cancel");
|
|
});
|
|
|
|
it("fills in placeholders", () => {
|
|
expect(t("editor.tabs.close", { name: "index.php" })).toBe("Close index.php");
|
|
expect(t("app.statusBar.version", { name: "Lutra", version: "0.1.0" })).toBe("Lutra 0.1.0");
|
|
});
|
|
|
|
it("formats numbers", () => {
|
|
expect(t("common.toolWindow.hide", { title: 3 })).toBe("Hide 3");
|
|
});
|
|
|
|
it("keeps a placeholder without a value", () => {
|
|
expect(t("editor.tabs.close")).toBe("Close {name}");
|
|
expect(t("editor.tabs.close", { other: "x" })).toBe("Close {name}");
|
|
});
|
|
|
|
it("does not resolve placeholders from the prototype", () => {
|
|
expect(t("editor.tabs.close", {})).toBe("Close {name}");
|
|
expect(t("common.error.unexpectedResult", { command: "{name}" })).toBe(
|
|
"unexpected result of {name}",
|
|
);
|
|
});
|
|
|
|
it("returns the key and warns on a missing key", () => {
|
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
expect(t("no.such.key")).toBe("no.such.key");
|
|
expect(warn).toHaveBeenCalledWith(expect.stringContaining("no.such.key"));
|
|
});
|
|
|
|
it("does not treat prototype names as keys", () => {
|
|
vi.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
expect(hasMessage("toString")).toBe(false);
|
|
expect(t("toString")).toBe("toString");
|
|
});
|
|
});
|
|
|
|
describe("en.json", () => {
|
|
it("uses flat dotted keys of known areas with string values", () => {
|
|
for (const [key, value] of Object.entries(en)) {
|
|
expect(key).toMatch(
|
|
/^(app|project|editor|common|theme|recovery|notification)\.[A-Za-z0-9]+(\.[A-Za-z0-9]+)*$/,
|
|
);
|
|
expect(typeof value).toBe("string");
|
|
expect(value.trim()).not.toBe("");
|
|
}
|
|
});
|
|
|
|
it("is sorted by key", () => {
|
|
const keys = Object.keys(en);
|
|
expect(keys).toEqual([...keys].sort());
|
|
});
|
|
|
|
it("contains every key the source passes to t", () => {
|
|
expect(usedKeys.length).toBeGreaterThan(0);
|
|
const missing = usedKeys.filter(({ key }) => !hasMessage(key));
|
|
expect(missing).toEqual([]);
|
|
});
|
|
|
|
it("has every runtime key", () => {
|
|
expect([...RUNTIME_KEYS].filter((key) => !hasMessage(key))).toEqual([]);
|
|
});
|
|
|
|
it("has no unused keys", () => {
|
|
const used = new Set([...usedKeys.map(({ key }) => key), ...RUNTIME_KEYS]);
|
|
expect(Object.keys(en).filter((key) => !used.has(key))).toEqual([]);
|
|
});
|
|
});
|