The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
import { invoke, isTauri } from '@tauri-apps/api/core';
import { afterEach, beforeEach, describe, expect, type Mock, test, vi } from 'vitest';
vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn(), isTauri: vi.fn() }));
vi.mock('../../services/coreRpcClient', () => ({
callCoreRpc: vi.fn(),
clearCoreRpcTokenCache: vi.fn(),
}));
describe('tauriCommands/core', () => {
const mockIsTauri = isTauri as Mock;
const mockInvoke = invoke as Mock;
let restartApp: typeof import('./core').restartApp;
let restartCoreProcess: typeof import('./core').restartCoreProcess;
let mockClearCoreRpcTokenCache: Mock;
beforeEach(async () => {
vi.clearAllMocks();
mockIsTauri.mockReturnValue(true);
const actual = await vi.importActual<typeof import('./core')>('./core');
restartApp = actual.restartApp;
restartCoreProcess = actual.restartCoreProcess;
const { clearCoreRpcTokenCache } = await import('../../services/coreRpcClient');
mockClearCoreRpcTokenCache = clearCoreRpcTokenCache as Mock;
});
describe('restartApp', () => {
// window.location.reload is non-configurable on jsdom's default location;
// swap in a mocked location object for the dev-mode tests and restore after.
let originalLocation: Location;
let reloadSpy: Mock;
beforeEach(() => {
originalLocation = window.location;
reloadSpy = vi.fn();
Object.defineProperty(window, 'location', {
value: { ...originalLocation, reload: reloadSpy },
configurable: true,
writable: true,
});
});
afterEach(() => {
Object.defineProperty(window, 'location', {
value: originalLocation,
configurable: true,
writable: true,
});
vi.unstubAllEnvs();
});
test('no-ops when not in Tauri', async () => {
mockIsTauri.mockReturnValue(false);
const debug = vi.spyOn(console, 'debug').mockImplementation(() => {});
await restartApp();
expect(mockInvoke).not.toHaveBeenCalled();
expect(reloadSpy).not.toHaveBeenCalled();
expect(debug).toHaveBeenCalledWith(
expect.stringContaining('restartApp: skipped — not running in Tauri')
);
debug.mockRestore();
});
test('reloads webview in dev mode (#1068 — avoid orphaning vite)', async () => {
// setup.ts seeds DEV=true globally; the binding imported above already
// captured that value, so we just need to invoke the dev-mode branch.
const debug = vi.spyOn(console, 'debug').mockImplementation(() => {});
await restartApp();
expect(reloadSpy).toHaveBeenCalledTimes(1);
expect(mockInvoke).not.toHaveBeenCalled();
expect(debug).toHaveBeenCalledWith(
expect.stringContaining('restartApp: dev mode → window.location.reload()')
);
debug.mockRestore();
});
test('invokes restart_app in production mode (IS_DEV_LIKE=false)', async () => {
// setup.ts globally mocks ../config with IS_DEV_LIKE: true (because
// IS_DEV: true → derived IS_DEV_LIKE). Override with doMock +
// resetModules so a fresh import of ./core sees IS_DEV_LIKE=false and
// runs the production branch (#1068 dev workaround should be inert).
vi.doMock('../config', () => ({ IS_DEV: false, IS_DEV_LIKE: false }));
vi.resetModules();
const prodCore = await import('./core');
await prodCore.restartApp();
expect(mockInvoke).toHaveBeenCalledWith('restart_app');
expect(reloadSpy).not.toHaveBeenCalled();
vi.doUnmock('../config');
});
});
describe('restartCoreProcess', () => {
test('no-ops when not in Tauri (#1922 — no cache clear either)', async () => {
mockIsTauri.mockReturnValue(false);
const debug = vi.spyOn(console, 'debug').mockImplementation(() => {});
await restartCoreProcess();
expect(mockInvoke).not.toHaveBeenCalled();
// No invoke → no cache clear. A defensive clear here would mask
// genuine "core never rotated" cases when the helper is called
// outside Tauri (e.g. test harness).
expect(mockClearCoreRpcTokenCache).not.toHaveBeenCalled();
expect(debug).toHaveBeenCalledWith(
expect.stringContaining('restartCoreProcess: skipped — not running in Tauri')
);
debug.mockRestore();
});
test('invokes restart_core_process then clears the RPC token cache (#1922)', async () => {
// Deferred Promise — proves the cache clear happens AFTER the IPC
// resolves, not merely after invoke() was called. A concurrent
// getCoreRpcToken() racing across an `await` boundary could
// otherwise repopulate from the dead core before the new one mints
// a fresh bearer.
let resolveInvoke!: () => void;
mockInvoke.mockImplementationOnce(
() =>
new Promise<void>(resolve => {
resolveInvoke = resolve;
})
);
const pending = restartCoreProcess();
// Yield the microtask queue so `await invoke(...)` parks. Cache MUST
// still be untouched.
await Promise.resolve();
expect(mockInvoke).toHaveBeenCalledWith('restart_core_process');
expect(mockClearCoreRpcTokenCache).not.toHaveBeenCalled();
resolveInvoke();
await pending;
expect(mockClearCoreRpcTokenCache).toHaveBeenCalledTimes(1);
});
});
describe('app-update wrappers', () => {
let checkAppUpdate: typeof import('./core').checkAppUpdate;
let applyAppUpdate: typeof import('./core').applyAppUpdate;
let downloadAppUpdate: typeof import('./core').downloadAppUpdate;
let installAppUpdate: typeof import('./core').installAppUpdate;
beforeEach(async () => {
const actual = await vi.importActual<typeof import('./core')>('./core');
checkAppUpdate = actual.checkAppUpdate;
applyAppUpdate = actual.applyAppUpdate;
downloadAppUpdate = actual.downloadAppUpdate;
installAppUpdate = actual.installAppUpdate;
});
test('checkAppUpdate returns null when not in Tauri', async () => {
mockIsTauri.mockReturnValue(false);
const out = await checkAppUpdate();
expect(out).toBeNull();
expect(mockInvoke).not.toHaveBeenCalled();
});
test('checkAppUpdate forwards invoke result on the happy path', async () => {
const expected = {
current_version: '0.50.0',
available: true,
available_version: '0.51.0',
body: 'notes',
};
mockInvoke.mockResolvedValueOnce(expected);
const out = await checkAppUpdate();
expect(mockInvoke).toHaveBeenCalledWith('check_app_update');
expect(out).toEqual(expected);
});
test('downloadAppUpdate returns null when not in Tauri', async () => {
mockIsTauri.mockReturnValue(false);
const out = await downloadAppUpdate();
expect(out).toBeNull();
expect(mockInvoke).not.toHaveBeenCalled();
});
test('downloadAppUpdate forwards invoke result on the happy path', async () => {
const expected = { ready: true, version: '0.51.0', body: null };
mockInvoke.mockResolvedValueOnce(expected);
const out = await downloadAppUpdate();
expect(mockInvoke).toHaveBeenCalledWith('download_app_update');
expect(out).toEqual(expected);
});
test('installAppUpdate is a no-op when not in Tauri', async () => {
mockIsTauri.mockReturnValue(false);
await installAppUpdate();
expect(mockInvoke).not.toHaveBeenCalled();
});
test('installAppUpdate invokes install_app_update', async () => {
mockInvoke.mockResolvedValueOnce(undefined);
await installAppUpdate();
expect(mockInvoke).toHaveBeenCalledWith('install_app_update');
});
test('applyAppUpdate is a no-op when not in Tauri', async () => {
mockIsTauri.mockReturnValue(false);
await applyAppUpdate();
expect(mockInvoke).not.toHaveBeenCalled();
});
test('applyAppUpdate invokes apply_app_update', async () => {
mockInvoke.mockResolvedValueOnce(undefined);
await applyAppUpdate();
expect(mockInvoke).toHaveBeenCalledWith('apply_app_update');
});
});
});
describe('tauriCommands/core onboarding flag outside Tauri', () => {
test('reads and writes onboarding_completed through core RPC in the browser build', async () => {
(isTauri as Mock).mockReturnValue(false);
const { callCoreRpc } = await import('../../services/coreRpcClient');
const mockCall = callCoreRpc as Mock;
const { getOnboardingCompleted, setOnboardingCompleted } =
await vi.importActual<typeof import('./core')>('./core');
mockCall.mockResolvedValueOnce(true);
await expect(setOnboardingCompleted(true)).resolves.toBe(true);
expect(mockCall).toHaveBeenCalledWith({
method: 'openhuman.config_set_onboarding_completed',
params: { value: true },
});
mockCall.mockResolvedValueOnce({ result: true });
await expect(getOnboardingCompleted()).resolves.toBe(true);
expect(mockCall).toHaveBeenLastCalledWith({
method: 'openhuman.config_get_onboarding_completed',
});
});
});