Files
romm/frontend/src/composables/useUserLocalStorage.test.ts
Georges-Antoine Assi db537728f1 feat(v2): scope stored preferences to the signed-in user (#5098)
* feat(v2): scope stored preferences to the signed-in user

Preferences in localStorage were shared by everyone using the same
browser, so one person's gallery layout, scan providers, chosen cores
and so on carried over to the next account. useUserLocalStorage stores
them under `user:<id>:<key>` while someone is signed in, and under the
bare key while signed out. The first user to sign in after this change
claims any unscoped value, so existing preferences carry over once.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(v2): address Greptile review on user-scoped storage

- Scope the core, BIOS and disc keys the shared EmulatorJS player writes
  (and v1's matching reads), so a game start no longer leaves bare keys
  for the next user to claim.
- Build the test user with userFixture.
- Trim the helper's header comment.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(v2): store the jukebox session under the user's storage key

The resume session now goes through userStorage like the other stored
preferences, so each user gets their own key instead of one shared key
carrying a userId to check on restore.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore(v2): lint against storage that isn't scoped to the user

Adds romm/no-unscoped-local-storage, which rejects raw localStorage and
vueuse's useLocalStorage/useStorage outside frozen v1, so new stored
preferences go through useUserLocalStorage/userStorage. The locale read
at module load stays unscoped with an inline disable.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 09:47:02 -04:00

99 lines
2.7 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { effectScope, nextTick, type EffectScope } from "vue";
import {
setStorageUser,
userStorage,
userStorageKey,
useUserLocalStorage,
} from "@/composables/useUserLocalStorage";
let scope: EffectScope;
beforeEach(() => {
localStorage.clear();
setStorageUser(null);
scope = effectScope();
});
afterEach(() => {
scope.stop();
setStorageUser(null);
localStorage.clear();
});
describe("userStorageKey", () => {
it("keeps the bare key while signed out", () => {
expect(userStorageKey("settings.theme")).toBe("settings.theme");
});
it("namespaces the key by user id once signed in", () => {
setStorageUser(7);
expect(userStorageKey("settings.theme")).toBe("user:7:settings.theme");
});
it("moves an unscoped value to the first user who reads it", () => {
localStorage.setItem("settings.theme", "light");
setStorageUser(7);
expect(userStorage.getItem("settings.theme")).toBe("light");
expect(localStorage.getItem("settings.theme")).toBeNull();
setStorageUser(8);
expect(userStorage.getItem("settings.theme")).toBeNull();
});
it("keeps the user's own value over an unscoped one", () => {
localStorage.setItem("user:7:settings.theme", "dark");
localStorage.setItem("settings.theme", "light");
setStorageUser(7);
expect(userStorage.getItem("settings.theme")).toBe("dark");
expect(localStorage.getItem("settings.theme")).toBeNull();
});
});
describe("userStorage", () => {
it("writes and removes under the user's key", () => {
setStorageUser(7);
userStorage.setItem("player:1:core", "snes9x");
expect(localStorage.getItem("user:7:player:1:core")).toBe("snes9x");
userStorage.removeItem("player:1:core");
expect(localStorage.getItem("user:7:player:1:core")).toBeNull();
});
});
describe("useUserLocalStorage", () => {
it("keeps each user's value apart on a shared browser", async () => {
const layout = scope.run(() =>
useUserLocalStorage("v2.gallery.layout", "grid"),
)!;
setStorageUser(1);
await nextTick();
layout.value = "list";
await nextTick();
setStorageUser(2);
await nextTick();
expect(layout.value).toBe("grid");
setStorageUser(1);
await nextTick();
expect(layout.value).toBe("list");
expect(localStorage.getItem("user:1:v2.gallery.layout")).toBe("list");
});
it("falls back to the default once signed out", async () => {
const muted = scope.run(() =>
useUserLocalStorage("soundtrack.muted", false),
)!;
setStorageUser(1);
await nextTick();
muted.value = true;
await nextTick();
setStorageUser(null);
await nextTick();
expect(muted.value).toBe(false);
});
});