1515 */
1616
1717import { existsSync , mkdirSync , rmSync , writeFileSync } from 'node:fs'
18+ import os from 'node:os'
1819import path from 'node:path'
1920
2021import { afterAll , beforeAll , describe , expect , it } from 'vitest'
@@ -37,8 +38,19 @@ describe('project context utilities', () => {
3738 const packageManagerField = path . join ( testRoot , 'pm-field-project' )
3839 const noLockProject = path . join ( testRoot , 'no-lock-project' )
3940 const nestedProject = path . join ( testRoot , 'nested' , 'deep' , 'project' )
41+ // Use a temp directory outside any project tree to test non-project detection.
42+ // This ensures no package.json exists in any parent directory.
43+ const emptyDir = path . join (
44+ os . tmpdir ( ) ,
45+ `socket-cli-test-empty-${ Date . now ( ) } ` ,
46+ 'deeply' ,
47+ 'nested' ,
48+ 'empty' ,
49+ )
4050
4151 beforeAll ( ( ) => {
52+ // Create empty directory (no package.json) for testing non-project detection.
53+ mkdirSync ( emptyDir , { recursive : true } )
4254 // Create npm project.
4355 mkdirSync ( npmProject , { recursive : true } )
4456 writeFileSync (
@@ -115,6 +127,11 @@ describe('project context utilities', () => {
115127 if ( existsSync ( testRoot ) ) {
116128 rmSync ( testRoot , { recursive : true , force : true } )
117129 }
130+ // Clean up the temp emptyDir base (the unique timestamped directory).
131+ const emptyDirBase = path . dirname ( path . dirname ( path . dirname ( emptyDir ) ) )
132+ if ( existsSync ( emptyDirBase ) ) {
133+ rmSync ( emptyDirBase , { recursive : true , force : true } )
134+ }
118135 } )
119136
120137 describe ( 'detectPackageManager' , ( ) => {
@@ -215,7 +232,7 @@ describe('project context utilities', () => {
215232 } )
216233
217234 it ( 'returns null when no package.json found' , async ( ) => {
218- const result = await findProjectRoot ( '/tmp' )
235+ const result = await findProjectRoot ( emptyDir )
219236
220237 expect ( result ) . toBeNull ( )
221238 } )
@@ -247,7 +264,7 @@ describe('project context utilities', () => {
247264 } )
248265
249266 it ( 'returns null for non-project directory' , async ( ) => {
250- const context = await getProjectContext ( '/tmp' )
267+ const context = await getProjectContext ( emptyDir )
251268
252269 expect ( context ) . toBeNull ( )
253270 } )
0 commit comments