forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappSettings.test.ts
More file actions
323 lines (304 loc) · 14.5 KB
/
Copy pathappSettings.test.ts
File metadata and controls
323 lines (304 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { describe, it, expect, beforeEach, afterAll, vi } from 'vitest'
import { mkdtempSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
// Point the store at a throwaway userData dir so the round-trip touches a real
// file without hitting the developer's actual profile.
const dir = mkdtempSync(join(tmpdir(), 'omi-appsettings-'))
vi.mock('electron', () => ({
app: { getPath: (): string => dir },
globalShortcut: {
register: (): boolean => true,
unregister: (): void => {},
isRegistered: (): boolean => false
}
}))
import {
getAppSettings,
setAppSettings,
sanitizeAppSettings,
onAppSettingsChanged,
_resetForTests
} from './appSettings'
afterAll(() => rmSync(dir, { recursive: true, force: true }))
describe('appSettings', () => {
beforeEach(() => {
// Reset to defaults between tests by clearing the file AND the in-memory
// cache (getAppSettings reads disk at most once per process).
_resetForTests()
try {
rmSync(join(dir, 'app-settings.json'), { force: true })
} catch {
/* ignore */
}
})
it('returns defaults when no file exists', () => {
const s = getAppSettings()
expect(s.closeToTrayNoticeShown).toBe(false)
expect(s.recordHotkey).toBe('Ctrl+Space')
})
it('round-trips a patched flag and preserves untouched fields', () => {
setAppSettings({ closeToTrayNoticeShown: true })
// Drop the cache so the assertion proves the value persisted to disk.
_resetForTests()
const s = getAppSettings()
expect(s.closeToTrayNoticeShown).toBe(true)
expect(s.recordHotkey).toBe('Ctrl+Space')
})
it('chatEngine defaults to pi_mono and round-trips the legacy_sse opt-out', () => {
expect(getAppSettings().chatEngine).toBe('pi_mono')
setAppSettings({ chatEngine: 'legacy_sse' })
_resetForTests()
expect(getAppSettings().chatEngine).toBe('legacy_sse')
// Junk / unknown values fall back to the default pi_mono path, never silently
// to legacy. Only the exact 'legacy_sse' string opts out.
expect(sanitizeAppSettings({ chatEngine: 'nope' } as never).chatEngine).toBe('pi_mono')
expect(sanitizeAppSettings(null).chatEngine).toBe('pi_mono')
})
it('chatScreenshotSharingEnabled defaults ON and round-trips an explicit off', () => {
expect(getAppSettings().chatScreenshotSharingEnabled).toBe(true)
setAppSettings({ chatScreenshotSharingEnabled: false })
_resetForTests()
expect(getAppSettings().chatScreenshotSharingEnabled).toBe(false)
// Absent / junk stays ON (opt-out) — only an explicit false disables it.
expect(sanitizeAppSettings(null).chatScreenshotSharingEnabled).toBe(true)
expect(sanitizeAppSettings({} as never).chatScreenshotSharingEnabled).toBe(true)
})
it('betaUpdatesEnabled defaults OFF (stable) and round-trips an explicit opt-in', () => {
expect(getAppSettings().betaUpdatesEnabled).toBe(false)
setAppSettings({ betaUpdatesEnabled: true })
_resetForTests()
expect(getAppSettings().betaUpdatesEnabled).toBe(true)
// Opt-IN (=== true): absent / junk stays OFF — only an explicit true enables it.
expect(sanitizeAppSettings(null).betaUpdatesEnabled).toBe(false)
expect(sanitizeAppSettings({} as never).betaUpdatesEnabled).toBe(false)
expect(sanitizeAppSettings({ betaUpdatesEnabled: 'yes' } as never).betaUpdatesEnabled).toBe(
false
)
})
it('round-trips a rebound record hotkey', () => {
setAppSettings({ recordHotkey: 'Ctrl+Shift+O' })
_resetForTests()
expect(getAppSettings().recordHotkey).toBe('Ctrl+Shift+O')
})
it('round-trips the record-hotkey enabled flag (default on)', () => {
expect(getAppSettings().recordHotkeyEnabled).toBe(true)
setAppSettings({ recordHotkeyEnabled: false })
_resetForTests()
expect(getAppSettings().recordHotkeyEnabled).toBe(false)
})
// The proactive coordinator's master toggle would otherwise be one-way: it
// re-reads the setting each tick (so OFF works), but only a listener can
// re-arm the loop when it goes back ON.
it('notifies listeners on every write, and a throwing listener does not lose the write', () => {
const seen: boolean[] = []
onAppSettingsChanged(() => {
throw new Error('boom')
})
onAppSettingsChanged((s) => seen.push(s.screenAnalysisEnabled))
vi.spyOn(console, 'warn').mockImplementation(() => {})
setAppSettings({ screenAnalysisEnabled: false })
setAppSettings({ screenAnalysisEnabled: true })
expect(seen).toEqual([false, true])
expect(getAppSettings().screenAnalysisEnabled).toBe(true)
})
it('sanitizes bad input back to safe defaults', () => {
expect(sanitizeAppSettings({} as never)).toEqual({
closeToTrayNoticeShown: false,
hotkeyConflictNoticeShown: false,
recordHotkey: 'Ctrl+Space',
recordHotkeyEnabled: true,
summonHotkey: 'Shift+Space',
hudContentProtection: true,
meeting: { mode: 'ask', endGraceMinutes: 2, perApp: {}, firstRunToastShown: false },
lastShownChangelogVersion: null,
betaUpdatesEnabled: false,
aiProfileEnabled: true,
focusEnabled: true,
focusNotificationsEnabled: true,
focusCooldownMinutes: 10,
focusExcludedApps: [],
glowOverlayEnabled: false,
screenAnalysisEnabled: true,
notificationsEnabled: true,
notificationFrequency: 0,
memoryEnabled: false,
memoryExtractionIntervalMin: 10,
memoryMinConfidence: 0.7,
memoryExcludedApps: [],
taskEnabled: true,
taskFallbackIntervalMin: 10,
taskMinConfidence: 0.75,
taskExcludedApps: [],
chatEngine: 'pi_mono',
chatScreenshotSharingEnabled: true,
goalAutoGenerationEnabled: false,
goalGenerationLastDate: null,
goalAutoGeneratedIds: []
})
// Proactive notifications default to Off (level 0) — an assistant may only
// interrupt once the user has chosen a frequency. Anything that is not a
// valid level falls back to Off, never to the NEAREST level: clamping a
// corrupt file (or a backend sync sending 10) up to 5 would mean "no
// throttle" — unthrottled toasts for a user whose default was silence.
expect(sanitizeAppSettings({ notificationFrequency: 4 }).notificationFrequency).toBe(4)
expect(sanitizeAppSettings({ notificationFrequency: 9 }).notificationFrequency).toBe(0)
expect(sanitizeAppSettings({ notificationFrequency: -2 }).notificationFrequency).toBe(0)
expect(sanitizeAppSettings({ notificationFrequency: 2.5 }).notificationFrequency).toBe(0)
expect(
sanitizeAppSettings({ notificationFrequency: 'max' } as never).notificationFrequency
).toBe(0)
// Goal auto-generation is opt-IN (=== true): OFF by default, only an explicit
// true enables it. The attribution record + last-date sanitize defensively —
// junk entries are dropped so cleanup can never match (delete) the wrong goal.
expect(sanitizeAppSettings({ goalAutoGenerationEnabled: true }).goalAutoGenerationEnabled).toBe(
true
)
expect(
sanitizeAppSettings({ goalAutoGenerationEnabled: 'yes' } as never).goalAutoGenerationEnabled
).toBe(false)
expect(
sanitizeAppSettings({ goalGenerationLastDate: '2026-07-15' }).goalGenerationLastDate
).toBe('2026-07-15')
expect(
sanitizeAppSettings({ goalGenerationLastDate: 123 } as never).goalGenerationLastDate
).toBe(null)
expect(
sanitizeAppSettings({
goalAutoGeneratedIds: [
{ id: 'g1', createdAt: 10 },
{ id: '', createdAt: 5 }, // dropped (no id)
{ id: 'g2', createdAt: 'soon' }, // dropped (bad createdAt)
'junk'
]
} as never).goalAutoGeneratedIds
).toEqual([{ id: 'g1', createdAt: 10 }])
// Screen analysis is opt-OUT: on unless the user turns it off.
expect(sanitizeAppSettings({ screenAnalysisEnabled: false }).screenAnalysisEnabled).toBe(false)
// The focus halo is opt-IN (=== true), matching Mac's default-OFF: absent or
// junk yields false, only an explicit true enables it.
expect(sanitizeAppSettings({} as never).glowOverlayEnabled).toBe(false)
expect(sanitizeAppSettings({ glowOverlayEnabled: true }).glowOverlayEnabled).toBe(true)
expect(sanitizeAppSettings({ glowOverlayEnabled: 'yes' } as never).glowOverlayEnabled).toBe(
false
)
// The AI user profile is opt-OUT now that Focus consumes it: on unless the
// user explicitly turns it off.
expect(sanitizeAppSettings({ aiProfileEnabled: false }).aiProfileEnabled).toBe(false)
expect(sanitizeAppSettings({ aiProfileEnabled: 'yes' } as never).aiProfileEnabled).toBe(true)
// Focus master flags are opt-OUT; cooldown and excluded-apps sanitize.
expect(sanitizeAppSettings({ focusEnabled: false }).focusEnabled).toBe(false)
expect(
sanitizeAppSettings({ focusNotificationsEnabled: false }).focusNotificationsEnabled
).toBe(false)
expect(sanitizeAppSettings({ focusCooldownMinutes: 5 }).focusCooldownMinutes).toBe(5)
// Zero/negative/junk cooldown falls back to 10 (never disables the cooldown).
expect(sanitizeAppSettings({ focusCooldownMinutes: 0 }).focusCooldownMinutes).toBe(10)
expect(sanitizeAppSettings({ focusCooldownMinutes: -3 }).focusCooldownMinutes).toBe(10)
expect(sanitizeAppSettings({ focusCooldownMinutes: 2.5 }).focusCooldownMinutes).toBe(10)
// Excluded apps: only non-empty strings survive.
expect(
sanitizeAppSettings({ focusExcludedApps: ['Slack', '', ' Discord ', 5] as never })
.focusExcludedApps
).toEqual(['Slack', 'Discord'])
expect(sanitizeAppSettings({ focusExcludedApps: 'nope' as never }).focusExcludedApps).toEqual(
[]
)
// Memory: master flag opt-OUT; interval reuses the cooldown sanitizer (positive
// integer minutes, junk → 10); min-confidence clamps to [0,1] (junk → 0.7).
expect(sanitizeAppSettings({ memoryEnabled: false }).memoryEnabled).toBe(false)
expect(
sanitizeAppSettings({ memoryExtractionIntervalMin: 15 }).memoryExtractionIntervalMin
).toBe(15)
expect(
sanitizeAppSettings({ memoryExtractionIntervalMin: 0 }).memoryExtractionIntervalMin
).toBe(10)
expect(sanitizeAppSettings({ memoryMinConfidence: 0.85 }).memoryMinConfidence).toBe(0.85)
expect(sanitizeAppSettings({ memoryMinConfidence: 2 }).memoryMinConfidence).toBe(1)
expect(sanitizeAppSettings({ memoryMinConfidence: -1 }).memoryMinConfidence).toBe(0)
expect(sanitizeAppSettings({ memoryMinConfidence: 'high' } as never).memoryMinConfidence).toBe(
0.7
)
expect(
sanitizeAppSettings({ memoryExcludedApps: ['Zoom', '', ' Music '] as never })
.memoryExcludedApps
).toEqual(['Zoom', 'Music'])
// Task: master flag is default-ON (only an explicit false disables it, same
// idiom as screenAnalysisEnabled); interval reuses the cooldown sanitizer
// (junk → 10); min-confidence clamps to [0,1] with a 0.75 floor (vs Memory's
// 0.7); excluded-apps sanitize.
expect(sanitizeAppSettings(null).taskEnabled).toBe(true)
expect(sanitizeAppSettings({ taskEnabled: false }).taskEnabled).toBe(false)
expect(sanitizeAppSettings({ taskEnabled: true }).taskEnabled).toBe(true)
expect(sanitizeAppSettings(null).taskFallbackIntervalMin).toBe(10)
expect(sanitizeAppSettings({ taskFallbackIntervalMin: 15 }).taskFallbackIntervalMin).toBe(15)
expect(sanitizeAppSettings({ taskFallbackIntervalMin: 0 }).taskFallbackIntervalMin).toBe(10)
expect(sanitizeAppSettings(null).taskMinConfidence).toBe(0.75)
expect(sanitizeAppSettings({ taskMinConfidence: 0.9 }).taskMinConfidence).toBe(0.9)
expect(sanitizeAppSettings({ taskMinConfidence: 2 }).taskMinConfidence).toBe(1)
expect(sanitizeAppSettings({ taskMinConfidence: -1 }).taskMinConfidence).toBe(0)
expect(sanitizeAppSettings({ taskMinConfidence: 'high' } as never).taskMinConfidence).toBe(0.75)
expect(sanitizeAppSettings(null).taskExcludedApps).toEqual([])
expect(
sanitizeAppSettings({ taskExcludedApps: ['Slack', '', ' Notion '] as never })
.taskExcludedApps
).toEqual(['Slack', 'Notion'])
expect(sanitizeAppSettings({ summonHotkey: ' ' } as never).summonHotkey).toBe('Shift+Space')
expect(sanitizeAppSettings({ summonHotkey: 'Alt+K' } as never).summonHotkey).toBe('Alt+K')
expect(sanitizeAppSettings({ recordHotkey: ' ' } as never).recordHotkey).toBe('Ctrl+Space')
expect(sanitizeAppSettings({ recordHotkey: 42 } as never).recordHotkey).toBe('Ctrl+Space')
expect(
sanitizeAppSettings({ closeToTrayNoticeShown: 'yes' } as never).closeToTrayNoticeShown
).toBe(false)
expect(sanitizeAppSettings(null).recordHotkey).toBe('Ctrl+Space')
// recordHotkeyEnabled defaults ON; only an explicit false disables it.
expect(sanitizeAppSettings(null).recordHotkeyEnabled).toBe(true)
expect(sanitizeAppSettings({ recordHotkeyEnabled: false }).recordHotkeyEnabled).toBe(false)
expect(sanitizeAppSettings({ recordHotkeyEnabled: 'nope' } as never).recordHotkeyEnabled).toBe(
true
)
// HUD capture-exclusion defaults ON and only an explicit false disables it.
expect(sanitizeAppSettings(null).hudContentProtection).toBe(true)
expect(sanitizeAppSettings({ hudContentProtection: false }).hudContentProtection).toBe(false)
expect(
sanitizeAppSettings({ hudContentProtection: 'nope' } as never).hudContentProtection
).toBe(true)
})
it('meeting settings default to ask/2min and sanitize bad values', () => {
const d = sanitizeAppSettings(null).meeting
expect(d).toEqual({ mode: 'ask', endGraceMinutes: 2, perApp: {}, firstRunToastShown: false })
const m = sanitizeAppSettings({
meeting: {
mode: 'auto',
endGraceMinutes: 999,
perApp: { zoom: 'off', bogus: 'sideways' as never },
firstRunToastShown: true
}
} as never).meeting
expect(m.mode).toBe('auto')
expect(m.endGraceMinutes).toBe(30) // clamped
expect(m.perApp).toEqual({ zoom: 'off' }) // invalid override dropped
expect(m.firstRunToastShown).toBe(true)
expect(sanitizeAppSettings({ meeting: { mode: 'loud' } } as never).meeting.mode).toBe('ask')
expect(
sanitizeAppSettings({ meeting: { endGraceMinutes: 0 } } as never).meeting.endGraceMinutes
).toBe(1)
})
it('round-trips meeting settings', () => {
setAppSettings({
meeting: {
mode: 'auto',
endGraceMinutes: 5,
perApp: { discord: 'off' },
firstRunToastShown: true
}
})
_resetForTests()
expect(getAppSettings().meeting).toEqual({
mode: 'auto',
endGraceMinutes: 5,
perApp: { discord: 'off' },
firstRunToastShown: true
})
})
})