114 lines
3.6 KiB
TypeScript
114 lines
3.6 KiB
TypeScript
import { EventEmitter } from 'events'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService'
|
|
import { AdMediationEngine } from '../../../src/main/services/ads/AdMediationEngine'
|
|
import {
|
|
getAudioCaptureService,
|
|
resetAudioCaptureServiceForTests,
|
|
} from '../../../src/main/services/AudioCaptureService'
|
|
import {
|
|
getLocalLLMService,
|
|
resetLocalLLMServiceForTests,
|
|
} from '../../../src/main/services/LocalLLMService'
|
|
|
|
const spawnState = vi.hoisted(() => ({
|
|
nextProc: null as null | (EventEmitter & {
|
|
stdout: EventEmitter
|
|
stderr: EventEmitter
|
|
kill: ReturnType<typeof vi.fn>
|
|
}),
|
|
}))
|
|
|
|
vi.mock('child_process', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('child_process')>()
|
|
return {
|
|
...actual,
|
|
spawn: vi.fn((...args: Parameters<typeof actual.spawn>) => {
|
|
if (spawnState.nextProc) {
|
|
const proc = spawnState.nextProc
|
|
spawnState.nextProc = null
|
|
return proc
|
|
}
|
|
return actual.spawn(...args)
|
|
}),
|
|
}
|
|
})
|
|
|
|
describe('Main service timer cleanup regressions', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
initInMemoryConfig()
|
|
resetLocalLLMServiceForTests()
|
|
resetAudioCaptureServiceForTests()
|
|
;(AdMediationEngine as unknown as { instance: null }).instance = null
|
|
})
|
|
|
|
afterEach(() => {
|
|
resetLocalLLMServiceForTests()
|
|
resetAudioCaptureServiceForTests()
|
|
;(AdMediationEngine as unknown as { instance: null }).instance = null
|
|
spawnState.nextProc = null
|
|
resetInMemoryConfig()
|
|
vi.unstubAllGlobals()
|
|
vi.useRealTimers()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('LocalLLMService_disposeAfterStartServer_cancelsPendingAvailabilityCheckTimers', async () => {
|
|
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ version: '0.5.0' }), { status: 200 }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const llm = getLocalLLMService()
|
|
const status = await llm.startServer()
|
|
expect(status).toBe('running')
|
|
expect(vi.getTimerCount()).toBe(2)
|
|
|
|
const callsBeforeDispose = fetchMock.mock.calls.length
|
|
llm.dispose()
|
|
|
|
expect(vi.getTimerCount()).toBe(0)
|
|
await vi.advanceTimersByTimeAsync(5_000)
|
|
expect(fetchMock).toHaveBeenCalledTimes(callsBeforeDispose)
|
|
})
|
|
|
|
it('AdMediationEngine_runAuctionSettlesBeforeTimeout_clearsBidTimeoutTimer', async () => {
|
|
const engine = AdMediationEngine.getInstance()
|
|
const config = engine.getConfig()
|
|
engine.setConfig({
|
|
headerBiddingTimeoutMs: 15_000,
|
|
networks: config.networks.map((n) =>
|
|
n.id === 'direct_sponsor' ? { ...n, enabled: true } : n,
|
|
),
|
|
})
|
|
|
|
const result = await engine.runAuction({
|
|
placementId: 'dock-1',
|
|
format: 'banner_dock',
|
|
})
|
|
|
|
expect(result.winner === null).toBe(true)
|
|
expect(result.winningBidEcpm).toBe(0)
|
|
expect(result.participatingBids).toHaveLength(1)
|
|
expect(vi.getTimerCount()).toBe(0)
|
|
})
|
|
|
|
it('AudioCaptureService_testCaptureExitsEarly_clearsDurationKillTimer', async () => {
|
|
const proc = Object.assign(new EventEmitter(), {
|
|
stdout: new EventEmitter(),
|
|
stderr: new EventEmitter(),
|
|
kill: vi.fn(),
|
|
})
|
|
spawnState.nextProc = proc
|
|
|
|
const service = getAudioCaptureService()
|
|
const capturePromise = service.testCapture(10_000)
|
|
expect(vi.getTimerCount()).toBe(1)
|
|
|
|
proc.emit('close', 0)
|
|
const stats = await capturePromise
|
|
|
|
expect(stats).toEqual({ averageLevel: 0, peakLevel: 0, hasAudio: false })
|
|
expect(proc.kill).not.toHaveBeenCalled()
|
|
expect(vi.getTimerCount()).toBe(0)
|
|
})
|
|
})
|