Android gives the app no writable temporary directory, and the debug and Play builds changed into the release package's files path, which failed and left the process in "/". Any SQLite statement journal or temporary table that spilled to a file failed with "disk I/O error", rolled back the whole transaction, and made every review merge fail, so the review window came back each time it closed. Temporary data now stays in memory on every connection, Android changes into the package's own files directory, a step failure logs SQLite's reason, and a review is merged or shown once per account snapshot instead of reopening right after it closes.
1860 lines
115 KiB
Ziran
1860 lines
115 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/app/locale.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/json.zi";
|
|
#import, file "../src/storage/storage_layout.zi";
|
|
#import "crypto"
|
|
#import "keys"
|
|
#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/storage/storage_habit_sync.zi";
|
|
#import, file "../src/habits/habit_model.zi";
|
|
#import, file "../src/habits/habit_calendar.zi";
|
|
#import, file "../src/habits/habit_schedule.zi";
|
|
#import, file "../src/cells/lumi_store.zi";
|
|
#import, file "../src/cells/diary_records.zi";
|
|
#import, file "../src/cells/package_files.zi";
|
|
#import, file "../src/storage/cell_records.zi";
|
|
#import, file "../src/storage/sync_nodes.zi";
|
|
#import, file "../src/storage/sync_node_storage.zi";
|
|
#import, file "../src/storage/sqlite.zi";
|
|
#import "json_scan"
|
|
Core :: #import, file "../src/storage/storage_core.zi";
|
|
|
|
test_node_cursors :: () {
|
|
root := make_root("sync-nodes")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
storage_set_setting_text("sync_server_url", "https://one.example")
|
|
check_true("save another node", SyncNodeAdd("https://two.example"))
|
|
check_true("migrate the original node", SyncNodeCount() == 2)
|
|
check_true("normalize duplicate node", SyncNodeAdd("https://two.example/"))
|
|
check_true("do not duplicate a node", SyncNodeCount() == 2)
|
|
storage_set_meta_int64("sync_last_server_version", 41)
|
|
storage_set_meta_int64("sync_server_clock", 12)
|
|
Core.set_meta_buf("sync_last_server_state_hash", "node-one-hash")
|
|
storage_set_setting_text("sync_auth_token", "old-node-token")
|
|
check_true("queue local diary", CellRecordWrite("cell.diary.file.2026-10-08.json", "pending-entry"))
|
|
check_true("switch node", SyncNodeActivate("https://two.example"))
|
|
check_true("new node starts with its own cursor", storage_meta_int64("sync_last_server_version", 0) == 0)
|
|
check_true("discard the old node session", storage_get_setting_text("sync_auth_token").count == 0)
|
|
check_true("keep queued diary data", storage_has_pending_sync_outbox() != 0)
|
|
storage_set_meta_int64("sync_last_server_version", 9)
|
|
check_true("return to original node", SyncNodeActivate("https://one.example"))
|
|
check_true("restore original node version", storage_meta_int64("sync_last_server_version", 0) == 41)
|
|
check_true("restore original node clock", storage_meta_int64("sync_server_clock", 0) == 12)
|
|
check_true("restore original node hash", storage_meta_text("sync_last_server_state_hash") == "node-one-hash")
|
|
check_true("choose a preferred node", SyncNodePrefer("https://one.example"))
|
|
check_true("fail over to another node", SyncNodeFailover())
|
|
check_true("failover restores the other node cursor", storage_meta_int64("sync_last_server_version", 0) == 9)
|
|
check_true("failover keeps the preferred node", storage_get_setting_text("sync_preferred_node") == "https://one.example")
|
|
check_true("prefer the other node", SyncNodePrefer("https://two.example"))
|
|
check_true("remove the active node", SyncNodeRemove(1))
|
|
check_true("keep the remaining node", SyncNodeCount() == 1)
|
|
check_true("replace a removed preference", storage_get_setting_text("sync_preferred_node") == "https://one.example")
|
|
check_true("keep one usable node", !SyncNodeRemove(0))
|
|
storage_set_setting_text("sync_public_id", "another-account")
|
|
storage_reset_sync_state()
|
|
check_true("add another account's node", SyncNodeAdd("https://two.example"))
|
|
check_true("switch another account's node", SyncNodeActivate("https://two.example"))
|
|
check_true("do not reuse another account's cursor", storage_meta_int64("sync_last_server_version", 0) == 0)
|
|
check_true("return with another account", SyncNodeActivate("https://one.example"))
|
|
check_true("keep account cursors separate", storage_meta_int64("sync_last_server_version", 0) == 0)
|
|
storage_close()
|
|
}
|
|
|
|
// Bytes a node stores for one Diary file in the payload, counted like
|
|
// Daochi's account storage: every stored field of each encrypted record.
|
|
payload_file_bytes :: (name: string, hash: string, chunks_only: bool) -> s64 {
|
|
records := records_array()
|
|
total: s64 = 0
|
|
count := ArrayCount(records, 0)
|
|
fields: [7]string = .["collection", "id", "key_id", "nonce", "ciphertext", "content_hash", "parent_id"]
|
|
chunk_prefix := join("cell.diary.chunk.", hash)
|
|
chunk_prefix = PackageText(chunk_prefix, "")
|
|
defer {
|
|
PackageFree(chunk_prefix)
|
|
}
|
|
entry := PackageText("cell.diary.file.", name)
|
|
defer {
|
|
PackageFree(entry)
|
|
}
|
|
for index: cast(s32)0..count - 1 {
|
|
record := ElementAt(records, 0, index)
|
|
id: [256]u8
|
|
id_length := ReadStringText(records, FieldAt(records, record, "id"), id[:])
|
|
if id_length <= 0 {
|
|
continue
|
|
}
|
|
identifier := TextFromBytes(id[:id_length])
|
|
chunk := identifier.count > chunk_prefix.count && identifier[:chunk_prefix.count] == chunk_prefix
|
|
if !chunk && (chunks_only || identifier != entry) {
|
|
continue
|
|
}
|
|
for field: fields {
|
|
value: [8192]u8
|
|
length := ReadStringText(records, FieldAt(records, record, field), value[:])
|
|
if length > 0 {
|
|
total += cast(s64)length
|
|
}
|
|
}
|
|
}
|
|
return total
|
|
}
|
|
|
|
queued_diary_chunks :: () -> s32 {
|
|
return read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='cell_record' AND entity_id GLOB 'cell.diary.chunk.*'")
|
|
}
|
|
|
|
format_bytes :: (bytes: s64) -> string {
|
|
output: [32]u8
|
|
if !SyncStorageFormatBytes(bytes, output[:]) {
|
|
return ""
|
|
}
|
|
return PackageText(TextFromCString(*output[0]), "")
|
|
}
|
|
|
|
// A node set not to store photos receives each Diary file entry and the day
|
|
// documents, never photo chunks. Reports, limits and the photo estimate.
|
|
test_node_storage_and_media :: () {
|
|
root := make_root("sync-node-media")
|
|
check_true("init media device", storage_init(root) != 0)
|
|
set_account()
|
|
storage_set_sync_enabled(1)
|
|
node := "https://api.waozi.xyz"
|
|
storage_set_setting_text("sync_server_url", node)
|
|
check_true("save diary day", DiaryRecordSave("2026-10-10.json", "{\"text\":\"Entry with a photo\"}"))
|
|
photo: [3000]u8
|
|
for index: cast(s32)0..cast(s32)2999 {
|
|
photo[index] = cast(u8)(index % 251)
|
|
}
|
|
check_true("save diary photo", DiaryRecordSave("photo-1-1.bin", TextFromBytes(photo[:])))
|
|
check_int("day and photo chunks queue", queued_diary_chunks(), 4)
|
|
check_true("photos upload by default", SyncNodeMediaUpload(node))
|
|
check_true("payload with photos builds", build_payload())
|
|
check_int("file entries and every chunk upload", record_count(), 6)
|
|
|
|
photo_hash := PackageText(read_db_text("SELECT json_extract(value,'$.hash') FROM settings WHERE key='cell.diary.file.photo-1-1.bin'"), "")
|
|
check_int("photo entry names its content", cast(s32)photo_hash.count, 64)
|
|
check_true("local photo bytes equal the node's chunk bytes",
|
|
SyncMediaLocalBytes() == payload_file_bytes("photo-1-1.bin", photo_hash, true))
|
|
estimate := SyncMediaNodeBytes(3000)
|
|
actual := payload_file_bytes("photo-1-1.bin", photo_hash, false)
|
|
check_true("photo estimate covers its chunks", estimate >= SyncMediaLocalBytes())
|
|
check_true("photo estimate stays within five percent of a node's bytes",
|
|
estimate * 100 <= actual * 105 && estimate * 100 >= actual * 95)
|
|
PackageFree(photo_hash)
|
|
|
|
check_true("turn photos off for the node", SyncNodeSetMediaUpload(node, false))
|
|
check_true("photo setting is stored", !SyncNodeMediaUpload("https://api.waozi.xyz/"))
|
|
check_true("other nodes keep photos on", SyncNodeMediaUpload("https://other.example"))
|
|
check_int("photo chunks leave the queue", queued_diary_chunks(), 1)
|
|
check_true("payload without photos builds", build_payload())
|
|
check_int("only entries and the day chunk upload", record_count(), 3)
|
|
check_true("photo entry still syncs", contains(payload(), "cell.diary.file.photo-1-1.bin"))
|
|
storage_enqueue_all_sync_state()
|
|
check_true("backfill payload builds", build_payload())
|
|
check_int("a backfill does not send photos", record_count(), 3)
|
|
check_true("photo room is unlimited without photo upload", SyncNodeMediaFits(100000000).fits)
|
|
check_true("turn photos back on", SyncNodeSetMediaUpload(node, true))
|
|
check_int("photos queue again", queued_diary_chunks(), 4)
|
|
|
|
check_true("unknown usage before a sync", !SyncNodeStorageRead(node).known)
|
|
check_true("unknown limit leaves room", SyncNodeMediaFits(100000000).fits)
|
|
SyncNodeStorageRecordResponse(node, "{\"status\":\"ok\",\"account_storage\":{\"used_bytes\":9437184,\"limit_bytes\":10485760}}")
|
|
storage := SyncNodeStorageRead("https://api.waozi.xyz/")
|
|
check_true("usage is known", storage.known && !storage.full)
|
|
check_true("usage is read", storage.used_bytes == 9437184 && storage.limit_bytes == 10485760)
|
|
check_int("usage percent", SyncNodeStoragePercent(storage), 90)
|
|
check_true("ninety percent is nearly full", SyncNodeStorageNearlyFull(storage))
|
|
check_true("a small photo fits", SyncNodeMediaFits(3000).fits)
|
|
room := SyncNodeMediaFits(1000000)
|
|
check_true("a large photo does not fit", !room.fits)
|
|
check_true("free space counts queued photos",
|
|
room.free_bytes == 10485760 - 9437184 - SyncMediaPendingBytes())
|
|
check_true("other 413 replies are not storage full",
|
|
!SyncNodeStorageRecordFull(node, "{\"error\":\"encrypted payload quota exceeded\"}"))
|
|
check_true("storage full is recorded",
|
|
SyncNodeStorageRecordFull(node, "{\"error\":\"account storage limit reached\",\"code\":\"storage_full\",\"account_storage\":{\"used_bytes\":10485000,\"limit_bytes\":10485760}}"))
|
|
storage = SyncNodeStorageRead(node)
|
|
check_true("full node keeps its usage", storage.full && storage.used_bytes == 10485000)
|
|
SyncNodeStorageRecordResponse(node, "{\"status\":\"ok\"}")
|
|
check_true("a reply without usage keeps the report", SyncNodeStorageRead(node).full)
|
|
SyncNodeStorageRecordResponse(node, "{\"status\":\"ok\",\"account_storage\":{\"used_bytes\":2048}}")
|
|
storage = SyncNodeStorageRead(node)
|
|
check_true("a sync clears full", !storage.full && storage.used_bytes == 2048)
|
|
check_true("an unlimited node has no limit", storage.limit_bytes == 0 && !SyncNodeStorageNearlyFull(storage))
|
|
storage_set_setting_text("sync_public_id", "another-account")
|
|
check_true("reports belong to one account", !SyncNodeStorageRead(node).known)
|
|
check_true("photo settings belong to one account", SyncNodeMediaUpload(node))
|
|
storage_close()
|
|
|
|
sizes: [6]s64 = .[0, 1023, 1536, 5557452, 10485760, 157286400]
|
|
expected: [6]string = .["0 B", "1023 B", "2 KB", "5.3 MB", "10 MB", "150 MB"]
|
|
for index: cast(s32)0..cast(s32)5 {
|
|
formatted := format_bytes(sizes[index])
|
|
check_true("storage size reads naturally", formatted == expected[index])
|
|
PackageFree(formatted)
|
|
}
|
|
check_true("node names drop the scheme", SyncNodeDisplayName("https://api.waozi.xyz/") == "api.waozi.xyz")
|
|
}
|
|
|
|
c :: #system_library "c";
|
|
sqlite_free :: (memory: *void) #foreign c "free";
|
|
ReviewFieldFailure :: (enabled: s32) #foreign c;
|
|
TestAllocate :: (size: usize) -> *void #foreign c "malloc";
|
|
|
|
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: string;
|
|
|
|
// 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 }
|
|
PackageFree(payload_text)
|
|
payload_text = PackageText(TextFromCString(payload), "")
|
|
storage_free_sync_payload_json(payload)
|
|
return true
|
|
}
|
|
|
|
payload :: () -> string {
|
|
return payload_text
|
|
}
|
|
|
|
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: StorageJsonBuilder;
|
|
|
|
// A v6 server response carrying `records` for a device to apply.
|
|
response_with :: (version: s32, records: string) -> string {
|
|
part: [24]u8
|
|
storage_json_builder_free(*response_buffer)
|
|
response_buffer = StorageJsonBuilder.{}
|
|
response_buffer.ok = 1
|
|
storage_json_builder_append_text(*response_buffer, "{\"protocol_version\":6,\"status\":\"ok\",\"server_version\":")
|
|
storage_json_builder_append_text(*response_buffer, FormatInteger(cast(s64)version, part[:]))
|
|
storage_json_builder_append_text(*response_buffer, ",\"server_clock\":")
|
|
storage_json_builder_append_text(*response_buffer, FormatInteger(cast(s64)version, part[:]))
|
|
storage_json_builder_append_text(*response_buffer, ",\"changes\":{\"encrypted_records\":")
|
|
storage_json_builder_append_text(*response_buffer, records)
|
|
storage_json_builder_append_text(*response_buffer, "}}")
|
|
return AppTextFromCString(response_buffer.data, cast(s32)(response_buffer.len + 1))
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
// Android gives the app no writable temporary directory: a merge whose
|
|
// statement journal spilled to a file failed with "disk I/O error" and rolled
|
|
// back the whole review. The storage connection keeps temporary data in memory.
|
|
test_temporary_data_in_memory :: () {
|
|
root := make_root("temp-store")
|
|
check_true("init db", storage_init(root) != 0)
|
|
statement: *void = null
|
|
store: s32 = -1
|
|
if SqlitePrepareText(storage_db_handle(), "PRAGMA temp_store", -1, *statement, null) == SQLITE_OK &&
|
|
SqliteStep(statement) == SQLITE_ROW {
|
|
store = SqliteColumnInt(statement, 0)
|
|
}
|
|
SqliteFinalize(statement)
|
|
check_true("temporary data stays in memory", store == 2)
|
|
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()
|
|
}
|
|
|
|
// A device whose records arrived in an older app version, which kept only
|
|
// the fields it knew, downloads every record again once after updating.
|
|
test_older_records_download_again_once :: () {
|
|
root := make_root("sync-record-fields")
|
|
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');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_habit_name_repair_v1_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_zero_habit_day_repair_v1_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_record_fields','1');")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
check_true("payload builds", build_payload())
|
|
check_true("older records are downloaded again", contains(payload(), "\"since_server_version\":0"))
|
|
check_true("apply the full download", storage_apply_sync_response_json(empty_response(530)) != 0)
|
|
check_true("payload builds again", build_payload())
|
|
check_true("the full download runs once", contains(payload(), "\"since_server_version\":530"))
|
|
storage_close()
|
|
}
|
|
|
|
// An updated device downloads every record before it sends its changes, so
|
|
// a record it got without some fields never goes out without them.
|
|
test_updated_device_downloads_before_sending :: () {
|
|
root := make_root("sync-download-first")
|
|
check_true("init db", storage_init(root) != 0)
|
|
set_account()
|
|
local: Habits
|
|
check_true("add habit", habits_add_custom(*local, "Read", Green(), 0, 0) == 0)
|
|
habits_save(*local)
|
|
habits_free(*local)
|
|
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');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_habit_name_repair_v1_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_zero_habit_day_repair_v1_done','1');INSERT OR REPLACE INTO meta(key,value) VALUES('sync_record_fields','1');")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
check_true("payload builds", build_payload())
|
|
check_true("the download comes first", contains(payload(), "\"since_server_version\":0"))
|
|
check_int("nothing is sent before the download", record_count(), 0)
|
|
check_true("apply the download", storage_apply_sync_response_json(empty_response(530)) != 0)
|
|
check_true("payload builds again", build_payload())
|
|
check_true("the change goes out after it", record_count() > 0)
|
|
storage_close()
|
|
}
|
|
|
|
// A habit an older version received without some settings gets them from
|
|
// the account's record during the download, even where it looks newer.
|
|
test_updated_device_fills_missing_settings :: () {
|
|
first := make_root("sync-fill-a")
|
|
second := make_root("sync-fill-b")
|
|
check_true("init device a", storage_init(first) != 0)
|
|
set_account()
|
|
source: Habits
|
|
check_true("add habit", habits_add_custom(*source, "Cold Shower", Green(), 0, 0) == 0)
|
|
source.items[0].icon = 17
|
|
weekly: HabitSchedule
|
|
weekly.repeat = HABIT_REPEAT_WEEKLY
|
|
weekly.weekdays = 1 + 4 + 8
|
|
check_true("weekly schedule", HabitScheduleSet(*source.items[0], weekly, 20260620, false))
|
|
habits_save(*source)
|
|
habits_free(*source)
|
|
check_true("device a payload", build_payload())
|
|
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)
|
|
storage_close()
|
|
// As an older version kept it: without the icon or schedule, and edited
|
|
// later, so the account's record looks older.
|
|
raw_exec(second, "UPDATE habits SET icon=0,schedule='',updated_at=4102444800 WHERE name='Cold Shower';INSERT OR REPLACE INTO meta(key,value) VALUES('sync_record_fields','1');")
|
|
check_true("reopen device b", storage_init(second) != 0)
|
|
check_true("device b downloads again", storage_apply_sync_response_json(response_with(6, records)) != 0)
|
|
filled: Habits
|
|
check_true("device b loads", storage_habits_load(cast(*void)*filled) != 0)
|
|
habit := find_habit(*filled, "Cold Shower")
|
|
check_true("device b has the habit", habit != null)
|
|
check_int("the icon is filled", habit.icon, 17)
|
|
check_true("the schedule is filled", habit.schedule_count > 0)
|
|
if habit.schedule_count > 0 {
|
|
check_int("the weekly days are back", habit.schedules[habit.schedule_count - 1].weekdays, 13)
|
|
}
|
|
habits_free(*filled)
|
|
storage_close()
|
|
}
|
|
|
|
// A record's fields this version doesn't read, such as ones a newer version
|
|
// added, go back out with it under this version's own changes, so an older
|
|
// version never removes them for the account's other devices.
|
|
test_unknown_record_fields_go_back_out :: () {
|
|
first := make_root("sync-fields-a")
|
|
second := make_root("sync-fields-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)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("device a payload", build_payload())
|
|
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)
|
|
storage_close()
|
|
check_int("device b keeps the record as it arrived",
|
|
raw_count(second, "SELECT COUNT(*) FROM sync_record_copies WHERE collection='private.inbe.v1.habits'"), 1)
|
|
// A field from a newer version that this one doesn't read.
|
|
raw_exec(second, "UPDATE sync_record_copies SET json=json_set(json,'$.future_field','kept') WHERE collection='private.inbe.v1.habits'")
|
|
check_true("reopen device b", storage_init(second) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("device b loads", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
habits_b.items[0].icon = 23
|
|
habits_save(*habits_b)
|
|
habits_free(*habits_b)
|
|
check_true("device b payload", build_payload())
|
|
storage_close()
|
|
check_int("the change goes out with the field it doesn't know",
|
|
raw_count(second, "SELECT COUNT(*) FROM sync_record_copies WHERE collection='private.inbe.v1.habits' AND json_extract(json,'$.future_field')='kept' AND json_extract(json,'$.icon')=23"), 1)
|
|
}
|
|
|
|
// 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_a.items[0].icon = 17
|
|
// A schedule changed after the recorded day, then a formula.
|
|
weekly: HabitSchedule
|
|
weekly.repeat = HABIT_REPEAT_WEEKLY
|
|
weekly.weekdays = 1 + 4 + 8
|
|
check_true("weekly schedule", HabitScheduleSet(*habits_a.items[0], weekly, 20260620, false))
|
|
formula: HabitSchedule
|
|
formula.repeat = HABIT_REPEAT_FORMULA
|
|
check_true("formula fits", HabitScheduleSetFormula(*formula, "days % 3 = 0 and weekday < 6"))
|
|
check_true("formula schedule", HabitScheduleSet(*habits_a.items[0], formula, 20260625, false))
|
|
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)
|
|
check_int("device b has the habit's icon", habit.icon, 17)
|
|
check_int("device b has every schedule", habit.schedule_count, 3)
|
|
check_int("the weekly schedule starts on its day", habit.schedules[1].from, 20260620)
|
|
check_int("the weekly schedule's days", habit.schedules[1].weekdays, 13)
|
|
check_true("the formula arrives whole",
|
|
HabitScheduleFormulaText(*habit.schedules[2]) == "days % 3 = 0 and weekday < 6")
|
|
check_int("the recorded day keeps its schedule", habit_scheduled_day(habit, 20260618), 1)
|
|
check_int("an off day under the weekly schedule", habit_scheduled_day(habit, 20260623), 0)
|
|
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;INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) SELECT 'habit_entry',id,0,1782300000 FROM habit_entries;")
|
|
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 = "{\"protocol_version\":6,\"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()
|
|
}
|
|
|
|
// The preview rehearses encrypted v6 application, then returns with the
|
|
// snapshot, local records, metadata and pending uploads still intact.
|
|
test_review_preview_and_choices :: () {
|
|
remote_seed: TextBuffer
|
|
append(*remote_seed, REMOTE_SQL)
|
|
append(*remote_seed, "INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('remote-habit',20260905,0,0,0,1782290000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('remote-habit',20260906,0,3,0,1782290000);")
|
|
append(*remote_seed, "INSERT INTO elist_lists VALUES('remote-review-list',(SELECT id FROM users LIMIT 1),'Remote list',0,0,1782290000);INSERT INTO elist_items VALUES('remote-review-item',(SELECT id FROM users LIMIT 1),'remote-review-list','Remote item','',0,0,0,1782290000);")
|
|
remote_records("review-preview-source", TextFromCString(*remote_seed.bytes[0]))
|
|
seed: TextBuffer
|
|
append(*seed, LOCAL_SQL)
|
|
append(*seed, "INSERT INTO elist_lists VALUES('local-review-list',(SELECT id FROM users LIMIT 1),'Local list',0,0,1782300000);INSERT INTO elist_items VALUES('local-review-item',(SELECT id FROM users LIMIT 1),'local-review-list','Local item','',0,0,0,1782300000);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,1782200000);INSERT INTO session_rounds VALUES('remote-session',0,25);")
|
|
root := local_device("review-preview-local", TextFromCString(*seed.bytes[0]), LOCAL_PENDING_SQL)
|
|
raw_exec(root, "INSERT INTO meditation_logs VALUES('local-review-meditation',(SELECT id FROM users LIMIT 1),'local-session',77,1782300000,1782300000);")
|
|
snapshot := snapshot_response(24, "review-preview-hash")
|
|
check_true("save review snapshot", storage_sync_review_write_json(snapshot) != 0)
|
|
storage_set_meta_int64(STORAGE_SYNC_PENDING_REVIEW_KEY, 1)
|
|
before_outbox := read_db_count("SELECT COUNT(*) FROM sync_outbox")
|
|
before_meta := read_db_count("SELECT COUNT(*) FROM meta")
|
|
state := storage_state_handle().*
|
|
plan: *u8 = null
|
|
check_true("merge preview", storage_sync_review_plan_for_json(snapshot, 2, *plan) != 0)
|
|
text := TextFromCString(plan)
|
|
check_true("encrypted habit appears in grouped preview", contains(text, "Remote Breath"))
|
|
check_true("changed session shows before and after", contains(text, "25 → 55"))
|
|
check_true("encrypted list and item appear in grouped preview", contains(text, "Remote list") && contains(text, "Remote item"))
|
|
check_true("merge leaves local-only habit out of changes", !contains(text, "Local Breath"))
|
|
check_true("empty incomplete days are not additions", !contains(text, "2026-09-05"))
|
|
check_true("incomplete days with real progress remain visible", contains(text, "2026-09-06"))
|
|
check_true("habit dates are readable", !contains(text, "20260624"))
|
|
sqlite_free(cast(*void)plan)
|
|
plan = null
|
|
check_true("replace preview", storage_sync_review_plan_for_json(snapshot, 1, *plan) != 0)
|
|
check_true("replacement explicitly includes cascading local removals", contains(TextFromCString(plan), "Local Breath") &&
|
|
contains(TextFromCString(plan), "Local list") && contains(TextFromCString(plan), "Local item"))
|
|
check_true("replacement includes stored meditation duration", contains(TextFromCString(plan), "77 s"))
|
|
sqlite_free(cast(*void)plan)
|
|
check_int("preview kept local habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 1)
|
|
check_int("preview kept stored meditation", read_db_count("SELECT COUNT(*) FROM meditation_logs WHERE id='local-review-meditation'"), 1)
|
|
check_int("preview did not add remote habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='remote-habit'"), 0)
|
|
check_int("preview kept outbox", read_db_count("SELECT COUNT(*) FROM sync_outbox"), before_outbox)
|
|
check_int("preview kept metadata", read_db_count("SELECT COUNT(*) FROM meta"), before_meta)
|
|
check_true("preview kept storage state", storage_state_handle().pending_sync_outbox_seq == state.pending_sync_outbox_seq &&
|
|
storage_state_handle().last_sync_changed == state.last_sync_changed)
|
|
check_true("preview kept pending review", storage_sync_review_pending() != 0)
|
|
plan = null
|
|
check_true("invalid encrypted preview is rejected", storage_sync_review_plan_for_json(
|
|
"{\"protocol_version\":6,\"changes\":{\"encrypted_records\":[{\"collection\":\"private.inbe.v1.sessions\",\"id\":\"bad\",\"key_id\":\"bad\",\"nonce\":\"bad\",\"ciphertext\":\"bad\"}]}}", 2, *plan) == 0)
|
|
check_true("failed preview returned no partial text", plan == null)
|
|
check_int("failed preview kept local habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 1)
|
|
ReviewFieldFailure(1)
|
|
check_true("failed field preview is rejected", storage_sync_review_plan_for_json(snapshot, 2, *plan) == 0)
|
|
check_true("failed field preview returned no partial text", plan == null)
|
|
ReviewFieldFailure(0)
|
|
sqlite_free(cast(*void)plan)
|
|
plan = null
|
|
check_int("failed field preview kept local habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 1)
|
|
check_true("merge applies", storage_apply_pending_sync_review(2) != 0)
|
|
check_int("merge retains local session", read_db_count("SELECT COUNT(*) FROM sessions WHERE id='local-session'"), 1)
|
|
check_int("merge retains stored meditation", read_db_count("SELECT COUNT(*) FROM meditation_logs WHERE id='local-review-meditation'"), 1)
|
|
check_int("merge adds remote session", read_db_count("SELECT COUNT(*) FROM sessions WHERE id='remote-session'"), 1)
|
|
check_int("merge retains local habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 1)
|
|
check_int("merge adds remote habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='remote-habit'"), 1)
|
|
check_int("merge retains local pending records", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id IN ('local-habit','local-session')"), 3)
|
|
check_int("merge retains local list and item", read_db_count("SELECT COUNT(*) FROM elist_items WHERE id='local-review-item' AND list_id='local-review-list'"), 1)
|
|
check_true("merge clears pending review", storage_sync_review_pending() == 0)
|
|
check_true("keep test saves review", storage_sync_review_write_json(snapshot) != 0)
|
|
storage_set_meta_int64(STORAGE_SYNC_PENDING_REVIEW_KEY, 1)
|
|
before_outbox = read_db_count("SELECT COUNT(*) FROM sync_outbox")
|
|
check_true("keep applies", storage_apply_pending_sync_review(0) != 0)
|
|
check_int("keep retains pending uploads", read_db_count("SELECT COUNT(*) FROM sync_outbox"), before_outbox)
|
|
check_int("keep retains local records", read_db_count("SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 1)
|
|
check_true("replace test saves review", storage_sync_review_write_json(snapshot) != 0)
|
|
check_true("replace applies", storage_apply_pending_sync_review(1) != 0)
|
|
check_int("replacement removes local habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='local-habit'"), 0)
|
|
check_int("replacement removes stored meditation", read_db_count("SELECT COUNT(*) FROM meditation_logs WHERE id='local-review-meditation'"), 0)
|
|
check_int("replacement retains remote habit", read_db_count("SELECT COUNT(*) FROM habits WHERE id='remote-habit'"), 1)
|
|
check_int("replacement removes absent local list", read_db_count("SELECT COUNT(*) FROM elist_lists WHERE id='local-review-list'"), 0)
|
|
check_int("replacement removes absent local item", read_db_count("SELECT COUNT(*) FROM elist_items WHERE id='local-review-item'"), 0)
|
|
check_int("replacement restores remote list and item", read_db_count("SELECT COUNT(*) FROM elist_items WHERE id='remote-review-item' AND list_id='remote-review-list'"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
test_review_social_differences :: () {
|
|
root := make_root("review-social-differences")
|
|
check_true("init social review db", storage_init(root) != 0)
|
|
set_account()
|
|
check_true("seed friend list", storage_set_social_cache_json("friends.list",
|
|
"{\"friends\":[{\"user_id_hash\":\"friend-a\",\"alias\":\"Alice\",\"updated_at\":1},{\"user_id_hash\":\"friend-b\",\"alias\":\"Bob\"}]}") != 0)
|
|
check_true("seed empty requests", storage_set_social_cache_json("friends.requests",
|
|
"{ \"outgoing\": [], \"incoming\": [] }") != 0)
|
|
equivalent := "{\"protocol_version\":6,\"changes\":{\"encrypted_records\":[],\"social_cache\":[{\"kind\":\"friends.list\",\"json\":{\"friends\":[{\"alias\":\"Bob\",\"user_id_hash\":\"friend-b\"},{\"updated_at\":2,\"alias\":\"Alice\",\"user_id_hash\":\"friend-a\"}]}},{\"kind\":\"friends.requests\",\"json\":{\"incoming\":[],\"outgoing\":[]}}]}}"
|
|
plan: *u8 = null
|
|
check_true("equivalent social preview", storage_sync_review_plan_for_json(equivalent, 2, *plan) != 0)
|
|
check_true("formatting, ordering and cache metadata are not changes",
|
|
TextFromCString(plan) == LocaleText("review_unchanged"))
|
|
check_true("equivalent social data does not trigger review", storage_sync_review_json_has_visible_diff(equivalent) == 0)
|
|
sqlite_free(cast(*void)plan)
|
|
plan = null
|
|
changed := "{\"protocol_version\":6,\"changes\":{\"encrypted_records\":[],\"social_cache\":[{\"kind\":\"friends.list\",\"json\":{\"friends\":[{\"user_id_hash\":\"friend-a\",\"alias\":\"Alicia\"},{\"user_id_hash\":\"friend-c\",\"alias\":\"Cara\"}]}},{\"kind\":\"friends.requests\",\"json\":{\"incoming\":[{\"id\":\"request-d\",\"requester_user_id_hash\":\"friend-d\",\"requester_alias\":\"Dani\"}],\"outgoing\":[{\"id\":\"request-e\",\"target_user_id_hash\":\"friend-e\",\"target_alias\":\"Eli\"}]}}]}}"
|
|
check_true("changed social preview", storage_sync_review_plan_for_json(changed, 2, *plan) != 0)
|
|
check_true("real social changes remain reviewable", storage_sync_review_json_has_visible_diff(changed) != 0)
|
|
text := TextFromCString(plan)
|
|
check_true("friend name change shows the exact values", contains(text, "Alice → Alicia"))
|
|
check_true("friend additions and removals name the person", contains(text, "Cara") && contains(text, "Bob"))
|
|
check_true("requests name their sender or recipient", contains(text, "Dani") && contains(text, "Eli"))
|
|
check_true("social review has no raw JSON or internal cache names",
|
|
!contains(text, "user_id_hash") && !contains(text, "friends.list") &&
|
|
!contains(text, "friends.requests") && !contains(text, "{\""))
|
|
sqlite_free(cast(*void)plan)
|
|
cached: [512]u8
|
|
check_true("preview leaves friend data intact", storage_get_social_cache_json("friends.list", *cached[0], 512) != 0 &&
|
|
contains(TextFromCString(*cached[0]), "Alice") && !contains(TextFromCString(*cached[0]), "Cara"))
|
|
storage_close()
|
|
}
|
|
|
|
test_review_no_changes_resolves_automatically :: () {
|
|
remote_seed: TextBuffer
|
|
append(*remote_seed, LOCAL_SQL)
|
|
append(*remote_seed, "UPDATE sessions SET activity=3;INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('local-habit',20260905,0,0,0,1782290000);")
|
|
remote_records("review-no-changes-source", TextFromCString(*remote_seed.bytes[0]))
|
|
seed: TextBuffer
|
|
append(*seed, LOCAL_SQL)
|
|
append(*seed, "UPDATE sessions SET activity=3;INSERT INTO elist_lists VALUES('local-only-list',(SELECT id FROM users LIMIT 1),'Keep this list',0,0,1782300000);")
|
|
unused local_device("review-no-changes-local", TextFromCString(*seed.bytes[0]), "DELETE FROM sync_outbox;")
|
|
snapshot := snapshot_response(55, "no-changes-receipt")
|
|
plan: *u8 = null
|
|
check_true("no-op preview succeeds", storage_sync_review_plan_for_json(snapshot, 2, *plan) != 0)
|
|
check_true("no-op preview and trigger agree", TextFromCString(plan) == LocaleText("review_unchanged") &&
|
|
storage_sync_review_json_has_visible_diff(snapshot) == 0)
|
|
sqlite_free(cast(*void)plan)
|
|
check_true("incoming no-op snapshot applies", storage_apply_sync_response_json(snapshot) != 0)
|
|
check_true("incoming no-op snapshot needs no review", storage_sync_review_pending() == 0)
|
|
check_int("no-op merge keeps local-only records", read_db_count("SELECT COUNT(*) FROM elist_lists WHERE id='local-only-list'"), 1)
|
|
check_int("no-op sync records its receipt", cast(s32)storage_meta_int64("sync_last_server_version", 0), 55)
|
|
check_true("no-op sync records its hash", storage_meta_text("sync_last_server_state_hash") == "no-changes-receipt")
|
|
storage_enqueue_all_sync_state()
|
|
mark_pending_payload_in_flight()
|
|
queued := read_db_count("SELECT COUNT(*) FROM sync_outbox")
|
|
check_true("fixture has pending local uploads", queued > 0)
|
|
pending_snapshot := snapshot_response(56, "resolved-no-changes-receipt")
|
|
padding: s64 = 70000
|
|
large := cast(*u8)TestAllocate(cast(usize)(padding + pending_snapshot.count + 1))
|
|
check_true("allocate a large review snapshot", large != null)
|
|
if large == null {
|
|
storage_close()
|
|
return
|
|
}
|
|
index: s64 = 0
|
|
while index < padding {
|
|
large[index] = cast(u8)32
|
|
index += 1
|
|
}
|
|
index = 0
|
|
while index < pending_snapshot.count {
|
|
large[padding + index] = pending_snapshot[index]
|
|
index += 1
|
|
}
|
|
large[padding + pending_snapshot.count] = cast(u8)0
|
|
check_true("save an older pending review larger than 64 KiB", storage_sync_review_write_json(TextFromCString(large)) != 0)
|
|
sqlite_free(cast(*void)large)
|
|
storage_set_meta_int64(STORAGE_SYNC_PENDING_REVIEW_KEY, 1)
|
|
check_true("large review is compared in full", storage_sync_review_has_visible_diff() == 0)
|
|
check_true("pending no-op review resolves", storage_sync_review_clear_if_no_visible_diff() != 0)
|
|
check_true("pending no-op review is cleared", storage_sync_review_pending() == 0)
|
|
check_int("pending no-op review preserves uploads", read_db_count("SELECT COUNT(*) FROM sync_outbox"), queued)
|
|
check_int("pending no-op review advances receipt", cast(s32)storage_meta_int64("sync_last_server_version", 0), 56)
|
|
check_int("pending no-op review keeps local-only data", read_db_count("SELECT COUNT(*) FROM elist_lists WHERE id='local-only-list'"), 1)
|
|
invalid := "{\"protocol_version\":6,\"changes\":{\"encrypted_records\":[{\"collection\":\"private.inbe.v1.sessions\",\"id\":\"bad\",\"key_id\":\"bad\",\"nonce\":\"bad\",\"ciphertext\":\"bad\"}]}}"
|
|
check_true("unverified data is not treated as a no-op", storage_sync_review_json_has_visible_diff(invalid) != 0)
|
|
check_true("save unverified review", storage_sync_review_write_json(invalid) != 0)
|
|
storage_set_meta_int64(STORAGE_SYNC_PENDING_REVIEW_KEY, 1)
|
|
check_true("unverified review is not silently cleared", storage_sync_review_clear_if_no_visible_diff() == 0 &&
|
|
storage_sync_review_pending() != 0)
|
|
storage_close()
|
|
}
|
|
|
|
// A snapshot that waits for review merges by itself: local records stay,
|
|
// remote records arrive, and its receipt advances. A device with Patterns
|
|
// sessions and other changes on the server waits for one.
|
|
test_waiting_review_merges :: () {
|
|
remote_records("review-waiting-remote", REMOTE_SQL)
|
|
seed: TextBuffer
|
|
append(*seed, LOCAL_SQL)
|
|
append(*seed, "UPDATE sessions SET activity=3;")
|
|
root := local_device("review-waiting-local", TextFromCString(*seed.bytes[0]), "DELETE FROM sync_outbox;")
|
|
check_true("apply the snapshot", storage_apply_sync_response_json(snapshot_response(58, "waiting-receipt")) != 0)
|
|
check_true("snapshot waits for review", storage_sync_review_pending() != 0)
|
|
check_true("waiting review merges", storage_sync_review_merge_pending() != 0)
|
|
check_true("merged review is cleared", 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("merge records its receipt", cast(s32)storage_meta_int64("sync_last_server_version", 0), 58)
|
|
check_true("nothing waiting merges nothing", storage_sync_review_merge_pending() == 0)
|
|
invalid := "{\"protocol_version\":6,\"changes\":{\"encrypted_records\":[{\"collection\":\"private.inbe.v1.sessions\",\"id\":\"bad\",\"key_id\":\"bad\",\"nonce\":\"bad\",\"ciphertext\":\"bad\"}]}}"
|
|
check_true("save unreadable review", storage_sync_review_write_json(invalid) != 0)
|
|
storage_set_meta_int64(STORAGE_SYNC_PENDING_REVIEW_KEY, 1)
|
|
check_true("unreadable review is not merged", storage_sync_review_merge_pending() == 0 &&
|
|
storage_sync_review_pending() != 0)
|
|
storage_close()
|
|
}
|
|
|
|
// A habit's questions and its entries reach another device. A habit
|
|
// record from an app before questions keeps them; an older entry does not
|
|
// replace a newer one, and a deletion reaches the other device.
|
|
test_questions_and_entries_sync :: () {
|
|
remote_records("questions-remote", "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,questions,span) VALUES('meal',(SELECT id FROM users LIMIT 1),'First meal',1,2,3,0,0,0,1,0,1782290000,'[{\"id\":7,\"type\":\"time\",\"text\":\"Started eating\",\"goal\":\"at_most\",\"low\":0,\"high\":480}]',1);INSERT INTO habit_entries(id,user_id,habit_id,local_date,started_at,ended_at,answers,deleted_at,updated_at) VALUES('entry-1',(SELECT id FROM users LIMIT 1),'meal',20260624,1782290000,1782300000,'[{\"id\":7,\"type\":\"time\",\"question\":\"Started eating\",\"value\":\"07:30\"}]',0,1782290000);")
|
|
root := make_root("questions-local")
|
|
check_true("init local device", storage_init(root) != 0)
|
|
set_account()
|
|
check_true("apply the remote records",
|
|
storage_apply_sync_response_json(snapshot_response(20, "questions-hash")) != 0)
|
|
check_int("questions and goals arrive with the habit", count_of(root,
|
|
"SELECT COUNT(*) FROM habits WHERE id='meal' AND json_extract(questions,'$[0].text')='Started eating' AND json_extract(questions,'$[0].goal')='at_most' AND span=1"), 1)
|
|
check_int("the entry arrives with its answers", count_of(root,
|
|
"SELECT COUNT(*) FROM habit_entries WHERE id='entry-1' AND habit_id='meal' AND local_date=20260624 AND started_at=1782290000 AND ended_at=1782300000 AND json_extract(answers,'$[0].value')='07:30' AND deleted_at=0"), 1)
|
|
older: string = "{\"protocol_version\":6,\"changes\":{\"habits\":[{\"id\":\"meal\",\"name\":\"First meal\",\"color_r\":1,\"color_g\":2,\"color_b\":3,\"sync_mode\":0,\"sync_activity\":0,\"counter_enabled\":false,\"counter_target\":1,\"sort_order\":1,\"deleted_at\":0,\"updated_at\":\"2026-06-30T00:00:00Z\"}]}}"
|
|
// Decrypted records go through the same apply.
|
|
check_true("apply an older app's habit", storage_apply_sync_habits_json(older) != 0)
|
|
check_int("an older app's habit keeps the questions and span", count_of(root,
|
|
"SELECT COUNT(*) FROM habits WHERE id='meal' AND json_extract(questions,'$[0].text')='Started eating' AND span=1 AND updated_at>1782290000"), 1)
|
|
stale: string = "{\"changes\":{\"habit_entries\":[{\"id\":\"entry-1\",\"habit_id\":\"meal\",\"local_date\":20260624,\"started_at\":1,\"ended_at\":2,\"answers\":\"\",\"deleted_at\":0,\"updated_at\":\"2026-06-01T00:00:00Z\"}]}}"
|
|
check_true("apply an older entry", storage_apply_sync_habit_entries_json(stale) != 0)
|
|
check_int("an older entry does not replace a newer one", count_of(root,
|
|
"SELECT COUNT(*) FROM habit_entries WHERE id='entry-1' AND started_at=1782290000"), 1)
|
|
removed: string = "{\"changes\":{\"habit_entries\":[{\"id\":\"entry-1\",\"habit_id\":\"meal\",\"local_date\":20260624,\"started_at\":1782290000,\"ended_at\":1782300000,\"answers\":\"\",\"deleted_at\":1783000000,\"updated_at\":\"2026-07-04T00:00:00Z\"}]}}"
|
|
check_true("apply a deletion", storage_apply_sync_habit_entries_json(removed) != 0)
|
|
check_int("the deletion reaches this device", count_of(root,
|
|
"SELECT COUNT(*) FROM habit_entries WHERE id='entry-1' AND deleted_at>0"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
// Whether the last payload uploads the record with id as a deletion: 1 it
|
|
// does, 0 it keeps it, -1 it has no such record.
|
|
record_deleted :: (record_id: string) -> s32 {
|
|
count := record_count()
|
|
index: s32 = 0
|
|
while index < count {
|
|
path: TextBuffer
|
|
append(*path, "$.encrypted_records[")
|
|
number: [16]u8
|
|
append(*path, FormatInteger(cast(s64)index, number[:]))
|
|
append(*path, "].")
|
|
id_path: TextBuffer
|
|
append(*id_path, TextFromCString(*path.bytes[0]))
|
|
append(*id_path, "id")
|
|
found: [96]u8
|
|
if storage_json_extract_text(payload(), TextFromCString(*id_path.bytes[0]),
|
|
*found[0], 96) != 0 && TextFromCString(*found[0]) == record_id {
|
|
deleted_path: TextBuffer
|
|
append(*deleted_path, TextFromCString(*path.bytes[0]))
|
|
append(*deleted_path, "deleted_at")
|
|
return ifx storage_json_extract_int64(payload(),
|
|
TextFromCString(*deleted_path.bytes[0]), 0) > 0 then 1 else 0
|
|
}
|
|
index += 1
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// A list, an item and an entry that are kept upload as kept; only a
|
|
// deleted entry uploads as a deletion.
|
|
test_kept_records_upload_kept :: () {
|
|
unused local_device("kept-upload", "INSERT INTO elist_lists(id,user_id,title,sort_order,deleted_at,updated_at) VALUES('list-kept',(SELECT id FROM users LIMIT 1),'Kept',0,0,1782300000);INSERT INTO elist_items(id,user_id,list_id,title,comment,done,sort_order,deleted_at,updated_at) VALUES('item-kept',(SELECT id FROM users LIMIT 1),'list-kept','Milk','',0,0,0,1782300000);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('meal',(SELECT id FROM users LIMIT 1),'First meal',1,2,3,0,0,0,1,0,1782300000);INSERT INTO habit_entries(id,user_id,habit_id,local_date,started_at,ended_at,answers,deleted_at,updated_at) VALUES('entry-kept',(SELECT id FROM users LIMIT 1),'meal',20260624,1782300000,1782300000,'',0,1782300000),('entry-gone',(SELECT id FROM users LIMIT 1),'meal',20260624,1782300100,1782300100,'',1782300500,1782300500);",
|
|
"INSERT OR REPLACE INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('elist_list','list-kept',0,1782300000),('elist_item','item-kept',0,1782300000),('habit_entry','entry-kept',0,1782300000),('habit_entry','entry-gone',0,1782300500);")
|
|
check_true("payload builds", build_payload())
|
|
check_int("a kept list uploads as kept", record_deleted("list-kept"), 0)
|
|
check_int("a kept item uploads as kept", record_deleted("item-kept"), 0)
|
|
check_int("a kept entry uploads as kept", record_deleted("entry-kept"), 0)
|
|
check_int("a deleted entry uploads as deleted", record_deleted("entry-gone"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
test_review_completion_changes :: () {
|
|
remote_seed: TextBuffer
|
|
append(*remote_seed, REMOTE_SQL)
|
|
append(*remote_seed, "DELETE FROM sessions;UPDATE habit_days SET completed=0,count=0,session_count=0;INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('remote-habit',20260905,0,0,0,1782290000);")
|
|
remote_records("review-completion-source", TextFromCString(*remote_seed.bytes[0]))
|
|
seed: TextBuffer
|
|
append(*seed, REMOTE_SQL)
|
|
append(*seed, "DELETE FROM sessions;INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('remote-habit',20260904,0,0,0,1782290000);")
|
|
unused local_device("review-completion-local", TextFromCString(*seed.bytes[0]), "")
|
|
snapshot := snapshot_response(24, "review-completion-hash")
|
|
plan: *u8 = null
|
|
check_true("completion replacement preview", storage_sync_review_plan_for_json(snapshot, 1, *plan) != 0)
|
|
text := TextFromCString(plan)
|
|
check_true("completion change is explicit", contains(text, "Completed → Not completed"))
|
|
check_true("new empty days are not additions", !contains(text, "2026-09-05"))
|
|
check_true("missing empty days are not removals", !contains(text, "2026-09-04"))
|
|
check_true("completion is not displayed as a toggle", !contains(text, "OFF") && !contains(text, "ON"))
|
|
sqlite_free(cast(*void)plan)
|
|
check_int("preview preserved completed day", read_db_count("SELECT completed FROM habit_days WHERE habit_id='remote-habit' AND local_date=20260624"), 1)
|
|
storage_close()
|
|
}
|
|
|
|
// Real encrypted fixtures for the historical owner collection and isolated
|
|
// Lumi collection. They use the same account seed on two synthetic devices.
|
|
cell_record_id_fixture :: (collection: string, key_id: string, record_id: string, cell_key: string) -> string {
|
|
key: [32]u8
|
|
fixture: StorageJsonBuilder
|
|
fixture.ok = 1
|
|
if storage_private_collection_key(collection, key_id, *key[0]) == 0 {
|
|
return ""
|
|
}
|
|
storage_json_builder_append_text(*fixture, "[")
|
|
ok := storage_append_encrypted_record_json(*fixture, *key[0], collection,
|
|
key_id, record_id, "cell_record", cell_key, 0, now_seconds(), 0)
|
|
SecureWipe(*key[0], 32)
|
|
storage_json_builder_append_text(*fixture, "]")
|
|
result: string = ""
|
|
if ok != 0 && fixture.ok != 0 {
|
|
result = PackageText(AppTextFromCString(fixture.data, cast(s32)(fixture.len + 1)), "")
|
|
}
|
|
storage_json_builder_free(*fixture)
|
|
return result
|
|
}
|
|
|
|
cell_record_fixture :: (collection: string, key_id: string, cell_key: string) -> string {
|
|
return cell_record_id_fixture(collection, key_id, cell_key, cell_key)
|
|
}
|
|
|
|
test_cell_record_identity :: () {
|
|
source := make_root("sync-cell-identity-source")
|
|
target := make_root("sync-cell-identity-target")
|
|
outer := "cell.lumi.message.outer"
|
|
inner := "cell.lumi.message.inner"
|
|
check_true("init record identity source", storage_init(source) != 0)
|
|
set_account()
|
|
check_true("seed encrypted cell identity fixture", CellRecordWrite(inner, "Bound history"))
|
|
wrong_lumi := cell_record_id_fixture(LumiPrivateCollection, LumiPrivateKeyID, outer, inner)
|
|
wrong_legacy := cell_record_id_fixture(LegacyPrivateCellsCollection, STORAGE_SYNC_RECORD_KEY_ID, outer, inner)
|
|
correct_lumi := cell_record_fixture(LumiPrivateCollection, LumiPrivateKeyID, inner)
|
|
defer {
|
|
PackageFree(wrong_lumi)
|
|
PackageFree(wrong_legacy)
|
|
PackageFree(correct_lumi)
|
|
}
|
|
storage_close()
|
|
check_true("init record identity target", storage_init(target) != 0)
|
|
set_account()
|
|
check_true("authenticated Lumi outer ID cannot write different embedded key",
|
|
storage_apply_sync_response_json(response_with(1, wrong_lumi)) == 0)
|
|
check_true("authenticated legacy outer ID cannot write different embedded key",
|
|
storage_apply_sync_response_json(response_with(1, wrong_legacy)) == 0)
|
|
check_int("identity mismatch changes no cell", read_db_count("SELECT COUNT(*) FROM settings WHERE key GLOB 'cell.*'"), 0)
|
|
check_int("identity mismatch queues no record", read_db_count("SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
check_int("identity mismatch preserves cursor", cast(s32)storage_meta_int64("sync_last_server_version", 0), 0)
|
|
check_true("matching authenticated ID still restores",
|
|
storage_apply_sync_response_json(response_with(1, correct_lumi)) != 0)
|
|
check_true("matching record data survives", storage_get_setting_text(inner) == "Bound history")
|
|
storage_close()
|
|
}
|
|
|
|
test_cell_plaintext_single_record :: () {
|
|
root := make_root("sync-cell-single-record")
|
|
check_true("init single cell record fixture", storage_init(root) != 0)
|
|
set_account()
|
|
record_id := "cell.lumi.message.bound"
|
|
cases: [11]string = .[
|
|
"{\"key\":\"cell.lumi.message.other\",\"value\":\"wrong key\",\"updated_at\":\"2026-10-07T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":\"one\",\"updated_at\":\"2026-10-07T00:00:00Z\"},{\"key\":\"cell.lumi.message.other\",\"value\":\"two\",\"updated_at\":\"2026-10-07T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"key\":\"cell.lumi.message.other\",\"value\":\"duplicate key\",\"updated_at\":\"2026-10-07T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"\\u006bey\":\"cell.lumi.message.other\",\"value\":\"escaped duplicate key\",\"updated_at\":\"2026-10-07T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":\"one\",\"value\":\"two\",\"updated_at\":\"2026-10-07T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":\"timestamp ambiguity\",\"updated_at\":\"2026-10-07T00:00:00Z\",\"updated_at\":\"2026-10-08T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":{\"nested\":\"not a string\"},\"updated_at\":\"2026-10-07T00:00:00Z\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":\"missing timestamp\"}",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":\"unknown field\",\"updated_at\":\"2026-10-07T00:00:00Z\",\"extra\":1}",
|
|
"[{\"key\":\"cell.lumi.message.bound\",\"value\":\"nested record\",\"updated_at\":\"2026-10-07T00:00:00Z\"}]",
|
|
"{\"key\":\"cell.lumi.message.bound\",\"value\":\"trailing object\",\"updated_at\":\"2026-10-07T00:00:00Z\"}{}"
|
|
]
|
|
for plaintext: cases {
|
|
check_true("malformed single-cell plaintext rejected before storage",
|
|
storage_apply_decrypted_record(LumiPrivateCollection, record_id, plaintext) == 0)
|
|
}
|
|
check_true("cell record requires authenticated outer ID",
|
|
storage_apply_decrypted_record(LumiPrivateCollection, "", "{\"key\":\"cell.lumi.message.bound\",\"value\":\"unbound\",\"updated_at\":\"2026-10-07T00:00:00Z\"}") == 0)
|
|
check_true("matching outer ID cannot admit Diary to Lumi",
|
|
storage_apply_decrypted_record(LumiPrivateCollection, "cell.diary.file.bound", "{\"key\":\"cell.diary.file.bound\",\"value\":\"wrong scope\",\"updated_at\":\"2026-10-07T00:00:00Z\"}") == 0)
|
|
check_int("malformed plaintext changes no cell", read_db_count("SELECT COUNT(*) FROM settings WHERE key GLOB 'cell.*'"), 0)
|
|
check_int("malformed plaintext queues no record", read_db_count("SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
check_true("one matching cell record remains valid",
|
|
storage_apply_decrypted_record(LumiPrivateCollection, record_id, "{\"key\":\"cell.lumi.message.bound\",\"value\":\"one valid record\",\"updated_at\":\"2026-10-07T00:00:00Z\"}") != 0)
|
|
check_true("valid single record data applied", storage_get_setting_text(record_id) == "one valid record")
|
|
storage_close()
|
|
}
|
|
|
|
test_lumi_collection_keys :: () {
|
|
root := make_root("sync-lumi-keys")
|
|
check_true("init isolated key device", storage_init(root) != 0)
|
|
legacy: [32]u8
|
|
lumi: [32]u8
|
|
legacy_hex: [65]u8
|
|
lumi_hex: [65]u8
|
|
check_true("Lumi key needs owner material",
|
|
storage_private_collection_key(LumiPrivateCollection, LumiPrivateKeyID, *lumi[0]) == 0)
|
|
set_account()
|
|
check_true("legacy cell key remains available to owner",
|
|
storage_private_collection_key(LegacyPrivateCellsCollection, STORAGE_SYNC_RECORD_KEY_ID, *legacy[0]) != 0)
|
|
check_true("separate Lumi key is available to owner",
|
|
storage_private_collection_key(LumiPrivateCollection, LumiPrivateKeyID, *lumi[0]) != 0)
|
|
unused BytesToHex(*legacy[0], 32, *legacy_hex[0], 65)
|
|
unused BytesToHex(*lumi[0], 32, *lumi_hex[0], 65)
|
|
check_true("Lumi key differs from global legacy key",
|
|
TextFromCString(*legacy_hex[0]) != TextFromCString(*lumi_hex[0]))
|
|
check_true("legacy generation is rejected for Lumi",
|
|
storage_private_collection_key(LumiPrivateCollection, STORAGE_SYNC_RECORD_KEY_ID, *lumi[0]) == 0)
|
|
check_true("Lumi generation is rejected for old cells",
|
|
storage_private_collection_key(LegacyPrivateCellsCollection, LumiPrivateKeyID, *legacy[0]) == 0)
|
|
check_true("Lumi generation is rejected for session history",
|
|
storage_private_collection_key("private.inbe.v1.sessions", LumiPrivateKeyID, *legacy[0]) == 0)
|
|
check_true("unapproved generation is rejected",
|
|
storage_private_collection_key(LumiPrivateCollection, "inbe-lumi-2", *lumi[0]) == 0)
|
|
check_true("legacy session key remains available",
|
|
storage_private_collection_key("private.inbe.v1.sessions", STORAGE_SYNC_RECORD_KEY_ID, *legacy[0]) != 0)
|
|
SecureWipe(*legacy[0], 32)
|
|
SecureWipe(*lumi[0], 32)
|
|
SecureWipe(*legacy_hex[0], 65)
|
|
SecureWipe(*lumi_hex[0], 65)
|
|
storage_close()
|
|
}
|
|
|
|
test_lumi_collection_migration :: () {
|
|
root := make_root("sync-lumi-older-layout")
|
|
check_true("init old collection fixture", storage_init(root) != 0)
|
|
set_account()
|
|
check_true("seed old Lumi history", CellRecordWrite("cell.lumi.message.old", "{\"text\":\"Earlier chat\",\"time\":1,\"user\":1}"))
|
|
check_true("seed old Diary history", CellRecordWrite("cell.diary.file.old", "Earlier diary"))
|
|
check_true("seed old feedback history", CellRecordWrite("cell.feedback.message.old", "Earlier feedback"))
|
|
storage_set_setting_text("lumi_pending_action", "preserved pending command")
|
|
storage_close()
|
|
// This fixture has the pre-upgrade sync layout: the v1 upload/backfill
|
|
// markers are complete and Lumi has no v2 marker or queued copy.
|
|
raw_exec(root, "DELETE FROM sync_outbox;DELETE FROM meta WHERE key='sync_lumi_collection_v2_done';INSERT OR REPLACE INTO meta(key,value) VALUES('sync_full_upload_done','1'),('sync_backfill_v2_done','1');INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) VALUES('cell_record','cell.diary.file.old',0,123);INSERT INTO settings(user_id,key,value,updated_at) SELECT id,'cell.lumi.INVALID','ignored malformed old key',1 FROM users LIMIT 1;")
|
|
check_true("upgrade old collection fixture", storage_init(root) != 0)
|
|
check_int("Lumi migration marker saved", cast(s32)storage_meta_int64(LumiCollectionMigrationKey, 0), 1)
|
|
check_int("only valid old Lumi history queued", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id GLOB 'cell.lumi.*'"), 1)
|
|
check_int("existing Diary pending upload preserved", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id='cell.diary.file.old' AND queued_at=123"), 1)
|
|
check_true("old Lumi plaintext remains intact", storage_get_setting_text("cell.lumi.message.old") == "{\"text\":\"Earlier chat\",\"time\":1,\"user\":1}")
|
|
LumiStoreReset()
|
|
check_int("old conversation remains loadable after migration", LumiStoreCount(), 1)
|
|
check_true("old conversation text remains loadable", LumiStoreRow(0).text == "Earlier chat")
|
|
check_true("old Diary remains intact", storage_get_setting_text("cell.diary.file.old") == "Earlier diary")
|
|
check_true("old feedback remains intact", storage_get_setting_text("cell.feedback.message.old") == "Earlier feedback")
|
|
check_true("pending command remains intact", storage_get_setting_text("lumi_pending_action") == "preserved pending command")
|
|
queued_seq := read_db_count("SELECT MAX(seq) FROM sync_outbox")
|
|
check_true("new-key copies can be uploaded", build_payload())
|
|
check_true("migration payload uses Lumi v2", contains(payload(), "private.inbe.v2.lumi") && contains(payload(), "inbe-lumi-1"))
|
|
check_true("migration keeps Diary in owner collection", contains(payload(), "private.inbe.v1.cells") && contains(payload(), "breathing-main-1"))
|
|
storage_close()
|
|
check_true("restart migrated device", storage_init(root) != 0)
|
|
LumiStoreReset()
|
|
check_int("old conversation remains loadable after restart", LumiStoreCount(), 1)
|
|
check_int("restart does not change queued copies", read_db_count("SELECT MAX(seq) FROM sync_outbox"), queued_seq)
|
|
check_int("restart does not duplicate migration", read_db_count("SELECT COUNT(*) FROM sync_outbox"), 2)
|
|
exec_db_sql("DELETE FROM sync_outbox;")
|
|
storage_close()
|
|
check_true("restart after migration acknowledgement", storage_init(root) != 0)
|
|
check_int("acknowledged migration stays complete", read_db_count("SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_lumi_collection_migration_rollback :: () {
|
|
root := make_root("sync-lumi-migration-rollback")
|
|
check_true("init interrupted migration fixture", storage_init(root) != 0)
|
|
set_account()
|
|
check_true("seed interrupted old Lumi", CellRecordWrite("cell.lumi.message.interrupted", "Older history"))
|
|
exec_db_sql("DELETE FROM sync_outbox;CREATE TRIGGER block_lumi_marker BEFORE INSERT ON meta WHEN NEW.key='sync_lumi_collection_v2_done' BEGIN SELECT RAISE(ABORT,'synthetic marker failure');END;")
|
|
check_true("migration reports marker failure", storage_migrate_lumi_collection() == 0)
|
|
check_int("failed migration rolls back queued copies", read_db_count("SELECT COUNT(*) FROM sync_outbox"), 0)
|
|
check_int("failed migration does not save marker", cast(s32)storage_meta_int64(LumiCollectionMigrationKey, 0), 0)
|
|
check_true("failed migration retains old history", storage_get_setting_text("cell.lumi.message.interrupted") == "Older history")
|
|
exec_db_sql("DROP TRIGGER block_lumi_marker;")
|
|
storage_close()
|
|
check_true("restart retries interrupted migration", storage_init(root) != 0)
|
|
check_int("retried migration queues one copy", read_db_count("SELECT COUNT(*) FROM sync_outbox"), 1)
|
|
check_int("retried migration saves marker", cast(s32)storage_meta_int64(LumiCollectionMigrationKey, 0), 1)
|
|
storage_close()
|
|
}
|
|
|
|
test_legacy_cells_and_lumi_scope :: () {
|
|
first := make_root("sync-lumi-legacy-source")
|
|
second := make_root("sync-lumi-legacy-restore")
|
|
check_true("init old encrypted record source", storage_init(first) != 0)
|
|
set_account()
|
|
check_true("seed historical combined Lumi", CellRecordWrite("cell.lumi.message.historical", "Historical chat"))
|
|
check_true("seed historical combined Diary", CellRecordWrite("cell.diary.file.historical", "Historical diary"))
|
|
check_true("seed historical combined feedback", CellRecordWrite("cell.feedback.message.historical", "Historical feedback"))
|
|
legacy_lumi := cell_record_fixture(LegacyPrivateCellsCollection, STORAGE_SYNC_RECORD_KEY_ID, "cell.lumi.message.historical")
|
|
legacy_diary := cell_record_fixture(LegacyPrivateCellsCollection, STORAGE_SYNC_RECORD_KEY_ID, "cell.diary.file.historical")
|
|
legacy_feedback := cell_record_fixture(LegacyPrivateCellsCollection, STORAGE_SYNC_RECORD_KEY_ID, "cell.feedback.message.historical")
|
|
forbidden_diary := cell_record_fixture(LumiPrivateCollection, LumiPrivateKeyID, "cell.diary.file.historical")
|
|
forbidden_feedback := cell_record_fixture(LumiPrivateCollection, LumiPrivateKeyID, "cell.feedback.message.historical")
|
|
defer {
|
|
PackageFree(legacy_lumi)
|
|
PackageFree(legacy_diary)
|
|
PackageFree(legacy_feedback)
|
|
PackageFree(forbidden_diary)
|
|
PackageFree(forbidden_feedback)
|
|
}
|
|
storage_close()
|
|
check_true("init historical restore device", storage_init(second) != 0)
|
|
set_account()
|
|
check_true("legacy global-key Lumi remains decryptable", storage_apply_sync_response_json(response_with(1, legacy_lumi)) != 0)
|
|
check_true("legacy global-key Diary remains decryptable", storage_apply_sync_response_json(response_with(2, legacy_diary)) != 0)
|
|
check_true("legacy global-key feedback remains decryptable", storage_apply_sync_response_json(response_with(3, legacy_feedback)) != 0)
|
|
check_true("legacy Lumi restored intact", storage_get_setting_text("cell.lumi.message.historical") == "Historical chat")
|
|
check_true("legacy Diary restored intact", storage_get_setting_text("cell.diary.file.historical") == "Historical diary")
|
|
check_true("legacy feedback restored intact", storage_get_setting_text("cell.feedback.message.historical") == "Historical feedback")
|
|
check_int("late legacy Lumi queues a new-key copy", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id='cell.lumi.message.historical'"), 1)
|
|
check_true("migration payload after legacy restore builds", build_payload())
|
|
check_true("legacy Lumi reuploads only to its isolated collection", contains(payload(), "private.inbe.v2.lumi") && !contains(payload(), "private.inbe.v1.cells"))
|
|
check_true("legacy replay acknowledges the migrated copy", storage_apply_sync_response_json(response_with(4, legacy_lumi)) != 0)
|
|
check_int("unchanged legacy replay does not requeue Lumi", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id='cell.lumi.message.historical'"), 0)
|
|
check_true("repeated legacy snapshot stays decryptable", storage_apply_sync_response_json(response_with(5, legacy_lumi)) != 0)
|
|
check_int("repeated legacy snapshot leaves upload queue empty", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id='cell.lumi.message.historical'"), 0)
|
|
check_true("legacy replay preserves chat history", storage_get_setting_text("cell.lumi.message.historical") == "Historical chat")
|
|
exec_db_sql("UPDATE settings SET value='Older local history',updated_at=1 WHERE key='cell.lumi.message.historical'")
|
|
check_true("changed legacy history still merges", storage_apply_sync_response_json(response_with(6, legacy_lumi)) != 0)
|
|
check_true("changed legacy history restored intact", storage_get_setting_text("cell.lumi.message.historical") == "Historical chat")
|
|
check_int("changed legacy history still queues its isolated copy", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id='cell.lumi.message.historical'"), 1)
|
|
check_true("updated migration payload builds", build_payload())
|
|
check_true("updated legacy copy is acknowledged", storage_apply_sync_response_json(response_with(7, legacy_lumi)) != 0)
|
|
check_int("acknowledged updated legacy copy is not requeued", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_id='cell.lumi.message.historical'"), 0)
|
|
check_true("cryptographically valid Diary is rejected in Lumi collection", storage_apply_sync_response_json(response_with(8, forbidden_diary)) == 0)
|
|
check_true("cryptographically valid feedback is rejected in Lumi collection", storage_apply_sync_response_json(response_with(8, forbidden_feedback)) == 0)
|
|
check_int("rejected foreign records do not advance sync cursor", cast(s32)storage_meta_int64("sync_last_server_version", 0), 7)
|
|
storage_close()
|
|
}
|
|
|
|
test_cell_records_round_trip :: () {
|
|
first := make_root("sync-cells-a")
|
|
second := make_root("sync-cells-b")
|
|
check_true("init cell device a", storage_init(first) != 0)
|
|
set_account()
|
|
LumiStoreReset()
|
|
LumiStoreAppend("Private conversation 水", true)
|
|
LumiStoreAppend("Private reply", false)
|
|
check_true("save private diary", DiaryRecordSave("2026-10-07.json", "{\"text\":\"Private diary 水\"}"))
|
|
photo: [4097]u8
|
|
for index: cast(s32)0..cast(s32)4096 {
|
|
photo[index] = cast(u8)(index % 256)
|
|
}
|
|
check_true("save binary diary photo", DiaryRecordSave("photo-test.png", TextFromBytes(photo[:])))
|
|
check_true("cell payload builds", build_payload())
|
|
check_true("Lumi uses separate encrypted collection", contains(payload(), "private.inbe.v2.lumi"))
|
|
check_true("Diary retains legacy owner collection", contains(payload(), "private.inbe.v1.cells"))
|
|
check_true("private chat is absent on wire", !contains(payload(), "Private conversation"))
|
|
check_true("private diary is absent on wire", !contains(payload(), "Private diary"))
|
|
records := records_array()
|
|
check_true("photo fixture exceeds the old bounded payload buffer", payload().count > 16384)
|
|
check_true("full cell payload is valid JSON", storage_json_valid(payload()) != 0)
|
|
check_true("cell record array is valid JSON", storage_json_valid(records) != 0)
|
|
storage_close()
|
|
|
|
check_true("init cell device b", storage_init(second) != 0)
|
|
set_account()
|
|
response := response_with(5, records)
|
|
check_true("full cell response is valid JSON", storage_json_valid(response) != 0)
|
|
check_true("encrypted cell restore", storage_apply_sync_response_json(response) != 0)
|
|
LumiStoreReset()
|
|
check_int("conversation restored", LumiStoreCount(), 2)
|
|
check_true("chat text survives", LumiStoreRow(0).text == "Private conversation 水")
|
|
diary := DiaryRecordRead("2026-10-07.json")
|
|
check_true("diary text survives", diary == "{\"text\":\"Private diary 水\"}")
|
|
PackageFree(diary)
|
|
restored := DiaryRecordRead("photo-test.png")
|
|
check_true("binary photo survives", restored == TextFromBytes(photo[:]))
|
|
PackageFree(restored)
|
|
check_int("restore does not requeue cells", read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='cell_record'"), 0)
|
|
check_true("cell collection cannot overwrite signing key",
|
|
storage_apply_decrypted_record("private.inbe.v1.cells", "sync_private_key", "{\"key\":\"sync_private_key\",\"value\":\"forged\",\"updated_at\":\"2026-10-07T00:00:00Z\"}") == 0)
|
|
storage_close()
|
|
}
|
|
|
|
#program_export
|
|
Check :: () -> s32 {
|
|
guard := support_begin("/tmp/inbe-storage-sync-zi-test/")
|
|
if guard != 0 { return guard }
|
|
test_node_cursors()
|
|
test_node_storage_and_media()
|
|
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_temporary_data_in_memory()
|
|
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_older_records_download_again_once()
|
|
test_unknown_record_fields_go_back_out()
|
|
test_updated_device_downloads_before_sending()
|
|
test_updated_device_fills_missing_settings()
|
|
test_records_round_trip_between_devices()
|
|
test_cell_records_round_trip()
|
|
test_cell_record_identity()
|
|
test_cell_plaintext_single_record()
|
|
test_lumi_collection_keys()
|
|
test_lumi_collection_migration()
|
|
test_lumi_collection_migration_rollback()
|
|
test_legacy_cells_and_lumi_scope()
|
|
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()
|
|
test_review_preview_and_choices()
|
|
test_review_social_differences()
|
|
test_review_completion_changes()
|
|
test_review_no_changes_resolves_automatically()
|
|
test_waiting_review_merges()
|
|
test_questions_and_entries_sync()
|
|
test_kept_records_upload_kept()
|
|
return failures
|
|
}
|