fix: repair persistence - safe-persist getItem now returns parsed objects, fix resetGame localStorage keys
Build and Publish Mana Loop Docker Image / build-and-publish (push) Successful in 1m22s

- safe-persist.ts: getItem now calls JSON.parse(str) so Zustand receives {state, version} envelope
- gameActions.ts: fix 5 wrong localStorage keys in createResetGame (mana→mana-storage, etc.)
- Add persistence.test.ts with 12 tests covering round-trip, key verification, and reset
- All 918 tests pass with zero regressions
This commit is contained in:
2026-05-27 19:14:01 +02:00
parent 5f8a860a3c
commit 7279050101
6 changed files with 236 additions and 13 deletions
+11 -2
View File
@@ -1,6 +1,11 @@
// ─── Safe Persist Storage ─────────────────────────────────────────────────────
// Wraps localStorage with error handling for Zustand persist middleware.
// Handles: quota exceeded, corrupted JSON, and unexpected read/write failures.
//
// Zustand persist storage contract:
// getItem(name) → returns parsed object { state, version } | null
// setItem(name, value) → receives object { state, version }, must serialize
// removeItem(name) → removes the key
import type { StateStorage } from 'zustand/middleware';
@@ -10,14 +15,16 @@ import type { StateStorage } from 'zustand/middleware';
* - Quota exceeded → logs warning, skips write
* - Other errors → logs warning, graceful fallback
*/
export function createSafeStorage(): any {
export function createSafeStorage(): StateStorage {
const storage: StateStorage = {
getItem: (name: string): string | null | Promise<string | null> => {
try {
const str = localStorage.getItem(name);
if (str === null) return null;
return str;
// Zustand expects the parsed storage envelope { state, version }
return JSON.parse(str);
} catch (error) {
// Corrupted JSON or other read error — clear the bad data
try {
localStorage.removeItem(name);
} catch {
@@ -28,6 +35,7 @@ export function createSafeStorage(): any {
},
setItem: (name: string, value: unknown): void => {
try {
// Zustand passes the storage envelope { state, version } as a JS object
localStorage.setItem(name, JSON.stringify(value));
} catch (error) {
console.error('[safe-persist] Failed to persist:', name, error);
@@ -37,6 +45,7 @@ export function createSafeStorage(): any {
try {
localStorage.removeItem(name);
} catch (error) {
// ignore
}
},
};