Files
ai-workflow-skill/internal/cli/inbox/integration_test.go
T

293 lines
6.8 KiB
Go

package inbox
import (
"bytes"
"encoding/json"
"path/filepath"
"testing"
)
func TestInboxLifecycle(t *testing.T) {
t.Parallel()
dbPath := filepath.Join(t.TempDir(), "coord.db")
initOut := runInboxCommand(t, "--db", dbPath, "--json", "init")
var initResp map[string]any
mustDecodeJSON(t, initOut, &initResp)
if initResp["ok"] != true {
t.Fatalf("expected init ok=true, got %#v", initResp)
}
sendOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-a",
"--subject", "Implement feature X",
"--summary", "Add retry policy",
"--body", "Implement retry handling for the HTTP client.",
"--run", "run_blog_001",
"--task", "T1",
)
var sendResp map[string]any
mustDecodeJSON(t, sendOut, &sendResp)
threadID := nestedString(t, sendResp, "data", "thread", "thread_id")
threadStatus := nestedString(t, sendResp, "data", "thread", "status")
if threadStatus != "pending" {
t.Fatalf("expected pending thread, got %q", threadStatus)
}
fetchOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"fetch",
"--agent", "worker-a",
"--status", "pending",
)
var fetchResp map[string]any
mustDecodeJSON(t, fetchOut, &fetchResp)
threadsValue := nestedValue(t, fetchResp, "data", "threads")
threads, ok := threadsValue.([]any)
if !ok || len(threads) != 1 {
t.Fatalf("expected one fetched thread, got %#v", threadsValue)
}
claimOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"claim",
"--agent", "worker-a",
"--thread", threadID,
"--lease-seconds", "300",
)
var claimResp map[string]any
mustDecodeJSON(t, claimOut, &claimResp)
claimedStatus := nestedString(t, claimResp, "data", "thread", "status")
if claimedStatus != "claimed" {
t.Fatalf("expected claimed thread, got %q", claimedStatus)
}
updateOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"update",
"--agent", "worker-a",
"--thread", threadID,
"--status", "in_progress",
"--summary", "Implementation started",
"--body", "Scanning current HTTP client usage.",
)
var updateResp map[string]any
mustDecodeJSON(t, updateOut, &updateResp)
updatedStatus := nestedString(t, updateResp, "data", "thread", "status")
if updatedStatus != "in_progress" {
t.Fatalf("expected in_progress thread, got %q", updatedStatus)
}
blockedOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"update",
"--agent", "worker-a",
"--thread", threadID,
"--status", "blocked",
"--summary", "Need timeout decision",
"--payload-json", `{"question":"Should retries apply to read timeouts?"}`,
)
var blockedResp map[string]any
mustDecodeJSON(t, blockedOut, &blockedResp)
blockedStatus := nestedString(t, blockedResp, "data", "thread", "status")
if blockedStatus != "blocked" {
t.Fatalf("expected blocked thread, got %q", blockedStatus)
}
replyOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"reply",
"--from", "leader",
"--to", "worker-a",
"--thread", threadID,
"--summary", "Retry read timeouts",
"--body", "Yes, include read timeouts in the retry policy.",
)
var replyResp map[string]any
mustDecodeJSON(t, replyOut, &replyResp)
replyKind := nestedString(t, replyResp, "data", "message", "kind")
if replyKind != "answer" {
t.Fatalf("expected answer reply, got %q", replyKind)
}
doneOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"done",
"--agent", "worker-a",
"--thread", threadID,
"--summary", "Retry policy implemented",
"--body", "The HTTP client now retries the selected transient failures.",
)
var doneResp map[string]any
mustDecodeJSON(t, doneOut, &doneResp)
doneStatus := nestedString(t, doneResp, "data", "thread", "status")
if doneStatus != "done" {
t.Fatalf("expected done thread, got %q", doneStatus)
}
showOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"show",
"--thread", threadID,
)
var showResp map[string]any
mustDecodeJSON(t, showOut, &showResp)
showStatus := nestedString(t, showResp, "data", "thread", "status")
if showStatus != "done" {
t.Fatalf("expected show status done, got %q", showStatus)
}
messagesValue := nestedValue(t, showResp, "data", "messages")
messages, ok := messagesValue.([]any)
if !ok || len(messages) != 6 {
t.Fatalf("expected six messages in thread history, got %#v", messagesValue)
}
}
func TestInboxFailLifecycle(t *testing.T) {
t.Parallel()
dbPath := filepath.Join(t.TempDir(), "coord.db")
runInboxCommand(t, "--db", dbPath, "--json", "init")
sendOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-b",
"--subject", "Investigate failing migration",
"--summary", "Check migration failure",
"--run", "run_blog_002",
"--task", "T2",
)
var sendResp map[string]any
mustDecodeJSON(t, sendOut, &sendResp)
threadID := nestedString(t, sendResp, "data", "thread", "thread_id")
runInboxCommand(
t,
"--db", dbPath,
"--json",
"claim",
"--agent", "worker-b",
"--thread", threadID,
)
failOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"fail",
"--agent", "worker-b",
"--thread", threadID,
"--summary", "Migration failed",
"--body", "The migration cannot proceed because the prior schema is inconsistent.",
)
var failResp map[string]any
mustDecodeJSON(t, failOut, &failResp)
failStatus := nestedString(t, failResp, "data", "thread", "status")
if failStatus != "failed" {
t.Fatalf("expected failed thread, got %q", failStatus)
}
showOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"show",
"--thread", threadID,
)
var showResp map[string]any
mustDecodeJSON(t, showOut, &showResp)
showStatus := nestedString(t, showResp, "data", "thread", "status")
if showStatus != "failed" {
t.Fatalf("expected show status failed, got %q", showStatus)
}
}
func runInboxCommand(t *testing.T, args ...string) string {
t.Helper()
cmd := NewRootCmd()
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.SetOut(&stdout)
cmd.SetErr(&stderr)
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("execute inbox command %v: %v\nstderr:\n%s", args, err, stderr.String())
}
return stdout.String()
}
func mustDecodeJSON(t *testing.T, raw string, target any) {
t.Helper()
if err := json.Unmarshal([]byte(raw), target); err != nil {
t.Fatalf("decode json %q: %v", raw, err)
}
}
func nestedString(t *testing.T, value map[string]any, keys ...string) string {
t.Helper()
current := nestedValue(t, value, keys...)
str, ok := current.(string)
if !ok {
t.Fatalf("expected string at %v, got %#v", keys, current)
}
return str
}
func nestedValue(t *testing.T, value map[string]any, keys ...string) any {
t.Helper()
var current any = value
for _, key := range keys {
obj, ok := current.(map[string]any)
if !ok {
t.Fatalf("expected object at %q in %v, got %#v", key, keys, current)
}
current, ok = obj[key]
if !ok {
t.Fatalf("missing key %q in %v", key, keys)
}
}
return current
}