Files
breeze/tests/sync_account_behavior.zi
T
waoziandClaude Sonnet 5.5 6c84ebe030 Fix sync and import regressions from the Ziran port; port storage tests to Ziran
Found by running the old storage tests as Ziran behaviour tests against real SQLite: encrypted habit/habit-day/session/list records were serialised without most fields (malformed JSON, so habits could not sync between devices); the remote habit apply SQL was truncated to 2048 bytes; check-in import queries lacked their SELECT prefix. Adds sync round-trip, payload, habit and import tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-09-28 19:02:49 -03:00

463 lines
22 KiB
Ziran

#import "byte_text_linux"
#import, file "storage_test_support.zi";
#import, file "../src/app/text_buffers.zi";
#import, file "../src/storage/data.zi";
#import, file "../src/storage/state.zi";
#import, file "../src/storage/db.zi";
#import, file "../src/storage/session_mood.zi";
#import, file "../src/storage/storage_core.zi";
#import, file "../src/storage/storage_sessions.zi";
#import, file "../src/habits/habit_types.zi";
#import, file "../src/habits/habit_memory.zi";
#import, file "../src/storage/storage_habits.zi";
#import, file "../src/habits/habit_model.zi";
#import, file "../src/storage/sync_account.zi";
#import, file "../src/storage/account_types.zi";
#import, file "../src/storage/sync_account_host.zi";
c :: #system_library "c";
sleep_microseconds :: (microseconds: u32) -> s32 #foreign c "usleep";
WorkerFunction :: #type (argument: *void) -> *void #c_call;
TestThreadCreate :: (thread: *u64, attributes: *void, function: WorkerFunction,
argument: *void) -> s32 #foreign c "pthread_create";
TestThreadJoin :: (thread: u64, result: **void) -> s32 #foreign c "pthread_join";
key_path_one: [600]u8;
key_path_two: [600]u8;
key_path: [600]u8;
export_path: [600]u8;
hex_digit :: (value: s32) -> u8 {
if value < 10 { return cast(u8)(48 + value) }
return cast(u8)(87 + value)
}
// Deterministic key material: bytes i * mul + add (+ variant).
make_hex :: (output: []u8, count: s32, multiplier: s32, offset: s32) {
index: s32 = 0
while index < count {
byte: s32 = (index * multiplier + offset) % 256
output[index * 2] = hex_digit(byte / 16)
output[index * 2 + 1] = hex_digit(byte % 16)
index += 1
}
output[count * 2] = cast(u8)0
}
AccountValues :: struct {
public_id: [65]u8
public_key: [2625]u8
private_key: [5121]u8
}
make_account :: (values: *AccountValues, variant: s32) {
raw: [1312]u8
index: s32 = 0
while index < 1312 {
raw[index] = cast(u8)((index * 31 + 7 + variant) % 256)
index += 1
}
sync_sha256_hex(*raw[0], 1312, *values.public_id[0])
make_hex(values.public_key[:], 1312, 31, 7 + variant)
make_hex(values.private_key[:], 2560, 17, 3 + variant * 3)
}
write_key_file :: (path: *u8, values: *AccountValues, id: string) {
text: TextBuffer
append(*text, "breathing-sync-key-v1\nalgorithm=ML-DSA-44\npublic_id=")
append(*text, id)
append(*text, "\npublic_key=")
append(*text, TextFromCString(*values.public_key[0]))
append(*text, "\nprivate_key=")
append(*text, TextFromCString(*values.private_key[0]))
append(*text, "\n")
check_true("write key file", write_text_file(path, *text))
}
account_text :: (field: *u8) -> string {
return TextFromCString(field)
}
import_key_and_save :: (account: *SyncAccount, path: *u8, clear_local_data: SyncAccountLocalDataChoice) -> SyncAccountSaveResult {
if sync_account_import_private_key_preview(account, path) == 0 {
return SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_FAILED
}
return sync_account_save(account, clear_local_data)
}
read_short_setting :: (argument: *void) -> *void {
ok: *s32 = cast(*s32)argument
ok.* = 0
if storage_get_setting_text("sync_enabled") == "1" { ok.* = 1 }
return null
}
begin_and_commit :: (argument: *void) -> *void {
result: *s32 = cast(*s32)argument
ok: s32 = exec_sql("BEGIN IMMEDIATE")
if ok != 0 { ok = exec_sql("COMMIT") }
result.* = ok
return null
}
test_parallel :: () {
root := make_root("account")
check_true("open parallel storage", storage_init(root) != 0)
key: [5121]u8
index: s32 = 0
while index < 5120 {
key[index] = cast(u8)97
index += 1
}
key[5120] = cast(u8)0
key_text := TextFromCString(*key[0])
storage_set_setting_text("sync_private_key", key_text)
storage_set_setting_text("sync_enabled", "1")
main_value := storage_get_setting_text("sync_private_key")
check_true("main thread reads full recovery key", main_value.count == 5120)
result: s32 = 0
worker: u64 = 0
started := TestThreadCreate(*worker, null, read_short_setting, cast(*void)*result) == 0
check_true("start parallel setting read", started)
if started { unused TestThreadJoin(worker, null) }
check_true("parallel setting read succeeds", result == 1)
check_true("parallel read preserves recovery key buffer", main_value == key_text)
check_true("main transaction begins", exec_sql("BEGIN IMMEDIATE") != 0)
result = 0
started = TestThreadCreate(*worker, null, begin_and_commit, cast(*void)*result) == 0
check_true("start parallel transaction", started)
sleep_microseconds(20000)
check_true("main transaction commits", exec_sql("COMMIT") != 0)
if started { unused TestThreadJoin(worker, null) }
check_true("parallel transaction waits and commits", result == 1)
storage_close()
remove_tree(root)
}
test_import_export_clear :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
make_account(*values, 0)
path_in(key_path[:], root, "breathing-sync.key")
path_in(export_path[:], root, "exported.key")
id := TextFromCString(*values.public_id[0])
write_key_file(*key_path[0], *values, id)
check_true("init storage", storage_init(root) != 0)
storage_set_setting_text("sync_account_alias", "old-alias")
check_true("import key", import_key_and_save(*account, *key_path[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("import public id", account_text(*account.public_id[0]) == id)
check_true("import clears stale alias", storage_get_setting_text("sync_account_alias").count == 0)
check_true("load imported key", sync_account_load(*account) != 0)
check_true("load public key",
account_text(*account.public_key_hex[0]) == TextFromCString(*values.public_key[0]))
check_true("load private key",
account_text(*account.private_key_hex[0]) == TextFromCString(*values.private_key[0]))
check_true("export key", sync_account_export_private_key(*account, *export_path[0]) != 0)
exported := host_load_file_text(*export_path[0])
check_true("read exported key", exported != null)
if exported != null {
text := TextFromCString(exported)
check_true("export uses generic key header", starts_with(text, "account-key-v1\n"))
check_true("export includes public key", contains(text, "\npublic_key="))
host_unload_file_text(exported)
}
check_true("clear key", sync_account_clear() != 0)
check_true("load after clear", sync_account_load(*account) == 0)
check_true("import exported key", import_key_and_save(*account, *export_path[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("reimport public id", account_text(*account.public_id[0]) == id)
text: TextBuffer
append(*text, "breathing-sync-key-v1\nalgorithm=ML-DSA-44\npublic_key=")
append(*text, TextFromCString(*values.public_key[0]))
append(*text, "\nsecret_key=")
append(*text, TextFromCString(*values.private_key[0]))
append(*text, "\n")
check_true("write secret key alias file", write_text_file(*key_path[0], *text))
check_true("import secret key alias", import_key_and_save(*account, *key_path[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("derive public id", account_text(*account.public_id[0]) == id)
json: TextBuffer
append(*json, "{\"exported_key\":\"breathing-sync-key-v1\\nalgorithm=ML-DSA-44\\npublic_id=")
append(*json, id)
append(*json, "\\npublic_key=")
append(*json, TextFromCString(*values.public_key[0]))
append(*json, "\\nprivate_key=")
append(*json, TextFromCString(*values.private_key[0]))
append(*json, "\\n\"}\n")
check_true("write exported key json file", write_text_file(*key_path[0], *json))
check_true("import exported key json", import_key_and_save(*account, *key_path[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("json public key",
account_text(*account.public_key_hex[0]) == TextFromCString(*values.public_key[0]))
storage_close()
remove_tree(root)
}
test_reject_invalid :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
make_account(*values, 0)
path_in(key_path[:], root, "breathing-sync.key")
check_true("init invalid storage", storage_init(root) != 0)
// Change the first character so the id no longer matches the key.
if values.public_id[0] == cast(u8)48 { values.public_id[0] = cast(u8)49 } else { values.public_id[0] = cast(u8)48 }
write_key_file(*key_path[0], *values, TextFromCString(*values.public_id[0]))
check_true("reject mismatched public id",
sync_account_import_private_key_preview(*account, *key_path[0]) == 0)
text: TextBuffer
append(*text, "breathing-sync-key-v1\nalgorithm=ML-DSA-44\npublic_id=")
append(*text, TextFromCString(*values.public_id[0]))
append(*text, "\nprivate_key=")
append(*text, TextFromCString(*values.private_key[0]))
append(*text, "\n")
check_true("write missing public key file", write_text_file(*key_path[0], *text))
check_true("reject missing public key",
sync_account_import_private_key_preview(*account, *key_path[0]) == 0)
check_true("no account after rejected imports", sync_account_load(*account) == 0)
storage_close()
remove_tree(root)
}
// Opens storage holding an imported account, with the server URL set.
prepare_account :: (root: string, values: *AccountValues, account: *SyncAccount) {
make_account(values, 0)
path_in(key_path[:], root, "breathing-sync.key")
write_key_file(*key_path[0], values, TextFromCString(*values.public_id[0]))
check_true("init storage", storage_init(root) != 0)
check_true("save account", import_key_and_save(account, *key_path[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
}
test_legacy_connected :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
prepare_account(root, *values, *account)
storage_set_setting_text("sync_server_url", "https://api.waozi.xyz")
exec_db_sql(join(join("INSERT OR REPLACE INTO meta(key,value) VALUES('sync_last_server_version','6638');", "INSERT OR REPLACE INTO meta(key,value) VALUES('sync_full_upload_done','1');"), "DELETE FROM settings WHERE key IN ('sync_server_connected','sync_enabled');"))
storage_close()
check_true("reopen legacy storage", storage_init(root) != 0)
check_true("legacy connected flag migrated", storage_get_setting_int("sync_enabled", 0) == 1)
check_true("legacy connected helper", storage_sync_enabled() != 0)
storage_close()
remove_tree(root)
}
test_unsynced_not_migrated :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
prepare_account(root, *values, *account)
storage_set_setting_text("sync_server_url", "https://api.waozi.xyz")
exec_db_sql("DELETE FROM settings WHERE key IN ('sync_server_connected','sync_enabled');")
storage_close()
check_true("reopen unsynced storage", storage_init(root) != 0)
check_true("unsynced account remains disconnected", storage_get_setting_int("sync_enabled", 0) == 0)
check_true("unsynced account helper disconnected", storage_sync_enabled() == 0)
storage_close()
remove_tree(root)
}
test_disconnected_queue :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
status: StorageSyncStatus
prepare_account(root, *values, *account)
storage_set_setting_text("sync_server_url", "https://api.waozi.xyz")
storage_set_sync_enabled(0)
exec_db_sql(join("INSERT INTO sync_outbox(entity_type,entity_id,local_date,queued_at) ", "VALUES('session','queued-local-session',0,strftime('%s','now'));"))
check_true("disconnected server helper", storage_sync_enabled() == 0)
check_true("status loads", storage_sync_status(*status) != 0)
check_true("status has account", status.has_account != 0)
check_true("status not connected", status.enabled == 0)
check_true("queued count visible", status.queued_changes == 1)
check_true("queued row preserved", read_db_count("SELECT COUNT(*) FROM sync_outbox") == 1)
storage_set_sync_enabled(1)
storage_set_setting_int("sync_last_result", 6)
storage_set_setting_int("sync_retry_attempt", 3)
check_true("same key save succeeds", sync_account_save(*account,
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("same key preserves retry", storage_get_setting_int("sync_retry_attempt", 0) == 3)
check_true("same key preserves enabled sync", storage_sync_enabled() != 0)
check_true("same key status", storage_sync_status(*status) != 0)
storage_close()
check_true("reopen retry state", storage_init(root) != 0)
check_true("retry survives restart", storage_get_setting_int("sync_retry_attempt", 0) == 3)
check_true("queue survives restart", storage_sync_status(*status) != 0 && status.queued_changes == 1)
storage_set_sync_enabled(0)
check_true("disconnect clears retry", storage_get_setting_int("sync_retry_attempt", -1) == 0)
exec_db_sql(join(join("DELETE FROM settings WHERE key='sync_enabled';", "INSERT OR REPLACE INTO settings(user_id,key,value,updated_at) "), "SELECT id,'sync_server_connected','0',strftime('%s','now') FROM users LIMIT 1;"))
storage_close()
check_true("open 1.9.19 disconnected account", storage_init(root) != 0)
check_true("migration preserves explicit disconnect", storage_sync_enabled() == 0)
exec_db_sql(join("DELETE FROM settings WHERE key='sync_enabled';", "UPDATE settings SET value='1' WHERE key='sync_server_connected';"))
storage_close()
check_true("open 1.9.19 connected account", storage_init(root) != 0)
check_true("migration preserves enabled account", storage_sync_enabled() != 0)
storage_close()
remove_tree(root)
}
test_backfill :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
rounds: [3]s32 = .[30, 45, 60]
make_account(*values, 0)
path_in(key_path[:], root, "breathing-sync.key")
write_key_file(*key_path[0], *values, TextFromCString(*values.public_id[0]))
check_true("init backfill storage", storage_init(root) != 0)
check_true("save existing local session",
storage_save_session_at_for_activity(20260621, 8, 15, 0, *rounds[0], 3, 0, 1, null, 0) != 0)
exec_db_sql(join(join(join(join(join(join(join(join(join("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-local',(SELECT id FROM users LIMIT 1),'Existing Local Habit',"), "64,128,192,1,1,1,0,0,strftime('%s','now'));"), "INSERT INTO habit_days(habit_id,local_date,completed,count,updated_at) "), "VALUES('habit-local',20260621,1,5,strftime('%s','now'));"), "INSERT OR REPLACE INTO meta(key,value) VALUES('sync_last_server_version','44');"), "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');"), "DELETE FROM sync_outbox;"))
check_true("import account resets sync state", import_key_and_save(*account, *key_path[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("imported deterministic account",
account_text(*account.public_id[0]) == TextFromCString(*values.public_id[0]))
check_true("account owner marker",
storage_sync_data_owner_public_id() == TextFromCString(*values.public_id[0]))
payload := storage_build_sync_payload_json(TextFromCString(*account.public_id[0]),
TextFromCString(*account.public_key_hex[0]))
check_true("bootstrap payload built", payload != null)
if payload != null {
text := TextFromCString(payload)
check_true("bootstrap flag present", contains(text, "\"bootstrap\":true"))
check_true("existing local habit uploaded",
contains(text, "\"collection\":\"private.inbe.v1.habits\",\"id\":\"habit-local\""))
check_true("existing local habit day uploaded",
contains(text, "\"id\":\"habit-local:20260621\""))
check_true("existing local session uploaded",
contains(text, "\"collection\":\"private.inbe.v1.sessions\""))
check_true("habit content is encrypted, not sent in the clear",
!contains(text, "Existing Local Habit") && !contains(text, "\"rounds\""))
check_true("local settings still excluded",
!contains(text, "\"preferences\"") && !contains(text, "\"settings\""))
}
storage_free_sync_payload_json(payload)
storage_close()
remove_tree(root)
}
test_logout_owner :: () {
root := make_root("account")
values: AccountValues
account: SyncAccount
prepare_account(root, *values, *account)
id := TextFromCString(*values.public_id[0])
check_true("owner before logout", storage_sync_data_owner_public_id() == id)
check_true("logout account", sync_account_clear() != 0)
check_true("credentials removed", sync_account_load(*account) == 0)
check_true("owner after logout", storage_sync_data_owner_public_id() == id)
check_true("logout clears sync outbox", read_db_count("SELECT COUNT(*) FROM sync_outbox") == 0)
storage_close()
remove_tree(root)
}
test_account_switch :: () {
root := make_root("account")
one: AccountValues
two: AccountValues
account: SyncAccount
loaded: SyncAccount
make_account(*one, 0)
make_account(*two, 9)
path_in(key_path_one[:], root, "one.key")
path_in(key_path_two[:], root, "two.key")
write_key_file(*key_path_one[0], *one, TextFromCString(*one.public_id[0]))
write_key_file(*key_path_two[0], *two, TextFromCString(*two.public_id[0]))
check_true("init account switch storage", storage_init(root) != 0)
exec_db_sql(join(join(join(join(join("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-switch',(SELECT id FROM users LIMIT 1),'Switch Habit',"), "64,128,192,1,1,1,0,0,strftime('%s','now'));"), "INSERT INTO habit_days(habit_id,local_date,completed,count,updated_at) "), "VALUES('habit-switch',20260622,1,3,strftime('%s','now'));"))
ask := SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK
check_true("first account binds data", import_key_and_save(*account, *key_path_one[0], ask) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("logout before account switch", sync_account_clear() != 0)
check_true("local syncable data remains", storage_has_local_syncable_data() != 0)
check_true("second account blocked before clear",
import_key_and_save(*account, *key_path_two[0], ask) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_NEEDS_CLEAR)
check_true("blocked switch leaves no credentials", sync_account_load(*loaded) == 0)
check_true("blocked switch keeps old owner",
storage_sync_data_owner_public_id() == TextFromCString(*one.public_id[0]))
check_true("blocked switch keeps local data", storage_has_local_syncable_data() != 0)
check_true("second account can clear local data",
import_key_and_save(*account, *key_path_two[0],
SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_REPLACE) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("new account stored", sync_account_load(*loaded) != 0)
check_true("new account public id",
account_text(*loaded.public_id[0]) == TextFromCString(*two.public_id[0]))
check_true("new account clears alias", storage_get_setting_text("sync_account_alias").count == 0)
check_true("new owner after clear",
storage_sync_data_owner_public_id() == TextFromCString(*two.public_id[0]))
check_true("local syncable data cleared", storage_has_local_syncable_data() == 0)
storage_close()
remove_tree(root)
}
test_social_cache :: () {
root := make_root("account")
one: AccountValues
two: AccountValues
account: SyncAccount
loaded: SyncAccount
make_account(*one, 1)
make_account(*two, 2)
path_in(key_path_one[:], root, "one.key")
path_in(key_path_two[:], root, "two.key")
write_key_file(*key_path_one[0], *one, TextFromCString(*one.public_id[0]))
write_key_file(*key_path_two[0], *two, TextFromCString(*two.public_id[0]))
ask := SyncAccountLocalDataChoice.SYNC_ACCOUNT_LOCAL_DATA_ASK
check_true("init social cache storage", storage_init(root) != 0)
check_true("first social cache account", import_key_and_save(*account, *key_path_one[0], ask) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("store social-only cache", storage_set_social_cache_json("friends.list",
"{\"friends\":[{\"user_id_hash\":\"friend\"}]}") != 0)
check_true("social-only cache is not local syncable data", storage_has_local_syncable_data() == 0)
check_true("logout social cache account", sync_account_clear() != 0)
check_true("second account saves without clear", import_key_and_save(*account, *key_path_two[0], ask) ==
SyncAccountSaveResult.SYNC_ACCOUNT_SAVE_OK)
check_true("second account stored", sync_account_load(*loaded) != 0)
check_true("second account owner",
storage_sync_data_owner_public_id() == TextFromCString(*two.public_id[0]))
storage_close()
remove_tree(root)
}
#program_export
Check :: () -> s32 {
// Never run against a real data directory.
guard := support_begin("/tmp/inbe-sync-account-zi-test/")
if guard != 0 { return guard }
test_parallel()
test_import_export_clear()
test_reject_invalid()
test_legacy_connected()
test_unsynced_not_migrated()
test_disconnected_queue()
test_backfill()
test_logout_owner()
test_account_switch()
test_social_cache()
return failures
}