Files
magpie/internal/davsync/this_device_test.go
T
yetone 05cadcafd4 sync: a provider can stay only on this device (treydtw on X)
Providers sync between computers (WebDAV, S3, GitHub), so a model served
from one machine's localhost reached the others, where it can't be
reached. A provider now has "Only on this device": sync neither sends it
out nor lets a copy from another computer replace or delete it.

Provider.ThisDevice is set from the provider row's right-click menu
(Only on this device, a check when on) or `magpie provider this-device
<id> yes|no`, over POST /api/provider/thisdevice. davsync's collect drops
those providers and their order entries (provider.SyncedOnly); Mirror
skips an incoming copy of one and keeps the local ones; Restore keeps a
local mark. A normal editor save keeps the mark. File backups still carry
the provider with its mark.

Default is off for every provider, localhost included. A 127.0.0.1
provider is also what another computer runs for its own copy of the same
local server, and default-on would remove those copies on their next
sync; a LAN address is reachable from the other computers. Turning it on
at one computer removes the synced copy at the others on their next sync.
docs/subsystems/sync-backup.md says so.

The menu item gives no toast: its check mark is the answer, which also
keeps the core script budget at +151 bytes (1,263,700 to
1,263,851).

Run, on mica: new davsync TestProviderOnThisDeviceStaysOnIt (two
computers over a fake DAV) and this-device.test.cjs (chromium and
webkit, en zh zh-TW ja de, widths 1100 and 420); provider-find updated
for the new menu entry; davsync, provider, backup, cli, gui Go packages
ok except TestCoreScriptBudget, then fixed; vet on darwin, windows and
linux nogui clean. Broken by hand: SyncedOnly made a no-op turns the
davsync test red ("a sends its own model out"), and the Mirror skip
removed turns it red ("b after a's model synced"). GUI files that fail
in a loaded run (claude-quota-onboarding, provider-find, skill-fold-all,
usage-agents-hide, usage-cards-hide, usage-hidden-way-back) were run
alone on origin/main 5e933e153: usage-hidden-way-back (#6295) and
usage-agents-hide (#3414) are red there too, the rest pass; this branch
gives the same result. gui-de and gui-ja "has every zh string" are red on
5e933e153 as well.

Not tried: a real WebDAV/S3/GitHub server between two real computers;
the CLI command has no test of its own.
2026-11-12 19:31:11 +08:00

87 lines
2.8 KiB
Go

package davsync
import (
"context"
"net/http/httptest"
"slices"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A provider kept on one computer (Provider.ThisDevice) is not sent out,
// and what another computer's sync brings neither replaces nor removes it
// (treydtw on X: a local model that exists only on this machine, synced to
// the others where it is unreachable).
func TestProviderOnThisDeviceStaysOnIt(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}
srv := httptest.NewServer(fake)
defer srv.Close()
ctx := context.Background()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "correct horse", Keys: true}
a, b := newComputer(t), newComputer(t)
sync := func(c computer) {
t.Helper()
c.use(t)
if err := Now(ctx); err != nil {
t.Fatal(err)
}
}
local := func(id, key string) provider.Provider {
return provider.Provider{ID: id, Name: id, Chat: "http://127.0.0.1:11434/v1", Key: key, ThisDevice: true}
}
a.use(t)
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
provider.Save(local("ollama", "ka"))
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
sync(a)
if out, err := collect(cfg); err != nil || slices.ContainsFunc(out.Providers, func(p provider.Provider) bool { return p.ID == "ollama" }) || slices.Contains(out.Order, "ollama") {
t.Fatalf("a sends its own model out: %v, %d providers, order %v", err, len(out.Providers), out.Order)
}
// b has a model of its own under the same id as a's, and another
b.use(t)
provider.Save(local("ollama", "kb"))
provider.Save(local("lmstudio", "kb2"))
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
sync(b)
if got := ids(); !slices.Equal(got, []string{"deepseek=k1", "lmstudio=kb2", "ollama=kb"}) {
t.Fatalf("b after joining: %v", got)
}
// a drops DeepSeek: it goes from b, and b's own stay
a.use(t)
if err := provider.Delete("deepseek"); err != nil {
t.Fatal(err)
}
sync(a)
sync(b)
if got := ids(); !slices.Equal(got, []string{"lmstudio=kb2", "ollama=kb"}) {
t.Fatalf("b after a dropped DeepSeek: %v", got)
}
sync(a)
if got := ids(); !slices.Equal(got, []string{"ollama=ka"}) {
t.Fatalf("a after b's syncs: %v", got)
}
// a lets its model sync: it goes out, but b keeps its own under that id
if err := provider.SetThisDevice("ollama", false); err != nil {
t.Fatal(err)
}
sync(a)
sync(b)
if got := ids(); !slices.Equal(got, []string{"lmstudio=kb2", "ollama=kb"}) {
t.Fatalf("b after a's model synced: %v", got)
}
// and a model another computer sends out does not come to one kept here
b.use(t)
if ps, _ := provider.Stored(); !slices.ContainsFunc(ps, func(p provider.Provider) bool { return p.ID == "ollama" && p.ThisDevice }) {
t.Fatal("b's own ollama lost its mark")
}
}