// effectiveArgs backport (#44): launch args must come from
// manifest.defaults.args (the canonical install-time setting written by
// `pgh app install`/`adopt`) when manifest.args is empty, with ${VAR}
// expansion — mirroring effectiveEnv. The old code read only
// manifest.args (always []) so apps launched with no args (wrong port /
// storage → RocksDB lock collision crash-loop).
//
// Tests use app names that aren't in ~/.config/pgh/apps.json so there's
// no user override (precedence's top tier is exercised via the manifest
// tiers; user-override is filesystem-coupled and covered by integration).
import test from 'brittle'
import process from '#node:process'
import { effectiveArgs } from '../lib/app-state.js'
test('falls back to manifest.defaults.args when manifest.args is empty', (t) => {
const manifest = {
args: [],
defaults: { args: ['serve', '5006', '--network'] }
}
t.alike(effectiveArgs('pgh-test-no-such-app-A', manifest),
['serve', '5006', '--network'], 'reads defaults.args, not the empty manifest.args')
})
test('manifest.args takes precedence over defaults.args', (t) => {
const manifest = {
args: ['serve', '4040'],
defaults: { args: ['serve', '5006'] }
}
t.alike(effectiveArgs('pgh-test-no-such-app-B', manifest),
['serve', '4040'], 'non-empty manifest.args wins')
})
test('expands ${VAR} from process.env and defaults.env', (t) => {
const prev = process.env.PGH_TEST_HOMEISH
process.env.PGH_TEST_HOMEISH = '/tmp/homeish'
try {
const manifest = {
args: [],
defaults: {
args: ['--storage=${PGH_TEST_HOMEISH}/store', '--name=${PGH_TEST_DEFENV}'],
env: { PGH_TEST_DEFENV: 'frommanifest' }
}
}
t.alike(effectiveArgs('pgh-test-no-such-app-C', manifest),
['--storage=/tmp/homeish/store', '--name=frommanifest'],
'${VAR} resolves from process.env and manifest.defaults.env')
} finally {
if (prev === undefined) process.env.PGH_TEST_HOMEISH = ''
else process.env.PGH_TEST_HOMEISH = prev
}
})
test('no args anywhere → empty list', (t) => {
t.alike(effectiveArgs('pgh-test-no-such-app-D', { args: [], defaults: {} }), [])
t.alike(effectiveArgs('pgh-test-no-such-app-E', {}), [])
})