Files
breeze/tests/achievements_behavior.zi
waozi dc1d003cd8 Merge pending Breeze work: Photos as its own app, Opus voice packs, 2.1.63 notes
Photos downloads from Apps instead of shipping in Breeze, practice and
guide voice clips move to Ogg Opus, dependencies update (Ziran, Kryon,
Apps, Arcade, practices), and the 2.1.63 version and store notes are
prepared. The Practice tab note is translated in every locale.
2026-11-06 21:58:14 -03:00

512 lines
35 KiB
Ziran

#import "std/byte_text_linux"
#import, file "../src/storage/json.zi";
// Exercise achievement rules against the actual account-owned SQLite store.
#import, file "storage_test_support.zi";
#import, file "../src/storage/achievements.zi";
#import, file "../src/storage/achievement_records.zi";
#import, file "../src/storage/achievement_sources.zi";
#import, file "../src/storage/bundled_app_records.zi";
#import, file "../src/storage/db.zi";
#import, file "../src/storage/sync_apply.zi";
#import, file "../src/storage/storage_json_sql.zi";
#import, file "../src/storage/storage_settings.zi";
#import, file "../src/storage/sync_collections.zi";
#import, file "../src/apps/package_files.zi";
#import "std/json_scan"
#import "std/text_buffer"
#import "std/bundle_host"
#import, file "../src/apps/achievement_packages.zi";
#import, file "../src/storage/import.zi";
#import, file "../src/storage/import_types.zi";
#import, file "../src/app/text_buffers.zi";
#import "std/c_string"
#import "std/vec"
#import "drawing_props"
#import, file "../apps/radio/radio_achievements.zi";
#import, file "../apps/diary/diary_achievements.zi";
#import, file "../src/apps/diary_records.zi";
#import, file "../apps/lists/lists_achievements.zi";
#import, file "../src/storage/storage_elist.zi";
#import, file "../src/storage/storage_sessions.zi";
#import, file "../src/storage/sync_state.zi";
#import, file "../src/storage/meta.zi";
#import, file "../src/storage/storage_habits.zi";
#import, file "../src/habits/habit_model.zi";
#import, file "../src/habits/habit_types.zi";
#import, file "../src/habits/habit_memory.zi";
#import, file "../apps/habits/habits_achievements.zi";
#import, file "../src/app/achievement_host.zi";
#import, file "../src/app/assets.zi";
test_entry: AchievementDefinition;
test_habits: Habits;
test_apps: s32;
test_packs: s32;
installed :: (id: string) -> bool {
names: [8]string = .["inbe.practices", "inbe.diary", "inbe.habits", "inbe.lists", "inbe.lumi", "inbe.photos", "inbe.radio", "inbe.leaderboard"]
for index: cast(s32)0..7 {
if names[index] == id { return (test_apps & (cast(s32)1 << index)) != 0 }
}
return false
}
pack_ready :: (id: s32) -> bool {
return id >= 0 && id < 5 && (test_packs & (cast(s32)1 << id)) != 0
}
no_catalogs :: () {}
visible_count :: () -> s32 {
AchievementVisibility()
count: s32 = 0
for index: cast(s32)0..achievement_count - 1 {
if achievement_definitions[index].visible { count += 1 }
}
return count
}
metric_value :: (metric: string, rule: string, variant: string = "") -> s32 {
test_entry = .{}
unused CopyCString(metric, test_entry.metric[:])
unused CopyCString(rule, test_entry.rule[:])
unused CopyCString(variant, test_entry.variant[:])
return cast(s32)AchievementMetric(*test_entry)
}
open_fixture :: (path: string) -> *void {
bytes: [128]u8
if !CopyCString(path, bytes[:]) { return null }
return OpenBundle(*bytes[0])
}
apply_record :: (key: string, value: string, date: string,
collection: string = AchievementCollection) -> s32 {
out: StorageJsonBuilder
out.ok = 1
storage_json_builder_append_text(*out, "{")
storage_json_builder_key_value_text(*out, "key", key)
storage_json_builder_append_text(*out, ",")
storage_json_builder_key_value_text(*out, "value", value)
storage_json_builder_append_text(*out, ",")
storage_json_builder_key_value_text(*out, "updated_at", date)
storage_json_builder_append_text(*out, "}")
document := TextFromCString(out.data)
ok := storage_apply_decrypted_record(collection, key, document)
storage_json_builder_free(*out)
return ok
}
fact_document :: (metric: string, day: s32, variant: string) -> string {
out: Vec(u8)
BuilderPrint(*out, "{\"version\":1,\"metric\":\"%\",\"subject\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"day\":%,\"civil\":%,\"amount\":1,\"variant\":\"%\",\"active\":1,\"deleted\":0,\"start\":0,\"end\":0}",
metric, day, AchievementDayIndex(day), variant)
return BuilderFinish(out)
}
catalogs_check :: () {
AchievementReset()
// Photos is built in its own repository; its fixture is the catalog the
// node's Photos package carries, which this host must still accept.
paths: [9]string = .["apps/inbe/achievements.json", "apps/lists/achievements.json",
"apps/habits/achievements.json", "apps/practice/achievements.json",
"apps/diary/achievements.json", "apps/lumi/achievements.json",
"apps/leaderboard/achievements.json", "tests/fixtures/photos-achievements.json",
"apps/radio/achievements.json"]
owners: [9]string = .["inbe", "inbe.lists", "inbe.habits", "inbe.practices", "inbe.diary", "inbe.lumi", "inbe.leaderboard", "inbe.photos", "inbe.radio"]
for index: cast(s32)0..8 {
document := PackageRead(paths[index], 262144)
check_true("all shipped catalogs validate", AchievementCatalog(document, owners[index]))
check_true("validate update without changing catalog", AchievementCatalog(document, owners[index], false))
PackageFree(document)
}
check_int("load all 160 achievements, with the node's Photos", achievement_count, 160)
AchievementBindHost(installed, pack_ready, no_catalogs)
check_int("uninstalled apps contribute no visible achievements", visible_count(), 10)
test_apps = 1
check_int("installed Practices shows its general set", visible_count(), 20)
test_packs = 1
check_int("only installed practice content contributes its set", visible_count(), 30)
test_apps = 255
test_packs = 31
check_int("all installed apps and practice packs expose full catalog", visible_count(), 160)
test_apps &= ~(cast(s32)1 << 5)
check_int("uninstalling the node's Photos immediately fixes visible totals", visible_count(), 150)
test_apps = 255
achievement_catalog_generation = AssetGeneration()
document := AchievementPageDocument(999)
check_int("projection clamps a stale page index", cast(s32)ReadNumber(document, FieldAt(document, 0, "page")).value, 7)
check_int("projection is bounded to twenty rows", ArrayCount(document, FieldAt(document, 0, "rows")), 20)
check_int("projection counts installed achievements", cast(s32)ReadNumber(document, FieldAt(document, 0, "total")).value, 160)
check_int("filter choices contain only apps with installed badges", ArrayCount(document, FieldAt(document, 0, "apps")), 9)
document = AchievementPageDocument(999, "inbe.diary")
check_int("Diary filter totals its own badges", cast(s32)ReadNumber(document, FieldAt(document, 0, "total")).value, 20)
check_int("Diary filter clamps page within its own set", cast(s32)ReadNumber(document, FieldAt(document, 0, "page")).value, 0)
rows := FieldAt(document, 0, "rows")
for index: cast(s32)0..ArrayCount(document, rows) - 1 {
id: [97]u8
unused ReadStringText(document, FieldAt(document, ElementAt(document, rows, index), "id"), id[:])
check_true("Diary filter contains only Diary awards", TextUntilNul(id[:]).count > 6 && TextUntilNul(id[:])[:6] == "diary.")
}
test_apps = 1
document = AchievementPageDocument(0, "inbe.diary")
check_int("uninstalled app is absent from the filter", ArrayCount(document, FieldAt(document, 0, "apps")), 2)
filter: [97]u8
unused ReadStringText(document, FieldAt(document, 0, "app"), filter[:])
check_true("uninstall resets an obsolete filter", TextUntilNul(filter[:]) == "")
test_apps = 255
}
#program_export
Check :: () -> s32 {
guard := support_begin("/tmp/inbe-achievements-test/")
if guard != 0 { return guard }
check_true("open achievements account", storage_init(make_root("rules")) != 0)
defer { storage_close() }
AchievementReset()
catalog := "{\"schema\":1,\"app\":\"inbe\",\"achievements\":[{\"id\":\"self.first\",\"group\":\"self\",\"metric\":\"*\",\"rule\":\"days\",\"target\":1,\"title_key\":\"first\",\"description_key\":\"first_desc\",\"variant\":\"\",\"pack\":\"\",\"needs\":\"\"}]}"
check_true("accept valid catalog", AchievementCatalog(catalog, "inbe"))
check_true("reject duplicate catalog atomically", !AchievementCatalog(catalog, "inbe") && achievement_count == 1)
check_true("reject wrong app identity", !AchievementCatalog(catalog, "inbe.diary"))
achievement_definitions[0].visible = true
check_true("new accounts enable alerts", AchievementAlerts())
check_true("record canonical practice", AchievementRecord("practice.meditation", "session-one", 20261001, 120))
check_true("same source is idempotent", AchievementRecord("practice.meditation", "session-one", 20261001, 120))
check_true("record another day", AchievementRecord("practice.meditation", "session-two", 20261016, 60))
check_int("count sessions once", metric_value("practice", "count"), 2)
check_int("sum actual meditation minutes", metric_value("practice.meditation", "minutes"), 3)
check_int("count distinct days", metric_value("practice", "days"), 2)
check_int("count intervening return days", metric_value("*", "return"), 14)
check_true("reject invalid date", !AchievementRecord("diary", "bad-day", 20260230))
check_true("reject corrupt record namespace", !AchievementRecordValid("cell.achievement.other"))
check_true("separate Lumi and achievements", !LumiBundledAppRecordValid(AchievementAlertsKey))
AchievementEvaluate()
check_true("award earned milestone", achievement_definitions[0].awarded)
check_int("present live milestone once", AchievementConsumeAlerts(), 1)
check_int("do not replay milestone", AchievementConsumeAlerts(), 0)
check_true("disable alerts", AchievementSetAlerts(false) && !AchievementAlerts())
achievement_definitions[0].pending = true
check_int("disabled alerts are discarded", AchievementConsumeAlerts(), 0)
check_true("enable alerts", AchievementSetAlerts(true))
check_int("enabling creates no backlog", AchievementConsumeAlerts(), 0)
check_true("remove a source fact", AchievementRecord("practice.meditation", "session-one", 20261001, 120, live = false, deleted = true))
AchievementEvaluate()
check_int("unearned progress follows source deletion", metric_value("practice", "count"), 1)
check_true("earned achievements stay earned", achievement_definitions[0].awarded)
check_true("radio interval one", AchievementRecord("radio.playback", "radio-one", 20261001, 120, subject = "station-one", started = 1000, ended = 1120))
check_true("overlapping second device", AchievementRecord("radio.playback", "radio-two", 20261001, 120, subject = "station-one", started = 1060, ended = 1180))
check_int("radio unions overlapping device intervals", metric_value("radio.playback", "minutes"), 3)
check_int("radio day needs a minute", metric_value("radio.playback", "days"), 1)
check_int("count each sufficiently heard station once", metric_value("radio.playback", "station_minutes"), 1)
check_true("reject fabricated radio duration", !AchievementRecord("radio.playback", "invalid-radio", 20261001, 100, started = 1000, ended = 1001))
// A hosted package awards only its own metric, under its verified id.
package_catalog := "{\"schema\":1,\"app\":\"xyz.waozi.choano\",\"achievements\":[{\"id\":\"xyz.waozi.choano.full_size\",\"group\":\"choano\",\"metric\":\"xyz.waozi.choano.full_size\",\"rule\":\"count\",\"target\":1,\"variant\":\"\",\"pack\":\"\",\"needs\":\"\",\"title_key\":\"achievement_choano_full_size_title\",\"description_key\":\"achievement_choano_full_size_description\",\"title\":\"Full size\",\"description\":\"Grow to full size in the amoeba dish and unlock Fish mode.\",\"icon\":\"assets/choano/icons/xyz.waozi.choano.full_size.shape\",\"icon_source\":\"mingcute_regular_trophy\"}]}"
check_true("reject a package catalog under another id", !AchievementCatalog(package_catalog, "xyz.waozi.other", false))
check_true("reject a package catalog under an Inbe id", !AchievementCatalog(package_catalog, "inbe.lists", false))
check_true("accept a package catalog under its own id", AchievementCatalog(package_catalog, "xyz.waozi.choano"))
package_index := achievement_count - 1
achievement_definitions[package_index].visible = true
full_size := "{\"metric\":\"xyz.waozi.choano.full_size\",\"source\":\"full-size\",\"amount\":1}"
check_true("reject another package's metric", !AchievementPackageReport("xyz.waozi.other", full_size))
check_true("reject an Inbe identity", !AchievementPackageReport("inbe.lists", "{\"metric\":\"inbe.lists.done\",\"source\":\"a\",\"amount\":1}"))
check_true("reject a metric with a longer prefix", !AchievementPackageReport("xyz.waozi.choano",
"{\"metric\":\"xyz.waozi.choanox.full_size\",\"source\":\"full-size\",\"amount\":1}"))
check_true("reject the bare package id", !AchievementPackageReport("xyz.waozi.choano",
"{\"metric\":\"xyz.waozi.choano\",\"source\":\"full-size\",\"amount\":1}"))
check_true("reject a zero amount", !AchievementPackageReport("xyz.waozi.choano",
"{\"metric\":\"xyz.waozi.choano.full_size\",\"source\":\"full-size\",\"amount\":0}"))
check_true("reject an amount above 1000", !AchievementPackageReport("xyz.waozi.choano",
"{\"metric\":\"xyz.waozi.choano.full_size\",\"source\":\"full-size\",\"amount\":1001}"))
check_true("reject a fractional amount", !AchievementPackageReport("xyz.waozi.choano",
"{\"metric\":\"xyz.waozi.choano.full_size\",\"source\":\"full-size\",\"amount\":1.5}"))
check_true("reject a missing source", !AchievementPackageReport("xyz.waozi.choano",
"{\"metric\":\"xyz.waozi.choano.full_size\",\"amount\":1}"))
check_true("reject trailing request data", !AchievementPackageReport("xyz.waozi.choano", "{\"metric\":\"x\"} {}"))
check_int("rejected reports record nothing", metric_value("xyz.waozi.choano.full_size", "count"), 0)
check_true("accept the package's own metric", AchievementPackageReport("xyz.waozi.choano", full_size))
check_true("a repeated report is idempotent", AchievementPackageReport("xyz.waozi.choano", full_size))
check_int("count the package fact once", metric_value("xyz.waozi.choano.full_size", "count"), 1)
AchievementEvaluate()
check_true("award the package achievement", achievement_definitions[package_index].awarded)
check_int("present the package award once", AchievementConsumeAlerts(), 1)
AchievementReset()
check_true("reload durable catalog", AchievementCatalog(catalog, "inbe"))
achievement_definitions[0].visible = true
AchievementEvaluate()
check_true("award survives engine reload", achievement_definitions[0].awarded)
check_int("reload stays silent", AchievementConsumeAlerts(), 0)
check_true("seed unknown legacy task date", AchievementRecord("list.completed", "old-task", 0, active = false, live = false))
check_true("recomplete legacy task", AchievementRecord("list.completed", "old-task", 20261018))
check_int("recompletion cannot invent first date", metric_value("list.completed", "days"), 0)
check_int("old item is still counted once", metric_value("list.completed", "count"), 1)
check_true("save a box breathing preset", AchievementRecord("practice.patterns", "box-session", 20261018, variant = "box"))
check_true("generic backfill keeps preset", AchievementRecord("practice.patterns", "box-session", 20261018, live = false))
check_int("box preset survives reconciliation", metric_value("practice.patterns", "count", "box"), 1)
check_true("Sunday diary", AchievementRecord("diary", "sunday", 20261004))
check_true("Monday diary", AchievementRecord("diary", "monday", 20261005))
check_true("different month diary", AchievementRecord("diary", "september", 20260930))
check_int("calendar weeks start Monday", metric_value("diary", "weeks"), 2)
check_int("calendar months stay distinct", metric_value("diary", "months"), 2)
check_true("habit scheduled date one", AchievementRecord("habit.scheduled", "h-one", 20261001, subject = "habit-one"))
check_true("habit scheduled date two", AchievementRecord("habit.scheduled", "h-two", 20261002, subject = "habit-one"))
check_true("other habit date", AchievementRecord("habit.scheduled", "h-three", 20261003, subject = "habit-two"))
check_int("same-habit milestones group by stable subject", metric_value("habit.scheduled", "max_days"), 2)
award_key := "cell.achievement.award.self.merge"
check_true("apply known remote award", apply_record(award_key, "{\"version\":1,\"earned\":1000,\"day\":20261001,\"rule\":1}", "2026-10-01T00:00:00Z") != 0)
check_true("late remote upload cannot erase earlier date", apply_record(award_key, "{\"version\":1,\"earned\":2000,\"day\":20261002,\"rule\":1}", "2026-10-18T00:00:00Z") != 0)
merged := BundledAppRecordRead(award_key)
check_int("keep earliest known earned time", cast(s32)ReadNumber(merged, FieldAt(merged, 0, "earned")).value, 1000)
PackageFree(merged)
check_true("achievement records reject wrong collection", apply_record(award_key, "{\"version\":1,\"earned\":500,\"day\":20261001,\"rule\":1}", "2026-10-18T00:00:00Z", "private.inbe.v1.cells") == 0)
check_true("remote alert setting uses dedicated collection", apply_record(AchievementAlertsKey, "0", "2099-01-01T00:00:00Z") != 0 && !AchievementAlerts())
check_true("collection routes achievements", storage_encrypted_collection_for_entity("cell_record", award_key) == AchievementCollection)
pattern_key := AchievementKey("practice.patterns", "box-session")
pattern_fact := fact_document("practice.patterns", 20261018, "")
check_true("merge generic pattern fact from another device", apply_record(pattern_key, pattern_fact, "2099-01-01T00:00:00Z") != 0)
check_int("sync keeps native breathing preset", metric_value("practice.patterns", "count", "box"), 1)
check_true("replay merged pattern fact", apply_record(pattern_key, pattern_fact, "2099-01-01T00:00:00Z") != 0)
check_int("replaying sync never duplicates a session", metric_value("practice.patterns", "count"), 1)
PackageFree(pattern_key)
PackageFree(pattern_fact)
check_true("known first task date", AchievementRecord("list.completed", "dated-task", 20261001))
task_key := AchievementKey("list.completed", "dated-task")
task_fact := fact_document("list.completed", 20261018, "")
check_true("merge later task completion", apply_record(task_key, task_fact, "2099-01-01T00:00:00Z") != 0)
task_merged := BundledAppRecordRead(task_key)
check_int("sync preserves first task completion date", cast(s32)ReadNumber(task_merged, FieldAt(task_merged, 0, "day")).value, 20261001)
PackageFree(task_key)
PackageFree(task_fact)
PackageFree(task_merged)
catalogs_check()
valid := open_fixture("build/achievements-test/valid.zx")
invalid := open_fixture("build/achievements-test/invalid.zx")
empty := open_fixture("build/achievements-test/empty.zx")
check_true("valid fixture loads", valid != null)
check_true("invalid fixture loads before metadata rejection", invalid != null)
check_true("empty fixture loads", empty != null)
check_true("real bundle accepts valid app-owned rules", AchievementBundleValid(valid, "inbe", "app"))
check_true("real bundle rejects unsupported rules", !AchievementBundleValid(invalid, "inbe", "app"))
check_true("older bundle without catalog remains supported", AchievementBundleValid(empty, "inbe", "app"))
check_int("package validation never mutates working definitions", achievement_count, 160)
CloseBundle(valid)
CloseBundle(invalid)
CloseBundle(empty)
storage_close()
check_true("open muted account", storage_init(make_root("muted")) != 0)
AchievementReset()
check_true("load muted account catalog", AchievementCatalog(catalog, "inbe"))
achievement_definitions[0].visible = true
check_true("mute before earning", AchievementSetAlerts(false))
check_true("earn while muted", AchievementRecord("diary", "muted-diary", 20261018))
AchievementEvaluate()
check_true("muting preserves awards", achievement_definitions[0].awarded)
check_int("muted birth is silent", AchievementConsumeAlerts(), 0)
check_true("unmute after earning", AchievementSetAlerts(true))
check_int("unmute has no historical replay", AchievementConsumeAlerts(), 0)
storage_close()
check_true("open independent profile", storage_init(make_root("other-profile")) != 0)
AchievementReset()
check_true("load independent profile catalog", AchievementCatalog(catalog, "inbe"))
achievement_definitions[0].visible = true
AchievementEvaluate()
check_true("profiles never share achievements", !achievement_definitions[0].awarded)
check_true("profiles do not share alert settings", AchievementAlerts())
check_true("historical source qualifies without alert", AchievementRecord("diary", "backfill-diary", 20261001, live = false))
AchievementEvaluate()
check_true("backfill awards existing progress", achievement_definitions[0].awarded)
check_int("backfill stays silent", AchievementConsumeAlerts(), 0)
archive: TextBuffer
append(*archive, make_root("archive"))
append(*archive, "/profile.zip")
check_true("save archive alert preference", AchievementSetAlerts(false))
check_true("export achievements with profile data", storage_export_zip(TextFromCString(*archive.bytes[0])) != 0)
storage_close()
check_true("open restore profile", storage_init(make_root("restored")) != 0)
check_true("restore permanent awards in data-only mode", storage_import_zip_ex(TextFromCString(*archive.bytes[0]), cast(StorageImportMode)StorageImportMode.STORAGE_IMPORT_DATA_ONLY) != 0)
AchievementReset()
check_true("load restored catalog", AchievementCatalog(catalog, "inbe"))
achievement_definitions[0].visible = true
AchievementEvaluate()
check_true("restored milestone survives missing original activity", achievement_definitions[0].awarded)
check_int("restored milestone remains silent", AchievementConsumeAlerts(), 0)
check_true("data-only import preserves device preference", AchievementAlerts())
check_true("restore settings when requested", storage_import_zip_ex(TextFromCString(*archive.bytes[0]), cast(StorageImportMode)StorageImportMode.STORAGE_IMPORT_DATA_AND_SETTINGS) != 0)
check_true("settings import restores alert choice", !AchievementAlerts())
storage_close()
check_true("open radio sampling profile", storage_init(make_root("radio-sampling")) != 0)
AchievementReset()
AchievementRadioSample(0, "station", true, 50, true, 100.0, 1000, 20261018)
for index: cast(s32)1..10 {
AchievementRadioSample(0, "station", true, 50, true, 100.0 + cast(float64)index,
1000 + cast(s64)index, 20261018)
}
AchievementRadioSample(0, "station", true, 50, true, 200.0, 1100, 20261018)
check_true("clock gap starts a fresh interval", achievement_radio_start == 1100)
check_int("gap flushes only observed seconds", cast(s32)achievement_radio_seconds, 0)
for index: cast(s32)1..15 {
AchievementRadioSample(0, "station", true, 50, true, 200.0 + cast(float64)index,
1100 + cast(s64)index, 20261018)
}
check_int("fresh segment checkpoints observed seconds", cast(s32)achievement_radio_start, 1115)
AchievementRadioSample(0, "station", true, 0, true, 216.0, 1116, 20261018)
for index: cast(s32)1..70 {
AchievementRadioSample(0, "station", true, 0, true, 216.0 + cast(float64)index,
1116 + cast(s64)index, 20261018)
}
check_int("muted playback adds no minute", metric_value("radio.playback", "minutes"), 0)
for index: cast(s32)1..70 {
AchievementRadioSample(0, "station", true, 50, false, 286.0 + cast(float64)index,
1186 + cast(s64)index, 20261018)
}
check_int("unverified guest playback adds no minute", metric_value("radio.playback", "minutes"), 0)
for index: cast(s32)0..60 {
AchievementRadioSample(0, "station", true, 50, true, 356.0 + cast(float64)index,
1256 + cast(s64)index, 20261018)
}
AchievementRadioSample(0, "station", false, 50, true, 417.0, 1317, 20261018)
check_int("verified audible playback earns actual minutes", metric_value("radio.playback", "minutes"), 1)
check_true("authored diary text is a real entry", AchievementDiarySaved("2026-10-18.json", "{\"version\":1,\"date\":\"2026-10-18\",\"text\":\"# A real entry\"}"))
check_int("authored diary text qualifies", metric_value("diary", "days"), 1)
check_true("formatting alone is no entry", !AchievementDiarySaved("2026-10-18.json", "{\"version\":1,\"date\":\"2026-10-18\",\"text\":\" ** _ # > ` ~ --- \\n\"}"))
check_int("formatting-only edit retracts diary progress", metric_value("diary", "days"), 0)
check_true("a broken photo is no entry", !AchievementDiarySaved("2026-10-18.json", "{\"version\":1,\"date\":\"2026-10-18\",\"text\":\"![missing](diary:missing.jpg)\"}"))
check_int("broken photo cannot qualify a diary day", metric_value("diary", "days"), 0)
check_true("legacy photo metadata saves", BundledAppRecordWrite("cell.diary.file.real.jpg", "{\"hash\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"size\":100}"))
check_true("a verified photo is an entry", AchievementDiarySaved("2026-10-18.json", "{\"version\":1,\"date\":\"2026-10-18\",\"text\":\"![photo](diary:real.jpg)\"}"))
check_int("verified photo-only diary day qualifies", metric_value("diary", "days"), 1)
check_int("verified photo gets its own diary metric", metric_value("diary", "days", "photo"), 1)
check_true("a removed document is no entry", !AchievementDiarySaved("2026-10-18.json", ""))
check_int("removed diary document retracts progress", metric_value("diary", "days"), 0)
// The days a Diary-linked habit brings along: only saved real entries.
check_true("written diary day saves", DiaryRecordSave("2026-10-16.json",
"{\"version\":1,\"date\":\"2026-10-16\",\"text\":\"Walked by the river\"}"))
check_true("empty diary day saves", DiaryRecordSave("2026-10-17.json",
"{\"version\":1,\"date\":\"2026-10-17\",\"text\":\" \"}"))
written := DiaryWrittenDays()
check_int("one written diary day", cast(s32)written.count, 1)
if written.count == 1 {
check_int("the written diary day", written[0], 20261016)
}
VecFree(written)
check_true("whitespace is not a saved task comment", !AchievementListComment(" \t\n"))
check_true("authored task comments qualify", AchievementListComment("A useful detail"))
storage_close()
check_true("open list action profile", storage_init(make_root("list-actions")) != 0)
AchievementReset()
list_id: [37]u8
items: [3][37]u8
check_true("create actual private list", storage_elist_create_list("A list", *list_id[0]) != 0)
for index: cast(s32)0..2 {
check_true("create actual private task", storage_elist_create_item(TextUntilNul(list_id[:]), "Task", "", *items[index][0]) != 0)
check_true("commit actual task completion", storage_elist_update_item(TextUntilNul(items[index][:]), "Task",
ifx index == 0 then "A detail" else "", 1, index) != 0)
}
check_int("count actual task transitions", metric_value("list.completed", "count"), 3)
check_int("count saved task comment", metric_value("list.comment", "count"), 1)
check_int("finishing the final task clears a list", metric_value("list.cleared", "count"), 1)
check_true("repeat completed task update", storage_elist_update_item(TextUntilNul(items[0][:]), "Renamed", "A detail", 1, 0) != 0)
check_int("editing a completed task adds no transition", metric_value("list.completed", "count"), 3)
check_true("delete completed task", storage_elist_delete_item(TextUntilNul(items[0][:])) != 0)
unused AchievementListItemBackfill(0)
check_int("deleted tasks leave locked progress", metric_value("list.completed", "count"), 2)
check_int("deleted task comments leave locked progress", metric_value("list.comment", "count"), 0)
check_true("delete private list", storage_elist_delete_list(TextUntilNul(list_id[:])) != 0)
unused AchievementListBackfill(0)
unused AchievementListItemBackfill(0)
check_int("deleted list removes its clearing contribution", metric_value("list.cleared", "count"), 0)
check_int("deleted list removes task contributions", metric_value("list.completed", "count"), 0)
shared_before := "{\"lists\":[{\"id\":\"shared-list\",\"revision\":1,\"items\":[{\"id\":\"mine\",\"done\":0},{\"id\":\"theirs\",\"done\":1}]}]}"
shared_after := "{\"lists\":[{\"id\":\"shared-list\",\"revision\":2,\"items\":[{\"id\":\"mine\",\"done\":1,\"comment\":\"Detail\"},{\"id\":\"theirs\",\"done\":1}]}]}"
shared_request := "{\"action\":\"save\",\"list\":{\"id\":\"shared-list\",\"revision\":1,\"items\":[{\"id\":\"mine\",\"done\":1},{\"id\":\"theirs\",\"done\":1}]}}"
AchievementSharedListReconcile(shared_before, shared_after)
check_int("observing another actor creates no task credit", metric_value("list.completed", "count"), 0)
AchievementSharedListConfirmed(shared_request, shared_before, shared_after)
check_int("confirmed own transition earns shared task credit", metric_value("list.completed", "count"), 1)
check_int("another actor's completed task stays uncredited", metric_value("list.completed", "subjects"), 1)
check_int("shared confirmation saves authored comment credit", metric_value("list.comment", "count"), 1)
AchievementSharedListConfirmed(shared_request, shared_after, shared_after)
check_int("shared response replay earns no duplicate credit", metric_value("list.completed", "count"), 1)
AchievementSharedListReconcile(shared_after, shared_before)
check_int("shared undo retracts locked progress", metric_value("list.completed", "count"), 0)
check_int("shared undo retracts comment progress", metric_value("list.comment", "count"), 0)
storage_close()
check_true("open local history profile", storage_init(make_root("history-clear")) != 0)
AchievementReset()
check_true("load history profile catalog", AchievementCatalog(catalog, "inbe"))
duration: [1]s32 = .[60]
check_true("save actual meditation history", storage_save_session_for_activity(*duration[0], 1, 0, 1, null, 0) != 0)
check_true("save derived habit contribution", AchievementRecord("habit.completed", "cleared-day", 20261018))
AchievementEvaluate()
check_true("earn before clearing source history", achievement_definitions[0].awarded)
check_true("clear local session and habit history", storage_delete_all_sessions() > 0)
check_int("cleared local sessions leave no orphan progress", metric_value("practice", "count"), 0)
check_int("cleared local habits leave no orphan progress", metric_value("habit.completed", "count"), 0)
AchievementReset()
check_true("reload permanent catalog after source clear", AchievementCatalog(catalog, "inbe"))
AchievementEvaluate()
check_true("source history clearing retains earned awards", achievement_definitions[0].awarded)
check_true("disable alerts before profile replacement", AchievementSetAlerts(false))
storage_set_setting_int("achievement.receipt.self.first", 1)
storage_set_setting_int("unrelated_preference", 7)
check_true("replace profile clears achievement storage", storage_clear_local_sync_data() != 0)
AchievementEvaluate()
check_true("replace profile clears visible awards", !achievement_definitions[0].awarded)
check_true("replace profile clears alert preference", AchievementAlerts())
check_int("replace profile clears local presentation receipts", storage_get_setting_int("achievement.receipt.self.first", 0), 0)
check_int("achievement reset preserves unrelated preferences", storage_get_setting_int("unrelated_preference", 0), 7)
storage_close()
check_true("open purge profile", storage_init(make_root("source-purge")) != 0)
AchievementReset()
saved_id: [96]u8
check_true("save session before deletion", storage_save_session_for_activity(*duration[0], 1, 0, 1, *saved_id[0], 96) != 0)
check_true("delete actual saved session", storage_delete_session(TextUntilNul(saved_id[:])) != 0)
meta_key: [64]u8
unused CopyCString("sync_full_upload_done", meta_key[:])
set_meta_int64(*meta_key[0], 1)
check_true("acknowledge deleted source upload", exec_sql("DELETE FROM sync_outbox") != 0)
storage_purge_synced_deleted_data()
check_int("purged source cannot leave progress behind", metric_value("practice", "count"), 0)
check_int("synced source tombstone is physically purged", read_db_count("SELECT COUNT(*) FROM sessions"), 0)
check_int("create actual counter habit", habits_add_custom(*test_habits, "Counter", Color.{}, 0, 0), 0)
counter := *test_habits.items[0]
counter.counter_enabled = 1
counter.counter_target = 5
storage_habits_save(cast(*void)*test_habits)
habit_set_day_count(*test_habits, 0, 20261018, 5)
storage_habits_save(cast(*void)*test_habits)
counter.counter_target = 10
storage_habits_save(cast(*void)*test_habits)
unused AchievementHabitDaysBackfill(*test_habits, 0)
check_int("historical counter target survives later increase", metric_value("habit.counter", "count"), 1)
none: [1]HabitAnswer
answers: [1]HabitAnswer
answers[0].id = 1
answers[0].answer_type = HABIT_ANSWER_TEXT
unused CopyCString(" \t\n", answers[0].value[:])
entry_id: [HABIT_ID_SIZE]u8
check_true("save whitespace answer", storage_habit_entry_save("", TextUntilNul(counter.id[:]), 20261018,
1000, 1000, answers[:], 1, entry_id[:]) != 0)
unused AchievementHabitAnswersBackfill(0)
check_int("whitespace-only habit answer does not count", metric_value("habit.answer", "count"), 0)
unused CopyCString("A real answer", answers[0].value[:])
updated_entry: [HABIT_ID_SIZE]u8
check_true("save authored habit answer", storage_habit_entry_save(TextUntilNul(entry_id[:]), TextUntilNul(counter.id[:]), 20261018,
1000, 1000, answers[:], 1, updated_entry[:]) != 0)
unused AchievementHabitAnswersBackfill(0)
check_int("actual authored habit answer counts once", metric_value("habit.answer", "count"), 1)
check_true("save a finished historical span", storage_habit_entry_save("", TextUntilNul(counter.id[:]), 20261018,
2000, 2060, none[:], 0, entry_id[:]) != 0)
unused AchievementHabitSpansBackfill(0)
check_int("saved span survives a current tracking-mode change", metric_value("habit.span", "count"), 1)
revision := AchievementSourceRevision()
storage_set_setting_int("unrelated_preference", 8)
check_true("a preference write leaves achievement sources unchanged", AchievementSourceRevision() == revision)
check_true("clear the sync outbox", exec_sql("DELETE FROM sync_outbox") != 0)
check_true("sync bookkeeping leaves achievement sources unchanged", AchievementSourceRevision() == revision)
habit_set_day_count(*test_habits, 0, 20261019, 5)
storage_habits_save(cast(*void)*test_habits)
check_true("a habit day moves the achievement source revision", AchievementSourceRevision() != revision)
habits_free(*test_habits)
return failures
}