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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323import { mkdir, mkdtemp, readFile, rm, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { dirname, join,} from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { memoryPathOf, readRememberedActivityWord,} from '../dist/final/node/statusline.mjs';
/** Throwaway home directory that removes itself at the end of an `await using` scope.
Tests pass it as the explicit `home` because sibling tests run concurrently and share `process.env`. */type TempHome = { /** Absolute path to the throwaway home, which also holds the transcripts. */ readonly path: string; /** Removes the directory. */ readonly [Symbol.asyncDispose]: () => Promise<void>;};
/** Creates a throwaway home.
@returns disposable home handle
@example ```ts await using home = await makeHome(); ``` */async function makeHome(): Promise<TempHome> { /** Throwaway directory. */ const path = await mkdtemp(join( tmpdir(), 'claude-statusline-memory-', ),);
return { path, [Symbol.asyncDispose]: async function removeHome(): Promise<void> { await rm( path, { recursive: true, force: true, }, ); }, };}
/** Writes a transcript holding one assistant text entry, or one user entry when there is no prose.
@param home - throwaway home directory
@param sessionId - transcript file name without extension
@param text - assistant prose; omitted for a transcript without assistant entries
@returns transcript path
@example ```ts await writeTranscript({ home: '/tmp/h', sessionId: 'a', text: 'Compiling' }); ``` */async function writeTranscript({ home, sessionId, text,}: Readonly<{ home: string; sessionId: string; text?: string;}>,): Promise<string> { /** Transcript path fixture. */ const transcriptPath = join( home, `${sessionId}.jsonl`, ); /** Entry written for the transcript. */ const entry = text === undefined ? { type: 'user', } : { type: 'assistant', message: { content: [{ type: 'text', text, },], }, }; await writeFile( transcriptPath, `${JSON.stringify(entry,)}\n`, ); return transcriptPath;}
await describe({ name: readRememberedActivityWord.name, children: [ it({ name: 'returns and persists a freshly extracted word', fn: async function testPersists(): Promise<void> { await using home = await makeHome(); /** Transcript with a gerund. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Now compiling', },);
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('Compiling',); expect(await readFile( memoryPathOf({ transcriptPath, home: home.path, },), 'utf8', ),).toBe('Compiling\n',); }, },), it({ name: 'falls back to the earlier word when the tail has no gerund', fn: async function testFallsBack(): Promise<void> { await using home = await makeHome(); await readRememberedActivityWord({ transcriptPath: await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Now compiling', },), home: home.path, },); /** Same session whose tail now holds no gerund. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Done here', },);
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('Compiling',); }, },), it({ name: 'replaces the remembered word when a new one is extracted', fn: async function testReplaces(): Promise<void> { await using home = await makeHome(); await readRememberedActivityWord({ transcriptPath: await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Now compiling', },), home: home.path, },); await readRememberedActivityWord({ transcriptPath: await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Now testing', },), home: home.path, },); /** Same session whose tail now holds no assistant prose. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-a', },);
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('Testing',); }, },), it({ name: 'returns empty when the session never produced a word', fn: async function testNeverProduced(): Promise<void> { await using home = await makeHome(); /** Transcript without any gerund. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-a', },);
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('',); }, },), it({ name: 'keeps sessions apart', fn: async function testSessionsApart(): Promise<void> { await using home = await makeHome(); await readRememberedActivityWord({ transcriptPath: await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Now compiling', },), home: home.path, },); /** Different session without a gerund. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-b', },);
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('',); }, },), it({ name: 'ignores a memory file that is not a single extractor word', fn: async function testCorruptMemory(): Promise<void> { await using home = await makeHome(); /** Transcript without any gerund. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-a', },); /** Memory file location of the session. */ const memoryPath = memoryPathOf({ transcriptPath, home: home.path, },); await mkdir( dirname(memoryPath,), { recursive: true, }, ); await writeFile( memoryPath, '\u001B[31mCompiling\n', );
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('',); }, },), it({ name: 'still returns the fresh word when the memory cannot be written', fn: async function testUnwritableMemory(): Promise<void> { await using home = await makeHome(); /** Transcript with a gerund. */ const transcriptPath = await writeTranscript({ home: home.path, sessionId: 'session-a', text: 'Now compiling', },); // A file where the memory directory belongs makes the directory creation fail. await mkdir(join( home.path, '.claude', ),); await writeFile( join( home.path, '.claude', 'statusline-activity', ), '', );
expect(await readRememberedActivityWord({ transcriptPath, home: home.path, },),).toBe('Compiling',); }, },), ],},);