Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249import { spawnSync, } from 'node:child_process';import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync,} from 'node:fs';import { join, } from 'node:path';import { setTimeout as wait, } from 'node:timers/promises';import { fileURLToPath, } from 'node:url';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { SESSION_NOT_FOUND_WARNING, } from './cli-core.ts';import { tempDir, } from './test-support.ts';
/** Number of polling attempts used while waiting for detached fake terminal-exec output. */const DETACHED_WRITE_ATTEMPTS = 50;
/** Milliseconds between detached fake terminal-exec output checks. */const DETACHED_WRITE_DELAY_MS = 10;
/** Source CLI path used for fallback smoke coverage. */const SOURCE_CLI_PATH = fileURLToPath(new URL( 'cli.ts', import.meta.url,),);
/** Directory name used for fake executables in CLI tests. */const FAKE_BIN_DIRNAME = 'bin';
/** File name used for fake terminal-exec executable in CLI tests. */const FAKE_TERMINAL_EXEC_NAME = 'terminal-exec';
/** Environment variable carrying fake terminal-exec launch record path. */const FAKE_TERMINAL_RECORD_PATH_ENV = 'SPAWN_PI_FAKE_TERMINAL_RECORD_PATH';
/** Non-nullish sentinel written when fake terminal-exec receives no spawn identifier. */const FAKE_TERMINAL_SPAWN_ID_ABSENT = '<absent>';
/** Executable permission bits for fake terminal-exec. */const EXECUTABLE_PERMISSION = 0o755;
/** Captured fake terminal-exec launch record. */type FakeTerminalRecord = { /** Arguments passed to terminal-exec. */ readonly argv: readonly string[]; /** Working directory observed by fake terminal-exec. */ readonly cwd: string; /** Spawn identifier inherited by terminal-exec, or sentinel text in unlinked mode. */ readonly spawnId: string;};
/** Writes fake terminal-exec executable that records its launch payload. @param binDir - directory receiving fake executable. @returns path to fake terminal-exec executable. @example ```typescript const fakeTerminal = writeFakeTerminalExec('/tmp/bin'); ``` */function writeFakeTerminalExec(binDir: string,): string { mkdirSync( binDir, { recursive: true, }, );
/** Fake terminal-exec executable path. */ const fakeTerminalPath = join( binDir, FAKE_TERMINAL_EXEC_NAME, );
writeFileSync( fakeTerminalPath, [ '#!/usr/bin/env node', "import { writeFileSync } from 'node:fs';", 'writeFileSync(', ` process.env.${FAKE_TERMINAL_RECORD_PATH_ENV},`, ' JSON.stringify({', ' argv: process.argv.slice(2),', ' cwd: process.cwd(),', ` spawnId: process.env.PI_SPAWN_ID ?? '${FAKE_TERMINAL_SPAWN_ID_ABSENT}',`, ' }),', ');', '', ].join('\n',), ); chmodSync( fakeTerminalPath, EXECUTABLE_PERMISSION, );
return fakeTerminalPath;}
/** Waits for detached fake terminal-exec to write its launch record. @param recordPath - JSON record path expected from fake terminal-exec. @returns parsed launch record. @throws when fake terminal-exec does not write record before polling ends. @example ```typescript const record = await readDetachedRecord('/tmp/record.json'); ``` */async function readDetachedRecord(recordPath: string,): Promise<FakeTerminalRecord> { for (let attempt = 0; attempt < DETACHED_WRITE_ATTEMPTS; attempt += 1) { if (existsSync(recordPath,)) { /** Raw JSON written by fake terminal-exec. */ const raw = readFileSync( recordPath, 'utf8', ); /** Parsed fake terminal-exec launch record. */ const record = JSON.parse(raw,) as FakeTerminalRecord; return record; }
// oxlint-disable-next-line eslint/no-await-in-loop -- Sequential polling observes detached child state after each delay; parallel waits would not check the filesystem between attempts. await wait(DETACHED_WRITE_DELAY_MS,); }
throw new Error('fake terminal-exec did not write launch record',);}
await describe({ name: '', concurrency: 1, children: [ describe({ name: 'unlinked fallback', children: [ it({ name: 'warns and launches child Pi without result forwarding when no parent mapping exists', fn: async function testUnlinkedFallback() { await using dir = await tempDir({ prefix: 'spawn-pi-cli-fallback-', },);
/** Fake executable directory prepended to PATH. */ const fakeBinDir = join( dir.path, FAKE_BIN_DIRNAME, ); /** Captured fake terminal-exec launch record path. */ const recordPath = join( dir.path, 'terminal-record.json', );
writeFakeTerminalExec(fakeBinDir,);
/** Source CLI result when no PID mapping directory exists. */ const result = spawnSync( 'node', [ SOURCE_CLI_PATH, '--cwd', dir.path, '--extra-arguments', '--thinking high', 'fallback prompt', ], { encoding: 'utf8', env: { ...process.env, PATH: `${fakeBinDir}:${process.env.PATH ?? ''}`, PI_CODING_AGENT_DIR: dir.path, [FAKE_TERMINAL_RECORD_PATH_ENV]: recordPath, }, }, );
expect(result.status,).toBe(0,); expect(result.stderr,).toContain(SESSION_NOT_FOUND_WARNING,); expect(result.stdout.trim(),).toBe('{"resultForwarding":false}',);
/** Launch record written by detached fake terminal-exec. */ const record = await readDetachedRecord(recordPath,);
expect(record.cwd,).toBe(dir.path,); expect(record.spawnId,).toBe(FAKE_TERMINAL_SPAWN_ID_ABSENT,); expect(record.argv,).toEqual([ '--title=spawn-pi unlinked', '--', 'pi', '--thinking', 'high', 'fallback prompt', ],); }, },), ], },), ],},);