gui: the window's core scripts have a size budget, so a page can't grow back into it (P2)

TestCoreScriptBudget adds up the scripts index.html loads that aren't a
plugin's (assets/plugins/<id>/) or the words (i18n.js): app.js may weigh
1,215,000 bytes (1,198,239 now Sessions, Library, Routing and Usage are
plugins) and the core 1,377,000 (1,358,034), about 1.4% room. A change that
needs more raises them and says why in its commit. boot.js, made by the
server, isn't on disk and isn't counted.

Run: go test -run TestCoreScriptBudget ./internal/gui ok. Broken by
hand: with the app.js budget at 1,190,000 it fails naming app.js's bytes,
and with plugins/usage/page.js counted as core it fails on the total
(about 1.5 MB).
This commit is contained in:
yetone
2026-10-15 01:32:13 +08:00
parent 3c7ec4ba24
commit 91b9065a5f
+52
View File
@@ -0,0 +1,52 @@
package gui
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
// What the window's core may weigh, in bytes of script: everything
// index.html loads that isn't a plugin's (assets/plugins/<id>/) and isn't
// the words (i18n.js). A page belongs in a plugin. Each page moved out
// lowers these to what is left, with about 1.5% room for the core's own work,
// so a page can't quietly grow back into the core; a change that needs more
// raises them and says why in its commit.
const (
appScriptBudget = 1_215_000 // app.js: 1,198,239 after Usage left (P2)
coreScriptBudget = 1_377_000 // every core script: 1,358,034 after Usage left
)
func TestCoreScriptBudget(t *testing.T) {
page, err := os.ReadFile("assets/index.html")
if err != nil {
t.Fatal(err)
}
total, n := 0, 0
for _, m := range regexp.MustCompile(`<script src="([^"?]+)`).FindAllStringSubmatch(string(page), -1) {
name := m[1]
if strings.HasPrefix(name, "plugins/") || name == "i18n.js" {
continue
}
b, err := os.ReadFile(filepath.Join("assets", name))
if os.IsNotExist(err) {
continue // made by the server as it serves the page (boot.js)
}
if err != nil {
t.Fatal(err)
}
if name == "app.js" && len(b) > appScriptBudget {
t.Errorf("app.js is %d bytes, over its budget of %d: put the page's code in its plugin, or raise the budget and say why", len(b), appScriptBudget)
}
total += len(b)
n++
}
if n < 5 {
t.Fatalf("found only %d core scripts in index.html", n)
}
if total > coreScriptBudget {
t.Errorf("the core's scripts are %d bytes, over their budget of %d: put the page's code in its plugin, or raise the budget and say why", total, coreScriptBudget)
}
}