forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelperProcess.test.ts
More file actions
76 lines (63 loc) · 2.68 KB
/
Copy pathhelperProcess.test.ts
File metadata and controls
76 lines (63 loc) · 2.68 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
// Regression for C7: the OCR/window-info helper subprocess must be killable so
// the will-quit handler can dispose it (without a dispose() call site it orphaned
// omi-*-ocr-helper.exe on every quit). We mock child_process.spawn so no real
// helper binary is needed, and assert dispose() kills the live child and that a
// later request re-spawns a fresh one.
import { EventEmitter } from 'node:events'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const spawnMock = vi.fn()
vi.mock('child_process', () => ({ spawn: (...args: unknown[]) => spawnMock(...args) }))
vi.mock('./resolveHelperPath', () => ({ resolveHelperPath: () => 'C:\\fake\\win-ocr-helper.exe' }))
type FakeChild = EventEmitter & {
stdout: EventEmitter
stderr: EventEmitter
stdin: { write: ReturnType<typeof vi.fn> }
kill: ReturnType<typeof vi.fn>
}
function makeFakeChild(): FakeChild {
const child = new EventEmitter() as FakeChild
child.stdout = new EventEmitter()
child.stderr = new EventEmitter()
child.stdin = { write: vi.fn() }
child.kill = vi.fn()
return child
}
describe('helperProcess.dispose (C7 — no orphaned OCR helper on quit)', () => {
beforeEach(() => {
spawnMock.mockReset()
vi.resetModules()
})
afterEach(() => {
vi.restoreAllMocks()
})
it('kills the live child on dispose()', async () => {
const child = makeFakeChild()
spawnMock.mockReturnValue(child)
const { helperProcess } = await import('./helperProcess')
// Fire a request to lazily spawn the child (we never resolve it — dispose
// rejects it below).
void helperProcess.windowInfo().catch(() => {})
expect(spawnMock).toHaveBeenCalledTimes(1)
// The helper is a console-subsystem exe; it must be spawned with
// windowsHide so it never flashes a stray console window in the taskbar.
expect(spawnMock.mock.calls[0][2]).toMatchObject({ windowsHide: true })
helperProcess.dispose()
expect(child.kill).toHaveBeenCalledTimes(1)
})
it('rejects the in-flight request when disposed mid-flight', async () => {
const child = makeFakeChild()
spawnMock.mockReturnValue(child)
const { helperProcess } = await import('./helperProcess')
const pending = helperProcess.windowInfo()
helperProcess.dispose()
await expect(pending).rejects.toThrow(/helper exited/)
})
it('re-spawns a fresh child on the next request after dispose()', async () => {
spawnMock.mockImplementation(() => makeFakeChild())
const { helperProcess } = await import('./helperProcess')
void helperProcess.windowInfo().catch(() => {})
helperProcess.dispose()
void helperProcess.windowInfo().catch(() => {})
expect(spawnMock).toHaveBeenCalledTimes(2)
})
})