Files
magpie/internal/plugin/updates_keep_test.go
T
yetoneandggbdpq 58651568b3 agent, sessions, provider: a state file that doesn't parse is kept aside, not written over as empty (#2263, ggbdpq)
applied.json, wsl.json and sessions-wsl.json were read as empty when they
didn't parse, and the next save wrote that emptiness over them. What
magpie had applied to each agent, the WSL distros and the WSL session
listing were lost with no copy. stash.json already kept a bad file aside
as .bad-<time>. Each of these now does the same through one helper,
lastgood.KeepUnparsed, instead of a copy per file:
- It does nothing when the file is missing or empty.
- When the file doesn't unmarshal into the reader's own type
  (lastgood.Into[T]), it copies the file aside (KeepBad).
- It stops the write when the copy can't be kept.

This also fixes the issue's claim about stash.json. Stash judged a file
with json.Valid, so `[]`, which a map reader takes as empty, was written
over unkept. Its failed copy was only logged, and the write went ahead
anyway. Both are fixed (TestUnreadStashArrayKeptAside), and
gateway-hidden.json uses the same helper.

Siblings found by going through every single-file JSON reader that takes
a bad file as empty and then writes it whole:
- stepfun-auth
- login-returns
- quota-history
- balance-history
- credits-daily
- Claude Desktop tiers
- plugin updates
- usage's computer id
- davsync's sync state

Each now keeps a bad copy. Caches that are rebuilt from the vendor are
left alone; docs/subsystems/providers-accounts.md lists them.

Run:
- gofmt -l, go vet (darwin -tags nogui, GOOS=linux -tags nogui,
  GOOS=windows): clean.
- go test -tags nogui -p 4 -timeout 40m ./... with the other three
  commits of this push: see the last one's body.

Broken by hand: on the code before this, these fail:
- the issue's applied_keep_test.go and wsl_keep_test.go
- TestUnreadStashArrayKeptAside
- TestUnreadStateKeptAside (10 subtests)
- the gateway, usage, davsync and plugin _keep tests
TestKeepUnparsed and TestKeepUnparsedFailsWhenItCantBeKept cover the
helper.

I dropped a test, "not written when it can't be kept", for stash. It passed
on the old code too, because a read-only folder also stops the atomic
write.

Not tried: a real WSL distro.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-15 09:36:26 +08:00

40 lines
1.1 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"testing"
)
// plugin-updates.json is read as no updates when it doesn't parse, and the
// next check wrote that over it: Updated, the plugins magpie updated by
// itself, can't be found again (#2263). What was there is copied aside
// first.
func TestUnreadUpdatesKeptAside(t *testing.T) {
dir := t.TempDir()
t.Setenv("HOME", dir)
t.Setenv("USERPROFILE", dir)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
if err := os.MkdirAll(filepath.Dir(updatesPath()), 0o700); err != nil {
t.Fatal(err)
}
was := `{"updated":[{"package":"opencode-gemini-auth","from":"0.1.2","to":"0.2`
if err := os.WriteFile(updatesPath(), []byte(was), 0o600); err != nil {
t.Fatal(err)
}
if _, err := CheckUpdates(context.Background()); err != nil {
t.Fatal(err)
}
bad, _ := filepath.Glob(updatesPath() + ".bad-*")
if len(bad) != 1 {
t.Fatalf("kept aside: %v", bad)
}
if b, _ := os.ReadFile(bad[0]); string(b) != was {
t.Fatalf("kept aside as %q, want %q", b, was)
}
if PendingUpdates().Checked.IsZero() {
t.Fatal("the check wasn't written")
}
}