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.
Monochromatic package claude-code-plugin source src handler claude-spawn session-finder.unit.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260import { mkdirSync, writeFileSync, utimesSync,} from 'node:fs';import { mkdtemp, rm,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, } from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { byPidDir, type Environment,} from './paths.ts';import { findByMostRecent, findByProcessTree, findCallingSession, SESSION_NOT_FOUND,} from './session-finder.ts';
/** Older fixture modification time. */const OLDER_MTIME = new Date(1_000,);
/** Newer fixture modification time. */const NEWER_MTIME = new Date(2_000,);
/** Temporary HOME handle that removes itself at the end of an `await using` scope. */type TempHome = { /** Absolute path to temporary HOME root. */ readonly path: string; /** Removes temporary HOME root recursively. */ readonly [Symbol.asyncDispose]: () => Promise<void>;};
/** Creates an isolated HOME directory for spawn-claude session-finder tests. @returns disposable temporary HOME handle @example ```ts await using home = await makeTempHome(); ``` */async function makeTempHome(): Promise<TempHome> { /** Temporary HOME root path. */ const path = await mkdtemp(join( tmpdir(), 'spawn-claude-session-finder-', ),);
return { path, [Symbol.asyncDispose]: async function cleanup(): Promise<void> { await rm( path, { force: true, recursive: true, }, ); }, };}
/** Builds environment override for a temporary HOME. @param home - temporary HOME path @returns environment override used by path helpers @example ```ts testEnv('/tmp/home'); ``` */function testEnv(home: string,): Environment { return { HOME: home, };}
/** Writes mapping fixture directly to PID mapping directory. @param env - environment controlling mapping directory @param pid - process id filename to write @param sessionId - session id stored in mapping @param mtime - file modification time to apply @example ```ts writeMappingFixture({ env, pid: 123, sessionId: 's', mtime: NEWER_MTIME }); ``` */function writeMappingFixture( { env, pid, sessionId, mtime, }: { readonly env: Environment; readonly pid: number; readonly sessionId: string; readonly mtime: Date; },): void { mkdirSync( byPidDir(env,), { recursive: true, }, ); /** Mapping fixture path. */ const path = join( byPidDir(env,), String(pid,), ); writeFileSync( path, JSON.stringify({ sessionId, transcriptPath: `/tmp/${sessionId}.jsonl`, },), ); utimesSync( path, mtime, mtime, );}
await describe({ name: '', concurrency: 1, children: [ describe({ name: findByMostRecent.name, children: [ it({ name: 'returns newest readable mapping from env-injected HOME', fn: async function testNewestMapping(): Promise<void> { await using home = await makeTempHome(); /** Environment override for this test. */ const env = testEnv(home.path,);
writeMappingFixture({ env, pid: 101, sessionId: 'older-session', mtime: OLDER_MTIME, },); writeMappingFixture({ env, pid: 202, sessionId: 'newer-session', mtime: NEWER_MTIME, },);
/** Mapping resolved by newest-file fallback. */ const mapping = await findByMostRecent(env,); if (mapping === SESSION_NOT_FOUND) throw new Error('expected newest mapping',); expect(mapping.sessionId,).toBe('newer-session',); }, },), ], },), describe({ name: findByProcessTree.name, children: [ it({ name: 'uses direct parent process mapping from env-injected HOME', fn: async function testProcessTreeMapping(): Promise<void> { await using home = await makeTempHome(); /** Environment override for this test. */ const env = testEnv(home.path,);
writeMappingFixture({ env, pid: process.ppid, sessionId: 'parent-session', mtime: NEWER_MTIME, },);
/** Mapping resolved by process-tree lookup. */ const mapping = await findByProcessTree(env,); if (mapping === SESSION_NOT_FOUND) throw new Error('expected process-tree mapping',); expect(mapping.sessionId,).toBe('parent-session',); }, },), ], },), describe({ name: findCallingSession.name, children: [ it({ name: 'falls back to newest mapping when process tree has no env mapping', fn: async function testFindCallingSessionFallback(): Promise<void> { await using home = await makeTempHome(); /** Environment override for this test. */ const env = testEnv(home.path,);
writeMappingFixture({ env, pid: 303, sessionId: 'fallback-session', mtime: NEWER_MTIME, },);
/** Mapping resolved by full tree-then-newest lookup. */ const mapping = await findCallingSession(env,); if (mapping === SESSION_NOT_FOUND) throw new Error('expected fallback mapping',); expect(mapping.sessionId,).toBe('fallback-session',); }, },), ], },), ],},);