"use strict"; // System Monitor on an ISOLATED Windows test machine: metrics, latency and pagefile configuration. // Pagefile changes only take effect after a restart; this test never restarts Windows and always restores the // original configuration. Environment: CHJ_USERS_WINDOWS_HOST, CHJ_USERS_WINDOWS_USER, CHJ_USERS_WINDOWS_FINGERPRINT // and CHJ_USERS_WINDOWS_PASSWORD or CHJ_USERS_WINDOWS_KEY (private key path). const test = require("node:test"); const assert = require("node:assert/strict"); const { SessionManager } = require("../src/main/sessions/sessionManager"); const { PrivilegedExecutor } = require("../src/main/sessions/privilegedExec"); const { VirtualMemoryService } = require("../src/main/system/virtualMemoryService"); const env = process.env; const enabled = Boolean(env.CHJ_USERS_WINDOWS_HOST && env.CHJ_USERS_WINDOWS_FINGERPRINT && (env.CHJ_USERS_WINDOWS_PASSWORD || env.CHJ_USERS_WINDOWS_KEY)); const skip = enabled ? false : "Set CHJ_USERS_WINDOWS_* to run on an isolated Windows test machine."; test("System Monitor on an isolated Windows machine", { skip, timeout: 600000 }, async (t) => { const fingerprint = Buffer.from(`${env.CHJ_USERS_WINDOWS_FINGERPRINT}=`, "base64").toString("hex"); const profileService = { get: () => ({ id: "w", label: "win", host: env.CHJ_USERS_WINDOWS_HOST, port: 22, username: env.CHJ_USERS_WINDOWS_USER || "codex", authMethod: env.CHJ_USERS_WINDOWS_KEY ? "privateKey" : "password", privateKeyPath: env.CHJ_USERS_WINDOWS_KEY }), getHostKey: () => fingerprint, trustHostKey() {}, getStoredPassword: () => env.CHJ_USERS_WINDOWS_PASSWORD || "", markUsed() {} }; const manager = new SessionManager({ profileService }); const latency = new Promise((resolve) => manager.on("state", (state) => { if (state.latency?.measuredAt) resolve(state.latency); })); await manager.connect({ sessionId: "w", profileId: "w" }); const service = new VirtualMemoryService({ sessionManager: manager, privilegedExecutor: new PrivilegedExecutor({ sessionManager: manager }) }); const original = await service.read("w"); t.after(async () => { // Restore the original pagefile configuration (pending changes are discarded, Windows is never restarted). try { const now = await service.read("w"); if (original.automatic === true && now.automatic !== true) await service.manage("w", { action: "automatic" }); else if (original.automatic === false) { for (const file of original.pagefiles.filter((entry) => entry.configured)) { await service.manage("w", { action: "set", drive: file.drive, initialMb: Math.round((file.initial || 0) / 1048576), maximumMb: Math.round((file.maximum || 0) / 1048576) }); } } const restored = await service.read("w"); assert.equal(restored.automatic, original.automatic, "original management mode restored"); } finally { await manager.disconnectAll(); } }); await t.test("metrics come from the Windows backend; unsupported values are null, not 0", async () => { await manager.readSystemMetrics("w"); await new Promise((resolve) => setTimeout(resolve, 2000)); const metrics = await manager.readSystemMetrics("w"); t.diagnostic(`${metrics.osName} ${metrics.kernel} ${metrics.installationType}`); assert.equal(metrics.os, "windows"); assert.equal(metrics.load1, null); assert.equal(metrics.loadAverageSupported, false); assert.ok(metrics.cpu.usagePercent >= 0 && metrics.cpu.usagePercent <= 100, "CPU usage from two raw counter samples"); assert.ok(metrics.cpu.logicalCores >= 1); assert.ok(metrics.uptimeSeconds > 0); const slices = metrics.memoryDetail.slices; const sum = slices.inUse + slices.modified + slices.standby + slices.free; assert.ok(Math.abs(sum - metrics.memory.total) / metrics.memory.total < 0.05, "non-overlapping memory lists add up to physical memory"); assert.ok(metrics.memoryDetail.commit.limit >= metrics.memory.total, "commit limit includes physical memory"); assert.ok(metrics.disk.total > 0 && metrics.disks.length >= 1); assert.ok(metrics.network.interfaces.some((entry) => entry.state === "up")); assert.equal(metrics.swap.kind, "pagefile"); }); await t.test("ICMP ping and SSH RTT are measured independently", async () => { const value = await Promise.race([latency, new Promise((resolve) => setTimeout(() => resolve(null), 40000))]); t.diagnostic(JSON.stringify(value)); assert.ok(value, "latency published"); assert.ok(value.sshRttMs > 0, "SSH RTT works even if ICMP is blocked"); assert.equal(value.sshRttMethod, "transport"); assert.ok(value.pingMs > 0 || ["timeout", "unreachable", "denied"].includes(value.pingStatus), "ICMP reports a value or a concrete reason"); }); await t.test("pagefile configuration is read, validated and changed only as a pending restart", async () => { t.diagnostic(`original: automatic=${original.automatic} pagefiles=${JSON.stringify(original.pagefiles.map((file) => [file.name, file.configured, file.active]))}`); assert.equal(original.platform, "windows"); assert.equal(original.canManage, true, "elevated administrator session"); assert.ok(original.pagefiles.some((file) => file.active)); for (const payload of [ { action: "set", drive: "C:", initialMb: 8, maximumMb: 16 }, { action: "set", drive: "C:", initialMb: 2048, maximumMb: 1024 }, { action: "set", drive: "C:", initialMb: 0, maximumMb: 1024 }, { action: "set", drive: "C:; Restart-Computer", initialMb: 1024, maximumMb: 2048 }, { action: "set", drive: "C:", initialMb: "1024", maximumMb: 2048.5 } ]) await assert.rejects(() => service.manage("w", payload), { code: /VM_PAGEFILE_(SIZE|DRIVE)_INVALID/ }, JSON.stringify(payload)); await assert.rejects(() => service.manage("w", { action: "set", drive: "Q:", initialMb: 1024, maximumMb: 2048 }), { code: "VM_PAGEFILE_DRIVE_INVALID" }); const system = original.systemDrive || "C:"; const changed = await service.manage("w", { action: "set", drive: system, initialMb: 1024, maximumMb: 2048 }); assert.equal(changed.restartRequired, true); assert.equal(changed.state.automatic, false); const configured = changed.state.pagefiles.find((file) => file.drive === system.toUpperCase()); assert.equal(configured.initial, 1024 * 1048576); assert.equal(configured.maximum, 2048 * 1048576); assert.equal(changed.state.restartPending, true); assert.ok(changed.state.pendingChanges.length >= 1); assert.match(changed.backup, /pagefile-backup-/); const others = changed.state.pagefiles.filter((file) => file.configured && file.drive !== system.toUpperCase()); if (!others.length) { await assert.rejects(() => service.manage("w", { action: "remove", drive: system }), { code: "VM_PAGEFILE_LAST" }); } const concurrent = service.manage("w", { action: "set", drive: system, initialMb: 1024, maximumMb: 4096 }); await assert.rejects(() => service.manage("w", { action: "automatic" }), { code: "VM_BUSY" }); await concurrent; }); });