▍ humdrum codex / glint v1.0.2
license AGPL-3.0
1.0 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package editor

import (
	"strings"
	"testing"
)

func cacheDoc() *Editor {
	e := New()
	var b strings.Builder
	for i := 0; i < 200; i++ {
		b.WriteString("Some **prose** line with a [link](http://x) and `code` here.\n")
	}
	e.SetContent([]byte(b.String()))
	e.SetSize(80, 24)
	return e
}

// Scrolling and rendering must not re-scan the whole document: the visual model
// depends only on content/width/theme/spell, none of which a scroll changes.
func TestScrollReusesVisualCache(t *testing.T) {
	e := cacheDoc()
	e.buildCount = 0
	for i := 0; i < 10; i++ {
		e.ScrollBy(1)
		_ = e.View()
	}
	if e.buildCount > 1 {
		t.Errorf("scroll+view rebuilt visual %d times, want <= 1", e.buildCount)
	}
}

// An edit must invalidate the cache so the next render reflects new content.
func TestEditInvalidatesCache(t *testing.T) {
	e := cacheDoc()
	_ = e.View() // prime cache
	e.SetContent([]byte("brand new content\n"))
	out := e.View()
	if !strings.Contains(out, "brand new content") {
		t.Errorf("View after SetContent did not reflect new content:\n%s", out)
	}
}