101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package agents
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"g.fc.run/misc/byre/internal/api"
|
|
"g.fc.run/misc/byre/internal/store"
|
|
)
|
|
|
|
func TestPaneFreed(t *testing.T) {
|
|
ctx := context.Background()
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "byre.db"), store.DefaultPortFirst, func(api.Event) {})
|
|
if err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
t.Cleanup(func() { st.Close() })
|
|
|
|
now := time.Now().UTC()
|
|
_, err = st.EnsureUser(ctx, "alice", "Alice")
|
|
if err != nil {
|
|
t.Fatalf("ensure user: %v", err)
|
|
}
|
|
err = st.CreateProject(ctx, api.Project{Id: "p1", Name: "demo", RepoPath: "/nonexistent", DefaultBranch: "main", Remote: "none", CreatedBy: "alice", CreatedAt: now})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
for _, id := range []string{"t1", "t2"} {
|
|
err = st.CreateTask(ctx, api.Task{Id: id, ProjectId: "p1", Title: "Fix " + id, Branch: id, WorktreePath: "/nonexistent", BaseRef: "x", Status: api.TaskStatusActive, CreatedBy: "alice", CreatedAt: now}, "alice")
|
|
if err != nil {
|
|
t.Fatalf("create task: %v", err)
|
|
}
|
|
}
|
|
err = st.CreateSession(ctx, api.AgentSession{Id: "s1", TaskId: "t1", Vendor: api.Claude, VendorSessionId: "s1", State: api.AgentSessionStateIdle, Role: api.Implementer, CreatedBy: "alice", CreatedAt: now}, "p1")
|
|
if err != nil {
|
|
t.Fatalf("create session: %v", err)
|
|
}
|
|
err = st.CreateInboxItem(ctx, api.InboxItem{
|
|
Id: "r1", ProjectId: "p1", TaskId: "t1", AgentSessionId: "s1", ThreadId: "r1",
|
|
Kind: api.Report, Priority: api.Fyi, Payload: map[string]any{"text": "done"},
|
|
State: api.Open, CreatedAt: now, Responses: []api.InboxResponse{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("file report: %v", err)
|
|
}
|
|
|
|
m := New(st, t.TempDir(), t.TempDir(), t.TempDir())
|
|
t1, t2, p1 := "t1", "t2", "p1"
|
|
focus := func(tab api.TaskTab) {
|
|
t.Helper()
|
|
err := st.SetFocus(ctx, "alice", api.Focus{TaskId: &t1, ProjectId: &p1, Tabs: []api.TaskTab{tab}, Tab: tab}, "alice")
|
|
if err != nil {
|
|
t.Fatalf("set focus: %v", err)
|
|
}
|
|
}
|
|
freed := func(kind, taskID string) string {
|
|
t.Helper()
|
|
text, err := m.freedText(ctx, "alice", api.Event{Kind: kind, TaskId: &taskID})
|
|
if err != nil {
|
|
t.Fatalf("freed text: %v", err)
|
|
}
|
|
return text
|
|
}
|
|
|
|
focus(api.TabInbox)
|
|
if text := freed("inbox.updated", t1); text != "" {
|
|
t.Fatalf("freed with an item still open: %q", text)
|
|
}
|
|
|
|
err = st.WithdrawItem(ctx, "r1")
|
|
if err != nil {
|
|
t.Fatalf("withdraw: %v", err)
|
|
}
|
|
text := freed("inbox.updated", t1)
|
|
if !strings.HasPrefix(text, "Your right pane is free: ") || !strings.HasSuffix(text, "task t1] has nothing left in its inbox.") {
|
|
t.Fatalf("emptied inbox: %q", text)
|
|
}
|
|
if text := freed("inbox.updated", t2); text != "" {
|
|
t.Fatalf("freed by another task: %q", text)
|
|
}
|
|
|
|
focus(api.TabTranscript)
|
|
if text := freed("inbox.updated", t1); text != "" {
|
|
t.Fatalf("freed on the transcript tab: %q", text)
|
|
}
|
|
|
|
_, err = st.MergeTask(ctx, "t1", "abc", "origin/main", "alice")
|
|
if err != nil {
|
|
t.Fatalf("merge: %v", err)
|
|
}
|
|
if text := freed("task.merged", t1); !strings.HasSuffix(text, "task t1] is merged.") {
|
|
t.Fatalf("merged: %q", text)
|
|
}
|
|
if text := freed("inbox.updated", t1); text != "" {
|
|
t.Fatalf("inbox.updated on a merged task: %q", text)
|
|
}
|
|
}
|