11import { afterEach , describe , expect , test } from "bun:test"
2- import { chmodSync , existsSync , mkdtempSync , mkdirSync , readFileSync , readdirSync , rmSync , writeFileSync } from "fs"
2+ import { chmodSync , existsSync , mkdtempSync , mkdirSync , readFileSync , readdirSync , rmSync , statSync , writeFileSync } from "fs"
33import { tmpdir } from "os"
4- import { join } from "path"
4+ import { dirname , join } from "path"
55import { spawnSync } from "child_process"
66
77const tempRoots : string [ ] = [ ]
8+ const tempLogDirs = new Set < string > ( )
89const scriptPath = join ( process . cwd ( ) , "bin" , "opencode-memory" )
910
1011function makeTempRoot ( ) : string {
@@ -18,17 +19,42 @@ function writeExecutable(filePath: string, content: string): void {
1819 chmodSync ( filePath , 0o755 )
1920}
2021
22+ function rememberLogDirFromPath ( logPath : string ) : void {
23+ tempLogDirs . add ( dirname ( logPath ) )
24+ }
25+
26+ function findRecentExtractLog ( startedAt : number ) : string {
27+ const logRoot = join ( "/tmp" , "opencode-claude-memory" )
28+ const projectDirs = readdirSync ( logRoot ) . map ( ( entry ) => join ( logRoot , entry ) )
29+
30+ const extractLogs = projectDirs . flatMap ( ( dir ) =>
31+ readdirSync ( dir )
32+ . filter ( ( name ) => name . startsWith ( "extract-" ) )
33+ . map ( ( name ) => join ( dir , name ) ) ,
34+ )
35+
36+ const recentLog = extractLogs . find ( ( logPath ) => statSync ( logPath ) . mtimeMs >= startedAt )
37+ expect ( recentLog ) . toBeDefined ( )
38+
39+ return recentLog ?? ""
40+ }
41+
2142afterEach ( ( ) => {
2243 while ( tempRoots . length > 0 ) {
2344 const root = tempRoots . pop ( )
2445 if ( root ) {
2546 rmSync ( root , { recursive : true , force : true } )
2647 }
2748 }
49+
50+ for ( const logDir of tempLogDirs ) {
51+ rmSync ( logDir , { recursive : true , force : true } )
52+ }
53+ tempLogDirs . clear ( )
2854} )
2955
3056describe ( "opencode-memory wrapper" , ( ) => {
31- test ( "normalizes TMPDIR before composing extraction log paths " , ( ) => {
57+ test ( "writes extraction logs under a stable per-project /tmp directory " , ( ) => {
3258 const root = makeTempRoot ( )
3359 const fakeBin = join ( root , "bin" )
3460 const homeDir = join ( root , "home" )
6086` ,
6187 )
6288
89+ const startedAt = Date . now ( )
6390 const result = spawnSync ( "bash" , [ scriptPath , "--help" ] , {
6491 cwd : root ,
6592 encoding : "utf-8" ,
@@ -82,7 +109,10 @@ exit 0
82109 expect ( logPathMatch ) . not . toBeNull ( )
83110
84111 const logPath = logPathMatch ?. [ 1 ] . trim ( ) ?? ""
85- expect ( logPath . startsWith ( join ( root , "tmp" , "opencode-memory-logs" , "extract-" ) ) ) . toBe ( true )
112+ rememberLogDirFromPath ( logPath )
113+ expect ( logPath ) . toMatch ( / ^ \/ t m p \/ o p e n c o d e - c l a u d e - m e m o r y \/ [ ^ / ] + \/ e x t r a c t - / )
114+ expect ( statSync ( logPath ) . mtimeMs ) . toBeGreaterThanOrEqual ( startedAt )
115+ expect ( logPath ) . toContain ( "/extract-" )
86116 expect ( existsSync ( logPath ) ) . toBe ( true )
87117 expect ( readFileSync ( logPath , "utf-8" ) ) . toContain ( "extraction ok" )
88118 } )
@@ -119,6 +149,7 @@ exit 0
119149` ,
120150 )
121151
152+ const startedAt = Date . now ( )
122153 const result = spawnSync ( "bash" , [ scriptPath , "--help" ] , {
123154 cwd : root ,
124155 encoding : "utf-8" ,
@@ -137,11 +168,8 @@ exit 0
137168 expect ( result . status ) . toBe ( 0 )
138169 expect ( result . stderr ) . toBe ( "" )
139170
140- const logDir = join ( root , "tmp" , "opencode-memory-logs" )
141- const logFiles = readdirSync ( logDir )
142- expect ( logFiles ) . toHaveLength ( 1 )
143-
144- const logPath = join ( logDir , logFiles [ 0 ] ?? "" )
171+ const logPath = findRecentExtractLog ( startedAt )
172+ rememberLogDirFromPath ( logPath )
145173 expect ( readFileSync ( logPath , "utf-8" ) ) . toContain ( "extraction ok" )
146174 } )
147175} )
0 commit comments