From 656193dc89e73ac24a089424f699d99e772f5846 Mon Sep 17 00:00:00 2001 From: Joe Beda Date: Sat, 21 Nov 2026 11:33:01 -0800 Subject: [PATCH] feat(mecatui): add explicit saved memory pointer actions Keep entry-row clicks select-only and add Inspect, Back, and Close controls with current-frame routing and keyboard parity. Co-authored-by: Mecatl --- cmd/mecatui/ui/keymark_liveness_test.go | 10 +- cmd/mecatui/ui/testdata/usermodel.golden | 4 +- .../ui/testdata/usermodel_constrained.golden | 26 + .../ui/testdata/usermodel_detail.golden | 30 + .../testdata/usermodel_empty_disabled.golden | 4 +- .../testdata/usermodel_empty_enabled.golden | 4 +- cmd/mecatui/ui/usermodel.go | 235 +++++--- cmd/mecatui/ui/usermodel_bounded_more_test.go | 9 +- .../ui/usermodel_bounded_scenario_test.go | 11 +- cmd/mecatui/ui/usermodel_mouse_test.go | 548 ++++++++++++++++++ cmd/mecatui/ui/usermodel_test.go | 37 +- 11 files changed, 819 insertions(+), 99 deletions(-) create mode 100644 cmd/mecatui/ui/testdata/usermodel_constrained.golden create mode 100644 cmd/mecatui/ui/testdata/usermodel_detail.golden create mode 100644 cmd/mecatui/ui/usermodel_mouse_test.go diff --git a/cmd/mecatui/ui/keymark_liveness_test.go b/cmd/mecatui/ui/keymark_liveness_test.go index 21f2b86af4..9af62cd724 100644 --- a/cmd/mecatui/ui/keymark_liveness_test.go +++ b/cmd/mecatui/ui/keymark_liveness_test.go @@ -826,15 +826,15 @@ func TestSoulAndUserModelOverlayHintsReflectKeyOverride(t *testing.T) { } }) t.Run("user model", func(t *testing.T) { - st := &userModelState{deps: surfaceDeps{theme: th, marks: hk, caps: client.Capabilities{UserModel: true}}, list: &bounded.List{}, viewport: &bounded.Viewport{}} + st := &userModelState{deps: surfaceDeps{theme: th, marks: hk, keys: applyKeyOverrides(defaultKeys(), overrideAll()), caps: client.Capabilities{UserModel: true}}, list: &bounded.List{}, viewport: &bounded.Viewport{}} st.setModel(client.UserModel{Entries: []client.UserModelEntry{{Key: "k"}}}) body, _ := st.Render(100, 30) got := stripANSIstr(body) - if !strings.Contains(got, "ctrl+f16 close") { - t.Errorf("user-model hint should carry live close: %q", got) + if !strings.Contains(got, "[ctrl+f16 Close]") { + t.Errorf("user-model control should carry captured close: %q", got) } - if strings.Contains(got, "esc close") { - t.Errorf("user-model hint still shows default close: %q", got) + if strings.Contains(got, "[esc Close]") { + t.Errorf("user-model control still shows default close: %q", got) } }) } diff --git a/cmd/mecatui/ui/testdata/usermodel.golden b/cmd/mecatui/ui/testdata/usermodel.golden index 4d58190515..1effb3c77a 100644 --- a/cmd/mecatui/ui/testdata/usermodel.golden +++ b/cmd/mecatui/ui/testdata/usermodel.golden @@ -12,7 +12,8 @@ ┃ │ the operator's name ┃ ┃ stack ┃ ┃ │ prefers Go + hexagonal architecture ┃ -┃ ↑/↓ select · enter inspect · the agent saves and updates these facts · esc close ┃ +┃ [enter Inspect] [esc Close] ┃ +┃ ↑/↓ select · the agent saves and updates these facts ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ @@ -20,7 +21,6 @@ - │ │ Ask mecatl to do something… (enter to send · shift+enter for newline · ? for help) │ diff --git a/cmd/mecatui/ui/testdata/usermodel_constrained.golden b/cmd/mecatui/ui/testdata/usermodel_constrained.golden new file mode 100644 index 0000000000..7144171370 --- /dev/null +++ b/cmd/mecatui/ui/testdata/usermodel_constrained.golden @@ -0,0 +1,26 @@ + session sess-test-00 +──────────────────────── +┏━━━━━━━━━━━━━━━━━━━━━━┓ +┃ ┃ +┃ Saved memory ┃ +┃ 2 facts · 64 bytes ┃ +┃ · sha:abc123def45 ┃ +┃ 6 ┃ +┃ ▶ name ┃ +┃ │ the operator's ┃ +┃ │ name ┃ +┃ [enter Inspect] ┃ +┃ [esc Close] ┃ +┃ ↑/↓ select · the a ┃ +┃ gent saves and upd ┃ +┃ ates these facts ┃ +┃ ┃ +┗━━━━━━━━━━━━━━━━━━━━━━┛ + +│ +│ Ask mecatl to do +│ something… (enter to +│ send · shift+enter +──────────────────────── + connected +? help · / commands · ctrl+c quit \ No newline at end of file diff --git a/cmd/mecatui/ui/testdata/usermodel_detail.golden b/cmd/mecatui/ui/testdata/usermodel_detail.golden new file mode 100644 index 0000000000..87b9dc647a --- /dev/null +++ b/cmd/mecatui/ui/testdata/usermodel_detail.golden @@ -0,0 +1,30 @@ + mecatui · Golden session · fixture/fixture-model/default · mode default +──────────────────────────────────────────────────────────────────────────────────────────────────── + + + +┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +┃ ┃ +┃ User model detail ┃ +┃ name ┃ +┃ ┃ +┃ Alice ┃ +┃ ┃ +┃ Earlier versions: 1 ┃ +┃ v1 · active · 0001-01-01 ┃ +┃ [esc Back] ┃ +┃ pgup/pgdn scroll · read-only · ask the agent to remove or restore this saved fact ┃ +┃ ┃ +┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + + + + + +│ +│ Ask mecatl to do something… (enter to send · shift+enter for newline · ? for help) +│ +│ +──────────────────────────────────────────────────────────────────────────────────────────────────── + connected ctx ▒▒▒▒░░░░ 50% · 2K/4K · ↑0 ↓0 cache 0% +? help · / commands · ctrl+c quit \ No newline at end of file diff --git a/cmd/mecatui/ui/testdata/usermodel_empty_disabled.golden b/cmd/mecatui/ui/testdata/usermodel_empty_disabled.golden index c54b3ac5ec..6866f65d5c 100644 --- a/cmd/mecatui/ui/testdata/usermodel_empty_disabled.golden +++ b/cmd/mecatui/ui/testdata/usermodel_empty_disabled.golden @@ -5,13 +5,13 @@ - ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ Saved memory ┃ ┃ Saved memory is not enabled on this server. ┃ ┃ Start or connect to a server with memory enabled. ┃ -┃ ↑/↓ select · enter inspect · the agent saves and updates these facts · esc close ┃ +┃ [esc Close] ┃ +┃ ↑/↓ select · the agent saves and updates these facts ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ diff --git a/cmd/mecatui/ui/testdata/usermodel_empty_enabled.golden b/cmd/mecatui/ui/testdata/usermodel_empty_enabled.golden index b502011882..9332d4cc03 100644 --- a/cmd/mecatui/ui/testdata/usermodel_empty_enabled.golden +++ b/cmd/mecatui/ui/testdata/usermodel_empty_enabled.golden @@ -10,7 +10,8 @@ ┃ ┃ ┃ Saved memory ┃ ┃ No saved memory yet. ┃ -┃ ↑/↓ select · enter inspect · the agent saves and updates these facts · esc close ┃ +┃ [esc Close] ┃ +┃ ↑/↓ select · the agent saves and updates these facts ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ @@ -20,7 +21,6 @@ - │ │ Ask mecatl to do something… (enter to send · shift+enter for newline · ? for help) │ diff --git a/cmd/mecatui/ui/usermodel.go b/cmd/mecatui/ui/usermodel.go index 669572e98e..c7bb2fc676 100644 --- a/cmd/mecatui/ui/usermodel.go +++ b/cmd/mecatui/ui/usermodel.go @@ -37,6 +37,14 @@ type userModelState struct { compact bool // view cache: refreshed from every Render offer listWidth int // view cache: preserves list anchors across model refreshes detailLines []string // view cache: wrapped again on every Render + hitTargets map[HitID]userModelHit +} + +type userModelHit struct { + key string + inspect, close bool + view userModelView + selected string } func (m Model) openUserModel() (tea.Model, tea.Cmd) { @@ -53,23 +61,58 @@ func (m Model) openUserModel() (tea.Model, tea.Cmd) { func (*userModelState) modalMaxOuterWidth() int { return 128 } func (s *userModelState) modalFrame() bool { return !s.compact } -func (*userModelState) Close() {} +func (s *userModelState) Close() { s.hitTargets = nil } + +func (s *userModelState) closeOrBack() bool { + if !s.deps.keys.Close.Enabled() { + return false + } + if s.compact || s.view != userModelDetail { + return true + } + s.generation++ + s.view = userModelPanel + s.detail = nil + s.requestKey = "" + s.loading = false + s.err = nil + return false +} + +func (s *userModelState) inspectAvailable() bool { + if s.view != userModelPanel || s.loading || s.list == nil || s.detailer == nil || !s.deps.keys.Choose.Enabled() { + return false + } + selected := s.list.CursorID() + if selected == "" { + return false + } + for _, entry := range s.model.Entries { + if entry.Key == selected { + return true + } + } + return false +} + +func (s *userModelState) canInspect() bool { return !s.compact && s.inspectAvailable() } + +func (s *userModelState) inspect() tea.Cmd { + if !s.canInspect() { + return nil + } + selected := s.list.CursorID() + s.generation++ + s.requestKey = selected + s.loading = true + s.err = nil + return client.GetUserModelEntryCmdTagged(s.deps.ctx, s.detailer, selected, s.generation) +} func (s *userModelState) HandleKey(msg tea.KeyPressMsg) (tea.Cmd, bool, bool) { + s.hitTargets = nil if key.Matches(msg, s.deps.keys.Close) { - if s.compact { - return nil, true, true - } - if s.view == userModelDetail { - s.generation++ - s.view = userModelPanel - s.detail = nil - s.requestKey = "" - s.loading = false - s.err = nil - return nil, true, false - } - return nil, true, true + return nil, true, s.closeOrBack() } if s.compact { return nil, true, false @@ -90,21 +133,14 @@ func (s *userModelState) HandleKey(msg tea.KeyPressMsg) (tea.Cmd, bool, bool) { s.list.Move(move) return nil, true, false } - if key.Matches(msg, s.deps.keys.Choose) && !s.loading && s.list != nil && len(s.model.Entries) > 0 && s.detailer != nil { - selected := s.list.CursorID() - if selected == "" { - return nil, true, false - } - s.generation++ - s.requestKey = selected - s.loading = true - s.err = nil - return client.GetUserModelEntryCmdTagged(s.deps.ctx, s.detailer, selected, s.generation), true, false + if key.Matches(msg, s.deps.keys.Choose) { + return s.inspect(), true, false } return nil, true, false } func (s *userModelState) HandleWheel(msg tea.MouseWheelMsg) (tea.Cmd, bool) { + s.hitTargets = nil if s.compact { return nil, true } @@ -121,7 +157,32 @@ func (s *userModelState) HandleWheel(msg tea.MouseWheelMsg) (tea.Cmd, bool) { return nil, true } +func (s *userModelState) handleHit(hit surfaceHitMsg) (tea.Cmd, bool, bool) { + target, current := s.hitTargets[hit.ID] + s.hitTargets = nil + if !current || s.compact || target.view != s.view { + return nil, true, false + } + if target.key != "" { + if s.view == userModelPanel && !s.loading && s.err == nil && s.list != nil { + s.list.SelectID(target.key) + } + return nil, true, false + } + if target.close { + return nil, true, s.closeOrBack() + } + if target.inspect && s.list != nil && target.selected == s.list.CursorID() { + return s.inspect(), true, false + } + return nil, true, false +} + func (s *userModelState) HandleMsg(msg tea.Msg) (tea.Cmd, bool, bool) { + if hit, ok := msg.(surfaceHitMsg); ok { + return s.handleHit(hit) + } + s.hitTargets = nil switch um := msg.(type) { case client.UserModelMsg: if um.Generation != s.generation || s.requestKey != "" || s.view != userModelPanel { @@ -162,6 +223,7 @@ func (s *userModelState) setModel(model client.UserModel) { } const ( + userModelBackLabel = "Back" userModelDescriptionMarker = "\x1b[8m" userModelDescriptionRail = "│ " ) @@ -235,68 +297,111 @@ func (s *userModelState) contentRowsFit(width, height, titleLines, metaLines, fo func (s *userModelState) Render(width, height int) (string, []ClickableRegion) { s.compact = true + s.hitTargets = nil if width <= 0 || height <= 0 { return "", nil } th := s.deps.theme title := "Saved memory" - footer := "↑/↓ select · enter inspect · the agent saves and updates these facts · " + s.deps.marks.closeOnly + " close" + help := "↑/↓ select · the agent saves and updates these facts" if s.view == userModelDetail { title = "User model detail" - footer = s.deps.marks.scroll + " scroll · read-only · ask the agent to remove or restore this saved fact · " + s.deps.marks.closeOnly + " back" + help = s.deps.marks.scroll + " scroll · read-only · ask the agent to remove or restore this saved fact" } titleLines := skillsTextLines(th.Style("askTitle"), title, width) - footerLines := skillsTextLines(th.Style("muted"), footer, width) meta := []string(nil) if s.view == userModelPanel && !s.loading && s.err == nil && len(s.model.Entries) > 0 { meta = skillsTextLines(th.Style("muted"), renderUserModelMeta(s.model), width) } - rows, fits := s.contentRowsFit(width, height, len(titleLines), len(meta), len(footerLines)) - if !fits { + inspectEnabled := s.inspectAvailable() + closeLabel := footerCloseLabel + if s.view == userModelDetail { + closeLabel = userModelBackLabel + } + footer, controls := renderFooterControls(th, width, []footerControlSpec{ + {hint: s.deps.marks.choose, label: "Inspect", enabled: s.view == userModelPanel && inspectEnabled}, + {hint: s.deps.marks.closeOnly, label: closeLabel, enabled: s.deps.keys.Close.Enabled()}, + }) + footer = append(footer, skillsTextLines(th.Style("muted"), help, width)...) + rows, fits := s.contentRowsFit(width, height, len(titleLines), len(meta), len(footer)) + if !fits || len(controls) == 0 && (inspectEnabled || s.deps.keys.Close.Enabled()) { + if !s.deps.keys.Close.Enabled() { + return boundedDisplayLine(th.Style("muted").Render("terminal too small"), width), nil + } return renderSkillsCompact(th, s.deps.marks, width), nil } s.compact = false - var body []string - if s.view == userModelPanel { - if s.loading || s.err != nil || len(s.model.Entries) == 0 { - text := userModelEmptyCopy(s.deps.caps) - style := th.Style("muted") - if s.loading { - text = "loading…" - } - if s.err != nil { - text = "get user model: " + terminaltext.Sanitize(s.err.Error()) - style = th.Style("errorText") - } - v := s.viewport - v.SetGeometry(width, rows, 0, bounded.Wrap) - body = v.View([]string{style.Render(text)}).Rows + s.hitTargets = make(map[HitID]userModelHit) + body, regions := s.renderBody(width, rows, len(titleLines)+len(meta)) + footerY := len(titleLines) + len(meta) + len(body) + selected := "" + if s.list != nil { + selected = s.list.CursorID() + } + for _, control := range controls { + if s.deps.hits == nil { + break + } + rect := control.rect + rect.y0 += footerY + rect.y1 += footerY + target := userModelHit{view: s.view, selected: selected} + if control.index == 0 && s.view == userModelPanel { + target.inspect = true } else { - s.list.SetGeometry(width, rows, 1, bounded.Wrap) - view := s.list.ViewWithIndicators(rows, s.list.RevealPending()) - if view.Above > 0 { - body = append(body, th.Style("muted").Render(fmt.Sprintf("↑ %d more", view.Above))) - } - for _, row := range view.Rows { - body = append(body, s.renderListRow(row)) - } - if view.Below > 0 { - body = append(body, th.Style("muted").Render(fmt.Sprintf("↓ %d more", view.Below))) - } + target.close = true } - } else { - lines := s.detailContent(width) - s.viewport.SetGeometry(width, rows, 0, bounded.Wrap) - // Viewport wraps the caller's lines. Keep the physical line count in sync - // with navigation even when a terminal resize reflows the content. - s.detailLines = lines - v := s.viewport.View(lines) - body = v.Rows + id := s.deps.hits.allocate() + s.hitTargets[id] = target + regions = append(regions, ClickableRegion{rect: rect, hit: id}) } result := append(titleLines, meta...) result = append(result, body...) - result = append(result, footerLines...) - return strings.Join(result, "\n"), nil + result = append(result, footer...) + return strings.Join(result, "\n"), regions +} + +func (s *userModelState) renderBody(width, rows, top int) ([]string, []ClickableRegion) { + th := s.deps.theme + if s.view == userModelDetail { + s.detailLines = s.detailContent(width) + s.viewport.SetGeometry(width, rows, 0, bounded.Wrap) + return s.viewport.View(s.detailLines).Rows, nil + } + if s.loading || s.err != nil || len(s.model.Entries) == 0 { + text := userModelEmptyCopy(s.deps.caps) + style := th.Style("muted") + if s.loading { + text = "loading…" + } + if s.err != nil { + text = "get user model: " + terminaltext.Sanitize(s.err.Error()) + style = th.Style("errorText") + } + s.viewport.SetGeometry(width, rows, 0, bounded.Wrap) + return s.viewport.View([]string{style.Render(text)}).Rows, nil + } + s.list.SetGeometry(width, rows, 1, bounded.Wrap) + view := s.list.ViewWithIndicators(rows, s.list.RevealPending()) + var body []string + var regions []ClickableRegion + if view.Above > 0 { + body = append(body, th.Style("muted").Render(fmt.Sprintf("↑ %d more", view.Above))) + } + for _, row := range view.Rows { + line := s.renderListRow(row) + y := top + len(body) + body = append(body, line) + if lineWidth := ansi.StringWidth(line); lineWidth > 0 && s.deps.hits != nil { + id := s.deps.hits.allocate() + s.hitTargets[id] = userModelHit{key: row.ID, view: s.view} + regions = append(regions, ClickableRegion{rect: cellRect{x1: min(width, lineWidth), y0: y, y1: y + 1}, hit: id}) + } + } + if view.Below > 0 { + body = append(body, th.Style("muted").Render(fmt.Sprintf("↓ %d more", view.Below))) + } + return body, regions } func (s *userModelState) renderListRow(row bounded.ListRow) string { diff --git a/cmd/mecatui/ui/usermodel_bounded_more_test.go b/cmd/mecatui/ui/usermodel_bounded_more_test.go index bdf86cf7a9..5198088cd2 100644 --- a/cmd/mecatui/ui/usermodel_bounded_more_test.go +++ b/cmd/mecatui/ui/usermodel_bounded_more_test.go @@ -500,11 +500,12 @@ func TestMecatuiSavedMemoryBoundedBrowser_Scenario3_WheelAndPointerIsolation(t * if s.list.Offset() != bottomOffset || s.list.CursorID() != selected || m.vp.YOffset() != before { t.Fatalf("inventory bottom wheel escaped modal: offset=%d selection=%q conversation=%d", s.list.Offset(), s.list.CursorID(), m.vp.YOffset()) } - for _, msg := range []tea.Msg{tea.MouseClickMsg{Button: tea.MouseLeft, X: 5, Y: 10}, tea.MouseMotionMsg{Button: tea.MouseLeft, X: 7, Y: 11}, tea.MouseReleaseMsg{Button: tea.MouseLeft, X: 7, Y: 11}} { - m = applyAll(m, msg) - } + m.deps.NoAltScreen = false + _ = m.View() + x, y := m.metrics.outerBounds.x0, m.metrics.outerBounds.y0 + m = applyAll(m, tea.MouseClickMsg{Button: tea.MouseLeft, X: x, Y: y}, tea.MouseMotionMsg{Button: tea.MouseLeft, X: x, Y: y}, tea.MouseReleaseMsg{Button: tea.MouseLeft, X: x, Y: y}) if m.sel.active || s.list.CursorID() != selected || s.view != userModelPanel { - t.Fatal("pointer selected or activated fact") + t.Fatal("border click selected or activated fact") } m = resize(m, 10, 12) _ = (&m).renderModalSurface() diff --git a/cmd/mecatui/ui/usermodel_bounded_scenario_test.go b/cmd/mecatui/ui/usermodel_bounded_scenario_test.go index b4c30e49c4..06b8bd8fb4 100644 --- a/cmd/mecatui/ui/usermodel_bounded_scenario_test.go +++ b/cmd/mecatui/ui/usermodel_bounded_scenario_test.go @@ -39,7 +39,12 @@ func TestMecatuiSavedMemoryBoundedBrowser_Scenario1_ResponsiveCardAndBodyBudget( if width == 140 { s := m.modal.(*userModelState) content := m.metrics.contentBounds - budget := content.y1 - content.y0 - len(skillsTextLines(m.deps.Theme.Style("askTitle"), "Saved memory", content.x1-content.x0)) - len(skillsTextLines(m.deps.Theme.Style("muted"), renderUserModelMeta(s.model), content.x1-content.x0)) - len(skillsTextLines(m.deps.Theme.Style("muted"), "↑/↓ select · enter inspect · the agent saves and updates these facts · "+s.deps.marks.closeOnly+" close", content.x1-content.x0)) + controls, _ := renderFooterControls(m.deps.Theme, content.x1-content.x0, []footerControlSpec{ + {hint: s.deps.marks.choose, label: "Inspect", enabled: s.inspectAvailable()}, + {hint: s.deps.marks.closeOnly, label: footerCloseLabel, enabled: s.deps.keys.Close.Enabled()}, + }) + footer := append(controls, skillsTextLines(m.deps.Theme.Style("muted"), "↑/↓ select · the agent saves and updates these facts", content.x1-content.x0)...) + budget := content.y1 - content.y0 - len(skillsTextLines(m.deps.Theme.Style("askTitle"), "Saved memory", content.x1-content.x0)) - len(skillsTextLines(m.deps.Theme.Style("muted"), renderUserModelMeta(s.model), content.x1-content.x0)) - len(footer) view := s.list.View() indicators := 0 if view.Above > 0 { @@ -71,7 +76,9 @@ func TestMecatuiSavedMemoryBoundedBrowser_Scenario1_ResponsiveCardAndBodyBudget( if width == 140 { s := m.modal.(*userModelState) content := m.metrics.contentBounds - budget := content.y1 - content.y0 - len(skillsTextLines(m.deps.Theme.Style("askTitle"), "User model detail", content.x1-content.x0)) - len(skillsTextLines(m.deps.Theme.Style("muted"), s.deps.marks.scroll+" scroll · read-only · ask the agent to remove or restore this saved fact · "+s.deps.marks.closeOnly+" back", content.x1-content.x0)) + controls, _ := renderFooterControls(m.deps.Theme, content.x1-content.x0, []footerControlSpec{{hint: s.deps.marks.closeOnly, label: "Back", enabled: s.deps.keys.Close.Enabled()}}) + footer := append(controls, skillsTextLines(m.deps.Theme.Style("muted"), s.deps.marks.scroll+" scroll · read-only · ask the agent to remove or restore this saved fact", content.x1-content.x0)...) + budget := content.y1 - content.y0 - len(skillsTextLines(m.deps.Theme.Style("askTitle"), "User model detail", content.x1-content.x0)) - len(footer) if got := s.viewport.Height(); got != budget || len(s.viewport.View(s.detailLines).Rows) != budget || budget <= 15 { t.Fatalf("detail usable rows=%d, offered content budget=%d, visible=%d", got, budget, len(s.viewport.View(s.detailLines).Rows)) } diff --git a/cmd/mecatui/ui/usermodel_mouse_test.go b/cmd/mecatui/ui/usermodel_mouse_test.go new file mode 100644 index 0000000000..eaa6094f64 --- /dev/null +++ b/cmd/mecatui/ui/usermodel_mouse_test.go @@ -0,0 +1,548 @@ +package ui + +import ( + "errors" + "fmt" + "strings" + "testing" + + tea "charm.land/bubbletea/v2" + + "github.com/stacklok/mecatl/cmd/mecatui/client" + "github.com/stacklok/mecatl/cmd/mecatui/ui/internal/bounded" +) + +func userModelMouseHit(t *testing.T, m Model, match func(userModelHit) bool) renderedHitRegion { + t.Helper() + s := m.modal.(*userModelState) + hit, ok := currentFrameHit(m, func(region renderedHitRegion) bool { + target, ok := s.hitTargets[region.id] + return ok && match(target) + }) + if !ok { + t.Fatalf("missing saved-memory hit in %d current-frame regions", len(m.hits.frame)) + } + return hit +} + +func TestUserModelMouseRowsSelectOnlyIncludingContinuations(t *testing.T) { + entries := []client.UserModelEntry{{Key: strings.Repeat("key ", 12) + "\x1b[2J", Description: strings.Repeat("description ", 12)}, {Key: "other", Description: "other description"}} + memory := &fakeUserModel{model: client.UserModel{Entries: entries}} + m, s := openedUserModel(t, newUserModelModel(t, memory, client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, 32, 28) + _ = m.View() + physicalRows := len(s.list.View().Rows) - 2 // other key and its description + if physicalRows < 3 { + t.Fatal("fixture lacks wrapped key/description rows") + } + for rowIndex := range physicalRows { + // Start with a different selection so an inert hit cannot pass. + s.list.SetCursor(1) + s.list.Scroll(bounded.Top) + _ = m.View() + var rows []renderedHitRegion + for _, hit := range m.hits.frame { + if target, ok := s.hitTargets[hit.id]; ok && target.key == entries[0].Key { + rows = append(rows, hit) + } + } + if rowIndex >= len(rows) { + t.Fatalf("missing physical row %d in %d hits", rowIndex, len(rows)) + } + var cmd tea.Cmd + m, cmd = clickFrameHit(m, rows[rowIndex], rowIndex%2 == 0) + if cmd != nil || s.list.CursorID() != entries[0].Key || s.loading || memory.calls != 1 { + t.Fatalf("row %d failed exact raw-key selection or fetched detail", rowIndex) + } + // Repeated selection uses a newly published frame, not an expired hit. + _ = m.View() + m, cmd = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.key == entries[0].Key }), false) + if cmd != nil || s.loading || memory.calls != 1 { + t.Fatal("repeat click fetched detail") + } + } +} + +func TestUserModelMouseInspectBackAndCloseMatchKeys(t *testing.T) { + memory := sampleUserModel() + memory.model.Detail = &client.UserModelDetail{Current: client.UserModelRevision{Key: "name", Value: "Alice"}} + m, s := openedUserModel(t, newUserModelModel(t, memory, client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, 80, 18) + frame := m.View().Content + inspect := userModelMouseHit(t, m, func(target userModelHit) bool { return target.inspect }) + closeHit := userModelMouseHit(t, m, func(target userModelHit) bool { return target.close }) + assertFrameHitLabel(t, frame, *m.metrics, inspect, "[enter Inspect]") + assertFrameHitLabel(t, frame, *m.metrics, closeHit, "[esc Close]") + m, cmd := clickFrameHit(m, inspect, true) + if cmd == nil || !s.loading || s.requestKey != "name" || memory.calls != 1 { + t.Fatal("Inspect did not share keyboard detail activation") + } + updated, duplicate := m.Update(surfaceHitMsg{ID: inspect.id}) + m = updated.(Model) + if duplicate != nil || memory.calls != 1 { + t.Fatal("stale Inspect submitted a duplicate request") + } + m = feedCmd(t, m, cmd) + if s.view != userModelDetail { + t.Fatal("Inspect response did not open detail") + } + frame = m.View().Content + back := userModelMouseHit(t, m, func(target userModelHit) bool { return target.close }) + assertFrameHitLabel(t, frame, *m.metrics, back, "[esc Back]") + m, cmd = clickFrameHit(m, back, false) + if cmd != nil || s.view != userModelPanel || s.list.CursorID() != "name" { + t.Fatal("Back did not preserve list selection") + } + _ = m.View() + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(target userModelHit) bool { return target.close }), false) + if m.modal != nil || !m.prompt.Focused() { + t.Fatal("Close did not share modal teardown") + } +} + +func TestUserModelMouseLoadingOnlyPublishesClose(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + s.loading = true + _ = m.View() + for _, hit := range m.hits.frame { + target := s.hitTargets[hit.id] + if target.key != "" || target.inspect { + t.Fatal("loading view published a row or Inspect target") + } + } + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(target userModelHit) bool { return target.close }), false) + if m.modal != nil { + t.Fatal("Close was unavailable while loading") + } +} + +func TestUserModelMouseInspectUsesOffscreenSelectionAndRemappedBindings(t *testing.T) { + m := newUserModelModel(t, &fakeUserModel{model: client.UserModel{Entries: savedMemoryEntries(30)}}, client.Capabilities{UserModel: true}) + m.keys = applyKeyOverrides(m.keys, map[string][]string{"Choose": {"i"}, "Close": {"x"}}) + m, s := openedUserModel(t, m) + m.deps.NoAltScreen = false + m = offerCardBody(m, 48, 12) + frame := m.View().Content + inspect := userModelMouseHit(t, m, func(target userModelHit) bool { return target.inspect }) + closeHit := userModelMouseHit(t, m, func(target userModelHit) bool { return target.close }) + assertFrameHitLabel(t, frame, *m.metrics, inspect, "[i Inspect]") + assertFrameHitLabel(t, frame, *m.metrics, closeHit, "[x Close]") + selected := s.list.CursorID() + for range 20 { + m = applyAll(m, tea.MouseWheelMsg{Button: tea.MouseWheelDown}) + } + _ = m.View() + for _, row := range s.list.View().Rows { + if row.ID == selected { + t.Fatal("wheel did not move the selected row offscreen") + } + } + m, cmd := clickFrameHit(m, userModelMouseHit(t, m, func(target userModelHit) bool { return target.inspect }), false) + if cmd == nil || s.requestKey != selected || !s.loading { + t.Fatal("Inspect did not use the retained offscreen selection") + } +} + +func TestUserModelMouseUnavailableActionsAndNoMouse(t *testing.T) { + for _, tc := range []struct { + name string + setup func(*userModelState, *Model) + }{ + {"no detailer", func(s *userModelState, _ *Model) { s.detailer = nil }}, + {"empty", func(s *userModelState, _ *Model) { s.setModel(client.UserModel{}) }}, + {"initial error", func(s *userModelState, _ *Model) { s.setModel(client.UserModel{}); s.err = errors.New("initial") }}, + {"disabled choose", func(_ *userModelState, m *Model) { + m.keys.Choose.SetEnabled(false) + m.modal.(*userModelState).deps.keys.Choose.SetEnabled(false) + }}, + } { + t.Run(tc.name, func(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + tc.setup(s, &m) + _ = m.View() + for _, hit := range m.hits.frame { + if s.hitTargets[hit.id].inspect { + t.Fatal("unavailable Inspect was published") + } + } + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }), true) + if m.modal != nil || !m.prompt.Focused() { + t.Fatal("enabled Close did not tear down unavailable-action screen") + } + }) + } + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m.deps.NoMouse = true + _ = m.View() + row := userModelMouseHit(t, m, func(target userModelHit) bool { return target.key == "stack" }) + selected := s.list.CursorID() + m, cmd := clickFrameHit(m, row, false) + if cmd != nil || s.list.CursorID() != selected { + t.Fatal("no-mouse pointer input changed saved-memory state") + } + updated, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) + m = updated.(Model) + if cmd == nil || !s.loading { + t.Fatal("no-mouse mode disabled keyboard Inspect") + } + m = feedCmd(t, m, cmd) + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyEsc}, tea.KeyPressMsg{Code: tea.KeyDown}) + if s.view != userModelPanel || s.list.CursorID() != "stack" { + t.Fatal("no-mouse mode disabled keyboard Back/navigation") + } + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyEsc}) + if m.modal != nil || !m.prompt.Focused() { + t.Fatal("no-mouse mode disabled keyboard Close") + } +} + +func TestUserModelMouseDetailErrorRetriesAndStaleFramesAreInert(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, 80, 18) + _ = m.View() + oldInspect := userModelMouseHit(t, m, func(target userModelHit) bool { return target.inspect }).id + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyDown}) + updated, cmd := m.Update(surfaceHitMsg{ID: oldInspect}) + m = updated.(Model) + if cmd != nil || s.loading || s.requestKey != "" { + t.Fatal("Inspect hit survived keyboard selection") + } + _ = m.View() + m, cmd = clickFrameHit(m, userModelMouseHit(t, m, func(target userModelHit) bool { return target.inspect }), false) + if cmd == nil { + t.Fatal("valid Inspect missing") + } + request := cmd().(client.UserModelDetailMsg) + request.Err = errors.New("detail failed") + m = applyAll(m, request) + if s.err == nil || s.loading || s.requestKey != "" { + t.Fatal("detail error did not become retryable") + } + _ = m.View() + m, retry := clickFrameHit(m, userModelMouseHit(t, m, func(target userModelHit) bool { return target.inspect }), false) + if retry == nil || !s.loading || m.modal != s { + t.Fatal("detail error Inspect did not retry") + } +} + +func TestUserModelMouseCompactCloseTearsDownDetail(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + updated, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) + m = feedCmd(t, updated.(Model), cmd) + if s.view != userModelDetail { + t.Fatal("fixture did not enter detail") + } + m = applyAll(m, tea.WindowSizeMsg{Width: 2, Height: 18}) + _ = m.View() + if !s.compact { + t.Fatal("fixture did not enter compact detail") + } + updated, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyEsc}) + m = updated.(Model) + if m.modal != nil { + t.Fatal("compact detail Close went Back instead of tearing down") + } +} + +func TestUserModelMousePartialRowAndWrappedFooter(t *testing.T) { + memory := &fakeUserModel{model: client.UserModel{Entries: []client.UserModelEntry{ + {Key: "first"}, {Key: "long-key", Description: strings.Repeat("description ", 30)}, + }}} + m, s := openedUserModel(t, newUserModelModel(t, memory, client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, 18, 18) + for range 5 { + m = applyAll(m, tea.MouseWheelMsg{Button: tea.MouseWheelDown}) + } + frame := m.View().Content + view := s.list.View() + if len(view.Rows) == 0 || view.Rows[0].ID != "long-key" || view.Rows[0].ItemLine == 0 || s.list.CursorID() != "first" { + t.Fatalf("fixture needs an unselected partial continuation: %+v", view) + } + inspect := userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }) + closeHit := userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }) + assertFrameHitLabel(t, frame, *m.metrics, inspect, "[enter Inspect]") + assertFrameHitLabel(t, frame, *m.metrics, closeHit, "[esc Close]") + if inspect.rect.y0 >= closeHit.rect.y0 { + t.Fatal("fixture did not wrap footer controls") + } + m, cmd := clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.key == "long-key" }), true) + if cmd != nil || s.list.CursorID() != "long-key" || memory.calls != 1 { + t.Fatal("partial continuation did not select its key without fetching") + } +} + +func TestUserModelMouseFooterButtonEdges(t *testing.T) { + for _, width := range []int{18, 80} { + for _, end := range []bool{false, true} { + t.Run(fmt.Sprintf("width=%d/end=%v", width, end), func(t *testing.T) { + memory := sampleUserModel() + m, s := openedUserModel(t, newUserModelModel(t, memory, client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, width, 24) + frame := m.View().Content + inspect := userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }) + closeHit := userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }) + assertFrameHitLabel(t, frame, *m.metrics, inspect, "[enter Inspect]") + assertFrameHitLabel(t, frame, *m.metrics, closeHit, "[esc Close]") + if width == 18 && inspect.rect.y0 == closeHit.rect.y0 { + t.Fatal("fixture did not wrap controls") + } + m, cmd := clickFrameHit(m, inspect, end) + if cmd == nil { + t.Fatal("Inspect edge did not fetch detail") + } + m = feedCmd(t, m, cmd) + if memory.calls != 2 || s.view != userModelDetail || s.detail.Current.Key != "name" { + t.Fatal("Inspect edge did not fetch exactly the selected detail once") + } + frame = m.View().Content + back := userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }) + assertFrameHitLabel(t, frame, *m.metrics, back, "[esc Back]") + m, _ = clickFrameHit(m, back, end) + if s.view != userModelPanel { + t.Fatal("Back edge did not restore list") + } + _ = m.View() + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }), end) + if m.modal != nil || !m.prompt.Focused() { + t.Fatal("Close edge did not tear down/refocus") + } + }) + } + } +} + +func TestUserModelMouseStaleTargets(t *testing.T) { + for _, transition := range []string{"selection", "resize", "preparation", "pending", "detail", "replacement", "removed"} { + t.Run(transition, func(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + _ = m.View() + old := []renderedHitRegion{ + userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.key == "stack" }), + userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }), + userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }), + } + // Capture coordinates from the actual displayed frame before invalidation. + var clicks []tea.MouseClickMsg + for _, hit := range old { + x, y := m.metrics.localToGlobal(hit.rect.x0, hit.rect.y0) + clicks = append(clicks, tea.MouseClickMsg{Button: tea.MouseLeft, X: x, Y: y}) + } + switch transition { + case "selection": + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyDown}) + case "resize": + m = applyAll(m, tea.WindowSizeMsg{Width: 80, Height: 28}) + case "preparation": + (&m).prepareSurfaceInput() + case "pending", "detail": + updated, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) + m = updated.(Model) + if transition == "detail" { + m = feedCmd(t, m, cmd) + } + case "replacement": + (&m).closeModal() + m, s = openedUserModel(t, m) + // Publish replacement IDs, but do not replay old coordinates onto + // a newly displayed action in the same position. + clicks = nil + case "removed": + m = applyAll(m, client.UserModelMsg{Generation: s.generation, UserModel: client.UserModel{Entries: []client.UserModelEntry{{Key: "different"}}}}) + } + selected, loading, view, generation := s.list.CursorID(), s.loading, s.view, s.generation + for i, hit := range old { + updated, cmd := m.Update(surfaceHitMsg{ID: hit.id}) + m = updated.(Model) + if cmd != nil { + t.Fatal("stale semantic target submitted a request") + } + if len(clicks) > 0 { + updated, cmd = m.Update(clicks[i]) + m = updated.(Model) + if cmd != nil { + t.Fatal("undisplayed stale coordinates submitted a request") + } + } + } + if m.modal != s || s.list.CursorID() != selected || s.loading != loading || s.view != view || s.generation != generation { + t.Fatal("stale target changed current surface state") + } + _ = m.View() + if transition != "pending" && transition != "detail" { + m, cmd := clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }), false) + if cmd == nil || m.modal != s || !s.loading { + t.Fatal("fresh positive-control Inspect did not work") + } + } + }) + } +} + +func TestUserModelMouseDisabledCloseAndHiddenInspect(t *testing.T) { + m := newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true}) + m.keys.Close.SetEnabled(false) + m, s := openedUserModel(t, m) + m.deps.NoAltScreen = false + _ = m.View() + for _, target := range s.hitTargets { + if target.close { + t.Fatal("disabled Close was published") + } + } + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyEsc}) + if m.modal != s { + t.Fatal("disabled Close key closed modal") + } + _ = m.View() + _ = userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }) + m = offerCardBody(m, 12, 30) // Inspect is wider than the offer; Close is disabled. + frame := stripANSIstr(m.View().Content) + if !s.compact || len(s.hitTargets) != 0 || strings.Contains(frame, "esc") { + t.Fatal("unusable footer exposed hits, hidden activation, or disabled Close hint") + } + updated, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) + m = updated.(Model) + if cmd != nil || s.loading { + t.Fatal("hidden Inspect remained keyboard-activatable") + } + m = offerCardBody(m, 60, 18) + _ = m.View() + m, cmd = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }), false) + if cmd == nil || !s.loading { + t.Fatal("usable Inspect did not restore") + } +} + +func TestUserModelMouseCompactRestoresIndependentBrowsers(t *testing.T) { + memory := &fakeUserModel{model: client.UserModel{Entries: savedMemoryEntries(40)}} + m, s := openedUserModel(t, newUserModelModel(t, memory, client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, 60, 16) + _ = m.View() + for range 15 { + m = applyAll(m, tea.MouseWheelMsg{Button: tea.MouseWheelDown}) + } + _ = m.View() + selected, listOffset := s.list.CursorID(), s.list.Offset() + m, cmd := clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }), false) + detail := cmd().(client.UserModelDetailMsg) + detail.UserModel.Detail = &client.UserModelDetail{ + Current: client.UserModelRevision{Key: selected, Value: strings.Repeat("exact current value ", 80)}, + HistoryAvailable: true, History: []client.UserModelRevision{{Version: "old-revision", Status: "active"}}, + } + m = applyAll(m, detail) + _ = m.View() + for range 4 { + m = applyAll(m, tea.MouseWheelMsg{Button: tea.MouseWheelDown}) + } + _ = m.View() + detailOffset := s.viewport.Offset() + if detailOffset == 0 || s.list.Offset() != listOffset { + t.Fatal("detail wheel failed to scroll independently") + } + m = offerCardBody(m, 60, 1) + _ = m.View() + if !s.compact || len(m.hits.frame) != 0 { + t.Fatal("compact view exposed pointer controls") + } + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyEnter}, tea.KeyPressMsg{Code: tea.KeyDown}, tea.MouseWheelMsg{Button: tea.MouseWheelDown}) + if s.list.CursorID() != selected || s.list.Offset() != listOffset || s.viewport.Offset() != detailOffset || s.view != userModelDetail { + t.Fatal("compact input changed hidden browsing state") + } + m = offerCardBody(m, 60, 16) + _ = m.View() + if s.compact || s.viewport.Offset() != detailOffset { + t.Fatal("enlarging did not restore reader anchor") + } + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyEnd}) + if frame := stripANSIstr(m.View().Content); !strings.Contains(frame, "old-revision") { + t.Fatal("exact selected history was not reachable") + } + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }), true) + _ = m.View() + if s.view != userModelPanel || s.list.CursorID() != selected || s.list.Offset() != listOffset || s.detail != nil || s.requestKey != "" || s.err != nil { + t.Fatal("pointer Back lost parent window or retained detail/request state") + } +} + +func TestUserModelMouseNonTargetsAreInert(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, &fakeUserModel{model: client.UserModel{Entries: savedMemoryEntries(40)}}, client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + m = offerCardBody(m, 70, 16) + m = applyAll(m, tea.MouseWheelMsg{Button: tea.MouseWheelDown}) + _ = m.View() + selected, offset := s.list.CursorID(), s.list.Offset() + misses := 0 + for y := m.metrics.outerBounds.y0; y < m.metrics.outerBounds.y1; y++ { + for x := m.metrics.outerBounds.x0; x < m.metrics.outerBounds.x1; x++ { + lx, ly := m.metrics.globalToLocal(x, y) + if _, hit := m.hits.at(lx, ly); hit { + continue + } + m = applyAll(m, tea.MouseClickMsg{Button: tea.MouseLeft, X: x, Y: y}) + misses++ + if m.modal != s || s.list.CursorID() != selected || s.list.Offset() != offset || s.loading || m.sel.active { + t.Fatalf("non-target cell %d,%d activated or leaked through modal", x, y) + } + } + } + if misses == 0 { + t.Fatal("fixture lacked border/padding/overflow/whitespace misses") + } + _ = m.View() + m, cmd := clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.key != "" && hit.key != selected }), false) + if cmd != nil || s.list.CursorID() == selected || m.modal != s { + t.Fatal("enabled-row positive control did not select") + } +} + +func TestUserModelMouseLateRepliesAcrossPointerLifecycle(t *testing.T) { + for _, transition := range []string{"navigation", "back", "close-reopen", "replacement"} { + for _, fail := range []bool{false, true} { + t.Run(fmt.Sprintf("%s/error=%v", transition, fail), func(t *testing.T) { + m, s := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m.deps.NoAltScreen = false + _ = m.View() + m, cmd := clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }), false) + late := cmd().(client.UserModelDetailMsg) + switch transition { + case "navigation": + m = applyAll(m, tea.KeyPressMsg{Code: tea.KeyDown}) + case "back": + m = applyAll(m, late) + _ = m.View() + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }), false) + case "close-reopen": + _ = m.View() + m, _ = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.close }), false) + m, s = openedUserModel(t, m) + case "replacement": + (&m).closeModal() + m, s = openedUserModel(t, m) + } + if fail { + late.Err = errors.New("late failure") + } + selected, generation := s.list.CursorID(), s.generation + m = applyAll(m, late) + if m.modal != s || s.view != userModelPanel || s.loading || s.err != nil || s.detail != nil || s.list.CursorID() != selected || s.generation != generation { + t.Fatal("late detail reply changed current owner") + } + _ = m.View() + m, cmd = clickFrameHit(m, userModelMouseHit(t, m, func(hit userModelHit) bool { return hit.inspect }), false) + if cmd == nil || m.modal != s || !s.loading { + t.Fatal("late reply blocked a current positive-control Inspect") + } + }) + } + } +} diff --git a/cmd/mecatui/ui/usermodel_test.go b/cmd/mecatui/ui/usermodel_test.go index ad500bf1e5..797a42b4fa 100644 --- a/cmd/mecatui/ui/usermodel_test.go +++ b/cmd/mecatui/ui/usermodel_test.go @@ -166,12 +166,12 @@ func TestUserModelDetailHistoryScrollKeepsExactData(t *testing.T) { for i := range history { history[i] = client.UserModelRevision{Version: fmt.Sprintf("v%02d", i), Status: "active"} } - s := &userModelState{view: userModelDetail, viewport: &bounded.Viewport{}, deps: surfaceDeps{theme: theme.New("aztec", theme.AztecPalette()), marks: defaultHelpKeys()}, detail: &client.UserModelDetail{Current: client.UserModelRevision{Key: "user/exact", Value: "full exact value"}, HistoryAvailable: true, History: history}} + s := &userModelState{view: userModelDetail, viewport: &bounded.Viewport{}, deps: surfaceDeps{theme: theme.New("aztec", theme.AztecPalette()), keys: defaultKeys(), marks: defaultHelpKeys()}, detail: &client.UserModelDetail{Current: client.UserModelRevision{Key: "user/exact", Value: "full exact value"}, HistoryAvailable: true, History: history}} s.Render(70, 12) s.viewport.Move(bounded.End, len(s.detailLines)) out, _ := s.Render(70, 12) plain := stripANSIstr(out) - if len(strings.Split(plain, "\n")) > 12 || !strings.Contains(plain, "v") || !strings.Contains(plain, "back") || s.detail.Current.Value != "full exact value" { + if len(strings.Split(plain, "\n")) > 12 || !strings.Contains(plain, "v") || !strings.Contains(plain, "Back") || s.detail.Current.Value != "full exact value" { t.Fatalf("history not reachable: %q", plain) } } @@ -181,6 +181,22 @@ func TestUserModelPanelGolden(t *testing.T) { m = goldenStatusFrame(t, m) compareGolden(t, "usermodel.golden", stripANSI([]byte(m.View().Content))) } +func TestUserModelDetailGolden(t *testing.T) { + memory := sampleUserModel() + memory.model.Detail = &client.UserModelDetail{Current: client.UserModelRevision{Key: "name", Value: "Alice"}, HistoryAvailable: true, History: []client.UserModelRevision{{Version: "v1", Status: "active"}}} + m, _ := openedUserModel(t, newUserModelModel(t, memory, client.Capabilities{UserModel: true})) + updated, cmd := m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) + m = feedCmd(t, updated.(Model), cmd) + m = goldenStatusFrame(t, m) + compareGolden(t, "usermodel_detail.golden", stripANSI([]byte(m.View().Content))) +} + +func TestUserModelConstrainedFooterGolden(t *testing.T) { + m, _ := openedUserModel(t, newUserModelModel(t, sampleUserModel(), client.Capabilities{UserModel: true})) + m = offerCardBody(m, 18, 12) + compareGolden(t, "usermodel_constrained.golden", stripANSI([]byte(m.View().Content))) +} + func TestUserModelPanelEmptyDisabledGolden(t *testing.T) { m := newUserModelModel(t, &fakeUserModel{}, client.Capabilities{}) s := &userModelState{deps: (&m).surfaceDeps(), list: &bounded.List{}, viewport: &bounded.Viewport{}} @@ -270,20 +286,7 @@ func TestUserModelDescriptionRailWideGraphemesFitOrCompact(t *testing.T) { } out, _ = s.Render(6, 80) - if s.compact || strings.Contains(out, userModelDescriptionMarker) { - t.Fatalf("width 6 must render rails without the sentinel: compact=%t output=%q", s.compact, out) - } - rows := s.list.View().Rows - haveRail := false - for _, row := range rows { - if strings.HasPrefix(row.Text, userModelDescriptionMarker+userModelDescriptionRail) { - haveRail = true - } - if got := ansi.StringWidth(s.renderListRow(row)); got > 6 { - t.Fatalf("width 6 row overflows (%d): %#v", got, row) - } - } - if !haveRail || s.list.CursorID() != entries[0].Key { - t.Fatalf("width 6 lost rail or selected key: rail=%t selected=%q rows=%#v", haveRail, s.list.CursorID(), rows) + if !s.compact || strings.Contains(out, userModelDescriptionMarker) || s.list.CursorID() != entries[0].Key { + t.Fatalf("width 6 must use close-only compact output without changing selection: compact=%t output=%q", s.compact, out) } }