import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from 'vitest'; import { Command } from 'commander'; import { buildPiSkillArgs, piForceSkillNames, registerRuntimeLaunchers, type RuntimeLaunchHandler, } from './launch.js'; /** * Tests for the commander wiring between `mosaic ` / `mosaic yolo ` * subcommands and the internal `launchRuntime` dispatcher. * * Regression target: see mosaicstack/stack#454 — before the fix, `mosaic yolo claude` * passed the literal string "claude" as an excess positional argument to the * underlying CLI, which Claude Code then interpreted as the first user message. * * The bug existed because Commander.js includes declared positional arguments * (here ``) in `cmd.args` alongside any true excess args. The action * handler must slice them off before forwarding. */ function buildProgram(handler: RuntimeLaunchHandler): Command { const program = new Command(); program.exitOverride(); // prevent process.exit on parse errors registerRuntimeLaunchers(program, handler); return program; } const fakeSkills = ['--skill', '/skills/test-driven-development', '--skill', '/skills/pdf']; const fakeForced = ['--skill', '/skills/mosaic-tools']; // `process.exit` returns `never`, so vi.spyOn demands a replacement with the // same signature. We throw from the mock to short-circuit into test-land. const exitThrows = (): never => { throw new Error('process.exit called'); }; describe('registerRuntimeLaunchers — non-yolo subcommands', () => { let mockExit: MockInstance; beforeEach(() => { // process.exit is called when the yolo action rejects an invalid runtime. // Stub it so the assertion catches the rejection instead of terminating // the test runner. mockExit = vi.spyOn(process, 'exit').mockImplementation(exitThrows); }); afterEach(() => { mockExit.mockRestore(); }); it.each(['claude', 'codex', 'opencode', 'pi'] as const)( 'forwards %s with empty extraArgs and yolo=false', (runtime) => { const handler = vi.fn(); const program = buildProgram(handler); program.parse(['node', 'mosaic', runtime]); expect(handler).toHaveBeenCalledTimes(1); expect(handler).toHaveBeenCalledWith(runtime, [], false); }, ); it('forwards excess args after a non-yolo runtime subcommand', () => { const handler = vi.fn(); const program = buildProgram(handler); program.parse(['node', 'mosaic', 'claude', '--print', 'hello']); expect(handler).toHaveBeenCalledWith('claude', ['--print', 'hello'], false); }); }); describe('buildPiSkillArgs', () => { it('disables auto-discovery but force-loads fleet-critical skills by default', () => { expect(buildPiSkillArgs([], {}, fakeSkills, fakeForced)).toEqual([ '--no-skills', '--skill', '/skills/mosaic-tools', ]); }); it('ignores _runtimeArgs (user --skill flags reach Pi via the launch handler, not here)', () => { expect(buildPiSkillArgs(['--skill', '/tmp/custom'], {}, fakeSkills, fakeForced)).toEqual([ '--no-skills', '--skill', '/skills/mosaic-tools', ]); }); it('emits only --no-skills when no forced skills are present on disk', () => { expect(buildPiSkillArgs([], {}, fakeSkills, [])).toEqual(['--no-skills']); }); it('all-skills mode merges the forced set in without duplicating discovered skills', () => { expect(buildPiSkillArgs([], { MOSAIC_PI_SKILL_MODE: 'all' }, fakeSkills, fakeForced)).toEqual([ '--no-skills', '--skill', '/skills/test-driven-development', '--skill', '/skills/pdf', '--skill', '/skills/mosaic-tools', ]); }); it('all-skills mode does not double-load a forced skill already discovered', () => { expect( buildPiSkillArgs([], { MOSAIC_PI_SKILL_MODE: 'all' }, fakeSkills, ['--skill', '/skills/pdf']), ).toEqual([ '--no-skills', '--skill', '/skills/test-driven-development', '--skill', '/skills/pdf', ]); }); it('force-loads fleet skills even under native Pi discovery', () => { expect( buildPiSkillArgs([], { MOSAIC_PI_SKILL_MODE: 'discover' }, fakeSkills, fakeForced), ).toEqual(['--skill', '/skills/mosaic-tools']); }); }); describe('piForceSkillNames', () => { it('defaults to mosaic-tools when MOSAIC_PI_FORCE_SKILLS is unset', () => { expect(piForceSkillNames({})).toEqual(['mosaic-tools']); }); it('treats an empty string as "disable force-loading" (distinct from unset)', () => { expect(piForceSkillNames({ MOSAIC_PI_FORCE_SKILLS: '' })).toEqual([]); }); it('parses a colon list, trimming blanks and whitespace', () => { expect(piForceSkillNames({ MOSAIC_PI_FORCE_SKILLS: 'mosaic-tools: mosaic-gitea ::' })).toEqual([ 'mosaic-tools', 'mosaic-gitea', ]); }); }); describe('registerRuntimeLaunchers — yolo ', () => { let mockExit: MockInstance; let mockError: MockInstance; beforeEach(() => { mockExit = vi.spyOn(process, 'exit').mockImplementation(exitThrows); mockError = vi.spyOn(console, 'error').mockImplementation(() => {}); }); afterEach(() => { mockExit.mockRestore(); mockError.mockRestore(); }); it.each(['claude', 'codex', 'opencode', 'pi'] as const)( 'does NOT pass the runtime name as an extra arg (regression #454) for yolo %s', (runtime) => { const handler = vi.fn(); const program = buildProgram(handler); program.parse(['node', 'mosaic', 'yolo', runtime]); expect(handler).toHaveBeenCalledTimes(1); // The critical assertion: extraArgs must be empty, not [runtime]. // Before the fix, cmd.args was [runtime] and the runtime name leaked // through to the underlying CLI as an initial positional argument. expect(handler).toHaveBeenCalledWith(runtime, [], true); }, ); it('forwards true excess args after a yolo runtime', () => { const handler = vi.fn(); const program = buildProgram(handler); program.parse(['node', 'mosaic', 'yolo', 'claude', '--print', 'hi']); expect(handler).toHaveBeenCalledWith('claude', ['--print', 'hi'], true); }); it('rejects an unknown runtime under yolo without invoking the handler', () => { const handler = vi.fn(); const program = buildProgram(handler); expect(() => program.parse(['node', 'mosaic', 'yolo', 'bogus'])).toThrow('process.exit called'); expect(handler).not.toHaveBeenCalled(); expect(mockExit).toHaveBeenCalledWith(1); }); });