Collect read settings with settings.Load, which stands the defaults in for a file that is there but can't be read, or doesn't parse with no good .bak. A sync then pushed those defaults as "changed here", and every other computer brought them in over its own theme, language, text size, allow list and per-model settings; an export carried them too. Collect now reads through settings.LoadChecked and stops with its error, as it does for providers.json (9c54a560). The sync ends before anything is sealed or pushed, and its status shows the error; the CLI and the GUI's export show it. A missing or empty file is unchanged. Sibling: library.Collect read any failed stat of library.json as "no library"; only a missing file is that now, any other error stops the backup. plugins.json (provider.Carried -> plugin.list) still reads as no plugins when unreadable; bringing that in never removes a plugin, so it is filed separately rather than changed here. What was run: - TestCollectStopsOnUnreadableSettings (new, internal/backup) and TestSyncStopsOnUnreadableSettings (new, internal/davsync: two computers on a fake WebDAV, a's file cut short with no .bak; a's sync errs with no PUT, the status says so, the server keeps a's settings, b keeps its own). Both FAIL on the old backup.go ("Collect read a settings.json that can't be read as settings: <nil> &{Theme:system ..." and "a synced a broken settings.json") and PASS on the new, on macOS and on the Windows box (cross-compiled binaries, sandbox home). - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean. - go test -p 4 -timeout 40m ./... on macOS (origin/main69d7ee33aplus these three commits, load average 15-27 from other suites): every package ok but internal/gateway, where TestMaxRPMTooLongIs429With RetryAfter got 200 (the window had passed under load; #1791, open, gateway untouched here). -run TestMaxRPM -count=3 passes on this tree. - After rebasing on origin/main226222118(which touched internal/library): go vet darwin, GOOS=linux -tags nogui and GOOS=windows clean; go test ./internal/update ./internal/pluginrt ./internal/backup ./internal/davsync ./internal/library ./internal/settings ok (library under -timeout 40m, 550 s at load average ~60). Not tried: a settings.json held open by another Windows program, or a real WebDAV/S3/GitHub server.
156 lines
4.7 KiB
Go
156 lines
4.7 KiB
Go
package davsync
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"net/http/httptest"
|
|
"os"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/backup"
|
|
"github.com/yetone/magpie/internal/lastgood"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// A providers.json that can't be read is never pushed as no providers: a
|
|
// sync stops on it, the server keeps the providers, and another computer
|
|
// mirroring the server keeps them too.
|
|
func TestSyncStopsOnUnreadableProviders(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)
|
|
b.use(t)
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a.use(t)
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.use(t)
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ids(); !slices.Equal(got, []string{"deepseek=k1"}) {
|
|
t.Fatalf("b's providers: %v", got)
|
|
}
|
|
|
|
// a's file is cut short
|
|
a.use(t)
|
|
whole, err := os.ReadFile(provider.Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
broken := whole[:len(whole)/2]
|
|
if err := os.WriteFile(provider.Path(), broken, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
puts := fake.puts
|
|
if err := Now(ctx); !errors.Is(err, provider.ErrUnreadable) {
|
|
t.Fatalf("a synced a broken providers.json: %v", err)
|
|
}
|
|
if fake.puts != puts {
|
|
t.Fatal("a pushed a broken providers.json")
|
|
}
|
|
if after, _ := os.ReadFile(provider.Path()); !bytes.Equal(after, broken) {
|
|
t.Fatal("a's broken providers.json was written over")
|
|
}
|
|
if _, err := backup.Collect(true, "test"); !errors.Is(err, provider.ErrUnreadable) {
|
|
t.Fatalf("a backup of a broken providers.json: %v", err)
|
|
}
|
|
remote, err := backup.Open(fake.files["/dav/magpie/magpie.magpie-backup"], "correct horse")
|
|
if err != nil || len(remote.Providers) != 1 {
|
|
t.Fatalf("on the server: %v %+v", err, remote.Providers)
|
|
}
|
|
b.use(t)
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ids(); !slices.Equal(got, []string{"deepseek=k1"}) {
|
|
t.Fatalf("b lost its providers to a's broken file: %v", got)
|
|
}
|
|
}
|
|
|
|
// A settings.json that can't be read is never pushed as the defaults: a
|
|
// sync stops on it and says so, the server keeps the settings, and another
|
|
// computer mirroring the server keeps its own (#2269).
|
|
func TestSyncStopsOnUnreadableSettings(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)
|
|
b.use(t)
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a.use(t)
|
|
if err := settings.Save(settings.Settings{Theme: "dark", Lang: "ja", TextSize: 125}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Configure(cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.use(t)
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := settings.Load(); s.Theme != "dark" || s.Lang != "ja" {
|
|
t.Fatalf("b's settings: %s %s", s.Theme, s.Lang)
|
|
}
|
|
|
|
// a's file is cut short, with no last good generation to fall back to
|
|
a.use(t)
|
|
whole, err := os.ReadFile(settings.Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
broken := whole[:len(whole)/2]
|
|
if err := os.WriteFile(settings.Path(), broken, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Remove(lastgood.Bak(settings.Path()))
|
|
puts := fake.puts
|
|
if err := Now(ctx); err == nil {
|
|
t.Fatal("a synced a broken settings.json")
|
|
}
|
|
if fake.puts != puts {
|
|
t.Fatal("a pushed a broken settings.json")
|
|
}
|
|
if after, _ := os.ReadFile(settings.Path()); !bytes.Equal(after, broken) {
|
|
t.Fatal("a's broken settings.json was written over")
|
|
}
|
|
if st := Status(); st.Error == "" {
|
|
t.Fatal("a's sync status doesn't say it stopped")
|
|
}
|
|
remote, err := backup.Open(fake.files["/dav/magpie/magpie.magpie-backup"], "correct horse")
|
|
if err != nil || remote.Settings == nil || remote.Settings.Theme != "dark" {
|
|
t.Fatalf("on the server: %v %+v", err, remote.Settings)
|
|
}
|
|
b.use(t)
|
|
if err := Now(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := settings.Load(); s.Theme != "dark" || s.Lang != "ja" || s.TextSize != 125 {
|
|
t.Fatalf("b lost its settings to a's broken file: %s %s %d", s.Theme, s.Lang, s.TextSize)
|
|
}
|
|
}
|