Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
import { describe, it, expect, afterEach, vi } from 'vitest';
import {
safeReadStorage, safeReadJsonStorage, safeWriteStorage, safeWriteJsonStorage, safeRemoveStorage,
safeReadJsonSession, safeWriteJsonSession, safeRemoveSession,
safeReadSession, safeWriteSession,
} from './safeStorage.js';
// Blocked storage is simulated with `vi.stubGlobal`, not `vi.spyOn`: assigning
// a method onto jsdom's Storage proxy is swallowed as a stored key, so a spy
// never installs and the test passes on a storage that never threw.
const throwingStorage = () => {
const boom = () => { throw new DOMException('The operation is insecure.', 'SecurityError'); };
return { getItem: boom, setItem: boom, removeItem: boom, clear: () => {} };
};
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
window.localStorage.clear();
window.sessionStorage.clear();
});
describe('safeStorage', () => {
it('round-trips through a healthy localStorage', () => {
expect(safeWriteStorage('k', 'v')).toBeUndefined();
expect(safeReadStorage('k')).toBe('v');
safeRemoveStorage('k');
expect(safeReadStorage('k')).toBeNull();
});
it('returns null for a missing key (distinguishes absent from empty)', () => {
expect(safeReadStorage('missing')).toBeNull();
window.localStorage.setItem('empty', '');
expect(safeReadStorage('empty')).toBe('');
});
it('returns null instead of throwing when getItem throws', () => {
window.localStorage.setItem('k', 'v');
vi.stubGlobal('localStorage', throwingStorage());
// Asserting on a key that IS set: a null from a missing key would pass on a
// storage that never threw at all.
expect(safeReadStorage('k')).toBeNull();
expect(safeReadJsonStorage('k', 'fallback')).toBe('fallback');
});
it('reads JSON and returns the supplied fallback for missing or corrupt values', () => {
window.localStorage.setItem('valid', JSON.stringify(['/brain/inbox']));
window.localStorage.setItem('corrupt', '{not-json');
expect(safeReadJsonStorage('valid', [])).toEqual(['/brain/inbox']);
expect(safeReadJsonStorage('missing', [])).toEqual([]);
expect(safeReadJsonStorage('corrupt', [])).toEqual([]);
});
it('writes JSON and swallows a circular-value serialization throw', () => {
safeWriteJsonStorage('obj', { a: 1 });
expect(safeReadJsonStorage('obj', null)).toEqual({ a: 1 });
const circular = {};
circular.self = circular;
expect(() => safeWriteJsonStorage('circular', circular)).not.toThrow();
expect(safeReadStorage('circular')).toBeNull();
});
it('swallows setItem / removeItem throws', () => {
vi.stubGlobal('localStorage', throwingStorage());
expect(() => safeWriteStorage('k', 'v')).not.toThrow();
expect(() => safeWriteJsonStorage('k', { a: 1 })).not.toThrow();
expect(() => safeRemoveStorage('k')).not.toThrow();
});
it('round-trips session JSON without touching localStorage', () => {
safeWriteJsonSession('draft', { a: 1 });
expect(safeReadJsonSession('draft')).toEqual({ a: 1 });
// Session scope is the point: the same key must not have been persisted
// where it would outlive the tab.
expect(safeReadStorage('draft')).toBeNull();
safeRemoveSession('draft');
expect(safeReadJsonSession('draft', 'fallback')).toBe('fallback');
});
it('falls back on corrupt session JSON', () => {
window.sessionStorage.setItem('corrupt', '{not-json');
expect(safeReadJsonSession('corrupt', 'fallback')).toBe('fallback');
expect(safeReadJsonSession('missing', 'fallback')).toBe('fallback');
});
it('stores a session string verbatim, with no JSON quoting', () => {
safeWriteSession('flag', 'build-abc');
// The raw bytes are the contract: the stale-chunk flag is compared against
// values already written by tabs open across an upgrade, so a JSON-quoted
// `"build-abc"` would silently never match and the anti-loop guard would
// stop working the day the helper was adopted.
expect(window.sessionStorage.getItem('flag')).toBe('build-abc');
expect(safeReadSession('flag')).toBe('build-abc');
expect(safeReadSession('missing')).toBeNull();
});
it('swallows a session storage that throws on every access', () => {
// Blocked storage (Safari private mode, a sandboxed iframe) throws from the
// accessor itself, so the whole call has to be inside the guard.
vi.stubGlobal('sessionStorage', throwingStorage());
expect(() => safeWriteJsonSession('k', { a: 1 })).not.toThrow();
expect(safeReadJsonSession('k', 'fallback')).toBe('fallback');
expect(() => safeRemoveSession('k')).not.toThrow();
expect(() => safeWriteSession('k', 'v')).not.toThrow();
expect(safeReadSession('k')).toBeNull();
});
});