Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions packages/cli/src/__tests__/pi-tui-mcp-status.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import assert from 'node:assert/strict';
import { describe, test } from 'node:test';
import { McpStatusOverlay } from '../pi-tui-mcp-status.js';
import type { TuiMcpSnapshot, TuiMcpSurface } from '../tui-mcp-control.js';
import { stripAnsi } from '../tui-ansi.js';

describe('MCP status overlay', () => {
test('renders the local publication and negotiated server status', () => {
const overlay = new McpStatusOverlay({
locale: 'en',
surface: surface({
initialization: 'ready',
publication: 'published',
toolCount: 2,
servers: [
{
serverId: 'filesystem',
state: 'connected',
transport: 'stdio',
negotiatedProtocol: { era: 'modern', revision: '2026-07-28' },
toolCount: 2,
},
],
}),
viewportRows: () => 8,
onClose: () => undefined,
onChange: () => undefined,
});

const text = overlay.render(100).map(stripAnsi).join('\n');
assert.match(text, /published · 2 tools/u);
assert.match(text, /filesystem connected · stdio · modern 2026-07-28 · 2 tools/u);
});

test('states the remote limitation instead of implying an empty local config', () => {
const overlay = new McpStatusOverlay({
locale: 'zh',
viewportRows: () => 6,
onClose: () => undefined,
onChange: () => undefined,
});

const text = overlay.render(100).map(stripAnsi).join('\n');
assert.match(text, /未连接本地 MCP 控制面/u);
assert.match(text, /远程 Runtime Host/u);
assert.doesNotMatch(text, /尚未配置/u);
});

test('localizes manager states without changing their source values', () => {
const overlay = new McpStatusOverlay({
locale: 'zh',
surface: surface({
initialization: 'ready',
publication: 'not_published',
toolCount: 0,
servers: [
{ serverId: 'oauth', state: 'needs-auth', transport: 'streamable-http', toolCount: 0 },
],
}),
viewportRows: () => 6,
onClose: () => undefined,
onChange: () => undefined,
});

const text = overlay.render(100).map(stripAnsi).join('\n');
assert.match(text, /oauth 需要登录 · streamable-http/u);
assert.doesNotMatch(text, /needs-auth/u);
});

test('subscribes only for the overlay lifetime', () => {
let subscribed = 0;
let disposed = 0;
let closed = 0;
const mcp = surface({
initialization: 'ready',
publication: 'not_published',
toolCount: 0,
servers: [],
});
mcp.subscribe = () => {
subscribed += 1;
return () => {
disposed += 1;
};
};
const overlay = new McpStatusOverlay({
locale: 'en',
surface: mcp,
viewportRows: () => 6,
onClose: () => {
closed += 1;
},
onChange: () => undefined,
});

assert.equal(subscribed, 1);
overlay.handleInput('q');
assert.equal(disposed, 1);
assert.equal(closed, 1);
});
});

function surface(
snapshot: TuiMcpSnapshot,
): TuiMcpSurface & { subscribe(listener: () => void): () => void } {
return {
snapshot: () => snapshot,
subscribe: () => () => undefined,
};
}
72 changes: 72 additions & 0 deletions packages/cli/src/__tests__/pi-tui-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,46 @@ describe('Maka Pi TUI runner', () => {
]);
});

test('opens the read-only /mcp status on an idle local TUI', async () => {
const terminal = new FakeTerminal();
const driver = new SlashCommandDriver();
const run = runMakaPiTui({
title: 'Maka',
driver,
cwd: '/repo',
model: 'deepseek-v4-flash',
connectionSlug: 'deepseek',
permissionMode: 'ask',
terminal,
mcp: {
snapshot: () => ({
initialization: 'ready',
publication: 'published',
toolCount: 1,
servers: [
{
serverId: 'filesystem',
state: 'connected',
transport: 'stdio',
negotiatedProtocol: { era: 'modern', revision: '2026-07-28' },
toolCount: 1,
},
],
}),
subscribe: () => () => undefined,
},
});

terminal.input('/mcp');
terminal.input('\r');
await waitFor(() => plainTerminalOutput(terminal.screenOutput()).includes('MCP SERVERS'));
assert.match(plainTerminalOutput(terminal.screenOutput()), /filesystem/u);
terminal.input('q');
await waitFor(() => !plainTerminalOutput(terminal.screenOutput()).includes('MCP SERVERS'));
exitMaka(terminal);
await run;
});

test('clears an unsent draft on Ctrl-C without closing Maka', async () => {
const terminal = new FakeTerminal();
const driver = new SlashCommandDriver();
Expand Down Expand Up @@ -2002,6 +2042,38 @@ describe('Maka Pi TUI runner', () => {
await run;
});

test('refuses /mcp during a running turn instead of steering it', async () => {
const terminal = new FakeTerminal();
const driver = new SteeringTurnDriver();
const run = runMakaPiTui({
title: 'Maka',
driver,
cwd: '/repo',
model: 'm',
connectionSlug: 'c',
permissionMode: 'bypass',
terminal,
});

terminal.input('start the work');
terminal.input('\r');
await waitFor(() => terminal.progressStates.at(-1) === true);
terminal.input('/mcp');
terminal.input('\r');
await waitFor(() =>
plainTerminalOutput(terminal.output()).includes('Cannot run /mcp while a turn is running'),
);
assert.deepEqual(driver.steered, []);

terminal.input('\x1b');
terminal.input('\x1b');
await waitFor(() => terminal.progressStates.at(-1) === false);
terminal.input('\x03');
terminal.input('/exit');
terminal.input('\r');
await run;
});

test('quit during a running turn closes the TUI instead of steering it', async () => {
const terminal = new FakeTerminal();
const driver = new SteeringTurnDriver();
Expand Down
Loading