912 lines
51 KiB
Ziran
912 lines
51 KiB
Ziran
#import "number_text"
|
|
#import "byte_text_linux"
|
|
#import "drawing_props"
|
|
#import, file "storage_test_support.zi";
|
|
#import, file "../src/app/text_buffers.zi";
|
|
#import, file "../src/storage/data.zi";
|
|
#import, file "../src/storage/state.zi";
|
|
#import, file "../src/storage/db.zi";
|
|
#import, file "../src/storage/session_mood.zi";
|
|
#import, file "../src/storage/storage_core.zi";
|
|
#import, file "../src/storage/storage_sessions.zi";
|
|
#import, file "../src/habits/habit_types.zi";
|
|
#import, file "../src/habits/habit_memory.zi";
|
|
#import, file "../src/storage/storage_habits.zi";
|
|
#import, file "../src/storage/sync_review.zi";
|
|
#import, file "../src/habits/habit_model.zi";
|
|
|
|
c :: #system_library "c";
|
|
sqlite_free :: (memory: *void) #foreign c "free";
|
|
|
|
habits_a: Habits;
|
|
habits_b: Habits;
|
|
|
|
// The account key material: an ML-DSA-sized key of repeating "42" hex.
|
|
set_account :: () {
|
|
key: [5121]u8
|
|
index: s32 = 0
|
|
while index < 5120 {
|
|
if index % 2 == 0 { key[index] = cast(u8)52 } else { key[index] = cast(u8)50 }
|
|
index += 1
|
|
}
|
|
key[5120] = cast(u8)0
|
|
storage_set_setting_text("sync_public_id", "test-public-id")
|
|
storage_set_setting_text("sync_public_key", "test-public-key")
|
|
storage_set_setting_text("sync_private_key", TextFromCString(*key[0]))
|
|
}
|
|
|
|
payload_text: TextBuffer;
|
|
|
|
// Builds the sync payload into `payload_text`; false when none is produced.
|
|
build_payload :: () -> bool {
|
|
payload := storage_build_sync_payload_json("test-public-id", "test-public-key")
|
|
if payload == null { return false }
|
|
payload_text = TextBuffer.{}
|
|
append(*payload_text, TextFromCString(payload))
|
|
storage_free_sync_payload_json(payload)
|
|
return true
|
|
}
|
|
|
|
payload :: () -> string {
|
|
return TextFromCString(*payload_text.bytes[0])
|
|
}
|
|
|
|
record_count :: () -> s32 {
|
|
return storage_json_array_count_path(payload(), "$.encrypted_records")
|
|
}
|
|
|
|
// The "encrypted_records" array of the last payload, as text.
|
|
records_array :: () -> string {
|
|
text := payload()
|
|
key: string = "\"encrypted_records\":"
|
|
start: s64 = 0
|
|
found: bool = false
|
|
while start + key.count <= text.count && !found {
|
|
matches: bool = true
|
|
index: s64 = 0
|
|
while index < key.count {
|
|
if text[start + index] != key[index] {
|
|
matches = false
|
|
break
|
|
}
|
|
index += 1
|
|
}
|
|
if matches { found = true } else { start += 1 }
|
|
}
|
|
if !found { return "[]" }
|
|
begin: s64 = start + key.count
|
|
depth: s32 = 0
|
|
end: s64 = begin
|
|
while end < text.count {
|
|
if text[end] == cast(u8)91 { depth += 1 }
|
|
if text[end] == cast(u8)93 {
|
|
depth -= 1
|
|
if depth == 0 {
|
|
end += 1
|
|
break
|
|
}
|
|
}
|
|
end += 1
|
|
}
|
|
return text[begin:end]
|
|
}
|
|
|
|
response_buffer: TextBuffer;
|
|
|
|
// A v6 server response carrying `records` for a device to apply.
|
|
response_with :: (version: s32, records: string) -> string {
|
|
part: [24]u8
|
|
response_buffer = TextBuffer.{}
|
|
append(*response_buffer, "{\"protocol_version\":6,\"status\":\"ok\",\"server_version\":")
|
|
append(*response_buffer, FormatInteger(cast(s64)version, part[:]))
|
|
append(*response_buffer, ",\"server_clock\":")
|
|
append(*response_buffer, FormatInteger(cast(s64)version, part[:]))
|
|
append(*response_buffer, ",\"changes\":{\"encrypted_records\":")
|
|
append(*response_buffer, records)
|
|
append(*response_buffer, "}}")
|
|
return TextFromCString(*response_buffer.bytes[0])
|
|
}
|
|
|
|
empty_response :: (version: s32) -> string {
|
|
return response_with(version, "[]")
|
|
}
|
|
|
|
ascii_lower :: (byte: u8) -> u8 {
|
|
if byte >= cast(u8)65 && byte <= cast(u8)90 { return byte + cast(u8)32 }
|
|
return byte
|
|
}
|
|
|
|
find_habit :: (habits: *Habits, name: string) -> *Habit {
|
|
index: s32 = 0
|
|
while index < habits.count {
|
|
text := TextFromCString(*habits.items[index].name[0])
|
|
if text.count == name.count {
|
|
same: bool = true
|
|
position: s64 = 0
|
|
while position < text.count {
|
|
if ascii_lower(text[position]) != ascii_lower(name[position]) { same = false }
|
|
position += 1
|
|
}
|
|
if same { return *habits.items[index] }
|
|
}
|
|
index += 1
|
|
}
|
|
return null
|
|
}
|
|
|
|
day_count_of :: (habit: *Habit, day: s32) -> s32 {
|
|
index := habit_find_day(habit, day)
|
|
if index < 0 { return -1 }
|
|
return habit.days[index].count
|
|
}
|
|
|
|
Green :: () -> Color {
|
|
return Color.{99, 196, 165, 255}
|
|
}
|
|
|
|
test_payload_after_upload_marker :: () {
|
|
root := make_root("sync-uploaded")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habit_set_day_count(*habits_a, 0, 20260612, 12)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
raw_exec(root, "INSERT OR REPLACE INTO meta(key,value) VALUES('sync_full_upload_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_backfill_v2_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_habit_name_repair_done','1');DELETE FROM sync_outbox;")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
check_true("payload builds", build_payload())
|
|
check_int("uploaded state is not sent again", record_count(), 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_backfill_includes_existing_habits :: () {
|
|
root := make_root("sync-backfill")
|
|
check_true("init db", storage_init(root) != 0)
|
|
check_true("no payload without a private record key",
|
|
storage_build_sync_payload_json("test-hash", "test-public-key") == null)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
raw_exec(root, "INSERT OR REPLACE INTO meta(key,value) VALUES('sync_full_upload_done','1');DELETE FROM sync_outbox;")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
check_true("payload builds", build_payload())
|
|
check_true("existing habit is in the encrypted backfill",
|
|
contains(payload(), "private.inbe.v1.habits") && !contains(payload(), "\"ops\":"))
|
|
storage_close()
|
|
}
|
|
|
|
test_payload_excludes_local_settings :: () {
|
|
root := make_root("sync-settings")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
storage_set_setting_text("theme", "dark")
|
|
storage_set_setting_text("main_tab", "0")
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("payload builds", build_payload())
|
|
check_true("settings are not in the payload",
|
|
!contains(payload(), "\"preferences\"") && !contains(payload(), "\"theme\"") &&
|
|
!contains(payload(), "\"main_tab\""))
|
|
storage_close()
|
|
}
|
|
|
|
test_payload_includes_queued_edits :: () {
|
|
root := make_root("sync-queued")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habit_set_day_count(*habits_a, 0, 20260617, 7)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("payload builds", build_payload())
|
|
check_true("queued habit day is sent", contains(payload(), ":20260617\""))
|
|
check_true("its content is encrypted", !contains(payload(), "\"count\":7"))
|
|
storage_close()
|
|
}
|
|
|
|
test_rejects_unknown_outbox_record :: () {
|
|
root := make_root("sync-unknown")
|
|
status: StorageSyncStatus
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
exec_db_sql("INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('unknown','record-1',0,1)")
|
|
check_true("unknown queued record rejects the payload", !build_payload())
|
|
check_true("status stays available", storage_sync_status(*status) != 0)
|
|
check_true("the record stays pending", status.queued_changes > 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_v6_encrypted_records :: () {
|
|
root := make_root("sync-v6")
|
|
collection: [80]u8
|
|
record_id: [180]u8
|
|
ciphertext: [4096]u8
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habit_set_day_count(*habits_a, 0, 20260829, 3)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("payload builds", build_payload())
|
|
text := payload()
|
|
check_true("v6 protocol", contains(text, "\"protocol_version\":6"))
|
|
check_true("app id", contains(text, "\"app_id\":\"inbe\""))
|
|
check_true("no legacy data opt-in", !contains(text, "include_legacy_data"))
|
|
check_true("capabilities", contains(text, "\"client_capabilities\"") &&
|
|
contains(text, "v6-device-transactions") && contains(text, "encrypted-primary") &&
|
|
!contains(text, "legacy-dual-write"))
|
|
check_true("encrypted records present", record_count() > 0)
|
|
check_true("first collection", storage_json_array_object_text(text, "$.encrypted_records", 0,
|
|
"collection", *collection[0], 80) != 0 && collection[0] != cast(u8)0)
|
|
check_true("first record id", storage_json_array_object_text(text, "$.encrypted_records", 0,
|
|
"id", *record_id[0], 180) != 0 && record_id[0] != cast(u8)0)
|
|
check_true("first ciphertext", storage_json_array_object_text(text, "$.encrypted_records", 0,
|
|
"ciphertext", *ciphertext[0], 4096) != 0 && ciphertext[0] != cast(u8)0 &&
|
|
!contains(TextFromCString(*ciphertext[0]), "Yoga"))
|
|
check_true("pre-v6 response rejected", storage_apply_sync_response_json(
|
|
"{\"protocol_version\":4,\"status\":\"ok\",\"server_version\":10,\"changes\":{\"encrypted_records\":[]}}") == 0)
|
|
check_true("payload builds again", build_payload())
|
|
check_true("records survive the rejected response", record_count() > 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_plaintext_private_response_ignored :: () {
|
|
root := make_root("sync-plaintext")
|
|
check_true("init db", storage_init(root) != 0)
|
|
check_true("v6 response with a plaintext private row applies", storage_apply_sync_response_json(
|
|
"{\"protocol_version\":6,\"status\":\"ok\",\"server_version\":12,\"server_clock\":12,\"changes\":{\"habits\":[{\"id\":\"plaintext-habit\",\"name\":\"Unexpected\",\"color_r\":1,\"color_g\":2,\"color_b\":3,\"sync_mode\":0,\"sync_activity\":0,\"counter_enabled\":0,\"sort_order\":0,\"deleted_at\":0,\"updated_at\":\"2026-08-29T12:30:00Z\"}],\"habit_days\":[],\"sessions\":[],\"encrypted_records\":[]}}") != 0)
|
|
check_int("plaintext private row ignored", storage_habit_count(), 0)
|
|
check_int("no changed private data signalled", storage_last_sync_changed(), 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_large_outbox_batches :: () {
|
|
root := make_root("sync-batch")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(root, "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('batch-habit',(SELECT id FROM users LIMIT 1),'Batch',255,255,255,0,0,1,0,0,1781902800);")
|
|
raw_exec(root, "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i+1 FROM n WHERE i<404) INSERT INTO habit_days(habit_id,local_date,completed,count,updated_at) SELECT 'batch-habit',20260101+i,1,1,1781902800 FROM n;")
|
|
raw_exec(root, "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i+1 FROM n WHERE i<404) INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day','batch-habit',20260101+i,1781902800 FROM n;")
|
|
raw_exec(root, "INSERT OR REPLACE INTO meta(key,value) VALUES('sync_backfill_v2_done','1');")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
// A full batch is far larger than the shared text buffer, so count it in place.
|
|
large := storage_build_sync_payload_json("test-public-id", "test-public-key")
|
|
check_true("payload builds", large != null)
|
|
if large != null {
|
|
check_int("large outbox payload is batched",
|
|
storage_json_array_count_path(TextFromCString(large), "$.encrypted_records"), 400)
|
|
storage_free_sync_payload_json(large)
|
|
}
|
|
check_true("apply response", storage_apply_sync_response_json(empty_response(1)) != 0)
|
|
storage_close()
|
|
check_int("later rows stay queued", raw_count(root, "SELECT COUNT(*) FROM sync_outbox"), 5)
|
|
}
|
|
|
|
test_outbox_keeps_edits_after_snapshot :: () {
|
|
root := make_root("sync-snapshot")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habit_set_day_count(*habits_a, 0, 20260618, 1)
|
|
habits_save(*habits_a)
|
|
check_true("first payload", build_payload())
|
|
check_true("first edit in payload", contains(payload(), ":20260618\""))
|
|
habit_set_day_count(*habits_a, 0, 20260619, 2)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("apply clears only the snapshot", storage_apply_sync_response_json(empty_response(1)) != 0)
|
|
check_true("second payload", build_payload())
|
|
check_true("later edit stays queued", contains(payload(), ":20260619\""))
|
|
check_true("earlier edit was cleared", !contains(payload(), ":20260618\""))
|
|
storage_close()
|
|
}
|
|
|
|
test_orphan_habit_days_reset_cursor :: () {
|
|
root := make_root("sync-orphan")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(root, "INSERT OR REPLACE INTO habit_days(habit_id,local_date,completed,updated_at) VALUES('habit-2',20260617,1,0);INSERT OR REPLACE INTO habit_days(habit_id,local_date,completed,updated_at) VALUES('habit-3',20260618,1,0);INSERT OR REPLACE INTO meta(key,value) VALUES('sync_last_server_version','530');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_full_upload_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_backfill_v2_done','1');")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
check_true("payload builds", build_payload())
|
|
check_true("orphan habit days request a full snapshot", contains(payload(), "\"since_server_version\":0"))
|
|
storage_close()
|
|
}
|
|
|
|
test_habit_name_repair_runs_once :: () {
|
|
root := make_root("sync-name-repair")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(root, "INSERT OR REPLACE INTO meta(key,value) VALUES('sync_last_server_version','530');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_full_upload_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_backfill_v2_done','1');")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
check_true("payload builds", build_payload())
|
|
check_true("repair requests a full snapshot", contains(payload(), "\"since_server_version\":0"))
|
|
check_true("apply repair response", storage_apply_sync_response_json(empty_response(530)) != 0)
|
|
check_true("payload builds again", build_payload())
|
|
check_true("repair only runs once", contains(payload(), "\"since_server_version\":530"))
|
|
storage_close()
|
|
}
|
|
|
|
// Device A's records applied on device B come out as the same habits.
|
|
test_records_round_trip_between_devices :: () {
|
|
first := make_root("sync-device-a")
|
|
second := make_root("sync-device-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
check_true("add habit", habits_add_custom(*habits_a, "Cold Shower", Green(), 0, 0) == 0)
|
|
habit_set_day_count(*habits_a, 0, 20260618, 3)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("device a payload", build_payload())
|
|
check_true("device a has records", record_count() > 0)
|
|
records := records_array()
|
|
storage_close()
|
|
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
check_true("device b applies", storage_apply_sync_response_json(response_with(5, records)) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("device b loads", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("device b habit count", habits_b.count, 1)
|
|
habit := find_habit(*habits_b, "Cold Shower")
|
|
check_true("device b has the habit", habit != null)
|
|
check_int("device b day count", day_count_of(habit, 20260618), 3)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
// Sends device A's queued changes to device B and clears A's queue.
|
|
ship_to :: (from_root: string, to_root: string, version: s32) -> bool {
|
|
if storage_init(from_root) == 0 { return false }
|
|
if !build_payload() {
|
|
storage_close()
|
|
return false
|
|
}
|
|
records := records_array()
|
|
copy: TextBuffer
|
|
append(*copy, records)
|
|
check_true("clear queue after upload", storage_apply_sync_response_json(empty_response(version)) != 0)
|
|
storage_close()
|
|
if storage_init(to_root) == 0 { return false }
|
|
ok: bool = storage_apply_sync_response_json(response_with(version, TextFromCString(*copy.bytes[0]))) != 0
|
|
storage_close()
|
|
return ok
|
|
}
|
|
|
|
test_deletion_round_trip :: () {
|
|
first := make_root("sync-delete-a")
|
|
second := make_root("sync-delete-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
check_true("add habit", habits_add_custom(*habits_a, "Cold Shower", Green(), 0, 0) == 0)
|
|
habit_set_day_count(*habits_a, 0, 20260618, 3)
|
|
habits_save(*habits_a)
|
|
storage_close()
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("ship habit", ship_to(first, second, 1))
|
|
check_true("reopen device b", storage_init(second) != 0)
|
|
check_int("device b has the habit", storage_habit_count(), 1)
|
|
storage_close()
|
|
|
|
check_true("reopen device a", storage_init(first) != 0)
|
|
habits_a = Habits.{}
|
|
check_true("load device a", storage_habits_load(cast(*void)*habits_a) != 0)
|
|
habits_delete(*habits_a, 0)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("deletion payload builds", build_payload())
|
|
check_true("deletion is marked", contains(payload(), "\"deleted_at\":"))
|
|
storage_close()
|
|
check_true("ship deletion", ship_to(first, second, 2))
|
|
check_true("reopen device b after deletion", storage_init(second) != 0)
|
|
check_int("device b lost the habit", storage_habit_count(), 0)
|
|
storage_close()
|
|
}
|
|
|
|
// A newer remote count replaces the local one, in both directions, and a
|
|
// queued local edit at the same time is not overwritten.
|
|
test_counter_counts_apply :: () {
|
|
first := make_root("sync-counter-a")
|
|
second := make_root("sync-counter-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
check_true("add habit", habits_add_custom(*habits_a, "Counter", Green(), 0, 0) == 0)
|
|
habit_set_day_count(*habits_a, 0, 20260619, 4)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("ship habit", ship_to(first, second, 1))
|
|
// Both devices now agree on 4. Pin the times so ordering is explicit.
|
|
raw_exec(first, "UPDATE habit_days SET count=1,updated_at=1781902900;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1781902900 FROM habit_days;")
|
|
raw_exec(second, "UPDATE habit_days SET updated_at=1781902800;DELETE FROM sync_outbox;")
|
|
check_true("ship lower newer count", ship_to(first, second, 2))
|
|
check_int("newer lower count applied", raw_count(second, "SELECT count FROM habit_days WHERE local_date=20260619"), 1)
|
|
|
|
raw_exec(first, "UPDATE habit_days SET count=5,updated_at=1781903000;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1781903000 FROM habit_days;")
|
|
check_true("ship higher newer count", ship_to(first, second, 3))
|
|
check_int("newer higher count applied", raw_count(second, "SELECT count FROM habit_days WHERE local_date=20260619"), 5)
|
|
|
|
// An older remote value must lose to a newer local one.
|
|
raw_exec(first, "UPDATE habit_days SET count=2,updated_at=1781902000;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1781902000 FROM habit_days;")
|
|
check_true("ship older count", ship_to(first, second, 4))
|
|
check_int("older remote count ignored", raw_count(second, "SELECT count FROM habit_days WHERE local_date=20260619"), 5)
|
|
}
|
|
|
|
// A queued local edit wins over an equal-time remote copy of the same day.
|
|
test_queued_edit_beats_equal_time_remote :: () {
|
|
first := make_root("sync-equal-a")
|
|
second := make_root("sync-equal-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
check_true("add habit", habits_add_custom(*habits_a, "Counter", Green(), 0, 0) == 0)
|
|
habit_set_day_count(*habits_a, 0, 20260619, 4)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("ship habit", ship_to(first, second, 1))
|
|
raw_exec(first, "UPDATE habit_days SET count=1,updated_at=1781902800;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1781902800 FROM habit_days;")
|
|
raw_exec(second, "UPDATE habit_days SET count=9,updated_at=1781902800;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1781902800 FROM habit_days;")
|
|
check_true("ship equal-time count", ship_to(first, second, 2))
|
|
check_int("queued local edit kept", raw_count(second, "SELECT count FROM habit_days WHERE local_date=20260619"), 9)
|
|
}
|
|
|
|
// Two devices that already share `habit` (a habit named "Counter" with one
|
|
// day), each in its own root. Returns to the caller with neither open.
|
|
share_counter_habit :: (first: string, second: string) {
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
check_true("add habit", habits_add_custom(*habits_a, "Counter", Green(), 0, 0) == 0)
|
|
habit_set_day_count(*habits_a, 0, 20260619, 4)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("ship habit", ship_to(first, second, 1))
|
|
}
|
|
|
|
test_counter_enabled_follows_remote :: () {
|
|
first := make_root("sync-toggle-a")
|
|
second := make_root("sync-toggle-b")
|
|
share_counter_habit(first, second)
|
|
raw_exec(first, "UPDATE habits SET counter_enabled=1,updated_at=1781903000;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit',id,0,1781903000 FROM habits;")
|
|
raw_exec(second, "UPDATE habits SET counter_enabled=0,updated_at=1781902800;DELETE FROM sync_outbox;")
|
|
check_true("ship enable", ship_to(first, second, 2))
|
|
check_int("remote enables the counter", raw_count(second, "SELECT counter_enabled FROM habits"), 1)
|
|
raw_exec(first, "UPDATE habits SET counter_enabled=0,updated_at=1781903100;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit',id,0,1781903100 FROM habits;")
|
|
check_true("ship disable", ship_to(first, second, 3))
|
|
check_int("remote disables the counter", raw_count(second, "SELECT counter_enabled FROM habits"), 0)
|
|
}
|
|
|
|
test_queued_counter_enabled_survives_equal_remote :: () {
|
|
first := make_root("sync-queued-toggle-a")
|
|
second := make_root("sync-queued-toggle-b")
|
|
share_counter_habit(first, second)
|
|
raw_exec(first, "UPDATE habits SET counter_enabled=0,updated_at=1781902800;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit',id,0,1781902800 FROM habits;")
|
|
raw_exec(second, "UPDATE habits SET counter_enabled=1,updated_at=1781902800;DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit',id,0,1781902800 FROM habits;")
|
|
check_true("ship equal-time habit", ship_to(first, second, 2))
|
|
check_int("queued local counter setting kept", raw_count(second, "SELECT counter_enabled FROM habits"), 1)
|
|
check_true("reopen device b", storage_init(second) != 0)
|
|
check_true("still queued for upload", build_payload() && record_count() > 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_stale_habit_save_keeps_remote_habits :: () {
|
|
first := make_root("sync-stale-a")
|
|
second := make_root("sync-stale-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
check_true("add push ups", habits_add_custom(*habits_a, "Push ups", Green(), 0, 0) == 0)
|
|
check_true("add cold shower", habits_add_custom(*habits_a, "Cold Shower", Green(), 0, 0) == 1)
|
|
habit_set_day(*habits_a, 0, 20260619, 1)
|
|
habit_set_day(*habits_a, 1, 20260619, 1)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("device a payload", build_payload())
|
|
records := records_array()
|
|
kept: TextBuffer
|
|
append(*kept, records)
|
|
storage_close()
|
|
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
// Device b's screen holds only the default habits.
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
check_true("apply remote habits behind the stale screen",
|
|
storage_apply_sync_response_json(response_with(1, TextFromCString(*kept.bytes[0]))) != 0)
|
|
habits_save(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load habits", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("stale save keeps the remote habits", habits_b.count, 4)
|
|
check_true("push ups survived", find_habit(*habits_b, "Push ups") != null)
|
|
check_true("cold shower survived", find_habit(*habits_b, "Cold Shower") != null)
|
|
check_true("default yoga survived", find_habit(*habits_b, "Yoga") != null)
|
|
habits_free(*habits_a)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_duplicate_named_habits_merge :: () {
|
|
first := make_root("sync-merge-a")
|
|
second := make_root("sync-merge-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(first, "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('habit-1',(SELECT id FROM users LIMIT 1),'New Habit',99,196,165,0,0,0,0,0,1781902800),('habit-2',(SELECT id FROM users LIMIT 1),'New Habit',99,196,165,0,0,0,1,0,1781902860),('meditation',(SELECT id FROM users LIMIT 1),'Meditation',126,183,230,1,3,0,2,0,1781902920);INSERT INTO habit_days(habit_id,local_date,completed,count,updated_at) VALUES('habit-1',20260617,1,1,1781902800),('habit-2',20260618,1,1,1781902860),('meditation',20260619,1,1,1781902920);DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit',id,0,1781902800 FROM habits;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1781902800 FROM habit_days;")
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("ship duplicates", ship_to(first, second, 1))
|
|
check_true("reopen device b", storage_init(second) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("load merged habits", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("duplicate habit names collapse to one", habits_b.count, 2)
|
|
merged := find_habit(*habits_b, "New Habit")
|
|
check_true("merged habit visible", merged != null)
|
|
check_true("first duplicate day kept", merged != null && habit_completed_day(merged, 20260617) != 0)
|
|
check_true("second duplicate day moved", merged != null && habit_completed_day(merged, 20260618) != 0)
|
|
check_true("meditation remains", find_habit(*habits_b, "Meditation") != null)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_session_last_write_wins :: () {
|
|
first := make_root("sync-session-a")
|
|
second := make_root("sync-session-b")
|
|
session_sql: string = "INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('session-equal-time',(SELECT id FROM users LIMIT 1),1781902800,20260619,0,1,'local',1781902800,'111',0,1781902800);"
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(first, session_sql)
|
|
raw_exec(first, "INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('session-equal-time',0,45);DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('session','session-equal-time',0,1781902800);")
|
|
raw_exec(second, session_sql)
|
|
raw_exec(second, "INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('session-equal-time',0,60);DELETE FROM sync_outbox;")
|
|
check_true("ship equal-time session", ship_to(first, second, 1))
|
|
check_int("equal-time remote update replaces rounds",
|
|
raw_count(second, "SELECT seconds FROM session_rounds WHERE session_id='session-equal-time' AND round_index=0"), 45)
|
|
// The same again with a queued local edit: it keeps its own rounds.
|
|
raw_exec(second, "UPDATE sessions SET rounds_hash='111',updated_at=1781902800;DELETE FROM session_rounds WHERE session_id='session-equal-time';INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('session-equal-time',0,60);INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('session','session-equal-time',0,1781902800);")
|
|
raw_exec(first, "DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('session','session-equal-time',0,1781902800);")
|
|
check_true("ship equal-time session again", ship_to(first, second, 2))
|
|
check_int("queued local rounds are kept",
|
|
raw_count(second, "SELECT seconds FROM session_rounds WHERE session_id='session-equal-time' AND round_index=0"), 60)
|
|
}
|
|
|
|
test_linked_session_delete_syncs :: () {
|
|
first := make_root("sync-linked-a")
|
|
second := make_root("sync-linked-b")
|
|
rounds: [1]s32 = .[60]
|
|
id: [44]u8
|
|
today := habits_today_index()
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habits_save(*habits_a)
|
|
check_true("save linked session", storage_save_session_for_activity(*rounds[0], 1, 0, 1, *id[0], 44) != 0)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
check_true("init device b", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
check_true("ship linked session and habit day", ship_to(first, second, 1))
|
|
check_int("device b has the session", raw_count(second, "SELECT COUNT(*) FROM sessions WHERE deleted_at=0"), 1)
|
|
check_int("device b has the linked day", raw_count(second, "SELECT COUNT(*) FROM habit_days WHERE count>0"), 1)
|
|
|
|
check_true("reopen device a", storage_init(first) != 0)
|
|
check_true("delete the linked session", storage_delete_session(TextFromCString(*id[0])) != 0)
|
|
check_true("deletion payload builds", build_payload())
|
|
check_true("session tombstone uploaded", contains(payload(), "private.inbe.v1.sessions") && contains(payload(), "\"deleted_at\":"))
|
|
check_true("linked habit day is cleared remotely too", contains(payload(), "private.inbe.v1.habit-days"))
|
|
storage_close()
|
|
check_true("ship deletion", ship_to(first, second, 2))
|
|
check_int("device b lost the session", raw_count(second, "SELECT COUNT(*) FROM sessions WHERE deleted_at=0"), 0)
|
|
check_int("device b cleared the linked day", raw_count(second, "SELECT COUNT(*) FROM habit_days WHERE count>0"), 0)
|
|
unused today
|
|
}
|
|
|
|
test_fresh_account_bootstraps_without_tombstones :: () {
|
|
root := make_root("sync-fresh-account")
|
|
check_true("init db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
check_true("add habit", habits_add_custom(*habits_a, "Work out", Green(), 0, 0) == 0)
|
|
habit_set_day(*habits_a, 0, 20260618, 1)
|
|
habits_save(*habits_a)
|
|
deleted := storage_delete_all_sessions()
|
|
check_true("local reset deleted rows", deleted >= cast(s64)2)
|
|
check_int("no active habits", storage_habit_count(), 0)
|
|
habits_free(*habits_a)
|
|
set_account()
|
|
storage_reset_sync_state()
|
|
check_true("payload builds", build_payload())
|
|
check_true("fresh account sends a bootstrap", contains(payload(), "\"since_server_version\":0") &&
|
|
contains(payload(), "\"bootstrap\":true"))
|
|
check_int("no stale tombstones or clears", record_count(), 0)
|
|
storage_close()
|
|
}
|
|
|
|
// ---- Full snapshots and the review flow ----------------------------------
|
|
|
|
remote_records_text: TextBuffer;
|
|
|
|
// The records a remote device would send: its own database seeded with `sql`
|
|
// and every row queued for upload.
|
|
remote_records :: (name: string, sql: string) {
|
|
root := make_root(name)
|
|
check_true("init remote device", storage_init(root) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(root, sql)
|
|
raw_exec(root, "DELETE FROM sync_outbox;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit',id,0,1782300000 FROM habits;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_day',habit_id,local_date,1782300000 FROM habit_days;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'session',id,0,1782300000 FROM sessions;")
|
|
check_true("reopen remote device", storage_init(root) != 0)
|
|
check_true("remote payload builds", build_payload())
|
|
remote_records_text = TextBuffer.{}
|
|
append(*remote_records_text, records_array())
|
|
storage_close()
|
|
}
|
|
|
|
snapshot_buffer: TextBuffer;
|
|
|
|
// A server response asking the client to take a full snapshot.
|
|
snapshot_response :: (version: s32, hash: string) -> string {
|
|
part: [24]u8
|
|
snapshot_buffer = TextBuffer.{}
|
|
append(*snapshot_buffer, "{\"protocol_version\":6,\"status\":\"ok\",\"server_version\":")
|
|
append(*snapshot_buffer, FormatInteger(cast(s64)version, part[:]))
|
|
append(*snapshot_buffer, ",\"server_state_hash\":\"")
|
|
append(*snapshot_buffer, hash)
|
|
append(*snapshot_buffer, "\",\"full_snapshot_required\":true,\"changes_complete\":false,\"changes\":{\"encrypted_records\":")
|
|
append(*snapshot_buffer, TextFromCString(*remote_records_text.bytes[0]))
|
|
append(*snapshot_buffer, "}}")
|
|
return TextFromCString(*snapshot_buffer.bytes[0])
|
|
}
|
|
|
|
REMOTE_SQL :: "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('remote-habit',(SELECT id FROM users LIMIT 1),'Remote Breath',90,80,70,0,0,1,1,0,1782290000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('remote-habit',20260624,1,7,0,1782290000);INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('remote-session',(SELECT id FROM users LIMIT 1),1782290000,20260624,2,2,'sync-test',1782290000,'202',0,1782290000);INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('remote-session',0,55);"
|
|
|
|
LOCAL_SQL :: "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('local-habit',(SELECT id FROM users LIMIT 1),'Local Breath',10,20,30,0,0,1,1,0,1782300000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('local-habit',20260624,1,4,0,1782300000);INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('local-session',(SELECT id FROM users LIMIT 1),1782300000,20260624,1,1,'test',1782300000,'101',0,1782300000);INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('local-session',0,40);"
|
|
|
|
LOCAL_PENDING_SQL :: "INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('habit','local-habit',0,1782300000);INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('habit_day','local-habit',20260624,1782300000);INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('session','local-session',0,1782300000);"
|
|
|
|
// A local device with its own data and (optionally) everything queued.
|
|
local_device :: (name: string, seed: string, pending: string) -> string {
|
|
root := make_root(name)
|
|
check_true("init local device", storage_init(root) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(root, seed)
|
|
if pending.count > 0 { raw_exec(root, pending) }
|
|
check_true("reopen local device", storage_init(root) != 0)
|
|
return root
|
|
}
|
|
|
|
// Starts an upload, so the pending changes are "in flight" when the server
|
|
// answers.
|
|
mark_pending_payload_in_flight :: () {
|
|
check_true("pending payload builds", build_payload())
|
|
}
|
|
|
|
count_of :: (root: string, sql: string) -> s32 {
|
|
return read_db_count(sql)
|
|
}
|
|
|
|
test_snapshot_with_pending_local_edits_merges :: () {
|
|
remote_records("review-remote-1", REMOTE_SQL)
|
|
root := local_device("review-pending", LOCAL_SQL, LOCAL_PENDING_SQL)
|
|
mark_pending_payload_in_flight()
|
|
check_true("apply the snapshot", storage_apply_sync_response_json(snapshot_response(12, "remote-hash-001")) != 0)
|
|
check_true("review is not pending for local edits", storage_sync_review_pending() == 0)
|
|
check_int("local session kept", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='local-session'"), 1)
|
|
check_int("remote session merged", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='remote-session'"), 1)
|
|
check_int("local habit kept", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 1)
|
|
check_int("remote habit merged", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='remote-habit'"), 1)
|
|
check_int("pending edits cleared after the accepted sync", count_of(root, "SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_tiny_pending_snapshot_merges :: () {
|
|
remote_records("review-remote-tiny", "INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('tiny-remote-session',(SELECT id FROM users LIMIT 1),1782290300,20260624,1,1,'sync-test',1782290300,'404',0,1782290300);INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('tiny-remote-session',0,55);")
|
|
root := local_device("review-tiny", "INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('tiny-local-session',(SELECT id FROM users LIMIT 1),1782300000,20260624,1,1,'test',1782300000,'101',0,1782300000);INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('tiny-local-session',0,40);", "INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('session','tiny-local-session',0,1782300000);")
|
|
mark_pending_payload_in_flight()
|
|
check_true("apply the snapshot", storage_apply_sync_response_json(snapshot_response(15, "remote-hash-tiny-session")) != 0)
|
|
check_true("review is not pending", storage_sync_review_pending() == 0)
|
|
check_int("local session kept", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='tiny-local-session'"), 1)
|
|
check_int("remote session merged", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='tiny-remote-session'"), 1)
|
|
check_int("outbox cleared", count_of(root, "SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_local_habit_without_activity_does_not_force_review :: () {
|
|
remote_records("review-remote-2", REMOTE_SQL)
|
|
root := local_device("review-empty-habit", "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('empty-local-habit',(SELECT id FROM users LIMIT 1),'Empty Habit',10,20,30,0,0,1,1,0,1782300000);", "INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('habit','empty-local-habit',0,1782300000);")
|
|
mark_pending_payload_in_flight()
|
|
check_true("apply the snapshot", storage_apply_sync_response_json(snapshot_response(12, "remote-hash-001")) != 0)
|
|
check_true("review is not pending", storage_sync_review_pending() == 0)
|
|
check_int("remote session applied", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='remote-session'"), 1)
|
|
check_int("remote habit applied", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='remote-habit'"), 1)
|
|
check_int("outbox cleared", count_of(root, "SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
storage_close()
|
|
}
|
|
|
|
// The review works on the visible differences of a snapshot; rows the
|
|
// server marks deleted are not among them.
|
|
test_review_ignores_deleted_remote_rows :: () {
|
|
root := make_root("review-deleted")
|
|
check_true("init db", storage_init(root) != 0)
|
|
deleted: string = "{\"server_version\":13,\"server_state_hash\":\"remote-hash-deleted\",\"full_snapshot_required\":true,\"changes_complete\":false,\"changes\":{\"habits\":[],\"habit_days\":[],\"sessions\":[{\"id\":\"deleted-session\",\"started_at\":\"2026-06-24T10:00:00Z\",\"local_date\":20260624,\"topic\":2,\"activity\":3,\"source\":\"sync-test\",\"rounds_hash\":202,\"deleted_at\":1782300000,\"updated_at\":\"2026-06-24T10:00:00Z\",\"rounds\":[{\"round_index\":0,\"hold_seconds\":55}]}],\"meditation_logs\":[]}}"
|
|
check_true("no visible diff", storage_sync_review_json_has_visible_diff(deleted) == 0)
|
|
check_true("review not pending", storage_sync_review_pending() == 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_remote_additions_apply_without_review :: () {
|
|
remote_records("review-remote-yoga", "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('yoga',(SELECT id FROM users LIMIT 1),'Yoga',239,178,102,1,4,0,1,0,1782290000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('yoga',20260624,1,1,0,1782290000);INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('sun-session',(SELECT id FROM users LIMIT 1),1782290000,20260624,0,2,'test',1782290000,'303',0,1782290000);INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('sun-session',0,1);")
|
|
root := make_root("review-additive")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
check_true("apply additions", storage_apply_sync_response_json(snapshot_response(14, "remote-hash-yoga")) != 0)
|
|
check_true("review not pending", storage_sync_review_pending() == 0)
|
|
check_int("sun session applied", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='sun-session'"), 1)
|
|
check_int("yoga habit applied", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='yoga' AND counter_enabled=0"), 1)
|
|
check_int("yoga day applied", count_of(root, "SELECT COUNT(*) FROM habit_days WHERE habit_id='yoga' AND count=1"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
test_apply_inside_an_outer_transaction :: () {
|
|
remote_records("review-remote-clean", "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('clean-habit',(SELECT id FROM users LIMIT 1),'Clean Breath',12,34,56,0,0,1,3,0,1782290000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('clean-habit',20260624,1,5,0,1782290000);INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('clean-session',(SELECT id FROM users LIMIT 1),1782290000,20260624,2,2,'sync-test',1782290000,'505',0,1782290000);INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('clean-session',0,44);")
|
|
clean := TextBuffer.{}
|
|
append(*clean, TextFromCString(*remote_records_text.bytes[0]))
|
|
root := make_root("review-nested")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
check_true("begin outer transaction", exec_sql("BEGIN IMMEDIATE") != 0)
|
|
check_true("apply inside it", storage_apply_sync_response_json(response_with(21, TextFromCString(*clean.bytes[0]))) != 0)
|
|
check_true("commit outer transaction", exec_sql("COMMIT") != 0)
|
|
check_int("habit applied", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='clean-habit'"), 1)
|
|
check_int("day applied", count_of(root, "SELECT COUNT(*) FROM habit_days WHERE habit_id='clean-habit' AND count=5"), 1)
|
|
check_int("session applied", count_of(root, "SELECT COUNT(*) FROM sessions WHERE id='clean-session'"), 1)
|
|
check_int("rounds applied", count_of(root, "SELECT COUNT(*) FROM session_rounds WHERE session_id='clean-session' AND seconds=44"), 1)
|
|
storage_close()
|
|
|
|
// A local habit with the same name but another id is replaced by the
|
|
// remote one, keeping its days.
|
|
second := make_root("review-id-repair")
|
|
check_true("init repair db", storage_init(second) != 0)
|
|
set_account()
|
|
storage_close()
|
|
raw_exec(second, "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('local-clean-id',(SELECT id FROM users LIMIT 1),'Clean Breath',9,9,9,0,0,1,3,0,1782200000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('local-clean-id',20260623,1,2,0,1782200000);DELETE FROM sync_outbox;")
|
|
check_true("reopen repair db", storage_init(second) != 0)
|
|
check_true("apply the remote habit", storage_apply_sync_response_json(response_with(22, TextFromCString(*clean.bytes[0]))) != 0)
|
|
check_int("local duplicate id removed", count_of(second, "SELECT COUNT(*) FROM habits WHERE id='local-clean-id'"), 0)
|
|
check_int("canonical remote id present", count_of(second, "SELECT COUNT(*) FROM habits WHERE id='clean-habit'"), 1)
|
|
check_int("local day moved to the canonical id", count_of(second, "SELECT COUNT(*) FROM habit_days WHERE habit_id='clean-habit' AND local_date=20260623 AND count=2"), 1)
|
|
check_int("remote day still applied", count_of(second, "SELECT COUNT(*) FROM habit_days WHERE habit_id='clean-habit' AND local_date=20260624 AND count=5"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
YOGA_SQL :: "INSERT INTO habits(id,user_id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at) VALUES('yoga',(SELECT id FROM users LIMIT 1),'Yoga',239,178,102,1,4,0,1,0,1782300000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('yoga',20260624,1,2,0,1782300000);"
|
|
|
|
test_snapshot_removes_absent_local_yoga :: () {
|
|
remote_records("review-remote-none", "DELETE FROM habits;")
|
|
root := local_device("review-removes-yoga", YOGA_SQL, "DELETE FROM sync_outbox;")
|
|
check_true("apply the snapshot", storage_apply_sync_response_json(snapshot_response(13, "remote-hash-deleted")) != 0)
|
|
check_true("review not pending", storage_sync_review_pending() == 0)
|
|
check_int("yoga habit removed", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='yoga'"), 0)
|
|
check_int("yoga day removed", count_of(root, "SELECT COUNT(*) FROM habit_days WHERE habit_id='yoga'"), 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_snapshot_keeps_pending_yoga :: () {
|
|
remote_records("review-remote-none-2", "DELETE FROM habits;")
|
|
root := local_device("review-keeps-yoga", YOGA_SQL, "INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('habit','yoga',0,1782300000);")
|
|
mark_pending_payload_in_flight()
|
|
check_true("apply the snapshot", storage_apply_sync_response_json(snapshot_response(13, "remote-hash-deleted")) != 0)
|
|
check_true("review not pending", storage_sync_review_pending() == 0)
|
|
check_int("pending yoga still local after the merge", count_of(root, "SELECT COUNT(*) FROM habits WHERE id='yoga'"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
test_normal_response_records_server_hash :: () {
|
|
root := make_root("review-normal")
|
|
status: StorageSyncStatus
|
|
check_true("init db", storage_init(root) != 0)
|
|
check_true("apply a normal response", storage_apply_sync_response_json(
|
|
"{\"protocol_version\":6,\"server_version\":5,\"server_clock\":77,\"server_state_hash\":\"normal-hash-001\",\"account_alias\":\"waozi\",\"changes\":{\"encrypted_records\":[]}}") != 0)
|
|
check_true("no review", storage_sync_review_pending() == 0)
|
|
check_true("alias saved", storage_get_setting_text("sync_account_alias") == "waozi")
|
|
check_true("status loads", storage_sync_status(*status) != 0)
|
|
check_int("server version recorded", cast(s32)status.server_version, 5)
|
|
check_int("server clock recorded", cast(s32)status.server_clock, 77)
|
|
set_account()
|
|
check_true("payload builds", build_payload())
|
|
check_true("hash in the next payload", contains(payload(), "\"last_server_state_hash\":\"normal-hash-001\""))
|
|
check_true("clock in the next payload", contains(payload(), "\"client_clock\":77"))
|
|
check_true("no full replace requested", !contains(payload(), "full_sync_requested"))
|
|
storage_close()
|
|
}
|
|
|
|
test_social_cache_is_server_authored :: () {
|
|
root := make_root("review-social")
|
|
cached: [512]u8
|
|
check_true("init db", storage_init(root) != 0)
|
|
check_true("store a local social cache", storage_set_social_cache_json("friends.list",
|
|
"{\"friends\":[{\"user_id_hash\":\"local-edit\"}]}") != 0)
|
|
check_true("a social cache alone is not local syncable data", storage_has_local_syncable_data() == 0)
|
|
set_account()
|
|
check_true("payload builds", build_payload())
|
|
check_true("social cache is not uploaded", !contains(payload(), "social_cache") && !contains(payload(), "friends.list"))
|
|
check_true("apply the server's social cache", storage_apply_sync_response_json(
|
|
"{\"protocol_version\":6,\"server_version\":9,\"server_clock\":9,\"server_state_hash\":\"social-hash-001\",\"changes\":{\"encrypted_records\":[],\"social_cache\":[{\"kind\":\"friends.list\",\"json\":{\"friends\":[{\"user_id_hash\":\"server-friend\"}]},\"updated_at\":\"2026-06-28T00:00:00Z\"}]}}") != 0)
|
|
check_true("load the cache", storage_get_social_cache_json("friends.list", *cached[0], 512) != 0)
|
|
text := TextFromCString(*cached[0])
|
|
check_true("the server's cache replaces the local edit", contains(text, "server-friend") && !contains(text, "local-edit"))
|
|
storage_close()
|
|
}
|
|
|
|
#program_export
|
|
Check :: () -> s32 {
|
|
guard := support_begin("/tmp/inbe-storage-sync-zi-test/")
|
|
if guard != 0 { return guard }
|
|
test_payload_after_upload_marker()
|
|
test_backfill_includes_existing_habits()
|
|
test_payload_excludes_local_settings()
|
|
test_payload_includes_queued_edits()
|
|
test_rejects_unknown_outbox_record()
|
|
test_v6_encrypted_records()
|
|
test_plaintext_private_response_ignored()
|
|
test_large_outbox_batches()
|
|
test_outbox_keeps_edits_after_snapshot()
|
|
test_orphan_habit_days_reset_cursor()
|
|
test_habit_name_repair_runs_once()
|
|
test_records_round_trip_between_devices()
|
|
test_deletion_round_trip()
|
|
test_counter_counts_apply()
|
|
test_queued_edit_beats_equal_time_remote()
|
|
test_counter_enabled_follows_remote()
|
|
test_queued_counter_enabled_survives_equal_remote()
|
|
test_stale_habit_save_keeps_remote_habits()
|
|
test_duplicate_named_habits_merge()
|
|
test_session_last_write_wins()
|
|
test_linked_session_delete_syncs()
|
|
test_fresh_account_bootstraps_without_tombstones()
|
|
test_snapshot_with_pending_local_edits_merges()
|
|
test_tiny_pending_snapshot_merges()
|
|
test_local_habit_without_activity_does_not_force_review()
|
|
test_review_ignores_deleted_remote_rows()
|
|
test_remote_additions_apply_without_review()
|
|
test_apply_inside_an_outer_transaction()
|
|
test_snapshot_removes_absent_local_yoga()
|
|
test_snapshot_keeps_pending_yoga()
|
|
test_normal_response_records_server_hash()
|
|
test_social_cache_is_server_authored()
|
|
return failures
|
|
}
|