Files
magpie/internal/library/skill_autoupdate_test.go
T
yetone 106a24b95e library: skills from GitHub and git update by themselves when switched on
0xYaCo on X: "skill 能自动化升级吗?". magpie already checked for skill
updates by itself (every 12h, as the Skills tab opens), but each update
waited for a click on Update. A new switch, Update by themselves, sits
under the Skills list while a skill comes from GitHub or git
(POST /api/library/skills/auto-update; `magpie library skill auto-update
on|off`; library.json's autoUpdateSkills, this computer's own). It is off
by default. When it is on, KeepSkillsCurrent (a gateway.WhileServing loop)
runs AutoUpdateTick 2 minutes after start and then hourly. Every 12h that
runs CheckSkills, then UpdateSomeSkills on the skills found at "update".
So a skill changed here stays in Unupdated with its Update button, one
removed upstream is only marked, and a replaced edited version goes to the
backups, as with an update by hand. Updated names and a new error are
logged.

Run (WSL2 magpie-desktop, Go 1.26.8; mote was at load 167, mica's disk full),
at ab637d3c4 (this change on 14807bcf2):
- TestAutoUpdateSkills (local git repo fixture, no network) ok.
  Broken by hand (switch and due checks removed): FAIL
  "off: ... Updated:[pdf] ... notes "two"". Restored: ok.
- -race -count=5 TestAutoUpdateSkills|TestInstallFromGitRepo: ok
- gofmt -l: clean; go vet -tags nogui ./... ok; GOOS=windows go vet ./...
  ok; go build -tags nogui, GOOS=windows go build ok
- go test -tags nogui -timeout 45m ./... under a temp HOME: every
  package ok except two reds in untouched packages, each also red alone
  at origin/main 659ca3f02: internal/update
  TestSettleAsksAboutADownloadKeptElsewhere (filed #6648) and
  internal/usage TestRequestStateKeptAcrossACatalogFetchedAgain (#6572).
- GUI: skill-auto-update.test.cjs (en, zh, zh-TW, ja, de; 400px wide;
  Chromium and WebKit), i18n-keys, i18n-en-no-cjk, skill-update-feedback
  and skill-update-edited: 36/36 pass.
Not run: the whole GUI suite had started but not finished at push
(pushed at the coordinator's ask), and nothing was run at the rebased head
e0e8f34a7 (its parents touch lessons/update only). No darwin vet: the
cgo-off cross vet fails in gui's traywindows_darwin_test.go on main too.
Not tried with a real GitHub skill in a live magpie.
2026-11-14 19:09:55 +08:00

67 lines
2.2 KiB
Go

package library
import (
"os"
"path/filepath"
"slices"
"testing"
"time"
)
// 0xYaCo on X: "skill 能自动化升级吗?". With the switch on, a skill its
// repository changed is fetched again by itself; off, it waits for the
// user; one changed here is left as it is; a look isn't made again until
// checkEvery has gone by.
func TestAutoUpdateSkills(t *testing.T) {
sandbox(t)
autoUpdate.Lock()
autoUpdate.at = time.Time{}
autoUpdate.Unlock()
addr, commit := gitRepo(t, map[string]string{
"skills/pdf/SKILL.md": "---\nname: pdf\ndescription: PDFs\n---\n",
"skills/pdf/notes.md": "one",
})
if _, err := InstallSkills(addr, []string{"skills/pdf"}, []string{"claude"}); err != nil {
t.Fatal(err)
}
notes := func() string {
b, _ := os.ReadFile(filepath.Join(skillDir("pdf"), "notes.md"))
return string(b)
}
commit(map[string]string{"skills/pdf/notes.md": "two"})
now := time.Now()
// off, the default: nothing is fetched
if res, err := AutoUpdateTick(now); err != nil || res != nil || notes() != "one" {
t.Fatalf("off: %+v %v, notes %q", res, err, notes())
}
if _, err := SetAutoUpdateSkills(true); err != nil {
t.Fatal(err)
}
if v, err := Read(nil); err != nil || !v.AutoUpdateSkills {
t.Fatalf("the view doesn't say it is on: %v", err)
}
res, err := AutoUpdateTick(now)
if err != nil || res == nil || !slices.Equal(res.Updated, []string{"pdf"}) || notes() != "two" {
t.Fatalf("on: %+v %v, notes %q", res, err, notes())
}
// not due again within checkEvery
commit(map[string]string{"skills/pdf/notes.md": "three"})
if res, err := AutoUpdateTick(now.Add(time.Hour)); err != nil || res != nil || notes() != "two" {
t.Fatalf("an hour later: %+v %v, notes %q", res, err, notes())
}
if res, err := AutoUpdateTick(now.Add(checkEvery)); err != nil || res == nil || notes() != "three" {
t.Fatalf("after checkEvery: %+v %v, notes %q", res, err, notes())
}
// changed here: left as it is
write(t, filepath.Join(skillDir("pdf"), "notes.md"), "mine")
commit(map[string]string{"skills/pdf/notes.md": "four"})
res, err = AutoUpdateTick(now.Add(2 * checkEvery))
if err != nil || res == nil || len(res.Updated) != 0 || len(res.Unupdated) != 1 || notes() != "mine" {
t.Fatalf("changed here: %+v %v, notes %q", res, err, notes())
}
}