|
| 1 | +/** |
| 2 | + * Round-trip persistence tests for the shortcuts store. |
| 3 | + * |
| 4 | + * These guard the two disk-state invariants documented in `CLAUDE.md` |
| 5 | + * § "Empty array vs missing key": |
| 6 | + * |
| 7 | + * - A persisted empty array (`"shortcut:<id>": []`) means "user removed all |
| 8 | + * shortcuts, don't use defaults" and must survive a reload. |
| 9 | + * - A `shortcut:<id>` key with no in-memory entry is stale and must be deleted |
| 10 | + * on save, so a removed/reset customization can't resurrect at next load. |
| 11 | + * |
| 12 | + * "Reload" is simulated with `vi.resetModules()`: the store's in-memory map is |
| 13 | + * module-scoped, so re-importing the module after a reset mimics a fresh webview |
| 14 | + * re-reading `shortcuts.json` from disk. The mock `load()` is backed by a single |
| 15 | + * shared `Map` (`disk`) that persists across resets, standing in for the file on |
| 16 | + * disk. |
| 17 | + */ |
| 18 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 19 | +// Static import so the file genuinely exercises source (not just its mocks). |
| 20 | +// `getDefaultShortcuts` reads the registry, not module-scoped store state, so |
| 21 | +// it stays valid across the `vi.resetModules()` reloads below. |
| 22 | +import { getDefaultShortcuts } from './shortcuts-store' |
| 23 | + |
| 24 | +// Shared backing store for the fake plugin-store, persisting across |
| 25 | +// `vi.resetModules()` to stand in for the on-disk `shortcuts.json`. A `Map` |
| 26 | +// avoids dynamic property delete. Declared via `vi.hoisted` so the hoisted |
| 27 | +// `vi.mock` factory can capture it. |
| 28 | +const disk = vi.hoisted(() => new Map<string, unknown>()) |
| 29 | + |
| 30 | +vi.mock('@tauri-apps/plugin-store', () => ({ |
| 31 | + load: vi.fn((_path: string, opts?: { defaults?: Record<string, unknown> }) => { |
| 32 | + // Apply defaults only for keys not already present (matches plugin-store). |
| 33 | + for (const [k, v] of Object.entries(opts?.defaults ?? {})) { |
| 34 | + if (!disk.has(k)) disk.set(k, v) |
| 35 | + } |
| 36 | + return Promise.resolve({ |
| 37 | + get: (key: string) => Promise.resolve(disk.get(key)), |
| 38 | + set: (key: string, value: unknown) => { |
| 39 | + disk.set(key, value) |
| 40 | + return Promise.resolve() |
| 41 | + }, |
| 42 | + delete: (key: string) => Promise.resolve(disk.delete(key)), |
| 43 | + keys: () => Promise.resolve([...disk.keys()]), |
| 44 | + save: () => Promise.resolve(), |
| 45 | + }) |
| 46 | + }), |
| 47 | +})) |
| 48 | + |
| 49 | +vi.mock('$lib/settings/store-path', () => ({ |
| 50 | + resolveStorePath: (name: string) => Promise.resolve(name), |
| 51 | +})) |
| 52 | + |
| 53 | +vi.mock('$lib/ipc/bindings', () => ({ |
| 54 | + commands: { |
| 55 | + updateMenuAccelerator: () => Promise.resolve({ status: 'ok' as const, data: null }), |
| 56 | + }, |
| 57 | +})) |
| 58 | + |
| 59 | +// Import fresh after a module reset so the store's module-scoped map starts empty, |
| 60 | +// then re-reads `disk`. Returns the relevant store functions. |
| 61 | +async function loadStore() { |
| 62 | + return await import('./shortcuts-store') |
| 63 | +} |
| 64 | + |
| 65 | +// The mutating store APIs (`setShortcut`, `addShortcut`, `removeShortcut`, |
| 66 | +// `resetShortcut`) are synchronous and fire `void saveToStore()`. Flush a few |
| 67 | +// microtask turns so the async write to `disk` lands before we assert on it. |
| 68 | +async function flushSave() { |
| 69 | + for (let i = 0; i < 5; i++) await Promise.resolve() |
| 70 | +} |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + // Fresh disk per test; resetModules so each test starts with an uninitialized store. |
| 74 | + disk.clear() |
| 75 | + vi.resetModules() |
| 76 | +}) |
| 77 | + |
| 78 | +describe('shortcuts-store persistence round-trips', () => { |
| 79 | + it('keeps a removed-only-default shortcut removed across a reload (RC2)', async () => { |
| 80 | + // `app.hide` defaults to ['⌘H']. Remove the only shortcut, leaving []. |
| 81 | + let store = await loadStore() |
| 82 | + await store.initializeShortcuts() |
| 83 | + |
| 84 | + store.removeShortcut('app.hide', 0) |
| 85 | + await flushSave() |
| 86 | + expect(store.getEffectiveShortcuts('app.hide')).toEqual([]) |
| 87 | + // Disk must hold the empty array, not the absence of the key. |
| 88 | + expect(disk.get('shortcut:app.hide')).toEqual([]) |
| 89 | + |
| 90 | + // Reload (fresh webview re-reads disk). |
| 91 | + vi.resetModules() |
| 92 | + store = await loadStore() |
| 93 | + await store.initializeShortcuts() |
| 94 | + |
| 95 | + expect(store.getEffectiveShortcuts('app.hide')).toEqual([]) |
| 96 | + }) |
| 97 | + |
| 98 | + it('does not resurrect a removed shortcut on a default-[] command (RC3)', async () => { |
| 99 | + // `app.showAll` defaults to []. Add a custom, then remove it. |
| 100 | + let store = await loadStore() |
| 101 | + await store.initializeShortcuts() |
| 102 | + |
| 103 | + store.addShortcut('app.showAll', 'F7') |
| 104 | + await flushSave() |
| 105 | + expect(store.getEffectiveShortcuts('app.showAll')).toEqual(['F7']) |
| 106 | + expect(disk.get('shortcut:app.showAll')).toEqual(['F7']) |
| 107 | + |
| 108 | + store.removeShortcut('app.showAll', 0) |
| 109 | + await flushSave() |
| 110 | + // Now matches the [] default, so the map entry is cleaned up and the stale |
| 111 | + // disk key must be deleted. |
| 112 | + expect(disk.has('shortcut:app.showAll')).toBe(false) |
| 113 | + |
| 114 | + vi.resetModules() |
| 115 | + store = await loadStore() |
| 116 | + await store.initializeShortcuts() |
| 117 | + |
| 118 | + expect(store.getEffectiveShortcuts('app.showAll')).toEqual([]) |
| 119 | + }) |
| 120 | + |
| 121 | + it('reset-to-default survives a reload (RC3)', async () => { |
| 122 | + // Customize `app.hide` away from its default, then reset it. |
| 123 | + let store = await loadStore() |
| 124 | + await store.initializeShortcuts() |
| 125 | + |
| 126 | + store.setShortcut('app.hide', 0, '⌃X') |
| 127 | + await flushSave() |
| 128 | + expect(disk.get('shortcut:app.hide')).toEqual(['⌃X']) |
| 129 | + |
| 130 | + store.resetShortcut('app.hide') |
| 131 | + await flushSave() |
| 132 | + // After reset the stale disk key must be gone. |
| 133 | + expect(disk.has('shortcut:app.hide')).toBe(false) |
| 134 | + |
| 135 | + vi.resetModules() |
| 136 | + store = await loadStore() |
| 137 | + await store.initializeShortcuts() |
| 138 | + |
| 139 | + expect(store.getEffectiveShortcuts('app.hide')).toEqual(getDefaultShortcuts('app.hide')) |
| 140 | + }) |
| 141 | + |
| 142 | + it('persists and reloads a normal customization (regression)', async () => { |
| 143 | + let store = await loadStore() |
| 144 | + await store.initializeShortcuts() |
| 145 | + |
| 146 | + store.setShortcut('app.showAll', 0, 'F9') |
| 147 | + await flushSave() |
| 148 | + expect(disk.get('shortcut:app.showAll')).toEqual(['F9']) |
| 149 | + |
| 150 | + vi.resetModules() |
| 151 | + store = await loadStore() |
| 152 | + await store.initializeShortcuts() |
| 153 | + |
| 154 | + expect(store.getEffectiveShortcuts('app.showAll')).toEqual(['F9']) |
| 155 | + }) |
| 156 | + |
| 157 | + it('resetAllShortcuts clears every customization across a reload', async () => { |
| 158 | + let store = await loadStore() |
| 159 | + await store.initializeShortcuts() |
| 160 | + |
| 161 | + store.setShortcut('app.showAll', 0, 'F9') |
| 162 | + await flushSave() |
| 163 | + store.setShortcut('app.hide', 0, '⌃X') |
| 164 | + await flushSave() |
| 165 | + expect(disk.get('shortcut:app.showAll')).toEqual(['F9']) |
| 166 | + expect(disk.get('shortcut:app.hide')).toEqual(['⌃X']) |
| 167 | + |
| 168 | + await store.resetAllShortcuts() |
| 169 | + expect(disk.has('shortcut:app.showAll')).toBe(false) |
| 170 | + expect(disk.has('shortcut:app.hide')).toBe(false) |
| 171 | + |
| 172 | + vi.resetModules() |
| 173 | + store = await loadStore() |
| 174 | + await store.initializeShortcuts() |
| 175 | + |
| 176 | + expect(store.getEffectiveShortcuts('app.showAll')).toEqual(getDefaultShortcuts('app.showAll')) |
| 177 | + expect(store.getEffectiveShortcuts('app.hide')).toEqual(getDefaultShortcuts('app.hide')) |
| 178 | + }) |
| 179 | + |
| 180 | + it('ignores non-array (garbage) values at load', async () => { |
| 181 | + // Simulate a corrupted entry on disk. |
| 182 | + disk.set('shortcut:app.showAll', 'not-an-array') |
| 183 | + |
| 184 | + const store = await loadStore() |
| 185 | + await store.initializeShortcuts() |
| 186 | + |
| 187 | + // Garbage is skipped, so the command falls back to its registry default ([]). |
| 188 | + expect(store.getEffectiveShortcuts('app.showAll')).toEqual(getDefaultShortcuts('app.showAll')) |
| 189 | + expect(store.isShortcutModified('app.showAll')).toBe(false) |
| 190 | + }) |
| 191 | +}) |
0 commit comments