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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233import { spawn, } from 'node:child_process';import { once, } from 'node:events';import { mkdir, mkdtemp, readFile, readdir, rm, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, } from 'node:path';import { text, } from 'node:stream/consumers';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
/** Built package entry used by the subprocess probe. */const BUILT_INDEX_PATH = join( import.meta.dirname, '..', 'dist', 'final', 'node', 'index.mjs',);
/** Message logged before the probe awaits anything from the logger package. */const STARTUP_MESSAGE = 'startup before consumer init await';
/** JSONL fragment proving the startup message reached the file sink. */const STARTUP_MESSAGE_FRAGMENT = `"message":${JSON.stringify(STARTUP_MESSAGE,)}`;
/** Debug message used by the process-stream startup probe. */const DEBUG_MESSAGE = 'debug before consumer init await';
/** Async-disposable temporary project for file-sink startup probes. */type TempProject = { readonly path: string; readonly scriptPath: string; readonly [Symbol.asyncDispose]: () => Promise<void>;};
/** Result captured from the probe subprocess. */type ProbeResult = { readonly exitCode: number; readonly stderr: string; readonly stdout: string;};
/** Builds a throwaway project root with `node_modules` so the file sink chooses an isolated `node_modules/.monochromatic` log directory. @param logLine - Logger call that the generated probe should execute. @returns Temporary project handle removed by `await using`. */async function createTempProject( { logLine, }: { readonly logLine: string; },): Promise<TempProject> { const path = await mkdtemp(join(tmpdir(), 'logger-startup-',),); await mkdir( join( path, 'node_modules', ), { recursive: true, }, );
/** Probe script that never imports or awaits `initPromise`. */ const scriptPath = join( path, 'probe.ts', ); /** Probe source assembled as separate lines so generated syntax stays readable. */ const script = [ `import { logger, } from ${JSON.stringify(BUILT_INDEX_PATH,)};`, '', logLine, 'await logger.flush();', '', ].join('\n',); await writeFile( scriptPath, script, );
return { path, scriptPath, async [Symbol.asyncDispose](): Promise<void> { await rm(path, { recursive: true, force: true, },); }, };}
/** Runs a probe script in its temporary project root. @param cwd - Project root used as `process.cwd()` by the file sink. @param env - Environment overrides applied to the probe process. @param scriptPath - Absolute path to the probe script. @returns Captured stdout, stderr, and exit code. */async function runProbe( { cwd, env = {}, scriptPath, }: { readonly cwd: string; readonly env?: Readonly<Record<string, string>>; readonly scriptPath: string; },): Promise<ProbeResult> { // Default stdio is 'pipe', which yields a ChildProcessWithoutNullStreams so // stdout/stderr are non-null Readables. The probe is a `.ts` file, so it runs // through the same Node runtime used by mise test tasks. const subprocess = spawn( 'node', [scriptPath,], { cwd, env: { ...process.env, ...env, }, }, );
const [ stdout, stderr, ] = await Promise.all([ text(subprocess.stdout,), text(subprocess.stderr,), once(subprocess, 'close',), ],);
return { // After 'close' the exit code is populated; null means killed by signal. exitCode: subprocess.exitCode ?? (-1), stderr, stdout, };}
/** Reads the single JSONL file created by the probe's file sink. @param projectPath - Temporary project root containing `node_modules`. @returns Log file contents. */async function readOnlyLogContent({ projectPath, }: { readonly projectPath: string; },): Promise<string> { const logDir = join( projectPath, 'node_modules', '.monochromatic', ); const logFiles = await readdir(logDir,); expect(logFiles.length,) .toBe(1,);
/** Single log file emitted by one probe process. */ const [logFile,] = logFiles; if (logFile === undefined) throw new Error('Expected logger to create a log file.',);
return await readFile( join( logDir, logFile, ), 'utf8', );}
await describe({ name: 'logger startup initialization', children: [ it({ name: 'delivers startup records to file sink without awaiting initPromise', fn: async () => { await using project = await createTempProject({ logLine: `logger.info(${JSON.stringify(STARTUP_MESSAGE,)},);`, },);
const result = await runProbe({ cwd: project.path, scriptPath: project.scriptPath, },); expect(result.exitCode,) .toBe(0,); expect(result.stderr,) .toBe('',); expect(result.stdout,) .toContain(STARTUP_MESSAGE,);
const content = await readOnlyLogContent({ projectPath: project.path, },); expect(content,) .toContain(STARTUP_MESSAGE_FRAGMENT,); }, },),
it({ name: 'writes debug startup records to stderr when process stderr is available', fn: async () => { await using project = await createTempProject({ logLine: `logger.debug(${JSON.stringify(DEBUG_MESSAGE,)},);`, },);
const result = await runProbe({ cwd: project.path, env: { MONOCHROMATIC_VERBOSE: 'true', }, scriptPath: project.scriptPath, },); expect(result.exitCode,) .toBe(0,); expect(result.stdout,) .toBe('',); expect(result.stderr,) .toContain(DEBUG_MESSAGE,); }, },), ],},);