Files
magpie/internal/davsync/off_legacy_test.go
T
ggbdpq 4082f6d00e davsync: a part re-enabled on a state with no Off mark joins again
kupakupar (#3356): re-enabling the library (or an agent's models) on a
computer that synced with the part left out sent that computer's older
content up over the server's newer one. What #6428 fixed for states
since it writes — a part left out is marked in Off, and one synced again
joins like a first sync — did not reach the states older magpies wrote:
they carry no Off mark, so the join saw a real hash against the
nothing-hash the last sync left and took it as a change here, against a
server it had already seen.

A part that syncs now, whose baseline is the nothing-hash a left-out
part carries and that holds something of its own here, joins again the
same way: each side's own counts as changed there, the newer stays. A
state since #6428 already names such parts in Off and takes the old
path; a first sync has no baseline to compare and is the join already.

Run (worktree at base a8fa771a6):
- go test ./internal/davsync -run TestSyncPartTurnedBackOn: ok, the
  marked and the unmarked state both; broken by hand: the unmarked
  state's fallback loop taken out puts the overwrite back
  (TestSyncPartTurnedBackOnWithoutOffMark)
- go test ./internal/davsync -count=1 -timeout 25m: ok
- gofmt -l: clean; go vet ./internal/davsync: clean

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-14 13:08:40 +08:00

100 lines
2.8 KiB
Go

package davsync
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/settings"
)
// dateLibrary puts the library's files at at: changed("library") reads
// their times from the kernel's clock.
func dateLibrary(t *testing.T, at time.Time) {
t.Helper()
os.Chtimes(filepath.Join(settings.Dir(), "library.json"), at, at)
os.Chtimes(library.Dir(), at, at)
filepath.WalkDir(library.Dir(), func(p string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
os.Chtimes(p, at, at)
return nil
})
}
// #3356: a state written before st.Off was recorded (a magpie upgraded
// from before #6428): the last sync left the library out but wrote no Off
// mark, so re-enabling the library must still join it like a first sync —
// the computer's older content gives way to the server's newer one, never
// the other way round.
func TestSyncPartTurnedBackOnWithoutOffMark(t *testing.T) {
fake, cfg := scopeServer(t)
// b has older library content and syncs with the library left out:
// its first sync starts the folder, without the library
b := newComputer(t)
b.use(t)
saySharedText(t, "older")
dateLibrary(t, time.Now().Add(-time.Hour))
no := false
without := cfg
without.Library = &no
Configure(without)
syncOK(t)
// a joins with newer library content: the library unset on the server
// lets a's go up (#5642)
a := newComputer(t)
a.use(t)
saySharedText(t, "newer")
Configure(cfg)
syncOK(t)
if got := serverBundle(t, fake).Library; got == nil || got.Texts["default"] != "newer" {
t.Fatalf("a's library on the server: %+v", got)
}
// b syncs again, still without the library: the state records the
// server's library and nothing of it here
b.use(t)
syncOK(t)
// the state as a magpie before Off was recorded left it: no Off mark
p := filepath.Join(settings.Dir(), "sync-state.json")
sb, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
var st map[string]any
if err := json.Unmarshal(sb, &st); err != nil {
t.Fatal(err)
}
delete(st, "off")
ob, err := json.MarshalIndent(st, "", " ")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, ob, 0o600); err != nil {
t.Fatal(err)
}
// b enables the library and syncs: the server's newer library comes
// in, b's older text replaced and kept aside — never up over it
Configure(cfg)
v := syncOK(t)
if got := serverBundle(t, fake).Library; got == nil || got.Texts["default"] != "newer" {
t.Fatalf("b's older library went up over the server's: %+v", got)
}
if got := sharedText(); got != "newer" {
t.Fatalf("b's library after the sync: %q", got)
}
if v.Notice == nil || !slices.Equal(v.Notice.Here, []string{"library"}) {
t.Fatalf("no notice about b's replaced library: %+v", v.Notice)
}
}