Supermarket queues CRM work in crm_actions and never writes to EspoCRM itself. A Hermes cron job (supermarket-crm, set up by `supermarket hermes`) runs every 15 minutes behind a monitor script that prints Supermarket's crm-state file, so unchanged state skips the AI run. Hermes calls the new MCP tools crm_pending_actions, crm_report_result and crm_ingest, creating each record through the Core's MCP hub unless a match exists, and copying Leads, Accounts and Contacts back every 6 hours for duplicate checks. Schema 5 adds crm_actions and crm_records. Prospects -> CRM queue shows the queue, a test Lead, sync, Retry and Cancel. When an action fails with a connection error, the page checks EspoCRM from this computer and says whether the CRM is down or the hub can't reach it, with a note to send to the Core's maintainer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
3.5 KiB
Go
99 lines
3.5 KiB
Go
package mcpserver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
|
|
"supermarket/internal/crm"
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// A CRM run as Hermes does it: fetch the work, report each action, send
|
|
// the synced records.
|
|
func TestCRMRun(t *testing.T) {
|
|
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.Close()
|
|
a, err := crm.Enqueue(d, crm.Action{Key: "test:1", Entity: "Lead",
|
|
Fields: map[string]any{"firstName": "Supermarket", "lastName": "Test"}, Match: map[string]string{"lastName": "Test"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
ct, st := mcp.NewInMemoryTransports()
|
|
if _, err := newServer(d, "test").Connect(ctx, st, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session, err := mcp.NewClient(&mcp.Implementation{Name: "hermes"}, nil).Connect(ctx, ct, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer session.Close()
|
|
call := func(name string, args map[string]any) (string, bool) {
|
|
t.Helper()
|
|
res, err := session.CallTool(ctx, &mcp.CallToolParams{Name: name, Arguments: args})
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
return res.Content[0].(*mcp.TextContent).Text, res.IsError
|
|
}
|
|
|
|
text, _ := call("crm_pending_actions", nil)
|
|
var pending pendingResult
|
|
if err := json.Unmarshal([]byte(text), &pending); err != nil {
|
|
t.Fatalf("%v: %s", err, text)
|
|
}
|
|
if len(pending.Actions) != 1 || pending.Actions[0].ID != a.ID || pending.Actions[0].Entity != "Lead" ||
|
|
pending.Actions[0].FindExisting["lastName"] != "Test" || pending.Sync == nil || pending.Sync.ModifiedSince != "" ||
|
|
!strings.Contains(pending.Instructions, "nothing else") {
|
|
t.Fatalf("pending: %s", text)
|
|
}
|
|
// Claimed: a second call (another run) doesn't get it again, and the
|
|
// sync was started, so it isn't asked for twice either... until done.
|
|
text, _ = call("crm_pending_actions", nil)
|
|
if strings.Contains(text, `"id": 1,`) {
|
|
t.Errorf("handed out twice: %s", text)
|
|
}
|
|
|
|
if msg, isErr := call("crm_report_result", map[string]any{"id": a.ID, "outcome": "created"}); !isErr || !strings.Contains(msg, "EspoCRM ID") {
|
|
t.Errorf("created without an id: %s", msg)
|
|
}
|
|
if _, isErr := call("crm_report_result", map[string]any{"id": a.ID, "outcome": "Created", "espo_id": "6555abc"}); isErr {
|
|
t.Error("report refused")
|
|
}
|
|
if got, _ := crm.Get(d, a.ID); got.Status != crm.StatusDone || got.EspoID != "6555abc" {
|
|
t.Errorf("after report: %+v", got)
|
|
}
|
|
|
|
if msg, isErr := call("crm_ingest", map[string]any{"entity": "Opportunity", "records": []any{}}); !isErr {
|
|
t.Errorf("unknown entity accepted: %s", msg)
|
|
}
|
|
msg, _ := call("crm_ingest", map[string]any{"entity": "lead", "records": []any{
|
|
map[string]any{"id": "6555abc", "firstName": "Supermarket", "lastName": "Test", "emailAddress": "t@acme.example"},
|
|
map[string]any{"name": "no id"},
|
|
}})
|
|
if !strings.Contains(msg, "Stored 1 Lead") || !strings.Contains(msg, "Skipped 1") {
|
|
t.Errorf("ingest: %s", msg)
|
|
}
|
|
if msg, _ := call("crm_ingest", map[string]any{"entity": "Account", "records": []any{
|
|
map[string]any{"id": "A1", "name": "Acme", "website": "acme.example"}}, "done": true}); !strings.Contains(msg, "Sync finished") {
|
|
t.Errorf("last batch: %s", msg)
|
|
}
|
|
if dups, _ := crm.Duplicates(d, "", "acme.example"); len(dups) != 2 {
|
|
t.Errorf("synced records: %+v", dups)
|
|
}
|
|
// Synced: the next run asks for changes since then, not for everything.
|
|
text, _ = call("crm_pending_actions", nil)
|
|
if strings.Contains(text, `"sync"`) {
|
|
t.Errorf("sync asked for again right after one: %s", text)
|
|
}
|
|
}
|