@heroku/mcp-server
Version:
Heroku Platform MCP Server
595 lines (594 loc) • 28.3 kB
JavaScript
import { EventEmitter } from 'node:events';
import { expect } from 'chai';
import sinon from 'sinon';
import { HerokuREPL } from './heroku-cli-repl.js';
import * as pjson from '../../package.json' with { type: 'json' };
const VERSION = pjson.default.version;
describe('HerokuREPL', () => {
let repl;
let mockProcess;
let spawnStub;
beforeEach(() => {
// Create mock process with stdin, stdout, and stderr streams
mockProcess = new (class extends EventEmitter {
stdin = new EventEmitter();
stdout = new EventEmitter();
stderr = new EventEmitter();
kill = sinon.stub();
constructor() {
super();
this.stdin.write = sinon.stub();
}
})();
// Stub the spawn function
spawnStub = sinon.stub(HerokuREPL, 'spawn').returns(mockProcess);
// Idle teardown disabled (0) for the shared fixture so these tests are not
// affected by the idle timer; the dedicated idle tests opt back in.
repl = new HerokuREPL(500, 0);
// The process is started lazily. Most tests below assume
// a spawned, ready process, so prime one explicitly here.
repl['initializeProcess']();
mockProcess.stdout.emit('data', 'heroku >');
(async () => {
for await (const command of repl) {
// debug here if needed
}
})();
});
afterEach(() => {
repl[Symbol.dispose]();
sinon.restore();
});
describe('initialization', () => {
it('should create a new process when initialized', () => {
expect(spawnStub.calledOnce).to.be.true;
expect(spawnStub.firstCall.args[2].env.HEROKU_MCP_MODE).to.equal('true');
expect(spawnStub.firstCall.args[2].env.HEROKU_MCP_SERVER_VERSION).to.equal(VERSION);
expect(spawnStub.firstCall.args[2].env.HEROKU_HEADERS).to.equal(JSON.stringify({
'User-Agent': `Heroku-MCP-Server/${VERSION} (${process.platform}; ${process.arch}; node/${process.version})`
}));
});
});
describe('executeCommand', () => {
it('should queue and execute commands', async () => {
const commandPromise = repl.executeCommand('apps');
// Simulate process output
setTimeout(() => mockProcess.stdout.emit('data', 'Command output\n<<<END RESULTS>>>'));
const result = await commandPromise;
expect(result).to.include('Command output');
});
it('should handle multiple commands in sequence', async () => {
const command1Promise = repl.executeCommand('apps');
const command2Promise = repl.executeCommand('addons');
setTimeout(() => mockProcess.stdout.emit('data', 'Command 1 output\n<<<END RESULTS>>>'));
setTimeout(() => mockProcess.stdout.emit('data', 'Command 2 output\n<<<END RESULTS>>>'));
const result1 = await command1Promise;
const result2 = await command2Promise;
expect(result1).to.include('Command 1 output');
expect(result2).to.include('Command 2 output');
});
it('should capture stderr output', async () => {
const commandPromise = repl.executeCommand('apps');
setTimeout(() => mockProcess.stderr.emit('data', 'Warning message'));
setTimeout(() => mockProcess.stdout.emit('data', 'Command output\n<<<END RESULTS>>>'));
const result = await commandPromise;
expect(result).to.include('Warning message');
});
it('should timeout if command takes too long', async () => {
const commandPromise = repl.executeCommand('apps');
setTimeout(() => mockProcess.stdout.emit('data', 'This is taking too long...\n'), 600);
const result = await commandPromise;
expect(result).to.include('The command failed to complete in 500ms');
});
});
describe('stdin protocol', () => {
// This test locks in the exact bytes written to the CLI process stdin.
// Commands are newline-delimited on stdin, so the REPL appends a single
// trailing '\n' to signal "end of command". This is why a command string
// must itself be a single line: the CLI treats everything after an embedded
// newline as a separate command. The CommandBuilder CR/LF validation
// upstream enforces that; this test documents the exact-bytes behavior it
// relies on.
it('should write exactly command + a single trailing newline to stdin', async () => {
const command = 'apps';
// Deterministically resolve the moment the async iterator writes to
// stdin, rather than waiting a fixed duration.
let onWrite;
const written = new Promise((resolve) => {
onWrite = resolve;
});
mockProcess.stdin.write.callsFake(() => {
onWrite();
return true;
});
const commandPromise = repl.executeCommand(command);
// Proceed as soon as the write actually happens.
await written;
expect(mockProcess.stdin.write.calledOnce).to.be.true;
const writtenValue = mockProcess.stdin.write.firstCall.args[0];
expect(writtenValue).to.equal(command + '\n');
// Clean up the outstanding command promise.
setTimeout(() => mockProcess.stdout.emit('data', 'Command output\n<<<END RESULTS>>>'));
await commandPromise;
});
});
describe('process management', () => {
it('should restart process on unexpected close', () => {
mockProcess.emit('close', 1);
expect(spawnStub.calledTwice).to.be.true;
});
it('should not restart process when aborted', () => {
repl[Symbol.dispose]();
mockProcess.emit('close', 0);
expect(spawnStub.calledOnce).to.be.true;
});
});
describe('async iterator', () => {
it('should yield command results as they are executed', async () => {
const commandPromise = repl.executeCommand('apps');
let commands = [];
void (async () => {
for await (const command of repl) {
commands.push(command);
}
})();
// Clean up the command
setTimeout(() => mockProcess.stdout.emit('data', 'Output\n<<<END RESULTS>>>'));
await commandPromise;
await new Promise((resolve) => setTimeout(resolve, 50));
expect(commands[0]).to.equal('Output\n<<<END RESULTS>>>');
});
});
describe('cleanup', () => {
it('should abort when disposed', () => {
repl[Symbol.dispose]();
expect(repl['abortController'].signal.aborted).to.be.true;
});
});
describe('lazy start and idle teardown', () => {
// These tests construct their own REPL instances so they can observe spawn
// behavior from a clean slate, independent of the primed shared fixture.
it('does not spawn a process at construction', () => {
repl[Symbol.dispose]();
spawnStub.resetHistory();
const lazyRepl = new HerokuREPL(500, 0);
expect(spawnStub.called).to.be.false;
expect(lazyRepl['process']).to.be.undefined;
lazyRepl[Symbol.dispose]();
});
it('spawns the process lazily on the first executeCommand', () => {
repl[Symbol.dispose]();
spawnStub.resetHistory();
const lazyRepl = new HerokuREPL(500, 0);
void (async () => {
for await (const command of lazyRepl) {
// drain
}
})();
void lazyRepl.executeCommand('apps');
expect(spawnStub.calledOnce).to.be.true;
expect(lazyRepl['process']).to.not.be.undefined;
lazyRepl[Symbol.dispose]();
});
it('tears down a ready, idle process after the idle timeout', () => {
repl[Symbol.dispose]();
const clock = sinon.useFakeTimers();
spawnStub.resetHistory();
mockProcess.kill.resetHistory();
const idleRepl = new HerokuREPL(500, 1000);
idleRepl['initializeProcess']();
expect(spawnStub.calledOnce).to.be.true;
// Process becomes ready with an empty queue -> schedules idle teardown.
mockProcess.stdout.emit('data', 'heroku >');
expect(mockProcess.kill.called).to.be.false;
clock.tick(1001);
expect(mockProcess.kill.called).to.be.true;
expect(idleRepl['process']).to.be.undefined;
expect(idleRepl['isReady']).to.be.false;
clock.restore();
idleRepl[Symbol.dispose]();
});
it('does not tear down while a command is queued or in flight', () => {
repl[Symbol.dispose]();
const clock = sinon.useFakeTimers();
spawnStub.resetHistory();
mockProcess.kill.resetHistory();
const idleRepl = new HerokuREPL(500, 1000);
idleRepl['initializeProcess']();
mockProcess.stdout.emit('data', 'heroku >');
// Queue work after the idle timer was armed; the pending timer must not
// kill a process that now has work to do.
void idleRepl.executeCommand('apps');
clock.tick(1001);
expect(mockProcess.kill.called).to.be.false;
expect(idleRepl['process']).to.not.be.undefined;
clock.restore();
idleRepl[Symbol.dispose]();
});
it('respawns the process on the next command after an idle teardown', () => {
repl[Symbol.dispose]();
spawnStub.resetHistory();
const idleRepl = new HerokuREPL(500, 1000);
idleRepl['initializeProcess']();
expect(spawnStub.calledOnce).to.be.true;
// Simulate an idle teardown.
idleRepl['teardownIdleProcess']();
expect(idleRepl['process']).to.be.undefined;
spawnStub.resetHistory();
void (async () => {
for await (const command of idleRepl) {
// drain
}
})();
void idleRepl.executeCommand('apps');
expect(spawnStub.calledOnce).to.be.true;
expect(idleRepl['process']).to.not.be.undefined;
idleRepl[Symbol.dispose]();
});
it('delivers and resolves a command issued after an idle teardown', async () => {
// Tearing the process down must not swap out the pauseIterator promise the
// async iterator is parked on, otherwise the next command's resolver wakes a
// promise nobody is awaiting and the respawned process never receives the
// queued command — the request would hang forever.
repl[Symbol.dispose]();
spawnStub.resetHistory();
// Auto-idle disabled (0); we drive teardown manually to control timing.
const idleRepl = new HerokuREPL(500, 0);
idleRepl['initializeProcess']();
mockProcess.stdout.emit('data', 'heroku >');
void (async () => {
for await (const command of idleRepl) {
// drain
}
})();
// First command completes normally.
const first = idleRepl.executeCommand('apps');
setTimeout(() => mockProcess.stdout.emit('data', 'first output\n<<<END RESULTS>>>'));
expect(await first).to.include('first output');
// Simulate the idle timer firing while the queue is empty.
idleRepl['teardownIdleProcess']();
expect(idleRepl['process']).to.be.undefined;
// A command issued after teardown must respawn the process AND actually
// resolve — not hang. The respawned process signals ready, then returns.
const second = idleRepl.executeCommand('addons');
setTimeout(() => mockProcess.stdout.emit('data', 'heroku >'), 0);
setTimeout(() => mockProcess.stdout.emit('data', 'second output\n<<<END RESULTS>>>'), 10);
expect(await second).to.include('second output');
idleRepl[Symbol.dispose]();
});
});
describe('MCP startup error handling', () => {
it('should emit fatalError if spawn throws', () => {
spawnStub.restore();
const error = new Error('spawn failed');
const badSpawn = sinon.stub(HerokuREPL, 'spawn').throws(error);
const fatalSpy = sinon.spy();
const replWithError = new HerokuREPL(500);
replWithError.on('fatalError', fatalSpy);
// Force re-init to trigger error
replWithError['initializeProcess']();
expect(fatalSpy.called).to.be.true;
const mcpError = fatalSpy.firstCall.args[0];
expect(mcpError.content[0].text).to.include('Startup error: spawn failed');
badSpawn.restore();
});
it('should emit fatalError if process emits error', () => {
const fatalSpy = sinon.spy();
repl.on('fatalError', fatalSpy);
const error = new Error('process error');
mockProcess.emit('error', error);
expect(fatalSpy.called).to.be.true;
const mcpError = fatalSpy.firstCall.args[0];
expect(mcpError.content[0].text).to.include('Startup error: process error');
});
it('should emit fatalError if CLI outputs old version warning', () => {
const fatalSpy = sinon.spy();
repl.on('fatalError', fatalSpy);
mockProcess.stdout.emit('data', 'Warning: --repl is not a heroku command.');
expect(fatalSpy.called).to.be.true;
const mcpError = fatalSpy.firstCall.args[0];
expect(mcpError.content[0].text).to.include('Your Heroku CLI version does not support --repl mode');
});
});
describe('respawn and failure hardening', () => {
it('resolves a command issued after an unexpected close re-parks the iterator', async () => {
// The 'close' handler calls pauseQueue() while the async iterator is parked
// on the current pauseIterator promise. pauseQueue() must be idempotent, or
// it orphans that promise and the post-close command never reaches stdin.
repl[Symbol.dispose]();
spawnStub.resetHistory();
const r = new HerokuREPL(500, 0);
r['initializeProcess']();
mockProcess.stdout.emit('data', 'heroku >');
void (async () => {
for await (const command of r) {
// drain
}
})();
// First command completes, draining the queue and parking the iterator.
const first = r.executeCommand('apps');
setTimeout(() => mockProcess.stdout.emit('data', 'first\n<<<END RESULTS>>>'));
expect(await first).to.include('first');
// Unexpected close (code 1) restarts the process and calls pauseQueue().
mockProcess.emit('close', 1);
// Respawned process signals ready.
mockProcess.stdout.emit('data', 'heroku >');
// A command issued after the close must reach stdin and actually resolve.
const second = r.executeCommand('addons');
setTimeout(() => mockProcess.stdout.emit('data', 'second\n<<<END RESULTS>>>'));
expect(await second).to.include('second');
r[Symbol.dispose]();
});
it('settles a queued command instead of hanging when no CLI is available', async () => {
repl[Symbol.dispose]();
const spawnSyncStub = sinon.stub(HerokuREPL, 'spawnSync').returns({
error: new Error('not found'),
status: 1,
stdout: ''
});
const r = new HerokuREPL(500, 0);
const fatalSpy = sinon.spy();
r.on('fatalError', fatalSpy);
void (async () => {
for await (const command of r) {
// drain
}
})();
const result = await r.executeCommand('apps');
expect(result).to.include('No suitable Heroku CLI found');
expect(fatalSpy.called).to.be.true;
r[Symbol.dispose]();
spawnSyncStub.restore();
});
it('settles a queued command instead of hanging when spawn throws', async () => {
repl[Symbol.dispose]();
spawnStub.restore();
const badSpawn = sinon.stub(HerokuREPL, 'spawn').throws(new Error('spawn boom'));
const r = new HerokuREPL(500, 0);
const fatalSpy = sinon.spy();
r.on('fatalError', fatalSpy);
void (async () => {
for await (const command of r) {
// drain
}
})();
const result = await r.executeCommand('apps');
expect(result).to.include('spawn boom');
expect(fatalSpy.called).to.be.true;
r[Symbol.dispose]();
badSpawn.restore();
});
it('settles a command if the process spawns but never becomes ready', async () => {
repl[Symbol.dispose]();
spawnStub.resetHistory();
const r = new HerokuREPL(500, 0);
r['initializeProcess']();
void (async () => {
for await (const command of r) {
// drain
}
})();
// Never emit 'heroku >': the process is up but never ready to dispatch.
const result = await r.executeCommand('apps');
expect(result).to.include('The command failed to complete in 500ms');
r[Symbol.dispose]();
});
it('resolves the CLI once and reuses it across respawns', () => {
repl[Symbol.dispose]();
const spawnSyncStub = sinon.stub(HerokuREPL, 'spawnSync');
spawnSyncStub.withArgs('heroku', ['version'], sinon.match.any).returns({
error: null,
status: 0,
stdout: 'heroku/11.0.0 (darwin-x64) node-v18.17.0'
});
const r = new HerokuREPL(500, 0);
r['initializeProcess']();
r['initializeProcess']();
r['initializeProcess']();
const herokuProbes = spawnSyncStub.getCalls().filter((c) => c.args[0] === 'heroku').length;
expect(herokuProbes).to.equal(1);
r[Symbol.dispose]();
spawnSyncStub.restore();
});
it('ignores late stdout from a torn-down process', () => {
repl[Symbol.dispose]();
spawnStub.resetHistory();
const r = new HerokuREPL(500, 0);
r['initializeProcess']();
mockProcess.stdout.emit('data', 'heroku >');
expect(r['isReady']).to.be.true;
r['teardownIdleProcess']();
expect(r['isReady']).to.be.false;
// Late output from the killed child must not revive readiness.
mockProcess.stdout.emit('data', 'heroku >');
expect(r['isReady']).to.be.false;
r[Symbol.dispose]();
});
it('settles pending commands when the instance is disposed mid-flight', async () => {
repl[Symbol.dispose]();
spawnStub.resetHistory();
const r = new HerokuREPL(500, 0);
r['initializeProcess']();
mockProcess.stdout.emit('data', 'heroku >');
void (async () => {
for await (const command of r) {
// drain
}
})();
const pending = r.executeCommand('apps');
// Dispose before the command completes.
r[Symbol.dispose]();
const result = await pending;
expect(result).to.include('shut down before the command completed');
});
});
describe('getHerokuCliCommandAndArgs', () => {
let spawnSyncStub;
let fatalSpy;
beforeEach(() => {
spawnSyncStub = sinon.stub(HerokuREPL, 'spawnSync');
fatalSpy = sinon.spy();
repl.on('fatalError', fatalSpy);
// The shared fixture already resolved (and now caches) a CLI via the real
// spawnSync in its beforeEach. Clear that cache so these tests exercise the
// resolver against the stub rather than returning the cached real result.
repl['resolvedCli'] = undefined;
});
afterEach(() => {
spawnSyncStub.restore();
});
it('should prefer the local heroku CLI (>= 10.10.0) over npx, without checking npx', () => {
// heroku CLI available with version >= 10.10.0
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: 'heroku/10.15.0 (darwin-x64) node-v18.17.0'
});
// npx is also available, but must not be used when a suitable local CLI exists
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: '9.8.1'
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.deep.equal({
cliCommand: 'heroku',
cliArgs: ['repl']
});
// npx must not even be probed when a suitable local CLI is found
expect(spawnSyncStub.calledWith('npx', ['--version'], sinon.match.any)).to.be.false;
expect(fatalSpy.called).to.be.false;
});
it('should prefer the local heroku CLI when it is > 10.x', () => {
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: 'heroku/11.0.0 (darwin-x64) node-v18.17.0'
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.deep.equal({
cliCommand: 'heroku',
cliArgs: ['repl']
});
expect(fatalSpy.called).to.be.false;
});
it('should fall back to npx when heroku CLI is not available', () => {
// heroku CLI not available
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: new Error('heroku not found'),
status: 1,
stdout: ''
});
// npx available
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: '9.8.1'
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.deep.equal({
cliCommand: 'npx',
cliArgs: ['-y', 'heroku@latest', 'repl']
});
expect(fatalSpy.called).to.be.false;
});
it('should fall back to npx when the local heroku CLI is too old (< 10.10.0)', () => {
// heroku CLI available but version < 10.10.0 — too old for `repl`
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: 'heroku/10.9.0 (darwin-x64) node-v18.17.0'
});
// npx available
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: '9.8.1'
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.deep.equal({
cliCommand: 'npx',
cliArgs: ['-y', 'heroku@latest', 'repl']
});
expect(fatalSpy.called).to.be.false;
});
it('should fall back to npx when the heroku CLI version format is unexpected', () => {
// heroku CLI available but version format is unexpected
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: 'some unexpected output without version'
});
// npx available
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: '9.8.1'
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.deep.equal({
cliCommand: 'npx',
cliArgs: ['-y', 'heroku@latest', 'repl']
});
expect(fatalSpy.called).to.be.false;
});
it('should fall back to npx when the heroku CLI stdout is not a string', () => {
// heroku CLI available but stdout is not a string
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: Buffer.from('heroku/10.15.0')
});
// npx available
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: '9.8.1'
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.deep.equal({
cliCommand: 'npx',
cliArgs: ['-y', 'heroku@latest', 'repl']
});
expect(fatalSpy.called).to.be.false;
});
it('should emit fatalError and return null when neither heroku CLI nor npx is available', () => {
// heroku CLI not available
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: new Error('heroku not found'),
status: 1,
stdout: ''
});
// npx not available
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: new Error('npx not found'),
status: 1,
stdout: ''
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.be.null;
expect(fatalSpy.called).to.be.true;
const mcpError = fatalSpy.firstCall.args[0];
expect(mcpError.content[0].text).to.include('Startup error: No suitable Heroku CLI found');
});
it('should emit fatalError and return null when a too-old heroku CLI and no npx are available', () => {
// heroku CLI present but too old
spawnSyncStub.withArgs('heroku', ['version'], sinon.match({ encoding: 'utf-8' })).returns({
error: null,
status: 0,
stdout: 'heroku/10.9.0 (darwin-x64) node-v18.17.0'
});
// npx not available
spawnSyncStub.withArgs('npx', ['--version'], sinon.match({ encoding: 'utf-8' })).returns({
error: new Error('npx not found'),
status: 1,
stdout: ''
});
const result = repl['getHerokuCliCommandAndArgs']();
expect(result).to.be.null;
expect(fatalSpy.called).to.be.true;
const mcpError = fatalSpy.firstCall.args[0];
expect(mcpError.content[0].text).to.include('Startup error: No suitable Heroku CLI found');
});
});
});