Files
magpie/internal/gui/assets_packed_test.go
T
yetone 89f66966d9 release builds: the UI's files are packed into one zip, ~5.7 MB off each binary (@ZhangXiaof48473 on X)
The Windows and Linux downloads (and the CLI builds) are the raw binary, so
what is embedded counts in full. 8.2 MB of the 12 MB under internal/gui/assets
is scripts, styles and svgs. `make assets-pack` (build/assetpack) writes them
to internal/gui/assets.zip (deflate; png/jpg/webm stored), and the release
targets and `make app` build with -tags packedassets, which embeds that zip
instead of the tree (assets_packed.go: a file is unpacked once, on first use,
and served seekable with no modification time, as an embedded one is). Plain
`go build`, `make build`, `make cli` and every test still embed the tree.
The zip is git-ignored and made before every release build.

Sizes (go build -trimpath -ldflags="-s -w", on mote, at e430e2750):
  linux/amd64 nogui   73,203,874 -> 67,502,242  (-5,701,632)
  windows/amd64 app   88,401,408 -> 82,755,584  (-5,645,824)
The macOS zip/dmg compress the binary already, so they gain nothing.

Run: assetpack, then `go test -tags packedassets -run TestPacked ./internal/gui`
(passes: every file byte for byte and no other; / and a range of the webm served;
no Last-Modified). The first try failed it with "packed has 0 files, the tree
301" (zip entries lacked the assets/ prefix), so it does go red on a bad pack.
go vet ./internal/gui on darwin, linux nogui, windows and -tags packedassets:
clean. gofmt clean.

Not run: the full suite and the GUI suite (default builds embed the tree as
before; api.go only lost the embed line), a real release through
magpie-releases (its release.yml calls these make targets), the Linux GTK
build (cgo).
2026-11-11 10:28:05 +08:00

81 lines
2.3 KiB
Go

//go:build packedassets
package gui
import (
"bytes"
"io/fs"
"net/http"
"net/http/httptest"
"os"
"sort"
"testing"
)
// The packed UI must be the tree's: every file, byte for byte, and no other.
// Run after `go run ./build/assetpack internal/gui/assets internal/gui/assets.zip`.
func TestPackedAssetsAreTheTree(t *testing.T) {
tree := os.DirFS("assets")
var want []string
fs.WalkDir(tree, ".", func(p string, d fs.DirEntry, err error) error {
if err == nil && d.Type().IsRegular() {
want = append(want, p)
}
return nil
})
if len(want) < 100 {
t.Fatalf("only %d files in the tree", len(want))
}
var got []string
fs.WalkDir(assets, "assets", func(p string, d fs.DirEntry, err error) error {
if err == nil && d.Type().IsRegular() {
got = append(got, p[len("assets/"):])
}
return nil
})
sort.Strings(want)
sort.Strings(got)
if len(got) != len(want) {
t.Fatalf("packed has %d files, the tree %d", len(got), len(want))
}
for i, n := range want {
if got[i] != n {
t.Fatalf("file %d: packed %q, tree %q", i, got[i], n)
}
a, _ := fs.ReadFile(tree, n)
b, err := assets.ReadFile("assets/" + n)
if err != nil || !bytes.Equal(a, b) {
t.Fatalf("%s differs from the tree (err %v)", n, err)
}
}
}
// The page is served as the tree's was: index at /, a range of a video, 404.
func TestPackedAssetsServe(t *testing.T) {
h := http.FileServer(http.FS(staticFS()))
get := func(path, rng string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", path, nil)
if rng != "" {
r.Header.Set("Range", rng)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
idx, _ := os.ReadFile("assets/index.html")
if w := get("/", ""); w.Code != 200 || !bytes.Equal(w.Body.Bytes(), idx) {
t.Fatalf("/ gave %d, %d bytes (index.html is %d)", w.Code, w.Body.Len(), len(idx))
}
webm, _ := os.ReadFile("assets/plugins/redact/xq_light.webm")
w := get("/plugins/redact/xq_light.webm", "bytes=100-199")
if w.Code != 206 || !bytes.Equal(w.Body.Bytes(), webm[100:200]) {
t.Fatalf("range of the video gave %d, %d bytes", w.Code, w.Body.Len())
}
if w := get("/nope.js", ""); w.Code != 404 {
t.Fatalf("a missing file gave %d", w.Code)
}
if lm := get("/app.js", "").Header().Get("Last-Modified"); lm != "" {
t.Fatalf("Last-Modified %q: an embedded file has none", lm)
}
}