package mcp import ( "strings" "testing" ) func TestStripAnsi(t *testing.T) { cases := []struct{ in, want string }{ {"plain", "plain"}, {"\x1b[31mred\x1b[0m text", "red text"}, {"a\x1b[?25hb", "ab"}, {"\x1b]0;title\x07after", "after"}, {"\x1b]0;title\x1b\\after", "after"}, {"\x1b]8;;http://x\x07link\x1b]8;;\x07", "link"}, {"\x1b(Bcharset", "Bcharset"}, // ESC ( is a two-byte sequence; only '(' is swallowed {"\x1b", ""}, {"tail\x1b[", "tail"}, } for _, tc := range cases { if got := stripAnsi(tc.in); got != tc.want { t.Errorf("stripAnsi(%q) = %q, want %q", tc.in, got, tc.want) } } } func TestBuildSentinelLines(t *testing.T) { const cmd, nonce = "echo hi", "ab12cd34" var cmdLine, sentLine string cmdLine, sentLine = buildSentinelLines(true, true, cmd, nonce) if cmdLine != cmd || sentLine != "echo __MCP_DONE_ab12cd34__$?" { t.Fatalf("posix: %q / %q", cmdLine, sentLine) } wire := string(buildTerminalWire(true, cmdLine, sentLine)) if want := "PS1=\r\necho hi\r\necho __MCP_DONE_ab12cd34__$?\r\n"; wire != want { t.Fatalf("posix wire = %q, want %q", wire, want) } cmdLine, sentLine = buildSentinelLines(true, false, cmd, nonce) if cmdLine != "@echo off & echo hi" || sentLine != "echo __MCP_DONE_ab12cd34__%errorlevel%" { t.Fatalf("conpty: %q / %q", cmdLine, sentLine) } if wire := string(buildTerminalWire(false, cmdLine, sentLine)); !strings.HasPrefix(wire, "@echo off & echo hi\r\necho ") { t.Fatalf("conpty wire = %q", wire) } cmdLine, sentLine = buildSentinelLines(false, false, cmd, nonce) if cmdLine != "echo hi 2>&1 && echo __MCP_DONE_ab12cd34__0 || echo __MCP_DONE_ab12cd34__1" || sentLine != "" { t.Fatalf("legacy: %q / %q", cmdLine, sentLine) } } // Samples follow the ConPTY repaint behaviour documented in // McpServer.cpp:3946-3989: the boot prompt and echoed input precede the real // output, and a repaint can glue the sentinel onto the last output line. func TestStripEchoedInputWindows(t *testing.T) { const nonce = "ab12cd34" cmdLine, sentLine := buildSentinelLines(true, false, "rem hello", nonce) got := cleanTerminalOutput([]byte( "\x1b[?25l@echo off & rem hello\r\n\r\n__MCP_DONE_ab12cd34__%errorlevel%\r\n__MCP_DONE_ab12cd34__0\r\n"), true, true, cmdLine, sentLine, nonce, true) if got != "" { t.Fatalf("rem hello output = %q, want empty", got) } cmdLine, sentLine = buildSentinelLines(true, false, "cmd /c echo abc", nonce) got = cleanTerminalOutput([]byte( "@echo off & cmd /c echo abc\r\nabc__MCP_DONE_ab12cd34__0\r\n"), true, true, cmdLine, sentLine, nonce, true) if got != "abc" { t.Fatalf("cmd /c echo abc output = %q, want %q", got, "abc") } cmdLine, sentLine = buildSentinelLines(true, false, "type .env", nonce) got = cleanTerminalOutput([]byte( "@echo off & type .env\r\nKEY=value\r\nSECOND=line\r\n__MCP_DONE_ab12cd34__1\r\n"), true, true, cmdLine, sentLine, nonce, true) if got != "KEY=value\nSECOND=line" { t.Fatalf("type .env output = %q", got) } // A long command line is wrapped at 80 columns by ConPTY; the echo matcher // skips the inserted newlines. "cmd /\c" is a literal path, not an escape. long := "cmd /c " + strings.Repeat("x", 100) cmdLine, sentLine = buildSentinelLines(true, false, long, nonce) wrapped := "@echo off & cmd /c " + strings.Repeat("x", 60) + "\r\n" + strings.Repeat("x", 40) + "\r\nwrapped-ok\r\n__MCP_DONE_ab12cd34__0\r\n" got = cleanTerminalOutput([]byte(wrapped), true, true, cmdLine, sentLine, nonce, true) if got != "wrapped-ok" { t.Fatalf("wrapped echo output = %q, want %q", got, "wrapped-ok") } } // The legacy non-PTY shell skips the echo by written length, so the command // echo must not be removed (it could be real output, e.g. `echo echo hello`). func TestCleanNonPTYKeepsCommandEcho(t *testing.T) { const nonce = "ab12cd34" cmdLine, sentLine := buildSentinelLines(false, false, "echo echo hello", nonce) raw := []byte("echo echo hello\r\nhello\r\n__MCP_DONE_ab12cd34__0\r\n") got := cleanTerminalOutput(raw, false, true, cmdLine, sentLine, nonce, true) if got != "echo echo hello\nhello" { t.Fatalf("non-PTY output = %q", got) } } func TestCleanTerminalOutputTrimOnlyOnFinalChunk(t *testing.T) { const nonce = "ab12cd34" if got := cleanTerminalOutput([]byte("LINE-17\r\n"), true, false, "", "", nonce, false); got != "LINE-17\n" { t.Fatalf("partial window = %q, want trailing newline kept", got) } if got := cleanTerminalOutput([]byte("LINE-17\r\n"), true, false, "", "", nonce, true); got != "LINE-17" { t.Fatalf("final window = %q, want trimmed", got) } } func TestStripSentinelTextEchoTemplate(t *testing.T) { got := stripSentinelText("before\necho __MCP_DONE_ab12cd34__%errorlevel%\nafter\n", "ab12cd34") // The template line's own newline stays: stripEchoedLine removes the // echoed sentinel line first in the real pipeline; this is the fallback. if got != "before\n\nafter\n" { t.Fatalf("template removal = %q", got) } // A negative exit code (%errorlevel% of a crashed process) is removed too. got = stripSentinelText("out\n__MCP_DONE_ab12cd34__-1073741510\n", "ab12cd34") if got != "out\n" { t.Fatalf("negative removal = %q", got) } } func TestContainsControlChar(t *testing.T) { for _, s := range []string{"a\nb", "a\rb", "a\tb", "\x01"} { if !containsControlChar(s) { t.Errorf("containsControlChar(%q) = false", s) } } for _, s := range []string{"", "echo ok", "中文", "a|b&&c"} { if containsControlChar(s) { t.Errorf("containsControlChar(%q) = true", s) } } } func TestHasPosixBackgroundAmpersand(t *testing.T) { cases := []struct { in string want bool }{ {"sleep 1 &", true}, {"echo a & echo b", true}, {"a && b", false}, {"a &> /dev/null", false}, {"a 2>&1", false}, {"echo 'a & b'", false}, {`echo a\&b`, false}, {`echo "a & b"`, false}, {"grep -v '^#' /etc/hosts", false}, } for _, tc := range cases { if got := hasPosixBackgroundAmpersand(tc.in); got != tc.want { t.Errorf("hasPosixBackgroundAmpersand(%q) = %v, want %v", tc.in, got, tc.want) } } } func TestNormalizeCRLF(t *testing.T) { if got := normalizeCRLF("a\r\nb\rc"); got != "a\nb\rc" { t.Fatalf("normalizeCRLF = %q", got) } }