65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import * as fsp from 'fs/promises';
|
|
import * as os from 'os';
|
|
import * as path from 'path';
|
|
import {
|
|
afterEach,
|
|
describe,
|
|
expect,
|
|
it
|
|
} from 'vitest';
|
|
import {
|
|
clearHighMemoryAlert,
|
|
readHighMemoryAlert,
|
|
resolveHighMemoryAlertPath,
|
|
writeHighMemoryAlert
|
|
} from './high-memory-alert.store';
|
|
|
|
describe('high-memory-alert.store', () => {
|
|
const tempDirs: string[] = [];
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(tempDirs.splice(0).map((dir) => fsp.rm(dir, {
|
|
recursive: true,
|
|
force: true
|
|
})));
|
|
});
|
|
|
|
it('writes and reads a pending startup alert record', async () => {
|
|
const userDataPath = await fsp.mkdtemp(path.join(os.tmpdir(), 'metoyou-high-memory-'));
|
|
|
|
tempDirs.push(userDataPath);
|
|
|
|
const record = {
|
|
logFilePath: path.join(userDataPath, 'diagnostics', 'perf-session.jsonl'),
|
|
detectedAt: 1_700_000_000_000,
|
|
peakWorkingSetKb: 2_200_000,
|
|
sessionId: 'session-1'
|
|
};
|
|
|
|
await writeHighMemoryAlert(userDataPath, record);
|
|
|
|
expect(resolveHighMemoryAlertPath(userDataPath)).toBe(
|
|
path.join(userDataPath, 'diagnostics', 'high-memory-pending.json')
|
|
);
|
|
|
|
expect(await readHighMemoryAlert(userDataPath)).toEqual(record);
|
|
});
|
|
|
|
it('clears the pending startup alert record', async () => {
|
|
const userDataPath = await fsp.mkdtemp(path.join(os.tmpdir(), 'metoyou-high-memory-'));
|
|
|
|
tempDirs.push(userDataPath);
|
|
|
|
await writeHighMemoryAlert(userDataPath, {
|
|
logFilePath: '/tmp/perf.jsonl',
|
|
detectedAt: Date.now(),
|
|
peakWorkingSetKb: 2_100_000,
|
|
sessionId: 'session-2'
|
|
});
|
|
|
|
await clearHighMemoryAlert(userDataPath);
|
|
|
|
expect(await readHighMemoryAlert(userDataPath)).toBeNull();
|
|
});
|
|
});
|