import { existsSync } from 'fs' import { join } from 'path' import { consoleContext } from './log' import { runProgram } from './run-program' import { createTestCase } from './testing/test-case' import { memoryStream } from './testing/test-streams' // Fast unit-suite coverage for program.ts: every command that does not actually // run a container (ls, validate, clean --cache, init, --file, unknown task). // The slow container-running cases (run, package, up/down) live in // `program.spec.ts` and run in the integration suite. // // shell() forwards args to commander via `[env.cwd, ...args]`, and commander's // default `from: 'node'` consumes the first two slots as node+script. So args // must start with a script-name placeholder (matching how production receives // process.argv); the real command starts at index 1. const HK = 'hammerkit' describe('program commands (fast)', () => { it('prints the bare version, so a script can read it', async () => { const t = createTestCase('cmd-version', {}) await t.setup(async (cwd, env) => { const out = memoryStream() env.stdout = out.stream env.console = consoleContext(out.stream) await expect(runProgram(env, [HK, '--version'], true)).rejects.toThrow() expect(out.read()).toMatch(/^\d+\.\d+\.\d+(-[\w.]+)?\n$/) }) }) it('ls lists tasks and services without running them', async () => { const t = createTestCase('cmd-ls', { '.hammerkit.yaml': { services: { api: { image: 'nginx', ports: [] } }, tasks: { build: { cmds: ['true'], description: 'build it', labels: { tier: 'backend' } }, }, }, }) await t.shell([HK, 'ls']) }) it('ls --filter narrows by label', async () => { const t = createTestCase('cmd-ls-filter', { '.hammerkit.yaml': { tasks: { build: { cmds: ['true'], labels: { app: 'web' } }, lint: { cmds: ['true'], labels: { app: 'tool' } }, }, }, }) await t.shell([HK, 'ls', '--filter', 'app=web']) }) it('validate returns cleanly for a healthy build file', async () => { const t = createTestCase('cmd-validate-ok', { '.hammerkit.yaml': { tasks: { build: { description: 'build it', cmds: ['true'] } }, }, }) await t.shell([HK, 'validate']) }) it('validate exits non-zero when a cycle is detected', async () => { const t = createTestCase('cmd-validate-cycle', { '.hammerkit.yaml': { tasks: { a: { description: 'a', cmds: ['true'], deps: ['b'] }, b: { description: 'b', cmds: ['true'], deps: ['a'] }, }, }, }) await expect(t.shell([HK, 'validate'])).rejects.toThrow('Detected errors') }) it('down stops services and never runs a task it selects', async () => { const t = createTestCase('cmd-down-no-tasks', { '.hammerkit.yaml': { tasks: { mark: { cmds: ["node -e \"require('fs').writeFileSync('marker', '')\""] } } }, }) await t.setup(async (cwd, env) => { await runProgram(env, [HK, 'down'], true) await runProgram(env, [HK, 'down', 'mark'], true) expect(existsSync(join(cwd, 'marker'))).toBe(false) }) }) it('up and down refuse a name that is no task or service', async () => { const t = createTestCase('cmd-up-unknown-name', { '.hammerkit.yaml': { services: { postgres: { image: 'postgres:16', ports: ['5432'] } } }, }) await t.setup(async (cwd, env) => { const out = memoryStream() env.stdout = out.stream env.console = consoleContext(out.stream) await expect(runProgram(env, [HK, 'up', 'postgress'], true)).rejects.toThrow('Up was not successful') await expect(runProgram(env, [HK, 'down', 'postgress'], true)).rejects.toThrow('Shutdown was not successful') expect(out.read()).toContain('no task or service named postgress, see hammerkit ls') }) }) it('up refuses names together with labels', async () => { const t = createTestCase('cmd-up-names-labels', { '.hammerkit.yaml': { services: { postgres: { image: 'postgres:16', ports: ['5432'] } } }, }) await t.setup(async (cwd, env) => { const out = memoryStream() env.stdout = out.stream env.console = consoleContext(out.stream) await expect(runProgram(env, [HK, 'up', 'postgres', '-f', 'app=api'], true)).rejects.toThrow( 'Up was not successful' ) expect(out.read()).toContain('name tasks and services or select them with --filter/--exclude, not both') }) }) it('clean --cache clears the backend cache (no container ops)', async () => { const t = createTestCase('cmd-clean-cache', { '.hammerkit.yaml': { tasks: { build: { cmds: ['true'] } }, }, }) await t.shell([HK, 'clean', '--cache']) }) it('graph serializes the build graph without running it', async () => { const t = createTestCase('cmd-graph', { '.hammerkit.yaml': { tasks: { a: { cmds: ['true'] }, b: { cmds: ['true'], deps: ['a'] }, }, }, }) await t.shell([HK, 'graph']) }) it('run --dry-run prints the plan without running', async () => { const t = createTestCase('cmd-dry-run', { '.hammerkit.yaml': { tasks: { build: { cmds: ['true'], src: ['input.txt'] } }, }, 'input.txt': 'x\n', }) await t.shell([HK, 'run', '--dry-run']) }) it('runs a task named mcp, the MCP server is remote mcp', async () => { const t = createTestCase('cmd-task-mcp', { '.hammerkit.yaml': { tasks: { mcp: { cmds: ['true'] } } }, }) await t.setup(async (cwd, env) => { const out = memoryStream() env.stdout = out.stream env.console = consoleContext(out.stream) await runProgram(env, [HK, 'mcp', '--dry-run'], true) expect(out.read()).toContain('mcp') }) }) it('writes nothing but the protocol to stdout for remote mcp', async () => { const t = createTestCase('cmd-remote-mcp-stdout', { '.hammerkit.yaml': { tasks: { a: { cmds: ['true'] } } }, 'build.yaml': { tasks: { b: { cmds: ['true'] } } }, }) await t.setup(async (cwd, env) => { const out = memoryStream() const err = memoryStream() env.stdout = out.stream env.stderr = err.stream env.console = consoleContext(out.stream) env.processEnvs = { ...env.processEnvs, HAMMERKIT_CONFIG: join(cwd, 'host-config.yaml') } await expect(runProgram(env, [HK, 'remote', 'mcp', '--build', 'payments'], true)).rejects.toThrow( /no server is registered/ ) expect(out.read()).toBe('') expect(err.read()).toContain('multiple hammerkit files') }) }) it('explain reports tasks without running them', async () => { const t = createTestCase('cmd-explain', { '.hammerkit.yaml': { tasks: { build: { cmds: ['true'], src: ['input.txt'] } }, }, 'input.txt': 'x\n', }) await t.shell([HK, 'explain']) }) it('explain --check fails while a task would be a cache miss', async () => { const t = createTestCase('cmd-explain-check', { '.hammerkit.yaml': { tasks: { build: { cmds: ['node -e 0'], src: ['input.txt'] } }, }, // unique content: the machine-wide default cache may already hold this task from an earlier run 'input.txt': `${Date.now()}-${Math.random()}\n`, }) await expect(t.shell([HK, 'explain', '--check'])).rejects.toThrow('would be a cache miss') }) it('explain --check passes when no task would be a cache miss', async () => { const t = createTestCase('cmd-explain-check-uncacheable', { '.hammerkit.yaml': { tasks: { build: { cmds: ['node -e 0'], cache: 'none' } }, }, }) await t.shell([HK, 'explain', '--check']) }) it('init writes a default .hammerkit.yaml when none exists', async () => { // empty fixture so getBuildFilename finds nothing; only the `init` command is registered then const t = createTestCase('cmd-init', {}) await t.shell([HK, 'init']) }) it('errors when the requested task does not exist', async () => { const t = createTestCase('cmd-unknown-task', { '.hammerkit.yaml': { tasks: { example: { cmds: ['true'] } } }, }) await expect(t.shell([HK, 'run', 'no-such-task'])).rejects.toThrow('No tasks found') }) it('--file selects a non-default build file', async () => { const t = createTestCase('cmd-file-arg', { 'custom.yaml': { tasks: { only: { description: 'only', cmds: ['true'] } }, }, }) await t.shell([HK, '--file', 'custom.yaml', 'ls']) }) })