Artifact 2f38a269c027c27deb6191cbdc53b44badbd9024e60e6a636dc0f4c70cff8e77:
- File
internal/highlight/gate_test.go
— part of check-in
[937b10d6de]
at
2026-08-31 12:25:02
on branch trunk
— the highlight gate is a safety valve, not a policy
256KB turned ten of sqlite's src files permanently grey, which trades the feature for the fix. Raised to 2MB; the per-blob cache carries the cost until windowing removes the need for a threshold at all. (user: quarry, size: 2358) [annotate] [blame] [check-ins using]
package highlight import ( "strings" "testing" ) // Highlighting cost tracks token density and was paid on every request: // sqlite's src/btree.c, 407KB of dense C, took 490ms and rendered 2.3MB // of HTML. Past MaxBytes the lexer is skipped so the page arrives. func TestOversizeContentIsNotHighlighted(t *testing.T) { big := []byte(strings.Repeat("int x = 1; /* filler */\n", (MaxBytes/23)+64)) if len(big) <= MaxBytes { t.Fatalf("fixture is %d bytes, not over the %d limit", len(big), MaxBytes) } if !TooBig(big) { t.Error("TooBig said no for content past the limit") } if html, ok := Code(big, "", "big.c"); ok || html != "" { t.Errorf("oversize content was highlighted (%d bytes of html)", len(html)) } // Just under the limit still gets colour: the gate must not swallow // ordinary files. small := []byte("int x = 1;\n") if _, ok := Code(small, "", "small.c"); !ok { t.Error("a small C file was not highlighted") } } // A blob is immutable, so its highlighting is too. The second call must // not tokenise again — and must return the same bytes. func TestHighlightingIsCachedByContent(t *testing.T) { src := []byte("package main\n\nfunc main() { println(\"x\") }\n") first, ok := Code(src, "", "a.go") if !ok || first == "" { t.Fatal("first highlight failed") } cache.Lock() n := len(cache.m) cache.Unlock() second, ok2 := Code(src, "", "a.go") if !ok2 || second != first { t.Error("cached result differs from the first render") } cache.Lock() after := len(cache.m) cache.Unlock() if after != n { t.Errorf("a repeat of identical content added an entry (%d -> %d)", n, after) } // Same bytes, different filename, is a different lexer question. if _, _ = Code(src, "", "a.txt"); true { cache.Lock() defer cache.Unlock() if len(cache.m) == after { t.Error("the filename is not part of the cache key") } } } // The budget is in BYTES because entries are wildly uneven. Overflowing // it must not grow without bound. func TestCacheStaysWithinItsBudget(t *testing.T) { cache.Lock() cache.m, cache.bytes = map[string]string{}, 0 cache.Unlock() cachePut("k", strings.Repeat("x", cacheBudget-1)) cachePut("k2", strings.Repeat("y", 1024)) cache.Lock() defer cache.Unlock() if cache.bytes > cacheBudget { t.Errorf("cache holds %d bytes, over the %d budget", cache.bytes, cacheBudget) } }