Files
magpie/internal/gui/core_budget_test.go
T
yetone d964031412 provider: a model's test can be sent to one chosen account or key (tmr11235 on X)
A provider with two or more accounts or keys that are on now offers
"Test on account..." in a model chip's right-click, an app menu (never a
native select) of those accounts' or keys' names. The pick is posted as
`account` with provider/test and pins the test (provider.TestOn,
accountNamed / keyNamed; an unknown or switched-off one is an error row,
not a silent fallback). Every result now names the account or key it went
to (Result.Account, set in probe), and the status line ends " - <name>"
for such a provider, whether the test was pinned or routing picked.
Test models and the CLI still test with routing's pick.

Core script budget raised 1,263,333 -> 1,263,700 (+367): the model chip
menu is core code and there is no page to move it to; the PLAY icon
literal was folded into one constant and the status call tightened first.

Run: gofmt clean; mote-run go test ./internal/gui ./internal/provider ok
(new TestModelTestPinnedToAKey, TestTestPicksNeedSeveral,
TestModelTestPinnedToAnAccount); model-test-account.test.cjs 10 of 10
(chromium and webkit, en zh zh-TW ja de) on mica. Broken by hand: with
the menu entry disabled the GUI test is 0 of 10. GUI batch model-*,
provider-*, i18n-*, gui-*: reds are the same on origin/main 2e92d71aa
(model-chip-steady, model-copy-id, provider-fetch-again, provider-max-
output, provider-many-keys, provider-mask, provider-typed, gui-de/ja),
all already filed (#4388, #4621, #3292, #6209, #5109, #4673, #5475).

Not tried: a real multi-account provider end to end; the CLI and the bulk
Test models button (unchanged); vet on windows/linux not rerun beyond the
earlier builds.
2026-11-12 18:09:14 +08:00

76 lines
2.6 KiB
Go

package gui
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
// What the window's core may weigh, in bytes of script without its
// whole-line comments: 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, so a page
// can't quietly grow back into the core; a change that needs more raises
// them and says why in its commit. Comments aren't counted, so they are
// never what pays for a change (#5918); the history of each number is in
// `git log -L` of this file.
const (
appScriptBudget = 1_086_267 // app.js: 1,080,614 at 8016205e3, with the 5,653 of room it had
coreScriptBudget = 1_263_700 // every core script at 8016205e3, the old measure 682 over its cap there (#5918); +367 for the model test on a chosen account (tmr11235), no page left to move it to
)
// codeBytes is how many bytes of b are code: its blank lines, its lines
// that are only a // comment, and its /* */ blocks that start a line,
// aren't counted.
func codeBytes(b []byte) int {
n, block := 0, false
for _, l := range strings.SplitAfter(string(b), "\n") {
t := strings.TrimSpace(l)
switch {
case block:
block = !strings.Contains(t, "*/")
case t == "", strings.HasPrefix(t, "//"):
case strings.HasPrefix(t, "/*") && (strings.HasSuffix(t, "*/") || !strings.Contains(t, "*/")):
block = !strings.HasSuffix(t, "*/")
default:
n += len(l)
}
}
return n
}
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)
}
code := codeBytes(b)
if name == "app.js" && code > appScriptBudget {
t.Errorf("app.js is %d bytes of code, over its budget of %d: put the page's code in its plugin, or raise the budget and say why (comments aren't counted, so cutting them doesn't help)", code, appScriptBudget)
}
total += code
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 of code, over their budget of %d: put the page's code in its plugin, or raise the budget and say why (comments aren't counted, so cutting them doesn't help)", total, coreScriptBudget)
}
}