| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | tea "charm.land/bubbletea/v2" |
| 8 | |
| 9 | "reasonix/internal/control" |
| 10 | "reasonix/internal/event" |
| 11 | ) |
| 12 | |
| 13 | func TestQueuedFoldedPasteExpandsBeforeInterjectSend(t *testing.T) { |
| 14 | runner := &recordingTurnRunner{} |
| 15 | events := make(chan event.Event, 8) |
| 16 | finalized := make(chan struct{}) |
| 17 | dir := t.TempDir() |
| 18 | var ctrl *control.Controller |
| 19 | ctrl = newOwnedTestController(t, control.Options{ |
| 20 | Runner: runner, |
| 21 | Sink: event.FuncSink(func(e event.Event) { |
| 22 | if e.Kind == event.TurnDone { |
| 23 | // Seal admission before delivery so the completion tail cannot reopen |
| 24 | // the durable inbox while this test removes its session directory. |
| 25 | ctrl.Close() |
| 26 | } |
| 27 | events <- e |
| 28 | }), |
| 29 | SessionDir: dir, |
| 30 | Label: "test", |
| 31 | Cleanup: func() { close(finalized) }, |
| 32 | }) |
| 33 | defer ctrl.Close() |
| 34 | ctrl.EnsureSessionPath() |
| 35 | m := newTestChatTUI() |
| 36 | m.ctrl = &busyInboxController{SessionAPI: ctrl} |
| 37 | m.eventCh = make(chan event.Event, 8) |
| 38 | m.state = tuiRunning |
| 39 | pasted := strings.Repeat("queued pasted content\n", 10) |
| 40 | model, _ := m.Update(tea.PasteMsg{Content: pasted}) |
| 41 | m = model.(chatTUI) |
| 42 | |
| 43 | display := strings.TrimSpace(m.input.Value()) |
| 44 | if !strings.Contains(display, "[Pasted text #1") { |
| 45 | t.Fatalf("paste should be folded, got %q", display) |
| 46 | } |
| 47 | |
| 48 | model, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) |
| 49 | m = model.(chatTUI) |
| 50 | |
| 51 | bodies := m.inboxBodies() |
| 52 | if len(bodies) != 1 { |
| 53 | t.Fatalf("queue should have 1 item, got %d", len(bodies)) |
| 54 | } |
| 55 | queued := bodies[0] |
| 56 | if queued == display { |
| 57 | t.Fatalf("queued interject kept the folded placeholder: %q", queued) |
| 58 | } |
| 59 | for _, want := range []string{ |
| 60 | "queued pasted content", |
| 61 | "--- Begin [Pasted text #1", |
| 62 | "--- End [Pasted text #1", |
| 63 | } { |
| 64 | if !strings.Contains(queued, want) { |
| 65 | t.Fatalf("queued interject missing %q in:\n%s", want, queued) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Resume inbox so controller can dispatch after TurnDone. |
| 70 | _ = ctrl.SetInboxPaused(false) |
| 71 | model, _ = m.Update(agentEventMsg(event.Event{Kind: event.TurnDone})) |
| 72 | m = model.(chatTUI) |
| 73 | // Wait for the completed dispatch with admission already sealed. |
| 74 | waitForCLIEvent(t, events, event.TurnDone) |
| 75 | <-finalized |
| 76 | |
| 77 | if len(runner.inputs) != 1 { |
| 78 | t.Fatalf("runner should receive queued interject, inputs=%q", runner.inputs) |
| 79 | } |
| 80 | sent := runner.inputs[0] |
| 81 | if sent == display { |
| 82 | t.Fatalf("runner received the folded placeholder: %q", sent) |
| 83 | } |
| 84 | if !strings.Contains(sent, "queued pasted content") { |
| 85 | t.Fatalf("runner input missing pasted content:\n%s", sent) |
| 86 | } |
| 87 | } |
| 88 |