Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138//go:build unix
// /btw asks beside a held turn through the real daemon: the answer shows in// the panel, the parent transcript gains nothing, Esc leaves the turn// running, and Continue opens a session that ends with the exchange.package e2e
import ( "context" "encoding/json" "strings" "sync" "testing" "time"
"albedo/cli/internal/daemon" "albedo/cli/internal/tui" tea "charm.land/bubbletea/v2")
func TestTUIBtwBesideHeldTurnAndContinue(t *testing.T) { release := make(chan struct{}) var releaseOnce sync.Once releaseTurn := func() { releaseOnce.Do(func() { close(release) }) } defer releaseTurn() profile := providerRoute(t, func(request map[string]any) string { encoded, _ := json.Marshal(request) if strings.Contains(string(encoded), "side-question") { return "side answer: retries are in retry.go" } if lastUserText(request) == "start the long task" { <-release return "main task done" } return echoReply(request) }) t.Parallel() workspace := t.TempDir() session := daemonSession(t, newSession(t, workspace)) connection := conn(t) client := daemon.NewChatClient(connection, session.ID) d := driveTUI(t, &session) t.Cleanup(func() { d.App.Chat.Close() }) d.connected() ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) defer cancel() inbox := make(chan tea.Msg, 64)
d.App.Chat.TextArea.SetValue("start the long task") streamMessages(ctx, d.Update(tea.KeyPressMsg{Code: tea.KeyEnter}), inbox) deadline := time.Now().Add(30 * time.Second) for len(suite.provider.requests(profile)) == 0 { if time.Now().After(deadline) { t.Fatal("the held turn never reached the provider") } time.Sleep(20 * time.Millisecond) }
d.App.Chat.TextArea.SetValue("/btw what owns retries?") streamMessages(ctx, d.Update(tea.KeyPressMsg{Code: tea.KeyEnter}), inbox) var exchange *daemon.SideQuestion for time.Now().Before(deadline) { current, err := daemon.GetSession(t.Context(), connection, session.ID) if err != nil { t.Fatal(err) } if current.SideQuestion != nil && current.SideQuestion.Status == "answered" { exchange = current.SideQuestion break } time.Sleep(20 * time.Millisecond) } if exchange == nil { t.Fatalf("side question never answered; view:\n%s", d.View()) } streamMessages(ctx, d.Update(tui.ChatStreamEventMsg{SessionID: session.ID, Generation: d.App.Chat.Generation, Event: daemon.StreamEvent{Type: daemon.EventSideQuestion, SideQuestion: exchange}}), inbox) if view := d.View(); !strings.Contains(view, "side answer: retries are in retry.go") { t.Fatalf("answer not in the panel:\n%s", view) } for _, entry := range d.App.Chat.History.Entries() { if strings.Contains(entry.Text, "what owns retries?") || strings.Contains(entry.Text, "side answer") { t.Fatalf("side exchange entered the transcript: %#v", entry) } }
streamMessages(ctx, d.Update(tea.KeyPressMsg{Code: tea.KeyEscape}), inbox) status, err := client.GetStatus(t.Context()) if err != nil { t.Fatal(err) } if d.App.Chat.BtwOpen() || !status.Running { t.Fatalf("esc closed=%v but turn running=%v", !d.App.Chat.BtwOpen(), status.Running) }
d.App.Chat.TextArea.SetValue("/btw") streamMessages(ctx, d.Update(tea.KeyPressMsg{Code: tea.KeyEnter}), inbox) if view := d.View(); !strings.Contains(view, "side answer: retries are in retry.go") { t.Fatalf("/btw did not reopen the answer:\n%s", view) }
// Continue switches sessions and starts the new chat's stream, which // never returns by itself, so commands run side by side. streamMessages(ctx, d.Update(tea.KeyPressMsg{Code: tea.KeyEnter}), inbox) for d.App.ActiveSession == nil || d.App.ActiveSession.ID == session.ID || !strings.Contains(d.View(), "side answer: retries are in retry.go") { select { case msg := <-inbox: streamMessages(ctx, d.Update(msg), inbox) case <-ctx.Done(): t.Fatalf("continue never opened the new session:\n%s", d.View()) } } continuedID := d.App.ActiveSession.ID cleanupSession(t, continuedID) preview, err := daemon.GetSessionPreview(t.Context(), connection, continuedID, 20) if err != nil { t.Fatal(err) } if len(preview.Items) < 2 { t.Fatalf("continued session history: %+v", preview.Items) } last := preview.Items[len(preview.Items)-2:] if !strings.Contains(last[0].Preview, "what owns retries?") || !strings.Contains(last[1].Preview, "side answer: retries are in retry.go") { t.Fatalf("continued session does not end with the exchange: %+v", last) }
releaseTurn() waitIdle(t, session.ID, profile, 2) parent, err := daemon.GetSessionPreview(t.Context(), connection, session.ID, 20) if err != nil { t.Fatal(err) } for _, item := range parent.Items { if strings.Contains(item.Preview, "what owns retries?") || strings.Contains(item.Preview, "side answer") { t.Fatalf("side exchange entered the parent transcript: %+v", item) } }}