-
-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathsession-status.test.ts
More file actions
104 lines (94 loc) · 4.46 KB
/
session-status.test.ts
File metadata and controls
104 lines (94 loc) · 4.46 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
import { mkdtempSync } from 'node:fs';
import { rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import * as path from 'node:path';
import { EventEmitter } from 'node:events';
import type { ChildProcess } from 'node:child_process';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { clearDaemonActivityRegistry } from '../../../daemon/activity-registry.ts';
import { getDefaultDebuggerManager } from '../../../utils/debugger/index.ts';
import { activeLogSessions } from '../../../utils/log_capture.ts';
import { activeDeviceLogSessions } from '../../../utils/log-capture/device-log-sessions.ts';
import {
clearAllSimulatorLaunchOsLogSessions,
registerSimulatorLaunchOsLogSession,
setSimulatorLaunchOsLogRegistryDirOverrideForTests,
} from '../../../utils/log-capture/simulator-launch-oslog-sessions.ts';
import { setSimulatorLaunchOsLogRecordActiveOverrideForTests } from '../../../utils/log-capture/simulator-launch-oslog-registry.ts';
import { setRuntimeInstanceForTests } from '../../../utils/runtime-instance.ts';
import { clearAllProcesses } from '../../tools/swift-package/active-processes.ts';
import { sessionStatusResourceLogic } from '../session-status.ts';
let registryDir: string;
function createTrackedChild(pid = 777): ChildProcess {
const emitter = new EventEmitter();
const child = emitter as ChildProcess;
Object.defineProperty(child, 'pid', { value: pid, configurable: true });
Object.defineProperty(child, 'exitCode', { value: null, writable: true, configurable: true });
child.kill = (() => true) as ChildProcess['kill'];
return child;
}
describe('session-status resource', () => {
beforeEach(async () => {
registryDir = mkdtempSync(path.join(tmpdir(), 'xcodebuildmcp-session-status-'));
setSimulatorLaunchOsLogRegistryDirOverrideForTests(registryDir);
setRuntimeInstanceForTests({ instanceId: 'session-status-test', pid: process.pid });
setSimulatorLaunchOsLogRecordActiveOverrideForTests(async () => true);
activeLogSessions.clear();
activeDeviceLogSessions.clear();
clearAllProcesses();
await clearAllSimulatorLaunchOsLogSessions();
clearDaemonActivityRegistry();
await getDefaultDebuggerManager().disposeAll();
});
afterEach(async () => {
activeLogSessions.clear();
activeDeviceLogSessions.clear();
clearAllProcesses();
await clearAllSimulatorLaunchOsLogSessions();
clearDaemonActivityRegistry();
await getDefaultDebuggerManager().disposeAll();
setSimulatorLaunchOsLogRecordActiveOverrideForTests(null);
setRuntimeInstanceForTests(null);
setSimulatorLaunchOsLogRegistryDirOverrideForTests(null);
await rm(registryDir, { recursive: true, force: true });
});
describe('Handler Functionality', () => {
it('should return empty status when no sessions exist', async () => {
const result = await sessionStatusResourceLogic();
expect(result.contents).toHaveLength(1);
const parsed = JSON.parse(result.contents[0].text);
expect(parsed.logging.simulator.activeSessionIds).toEqual([]);
expect(parsed.logging.simulator.activeLaunchOsLogSessions).toEqual([]);
expect(parsed.logging.device.activeSessionIds).toEqual([]);
expect(parsed.debug.currentSessionId).toBe(null);
expect(parsed.debug.sessionIds).toEqual([]);
expect(parsed.watcher).toEqual({ running: false, watchedPath: null });
expect(parsed.video.activeSessionIds).toEqual([]);
expect(parsed.swiftPackage.activePids).toEqual([]);
expect(parsed.activity).toEqual({ activeOperationCount: 0, byCategory: {} });
expect(parsed.process.pid).toBeTypeOf('number');
expect(parsed.process.uptimeMs).toBeTypeOf('number');
expect(parsed.process.rssBytes).toBeTypeOf('number');
expect(parsed.process.heapUsedBytes).toBeTypeOf('number');
});
it('should include tracked launch OSLog sessions', async () => {
await registerSimulatorLaunchOsLogSession({
process: createTrackedChild(888),
simulatorUuid: 'sim-1',
bundleId: 'io.sentry.app',
logFilePath: '/tmp/app.log',
});
const result = await sessionStatusResourceLogic();
const parsed = JSON.parse(result.contents[0].text);
expect(parsed.logging.simulator.activeLaunchOsLogSessions).toEqual([
expect.objectContaining({
simulatorUuid: 'sim-1',
bundleId: 'io.sentry.app',
pid: 888,
logFilePath: '/tmp/app.log',
ownedByCurrentProcess: true,
}),
]);
});
});
});