Files
magpie/internal/agent/apps_kept_test.go
T
yetone ffc18a850d agent: an app folder is listed and an Info.plist read again only when it changed
The Agents page asks every desktop app whether it is installed, and its
version, on each poll. Each ask listed /Applications and ~/Applications
whole (os.ReadDir, for the one-level-down folders of #2325) and read
every candidate bundle's Info.plist. With real accounts, 40 window polls
spent 38.6 MB of allocations in Agent.Detected, 14.5 MB of it ReadDir.

folderSubs keeps a folder's non-bundle subfolders while its
modification time stays (adding, removing or renaming an entry changes
it), and bundleInfo keeps what an Info.plist said while its size and
modification time stay. A time under 2s old is never trusted, as git
treats a racily clean index. macBundle and desktopApp.version read
through bundleInfo; what each reports is unchanged.

Run on mote (macOS), at this tree:
- gofmt clean; go vet ./internal/agent for darwin, windows and linux ok.
- TestAppFoldersReadAgainOnlyWhenChanged alone 3/3 ok.
- Broken by hand: never trusting the kept list and plist -> "an
  unchanged app folder and Info.plist cost 1268 allocations a look";
  trusting them whatever changed -> "Moved.app in a folder added since
  isn't found" and "Kept.app's Info.plist says another id now".
- go test ./internal/agent ./internal/tui ./internal/cli: ok.
- go test ./internal/gui (with the next commit's providers.go too): red
  only on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949), as before.
- Real-account sandbox, 40 window polls against v0.1.2504's tree:
  total allocations 509.9 -> 427.6 MB, Agent.Detected 38.6 -> 21.5 MB,
  os.ReadDir 14.5 -> 4.0 MB; /api/state and drift output byte-identical.
  phys_footprint_peak 139 MB (136 before; within run noise).
Not run: the whole ./... suite; the Linux and Windows app paths (they
don't reach these readers).
2026-11-01 08:12:34 +08:00

80 lines
2.7 KiB
Go

package agent
import (
"os"
"path/filepath"
"strconv"
"testing"
"time"
)
// The Agents page asks every desktop app whether it is installed on each
// poll. An app folder of many entries is listed again only when it
// changed, and an Info.plist is read again only when it changed.
func TestAppFoldersReadAgainOnlyWhenChanged(t *testing.T) {
apps := t.TempDir()
wasFolders, wasSpot := appFolders, spotlightApps
appFolders = func() []string { return []string{apps} }
spotlightApps = func(string) []string { return nil }
t.Cleanup(func() { appFolders, spotlightApps = wasFolders, wasSpot })
plist := func(id string) []byte {
return []byte("<plist><dict><key>CFBundleIdentifier</key><string>" + id +
"</string><key>CFBundleShortVersionString</key><string>1.2.3</string></dict></plist>")
}
write := func(bundle, id string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(bundle, "Contents"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(bundle, "Contents", "Info.plist"), plist(id), 0o644); err != nil {
t.Fatal(err)
}
}
for i := range 300 {
if err := os.Mkdir(filepath.Join(apps, "Other "+strconv.Itoa(i)+".app"), 0o755); err != nil {
t.Fatal(err)
}
}
write(filepath.Join(apps, "Kept.app"), "test.kept")
old := time.Now().Add(-time.Hour)
age := func(paths ...string) {
t.Helper()
for _, p := range paths {
if err := os.Chtimes(p, old, old); err != nil {
t.Fatal(err)
}
}
}
keptPlist := filepath.Join(apps, "Kept.app", "Contents", "Info.plist")
age(apps, keptPlist)
app := desktopApp{id: "test.kept", names: []string{"Kept.app"}}
if !macBundle(app) || app.version() != "1.2.3" {
t.Fatalf("Kept.app not found: bundle %v version %q", macBundle(app), app.version())
}
if n := testing.AllocsPerRun(20, func() { macBundle(app); app.version() }); n > 40 {
t.Errorf("an unchanged app folder and Info.plist cost %v allocations a look; they are listed and read again", n)
}
// A folder added one level down is looked in at once (#2325).
moved := desktopApp{id: "test.moved", names: []string{"Moved.app"}}
if macBundle(moved) {
t.Fatal("Moved.app found before it was installed")
}
write(filepath.Join(apps, "AI", "Moved.app"), "test.moved")
if !macBundle(moved) {
t.Error("Moved.app in a folder added since isn't found: the folder's old list was kept")
}
// So is an Info.plist written since, of the same size and an old time
// the folder list was kept at.
age(apps, filepath.Join(apps, "AI"))
macBundle(moved)
if err := os.WriteFile(keptPlist, plist("test.kxpt"), 0o644); err != nil {
t.Fatal(err)
}
if macBundle(app) {
t.Error("Kept.app's Info.plist says another id now, and the old one was kept")
}
}