devserver-launch.test.ts
1 import assert from 'node:assert/strict' 2 import fs from 'fs' 3 import os from 'os' 4 import path from 'path' 5 import { afterEach, describe, it } from 'node:test' 6 import { resolveDevServerLaunchDir } from '@/lib/server/runtime/devserver-launch' 7 8 const tempDirs: string[] = [] 9 10 afterEach(() => { 11 while (tempDirs.length) { 12 const dir = tempDirs.pop() 13 if (!dir) continue 14 fs.rmSync(dir, { recursive: true, force: true }) 15 } 16 }) 17 18 function makeTempDir(): string { 19 const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'swarmclaw-devserver-launch-')) 20 tempDirs.push(dir) 21 return dir 22 } 23 24 describe('resolveDevServerLaunchDir', () => { 25 it('resolves the repo root when launched from src/app', () => { 26 const repoRoot = path.resolve(process.cwd()) 27 const nested = path.join(repoRoot, 'src', 'app') 28 const result = resolveDevServerLaunchDir(nested) 29 assert.equal(result.launchDir, repoRoot) 30 assert.equal(result.packageRoot, repoRoot) 31 assert.equal(result.framework, 'next') 32 }) 33 34 it('returns the nearest npm package root for nested package folders', () => { 35 const root = makeTempDir() 36 const nested = path.join(root, 'src', 'feature') 37 fs.mkdirSync(nested, { recursive: true }) 38 fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ 39 name: 'fixture', 40 scripts: { dev: 'vite' }, 41 devDependencies: { vite: '^6.0.0' }, 42 })) 43 44 const result = resolveDevServerLaunchDir(nested) 45 assert.equal(result.launchDir, root) 46 assert.equal(result.packageRoot, root) 47 assert.equal(result.framework, 'npm') 48 }) 49 50 it('falls back to the input directory when no package root exists', () => { 51 const root = makeTempDir() 52 const nested = path.join(root, 'plain', 'folder') 53 fs.mkdirSync(nested, { recursive: true }) 54 55 const result = resolveDevServerLaunchDir(nested) 56 assert.equal(result.launchDir, nested) 57 assert.equal(result.packageRoot, null) 58 assert.equal(result.framework, 'unknown') 59 }) 60 })