Files
RuneStone/tests/unit/layout/settings.spec.ts
T
2026-11-03 00:23:07 -07:00

56 lines
3.6 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, showFileTitle: true, keepLineBreaks: false });
}
assert.deepEqual(parseSettings('{"autosaveEnabled":"false","autosaveSeconds":10}'), { autosaveEnabled: true, autosaveSeconds: 10, showInternalFolder: false, tableFormulas: true, showFileTitle: true, keepLineBreaks: false });
});
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, showFileTitle: autosaveSeconds !== 300, keepLineBreaks: autosaveSeconds === 1 };
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, showFileTitle: true, keepLineBreaks: false }));
}
});
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, showFileTitle: true, keepLineBreaks: false });
assert.equal(normalizeSettings({ tableFormulas: false }).tableFormulas, false);
for (const tableFormulas of [null, 0, "false", {}]) assert.equal(normalizeSettings({ tableFormulas }).tableFormulas, true);
assert.equal(normalizeSettings({ showFileTitle: false }).showFileTitle, false);
for (const showFileTitle of [null, 0, "false", {}]) assert.equal(normalizeSettings({ showFileTitle }).showFileTitle, true);
assert.equal(normalizeSettings({ keepLineBreaks: true }).keepLineBreaks, true);
for (const keepLineBreaks of [null, 1, "true", {}]) assert.equal(normalizeSettings({ keepLineBreaks }).keepLineBreaks, false);
});