Files
tolaria/src/mock-tauri/index.ts
Luca Rossi 6e038a5310 refactor: split mock-tauri.ts into focused modules (8.81 → 10.0) (#65)
* refactor: split mock-tauri.ts into focused modules

The monolithic mock-tauri.ts (1927 lines, Code Health 8.8) is now split
into 5 cohesive modules under src/mock-tauri/:

- index.ts (barrel, isTauri, simplified mockInvoke) — 10.0
- mock-content.ts (markdown test data) — 10.0
- mock-entries.ts (VaultEntry[] + bulk generator) — 10.0
- mock-handlers.ts (command handlers + state) — 9.68
- vault-api.ts (vault API detection + proxy) — 10.0

Key improvements:
- mockInvoke complexity reduced from cc=17 to trivial (vault API
  extracted to tryVaultApi, conditional routing via data-driven map)
- save_settings complexity reduced via trimOrNull helper
- All 726 tests pass unchanged, coverage remains above 70%
- Public API surface is identical (no import changes needed)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style: cargo fmt

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 13:12:23 +00:00

33 lines
1.0 KiB
TypeScript

/**
* Mock Tauri invoke for browser testing.
* When running outside Tauri (e.g. in Chrome via localhost:5173),
* this provides realistic test data so the UI can be verified visually.
*/
import { MOCK_CONTENT } from './mock-content'
import { mockHandlers, addMockEntry, updateMockContent } from './mock-handlers'
import { tryVaultApi } from './vault-api'
export { addMockEntry, updateMockContent }
export function isTauri(): boolean {
return typeof window !== 'undefined' && ('__TAURI__' in window || '__TAURI_INTERNALS__' in window)
}
// Initialize window.__mockContent for browser testing
if (typeof window !== 'undefined') {
window.__mockContent = MOCK_CONTENT
}
export async function mockInvoke<T>(cmd: string, args?: Record<string, unknown>): Promise<T> {
const vaultResult = await tryVaultApi<T>(cmd, args)
if (vaultResult !== undefined) return vaultResult
const handler = mockHandlers[cmd]
if (handler) {
await new Promise((r) => setTimeout(r, 100))
return handler(args) as T
}
throw new Error(`No mock handler for command: ${cmd}`)
}