Files
magpie/internal/gui/assets_test.go
T
yetone 5e6fb27702 perf(gui): the page's mime types are added when a page is served, not at launch
gui's init called mime.AddExtensionType, and the mime package reads and
parses the system's whole type table (/etc/apache2/mime.types on macOS,
/etc/mime.types on Linux) the first time anything asks it. So every
launch, `magpie statusline` on each Claude Code message included, paid
~18,000 allocations for a table only a served page uses. Handler now
adds the types once (sync.OnceFunc) before it builds the mux.

TestAssetTypesHoldAgainstTheRegistry now sets each type wrong first and
asks Handler to set it right. The old form passed with the call removed
on both mote and the Windows box (both tables already agree), so it
guarded nothing; with the call removed the new one is red on mote
(.css, .js, .html, .svg, .png served as application/x-other).

Measured on mote, sandbox HOME, t43 (v0.1.3517) against t44 (this):
- GODEBUG=inittrace=1 sum: 4.63 MB / 46,795 allocs -> 3.94 MB / 28,742
- `magpie statusline`, 3 runs: instructions retired ~177M -> ~156M,
  peak footprint 16.4-17.0 MB -> 16.1-16.3 MB

Run: go vet ./internal/gui (darwin, GOOS=windows) ok; go test
./internal/gui ok (130s); root go vet ./... after rebase below.
2026-11-10 13:02:57 +08:00

25 lines
653 B
Go

package gui
import (
"mime"
"sync"
"testing"
)
// Handler sets the page's own types even where the system's table says
// otherwise (Windows' registry, which another program may have changed).
func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) {
for ext := range assetTypes {
mime.AddExtensionType(ext, "application/x-other")
}
was := addAssetTypes
addAssetTypes = sync.OnceFunc(registerAssetTypes)
t.Cleanup(func() { addAssetTypes = was; registerAssetTypes() })
Handler(nil, nil)
for ext, want := range assetTypes {
if got := mime.TypeByExtension(ext); got != want {
t.Errorf("%s served as %q, not %q", ext, got, want)
}
}
}