1package daemon
2
3import (
4 "testing"
5
6 renderer "github.com/bnema/vev-vt"
7 "github.com/bnema/vev/internal/domain"
8 "github.com/bnema/vev/internal/usecase/ui"
9 "github.com/stretchr/testify/require"
10)
11
12// framesShareStorage reports whether writing through a is visible through b.
13func framesShareStorage(a, b renderer.Frame) bool {
14 if a.Width <= 0 || a.Height <= 0 || b.Width != a.Width || b.Height != a.Height {
15 return false
16 }
17 orig := a.Cell(0, 0)
18 sentinel := renderer.Cell{Rune: '§', Style: renderer.DefaultStyle()}
19 if orig.Equal(sentinel) {
20 sentinel.Rune = '¶'
21 }
22 a.Set(0, 0, sentinel)
23 shared := b.Cell(0, 0).Equal(sentinel)
24 a.Set(0, 0, orig)
25 return shared
26}
27
28func transientToastFloatingState(withToast, withFloating, withModal bool, seed rune) capturedRenderState {
29 var notices []domain.Notification
30 if withToast {
31 notices = []domain.Notification{{Code: domain.NoticeClipboard, Severity: domain.NoticeInfo, Message: "copied", Count: 1}}
32 }
33 state := toastDamageState(notices, []renderer.Damage{{Kind: renderer.DamageText, X: 0, Y: 0, Width: 1, Height: 1}}, false)
34 state.panes[0].frame.Set(0, 0, renderer.Cell{Rune: seed, Style: renderer.DefaultStyle()})
35 if withFloating {
36 content := domain.Rect{Y: 1, Width: 80, Height: 20}
37 geometry := calculateContentFloatingGeometry(domain.Size{Cols: content.Width, Rows: content.Height}, domain.FloatingConfig{Width: 60, Height: 60})
38 state.floating = capturedFloatingRenderState{
39 visible: true,
40 pane: capturedPaneRenderState{frame: renderer.NewFrame(geometry.Inner.Width, geometry.Inner.Height), title: "float"},
41 geometry: geometry,
42 title: "float",
43 generation: 1,
44 }
45 }
46 if withModal {
47 // A modal without toast or floating is the second writer of the
48 // overlay transient page.
49 state.overlays.promptActive = true
50 state.overlays.prompt = capturedModal{
51 active: true,
52 title: "Prompt",
53 presentation: ui.Presentation{
54 Bounds: domain.Rect{X: 10, Y: 5, Width: 20, Height: 3},
55 Inner: domain.Rect{X: 11, Y: 6, Width: 18, Height: 1},
56 Borders: ui.BorderAll,
57 },
58 }
59 }
60 return state
61}
62
63// TestComposeFrameTransientScratchNeverAliasesCaches replays the attachment
64// commit/spare rotation across toast, floating, modal, and plain frames, and
65// across failed publications that compose again without committing. Each result
66// must match an independent composition without scratch, the retained cache
67// must stay toast-free, and no handed-out frame may share a page with either
68// cache slot (which would let a later paint corrupt committed state).
69func TestComposeFrameTransientScratchNeverAliasesCaches(t *testing.T) {
70 type step struct {
71 toast, floating, modal bool
72 // failed skips commitComposition, as a failed prepare or send does, so
73 // the next step composes again from the same spare.
74 failed bool
75 }
76 steps := []step{
77 {}, {toast: true}, {toast: true}, {toast: true, floating: true}, {floating: true},
78 {modal: true}, {modal: true, failed: true}, {modal: true}, {toast: true, modal: true},
79 {toast: true, floating: true, failed: true}, {toast: true, floating: true}, {toast: true},
80 {}, {toast: true, failed: true}, {toast: true}, {floating: true}, {toast: true, floating: true},
81 }
82 var render attachmentRenderState
83 for i, st := range steps {
84 seed := rune('A' + i)
85 state := transientToastFloatingState(st.toast, st.floating, st.modal, seed)
86 state.reset = i == 0
87 // A frame without scratch is the reference: it only ever clones.
88 want := composeFrame(state, render.cache)
89 got := composeFrame(state, render.cache, render.spare)
90
91 require.Equal(t, frameRows(want.frame), frameRows(got.frame), "step %d frame", i)
92 require.Equal(t, frameRows(want.cache.frame), frameRows(got.cache.frame), "step %d cache", i)
93 require.Equal(t, want.damage, got.damage, "step %d damage", i)
94
95 // The result is built in the spare page, so it may share that one, but it
96 // must never touch the committed cache a failed publication falls back to.
97 require.False(t, framesShareStorage(got.frame, render.cache.frame), "step %d: composed frame aliases the committed cache", i)
98 require.False(t, framesShareStorage(got.cache.frame, render.cache.frame), "step %d: new cache base aliases the committed cache", i)
99 if st.toast || st.floating || st.modal {
100 // A decorated frame lives on a transient page: never the cache base
101 // (toast-free by contract) nor the committed or spare cache pages.
102 require.False(t, framesShareStorage(got.frame, got.cache.frame), "step %d: decorated frame aliases the cache base", i)
103 require.False(t, framesShareStorage(got.frame, render.spare.frame), "step %d: decorated frame aliases the spare cache", i)
104 }
105 require.NotContains(t, frameText(got.cache.frame), "copied", "step %d: cache must stay toast-free", i)
106
107 if st.failed {
108 continue
109 }
110 beforeCommitted := frameRows(render.cache.frame)
111 render.commitComposition(got.cache)
112 if i > 0 {
113 require.Equal(t, beforeCommitted, frameRows(render.spare.frame))
114 }
115 // The two slots' transient pages must be distinct from each other, from
116 // both cache frames, and from every frame the next composition reads.
117 pages := []renderer.Frame{render.cache.transient.overlay, render.cache.transient.popup, render.spare.transient.overlay, render.spare.transient.popup}
118 for a := range pages {
119 for b := a + 1; b < len(pages); b++ {
120 require.False(t, framesShareStorage(pages[a], pages[b]), "step %d: transient pages %d and %d alias", i, a, b)
121 }
122 require.False(t, framesShareStorage(pages[a], render.cache.frame), "step %d: transient page %d aliases cache", i, a)
123 require.False(t, framesShareStorage(pages[a], render.spare.frame), "step %d: transient page %d aliases spare", i, a)
124 }
125 }
126}
127
128// TestComposeFrameStableToastReusesScratchPages pins the saving: with a primed
129// scratch, a stable toast frame no longer clones the base page.
130func TestComposeFrameStableToastReusesScratchPages(t *testing.T) {
131 base := composeFrame(toastDamageState(nil, nil, true), composeCacheInput{})
132 notice := []domain.Notification{{Code: domain.NoticeClipboard, Severity: domain.NoticeInfo, Message: "copied", Count: 1}}
133 shown := composeFrame(toastDamageState(notice, nil, false), base.cache, composeCacheInput{})
134 state := toastDamageState(notice, []renderer.Damage{{Kind: renderer.DamageText, X: 0, Y: 0, Width: 1, Height: 1}}, false)
135
136 cache, scratch := shown.cache, base.cache
137 // Prime both rotation slots so every page has reached its steady size.
138 for range 3 {
139 out := composeFrame(state, cache, scratch)
140 scratch, cache = cache, out.cache
141 }
142 reused := testing.AllocsPerRun(50, func() {
143 out := composeFrame(state, cache, scratch)
144 scratch, cache = cache, out.cache
145 })
146 // The baseline composes the same frame without scratch, so it clones the
147 // base page every time. Reuse must save at least one full Clone.
148 baseline := testing.AllocsPerRun(50, func() {
149 benchmarkComposeSink.frame = composeFrame(state, cache, composeCacheInput{}).frame
150 })
151 cloned := testing.AllocsPerRun(50, func() { benchmarkComposeSink.frame = cache.frame.Clone() })
152 require.Positive(t, cloned)
153 require.LessOrEqual(t, reused, baseline-cloned, "scratch reuse must save at least one Clone (baseline=%v clone=%v)", baseline, cloned)
154 out := composeFrame(state, cache, scratch)
155 require.False(t, framesShareStorage(out.frame, out.cache.frame))
156 require.Contains(t, frameText(out.frame), "copied")
157 require.NotContains(t, frameText(out.cache.frame), "copied")
158}