A habit's editor now has "Ask when done": add questions, each with its words and how it is answered, as text, a whole number, a time or yes or no. A habit without questions works as before. With them, tapping an open day shows the questions, and Done records the answers and marks the day. Tapping a day that is done shows its answers to change, like a practice's session data, with Not done to open the day again. Counting habits show their questions under the count steps. Each answer keeps its question's words, so earlier days read the same after a question is reworded or removed. Questions travel with the habit and answers with the day to every device, only inside the encrypted records; older versions of the app keep them. The habit's statistics add up each question over the last 30 days: the average and earliest to latest of times, with a dot per day, bedtimes around midnight averaging correctly; the average and total of numbers with a bar per day; how often the answer was yes; and the latest written answers.
696 lines
33 KiB
Ziran
696 lines
33 KiB
Ziran
#import "byte_text_linux"
|
|
#import "drawing_props"
|
|
#import, file "storage_test_support.zi";
|
|
#import, file "../src/app/text_buffers.zi";
|
|
#import, file "../src/storage/data.zi";
|
|
#import, file "../src/storage/state.zi";
|
|
#import, file "../src/storage/db.zi";
|
|
#import, file "../src/storage/import.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/habits/habit_questions.zi";
|
|
#import, file "../src/habits/habit_answer_stats.zi";
|
|
|
|
c :: #system_library "c";
|
|
#import, file "../src/storage/import_types.zi";
|
|
using StorageImportMode;
|
|
|
|
// Habit sets are large, so the tests keep theirs in globals.
|
|
habits_a: Habits;
|
|
habits_b: Habits;
|
|
|
|
ascii_lower :: (byte: u8) -> u8 {
|
|
if byte >= cast(u8)65 && byte <= cast(u8)90 { return byte + cast(u8)32 }
|
|
return byte
|
|
}
|
|
|
|
equal_ignoring_case :: (left: string, right: string) -> bool {
|
|
if left.count != right.count { return false }
|
|
index: s64 = 0
|
|
while index < left.count {
|
|
if ascii_lower(left[index]) != ascii_lower(right[index]) { return false }
|
|
index += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
find_habit :: (habits: *Habits, name: string) -> *Habit {
|
|
index: s32 = 0
|
|
while index < habits.count {
|
|
if equal_ignoring_case(TextFromCString(*habits.items[index].name[0]), name) {
|
|
return *habits.items[index]
|
|
}
|
|
index += 1
|
|
}
|
|
return null
|
|
}
|
|
|
|
has_day :: (habit: *Habit, day: s32) -> bool {
|
|
return habit != null && habit_completed_day(habit, day) != 0
|
|
}
|
|
|
|
// A habit's count on a day, or -1.
|
|
day_count_of :: (habit: *Habit, day: s32) -> s32 {
|
|
index := habit_find_day(habit, day)
|
|
if index < 0 { return -1 }
|
|
return habit.days[index].count
|
|
}
|
|
|
|
Blue :: () -> Color {
|
|
return Color.{126, 183, 230, 255}
|
|
}
|
|
|
|
Orange :: () -> Color {
|
|
return Color.{224, 124, 104, 255}
|
|
}
|
|
|
|
Green :: () -> Color {
|
|
return Color.{99, 196, 165, 255}
|
|
}
|
|
|
|
Purple :: () -> Color {
|
|
return Color.{180, 132, 220, 255}
|
|
}
|
|
|
|
save_source_session :: (root: string) -> bool {
|
|
rounds: [3]s32 = .[45, 60, 75]
|
|
if storage_init(root) == 0 { return false }
|
|
ok: bool = storage_save_session(*rounds[0], 3, null, 0) != 0
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habit_set_day(*habits_a, 0, 20260613, 1)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
return ok
|
|
}
|
|
|
|
test_habit_name_merge_import :: () {
|
|
source := make_root("habit-name-source")
|
|
dest := make_root("habit-name-dest")
|
|
archive: TextBuffer
|
|
append(*archive, source)
|
|
append(*archive, "/export.zip")
|
|
|
|
check_true("init habit merge source", storage_init(source) != 0)
|
|
habits_a = Habits.{}
|
|
check_int("add imported meditation", habits_add_custom(*habits_a, "meditation", Orange(), 0, 0), 0)
|
|
habit_set_day(*habits_a, 0, 20260613, 1)
|
|
habits_save(*habits_a)
|
|
check_int("add imported push ups", habits_add_custom(*habits_a, "Push ups", Purple(), 0, 0), 1)
|
|
habit_set_day(*habits_a, 1, 20260614, 1)
|
|
habits_save(*habits_a)
|
|
check_int("add imported cold shower", habits_add_custom(*habits_a, "Cold Shower", Green(), 0, 0), 2)
|
|
habit_set_day(*habits_a, 2, 20260615, 1)
|
|
habits_save(*habits_a)
|
|
check_true("habit merge export", storage_export_zip(TextFromCString(*archive.bytes[0])) != 0)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
|
|
check_true("init habit merge dest", storage_init(dest) != 0)
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habit_set_day(*habits_a, 0, 20260612, 1)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
check_true("habit merge import", storage_import_zip(TextFromCString(*archive.bytes[0])) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("habit merge load", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("habit merge count", habits_b.count, 4)
|
|
meditation := find_habit(*habits_b, "Meditation")
|
|
check_true("habit merge meditation exists", meditation != null)
|
|
if meditation != null {
|
|
check_true("habit merge preserves local case",
|
|
TextFromCString(*meditation.name[0]) == "Meditation")
|
|
}
|
|
check_true("habit merge keeps local day", has_day(meditation, 20260612))
|
|
check_true("habit merge imports day", has_day(meditation, 20260613))
|
|
check_true("habit merge push ups day", has_day(find_habit(*habits_b, "Push ups"), 20260614))
|
|
check_true("habit merge cold shower day", has_day(find_habit(*habits_b, "Cold Shower"), 20260615))
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_import_conflict_prefers_data :: () {
|
|
source := make_root("conflict-source")
|
|
dest := make_root("conflict-dest")
|
|
archive: TextBuffer
|
|
append(*archive, source)
|
|
append(*archive, "/conflict-export.zip")
|
|
|
|
check_true("init conflict source", storage_init(source) != 0)
|
|
habits_a = Habits.{}
|
|
check_int("add conflict source habit", habits_add_custom(*habits_a, "Meditation", Orange(), 0, 0), 0)
|
|
storage_set_setting_text("language", "")
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
raw_exec(source, "INSERT OR REPLACE INTO habit_days(habit_id,local_date,completed,updated_at) VALUES('habit-1',20260618,0,0)")
|
|
check_true("reopen conflict source", storage_init(source) != 0)
|
|
check_true("conflict source export", storage_export_zip(TextFromCString(*archive.bytes[0])) != 0)
|
|
storage_close()
|
|
|
|
check_true("init conflict dest", storage_init(dest) != 0)
|
|
habits_a = Habits.{}
|
|
check_int("add conflict dest habit", habits_add_custom(*habits_a, "Meditation", Blue(), 0, 0), 0)
|
|
habit_set_day(*habits_a, 0, 20260618, 1)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_set_setting_text("language", "en")
|
|
check_true("conflict import", storage_import_zip_ex(TextFromCString(*archive.bytes[0]),
|
|
cast(StorageImportMode)1) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("conflict habits load", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_true("conflict keeps completed day", has_day(find_habit(*habits_b, "Meditation"), 20260618))
|
|
check_true("conflict keeps non-empty setting", storage_get_setting_text("language") == "en")
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_delete_all_resets :: () {
|
|
root := make_root("delete-all")
|
|
check_true("init delete all db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
check_int("add delete all habit", habits_add_custom(*habits_a, "Work out", Green(), 0, 0), 0)
|
|
habit_set_day(*habits_a, 0, 20260618, 1)
|
|
habits_save(*habits_a)
|
|
check_int("habit count before", storage_habit_count(), 1)
|
|
check_true("sees habit-only data", storage_has_any() != 0)
|
|
deleted := storage_delete_all_sessions()
|
|
check_true("delete removed habit data", deleted >= cast(s64)2)
|
|
check_int("habit count after", storage_habit_count(), 0)
|
|
check_true("hides active data", storage_has_any() == 0)
|
|
habits_add_default_set(*habits_a)
|
|
habits_free(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load default habit after delete all", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("default habit count after delete all", habits_b.count, 2)
|
|
check_true("default meditation", find_habit(*habits_b, "Meditation") != null)
|
|
check_true("default yoga", find_habit(*habits_b, "Yoga") != null)
|
|
check_true("old workout day removed", !has_day(find_habit(*habits_b, "Meditation"), 20260618))
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_empty_initialized_seeds_meditation :: () {
|
|
root := make_root("empty-initialized")
|
|
check_true("init db", storage_init(root) != 0)
|
|
storage_mark_habits_initialized()
|
|
storage_close()
|
|
raw_exec(root, "DELETE FROM habit_days; DELETE FROM habits;")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
habits_init(*habits_a)
|
|
check_int("startup seeds default habits", habits_a.count, 2)
|
|
meditation := find_habit(*habits_a, "Meditation")
|
|
yoga := find_habit(*habits_a, "Yoga")
|
|
check_true("seeds meditation", meditation != null)
|
|
check_true("seeds yoga", yoga != null)
|
|
if meditation != null {
|
|
check_int("meditation linked mode", meditation.sync_mode, 1)
|
|
check_int("meditation linked activities", meditation.sync_activity, 1 | 2 | 8)
|
|
}
|
|
if yoga != null {
|
|
check_int("yoga linked mode", yoga.sync_mode, 1)
|
|
check_int("yoga linked activities", yoga.sync_activity, 4)
|
|
}
|
|
habits_free(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load seeded defaults", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("seeded defaults persisted", habits_b.count, 2)
|
|
persisted := find_habit(*habits_b, "Meditation")
|
|
check_true("persisted meditation", persisted != null)
|
|
if persisted != null {
|
|
check_int("persisted meditation linked activities", persisted.sync_activity, 1 | 2 | 8)
|
|
}
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_defaults_wait_for_language :: () {
|
|
root := make_root("deferred-defaults")
|
|
check_true("init db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
habits_init_with_defaults(*habits_a, 0)
|
|
check_int("deferred startup keeps habits empty", habits_a.count, 0)
|
|
check_int("deferred startup does not initialize habits", storage_habits_initialized(), 0)
|
|
habits_free(*habits_a)
|
|
habits_a = Habits.{}
|
|
check_true("seed defaults after language setup", habits_seed_default_set_if_needed(*habits_a) != 0)
|
|
check_int("deferred defaults seeded", habits_a.count, 2)
|
|
check_true("deferred meditation", find_habit(*habits_a, "Meditation") != null)
|
|
check_true("deferred yoga", find_habit(*habits_a, "Yoga") != null)
|
|
check_int("deferred habits initialized", storage_habits_initialized(), 1)
|
|
habits_free(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load deferred seeded defaults", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("deferred persisted count", habits_b.count, 2)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_default_meditation_not_repaired :: () {
|
|
root := make_root("default-meditation")
|
|
check_true("init db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
check_int("add unlinked meditation", habits_add_custom(*habits_a, "Meditation", Blue(), 0, 0), 0)
|
|
habits_free(*habits_a)
|
|
habits_a = Habits.{}
|
|
habits_init(*habits_a)
|
|
check_int("no repair count", habits_a.count, 1)
|
|
meditation := find_habit(*habits_a, "Meditation")
|
|
check_true("no repair exists", meditation != null)
|
|
if meditation != null {
|
|
check_int("no repair sync mode", meditation.sync_mode, 0)
|
|
check_int("no repair sync activity", meditation.sync_activity, 0)
|
|
}
|
|
habits_free(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load unchanged meditation", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
persisted := find_habit(*habits_b, "Meditation")
|
|
if persisted != null {
|
|
check_int("persisted unchanged sync mode", persisted.sync_mode, 0)
|
|
check_int("persisted unchanged sync activity", persisted.sync_activity, 0)
|
|
} else {
|
|
check_true("persisted meditation exists", false)
|
|
}
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
write_multi_habit_source :: (root: string, archive: string) {
|
|
rounds: [1]s32 = .[77]
|
|
check_true("init multi habit source", storage_init(root) != 0)
|
|
check_true("save source session", storage_save_session_for_activity(*rounds[0], 1, 0, 1, null, 0) != 0)
|
|
habits_a = Habits.{}
|
|
check_int("add meditation", habits_add_custom(*habits_a, "Meditation", Orange(), 1, 1 | 2), 0)
|
|
check_int("add push ups", habits_add_custom(*habits_a, "Push ups", Purple(), 0, 0), 1)
|
|
check_int("add cold shower", habits_add_custom(*habits_a, "Cold Shower", Green(), 0, 0), 2)
|
|
habit_set_day(*habits_a, 0, 20260617, 1)
|
|
habits_save(*habits_a)
|
|
habit_set_day(*habits_a, 1, 20260617, 1)
|
|
habits_save(*habits_a)
|
|
habit_set_day(*habits_a, 2, 20260617, 1)
|
|
habits_save(*habits_a)
|
|
habits_free(*habits_a)
|
|
storage_set_setting_int("speed", 7)
|
|
storage_set_setting_int("sun_salutation_character", 3)
|
|
storage_set_setting_int("sun_salutation_repetitions", 6)
|
|
storage_set_setting_int("sun_salutation_start_seconds", 9)
|
|
storage_set_setting_int("sun_salutation_end_seconds", 4)
|
|
storage_set_setting_int("audio_custom_music_count", 1)
|
|
storage_set_setting_text("audio_custom_music_0_title", "Test track")
|
|
storage_set_setting_text("audio_custom_music_0_path", "/tmp/test-track.ogg")
|
|
storage_set_setting_text("audio_custom_music_0_file", "ignore-me")
|
|
storage_set_setting_text("language", "en")
|
|
storage_set_setting_text("future_unknown_key", "ignore-me")
|
|
check_true("export multi habit source", storage_export_zip(archive) != 0)
|
|
storage_close()
|
|
}
|
|
|
|
assert_multi_habits_imported :: (root: string, want_speed: s32, expect_settings: bool) {
|
|
check_true("init multi habit import db", storage_init(root) != 0)
|
|
check_int("imported sessions", storage_session_count(), 1)
|
|
habits_b = Habits.{}
|
|
check_true("multi habit load", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("multi habit count", habits_b.count, 3)
|
|
meditation := find_habit(*habits_b, "Meditation")
|
|
check_true("multi meditation exists", meditation != null)
|
|
if meditation != null {
|
|
check_int("multi meditation sync mode", meditation.sync_mode, 1)
|
|
check_int("multi meditation sync activity", meditation.sync_activity, 1 | 2)
|
|
}
|
|
check_true("multi meditation day", has_day(meditation, 20260617))
|
|
check_true("multi push ups day", has_day(find_habit(*habits_b, "Push ups"), 20260617))
|
|
check_true("multi cold shower day", has_day(find_habit(*habits_b, "Cold Shower"), 20260617))
|
|
habits_free(*habits_b)
|
|
check_int("import speed setting", storage_get_setting_int("speed", -1), want_speed)
|
|
if expect_settings {
|
|
check_int("sun character", storage_get_setting_int("sun_salutation_character", -1), 3)
|
|
check_int("sun reps", storage_get_setting_int("sun_salutation_repetitions", -1), 6)
|
|
check_int("sun start", storage_get_setting_int("sun_salutation_start_seconds", -1), 9)
|
|
check_int("sun end", storage_get_setting_int("sun_salutation_end_seconds", -1), 4)
|
|
check_int("audio count", storage_get_setting_int("audio_custom_music_count", -1), 1)
|
|
check_true("audio title", storage_get_setting_text("audio_custom_music_0_title") == "Test track")
|
|
check_true("audio path", storage_get_setting_text("audio_custom_music_0_path") == "/tmp/test-track.ogg")
|
|
} else {
|
|
check_int("data-only skips sun character", storage_get_setting_int("sun_salutation_character", -1), -1)
|
|
check_int("data-only skips sun reps", storage_get_setting_int("sun_salutation_repetitions", -1), -1)
|
|
check_int("data-only skips audio count", storage_get_setting_int("audio_custom_music_count", -1), -1)
|
|
}
|
|
check_true("malformed audio setting ignored",
|
|
storage_get_setting_text("audio_custom_music_0_file").count == 0)
|
|
check_true("unknown setting ignored", storage_get_setting_text("future_unknown_key").count == 0)
|
|
storage_close()
|
|
}
|
|
|
|
test_import_modes :: () {
|
|
source := make_root("multi-source")
|
|
dest_data := make_root("multi-dest-data")
|
|
dest_settings := make_root("multi-dest-settings")
|
|
archive: TextBuffer
|
|
append(*archive, source)
|
|
append(*archive, "/multi-export.zip")
|
|
archive_path := TextFromCString(*archive.bytes[0])
|
|
write_multi_habit_source(source, archive_path)
|
|
|
|
check_true("init inspect dest", storage_init(dest_data) != 0)
|
|
info: StorageImportInfo
|
|
check_true("inspect multi export", storage_inspect_import(archive_path, *info) != 0)
|
|
check_true("inspect valid", info.valid != 0)
|
|
check_true("inspect sessions", info.has_sessions != 0)
|
|
check_true("inspect habits", info.has_habits != 0)
|
|
check_true("inspect settings", info.has_settings != 0)
|
|
check_int("inspect habit count", info.habit_count, 3)
|
|
storage_set_setting_int("speed", 1)
|
|
check_true("data only import",
|
|
storage_import_zip_ex(archive_path, cast(StorageImportMode)0) != 0)
|
|
storage_close()
|
|
assert_multi_habits_imported(dest_data, 1, false)
|
|
|
|
check_true("init settings import dest", storage_init(dest_settings) != 0)
|
|
storage_set_setting_int("speed", 1)
|
|
check_true("data and settings import",
|
|
storage_import_zip_ex(archive_path, cast(StorageImportMode)1) != 0)
|
|
storage_close()
|
|
assert_multi_habits_imported(dest_settings, 7, true)
|
|
}
|
|
|
|
metadata_topic: s32;
|
|
metadata_activity: s32;
|
|
|
|
record_metadata :: (path: string, year: s32, month: s32, day: s32, hour: s32,
|
|
minute: s32, second: s32, topic: s32, activity: s32, round_times: []s32,
|
|
round_count: s32, user: s64) {
|
|
unused path
|
|
unused year
|
|
unused month
|
|
unused day
|
|
unused hour
|
|
unused minute
|
|
unused second
|
|
unused round_times
|
|
unused round_count
|
|
unused user
|
|
metadata_topic = topic
|
|
metadata_activity = activity
|
|
}
|
|
|
|
test_session_metadata :: () {
|
|
root := make_root("metadata")
|
|
rounds: [2]s32 = .[30, 45]
|
|
check_true("init metadata db", storage_init(root) != 0)
|
|
check_true("save metadata session",
|
|
storage_save_session_for_activity(*rounds[0], 2, 2, 3, null, 0) != 0)
|
|
metadata_topic = -1
|
|
metadata_activity = -1
|
|
callback: StorageSessionRecordCallback = record_metadata
|
|
storage_list_session_records(callback, null)
|
|
check_int("metadata topic", metadata_topic, 2)
|
|
check_int("metadata activity", metadata_activity, 3)
|
|
storage_close()
|
|
}
|
|
|
|
test_profile_week_stats :: () {
|
|
root := make_root("profile-week")
|
|
rounds: [1]s32 = .[45]
|
|
check_true("init profile week db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habits_save(*habits_a)
|
|
check_true("session today", storage_save_session_at_for_activity(20260715, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0)
|
|
check_true("session earlier", storage_save_session_at_for_activity(20260713, 9, 0, 0, *rounds[0], 1, 0, 1, null, 0) != 0)
|
|
check_true("session outside week", storage_save_session_at_for_activity(20260708, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0)
|
|
habit_id := TextFromCString(*habits_a.items[0].id[0])
|
|
check_true("habit distinct day", storage_habit_day_save(habit_id, 20260714, 1, 1) != 0)
|
|
check_true("habit overlapping day", storage_habit_day_save(habit_id, 20260713, 1, 1) != 0)
|
|
active_days: s32 = -1
|
|
practice_sessions: s32 = -1
|
|
check_true("week stats call", storage_profile_week_stats(20260715, *active_days, *practice_sessions) != 0)
|
|
check_int("week active days", active_days, 3)
|
|
check_int("week practice sessions", practice_sessions, 2)
|
|
// The week crosses a month boundary; 20260627 is seven days back.
|
|
check_true("session start of month", storage_save_session_at_for_activity(20260701, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0)
|
|
check_true("session end of month", storage_save_session_at_for_activity(20260628, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0)
|
|
check_true("session before week", storage_save_session_at_for_activity(20260627, 9, 0, 0, *rounds[0], 1, 0, 0, null, 0) != 0)
|
|
check_true("month week stats call", storage_profile_week_stats(20260704, *active_days, *practice_sessions) != 0)
|
|
check_int("month week active days", active_days, 2)
|
|
check_int("month week practice sessions", practice_sessions, 2)
|
|
habits_free(*habits_a)
|
|
storage_close()
|
|
}
|
|
|
|
test_linked_counts_materialize :: () {
|
|
root := make_root("linked-counts")
|
|
first: [1]s32 = .[30]
|
|
second: [1]s32 = .[45]
|
|
first_id: [44]u8
|
|
second_id: [44]u8
|
|
today := habits_today_index()
|
|
check_true("init linked counter db", storage_init(root) != 0)
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
check_true("save session one", storage_save_session_for_activity(*first[0], 1, 0, 1, *first_id[0], 44) != 0)
|
|
check_true("save session two", storage_save_session_for_activity(*second[0], 1, 0, 1, *second_id[0], 44) != 0)
|
|
habits_free(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load linked habits", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("linked sessions materialize count", day_count_of(*habits_b.items[0], today), 2)
|
|
habit_set_day_count(*habits_b, 0, today, 0)
|
|
habits_save(*habits_b)
|
|
habits_free(*habits_b)
|
|
habits_b = Habits.{}
|
|
check_true("reload after manual clear", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("manual clear overrides linked sessions", day_count_of(*habits_b.items[0], today), 0)
|
|
habit_set_day_count(*habits_b, 0, today, 2)
|
|
habits_save(*habits_b)
|
|
check_true("delete linked session one", storage_delete_session(TextFromCString(*first_id[0])) != 0)
|
|
habits_free(*habits_b)
|
|
habits_b = Habits.{}
|
|
check_true("reload after delete", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("delete lowers derived count", day_count_of(*habits_b.items[0], today), 1)
|
|
habit_set_day_count(*habits_b, 0, today, 4)
|
|
habits_save(*habits_b)
|
|
check_true("delete linked session two", storage_delete_session(TextFromCString(*second_id[0])) != 0)
|
|
habits_free(*habits_b)
|
|
habits_b = Habits.{}
|
|
check_true("reload after manual override", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("delete preserves manual count", day_count_of(*habits_b.items[0], today), 4)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_existing_sessions_materialize :: () {
|
|
root := make_root("existing-linked")
|
|
rounds: [1]s32 = .[30]
|
|
today := habits_today_index()
|
|
check_true("init db", storage_init(root) != 0)
|
|
check_true("save existing session", storage_save_session_for_activity(*rounds[0], 1, 0, 1, null, 0) != 0)
|
|
habits_a = Habits.{}
|
|
habits_add_default_set(*habits_a)
|
|
habits_free(*habits_a)
|
|
habits_b = Habits.{}
|
|
check_true("load habits", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("existing session materializes after habit save", day_count_of(*habits_b.items[0], today), 1)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
// External meditation logs count towards the profile streak and average; a
|
|
// log for a session that exists is not counted twice.
|
|
test_meditation_logs_feed_profile_stats :: () {
|
|
root := make_root("meditation-log-stats")
|
|
check_true("init db", storage_init(root) != 0)
|
|
storage_close()
|
|
raw_exec(root, "INSERT INTO sessions(id,user_id,started_at,local_date,topic,activity,source,imported_at,rounds_hash,deleted_at,updated_at) VALUES('med-session-1',(SELECT id FROM users LIMIT 1),strftime('%s','2026-07-15T00:00:00Z'),20260715,0,1,'sync','1','101',0,strftime('%s','2026-07-15T00:00:00Z'));INSERT INTO session_rounds(session_id,round_index,seconds) VALUES('med-session-1',0,600);INSERT INTO meditation_logs(id,user_id,session_id,duration_seconds,completed_at,updated_at) VALUES('med-log-external',(SELECT id FROM users LIMIT 1),'external-log-session',1200,strftime('%s','2026-07-14T00:00:00Z'),1),('med-log-duplicate',(SELECT id FROM users LIMIT 1),'med-session-1',3000,strftime('%s','2026-07-15T00:00:00Z'),1);")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
streak: s32 = -1
|
|
average: s64 = -1
|
|
check_true("stats call", storage_profile_activity_stats(1, 20260715, *streak, *average) != 0)
|
|
check_int("streak counts the external log day", streak, 2)
|
|
check_int("average counts each session once", cast(s32)average, 900)
|
|
check_true("stale day stats call", storage_profile_activity_stats(1, 20260716, *streak, *average) != 0)
|
|
check_int("streak resets on a stale day", streak, 0)
|
|
storage_close()
|
|
}
|
|
|
|
// Habit ids that are full UUIDs load intact, with their days.
|
|
test_uuid_habit_ids_load_days :: () {
|
|
root := make_root("uuid-habit-load")
|
|
check_true("init db", storage_init(root) != 0)
|
|
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('940dd8b7-0d60-48ce-9c8f-433b9170b6b1',(SELECT id FROM users LIMIT 1),'Meditation',1,2,3,0,0,1,0,0,1782200000);INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at) VALUES('940dd8b7-0d60-48ce-9c8f-433b9170b6b1',20260629,1,1,0,1782200000);")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("load habits", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
check_int("habit count", habits_b.count, 1)
|
|
check_true("uuid id is not truncated",
|
|
TextFromCString(*habits_b.items[0].id[0]) == "940dd8b7-0d60-48ce-9c8f-433b9170b6b1")
|
|
check_int("habit day loaded", habits_b.items[0].day_count, 1)
|
|
if habits_b.items[0].day_count > 0 {
|
|
check_int("habit day date", habits_b.items[0].days[0].day_index, 20260629)
|
|
}
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
// Questions travel with the habit; a day keeps its answers with the
|
|
// question's words, so they read the same after the question goes.
|
|
test_habit_questions_and_answers :: () {
|
|
root := make_root("habit-questions")
|
|
check_true("init db", storage_init(root) != 0)
|
|
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('meal',(SELECT id FROM users LIMIT 1),'First meal',1,2,3,0,0,0,0,0,1782200000);")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("load habit", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
habit := *habits_b.items[0]
|
|
check_int("no questions at first", habit.question_count, 0)
|
|
check_true("add a time question",
|
|
HabitQuestionAdd(habit.questions[:], *habit.question_count, 1782200000))
|
|
unused AppCopyText("When did I start \"eating\"?", habit.questions[0].text[:])
|
|
habit.questions[0].answer_type = HABIT_ANSWER_TIME
|
|
check_true("add a number question",
|
|
HabitQuestionAdd(habit.questions[:], *habit.question_count, 1782200000))
|
|
unused AppCopyText("Plates", habit.questions[1].text[:])
|
|
habit.questions[1].answer_type = HABIT_ANSWER_NUMBER
|
|
check_true("add a question without words",
|
|
HabitQuestionAdd(habit.questions[:], *habit.question_count, 1782200000))
|
|
HabitQuestionsKeepWorded(habit.questions[:], *habit.question_count)
|
|
check_int("a question without words is dropped", habit.question_count, 2)
|
|
check_true("each question has its own id",
|
|
habit.questions[0].id != habit.questions[1].id)
|
|
time_id := habit.questions[0].id
|
|
storage_habits_save(cast(*void)*habits_b)
|
|
check_int("the habit is queued to sync",
|
|
read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='habit' AND entity_id='meal'"), 1)
|
|
habits_free(*habits_b)
|
|
habits_b = Habits.{}
|
|
check_true("reload habit", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
habit = *habits_b.items[0]
|
|
check_int("questions kept", habit.question_count, 2)
|
|
check_true("words kept, quotes too",
|
|
HabitQuestionText(*habit.questions[0]) == "When did I start \"eating\"?")
|
|
check_int("answer type kept", habit.questions[0].answer_type, HABIT_ANSWER_TIME)
|
|
check_true("id kept", habit.questions[0].id == time_id)
|
|
|
|
none: [1]HabitAnswer
|
|
answers: [HABIT_DAY_ANSWER_MAX]HabitAnswer
|
|
count := HabitAnswersForDay(habit, none[:], 0, answers[:])
|
|
check_int("a day asks every question", count, 2)
|
|
unused AppCopyText("07:30", answers[0].value[:])
|
|
unused AppCopyText("2", answers[1].value[:])
|
|
check_true("save answers",
|
|
storage_habit_day_answers_save("meal", 20260701, answers[:], count) != 0)
|
|
check_int("the day is queued to sync",
|
|
read_db_count("SELECT COUNT(*) FROM sync_outbox WHERE entity_type='habit_day' AND entity_id='meal' AND local_date=20260701"), 1)
|
|
loaded: [HABIT_DAY_ANSWER_MAX]HabitAnswer
|
|
loaded_count := storage_habit_day_answers_load("meal", 20260701, loaded[:])
|
|
check_int("answers loaded", loaded_count, 2)
|
|
check_true("time answer loaded", HabitAnswerValue(*loaded[0]) == "07:30" &&
|
|
loaded[0].id == time_id && loaded[0].answer_type == HABIT_ANSWER_TIME)
|
|
check_true("question words kept with the answer",
|
|
HabitAnswerQuestion(*loaded[0]) == "When did I start \"eating\"?")
|
|
|
|
HabitQuestionRemove(habit.questions[:], *habit.question_count, 0)
|
|
shown := HabitAnswersForDay(habit, loaded[:], loaded_count, answers[:])
|
|
check_int("a removed question's answer still shows", shown, 2)
|
|
check_true("current questions come first",
|
|
HabitAnswerQuestion(*answers[0]) == "Plates" &&
|
|
HabitAnswerValue(*answers[0]) == "2")
|
|
check_true("the removed question keeps its words",
|
|
HabitAnswerValue(*answers[1]) == "07:30" && answers[1].id == time_id)
|
|
|
|
cleared := storage_habit_day_answers_save("meal", 20260701, none[:], 0)
|
|
check_true("clear answers", cleared != 0 &&
|
|
storage_habit_day_answers_load("meal", 20260701, loaded[:]) == 0)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
}
|
|
|
|
test_answer_statistics :: () {
|
|
root := make_root("habit-answer-stats")
|
|
check_true("init db", storage_init(root) != 0)
|
|
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,questions) VALUES('sleep',(SELECT id FROM users LIMIT 1),'Sleep',1,2,3,0,0,0,0,0,1782200000,'[{\"id\":1,\"type\":\"time\",\"text\":\"Bedtime\"},{\"id\":2,\"type\":\"number\",\"text\":\"Pages\"},{\"id\":3,\"type\":\"yes_no\",\"text\":\"Tea\"},{\"id\":4,\"type\":\"text\",\"text\":\"Dream\"}]');INSERT INTO habit_days(habit_id,local_date,completed,count,session_count,updated_at,answers) VALUES('sleep',20260701,1,1,0,1782200000,'[{\"id\":1,\"type\":\"time\",\"question\":\"Bedtime\",\"value\":\"23:30\"},{\"id\":2,\"type\":\"number\",\"question\":\"Pages\",\"value\":\"2\"},{\"id\":3,\"type\":\"yes_no\",\"question\":\"Tea\",\"value\":\"yes\"},{\"id\":4,\"type\":\"text\",\"question\":\"Dream\",\"value\":\"Flying\"}]'),('sleep',20260702,1,1,0,1782200000,'[{\"id\":1,\"type\":\"time\",\"question\":\"Bedtime\",\"value\":\"00:30\"},{\"id\":2,\"type\":\"number\",\"question\":\"Pages\",\"value\":\"3\"},{\"id\":3,\"type\":\"yes_no\",\"question\":\"Tea\",\"value\":\"no\"},{\"id\":4,\"type\":\"text\",\"question\":\"Dream\",\"value\":\"Sea\"}]');")
|
|
check_true("reopen db", storage_init(root) != 0)
|
|
habits_b = Habits.{}
|
|
check_true("load habit", storage_habits_load(cast(*void)*habits_b) != 0)
|
|
habit := *habits_b.items[0]
|
|
check_int("questions read from JSON", habit.question_count, 4)
|
|
check_int("yes or no read", habit.questions[2].answer_type, HABIT_ANSWER_YES_NO)
|
|
days: [HABIT_ANSWER_STATS_DAYS]s32
|
|
slot: s32 = 0
|
|
while slot < HABIT_ANSWER_STATS_DAYS {
|
|
days[slot] = 20260701 + slot
|
|
slot += 1
|
|
}
|
|
stats: HabitAnswerStats
|
|
text: [16]u8
|
|
unused storage_habit_answer_stats("sleep", *habit.questions[0], days[:], *stats)
|
|
check_int("bedtimes answered", stats.answered, 2)
|
|
check_true("bedtimes around midnight average to midnight",
|
|
HabitAnswerTimeText(cast(s32)((HabitAnswerStatsAverage100(*stats) + 50) / 100),
|
|
text[:]) == "00:00")
|
|
check_true("earliest bedtime is 23:30",
|
|
HabitAnswerTimeText(stats.lowest, text[:]) == "23:30")
|
|
check_true("latest bedtime is 00:30",
|
|
HabitAnswerTimeText(stats.highest, text[:]) == "00:30")
|
|
unused storage_habit_answer_stats("sleep", *habit.questions[1], days[:], *stats)
|
|
check_true("pages sum", stats.sum == 5)
|
|
check_true("pages average 2.5",
|
|
HabitAnswerDecimalText(HabitAnswerStatsAverage100(*stats), text[:]) == "2.5")
|
|
check_int("first day's pages in its slot", stats.values[0], 2)
|
|
check_int("an unanswered day has no value", stats.values[2], -1)
|
|
unused storage_habit_answer_stats("sleep", *habit.questions[2], days[:], *stats)
|
|
check_true("one yes and one no", stats.yes == 1 && stats.no == 1)
|
|
unused storage_habit_answer_stats("sleep", *habit.questions[3], days[:], *stats)
|
|
check_true("newest text answer first", stats.recent_count == 2 &&
|
|
AppTextFromCString(*stats.recent[0][0], HABIT_ANSWER_SIZE) == "Sea" &&
|
|
stats.recent_days[0] == 20260702)
|
|
habits_free(*habits_b)
|
|
storage_close()
|
|
|
|
day_times: [3]s32 = .[420, 480, 540]
|
|
check_int("times through the day keep midnight", HabitAnswerTimeCut(day_times[:]), -1)
|
|
typed: [8]u8
|
|
check_true("typed 7 and 5 read 07:05",
|
|
HabitTimeFromTyped("7", "5", typed[:]) &&
|
|
AppTextFromCString(*typed[0], 8) == "07:05")
|
|
check_true("no hours, no time",
|
|
HabitTimeFromTyped("", "30", typed[:]) &&
|
|
AppTextFromCString(*typed[0], 8) == "")
|
|
check_true("24 hours is not a time", !HabitTimeFromTyped("24", "00", typed[:]))
|
|
hours: s32 = 0
|
|
minutes: s32 = 0
|
|
check_true("9:05 reads", HabitTimeParts("9:05", *hours, *minutes) &&
|
|
hours == 9 && minutes == 5)
|
|
check_true("9:5 does not", !HabitTimeParts("9:5", *hours, *minutes))
|
|
}
|
|
|
|
#program_export
|
|
Check :: () -> s32 {
|
|
guard := support_begin("/tmp/inbe-storage-habits-zi-test/")
|
|
if guard != 0 { return guard }
|
|
test_habit_name_merge_import()
|
|
test_import_conflict_prefers_data()
|
|
test_delete_all_resets()
|
|
test_empty_initialized_seeds_meditation()
|
|
test_defaults_wait_for_language()
|
|
test_default_meditation_not_repaired()
|
|
test_import_modes()
|
|
test_session_metadata()
|
|
test_profile_week_stats()
|
|
test_linked_counts_materialize()
|
|
test_existing_sessions_materialize()
|
|
test_meditation_logs_feed_profile_stats()
|
|
test_uuid_habit_ids_load_days()
|
|
test_habit_questions_and_answers()
|
|
test_answer_statistics()
|
|
return failures
|
|
}
|