MCPcopy Index your code
hub / github.com/dnote/dnote / TestSaveServerState

Function TestSaveServerState

pkg/cli/cmd/sync/sync_test.go:1795–1922  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1793}
1794
1795func TestSaveServerState(t *testing.T) {
1796 t.Run("with data received", func(t *testing.T) {
1797 // set up
1798 db := database.InitTestMemoryDB(t)
1799 testutils.LoginDB(t, db)
1800
1801 database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742))
1802 database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 8)
1803
1804 // execute
1805 tx, err := db.Begin()
1806 if err != nil {
1807 t.Fatal(errors.Wrap(err, "beginning a transaction").Error())
1808 }
1809
1810 serverTime := int64(1541108743)
1811 serverMaxUSN := 100
1812 userMaxUSN := 100
1813
1814 err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN)
1815 if err != nil {
1816 tx.Rollback()
1817 t.Fatal(errors.Wrap(err, "executing").Error())
1818 }
1819
1820 tx.Commit()
1821
1822 // test
1823 var lastSyncedAt int64
1824 var lastMaxUSN int
1825
1826 database.MustScan(t, "getting system value",
1827 db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt)
1828 database.MustScan(t, "getting system value",
1829 db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN)
1830
1831 assert.Equal(t, lastSyncedAt, serverTime, "last synced at mismatch")
1832 assert.Equal(t, lastMaxUSN, serverMaxUSN, "last max usn mismatch")
1833 })
1834
1835 t.Run("with empty fragment but server has data - preserves last_max_usn", func(t *testing.T) {
1836 // This tests the fix for the infinite sync bug where empty fragments
1837 // would reset last_max_usn to 0, causing the client to re-download all data.
1838 // When serverMaxUSN=0 (no data in fragment) but userMaxUSN>0 (server has data),
1839 // we're caught up and should preserve the existing last_max_usn.
1840
1841 // set up
1842 db := database.InitTestMemoryDB(t)
1843 testutils.LoginDB(t, db)
1844
1845 existingLastMaxUSN := 100
1846 database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742))
1847 database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, existingLastMaxUSN)
1848
1849 // execute
1850 tx, err := db.Begin()
1851 if err != nil {
1852 t.Fatal(errors.Wrap(err, "beginning a transaction").Error())

Callers

nothing calls this directly

Calls 11

InitTestMemoryDBFunction · 0.92
LoginDBFunction · 0.92
MustExecFunction · 0.92
MustScanFunction · 0.92
EqualFunction · 0.92
saveSyncStateFunction · 0.85
BeginMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
QueryRowMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected