Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

4.0 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { clearAllAppData } from '../clearAllAppData';

const { mockPurge, mockReset, mockRestart } = vi.hoisted(() => ({
  mockPurge: vi.fn().mockResolvedValue(undefined),
  mockReset: vi.fn().mockResolvedValue(undefined),
  mockRestart: vi.fn().mockResolvedValue(undefined),
}));

vi.mock('../../store', () => ({ persistor: { purge: mockPurge } }));

vi.mock('../tauriCommands', () => ({
  resetOpenHumanDataAndRestartCore: mockReset,
  restartApp: mockRestart,
}));

describe('clearAllAppData', () => {
  beforeEach(() => {
    mockPurge.mockReset().mockResolvedValue(undefined);
    mockReset.mockReset().mockResolvedValue(undefined);
    mockRestart.mockReset().mockResolvedValue(undefined);
    window.localStorage.clear();
    window.sessionStorage.clear();
  });

  it('runs the full wipe sequence and restarts the app', async () => {
    const clearSession = vi.fn().mockResolvedValue(undefined);

    // Seed active-user key + user-1-scoped data + another user's data
    window.localStorage.setItem('OPENHUMAN_ACTIVE_USER_ID', 'user-1');
    window.localStorage.setItem('user-1:persist:accounts', 'a');
    window.localStorage.setItem('user-1:persist:coreMode', 'b');
    window.localStorage.setItem('user-2:persist:accounts', 'other');
    window.sessionStorage.setItem('session-persisted', '1');

    await clearAllAppData({ clearSession, userId: 'user-1' });

    expect(clearSession).toHaveBeenCalledTimes(1);
    expect(mockReset).toHaveBeenCalledTimes(1);
    // #4950: the reset must receive the signed-in user id so it deletes the
    // right `users/<id>` slice even though clearSession already removed the
    // active-user marker. Passing no id here is the bug that left data behind.
    expect(mockReset).toHaveBeenCalledWith('user-1');
    expect(mockPurge).toHaveBeenCalledTimes(1);
    // user-1's own keys are gone
    expect(window.localStorage.getItem('OPENHUMAN_ACTIVE_USER_ID')).toBeNull();
    expect(window.localStorage.getItem('user-1:persist:accounts')).toBeNull();
    expect(window.localStorage.getItem('user-1:persist:coreMode')).toBeNull();
    // user-2's keys are untouched (#983: other accounts must not lose data)
    expect(window.localStorage.getItem('user-2:persist:accounts')).toBe('other');
    expect(window.sessionStorage.getItem('session-persisted')).toBeNull();
    expect(mockRestart).toHaveBeenCalledTimes(1);
  });

  it('falls back to localStorage.clear() when no userId is provided', async () => {
    window.localStorage.setItem('user-1:persist:accounts', 'a');
    window.localStorage.setItem('user-2:persist:accounts', 'b');
    window.sessionStorage.setItem('session-persisted', '1');

    await clearAllAppData();

    // No clearSession was provided — call sequence still completes.
    expect(mockReset).toHaveBeenCalledTimes(1);
    // Pre-login recovery (Welcome screen) has no user id: the reset falls back
    // to marker-based resolution, so `null` is forwarded.
    expect(mockReset).toHaveBeenCalledWith(null);
    expect(mockRestart).toHaveBeenCalledTimes(1);
    // Without a userId we have no way to scope, so everything is cleared
    expect(window.localStorage.getItem('user-1:persist:accounts')).toBeNull();
    expect(window.localStorage.getItem('user-2:persist:accounts')).toBeNull();
  });

  it('continues if clearSession fails (best-effort)', async () => {
    const clearSession = vi.fn().mockRejectedValue(new Error('logout boom'));

    await expect(clearAllAppData({ clearSession })).resolves.toBeUndefined();

    expect(clearSession).toHaveBeenCalledTimes(1);
    expect(mockReset).toHaveBeenCalledTimes(1);
    expect(mockRestart).toHaveBeenCalledTimes(1);
  });

  it('throws when resetOpenHumanDataAndRestartCore fails (unrecoverable)', async () => {
    mockReset.mockRejectedValueOnce(new Error('core reset boom'));

    await expect(clearAllAppData()).rejects.toThrow('core reset boom');

    expect(mockPurge).not.toHaveBeenCalled();
    expect(mockRestart).not.toHaveBeenCalled();
  });
});