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
132 changes: 132 additions & 0 deletions pkgs/mcpx/internal/e2e/cli_inspection_burndown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package e2e_test

// Burn-down exercises for the inspection-group CLI commands (#8463):
// dashboard, hook, interactions and logs each get one real exercise
// through the harness against a live daemon. A successful run is what
// records the feature's coverage-ledger row, which is what lets its
// line leave feature_coverage_exemptions.txt. explore, the fifth
// command in the group, needs a terminal and lives in
// cli_inspection_explore_linux_test.go.

import (
"io"
"net/http"
"strings"
"testing"
)

func TestDashboardPrintsItsLiveURL(t *testing.T) {
e := newEnv(t, oneServer)
out := e.run("dashboard")
if !strings.Contains(out, "mcpx dashboard running at ") {
t.Fatalf("dashboard did not report where it is running:\n%s", out)
}
if !strings.Contains(out, "/dashboard") {
t.Fatalf("the dashboard URL does not name the dashboard path:\n%s", out)
}
}

func TestHookLsListsDeclaredHooks(t *testing.T) {
e := newEnv(t, oneServer)
if out := e.run("hook", "ls"); !strings.Contains(out, "No hooks declared") {
t.Fatalf("an env with no hooks setting should say so:\n%s", out)
}

declared := newEnv(t, `{
"mcpServers": {
"demo": { "command": "FAKE", "mcpx": { "sharing": "shared", "scope": "global", "description": "a fake server" } }
},
"hooks": [ { "name": "page", "on": "server.stopped", "run": ["echo", "done"] } ]
}`)
out := declared.run("hook", "ls")
for _, want := range []string{"page", "server.stopped", "echo done"} {
if !strings.Contains(out, want) {
t.Errorf("hook ls does not show %q:\n%s", want, out)
}
}
}

func TestInteractionsListsSearchesAndShowsAProxyTurn(t *testing.T) {
e := newEnv(t, oneServer)
if out := e.run("interactions"); !strings.Contains(out, "No matching interactions found.") {
t.Fatalf("a fresh daemon should report an empty history:\n%s", out)
}

// Interactions are recorded by the daemon's proxy surface: a chat
// completion with no upstream configured is answered locally and
// leaves a trace behind. Drive one through the daemon socket,
// then read it back through every interactions surface: list,
// search and --show.
req, err := http.NewRequest(http.MethodPost, "http://mcpx/v1/chat/completions",
strings.NewReader(`{"messages":[{"role":"user","content":"inspectionprobe question"}]}`))
if err != nil {
t.Fatal(err)
}
resp, err := e.socketClient(t).Do(req)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("the proxy turn failed: status %d: %s", resp.StatusCode, body)
}

list := e.run("interactions")
if !strings.Contains(list, "TRACE ID") || !strings.Contains(list, "inspectionprobe") {
t.Fatalf("the recorded turn is not in the interactions list:\n%s", list)
}
var trace string
for _, l := range strings.Split(list, "\n") {
if strings.Contains(l, "inspectionprobe") {
trace = strings.Fields(l)[0]
break
}
}
if trace == "" {
t.Fatalf("no trace id in the interactions list:\n%s", list)
}

shown := e.run("interactions", "--show", trace)
if !strings.Contains(shown, "Trace ID:") || !strings.Contains(shown, "inspectionprobe") {
t.Fatalf("--show does not render the recorded turn:\n%s", shown)
}
if out := e.run("interactions", "--q", "inspectionprobe"); !strings.Contains(out, trace) {
t.Fatalf("search should find the recorded turn:\n%s", out)
}
if out := e.run("interactions", "--q", "no-such-input-zzz"); !strings.Contains(out, "No matching interactions found.") {
t.Fatalf("a search with no hits should say so:\n%s", out)
}
}

// The log command's canonical spelling is covered by logs_test.go;
// the dispatcher's "logs" alias key is a registry feature of its own,
// and this is its exercise — the same surfaces, spelled "logs".
func TestLogsAliasQueriesTheLog(t *testing.T) {
e := newEnv(t, oneServer)
e.run("call", "demo.echo", `{"message":"x"}`)

out := e.run("logs", "sql", "SELECT count(*) AS n FROM records")
if !strings.Contains(out, "n") {
t.Fatalf("logs sql did not print a table:\n%s", out)
}

slowest := e.run("stats", "slowest")
var trace string
for _, l := range strings.Split(slowest, "\n") {
if !strings.Contains(l, "echo") {
continue
}
f := strings.Fields(l)
trace = f[len(f)-1]
}
if !strings.HasPrefix(trace, "cal-") {
t.Fatalf("no call trace id in:\n%s", slowest)
}
chain := e.run("logs", "--chain", trace)
for _, want := range []string{"dmn-", "srv-", "cal-", "daemon starting"} {
if !strings.Contains(chain, want) {
t.Fatalf("the chain does not reach %q:\n%s", want, chain)
}
}
}
103 changes: 103 additions & 0 deletions pkgs/mcpx/internal/e2e/cli_inspection_explore_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//go:build linux

package e2e_test

// The explore exercise for the #8463 inspection-group burn-down.
// explore refuses to run without a terminal on stdin (see
// TestExploreRefusesWithoutATerminal), and the harness runners only
// ever offer it a pipe — so this file gives the env a runner whose
// stdin is a real pty. Linux-only: the pty is allocated through
// /dev/ptmx ioctls.

import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"syscall"
"testing"
"time"
"unsafe"

"golang.org/x/sys/unix"
)

// ptyRunner runs the env's binary with a pty slave as its standard
// input: the one arrangement under which the terminal-only commands
// will run at all.
type ptyRunner struct {
slave *os.File
}

func (ptyRunner) name() string { return "pty" }
func (ptyRunner) needsProcess() bool { return true }

func (r ptyRunner) run(ctx context.Context, e *env, _ io.Reader, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 90*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, e.mcpx, args...)
cmd.Dir = e.dir
cmd.Env = e.envVars
cmd.Stdin = r.slave
out, err := cmd.CombinedOutput()
return string(out), harnessTimeout(ctx, err)
}

func openPTY() (master, slave *os.File, err error) {
master, err = os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
if err != nil {
return nil, nil, err
}
// TIOCGPTN and TIOCSPTLCK both take a pointer to an int, so they
// cannot go through the by-value ioctl helpers.
var n uint32
if _, _, errno := unix.Syscall(syscall.SYS_IOCTL, master.Fd(),
uintptr(unix.TIOCGPTN), uintptr(unsafe.Pointer(&n))); errno != 0 {
_ = master.Close()
return nil, nil, errno
}
var unlock int32
if _, _, errno := unix.Syscall(syscall.SYS_IOCTL, master.Fd(),
uintptr(unix.TIOCSPTLCK), uintptr(unsafe.Pointer(&unlock))); errno != 0 {
_ = master.Close()
return nil, nil, errno
}
slave, err = os.OpenFile(fmt.Sprintf("/dev/pts/%d", n), os.O_RDWR, 0)
if err != nil {
_ = master.Close()
return nil, nil, err
}
return master, slave, nil
}

func TestExploreBrowsesUnderATerminal(t *testing.T) {
master, slave, err := openPTY()
if err != nil {
t.Fatalf("allocate pty: %v", err)
}
// The pty must outlive the env's own cleanup, which stops the
// daemon by running one more command through this runner — so
// the close is registered before newEnv registers its cleanup
// (cleanups run last-in, first-out).
t.Cleanup(func() {
_ = master.Close()
_ = slave.Close()
})
// Answer the prompt loop's first read with "quit", up front: the
// pty buffers it until explore reads.
if _, err := master.WriteString("q\n"); err != nil {
t.Fatalf("queue the quit command: %v", err)
}

e := newEnv(t, oneServer)
e.runner = ptyRunner{slave: slave}
out := e.run("explore")
if !strings.Contains(out, "mcpx explore") {
t.Fatalf("explore did not open with its banner:\n%s", out)
}
if !strings.Contains(out, "demo") {
t.Fatalf("explore's first screen does not list the namespace:\n%s", out)
}
}
5 changes: 0 additions & 5 deletions pkgs/mcpx/internal/e2e/feature_coverage_exemptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ cli reload no integration-test exercise yet; burn-down #8461 (cli daemon group)
cli stdio-proxy no integration-test exercise yet; burn-down #8461 (cli daemon group)
cli supervise no integration-test exercise yet; burn-down #8461 (cli daemon group)
cli upgrade no integration-test exercise yet; burn-down #8461 (cli daemon group)
cli dashboard no integration-test exercise yet; burn-down #8463 (cli inspection group)
cli explore no integration-test exercise yet; burn-down #8463 (cli inspection group)
cli hook no integration-test exercise yet; burn-down #8463 (cli inspection group)
cli interactions no integration-test exercise yet; burn-down #8463 (cli inspection group)
cli logs no integration-test exercise yet; burn-down #8463 (cli inspection group)
cli artifacts no integration-test exercise yet; burn-down #8464 (cli operations group)
cli complete no integration-test exercise yet; burn-down #8464 (cli operations group)
cli events no integration-test exercise yet; burn-down #8464 (cli operations group)
Expand Down