Files
RuneStone/tests/unit/layout/settings.spec.ts
T
Knotty Wyvern 5e1533c371 Make Table Formulas a built-in feature
The formula engine, the rendered results, and the formula help now
come from src/lib/formulas/. The editor and the renderer call this
module directly. The tableFormulas contribution type is removed.

- Add the tableFormulas general setting (default on) and a switch in
  Settings > General. When it is off, formulas show as source text.
- Do not load an installed runestone.table-formulas package. Settings
  > Plug-ins marks it as included with RuneStone and tells the user
  that it can be removed. The user's files stay in place.
- Translate the formula help and the error descriptions into French,
  Japanese, and Simplified Chinese.
- Port the plugin unit tests. Test the setting and use a test-only
  contribution for the generic plugin lifecycle tests.
- Smoke tests load only Terminal. The formula and lifecycle tests use
  the built-in feature and its setting.
- Remove scripts/buildPlugins.mjs, the build:plugins script, and the
  plugin-packages requirement of the smoke runner.
- Remove RuneStone_Table_Formulas from the menu audit scope. It had no
  menu surfaces.
- Update the README, the user guides, and the developer references.
2026-10-28 01:05:43 -07:00

52 lines
3.0 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { DEFAULT_SETTINGS, SETTINGS_KEY, normalizeSettings, parseSettings, readSettings, saveSettings } from "../../../src/lib/workspace/settings.ts";
test("missing or damaged preferences keep safe defaults", () => {
for (const raw of [null, "{broken", "null", "[]", "42", '"text"']) {
assert.deepEqual(parseSettings(raw), DEFAULT_SETTINGS);
}
for (const autosaveSeconds of [0, -1, 301, 1.5, "10", null]) {
assert.deepEqual(parseSettings(JSON.stringify({ autosaveEnabled: false, autosaveSeconds, showInternalFolder: false })), { autosaveEnabled: false, autosaveSeconds: 5, showInternalFolder: false, tableFormulas: true });
}
assert.deepEqual(parseSettings('{"autosaveEnabled":"false","autosaveSeconds":10}'), { autosaveEnabled: true, autosaveSeconds: 10, showInternalFolder: false, tableFormulas: true });
});
function withStorage(storage: Pick<Storage, "getItem" | "setItem">, body: () => void) {
const prior = globalThis.localStorage;
try { globalThis.localStorage = storage as Storage; body(); }
finally { globalThis.localStorage = prior; }
}
test("preferences persist and load at both interval limits", () => {
const store = new Map<string, string>();
withStorage({ getItem: (key: string) => store.get(key) ?? null, setItem: (key: string, value: string) => { store.set(key, value); } }, () => {
for (const autosaveSeconds of [1, 23, 300]) {
const settings = { autosaveEnabled: false, autosaveSeconds, showInternalFolder: false, tableFormulas: autosaveSeconds !== 23 };
void saveSettings(settings);
assert.deepEqual(parseSettings(store.get(SETTINGS_KEY)!), settings);
assert.deepEqual(readSettings(), settings);
}
});
});
test("invalid changes never reach storage and storage failures reach the caller", () => {
let writes = 0;
withStorage({ getItem: () => null, setItem: () => { writes++; } }, () => {
for (const autosaveSeconds of [NaN, Infinity, 0, 301, 2.5]) {
assert.throws(() => saveSettings({ autosaveEnabled: true, autosaveSeconds, showInternalFolder: false, tableFormulas: true }));
}
});
assert.equal(writes, 0);
withStorage({ getItem: () => null, setItem: () => { throw new Error("Storage unavailable"); } }, () => {
assert.throws(() => saveSettings({ ...DEFAULT_SETTINGS }), /Storage unavailable/);
});
});
test("stored values of any type normalize to safe settings", () => {
for (const value of [undefined, null, 42, "text", [], { autosaveSeconds: 0 }]) assert.deepEqual(normalizeSettings(value), DEFAULT_SETTINGS);
assert.deepEqual(normalizeSettings({ autosaveEnabled: false, autosaveSeconds: 9, showInternalFolder: true }), { autosaveEnabled: false, autosaveSeconds: 9, showInternalFolder: true, tableFormulas: true });
assert.equal(normalizeSettings({ tableFormulas: false }).tableFormulas, false);
for (const tableFormulas of [null, 0, "false", {}]) assert.equal(normalizeSettings({ tableFormulas }).tableFormulas, true);
});