UNPKG

@heroku/mcp-server

Version:
163 lines (162 loc) • 9.96 kB
import { expect } from 'chai'; import { parse } from 'shell-quote'; import { CommandBuilder } from './command-builder.js'; import { TOOL_COMMAND_MAP } from './tool-commands.js'; /** * Unit tests for the CommandBuilder class. * Some of these tests do not produce valid commands, but they are designed to test the CommandBuilder class. */ describe('CommandBuilder', () => { describe('constructor', () => { it('creates a new instance with the base command', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); expect(builder.build()).to.equal('apps'); }); }); describe('addFlags', () => { it('adds boolean flags correctly', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ all: true, json: true }); expect(builder.build()).to.equal('apps --all --json'); }); it('adds string flags with values correctly', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ team: 'my-team', space: 'my-space' }); expect(builder.build()).to.equal("apps --team='my-team' --space='my-space'"); }); it('handles mixed boolean and string flags', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ all: true, team: 'my-team', json: true }); expect(builder.build()).to.equal("apps --all --team='my-team' --json"); }); it('ignores undefined flag values', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ all: true, team: undefined, json: true }); expect(builder.build()).to.equal('apps --all --json'); }); it('ignores false boolean flags', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ all: false, json: true }); expect(builder.build()).to.equal('apps --json'); }); it('supports method chaining', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); const result = builder.addFlags({ all: true }); expect(result).to.equal(builder); }); it('rejects flag values containing line breaks', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); expect(() => builder.addFlags({ team: 'my-team\nsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/); expect(() => builder.addFlags({ team: 'my-team\rother' })).to.throw(/line breaks/); }); it('quotes flag values that contain spaces (only line breaks are rejected)', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); expect(() => builder.addFlags({ team: 'my team' })).to.not.throw(); expect(builder.build()).to.equal("apps --team='my team'"); }); it('POSIX-quotes apostrophes inside flag values', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ team: "it's" }); expect(builder.build()).to.equal("apps --team='it'\\''s'"); }); it('names the offending flag and the "flag" kind in the error message', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); expect(() => builder.addFlags({ team: 'my-team\nsecond-line' })).to.throw(/Invalid flag value for "team"/); }); }); describe('addPositionalArguments', () => { it('adds positional arguments correctly', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP); builder.addPositionalArguments({ new_name: 'new-app-name' }); expect(builder.build()).to.equal("apps:rename -- 'new-app-name'"); }); it('adds multiple positional arguments in order', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.TRANSFER_APP); builder.addPositionalArguments({ app: 'my-app', recipient: 'user@example.com' }); expect(builder.build()).to.equal("apps:transfer -- 'my-app' 'user@example.com'"); }); it('ignores undefined argument values', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP); builder.addPositionalArguments({ app: undefined, new_name: 'new-app-name' }); expect(builder.build()).to.equal("apps:rename -- 'new-app-name'"); }); it('supports method chaining', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP); const result = builder.addPositionalArguments({ new_name: 'new-app-name' }); expect(result).to.equal(builder); }); it('rejects positional argument values containing line breaks', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP); expect(() => builder.addPositionalArguments({ new_name: 'ok\nsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/); }); it('names the offending argument and the "argument" kind in the error message', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP); expect(() => builder.addPositionalArguments({ new_name: 'ok\nsecond-line' })).to.throw(/Invalid argument value for "new_name"/); }); }); describe('build', () => { it('combines flags and positional arguments correctly', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.CREATE_APP); builder.addFlags({ region: 'eu', team: 'my-team' }).addPositionalArguments({ app: 'my-new-app' }); expect(builder.build()).to.equal("apps:create --region='eu' --team='my-team' -- 'my-new-app'"); }); it('handles commands with no flags or arguments', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); expect(builder.build()).to.equal('apps'); }); it('handles commands with only flags', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS); builder.addFlags({ all: true, json: true }); expect(builder.build()).to.equal('apps --all --json'); }); it('handles commands with only positional arguments', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP); builder.addPositionalArguments({ new_name: 'new-app-name' }); expect(builder.build()).to.equal("apps:rename -- 'new-app-name'"); }); it('maintains flag and argument order across multiple calls', () => { const builder = new CommandBuilder(TOOL_COMMAND_MAP.CREATE_APP); builder.addFlags({ region: 'eu' }).addPositionalArguments({ app: 'my-app' }).addFlags({ team: 'my-team' }); expect(builder.build()).to.equal("apps:create --region='eu' --team='my-team' -- 'my-app'"); }); it('can never emit a command containing carriage returns or line feeds', () => { // Validation rejects CR/LF at add-time, so a value with a line break is never // stored and the build output is always free of them. This test depends on that // validation: it asserts the invalid add throws AND that the command built from // the remaining valid values contains no CR/LF. const builder = new CommandBuilder(TOOL_COMMAND_MAP.CREATE_APP); builder.addFlags({ region: 'eu' }); expect(() => builder.addFlags({ team: 'my-team\nsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/); expect(() => builder.addPositionalArguments({ app: 'my-app\rsecond-line' })).to.throw(/line breaks \(CR\/LF\) are not allowed/); const command = builder.build(); expect(command).to.not.match(/[\r\n]/); expect(command).to.equal("apps:create --region='eu'"); }); }); describe('heroku repl tokenization (shell-quote.parse)', () => { it('keeps a space-injected --confirm= in version as one flag token (W-23972762)', () => { const command = new CommandBuilder('pg:upgrade') .addFlags({ app: 'prod-db-app', version: '14 --confirm=prod-db-app' }) .build(); expect(command).to.equal("pg:upgrade --app='prod-db-app' --version='14 --confirm=prod-db-app'"); const tokens = parse(command).filter((token) => typeof token === 'string'); expect(tokens).to.deep.equal(['pg:upgrade', '--app=prod-db-app', '--version=14 --confirm=prod-db-app']); expect(tokens.some((token) => token.startsWith('--confirm='))).to.equal(false); }); it('does not expand or split $, backticks, |, ;, or # in flag values', () => { const payload = '14 $HOME `id` | cat ; echo hi # comment'; const command = new CommandBuilder('pg:upgrade').addFlags({ version: payload }).build(); const tokens = parse(command).filter((token) => typeof token === 'string'); expect(tokens).to.deep.equal(['pg:upgrade', `--version=${payload}`]); expect(tokens.some((token) => typeof token === 'object')).to.equal(false); }); it('round-trips apostrophes and keeps a positional value as one token', () => { const command = new CommandBuilder('apps:rename') .addFlags({ app: 'prod-db-app' }) .addPositionalArguments({ new_name: "it's a name --confirm=prod-db-app" }) .build(); const tokens = parse(command).filter((token) => typeof token === 'string'); expect(tokens).to.deep.equal(['apps:rename', '--app=prod-db-app', '--', "it's a name --confirm=prod-db-app"]); }); }); });