Files
magpie/internal/davsync/skill_source_test.go
T
yetone 223b900b48 library: a skill from GitHub or git syncs as its source, and what a sync leaves out is said (#5715)
JJasonsun (Discord #help, #5715): a skill over a sync's 64 MiB budget was
left out of the server's file while Settings > Sync said the sync worked,
so the other computer silently didn't have it.

A library skill from GitHub or another git repository whose folder is
still what was fetched (editState known and unedited, or Pending) now
goes as its source, as a plugin does (plugin_carry.go): CarriedSkill
gets Fetch, Hash and Commit, and the receiving computer fetches it
itself after Put lets go of the library lock (fetchSkills, through
updateSkills). Its files still go as far as the room the skills carried
as files leave, so a magpie from before this, which ignores Fetch,
writes the skill as it did.

On the receiving side (fromSourceHere): same version is kept; one
changed here is kept and, when the other computer has another version,
said as a problem ("Update replaces them"); another version or a skill
new here is fetched, unless the carried files are already the whole of
it. A skill that can't be fetched stays Pending, shows "Not fetched
yet" on its Library row, and is said as a problem until Update fetches
it (library.Unsettled drops a settled one).

A skill changed here, one from a folder, and one from a link holding a
token (whose link must not be synced) go as their files, as before.

What a sync still leaves out is said: state.LibraryLeft names skills
and plugins whose files were too big (or that this computer never got,
Skill.Missing, carried on as Left so a third computer isn't told it
has them), state.LibraryProblems what the last sync bringing the
library in couldn't put here. Status gives them as libraryLeft and
libraryProblems, and Settings > Sync shows them in red under the
status (askPreviewRows). Strings in every language.

Run at this head (rebased on origin/main 7b552eef3):
- gofmt -l: clean. go vet darwin, GOOS=linux, GOOS=windows: ok.
- mica: go test -count=1 -timeout 45m ./internal/library
  ./internal/davsync ./internal/backup ./internal/gui: ok.
- mote: node --test in Chromium and WebKit of sync-*, i18n-*, gui-zh-tw,
  gui-ja, gui-de, skill-*, library-items, library-all, library-plugins
  and the new sync-library-left (16/16 in both engines). Red, and red
  the same in a fresh origin/main 7b552eef3 worktree, each filed:
  skill-fold-all (#6667), skill-manual (#6668), skill-group-shared
  (#6687), skill-where's zh 900px under concurrency (#5859).
- The first package run at this head had TestSkillCarriedBySource and
  ...NotFetched red: InstallSkills took another test's cached probe of
  owner/repo (SKILL.md only). carrySource now forgets the probes before
  and after; ./internal/library ok again, once more with -shuffle=on.

Broken by hand, each red: bySource always false (5 library tests),
fetchSkills removed (library + davsync), changedHere removed, Missing
not carried on, Unsettled removed, Pending not set in Put, signedIn
check removed from Collect and from Put (TestSkillSignedInLinkGoesAsFiles),
leftOut returning nil, Fetch not excluded from leftOut.

Not tried: a real sync between two computers with a real >64 MiB skill
from GitHub; the tests use a local git repository as the source. A
restore (backup.Restore through the restore and plan paths) carries
skills by source too but doesn't record LibraryLeft/LibraryProblems
yet (said in sync-backup.md).
2026-11-18 03:05:47 +08:00

133 lines
4.5 KiB
Go

package davsync
import (
"context"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/library"
)
// #5715 (JJasonsun on Discord): a library past a sync's size limit is
// said, never a silent success. A skill from a git repository goes as its
// source, and the other computer fetches it itself: its size doesn't
// count, and one that can't be fetched there is said until Update fetches
// it. A skill of the user's own too big to go is named.
func TestSyncSkillsBySourceAndLeftOut(t *testing.T) {
git, err := exec.LookPath("git")
if err != nil {
t.Skip("git isn't installed")
}
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(t.TempDir(), "gitconfig"))
big := strings.Repeat("x", 17<<20) // past a file's 16 MiB
repo := t.TempDir()
work := filepath.Join(repo, "work")
run := func(dir string, args ...string) {
t.Helper()
cmd := exec.Command(git, append([]string{"-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "init.defaultBranch=main", "-c", "commit.gpgsign=false"}, args...)...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
run(repo, "init", "-q", "--bare", "skills.git")
run(repo, "init", "-q", work)
for p, body := range map[string]string{
"skills/pdf/SKILL.md": "---\nname: pdf\ndescription: PDFs\n---\n",
"skills/pdf/big.txt": big,
} {
p = filepath.Join(work, filepath.FromSlash(p))
os.MkdirAll(filepath.Dir(p), 0o755)
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
run(work, "add", "-A")
run(work, "commit", "-q", "-m", "skills")
run(work, "push", "-q", filepath.Join(repo, "skills.git"), "HEAD:refs/heads/main")
addr := "file://" + filepath.ToSlash(filepath.Join(repo, "skills.git"))
if !strings.HasPrefix(addr, "file:///") {
addr = "file:///" + strings.TrimPrefix(addr, "file://") // C:/… on Windows
}
withGit := func(c computer) {
t.Helper()
if err := os.Symlink(git, filepath.Join(string(c), "bin", "git")); err != nil {
t.Fatal(err)
}
}
f, srv := newFakeS3(t)
cfg := f.config(srv)
cfg.Keys, cfg.Agents = false, false
ctx := context.Background()
a, b := newComputer(t), newComputer(t)
// A: pdf from the repository, and a skill of its own too big to go
a.use(t)
withGit(a)
if _, err := library.InstallSkills(addr, []string{"skills/pdf"}, []string{"claude"}); err != nil {
t.Fatal(err)
}
if _, err := library.CreateSkill("mine", "my own", []string{"claude"}); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(library.Dir(), "skills", "mine", "big.txt"), []byte(big), 0o644); err != nil {
t.Fatal(err)
}
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
if v := Status(); v.Error != "" || !slices.Equal(v.LibraryLeft, []string{"mine"}) {
t.Fatalf("A's sync: error %q, left out %v, want mine said", v.Error, v.LibraryLeft)
}
// B, with no git: pdf can't be fetched, and that is said
b.use(t)
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
v := Status()
if v.Error != "" || len(v.LibraryProblems) != 1 || !strings.HasPrefix(v.LibraryProblems[0], "pdf: not fetched") {
t.Fatalf("B's sync: error %q, problems %q", v.Error, v.LibraryProblems)
}
// git here now: Update fetches it, the whole of it, and nothing is said
withGit(b)
if _, err := library.UpdateSkill("pdf", false); err != nil {
t.Fatal(err)
}
if got, err := os.ReadFile(filepath.Join(library.Dir(), "skills", "pdf", "big.txt")); err != nil || len(got) != len(big) {
t.Fatalf("pdf's big.txt on B: %d bytes, %v", len(got), err)
}
if v := Status(); len(v.LibraryProblems) != 0 {
t.Errorf("fetched, still said: %q", v.LibraryProblems)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
if v := Status(); v.Error != "" || len(v.LibraryProblems) != 0 || !slices.Equal(v.LibraryLeft, []string{"mine"}) {
t.Errorf("B's next sync: error %q, problems %q, left %v", v.Error, v.LibraryProblems, v.LibraryLeft)
}
// what B never had of mine isn't taken from A by B's sync
a.use(t)
if err := Now(ctx); err != nil {
t.Fatal(err)
}
if got, err := os.ReadFile(filepath.Join(library.Dir(), "skills", "mine", "big.txt")); err != nil || len(got) != len(big) {
t.Errorf("A's mine/big.txt after B's sync: %d bytes, %v", len(got), err)
}
if v := Status(); v.Error != "" || !slices.Equal(v.LibraryLeft, []string{"mine"}) {
t.Errorf("A's next sync: error %q, left %v", v.Error, v.LibraryLeft)
}
}