Complete inbox CLI implementation

This commit is contained in:
2026-03-19 03:15:17 +08:00
parent 11bee52ff4
commit c3314cd9cf
15 changed files with 1524 additions and 43 deletions
+289 -5
View File
@@ -3,8 +3,10 @@ package inbox
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
func TestInboxLifecycle(t *testing.T) {
@@ -238,9 +240,294 @@ func TestInboxFailLifecycle(t *testing.T) {
}
}
func TestInboxRenewWaitReplyAndCancel(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-c",
"--subject", "Investigate auth edge case",
"--summary", "Check auth redirect behavior",
"--run", "run_blog_003",
"--task", "T3",
)
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-c",
"--thread", threadID,
"--lease-seconds", "300",
)
renewOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"renew",
"--agent", "worker-c",
"--thread", threadID,
"--lease-seconds", "600",
)
var renewResp map[string]any
mustDecodeJSON(t, renewOut, &renewResp)
if got := nestedString(t, renewResp, "data", "message", "summary"); got != "lease renewed" {
t.Fatalf("expected lease renewed summary, got %q", got)
}
blockedOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"update",
"--agent", "worker-c",
"--thread", threadID,
"--status", "blocked",
"--summary", "Need policy decision",
"--body", "Should guest users be redirected to login or shown a 403 page?",
)
var blockedResp map[string]any
mustDecodeJSON(t, blockedOut, &blockedResp)
blockedMessageID := nestedString(t, blockedResp, "data", "message", "message_id")
type commandResult struct {
stdout string
stderr string
err error
}
waitCh := make(chan commandResult, 1)
go func() {
stdout, stderr, err := executeInboxCommand(
"--db", dbPath,
"--json",
"wait-reply",
"--thread", threadID,
"--after-message", blockedMessageID,
"--timeout-seconds", "2",
)
waitCh <- commandResult{stdout: stdout, stderr: stderr, err: err}
}()
time.Sleep(200 * time.Millisecond)
runInboxCommand(
t,
"--db", dbPath,
"--json",
"reply",
"--from", "leader",
"--to", "worker-c",
"--thread", threadID,
"--summary", "Redirect to login",
"--body", "Redirect guests to login for the MVP.",
)
var waitResult commandResult
select {
case waitResult = <-waitCh:
case <-time.After(3 * time.Second):
t.Fatal("wait-reply command did not return")
}
if waitResult.err != nil {
t.Fatalf("wait-reply failed: %v\nstderr:\n%s", waitResult.err, waitResult.stderr)
}
var waitResp map[string]any
mustDecodeJSON(t, waitResult.stdout, &waitResp)
if woke, ok := nestedValue(t, waitResp, "data", "woke").(bool); !ok || !woke {
t.Fatalf("expected wait-reply to wake, got %#v", nestedValue(t, waitResp, "data", "woke"))
}
if kind := nestedString(t, waitResp, "data", "message", "kind"); kind != "answer" {
t.Fatalf("expected answer wake message, got %q", kind)
}
cancelOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"cancel",
"--agent", "leader",
"--thread", threadID,
"--reason", "Task superseded by a larger refactor",
)
var cancelResp map[string]any
mustDecodeJSON(t, cancelOut, &cancelResp)
if status := nestedString(t, cancelResp, "data", "thread", "status"); status != "cancelled" {
t.Fatalf("expected cancelled thread, got %q", status)
}
}
func TestInboxWatchListUnreadAndAppend(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "coord.db")
bodyPath := filepath.Join(tempDir, "task.md")
if err := os.WriteFile(bodyPath, []byte("Implement the initial admin post editor."), 0o644); err != nil {
t.Fatalf("write body file: %v", err)
}
runInboxCommand(t, "--db", dbPath, "--json", "init")
type commandResult struct {
stdout string
stderr string
err error
}
watchCh := make(chan commandResult, 1)
go func() {
stdout, stderr, err := executeInboxCommand(
"--db", dbPath,
"--json",
"watch",
"--agent", "worker-d",
"--status", "pending",
"--timeout-seconds", "2",
)
watchCh <- commandResult{stdout: stdout, stderr: stderr, err: err}
}()
time.Sleep(200 * time.Millisecond)
sendOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-d",
"--subject", "Build admin editor",
"--summary", "Create the first editor screen",
"--body-file", bodyPath,
"--run", "run_blog_004",
"--task", "T4",
)
var sendResp map[string]any
mustDecodeJSON(t, sendOut, &sendResp)
threadID := nestedString(t, sendResp, "data", "thread", "thread_id")
var watchResult commandResult
select {
case watchResult = <-watchCh:
case <-time.After(3 * time.Second):
t.Fatal("watch command did not return")
}
if watchResult.err != nil {
t.Fatalf("watch failed: %v\nstderr:\n%s", watchResult.err, watchResult.stderr)
}
var watchResp map[string]any
mustDecodeJSON(t, watchResult.stdout, &watchResp)
if woke, ok := nestedValue(t, watchResp, "data", "woke").(bool); !ok || !woke {
t.Fatalf("expected watch to wake, got %#v", nestedValue(t, watchResp, "data", "woke"))
}
if watchedThreadID := nestedString(t, watchResp, "data", "thread", "thread_id"); watchedThreadID != threadID {
t.Fatalf("expected watch on thread %s, got %s", threadID, watchedThreadID)
}
fetchOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"fetch",
"--agent", "worker-d",
"--status", "pending",
"--unread",
)
var fetchResp map[string]any
mustDecodeJSON(t, fetchOut, &fetchResp)
fetchedThreads, ok := nestedValue(t, fetchResp, "data", "threads").([]any)
if !ok || len(fetchedThreads) != 1 {
t.Fatalf("expected one unread pending thread, got %#v", nestedValue(t, fetchResp, "data", "threads"))
}
listOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"list",
"--assigned-to", "worker-d",
"--status", "pending",
)
var listResp map[string]any
mustDecodeJSON(t, listOut, &listResp)
listedThreads, ok := nestedValue(t, listResp, "data", "threads").([]any)
if !ok || len(listedThreads) != 1 {
t.Fatalf("expected one listed thread, got %#v", nestedValue(t, listResp, "data", "threads"))
}
runInboxCommand(
t,
"--db", dbPath,
"--json",
"send",
"--from", "leader",
"--to", "worker-d",
"--thread", threadID,
"--summary", "Use a markdown editor",
"--body", "Prefer a textarea-based markdown editor for v1.",
)
showOut := runInboxCommand(
t,
"--db", dbPath,
"--json",
"show",
"--thread", threadID,
)
var showResp map[string]any
mustDecodeJSON(t, showOut, &showResp)
messages, ok := nestedValue(t, showResp, "data", "messages").([]any)
if !ok || len(messages) != 2 {
t.Fatalf("expected two messages after append, got %#v", nestedValue(t, showResp, "data", "messages"))
}
firstMessage, ok := messages[0].(map[string]any)
if !ok {
t.Fatalf("expected first message object, got %#v", messages[0])
}
if firstMessage["body"] != "Implement the initial admin post editor." {
t.Fatalf("expected body-file content in first message, got %#v", firstMessage["body"])
}
}
func runInboxCommand(t *testing.T, args ...string) string {
t.Helper()
stdout, stderr, err := executeInboxCommand(args...)
if err != nil {
t.Fatalf("execute inbox command %v: %v\nstderr:\n%s", args, err, stderr)
}
return stdout
}
func executeInboxCommand(args ...string) (string, string, error) {
cmd := NewRootCmd()
var stdout bytes.Buffer
var stderr bytes.Buffer
@@ -248,11 +535,8 @@ func runInboxCommand(t *testing.T, args ...string) string {
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()
err := cmd.Execute()
return stdout.String(), stderr.String(), err
}
func mustDecodeJSON(t *testing.T, raw string, target any) {