| 1 | // Session-state and child-process fixture. No GUI or real input is touched. |
| 2 | import fs from "node:fs"; |
| 3 | import crypto from "node:crypto"; |
| 4 | import { runOk, withSignal } from "../../src/exec.mjs"; |
| 5 | |
| 6 | export function create() { |
| 7 | const instance = crypto.randomUUID(); |
| 8 | const log = process.env.CU_SESSION_CALLS; |
| 9 | let appName = null; |
| 10 | let pointerDown = false; |
| 11 | const record = (method, extra = {}) => fs.appendFileSync(log, JSON.stringify({ instance, method, appName, at: Date.now(), ...extra }) + "\n"); |
| 12 | return { |
| 13 | async probe() { return { ready: true }; }, |
| 14 | async get_app_state({ app_ref }) { |
| 15 | appName = app_ref?.name; |
| 16 | record("get_app_state"); |
| 17 | return { found: true, name: appName, elements: [] }; |
| 18 | }, |
| 19 | async type({ text }) { |
| 20 | if (!appName) throw Object.assign(new Error("Observe an app in this session first"), { code: "target_app_required" }); |
| 21 | record("type", { text }); |
| 22 | return { action_sent: true, appName }; |
| 23 | }, |
| 24 | async hold_key({ text }) { |
| 25 | record("hold_key", { text }); |
| 26 | try { |
| 27 | await runOk(process.execPath, ["-e", ` |
| 28 | const fs = require('node:fs'); |
| 29 | const [file, instance, text] = process.argv.slice(1); |
| 30 | const record = method => fs.appendFileSync(file, JSON.stringify({ instance, method, text, at: Date.now() }) + '\\n'); |
| 31 | record('child_started'); |
| 32 | setTimeout(() => record('late_input'), 5000); |
| 33 | `, log, instance, text]); |
| 34 | } finally { |
| 35 | await withSignal(null, () => runOk(process.execPath, ["-e", ` |
| 36 | const fs = require('node:fs'); |
| 37 | fs.appendFileSync(process.argv[1], JSON.stringify({ instance: process.argv[2], method: 'child_released', at: Date.now() }) + '\\n'); |
| 38 | `, log, instance])); |
| 39 | } |
| 40 | return { action_sent: true }; |
| 41 | }, |
| 42 | async left_mouse_down() { pointerDown = true; record("pointer_down"); return { action_sent: true }; }, |
| 43 | async releaseInput() { |
| 44 | record("release_input", { pointerDown }); |
| 45 | pointerDown = false; |
| 46 | }, |
| 47 | async closeSession() { record("session_closed"); }, |
| 48 | }; |
| 49 | } |
| 50 |