jinpwng (#6428, on X) syncs two computers and wants only Library and memory to go between them: each keeps its own providers, settings and models. They also want to see what a sync would change before it applies, and to roll the last one back. What syncs: providers, settings and profiles now have switches like agents' models, library and memory (Config.Providers/Settings/Profiles, *bool, nil = synced as before, so an existing setup syncs exactly what it did). The sync form shows Providers (and its API keys), Settings and Agents' models up front; Profiles, usage and sign-ins sit under Advanced, closed unless one is set away from its default. The form sends a part's choice only when it is or was off. A part left out stays as it is here and on the server; turning it back on joins it like a first sync (st.Off, rejoin). The sync's line says which parts each computer keeps ("Stays as it is on each computer: ..."). The setup text and the CLI carry it: magpie <webdav|s3|github> set providers=no settings=no profiles=no. Preview: davsync.Preview (plan.go) says, per part, what syncing now would bring in, send up, or leave; library items and memory files by name, sign-ins and plugins gone. Preview... in Settings > Sync shows it, and Sync now from there runs SyncPreviewed(token): if the server or this computer changed since, it says so and shows the new plan instead of syncing something else. CLI: magpie <kind> preview. Undo: a sync that brings parts in records them (st.Applied) and keeps a sealed copy of this computer first (keepSealed, as Restore does). Undo the last sync puts those parts back from that copy, on this computer only (st.Local, so the next sync sends it up rather than fetching the server's again). If one of them changed here since, it asks first (409 changed, Undo anyway). CLI: magpie <kind> undo-sync [anyway]. Restore's own Undo is unchanged. docs/subsystems/sync-backup.md and the site's sync docs (en, zh, ja) have a section on it. i18n: zh, zh-TW, ja, de. Run (worktree at base48e394146): - go test -count=1 . ./internal/davsync ./internal/cli: ok (mote). Broken by hand, each went red: syncs() forced true (scope tests), rejoin's again forced false, UndoSync without the st.Local update. - go test -count=1 ./internal/gui: TestCoreScriptBudget was over by 85 bytes; the sync-left line moved into syncfiles.js, now ok. - go vet ./internal/gui, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. gofmt -l: nothing. - node --check app.js, syncfiles.js, sync-scope.test.cjs: ok. - GUI (mica), every test file that opens Settings > Sync or calls davsync (22 files) plus i18n-keys, Chromium and WebKit, 1100 and 420px, en/zh/zh-TW/ja/de: 453 of 454 pass. The red is s3-sync webkit en "picking S3 must not scroll the page" (0 !== 98), the same on origin/main52c9880cb(#3413, #2448). An earlier run also had library-new-skill chromium en 420 red, the same on main (#6539). sync-scope.test.cjs is new: on the old code there is no Providers switch, Preview or Undo, so it fails at once. - Rebased on origin/main7eecbcc56(app.js and i18n.js changed there): TestCoreScriptBudget, go test . ./internal/davsync, node --check, and i18n-keys, sync-scope, webdav-settings-refresh (both engines): ok. - The full go test ./... was started through mote's suite lock when this was pushed; its result goes on #6428. Not tried: a real WebDAV, S3 or GitHub server (the tests use fakes); the real app on Linux WebKitGTK. "Save and sync" from the form, after a preview, syncs without the preview's token check.
241 lines
7.1 KiB
Go
241 lines
7.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
"github.com/yetone/magpie/internal/davsync"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// webdavHome is a machine of its own: no agent on it, no gateway at the
|
|
// address, and nothing on stdin.
|
|
func webdavHome(t *testing.T) {
|
|
t.Helper()
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("PATH", "")
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
t.Setenv("APPDATA", "")
|
|
t.Setenv("LOCALAPPDATA", "")
|
|
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
|
|
t.Setenv("CODEX_HOME", filepath.Join(home, ".codex"))
|
|
t.Setenv("MAGPIE_ADDR", "127.0.0.1:1")
|
|
piped(t)
|
|
}
|
|
|
|
// davServer is a WebDAV server for user me with a password of its own,
|
|
// keeping files in memory, and every password it was sent.
|
|
type davServer struct {
|
|
mu sync.Mutex
|
|
pass string
|
|
files map[string][]byte
|
|
sent []string
|
|
url string
|
|
}
|
|
|
|
func newDAVServer(t *testing.T, pass string) *davServer {
|
|
t.Helper()
|
|
d := &davServer{pass: pass, files: map[string][]byte{}}
|
|
srv := httptest.NewServer(d)
|
|
t.Cleanup(srv.Close)
|
|
d.url = srv.URL + "/dav/"
|
|
return d
|
|
}
|
|
|
|
func (d *davServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
u, p, ok := r.BasicAuth()
|
|
if ok {
|
|
d.sent = append(d.sent, p)
|
|
}
|
|
if !ok || u != "me" || p != d.pass {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
b, ok := d.files[r.URL.Path]
|
|
if !ok {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
w.Write(b)
|
|
case http.MethodPut:
|
|
d.files[r.URL.Path], _ = io.ReadAll(r.Body)
|
|
w.WriteHeader(http.StatusCreated)
|
|
case "MKCOL":
|
|
w.WriteHeader(http.StatusCreated)
|
|
default:
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func (d *davServer) file() []byte {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
return d.files["/dav/magpie/magpie.magpie-backup"]
|
|
}
|
|
|
|
func (d *davServer) passwords() []string {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
return slices.Clone(d.sent)
|
|
}
|
|
|
|
// piped is what stdin holds for the secrets asked, a line each: a pipe, so
|
|
// they are read as a script's are, never from the terminal the test is in.
|
|
func piped(t *testing.T, lines ...string) {
|
|
t.Helper()
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(lines) > 0 {
|
|
w.WriteString(strings.Join(lines, "\n") + "\n")
|
|
}
|
|
w.Close()
|
|
old := os.Stdin
|
|
os.Stdin = r
|
|
t.Cleanup(func() {
|
|
os.Stdin = old
|
|
r.Close()
|
|
})
|
|
}
|
|
|
|
func TestWebdavCmd(t *testing.T) {
|
|
webdavHome(t)
|
|
first := newDAVServer(t, "pw")
|
|
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
|
|
|
|
// off: nothing to change or sync
|
|
if err := webdavCmd([]string{"set", "agents=no"}); err == nil || !strings.Contains(err.Error(), "is off") {
|
|
t.Fatalf("set while off: %v", err)
|
|
}
|
|
if err := webdavCmd([]string{"now"}); err == nil || !strings.Contains(err.Error(), "is off") {
|
|
t.Fatalf("now while off: %v", err)
|
|
}
|
|
if err := webdavCmd(nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// a wrong address is said before any secret is asked for
|
|
if err := webdavCmd([]string{"on", "dav.example.com", "user=me"}); err == nil || !strings.Contains(err.Error(), "not a WebDAV address") {
|
|
t.Fatalf("an address with no https://: %v", err)
|
|
}
|
|
|
|
// on: the password and the passphrase asked for, and a sync at once
|
|
piped(t, "pw", "correct horse")
|
|
if err := webdavCmd([]string{"on", first.url, "user=me", "library=no"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, ok := davsync.Load()
|
|
if !ok || c.URL != first.url || c.User != "me" || c.Password != "pw" || c.Passphrase != "correct horse" ||
|
|
!c.Keys || !c.Agents || c.Library == nil || *c.Library {
|
|
t.Fatalf("on: %v %+v", ok, c)
|
|
}
|
|
if up := first.file(); len(up) == 0 || strings.Contains(string(up), "deepseek") {
|
|
t.Fatalf("on the server: %q", up)
|
|
}
|
|
if v := davsync.Status(); v.Error != "" || v.Last.IsZero() {
|
|
t.Fatalf("status: %+v", v)
|
|
}
|
|
|
|
// set: what is given changes, the rest (secrets too) stays
|
|
if err := webdavCmd([]string{"set", "agents=no", "keys=no"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, _ := davsync.Load(); c.Agents || c.Keys || c.Password != "pw" || c.Passphrase != "correct horse" || c.URL != first.url {
|
|
t.Fatalf("set: %+v", c)
|
|
}
|
|
// providers and settings can stay as they are on each computer (#6428)
|
|
if err := webdavCmd([]string{"set", "providers=no", "settings=no"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, _ := davsync.Load(); c.Providers == nil || *c.Providers || c.Settings == nil || *c.Settings || c.Profiles != nil {
|
|
t.Fatalf("set providers=no settings=no: %+v", c)
|
|
}
|
|
if err := webdavCmd([]string{"set", "providers=yes", "settings=yes"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, _ := davsync.Load(); c.Providers != nil && !*c.Providers {
|
|
t.Fatalf("set providers=yes: %+v", c)
|
|
}
|
|
// an address with = in it, bare: the same server, so the password stays
|
|
if err := webdavCmd([]string{"set", first.url + "?k=v"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c, _ := davsync.Load(); c.URL != first.url+"?k=v" || c.Password != "pw" {
|
|
t.Fatalf("an address with = in it: %+v", c)
|
|
}
|
|
for _, bad := range [][]string{{"set", "passphrase=on-the-command-line"}, {"set", "password=on-the-command-line"}, {"set", "keys=maybe"}, {"set", "colour=red"}, {"set", first.url, first.url}} {
|
|
if err := webdavCmd(bad); err == nil {
|
|
t.Errorf("%v: no error", bad)
|
|
}
|
|
}
|
|
|
|
// password= asks for a new one; a wrong one shows at once, and it stays
|
|
// on to be put right
|
|
piped(t, "nope")
|
|
if err := webdavCmd([]string{"set", "password="}); err == nil || !strings.Contains(err.Error(), "401") {
|
|
t.Fatalf("wrong password: %v", err)
|
|
}
|
|
if c, ok := davsync.Load(); !ok || c.Password != "nope" {
|
|
t.Fatalf("after a failed sync: %v %+v", ok, c)
|
|
}
|
|
piped(t, "pw")
|
|
if err := webdavCmd([]string{"set", "password="}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// another server: the password saved is not sent there, and one is
|
|
// asked for
|
|
second := newDAVServer(t, "pw2")
|
|
piped(t, "pw2")
|
|
if err := webdavCmd([]string{"set", second.url}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if slices.Contains(second.passwords(), "pw") {
|
|
t.Fatal("the first server's password went to the second")
|
|
}
|
|
if c, _ := davsync.Load(); c.URL != second.url || c.Password != "pw2" || c.Passphrase != "correct horse" {
|
|
t.Fatalf("moved: %+v", c)
|
|
}
|
|
|
|
// user= alone: no sign-in, nothing asked for, no password sent
|
|
sent := len(second.passwords())
|
|
if err := webdavCmd([]string{"set", "user="}); err == nil || !strings.Contains(err.Error(), "401") {
|
|
t.Fatalf("no sign-in, on a server that asks for one: %v", err)
|
|
}
|
|
if c, _ := davsync.Load(); c.User != "" || c.Password != "" {
|
|
t.Fatalf("user= alone: %+v", c)
|
|
}
|
|
if n := len(second.passwords()); n != sent {
|
|
t.Fatalf("%d passwords sent with no user", n-sent)
|
|
}
|
|
|
|
if err := webdavCmd([]string{"off"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := davsync.Load(); ok {
|
|
t.Fatal("still on after off")
|
|
}
|
|
if len(second.file()) == 0 {
|
|
t.Fatal("off took the file off the server")
|
|
}
|
|
}
|