package client import "testing" func TestDecideTable(t *testing.T) { // A database this client has synced at version 2, digest "a". base := situation{LocalExists: true, LocalSHA: "a", SyncedSHA: "a", SyncedVersion: 2, RemoteSHA: "a", RemoteVersion: 2, CanWrite: true, Strategy: "server-wins"} with := func(f func(*situation)) situation { s := base f(&s) return s } for _, tc := range []struct { name string s situation want action }{ {"settled", base, actNothing}, {"local save", with(func(s *situation) { s.LocalSHA = "b" }), actPush}, {"remote update", with(func(s *situation) { s.RemoteSHA, s.RemoteVersion = "c", 3 }), actPull}, {"missing local file", with(func(s *situation) { s.LocalExists = false }), actPull}, {"missing local file, empty database", situation{RemoteVersion: 0}, actNothing}, {"adopt a local file into an empty database", situation{LocalExists: true, LocalSHA: "a", CanWrite: true, Strategy: "server-wins"}, actPush}, {"both changed, server wins", with(func(s *situation) { s.LocalSHA, s.RemoteSHA, s.RemoteVersion = "b", "c", 3 }), actServerWins}, {"both changed, client wins", with(func(s *situation) { s.LocalSHA, s.RemoteSHA, s.RemoteVersion, s.Strategy = "b", "c", 3, "client-wins" }), actForcePush}, {"both changed, merge", with(func(s *situation) { s.LocalSHA, s.RemoteSHA, s.RemoteVersion, s.Strategy = "b", "c", 3, "merge" }), actPushForMerge}, {"local save on a merge vault with no remote change", with(func(s *situation) { s.LocalSHA, s.Strategy = "b", "merge" }), actPush}, {"both changed, manual", with(func(s *situation) { s.LocalSHA, s.RemoteSHA, s.RemoteVersion, s.Strategy = "b", "c", 3, "manual" }), actManual}, {"manual conflict already reported", with(func(s *situation) { s.LocalSHA, s.RemoteSHA, s.RemoteVersion, s.Strategy = "b", "c", 3, "manual" s.ConflictSeen = true }), actNothing}, {"local save without write access", with(func(s *situation) { s.LocalSHA, s.CanWrite = "b", false }), actRestore}, {"both changed without write access", with(func(s *situation) { s.LocalSHA, s.RemoteSHA, s.RemoteVersion, s.CanWrite = "b", "c", 3, false }), actRestore}, {"version moved but content identical", with(func(s *situation) { s.RemoteVersion = 3 }), actPull}, } { if got := decide(tc.s); got != tc.want { t.Errorf("%s: decide = %s, want %s", tc.name, got, tc.want) } } }