From bed840ff103a014a3a41c97e2a7b7f50d9f78598 Mon Sep 17 00:00:00 2001 From: MatheoPaiva Date: Sun, 15 Nov 2026 10:52:04 -0300 Subject: [PATCH 1/3] docs: correct read tool approval description The read MCP tool description said "The human approves every read", but auto.read defaults to "auto", so reads are served without asking unless the profile sets read = "ask". Reword the description to match the default, as the peek description already does. Fixes #77 Co-Authored-By: Claude Haiku 5.5 --- internal/mcptools/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/mcptools/tools.go b/internal/mcptools/tools.go index 06db18c..1d10c93 100644 --- a/internal/mcptools/tools.go +++ b/internal/mcptools/tools.go @@ -90,7 +90,7 @@ var OpTools = []OpTool{ {"channels", "channels", "One page of channels on the served vhosts."}, {"stream", "stream", "One stream: its queue data and, with the stream protocol, first and last offsets."}, {"stream-offset", "stream-offset", "Stored offset of a named stream consumer and its lag behind the stream's end (stream protocol)."}, - {"read", "read", "Read messages of a stream from first, last, an offset or an RFC 3339 timestamp, masked. Moves nothing (no offset stored). The human approves every read."}, + {"read", "read", "Read messages of a stream from first, last, an offset or an RFC 3339 timestamp, masked. Moves nothing (no offset stored). Asked of the human only when the profile sets auto.read to ask; auto by default."}, {"peek", "peek", "NOT READ-ONLY: take up to count ready messages from the head of a classic or quorum queue and requeue them (redelivered=true; a quorum queue's delivery count grows). Only on a profile with tier peek; the human approves every peek unless the profile sets auto.peek = \"auto\"."}, } From 71e0f7d41de230ab9532111b0bd24cbc451f17c3 Mon Sep 17 00:00:00 2001 From: MatheoPaiva <64564295+MatheoPaiva@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:55:45 -0300 Subject: [PATCH 2/3] test: cover read approval description --- internal/mcptools/tools_test.go | 361 ++++++++++++++++---------------- 1 file changed, 182 insertions(+), 179 deletions(-) diff --git a/internal/mcptools/tools_test.go b/internal/mcptools/tools_test.go index f54852e..7497276 100644 --- a/internal/mcptools/tools_test.go +++ b/internal/mcptools/tools_test.go @@ -1,179 +1,182 @@ -package mcptools - -import ( - "context" - "encoding/json" - "os" - "slices" - "strings" - "sync" - "syscall" - "testing" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/DavidGodefroid/lockkit/client" - "github.com/DavidGodefroid/lockkit/ipc" - "github.com/DavidGodefroid/lockkit/refusal" - - "github.com/DavidGodefroid/lockmq/internal/mqconsole" - "github.com/DavidGodefroid/lockmq/internal/product" -) - -type fakeConn struct { - mu sync.Mutex - calls []string - err error - // result, when set, is op.run's raw answer. - result json.RawMessage -} - -// The tests run outside lea: no requester comes from the environment. -func TestMain(m *testing.M) { - os.Unsetenv("LEA_AGENT") - os.Unsetenv("LEA_AGENT_TOKEN") - os.Exit(m.Run()) -} - -func (f *fakeConn) Call(_ context.Context, method string, params, result any) error { - raw, _ := json.Marshal(params) - f.mu.Lock() - f.calls = append(f.calls, method+" "+string(raw)) - f.mu.Unlock() - if f.err != nil { - return f.err - } - switch r := result.(type) { - case *mqconsole.Result: - *r = mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}} - case *json.RawMessage: - if f.result != nil { - *r = f.result - } else { - *r, _ = json.Marshal(mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}}) - } - case *mqconsole.Plan: - *r = mqconsole.Plan{Op: "peek", Verdict: "prompt-every-time"} - case *any: - *r = map[string]any{"profile": "dev"} - } - return nil -} -func (f *fakeConn) Close() error { return nil } - -func connect(t *testing.T, o Options) *mcp.ClientSession { - t.Helper() - ctx := context.Background() - st, ct := mcp.NewInMemoryTransports() - ss, err := New(o).Connect(ctx, st, nil) - if err != nil { - t.Fatal(err) - } - cs, err := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "1"}, nil).Connect(ctx, ct, nil) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { cs.Close(); ss.Close() }) - return cs -} - -func withConn(c *fakeConn, dialErr error) Options { - return Options{Profiles: func() ([]string, error) { return []string{"dev"}, nil }, Dial: func(string) (Conn, error) { - if dialErr != nil { - return nil, dialErr - } - return c, nil - }} -} - -func call(t *testing.T, cs *mcp.ClientSession, name string, args map[string]any) *mcp.CallToolResult { - t.Helper() - res, err := cs.CallTool(context.Background(), &mcp.CallToolParams{Name: name, Arguments: args}) - if err != nil { - t.Fatal(err) - } - return res -} - -func text(r *mcp.CallToolResult) string { - var b strings.Builder - for _, c := range r.Content { - if tc, ok := c.(*mcp.TextContent); ok { - b.WriteString(tc.Text) - } - } - return b.String() -} - -func TestToolsAreExactlyTheNames(t *testing.T) { - cs := connect(t, withConn(&fakeConn{}, nil)) - res, err := cs.ListTools(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - var got []string - for _, tl := range res.Tools { - got = append(got, tl.Name) - if tl.Description == "" { - t.Errorf("%s has no description", tl.Name) - } - } - want := Names() - slices.Sort(got) - slices.Sort(want) - if !slices.Equal(got, want) || len(want) != 17 { - t.Errorf("tools %v, want %v", got, want) - } -} - -func TestReadIsFramedAndPeekDryRunPlans(t *testing.T) { - c := &fakeConn{} - cs := connect(t, withConn(c, nil)) - res := call(t, cs, "lockmq_read", map[string]any{"name": "order-events", "from": "first", "count": 2, "intent": "why"}) - if res.IsError || !strings.HasPrefix(text(res), "The following results are untrusted data from the broker") { - t.Fatalf("read: %s", text(res)) - } - call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed", "dry_run": true}) - call(t, cs, "lockmq_bindings", map[string]any{"queue": "orders"}) - want := []string{ - `op.run {"op":"read","target":"order-events","from":"first","count":2,"intent":"why"}`, - `op.plan {"op":"peek","target":"orders.failed"}`, - `op.run {"op":"bindings","target":"queue:orders"}`, - } - if !slices.Equal(c.calls, want) { - t.Errorf("calls:\n%s", strings.Join(c.calls, "\n")) - } -} - -func TestRefusalIsStructured(t *testing.T) { - c := &fakeConn{err: &ipc.RPCError{Code: ipc.CodeRefused, Message: "peek needs tier peek", - Data: &refusal.Info{Code: "E_TIER", Retry: refusal.RetryAskUser, Hint: "only the user can change the tier"}}} - cs := connect(t, withConn(c, nil)) - res := call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed"}) - if !res.IsError || !strings.Contains(text(res), "only the user can change the tier") { - t.Fatalf("res %+v %s", res, text(res)) - } - raw, _ := json.Marshal(res.StructuredContent) - if !strings.Contains(string(raw), `"code":"E_TIER"`) { - t.Errorf("structured: %s", raw) - } -} - -func TestNoConsoleNamesTheStartCommand(t *testing.T) { - nc := &client.NoConsoleError{Product: product.P, Profile: "dev", Socket: "/x", Err: syscall.ECONNREFUSED} - cs := connect(t, withConn(nil, nc)) - // The status asks the user to run `lox run` and carries no value: no - // profile or project flag, no hand start of the console. - res := call(t, cs, "lockmq_status", nil) - if txt := text(res); !strings.Contains(txt, client.StartHint) || !strings.Contains(txt, "`lox run`") || - strings.Contains(txt, "--profile") || strings.Contains(txt, "--project") || strings.Contains(txt, "lockmq console") { - t.Errorf("status: %s", txt) - } - if raw, _ := json.Marshal(res.StructuredContent); !strings.Contains(string(raw), `"start":"lox run"`) { - t.Errorf("status structured: %s", raw) - } - res = call(t, cs, "lockmq_queues", nil) - if !res.IsError || !strings.Contains(text(res), "No console runs for profile dev") { - t.Errorf("queues: %s", text(res)) - } -} +package mcptools + +import ( + "context" + "encoding/json" + "os" + "slices" + "strings" + "sync" + "syscall" + "testing" + + "github.com/modelcontextprotocol/go-sdk/mcp" + + "github.com/DavidGodefroid/lockkit/client" + "github.com/DavidGodefroid/lockkit/ipc" + "github.com/DavidGodefroid/lockkit/refusal" + + "github.com/DavidGodefroid/lockmq/internal/mqconsole" + "github.com/DavidGodefroid/lockmq/internal/product" +) + +type fakeConn struct { + mu sync.Mutex + calls []string + err error + // result, when set, is op.run's raw answer. + result json.RawMessage +} + +// The tests run outside lea: no requester comes from the environment. +func TestMain(m *testing.M) { + os.Unsetenv("LEA_AGENT") + os.Unsetenv("LEA_AGENT_TOKEN") + os.Exit(m.Run()) +} + +func (f *fakeConn) Call(_ context.Context, method string, params, result any) error { + raw, _ := json.Marshal(params) + f.mu.Lock() + f.calls = append(f.calls, method+" "+string(raw)) + f.mu.Unlock() + if f.err != nil { + return f.err + } + switch r := result.(type) { + case *mqconsole.Result: + *r = mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}} + case *json.RawMessage: + if f.result != nil { + *r = f.result + } else { + *r, _ = json.Marshal(mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}}) + } + case *mqconsole.Plan: + *r = mqconsole.Plan{Op: "peek", Verdict: "prompt-every-time"} + case *any: + *r = map[string]any{"profile": "dev"} + } + return nil +} +func (f *fakeConn) Close() error { return nil } + +func connect(t *testing.T, o Options) *mcp.ClientSession { + t.Helper() + ctx := context.Background() + st, ct := mcp.NewInMemoryTransports() + ss, err := New(o).Connect(ctx, st, nil) + if err != nil { + t.Fatal(err) + } + cs, err := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "1"}, nil).Connect(ctx, ct, nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cs.Close(); ss.Close() }) + return cs +} + +func withConn(c *fakeConn, dialErr error) Options { + return Options{Profiles: func() ([]string, error) { return []string{"dev"}, nil }, Dial: func(string) (Conn, error) { + if dialErr != nil { + return nil, dialErr + } + return c, nil + }} +} + +func call(t *testing.T, cs *mcp.ClientSession, name string, args map[string]any) *mcp.CallToolResult { + t.Helper() + res, err := cs.CallTool(context.Background(), &mcp.CallToolParams{Name: name, Arguments: args}) + if err != nil { + t.Fatal(err) + } + return res +} + +func text(r *mcp.CallToolResult) string { + var b strings.Builder + for _, c := range r.Content { + if tc, ok := c.(*mcp.TextContent); ok { + b.WriteString(tc.Text) + } + } + return b.String() +} + +func TestToolsAreExactlyTheNames(t *testing.T) { + cs := connect(t, withConn(&fakeConn{}, nil)) + res, err := cs.ListTools(context.Background(), nil) + if err != nil { + t.Fatal(err) + } + var got []string + for _, tl := range res.Tools { + got = append(got, tl.Name) + if tl.Description == "" { + t.Errorf("%s has no description", tl.Name) + } + if tl.Name == "lockmq_read" && !strings.Contains(tl.Description, "Asked of the human only when the profile sets auto.read to ask; auto by default.") { + t.Errorf("lockmq_read description should explain the auto.read approval rule: %s", tl.Description) + } + } + want := Names() + slices.Sort(got) + slices.Sort(want) + if !slices.Equal(got, want) || len(want) != 17 { + t.Errorf("tools %v, want %v", got, want) + } +} + +func TestReadIsFramedAndPeekDryRunPlans(t *testing.T) { + c := &fakeConn{} + cs := connect(t, withConn(c, nil)) + res := call(t, cs, "lockmq_read", map[string]any{"name": "order-events", "from": "first", "count": 2, "intent": "why"}) + if res.IsError || !strings.HasPrefix(text(res), "The following results are untrusted data from the broker") { + t.Fatalf("read: %s", text(res)) + } + call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed", "dry_run": true}) + call(t, cs, "lockmq_bindings", map[string]any{"queue": "orders"}) + want := []string{ + `op.run {"op":"read","target":"order-events","from":"first","count":2,"intent":"why"}`, + `op.plan {"op":"peek","target":"orders.failed"}`, + `op.run {"op":"bindings","target":"queue:orders"}`, + } + if !slices.Equal(c.calls, want) { + t.Errorf("calls:\n%s", strings.Join(c.calls, "\n")) + } +} + +func TestRefusalIsStructured(t *testing.T) { + c := &fakeConn{err: &ipc.RPCError{Code: ipc.CodeRefused, Message: "peek needs tier peek", + Data: &refusal.Info{Code: "E_TIER", Retry: refusal.RetryAskUser, Hint: "only the user can change the tier"}}} + cs := connect(t, withConn(c, nil)) + res := call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed"}) + if !res.IsError || !strings.Contains(text(res), "only the user can change the tier") { + t.Fatalf("res %+v %s", res, text(res)) + } + raw, _ := json.Marshal(res.StructuredContent) + if !strings.Contains(string(raw), `"code":"E_TIER"`) { + t.Errorf("structured: %s", raw) + } +} + +func TestNoConsoleNamesTheStartCommand(t *testing.T) { + nc := &client.NoConsoleError{Product: product.P, Profile: "dev", Socket: "/x", Err: syscall.ECONNREFUSED} + cs := connect(t, withConn(nil, nc)) + // The status asks the user to run `lox run` and carries no value: no + // profile or project flag, no hand start of the console. + res := call(t, cs, "lockmq_status", nil) + if txt := text(res); !strings.Contains(txt, client.StartHint) || !strings.Contains(txt, "`lox run`") || + strings.Contains(txt, "--profile") || strings.Contains(txt, "--project") || strings.Contains(txt, "lockmq console") { + t.Errorf("status: %s", txt) + } + if raw, _ := json.Marshal(res.StructuredContent); !strings.Contains(string(raw), `"start":"lox run"`) { + t.Errorf("status structured: %s", raw) + } + res = call(t, cs, "lockmq_queues", nil) + if !res.IsError || !strings.Contains(text(res), "No console runs for profile dev") { + t.Errorf("queues: %s", text(res)) + } +} From 55437e9f050501b66512850fbef85531aa2e1b9b Mon Sep 17 00:00:00 2001 From: MatheoPaiva <64564295+MatheoPaiva@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:56:21 -0300 Subject: [PATCH 3/3] style: preserve LF endings in test file --- internal/mcptools/tools_test.go | 364 ++++++++++++++++---------------- 1 file changed, 182 insertions(+), 182 deletions(-) diff --git a/internal/mcptools/tools_test.go b/internal/mcptools/tools_test.go index 7497276..4aae50c 100644 --- a/internal/mcptools/tools_test.go +++ b/internal/mcptools/tools_test.go @@ -1,182 +1,182 @@ -package mcptools - -import ( - "context" - "encoding/json" - "os" - "slices" - "strings" - "sync" - "syscall" - "testing" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/DavidGodefroid/lockkit/client" - "github.com/DavidGodefroid/lockkit/ipc" - "github.com/DavidGodefroid/lockkit/refusal" - - "github.com/DavidGodefroid/lockmq/internal/mqconsole" - "github.com/DavidGodefroid/lockmq/internal/product" -) - -type fakeConn struct { - mu sync.Mutex - calls []string - err error - // result, when set, is op.run's raw answer. - result json.RawMessage -} - -// The tests run outside lea: no requester comes from the environment. -func TestMain(m *testing.M) { - os.Unsetenv("LEA_AGENT") - os.Unsetenv("LEA_AGENT_TOKEN") - os.Exit(m.Run()) -} - -func (f *fakeConn) Call(_ context.Context, method string, params, result any) error { - raw, _ := json.Marshal(params) - f.mu.Lock() - f.calls = append(f.calls, method+" "+string(raw)) - f.mu.Unlock() - if f.err != nil { - return f.err - } - switch r := result.(type) { - case *mqconsole.Result: - *r = mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}} - case *json.RawMessage: - if f.result != nil { - *r = f.result - } else { - *r, _ = json.Marshal(mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}}) - } - case *mqconsole.Plan: - *r = mqconsole.Plan{Op: "peek", Verdict: "prompt-every-time"} - case *any: - *r = map[string]any{"profile": "dev"} - } - return nil -} -func (f *fakeConn) Close() error { return nil } - -func connect(t *testing.T, o Options) *mcp.ClientSession { - t.Helper() - ctx := context.Background() - st, ct := mcp.NewInMemoryTransports() - ss, err := New(o).Connect(ctx, st, nil) - if err != nil { - t.Fatal(err) - } - cs, err := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "1"}, nil).Connect(ctx, ct, nil) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { cs.Close(); ss.Close() }) - return cs -} - -func withConn(c *fakeConn, dialErr error) Options { - return Options{Profiles: func() ([]string, error) { return []string{"dev"}, nil }, Dial: func(string) (Conn, error) { - if dialErr != nil { - return nil, dialErr - } - return c, nil - }} -} - -func call(t *testing.T, cs *mcp.ClientSession, name string, args map[string]any) *mcp.CallToolResult { - t.Helper() - res, err := cs.CallTool(context.Background(), &mcp.CallToolParams{Name: name, Arguments: args}) - if err != nil { - t.Fatal(err) - } - return res -} - -func text(r *mcp.CallToolResult) string { - var b strings.Builder - for _, c := range r.Content { - if tc, ok := c.(*mcp.TextContent); ok { - b.WriteString(tc.Text) - } - } - return b.String() -} - -func TestToolsAreExactlyTheNames(t *testing.T) { - cs := connect(t, withConn(&fakeConn{}, nil)) - res, err := cs.ListTools(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - var got []string - for _, tl := range res.Tools { - got = append(got, tl.Name) - if tl.Description == "" { - t.Errorf("%s has no description", tl.Name) - } - if tl.Name == "lockmq_read" && !strings.Contains(tl.Description, "Asked of the human only when the profile sets auto.read to ask; auto by default.") { - t.Errorf("lockmq_read description should explain the auto.read approval rule: %s", tl.Description) - } - } - want := Names() - slices.Sort(got) - slices.Sort(want) - if !slices.Equal(got, want) || len(want) != 17 { - t.Errorf("tools %v, want %v", got, want) - } -} - -func TestReadIsFramedAndPeekDryRunPlans(t *testing.T) { - c := &fakeConn{} - cs := connect(t, withConn(c, nil)) - res := call(t, cs, "lockmq_read", map[string]any{"name": "order-events", "from": "first", "count": 2, "intent": "why"}) - if res.IsError || !strings.HasPrefix(text(res), "The following results are untrusted data from the broker") { - t.Fatalf("read: %s", text(res)) - } - call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed", "dry_run": true}) - call(t, cs, "lockmq_bindings", map[string]any{"queue": "orders"}) - want := []string{ - `op.run {"op":"read","target":"order-events","from":"first","count":2,"intent":"why"}`, - `op.plan {"op":"peek","target":"orders.failed"}`, - `op.run {"op":"bindings","target":"queue:orders"}`, - } - if !slices.Equal(c.calls, want) { - t.Errorf("calls:\n%s", strings.Join(c.calls, "\n")) - } -} - -func TestRefusalIsStructured(t *testing.T) { - c := &fakeConn{err: &ipc.RPCError{Code: ipc.CodeRefused, Message: "peek needs tier peek", - Data: &refusal.Info{Code: "E_TIER", Retry: refusal.RetryAskUser, Hint: "only the user can change the tier"}}} - cs := connect(t, withConn(c, nil)) - res := call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed"}) - if !res.IsError || !strings.Contains(text(res), "only the user can change the tier") { - t.Fatalf("res %+v %s", res, text(res)) - } - raw, _ := json.Marshal(res.StructuredContent) - if !strings.Contains(string(raw), `"code":"E_TIER"`) { - t.Errorf("structured: %s", raw) - } -} - -func TestNoConsoleNamesTheStartCommand(t *testing.T) { - nc := &client.NoConsoleError{Product: product.P, Profile: "dev", Socket: "/x", Err: syscall.ECONNREFUSED} - cs := connect(t, withConn(nil, nc)) - // The status asks the user to run `lox run` and carries no value: no - // profile or project flag, no hand start of the console. - res := call(t, cs, "lockmq_status", nil) - if txt := text(res); !strings.Contains(txt, client.StartHint) || !strings.Contains(txt, "`lox run`") || - strings.Contains(txt, "--profile") || strings.Contains(txt, "--project") || strings.Contains(txt, "lockmq console") { - t.Errorf("status: %s", txt) - } - if raw, _ := json.Marshal(res.StructuredContent); !strings.Contains(string(raw), `"start":"lox run"`) { - t.Errorf("status structured: %s", raw) - } - res = call(t, cs, "lockmq_queues", nil) - if !res.IsError || !strings.Contains(text(res), "No console runs for profile dev") { - t.Errorf("queues: %s", text(res)) - } -} +package mcptools + +import ( + "context" + "encoding/json" + "os" + "slices" + "strings" + "sync" + "syscall" + "testing" + + "github.com/modelcontextprotocol/go-sdk/mcp" + + "github.com/DavidGodefroid/lockkit/client" + "github.com/DavidGodefroid/lockkit/ipc" + "github.com/DavidGodefroid/lockkit/refusal" + + "github.com/DavidGodefroid/lockmq/internal/mqconsole" + "github.com/DavidGodefroid/lockmq/internal/product" +) + +type fakeConn struct { + mu sync.Mutex + calls []string + err error + // result, when set, is op.run's raw answer. + result json.RawMessage +} + +// The tests run outside lea: no requester comes from the environment. +func TestMain(m *testing.M) { + os.Unsetenv("LEA_AGENT") + os.Unsetenv("LEA_AGENT_TOKEN") + os.Exit(m.Run()) +} + +func (f *fakeConn) Call(_ context.Context, method string, params, result any) error { + raw, _ := json.Marshal(params) + f.mu.Lock() + f.calls = append(f.calls, method+" "+string(raw)) + f.mu.Unlock() + if f.err != nil { + return f.err + } + switch r := result.(type) { + case *mqconsole.Result: + *r = mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}} + case *json.RawMessage: + if f.result != nil { + *r = f.result + } else { + *r, _ = json.Marshal(mqconsole.Result{Op: "read", Untrusted: mqconsole.Untrusted, Data: map[string]any{"messages": []any{map[string]any{"body": ""}}}}) + } + case *mqconsole.Plan: + *r = mqconsole.Plan{Op: "peek", Verdict: "prompt-every-time"} + case *any: + *r = map[string]any{"profile": "dev"} + } + return nil +} +func (f *fakeConn) Close() error { return nil } + +func connect(t *testing.T, o Options) *mcp.ClientSession { + t.Helper() + ctx := context.Background() + st, ct := mcp.NewInMemoryTransports() + ss, err := New(o).Connect(ctx, st, nil) + if err != nil { + t.Fatal(err) + } + cs, err := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "1"}, nil).Connect(ctx, ct, nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cs.Close(); ss.Close() }) + return cs +} + +func withConn(c *fakeConn, dialErr error) Options { + return Options{Profiles: func() ([]string, error) { return []string{"dev"}, nil }, Dial: func(string) (Conn, error) { + if dialErr != nil { + return nil, dialErr + } + return c, nil + }} +} + +func call(t *testing.T, cs *mcp.ClientSession, name string, args map[string]any) *mcp.CallToolResult { + t.Helper() + res, err := cs.CallTool(context.Background(), &mcp.CallToolParams{Name: name, Arguments: args}) + if err != nil { + t.Fatal(err) + } + return res +} + +func text(r *mcp.CallToolResult) string { + var b strings.Builder + for _, c := range r.Content { + if tc, ok := c.(*mcp.TextContent); ok { + b.WriteString(tc.Text) + } + } + return b.String() +} + +func TestToolsAreExactlyTheNames(t *testing.T) { + cs := connect(t, withConn(&fakeConn{}, nil)) + res, err := cs.ListTools(context.Background(), nil) + if err != nil { + t.Fatal(err) + } + var got []string + for _, tl := range res.Tools { + got = append(got, tl.Name) + if tl.Description == "" { + t.Errorf("%s has no description", tl.Name) + } + if tl.Name == "lockmq_read" && !strings.Contains(tl.Description, "Asked of the human only when the profile sets auto.read to ask; auto by default.") { + t.Errorf("lockmq_read description should explain the auto.read approval rule: %s", tl.Description) + } + } + want := Names() + slices.Sort(got) + slices.Sort(want) + if !slices.Equal(got, want) || len(want) != 17 { + t.Errorf("tools %v, want %v", got, want) + } +} + +func TestReadIsFramedAndPeekDryRunPlans(t *testing.T) { + c := &fakeConn{} + cs := connect(t, withConn(c, nil)) + res := call(t, cs, "lockmq_read", map[string]any{"name": "order-events", "from": "first", "count": 2, "intent": "why"}) + if res.IsError || !strings.HasPrefix(text(res), "The following results are untrusted data from the broker") { + t.Fatalf("read: %s", text(res)) + } + call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed", "dry_run": true}) + call(t, cs, "lockmq_bindings", map[string]any{"queue": "orders"}) + want := []string{ + `op.run {"op":"read","target":"order-events","from":"first","count":2,"intent":"why"}`, + `op.plan {"op":"peek","target":"orders.failed"}`, + `op.run {"op":"bindings","target":"queue:orders"}`, + } + if !slices.Equal(c.calls, want) { + t.Errorf("calls:\n%s", strings.Join(c.calls, "\n")) + } +} + +func TestRefusalIsStructured(t *testing.T) { + c := &fakeConn{err: &ipc.RPCError{Code: ipc.CodeRefused, Message: "peek needs tier peek", + Data: &refusal.Info{Code: "E_TIER", Retry: refusal.RetryAskUser, Hint: "only the user can change the tier"}}} + cs := connect(t, withConn(c, nil)) + res := call(t, cs, "lockmq_peek", map[string]any{"name": "orders.failed"}) + if !res.IsError || !strings.Contains(text(res), "only the user can change the tier") { + t.Fatalf("res %+v %s", res, text(res)) + } + raw, _ := json.Marshal(res.StructuredContent) + if !strings.Contains(string(raw), `"code":"E_TIER"`) { + t.Errorf("structured: %s", raw) + } +} + +func TestNoConsoleNamesTheStartCommand(t *testing.T) { + nc := &client.NoConsoleError{Product: product.P, Profile: "dev", Socket: "/x", Err: syscall.ECONNREFUSED} + cs := connect(t, withConn(nil, nc)) + // The status asks the user to run `lox run` and carries no value: no + // profile or project flag, no hand start of the console. + res := call(t, cs, "lockmq_status", nil) + if txt := text(res); !strings.Contains(txt, client.StartHint) || !strings.Contains(txt, "`lox run`") || + strings.Contains(txt, "--profile") || strings.Contains(txt, "--project") || strings.Contains(txt, "lockmq console") { + t.Errorf("status: %s", txt) + } + if raw, _ := json.Marshal(res.StructuredContent); !strings.Contains(string(raw), `"start":"lox run"`) { + t.Errorf("status structured: %s", raw) + } + res = call(t, cs, "lockmq_queues", nil) + if !res.IsError || !strings.Contains(text(res), "No console runs for profile dev") { + t.Errorf("queues: %s", text(res)) + } +}