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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204package tui
import ( tea "charm.land/bubbletea/v2" "testing"
"albedo/cli/internal/daemon")
func queuedPending(id, text string) PendingUserTurn { return PendingUserTurn{OperationID: id, Text: text, Queued: true}}
func heldMsg(m ChatModel, id string) ChatQueueHeldMsg { return ChatQueueHeldMsg{SessionID: m.SessionID, Generation: m.Generation, ID: id, LeaseMs: 30000}}
// Catches an edit that loses the draft typed before it, or loads the message// without its attachments.func TestEditLoadsQueuedMessageAndKeepsTheDraft(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) image := daemon.ImageAttachment{ImageMetadata: daemon.ImageMetadata{MimeType: "image/png"}} m.TextArea.SetValue("my draft") m.pendingUsers = []PendingUserTurn{queuedPending("a", "first"), queuedPending("b", "second [Image #1]")} m.pendingUsers[1].Images = []daemon.ImageAttachment{image}
if cmd := m.startQueueEdit(""); cmd == nil { t.Fatal("Up with a queued message returned no hold") } m.handleQueueEdit(heldMsg(m, "b"))
if m.editing == nil || m.editing.id != "b" { t.Fatalf("editing = %+v, want message b", m.editing) } if got := m.TextArea.Value(); got != "second [Image #1]" { t.Fatalf("composer = %q, want the message text", got) } if _, items := m.Images.resolve(m.TextArea.Value()); len(items) != 1 { t.Fatalf("composer holds %d images, want 1", len(items)) } if m.editing.stash.text != "my draft" { t.Fatalf("stash = %q, want the draft", m.editing.stash.text) }}
// Catches a step to an earlier message that releases the wrong hold or drops// the draft kept for the edit in progress.func TestUpStepsToTheMessageBefore(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.pendingUsers = []PendingUserTurn{queuedPending("a", "first"), queuedPending("b", "second")} m.handleQueueEdit(heldMsg(m, "b")) m.queueHold = false
if m.startQueueEdit(m.editing.id) == nil { t.Fatal("Up on an unchanged edit did not hold the message before") } m.handleQueueEdit(heldMsg(m, "a"))
if m.editing.id != "a" || m.TextArea.Value() != "first" { t.Fatalf("after step: id %q, composer %q", m.editing.id, m.TextArea.Value()) }}
// Catches a second Up that holds the message again while the first hold is in// flight, which would release the hold the edit depends on.func TestSecondUpWaitsForTheHoldInFlight(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.pendingUsers = []PendingUserTurn{queuedPending("a", "first")} if m.startQueueEdit("") == nil { t.Fatal("first Up returned no hold") } if cmd := m.startQueueEdit(""); cmd != nil { t.Fatal("second Up held while the first hold was in flight") }}
// Catches a saved edit that leaves the old text on the message or keeps the// draft out of the composer.func TestSavedEditUpdatesTheMessageAndRestoresTheDraft(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.TextArea.SetValue("draft") m.pendingUsers = []PendingUserTurn{queuedPending("a", "first")} m.handleQueueEdit(heldMsg(m, "a")) m.TextArea.SetValue("first, fixed") if cmd := m.submitQueueEdit(); cmd == nil { t.Fatal("submitting an edit returned no save") }
m.handleQueueEdit(ChatQueueEditedMsg{SessionID: "s", Generation: m.Generation, ID: "a", Text: "first, fixed"})
if m.editing != nil { t.Fatal("editing is still set after the save") } if m.pendingUsers[0].Text != "first, fixed" { t.Fatalf("message = %q, want the edited text", m.pendingUsers[0].Text) } if m.TextArea.Value() != "draft" { t.Fatalf("composer = %q, want the draft back", m.TextArea.Value()) }}
// Catches an edit the turn read first that drops what was typed: the draft// should keep both the old draft and the edit.func TestEditReadByTheTurnKeepsTheTypedText(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.TextArea.SetValue("draft") m.pendingUsers = []PendingUserTurn{queuedPending("a", "first")} m.handleQueueEdit(heldMsg(m, "a")) m.TextArea.SetValue("first, fixed") m.submitQueueEdit()
m.handleQueueEdit(ChatQueueEditedMsg{SessionID: "s", Generation: m.Generation, ID: "a", Err: &daemon.APIError{StatusCode: 409, Code: "input_not_waiting"}})
if m.editing != nil { t.Fatal("editing is still set after the turn read the message") } if got, want := m.TextArea.Value(), "draft\n\nfirst, fixed"; got != want { t.Fatalf("composer = %q, want %q", got, want) }}
// Catches an empty submit that does not cancel the message, or that restores// the draft when the turn had already read the message.func TestEmptySubmitCancelsTheMessage(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.TextArea.SetValue("draft") m.pendingUsers = []PendingUserTurn{queuedPending("a", "first")} m.handleQueueEdit(heldMsg(m, "a")) m.TextArea.SetValue(" ") if cmd := m.submitQueueEdit(); cmd == nil { t.Fatal("an empty submit returned no cancel") }
m.handleQueueEdit(ChatQueueDroppedMsg{SessionID: "s", Generation: m.Generation, ID: "a"})
if len(m.pendingUsers) != 0 { t.Fatalf("pending = %+v, want the message cancelled", m.pendingUsers) } if m.TextArea.Value() != "draft" { t.Fatalf("composer = %q, want the draft back", m.TextArea.Value()) }}
// Catches a hold answered as read that still starts an edit.func TestHoldAnsweredAsReadChangesNothing(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.TextArea.SetValue("draft") m.pendingUsers = []PendingUserTurn{queuedPending("a", "first")}
m.handleQueueEdit(ChatQueueHeldMsg{SessionID: "s", Generation: m.Generation, ID: "a", Err: &daemon.APIError{StatusCode: 409, Code: "input_not_waiting"}})
if m.editing != nil || m.TextArea.Value() != "draft" { t.Fatalf("editing %+v, composer %q; want no edit and the draft kept", m.editing, m.TextArea.Value()) }}
// Catches a joined draft that renumbers attachments twice, so one composer// marker names the wrong image.func TestJoinDraftsNumbersAttachmentsOnce(t *testing.T) { one := daemon.ImageAttachment{ImageMetadata: daemon.ImageMetadata{MimeType: "image/png", Width: 1}} two := daemon.ImageAttachment{ImageMetadata: daemon.ImageMetadata{MimeType: "image/png", Width: 2}} var first, second editDraft first.text = first.images.add(pastedImage{ImageAttachment: one}) second.text = second.images.add(pastedImage{ImageAttachment: two})
joined := joinDrafts(first, second)
_, items := joined.images.resolve(joined.text) if len(items) != 2 || items[0].Width != 1 || items[1].Width != 2 { t.Fatalf("joined text %q holds %+v, want both images in order", joined.text, items) }}
func TestRenewalDuringSaveKeepsTimerAlive(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.pendingUsers = []PendingUserTurn{queuedPending("a", "first")} m.startQueueEdit("") held := heldMsg(m, "a") held.LeaseMs = 1 m.handleQueueEdit(held) m.submitQueueEdit() tick := ChatQueueRenewMsg{SessionID: "s", Generation: m.Generation, ID: "a"} cmd, _ := m.handleQueueEdit(tick) if cmd == nil || cmd() != tick { t.Fatal("renewal during a save must schedule the next check") }}
func TestSavingEditCannotSwitchToEarlierHold(t *testing.T) { m := newTestChatModel(t, &daemon.Session{ID: "s"}) m.pendingUsers = []PendingUserTurn{queuedPending("a", "first"), queuedPending("b", "second")} m.handleInput(tea.KeyPressMsg{Code: tea.KeyUp}) m.handleQueueEdit(heldMsg(m, "b")) m.handleInput(tea.KeyPressMsg{Code: tea.KeyEnter}) cmd, _ := m.handleInput(tea.KeyPressMsg{Code: tea.KeyUp}) if cmd != nil { m.handleQueueEdit(heldMsg(m, "a")) } m.handleQueueEdit(ChatQueueEditedMsg{SessionID: "s", Generation: m.Generation, ID: "b", Text: "second"}) if m.editing != nil { t.Fatalf("save completed with editor still open: %+v", m.editing) }}