import { beforeEach, describe, expect, it, vi } from 'vitest'; import * as os from 'os'; import * as path from 'path'; import * as fsp from 'fs/promises'; import { captureHighMemoryDiagnostics } from './high-memory-capture'; vi.mock('./immediate-renderer-samples.collector', () => ({ collectImmediateRendererSamples: vi.fn(async () => []) })); vi.mock('./session-context.collector', () => ({ collectSessionContext: vi.fn(() => ({ platform: 'linux', userDataPath: '/tmp/user-data' })) })); describe('captureHighMemoryDiagnostics', () => { let userDataPath = ''; beforeEach(async () => { userDataPath = await fsp.mkdtemp(path.join(os.tmpdir(), 'metoyou-high-memory-capture-')); }); it('writes a diagnostics snapshot and returns an alert record', async () => { const record = await captureHighMemoryDiagnostics({ userDataPath, sessionStartedAt: Date.now() - 60_000, metrics: { collectedAt: Date.now(), processes: [ { pid: 1, type: 'Browser', workingSetKb: 2_200_000 } ] }, totalWorkingSetKb: 2_200_000, writer: null, mainWindow: null, reason: 'manual' }); expect(record.peakWorkingSetKb).toBe(2_200_000); expect(record.reason).toBe('manual'); expect(record.logFilePath).toContain(userDataPath); await expect(fsp.stat(record.logFilePath)).resolves.toBeDefined(); }); });