package tui import ( "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) } }