import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { delimiter, join } from 'node:path' import { afterEach, describe, expect, it } from 'bun:test' import type { Action } from '@stacksjs/enums' import { actionNodePath, developmentConditionForProject, developmentWatchFlag, publishedActionCandidates, runActionSequence } from '../src/helpers/utils' const roots: string[] = [] afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) }) function projectRoot(): string { const root = mkdtempSync(join(tmpdir(), 'stacks-action-condition-')) roots.push(root) return root } describe('developmentConditionForProject', () => { it('enables source exports for a vendored workspace app', () => { const root = projectRoot() mkdirSync(join(root, 'storage/framework/core'), { recursive: true }) mkdirSync(join(root, 'node_modules/@stacksjs/env/src'), { recursive: true }) writeFileSync(join(root, 'node_modules/@stacksjs/env/src/index.ts'), '') expect(developmentConditionForProject(root)).toBe('--conditions=development') }) it('selects development exports when passed as one spawn argv token', () => { const root = projectRoot() mkdirSync(join(root, 'storage/framework/core'), { recursive: true }) mkdirSync(join(root, 'node_modules/@stacksjs/env/src'), { recursive: true }) writeFileSync(join(root, 'node_modules/@stacksjs/env/src/index.ts'), '') const dependency = join(root, 'node_modules/condition-witness') mkdirSync(dependency, { recursive: true }) writeFileSync(join(dependency, 'package.json'), JSON.stringify({ name: 'condition-witness', type: 'module', exports: { development: './development.mjs', default: './default.mjs' }, })) writeFileSync(join(dependency, 'development.mjs'), 'export default "development"') writeFileSync(join(dependency, 'default.mjs'), 'export default "default"') writeFileSync(join(root, 'entry.mjs'), 'import value from "condition-witness"; console.log(value)') const child = Bun.spawnSync([process.execPath, developmentConditionForProject(root), join(root, 'entry.mjs')], { cwd: root, stdout: 'pipe', stderr: 'pipe', }) expect(child.exitCode, child.stderr.toString()).toBe(0) expect(child.stdout.toString().trim()).toBe('development') }) it('leaves published dist-only installs unchanged', () => { const root = projectRoot() mkdirSync(join(root, 'storage/framework/core'), { recursive: true }) expect(developmentConditionForProject(root)).toBe('') }) }) describe('developmentWatchFlag', () => { it('lets STX dashboard processes own their HMR lifecycle', () => { expect(developmentWatchFlag('dev/dashboard' as Action)).toBe('') expect(developmentWatchFlag('dev/desktop' as Action)).toBe('') }) it('keeps process watch mode for other development actions', () => { expect(developmentWatchFlag('dev/api' as Action)).toBe('--watch') }) }) describe('publishedActionCandidates', () => { it('resolves flat, legacy nested, and source package layouts', () => { expect(publishedActionCandidates('bump', '/app/node_modules/@stacksjs/actions')).toEqual([ '/app/node_modules/@stacksjs/actions/dist/bump.js', '/app/node_modules/@stacksjs/actions/dist/src/bump.js', '/app/node_modules/@stacksjs/actions/src/bump.ts', ]) }) }) describe('actionNodePath', () => { it('keeps installed dependencies ahead of Pantry fallbacks', () => { expect(actionNodePath('/app')).toBe([ join('/app', 'node_modules'), join('/app', 'pantry'), ].join(delimiter)) }) it('preserves existing lookups without letting Pantry shadow node_modules', () => { const pantry = join('/app', 'pantry') const existing = [pantry, '/shared/packages', pantry].join(delimiter) expect(actionNodePath('/app', existing).split(delimiter)).toEqual([ join('/app', 'node_modules'), '/shared/packages', pantry, ]) }) }) describe('runActionSequence', () => { it('runs actions in order through the canonical resolver', async () => { const calls: string[] = [] const options = { cwd: '/app', bump: 'patch' } const result = await runActionSequence(['generate/library-entries', 'bump'], options, async (action, received) => { calls.push(action) expect(received).toBe(options) return { isErr: false, value: action } as any }) expect(calls).toEqual(['generate/library-entries', 'bump']) expect(result.value).toBe('bump') }) it('stops after the first failed action', async () => { const calls: string[] = [] const result = await runActionSequence(['generate/library-entries', 'bump'], undefined, async (action) => { calls.push(action) return action === 'generate/library-entries' ? { isErr: true, error: 'failed' } as any : { isErr: false, value: action } as any }) expect(calls).toEqual(['generate/library-entries']) expect(result.isErr).toBe(true) }) })