Ramon317317 asked for a main profile: one computer the others take their
setup from, so they never drift apart. Sync merged both ways only;
Restore is a one-time pull and an agent's own fields cover agents only.
A computer can now be set to follow the server (Settings -> Sync -> This
computer: Both ways / Follows the server; /api/davsync/follow-server;
`magpie webdav|s3|github follow on|off`). It is chosen per computer and
kept in sync.json (Config.FollowServer, kept by Configure like AutoEvery).
A following computer:
- takes every changed part from the file, whichever side is newer;
- never writes the file: no push, no merged sign-in, no rebuild of a
damaged file;
- keeps a change made on it in the sync folder, as a join does, and the
notice says it was replaced because this computer follows the server
(Notice.Followed; GUI and CLI);
- still keeps an unset part on the server from emptying its own, and own
agent fields and agents only here;
- refuses to follow a server with no file yet, sending nothing.
The computer followed stays two-way; nothing changes for anyone who
doesn't pick it. Usage sharing is unchanged. docs/subsystems/sync-backup.md
says so.
app.js stays inside the core script budget (TestCoreScriptBudget) with
no raise: renderSync's comments are cut down to pay for the row.
Run (mote, at this head):
- go build ./...; go vet ./... (darwin), GOOS=linux go vet -tags nogui
./..., GOOS=windows go vet ./...; gofmt -l clean.
- go test ./internal/davsync ./internal/cli ./internal/gui: ok (cli
with the route guard fix committed beside this one).
TestFollowServer (empty server, join, own change replaced and kept,
both changed, unchanged sync, Configure keeps it, back to both ways)
and TestFollowServerSendsNoSignIn are new.
- GUI (node --test, Chromium and WebKit): sync-follow-server (new; en and
zh, 900px and 400px: name, sub, picked segment, the post, the notice,
no scroll, no overflow, no select, re-pick posts nothing), plus
sync-auto-restore, sync-note-show, sync-own-fields, sync-not-backup,
sync-other-kind, sync-signins, sync-spin, i18n-keys, i18n-en-no-cjk,
gui-zh-tw, gui-ja, gui-de: all pass.
Broken by hand:
- push guard removed: TestFollowServerSendsNoSignIn red ("sent its
sign-in: 1 puts").
- follow branch off: TestFollowServer red ("b's own change stayed:
[deepseek=k1 kimi=kb2]").
- the row's click not posting: sync-follow-server red, 8 of 8.
Not tried: a real WebDAV, S3 or GitHub server (fake servers only); two
real computers; Windows run of the tests (vet only). The full suite starts
after the tag.
171 lines
5.8 KiB
Go
171 lines
5.8 KiB
Go
package davsync
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/backup"
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// A computer set to follow the server takes its setup and sends none of
|
|
// its own (Ramon317317 on X: one computer the others all take theirs
|
|
// from, so they never drift apart). A change made on it is replaced on
|
|
// the next sync, kept in the sync folder and said; a change on both sides
|
|
// is the server's whichever is newer. The computer followed syncs both
|
|
// ways, and one set back syncs both ways again.
|
|
func TestFollowServer(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, Agents: true}
|
|
now := func(t *testing.T) View {
|
|
t.Helper()
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return Status()
|
|
}
|
|
main, b := newComputer(t), newComputer(t)
|
|
|
|
// following an empty server: an error, and nothing goes up
|
|
b.use(t)
|
|
provider.Save(provider.Provider{ID: "mine", Name: "Mine", Chat: "https://x/v1", Key: "kb"})
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetFollowServer(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Now(ctx); !errors.Is(err, errNothingToFollow) || fake.puts != 0 {
|
|
t.Fatalf("following an empty server: %v, %d puts", err, fake.puts)
|
|
}
|
|
|
|
main.use(t)
|
|
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
|
|
settings.Save(settings.Settings{Theme: "dark"})
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v := now(t); v.FollowServer || fake.puts != 1 {
|
|
t.Fatalf("main: %+v, %d puts", v, fake.puts)
|
|
}
|
|
|
|
// b joins following: the server's setup comes in, b's kept aside
|
|
b.use(t)
|
|
if v := now(t); !v.FollowServer || v.Notice == nil || !slices.Contains(v.Notice.Here, "providers") {
|
|
t.Fatalf("b joined: %+v", v)
|
|
}
|
|
if got := ids(); !slices.Equal(got, []string{"deepseek=k1"}) || settings.Load().Theme != "dark" {
|
|
t.Fatalf("b after joining: %v %q", got, settings.Load().Theme)
|
|
}
|
|
Dismiss()
|
|
|
|
// a change made on b: replaced, never sent, kept and said
|
|
puts := fake.puts
|
|
provider.Save(provider.Provider{ID: "kimi", Name: "Kimi", Chat: "https://api.moonshot.cn/v1", Key: "kb2"})
|
|
v := now(t)
|
|
if fake.puts != puts {
|
|
t.Fatalf("b, following, wrote the server %d times", fake.puts-puts)
|
|
}
|
|
if got := ids(); !slices.Equal(got, []string{"deepseek=k1"}) {
|
|
t.Fatalf("b's own change stayed: %v", got)
|
|
}
|
|
if v.Notice == nil || !v.Notice.Followed || !slices.Equal(v.Notice.Here, []string{"providers"}) {
|
|
t.Fatalf("b's notice: %+v", v.Notice)
|
|
}
|
|
kept, _ := filepath.Glob(filepath.Join(settings.Dir(), "sync", "*-this-computer"+backup.Ext))
|
|
found := false
|
|
for _, k := range kept {
|
|
if old, err := backup.Open(must(os.ReadFile(k)), "correct horse"); err == nil && slices.ContainsFunc(old.Providers, func(p provider.Provider) bool { return p.ID == "kimi" }) {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("b's change isn't kept: %v", kept)
|
|
}
|
|
Dismiss()
|
|
|
|
// both change the settings, b's the newer: the server's still comes in
|
|
main.use(t)
|
|
settings.Save(settings.Settings{Theme: "light"})
|
|
now(t)
|
|
b.use(t)
|
|
settings.Save(settings.Settings{Theme: "system", Lang: "zh"})
|
|
puts = fake.puts
|
|
if v := now(t); fake.puts != puts || settings.Load().Theme != "light" || settings.Load().Lang == "zh" || v.Notice == nil || !v.Notice.Followed {
|
|
t.Fatalf("both changed: %q %q, %d puts, %+v", settings.Load().Theme, settings.Load().Lang, fake.puts-puts, v.Notice)
|
|
}
|
|
// nothing changed since: nothing brought or written
|
|
Dismiss()
|
|
if v := now(t); fake.puts != puts || v.Notice != nil {
|
|
t.Fatalf("an unchanged sync: %d puts, %+v", fake.puts-puts, v.Notice)
|
|
}
|
|
|
|
// saving the server's form keeps the choice; set back, b syncs both ways
|
|
if err := Configure(cfg); err != nil || !Status().FollowServer {
|
|
t.Fatalf("the form dropped following: %v", err)
|
|
}
|
|
if err := SetFollowServer(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
provider.Save(provider.Provider{ID: "kimi", Name: "Kimi", Chat: "https://api.moonshot.cn/v1", Key: "kb2"})
|
|
now(t)
|
|
main.use(t)
|
|
now(t)
|
|
if got := ids(); !slices.Equal(got, []string{"deepseek=k1", "kimi=kb2"}) {
|
|
t.Fatalf("main after b synced both ways again: %v", got)
|
|
}
|
|
}
|
|
|
|
// With sign-ins synced, one made on a computer following the server is
|
|
// still not sent: the merge of sign-ins would put it in the file, and the
|
|
// file is the followed computer's to write.
|
|
func TestFollowServerSendsNoSignIn(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", SignIns: true}
|
|
main, b := newComputer(t), newComputer(t)
|
|
main.use(t)
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.use(t)
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetFollowServer(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := plugin.EditAuth(func(m map[string]json.RawMessage) (bool, error) {
|
|
m["kiro"] = json.RawMessage(`{"type":"oauth","access":"kiro-b","refresh":"kr","expires":2000,"accountId":"k@x.com"}`)
|
|
return true, nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
puts := fake.puts
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fake.puts != puts {
|
|
t.Fatalf("b, following, sent its sign-in: %d puts", fake.puts-puts)
|
|
}
|
|
}
|