| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtempSync, readFileSync, readdirSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import { test } from "node:test"; |
| 6 | import type { HelloResult } from "./handshake.js"; |
| 7 | import { ShellLifecycle } from "./shellLifecycle.js"; |
| 8 | |
| 9 | function hello(enabled: boolean): HelloResult { |
| 10 | return { |
| 11 | protocolVersion: 11, |
| 12 | contractDigest: "sha256:test", |
| 13 | service: { version: "v1.40.0", channel: "stable", commit: "abc", pid: 42 }, |
| 14 | runtimeGeneration: "g-1", |
| 15 | runId: "service-run", |
| 16 | incidentId: "service-incident", |
| 17 | diagnosticsEnabled: enabled, |
| 18 | resources: { origin: "http://127.0.0.1:1", token: "token" }, |
| 19 | window: { |
| 20 | width: 1, |
| 21 | height: 1, |
| 22 | minWidth: 1, |
| 23 | minHeight: 1, |
| 24 | frameless: false, |
| 25 | zoomFactor: 1, |
| 26 | }, |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | test("shell lifecycle starts only after diagnostics-enabled hello and removes only its own record", () => { |
| 31 | const root = mkdtempSync(join(tmpdir(), "reasonix-shell-lifecycle-")); |
| 32 | const disabled = new ShellLifecycle(root, { |
| 33 | version: "v1.40.0", |
| 34 | channel: "stable", |
| 35 | commit: "abc", |
| 36 | }); |
| 37 | disabled.start(hello(false)); |
| 38 | const dir = join(root, "diagnostics", "lifecycle"); |
| 39 | assert.deepEqual(readdirSync(root), []); |
| 40 | |
| 41 | const tracker = new ShellLifecycle(root, { |
| 42 | version: "v1.40.0", |
| 43 | channel: "stable", |
| 44 | commit: "abc", |
| 45 | }); |
| 46 | tracker.start(hello(true)); |
| 47 | const [name] = readdirSync(dir); |
| 48 | const state = JSON.parse(readFileSync(join(dir, name), "utf8")) as Record<string, unknown>; |
| 49 | assert.equal(state.schemaVersion, 3); |
| 50 | assert.equal(state.processRole, "shell"); |
| 51 | assert.equal(state.incidentId, "service-incident"); |
| 52 | tracker.complete(); |
| 53 | assert.deepEqual(readdirSync(dir), []); |
| 54 | }); |
| 55 |