11import { afterEach , describe , expect , test } from "bun:test"
2- import { chmodSync , existsSync , mkdtempSync , mkdirSync , readFileSync , readdirSync , rmSync , statSync , writeFileSync } from "fs"
2+ import { chmodSync , existsSync , mkdtempSync , mkdirSync , readFileSync , readdirSync , rmSync , writeFileSync } from "fs"
33import { tmpdir } from "os"
4- import { dirname , join } from "path"
4+ import { join } from "path"
55import { spawnSync } from "child_process"
66
77const tempRoots : string [ ] = [ ]
8- const tempLogDirs = new Set < string > ( )
98const scriptPath = join ( process . cwd ( ) , "bin" , "opencode-memory" )
109
1110function makeTempRoot ( ) : string {
@@ -19,42 +18,17 @@ function writeExecutable(filePath: string, content: string): void {
1918 chmodSync ( filePath , 0o755 )
2019}
2120
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-
4221afterEach ( ( ) => {
4322 while ( tempRoots . length > 0 ) {
4423 const root = tempRoots . pop ( )
4524 if ( root ) {
4625 rmSync ( root , { recursive : true , force : true } )
4726 }
4827 }
49-
50- for ( const logDir of tempLogDirs ) {
51- rmSync ( logDir , { recursive : true , force : true } )
52- }
53- tempLogDirs . clear ( )
5428} )
5529
5630describe ( "opencode-memory wrapper" , ( ) => {
57- test ( "writes extraction logs under a stable per-project /tmp directory " , ( ) => {
31+ test ( "normalizes TMPDIR before composing extraction log paths " , ( ) => {
5832 const root = makeTempRoot ( )
5933 const fakeBin = join ( root , "bin" )
6034 const homeDir = join ( root , "home" )
8660` ,
8761 )
8862
89- const startedAt = Date . now ( )
9063 const result = spawnSync ( "bash" , [ scriptPath , "--help" ] , {
9164 cwd : root ,
9265 encoding : "utf-8" ,
@@ -109,10 +82,7 @@ exit 0
10982 expect ( logPathMatch ) . not . toBeNull ( )
11083
11184 const logPath = logPathMatch ?. [ 1 ] . trim ( ) ?? ""
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-" )
85+ expect ( logPath . startsWith ( join ( root , "tmp" , "opencode-memory-logs" , "extract-" ) ) ) . toBe ( true )
11686 expect ( existsSync ( logPath ) ) . toBe ( true )
11787 expect ( readFileSync ( logPath , "utf-8" ) ) . toContain ( "extraction ok" )
11888 } )
@@ -149,7 +119,6 @@ exit 0
149119` ,
150120 )
151121
152- const startedAt = Date . now ( )
153122 const result = spawnSync ( "bash" , [ scriptPath , "--help" ] , {
154123 cwd : root ,
155124 encoding : "utf-8" ,
@@ -168,8 +137,11 @@ exit 0
168137 expect ( result . status ) . toBe ( 0 )
169138 expect ( result . stderr ) . toBe ( "" )
170139
171- const logPath = findRecentExtractLog ( startedAt )
172- rememberLogDirFromPath ( logPath )
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 ] ?? "" )
173145 expect ( readFileSync ( logPath , "utf-8" ) ) . toContain ( "extraction ok" )
174146 } )
175147} )
0 commit comments