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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package chat

import (
	"strings"
	"testing"

	"github.com/charmbracelet/crush/internal/message"
	"github.com/charmbracelet/crush/internal/ui/styles"
	"github.com/stretchr/testify/require"
)

// Fixed Unix timestamps for deterministic cache-equality tests. The
// thinking section's `extra` hash folds in ThinkingDuration, which
// in turn depends on (FinishedAt - StartedAt). Anchoring both
// timestamps removes any wall-clock dependency from the cache key
// so two builds across a second boundary still hit the cache.
const (
	testStartedAt  int64 = 1_700_000_000
	testFinishedAt int64 = 1_700_000_005
	testFinishTime int64 = 1_700_000_006
)

// thinkingMessage builds an assistant message with a fixed reasoning
// content and an optional text content. When text is empty the
// message represents a still-thinking turn (matches IsThinking()).
// Both reasoning timestamps are anchored to fixed Unix seconds so
// ThinkingDuration is deterministic and cache-equality assertions
// don't depend on wall-clock time.
func thinkingMessage(id, thinking, text string) *message.Message {
	parts := []message.ContentPart{
		message.ReasoningContent{
			Thinking:   thinking,
			StartedAt:  testStartedAt,
			FinishedAt: testFinishedAt,
		},
	}
	if text != "" {
		parts = append(parts, message.TextContent{Text: text})
	}
	return &message.Message{
		ID:    id,
		Role:  message.Assistant,
		Parts: parts,
	}
}

// errorMessage builds a finished assistant message whose finish part
// carries an error reason plus a custom message and details.
func errorMessage(id, errMsg, errDetails string) *message.Message {
	return &message.Message{
		ID:   id,
		Role: message.Assistant,
		Parts: []message.ContentPart{
			message.TextContent{Text: "partial output"},
			message.Finish{
				Reason:  message.FinishReasonError,
				Message: errMsg,
				Details: errDetails,
				Time:    testFinishTime,
			},
		},
	}
}

// renderTwoSetMessages drives a SetMessage cycle and returns the
// section-cache identity (out string pointers via direct comparison
// of the cached fields). The test compares `out` strings; identical
// output across cycles is the cache-hit indicator we rely on.
type sectionSnapshot struct {
	thinking string
	content  string
	errSec   string
}

func snapshot(a *AssistantMessageItem) sectionSnapshot {
	return sectionSnapshot{
		thinking: a.thinkingSec.out,
		content:  a.contentSec.out,
		errSec:   a.errorSec.out,
	}
}

// TestAssistantSectionCache_ContentChangeDoesNotInvalidateThinking covers
// the central F4 invariant: streaming the main content through SetMessage
// must keep the cached thinking render intact, provided the inputs to
// the thinking section render (text, expanded flag, footer state) are
// unchanged. We seed an already-non-empty content so that IsThinking()
// is false on both renders — that's the steady streaming state where
// the thinking block has finished and content keeps growing.
func TestAssistantSectionCache_ContentChangeDoesNotInvalidateThinking(t *testing.T) {
	sty := styles.CharmtonePantera()
	thinking := "Step 1\nStep 2\nStep 3"
	msg := thinkingMessage("a1", thinking, "Initial answer.")
	item := NewAssistantMessageItem(&sty, msg).(*AssistantMessageItem)

	const width = 71

	_ = item.RawRender(width)
	first := snapshot(item)
	require.NotEmpty(t, first.thinking, "thinking section must be populated after first render")

	// Stream more content into the existing turn. Thinking text and
	// footer state are byte-identical between the two renders.
	updated := thinkingMessage("a1", thinking, "Initial answer. More streamed text.")
	item.SetMessage(updated)
	_ = item.RawRender(width)
	second := snapshot(item)

	require.Equal(t, first.thinking, second.thinking,
		"content streaming must not invalidate the thinking section render")
	require.NotEqual(t, first.content, second.content,
		"content section must have been re-rendered")
}

// TestAssistantSectionCache_ThinkingChangeDoesNotInvalidateContent is the
// mirror of the previous test: extending thinking text must not force a
// re-render of the content section.
func TestAssistantSectionCache_ThinkingChangeDoesNotInvalidateContent(t *testing.T) {
	sty := styles.CharmtonePantera()
	content := "Final answer goes here."
	msg := thinkingMessage("a2", "Step 1", content)
	item := NewAssistantMessageItem(&sty, msg).(*AssistantMessageItem)

	const width = 73

	_ = item.RawRender(width)
	first := snapshot(item)
	require.NotEmpty(t, first.content)

	updated := thinkingMessage("a2", "Step 1\nStep 2", content)
	item.SetMessage(updated)
	_ = item.RawRender(width)
	second := snapshot(item)

	require.Equal(t, first.content, second.content,
		"thinking streaming must not invalidate the content section render")
	require.NotEqual(t, first.thinking, second.thinking,
		"thinking text changed; thinking section must have re-rendered")
}

// TestAssistantSectionCache_HashKeyDiscrimination asserts that two
// messages with different source text hash to different per-section
// keys, and that messages with identical source text hit the cache.
func TestAssistantSectionCache_HashKeyDiscrimination(t *testing.T) {
	sty := styles.CharmtonePantera()
	msgA := thinkingMessage("a3", "thinking A", "content A")
	msgB := thinkingMessage("a3", "thinking B", "content B")

	itemA := NewAssistantMessageItem(&sty, msgA).(*AssistantMessageItem)
	itemB := NewAssistantMessageItem(&sty, msgB).(*AssistantMessageItem)

	thinkSrcA, _ := itemA.thinkingKey()
	thinkSrcB, _ := itemB.thinkingKey()
	require.NotEqual(t, thinkSrcA, thinkSrcB,
		"distinct thinking text must produce distinct FNV-64 source hashes")

	contentSrcA, _ := itemA.contentKey()
	contentSrcB, _ := itemB.contentKey()
	require.NotEqual(t, contentSrcA, contentSrcB,
		"distinct content text must produce distinct FNV-64 source hashes")

	// Identical source text on a fresh item must produce the same
	// hashes — keying invariant for cache hits.
	itemAClone := NewAssistantMessageItem(&sty, thinkingMessage("a3", "thinking A", "content A")).(*AssistantMessageItem)
	thinkSrcAClone, _ := itemAClone.thinkingKey()
	contentSrcAClone, _ := itemAClone.contentKey()
	require.Equal(t, thinkSrcA, thinkSrcAClone)
	require.Equal(t, contentSrcA, contentSrcAClone)
}

// TestAssistantSectionCache_CloneRoundTrip guards the contract that
// message.Clone() does not invalidate any section cache: re-keying off
// the cloned message must produce identical hashes and the section
// caches must serve byte-identical renders.
func TestAssistantSectionCache_CloneRoundTrip(t *testing.T) {
	sty := styles.CharmtonePantera()
	orig := thinkingMessage("a4", "Reasoning step.", "Answer text.")
	item := NewAssistantMessageItem(&sty, orig).(*AssistantMessageItem)

	const width = 75
	_ = item.RawRender(width)
	first := snapshot(item)

	cloned := orig.Clone()
	item.SetMessage(&cloned)
	_ = item.RawRender(width)
	second := snapshot(item)

	require.Equal(t, first.thinking, second.thinking, "clone must hit the thinking cache")
	require.Equal(t, first.content, second.content, "clone must hit the content cache")
}

// TestAssistantSectionCache_ResizeInvalidatesAll asserts that a width
// change forces a re-render of every section.
func TestAssistantSectionCache_ResizeInvalidatesAll(t *testing.T) {
	sty := styles.CharmtonePantera()
	msg := errorMessage("a5", "boom", strings.Repeat("detail line\n", 5))
	// errorMessage returns FinishReasonError; combine with thinking
	// content so all three sections are exercised.
	msg.Parts = append([]message.ContentPart{
		message.ReasoningContent{
			Thinking:   "Considering options.",
			StartedAt:  testStartedAt,
			FinishedAt: testFinishedAt,
		},
	}, msg.Parts...)
	item := NewAssistantMessageItem(&sty, msg).(*AssistantMessageItem)

	_ = item.RawRender(77)
	first := snapshot(item)
	require.NotEmpty(t, first.thinking)
	require.NotEmpty(t, first.content)
	require.NotEmpty(t, first.errSec)

	_ = item.RawRender(117)
	second := snapshot(item)

	require.NotEqual(t, first.thinking, second.thinking, "resize must re-render the thinking section")
	require.NotEqual(t, first.content, second.content, "resize must re-render the content section")
	require.NotEqual(t, first.errSec, second.errSec, "resize must re-render the error section")
}

// TestAssistantSectionCache_ErrorIndependentOfThinkingAndContent guards
// that the error section caches independently. Editing the error
// message must not invalidate the other two sections, and editing the
// content must not invalidate the error section.
func TestAssistantSectionCache_ErrorIndependentOfThinkingAndContent(t *testing.T) {
	sty := styles.CharmtonePantera()
	build := func(thinking, content, errMsg, errDetails string) *message.Message {
		return &message.Message{
			ID:   "a6",
			Role: message.Assistant,
			Parts: []message.ContentPart{
				message.ReasoningContent{
					Thinking:   thinking,
					StartedAt:  testStartedAt,
					FinishedAt: testFinishedAt,
				},
				message.TextContent{Text: content},
				message.Finish{
					Reason:  message.FinishReasonError,
					Message: errMsg,
					Details: errDetails,
					Time:    testFinishTime,
				},
			},
		}
	}

	item := NewAssistantMessageItem(&sty, build("think", "content", "boom", "details")).(*AssistantMessageItem)
	_ = item.RawRender(79)
	first := snapshot(item)

	// Change only the error text. Thinking and content caches must
	// survive; error cache must miss and re-render.
	item.SetMessage(build("think", "content", "different boom", "different details"))
	_ = item.RawRender(79)
	second := snapshot(item)

	require.Equal(t, first.thinking, second.thinking, "error change must not invalidate thinking")
	require.Equal(t, first.content, second.content, "error change must not invalidate content")
	require.NotEqual(t, first.errSec, second.errSec, "error change must re-render the error section")

	// Now change only the content; error cache must survive.
	item.SetMessage(build("think", "different content", "different boom", "different details"))
	_ = item.RawRender(79)
	third := snapshot(item)

	require.Equal(t, second.thinking, third.thinking)
	require.NotEqual(t, second.content, third.content)
	require.Equal(t, second.errSec, third.errSec, "content change must not invalidate the error section")
}

// TestAssistantSectionCache_PrefixCacheRespectsSectionChanges guards
// the F3/F4 boundary: the prefix cache must invalidate when any
// underlying section changes. We verify by comparing the F3-cached
// Render output across SetMessage cycles.
func TestAssistantSectionCache_PrefixCacheRespectsSectionChanges(t *testing.T) {
	sty := styles.CharmtonePantera()
	build := func(content string) *message.Message {
		return &message.Message{
			ID:   "a7",
			Role: message.Assistant,
			Parts: []message.ContentPart{
				message.TextContent{Text: content},
				message.Finish{Reason: message.FinishReasonEndTurn, Time: testFinishTime},
			},
		}
	}

	item := NewAssistantMessageItem(&sty, build("first content")).(*AssistantMessageItem)
	item.SetFocused(true)

	const width = 81
	first := item.Render(width)

	item.SetMessage(build("second content"))
	second := item.Render(width)
	require.NotEqual(t, first, second,
		"prefix cache must invalidate when the content section changes")

	// Re-set to the original content; the prefix cache should
	// produce identical output again.
	item.SetMessage(build("first content"))
	third := item.Render(width)
	require.Equal(t, first, third)
}

// TestAssistantSectionCache_ByteIdenticalToFreshRender asserts that the
// F4 cached path produces the same bytes as a fresh-instance render of
// the equivalent message — i.e. caching is invisible from the outside.
// Drives a sequence of mutations (thinking change, content change,
// finish) and compares every step against an independent item rendered
// from scratch.
func TestAssistantSectionCache_ByteIdenticalToFreshRender(t *testing.T) {
	sty := styles.CharmtonePantera()
	const width = 83

	type step struct {
		name string
		msg  *message.Message
	}
	startedAt := testStartedAt
	finishedAt := testFinishedAt
	finishTime := testFinishTime
	steps := []step{
		{
			name: "thinking-only",
			msg: &message.Message{
				ID: "iso", Role: message.Assistant,
				Parts: []message.ContentPart{
					message.ReasoningContent{Thinking: "first reasoning", StartedAt: startedAt},
				},
			},
		},
		{
			name: "thinking-grew",
			msg: &message.Message{
				ID: "iso", Role: message.Assistant,
				Parts: []message.ContentPart{
					message.ReasoningContent{Thinking: "first reasoning more", StartedAt: startedAt},
				},
			},
		},
		{
			name: "content-arrived",
			msg: &message.Message{
				ID: "iso", Role: message.Assistant,
				Parts: []message.ContentPart{
					message.ReasoningContent{Thinking: "first reasoning more", StartedAt: startedAt, FinishedAt: finishedAt},
					message.TextContent{Text: "the answer"},
				},
			},
		},
		{
			name: "finished-end-turn",
			msg: &message.Message{
				ID: "iso", Role: message.Assistant,
				Parts: []message.ContentPart{
					message.ReasoningContent{Thinking: "first reasoning more", StartedAt: startedAt, FinishedAt: finishedAt},
					message.TextContent{Text: "the answer"},
					message.Finish{Reason: message.FinishReasonEndTurn, Time: finishTime},
				},
			},
		},
	}

	first := steps[0].msg.Clone()
	cached := NewAssistantMessageItem(&sty, &first).(*AssistantMessageItem)
	for _, s := range steps {
		cached.SetMessage(s.msg)
		freshMsg := s.msg.Clone()
		fresh := NewAssistantMessageItem(&sty, &freshMsg).(*AssistantMessageItem)
		require.Equal(t, fresh.RawRender(width), cached.RawRender(width),
			"step %q: cached path must match fresh render byte-for-byte", s.name)
	}
}

// TestAssistantSectionCache_PrefixCacheInvalidatesOnCompositionOnlyChange
// guards the F3 prefix cache against composition-only changes:
// flipping the finish reason from EndTurn to Canceled appends a
// constant "Canceled" line via renderMessageContent, but no
// section's own source text changes. The prefix cache must observe
// the difference (compositionKey is folded into prefixCacheKey) and
// the resulting bytes must differ. As a second guarantee, a fresh
// item built with the same final state must produce byte-equal
// output to the cached item — caching must never produce stale or
// divergent renders.
func TestAssistantSectionCache_PrefixCacheInvalidatesOnCompositionOnlyChange(t *testing.T) {
	sty := styles.CharmtonePantera()
	const width = 87

	build := func(reason message.FinishReason) *message.Message {
		return &message.Message{
			ID:   "comp",
			Role: message.Assistant,
			Parts: []message.ContentPart{
				message.TextContent{Text: "hi"},
				message.Finish{Reason: reason, Time: testFinishTime},
			},
		}
	}

	item := NewAssistantMessageItem(&sty, build(message.FinishReasonEndTurn)).(*AssistantMessageItem)
	endTurnOut := item.Render(width)

	// Flip only the finish reason. Thinking is empty and content
	// text is unchanged, so no section's source hash moves; only
	// compositionKey shifts. The prefix cache must miss.
	item.SetMessage(build(message.FinishReasonCanceled))
	canceledOut := item.Render(width)
	require.NotEqual(t, endTurnOut, canceledOut,
		"prefix cache must invalidate on composition-only change (finish reason)")

	// A fresh item built with the same final state must match the
	// cached item byte-for-byte — caching is invisible from the
	// outside and never serves stale output.
	fresh := NewAssistantMessageItem(&sty, build(message.FinishReasonCanceled)).(*AssistantMessageItem)
	require.Equal(t, fresh.Render(width), canceledOut,
		"cached output must equal a fresh render of the same final state")
}

// TestAssistantSectionCache_ThinkingBoxHeightSurvivesCacheHit guards
// click-detection geometry across thinking-section cache hits. The
// thinking box height feeds HandleMouseClick; it is recomputed
// inside renderThinking and must be restored from
// assistantSection.aux when the thinking cache hits. We render once
// to capture the original height, trigger a content-only change so
// thinkingKey stays identical (thinking text, expanded flag, and
// footer state all unchanged), render again, and assert the
// thinkingBoxHeight field is preserved.
func TestAssistantSectionCache_ThinkingBoxHeightSurvivesCacheHit(t *testing.T) {
	sty := styles.CharmtonePantera()
	const width = 71

	thinking := strings.Join([]string{
		"Considering the request.",
		"Looking at the relevant files.",
		"Drafting a plan.",
		"Verifying constraints.",
	}, "\n")
	msg := thinkingMessage("hbox", thinking, "initial answer")
	item := NewAssistantMessageItem(&sty, msg).(*AssistantMessageItem)
	item.thinkingViewMode = thinkingFullExpanded

	_ = item.RawRender(width)
	originalHeight := item.thinkingBoxHeight
	require.Greater(t, originalHeight, 0,
		"thinking box height must be populated after first render")

	// Stomp the field so a stale read (cache hit that fails to
	// restore aux) is detectable. Then trigger a content-only
	// change: thinkingKey is byte-identical between renders, so
	// the thinking section cache must hit and restore the
	// preserved height via assistantSection.aux.
	item.thinkingBoxHeight = -1
	updated := thinkingMessage("hbox", thinking, "initial answer with more streamed text")
	item.SetMessage(updated)
	_ = item.RawRender(width)

	require.Equal(t, originalHeight, item.thinkingBoxHeight,
		"thinkingBoxHeight must be preserved across thinking section cache hits "+
			"so HandleMouseClick keeps targeting the right rows")
}