Files
platform/cmd/cmsctl/commerce_commands_test.go

59 lines
2.2 KiB
Go

package main
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCommerceCartAddUsesBaseURLAndPayload(t *testing.T) {
var gotMethod, gotPath, gotBody string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{ "id": "cart-1", "items": [] }`)
}))
defer server.Close()
client := newClient(Config{BaseURL: server.URL}, "token", "", 0)
var out strings.Builder
if err := runCommerceCarts(context.Background(), client, []string{"add", "cart-1", "--sku", "ND10200818HB0001", "--quantity", "2"}, strings.NewReader(""), &out); err != nil {
t.Fatal(err)
}
if gotMethod != http.MethodPost || gotPath != "/cart/byid/cart-1" {
t.Fatalf("request = %s %s, want POST /cart/byid/cart-1", gotMethod, gotPath)
}
if gotBody != `{"quantity":2,"sku":"ND10200818HB0001"}` {
t.Fatalf("body = %s", gotBody)
}
}
func TestCommercePromotionUpsertUsesMCPHandshakeAndToolCall(t *testing.T) {
var methods []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
methods = append(methods, string(body))
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"jsonrpc":"2.0","id":1,"result":{}}`)
}))
defer server.Close()
client := newClient(Config{BaseURL: server.URL}, "token", "", 0)
promotion := `{"id":"promo-test","name":"Test","status":"active","conditions":[],"actions":[]}`
var out strings.Builder
if err := runCommercePromotions(context.Background(), client, []string{"upsert", "--input", "-"}, strings.NewReader(promotion), &out); err != nil {
t.Fatal(err)
}
if len(methods) != 3 {
t.Fatalf("MCP request count = %d, want 3", len(methods))
}
if !strings.Contains(methods[0], `"method":"initialize"`) || !strings.Contains(methods[1], `"method":"notifications/initialized"`) || !strings.Contains(methods[2], `"method":"tools/call"`) || !strings.Contains(methods[2], `"name":"upsert_promotion"`) {
t.Fatalf("unexpected MCP requests: %v", methods)
}
}