Files
ihasmail/web/src/lib/__tests__/settingsSync.test.ts
T
jcoffey-dev b7e0fc0c7d Keep settings with the account, not the browser
Every setting lived in localStorage, so none of them travelled between
devices. The sharpest edge is the default identity: with none set the
address that sorts first wins, so mail goes out from an address the
recipient may not recognise -- and someone who sets it at work finds it
unset at home, with nothing to say so. Reported in #54.

They now live in a settings.json in the account's own JMAP Files, beside
the signature images already kept there. ihasmail itself stays stateless:
no volume, no database, nothing to back up separately, and the settings
are covered by whatever backs up the mail store.

localStorage stays as a cache rather than the source of truth, so the
first frame is painted from it and the file corrects it a moment later.
A private window has no cache and shows defaults for that one frame,
which is the trade for not gating the whole app on a network round trip.

Not everything should follow the account. A list-pane width picked on a
27" monitor is wrong on a laptop, and the notification toggles track a
permission the browser grants per-device, so claiming it elsewhere would
be a lie. Those stay local, written as a list of exceptions so that a
setting added later syncs by default -- which is what adding one almost
always means.

Writes are coalesced: update() fires on every frame of a splitter drag,
so a change waits 3s and the newest value wins. A tab going away flushes
first, as does signing out, so a setting changed seconds before either
is not lost.

The ihasmail folder is now hidden from the Files view, contents and all.
Hiding the folder alone would have been worse than showing it: the tree
attaches a node whose parent is missing to the root, so the signature
images would have spilled into the top level as if the user had put them
there. Those images have been visible since signatures shipped.

Requires 0.16 -- FileNode/query cannot see directories before that. On
0.15 settings stay local exactly as they were.

Verified against the mock end to end: folder create, blob upload, node
create, read back, update, re-read. Not yet exercised against the live
0.16.19.
2026-08-26 08:19:57 -07:00

84 lines
3.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { DEFAULT_SETTINGS, DEVICE_KEYS, acceptRemote, syncedPart, type Settings } from "@/store/settings";
import { isAppFolder } from "../appFolder";
/**
* Settings used to live only in localStorage, so nothing followed the user
* between devices — issue #54, whose sharpest case is the default identity:
* with none set, the address that sorts first wins, so mail goes out from an
* address the recipient may not recognise.
*
* The split is written as a list of exceptions, which means the interesting
* test is not "does this key sync" but "does a key added later sync without
* anyone remembering to add it".
*/
describe("which settings follow the account", () => {
it("syncs everything that is not explicitly device-local", () => {
const synced = syncedPart(DEFAULT_SETTINGS);
const expected = (Object.keys(DEFAULT_SETTINGS) as Array<keyof Settings>).filter((k) => !DEVICE_KEYS.has(k));
expect(Object.keys(synced).sort()).toEqual(expected.sort());
});
it("keeps this screen's and this browser's settings out of the file", () => {
const synced = syncedPart(DEFAULT_SETTINGS);
// A pane width picked on a monitor is wrong on a laptop, and the
// notification toggles track a per-browser permission grant.
for (const key of ["listPaneWidth", "listPaneHeight", "density", "fontSize", "sidebarCollapsed", "desktopNotifications", "notificationSound"]) {
expect(synced, key).not.toHaveProperty(key);
}
});
it("syncs the default identity, which is what #54 was actually about", () => {
const settings: Settings = { ...DEFAULT_SETTINGS, defaultIdentityByAccount: { a1: "i7" } };
expect(syncedPart(settings).defaultIdentityByAccount).toEqual({ a1: "i7" });
});
it("syncs theme and reading pane", () => {
const synced = syncedPart({ ...DEFAULT_SETTINGS, theme: "dark", readingPane: "bottom" });
expect(synced.theme).toBe("dark");
expect(synced.readingPane).toBe("bottom");
});
});
describe("applying a settings file", () => {
it("takes known, non-device keys", () => {
const applied = acceptRemote({ theme: "dark", weekStart: 0, locale: "de-DE" });
expect(applied).toEqual({ theme: "dark", weekStart: 0, locale: "de-DE" });
});
it("ignores keys it has never heard of", () => {
// A newer ihasmail's settings, or a hand-edited file.
expect(acceptRemote({ theme: "dark", somethingNewer: 42 })).toEqual({ theme: "dark" });
});
it("refuses device keys even when the file carries them", () => {
// An earlier build wrote the whole settings object up; that file must not
// now drag one machine's pane width onto every other one.
expect(acceptRemote({ theme: "dark", listPaneWidth: 900, fontSize: "large" })).toEqual({ theme: "dark" });
});
it("does not invent keys from an empty file", () => {
expect(acceptRemote({})).toEqual({});
});
it("keeps a false or zero value, which is not the same as absent", () => {
const applied = acceptRemote({ conversationMode: false, markReadDelay: 0 });
expect(applied).toEqual({ conversationMode: false, markReadDelay: 0 });
});
});
describe("the client's own folder", () => {
it("is the top-level ihasmail directory", () => {
expect(isAppFolder({ name: "ihasmail", parentId: null, nodeType: "directory" })).toBe(true);
});
it("is not a folder of that name someone made inside another one", () => {
expect(isAppFolder({ name: "ihasmail", parentId: "n1", nodeType: "directory" })).toBe(false);
});
it("is not a file that happens to be called that", () => {
expect(isAppFolder({ name: "ihasmail", parentId: null, nodeType: "file" })).toBe(false);
});
});