Files
breeze/tests/cells_behavior.zi
T
waozi 7b65ddaf5e Let habits ask questions when a day is done, with their statistics
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.
2026-10-13 13:56:21 -03:00

571 lines
27 KiB
Ziran

#import, file "lumi_cells_behavior.zi";
#import, file "cells_locale_behavior.zi";
#import, file "../src/cells/module_host.zi";
#import, file "../src/cells/asset_paths.zi";
#import, file "../src/cells/module_types.zi";
c_cache :: #system_library "c";
ModuleTestUseCache :: (enabled: bool) #foreign c_cache;
ModuleTestCachedReads :: (index: s32) -> s32 #foreign c_cache;
ModuleTestBundleOpens :: () -> s32 #foreign c_cache;
ModuleTestInstanceOpens :: () -> s32 #foreign c_cache;
ModuleTestCorruptCachedLists :: (enabled: bool) #foreign c_cache;
ModuleTestFailInstantiate :: () #foreign c_cache;
ModuleTestFailRun :: () #foreign c_cache;
#import, file "../src/cells/shared_state.zi";
#import, file "../src/cells/lists_types.zi";
#import, file "../src/cells/lists_visibility.zi";
#import, file "../src/cells/habits_types.zi";
#import, file "../src/cells/practice_types.zi";
#import, file "../src/cells/inbe_types.zi";
#import, file "../src/cells/diary_types.zi";
Dates :: #import, file "../apps/diary/diary_date.zi";
#import, file "../src/cells/diary_store.zi";
#import, file "../src/cells/package_files.zi";
#import, file "../src/app/app_types.zi";
#import, file "../src/core/types.zi";
#import, file "../src/core/breath_timing.zi";
#import, file "../src/practices/sun_salutation/sun_salutation_rules.zi";
#import, file "../src/storage/db.zi";
#import, file "../src/storage/data.zi";
#import, file "../src/storage/storage_core.zi";
#import, file "../src/storage/storage_elist.zi";
#import, file "../src/storage/storage_habits.zi";
#import, file "../src/storage/storage_sessions.zi";
#import, file "../src/storage/state.zi";
#import, file "../src/storage/legacy_session_files.zi";
#import, file "../src/storage/legacy_session_time.zi";
#import, file "../src/storage/storage_thread_buffers.zi";
#import, file "../src/app/host_services.zi";
#import, file "../src/screens/elist_types.zi";
#import, file "../src/habits/habit_model.zi";
#import, file "../src/habits/habit_types.zi";
#import, file "../src/habits/habit_memory.zi";
#import, file "../src/app/text_buffers.zi";
#import, file "../src/app/app_size.zi";
#import "byte_text_linux"
#import, file "../src/app/assets.zi";
#import, file "../src/app/locale.zi";
c :: #system_library "c";
ModuleTestText :: (name: string) -> string #foreign c;
ModuleTestCount :: (sql: string) -> s32 #foreign c;
ModuleTestSeedOld :: () -> bool #foreign c;
ModuleTestRuns :: () -> s32 #foreign c;
ModuleTestPackageBytes :: () -> bool #foreign c;
app: InnerBreeze;
list_id: [37]u8;
item_id: [37]u8;
user_id: [128]u8;
migrated_habit_id: [HABIT_ID_SIZE]u8;
edited_habit_id: [HABIT_ID_SIZE]u8;
failures: s32;
FindHabit :: (name: string) -> s32 {
index: s32 = 0
while index < app.habits.count {
if AppTextFromCString(*app.habits.items[index].name[0], HABIT_NAME_SIZE) == name {
return index
}
index += 1
}
return -1
}
Expect :: (ok: bool, code: s32) {
if !ok {
print("Module check failed: %\n", code)
if failures == 0 {
failures = code
}
}
}
CheckRootConfiguration :: () {
resources: [4]string = .["assets/lists/check.png", "assets/habits/leaf.png",
"assets/practices/breath.png", "assets/diary/calendar.png"]
kinds: [4]s32 = .[MODULE_LISTS, MODULE_HABITS, MODULE_PRACTICE, MODULE_DIARY]
for index: cast(s32)0..cast(s32)3 {
Expect(PackageResourceValid(resources[index], kinds[index]), 120 + index)
Expect(!PackageResourceValid(resources[index], kinds[(index + 1) % 4]), 124 + index)
}
unsafe: [8]string = .["assets/../apps/publishers.json", "assets//image.png",
"assets/image.png/", "/assets/image.png", "assets/./image.png",
"apps/publishers.json", "diary/2026-10-06.json", "assets/image:png"]
for index: cast(s32)0..cast(s32)7 {
Expect(!PackageResourceValid(unsafe[index], MODULE_INBE), 128 + index)
}
Expect(ModuleTestPackageBytes(), 74)
Expect(!CellsOpenPackage(""), 57)
Expect(!CellsOpenPackage(ModuleTestText("missing-assets")), 58)
Expect(!CellsOpenPackage(ModuleTestText("invalid-nested")), 59)
Expect(CellsOpenPackage(ModuleTestText("inbe")), 60)
Expect(CellsPackageSize(0) == ModuleTestText("inbe").count, 221)
names: [6]string = .["lists", "habits", "practice", "diary", "lumi", "leaderboard"]
for index: cast(s32)1..6 {
Expect(CellsPackageSize(index) == ModuleTestText(names[index - 1]).count, 222 + index)
}
Expect(CellsPackageSize(-1) == 0 && CellsPackageSize(7) == 0, 228)
size_text: [32]u8
Expect(AppBundleSizeText(66076, size_text[:]) &&
AppTextFromCString(*size_text[0], 32) == "66.1 kB", 230)
Expect(AppBundleSizeText(26131884, size_text[:]) &&
AppTextFromCString(*size_text[0], 32) == "26.1 MB", 231)
Expect(AppBundleSizeText(999950, size_text[:]) &&
AppTextFromCString(*size_text[0], 32) == "1.0 MB", 232)
Expect(!AppBundleSizeText(0, size_text[:]) && size_text[0] == cast(u8)0, 233)
art := AssetRead("assets/practices/whm/banner-light.png")
Expect(art.found && art.data.count > 8 && art.data[0] == cast(u8)137 && art.mime == "image/png", 106)
language := AssetRead("locales/es.txt")
Expect(language.found && language.data.count > 1000, 107)
Expect(!AssetRead("apps/publishers.json").found, 108)
initial := InbeRun(InbeMessage.{.action = INBE_INITIAL_SCREEN,
.enabled_apps = APP_ALL, .screen = ScreenStart})
Expect(initial.succeeded && initial.screen == ScreenLanguage, 61)
initial.language_selected = true
initial = InbeRun(initial)
Expect(initial.succeeded && initial.screen == ScreenAppSelection, 62)
mask: s32 = 0
while mask <= APP_ALL {
Expect(CellsSetEnabled(mask) && CellsReady(), 63)
Expect(CellsInstalledMask() == APP_ALL, 219)
Expect(InbeAllows(ScreenLumi) &&
InbeAllows(ScreenEList) == ((mask & APP_LISTS) != 0) &&
InbeAllows(ScreenHabits) == ((mask & APP_HABITS) != 0) &&
InbeAllows(ScreenPatterns) == ((mask & APP_PRACTICE) != 0), 64)
Expect(InbeAllows(ScreenSettings) && InbeAllows(ScreenAppSelection), 65)
initial = InbeMessage.{.action = INBE_INITIAL_SCREEN,
.enabled_apps = mask, .screen = ScreenStart,
.language_selected = true, .apps_selected = true}
initial = InbeRun(initial)
expected: s32 = ScreenSettings
if (mask & APP_PRACTICE) != 0 {
expected = ScreenStart
} else if (mask & APP_LUMI) != 0 {
expected = ScreenLumi
} else if (mask & APP_HABITS) != 0 {
expected = ScreenHabits
} else if (mask & APP_LISTS) != 0 {
expected = ScreenEList
} else if (mask & APP_DIARY) != 0 {
expected = ScreenDiary
}
Expect(initial.succeeded && initial.screen == expected, 66)
if (mask & APP_LISTS) == 0 {
Expect(ListsRun(ListsMessage.{.action = LISTS_ROUTE,
.screen = ScreenEList}).succeeded, 67)
}
if (mask & APP_HABITS) == 0 {
Expect(HabitsRun(HabitsMessage.{.action = HABITS_ROUTE,
.screen = ScreenHabits}).succeeded, 68)
}
if (mask & APP_PRACTICE) == 0 {
Expect(PracticeRun(PracticeMessage.{.action = PRACTICE_ROUTE,
.screen = ScreenStart}).succeeded, 69)
}
mini := InbeRun(InbeMessage.{.action = INBE_MINI_CONFIGURE,
.enabled_apps = mask})
Expect(mini.succeeded && mini.enabled_apps == APP_PRACTICE &&
mini.screen == ScreenStart, 75)
Expect(CellsSetEnabled(mini.enabled_apps) &&
InbeAllows(ScreenStart) && !InbeAllows(ScreenEList), 76)
mini_session := PracticeRun(PracticeMessage.{
.action = PRACTICE_MEDITATION_STEP,
.remaining_seconds = 1, .elapsed_ms = 1000})
Expect(mini_session.succeeded && mini_session.completed, 77)
mask += 1
}
toggle := InbeRun(InbeMessage.{.action = INBE_TOGGLE_APP,
.enabled_apps = APP_ALL, .app = APP_PRACTICE, .checked = false})
Expect(toggle.succeeded && toggle.enabled_apps == (APP_ALL & ~APP_PRACTICE), 70)
toggle.app = APP_LISTS | APP_HABITS
Expect(!InbeRun(toggle).succeeded, 71)
Expect(CellsSetEnabled(APP_ALL), 72)
instances := ModuleTestInstanceOpens()
for frame: 0..199 {
unused frame
ready := ListsUiRun(ListsMessage.{.action = LISTS_ROUTE, .screen = ScreenEList})
root := InbeUiRun(InbeMessage.{.action = INBE_ROUTE, .screen = ScreenEList,
.enabled_apps = APP_ALL})
Expect(ready.succeeded && root.succeeded && root.allowed, 235)
}
Expect(ModuleTestInstanceOpens() == instances, 236)
ModuleTestFailRun()
failed := ListsUiRun(ListsMessage.{.action = LISTS_ROUTE, .screen = ScreenEList})
Expect(!failed.succeeded, 237)
ModuleTestFailInstantiate()
reset := ListsUiRun(ListsMessage.{.action = LISTS_ROUTE, .screen = ScreenEList})
Expect(!reset.succeeded && CellsReady(), 220)
recovered := ListsUiRun(ListsMessage.{.action = LISTS_ROUTE, .screen = ScreenEList})
Expect(recovered.succeeded && InbeAllows(ScreenEList), 238)
Expect(CellsPackageSize(1) == ModuleTestText("lists").count, 234)
runs := ModuleTestRuns()
Expect(InbeAllows(ScreenEList) && ModuleTestRuns() == runs + 2, 73)
}
CheckDiary :: () {
Expect(CellsValidatePackage(4, ModuleTestText("diary")), 80)
Expect(!CellsValidatePackage(4, ModuleTestText("habits")), 81)
instances := ModuleTestInstanceOpens()
for frame: 0..49 {
unused frame
loaded := DiaryRun(DiaryMessage.{.action = DIARY_LOAD,
.year = 2024, .month = 2, .day = 28})
Expect(loaded.succeeded, 239)
}
Expect(ModuleTestInstanceOpens() == instances, 240)
state := DiaryRun(DiaryMessage.{.action = DIARY_LOAD, .year = 2024, .month = 2, .day = 28})
Expect(state.succeeded && state.saved && !state.load_error && state.text == "", 82)
state.action = DIARY_EDIT
state.replacement = "A calm day 🌿\nSecond line"
state = DiaryRun(state)
Expect(state.succeeded && !state.saved && state.text == "A calm day 🌿\nSecond line", 83)
// Routing must not overwrite unsaved text held by the editor.
Expect(InbeAllows(ScreenDiary), 84)
state.action = DIARY_CONTROL
state.control = 29002
state = DiaryRun(state)
Expect(state.succeeded && state.day == 29 && state.month == 2 && state.saved, 85)
loaded := DiaryRun(DiaryMessage.{.action = DIARY_LOAD, .year = 2024, .month = 2, .day = 28})
Expect(loaded.succeeded && loaded.text == "A calm day 🌿\nSecond line", 86)
loaded.action = DIARY_CONTROL
loaded.control = 29003
loaded = DiaryRun(loaded)
Expect(loaded.calendar && loaded.month_days == 29 && loaded.month_weekday == 3 && loaded.marks[28] == cast(u8)1, 87)
loaded.control = 29129
loaded = DiaryRun(loaded)
Expect(loaded.succeeded && !loaded.calendar && loaded.day == 29, 88)
Expect(!DiaryStoreSave("../outside.json", "invalid"), 89)
Expect(DiaryStoreSave("2024-03-01.json", "{\"version\":1,\"date\":\"2024-03-01\",\"text\":\"Before\",\"photos\":[{\"file\":\"photo.png\",\"name\":\"Family\"}]}"), 90)
photo := DiaryRun(DiaryMessage.{.action = DIARY_LOAD, .year = 2024, .month = 3, .day = 1})
photo.action = DIARY_EDIT
photo.edit_start = 6
photo.edit_end = 6
photo.replacement = " after"
photo = DiaryRun(photo)
photo.action = DIARY_SAVE
photo = DiaryRun(photo)
Expect(photo.saved && photo.document == "{\"version\":1,\"date\":\"2024-03-01\",\"text\":\"Before after\",\"photos\":[{\"file\":\"photo.png\",\"name\":\"Family\"}]}", 91)
Expect(photo.photo_count == 1 && photo.photo_name == "Family", 100)
photo.action = DIARY_ATTACH
photo.attachment_file = "photo-two.bin"
photo.attachment_name = "A second photo 🌿"
photo = DiaryRun(photo)
Expect(photo.succeeded && photo.saved && photo.photo_count == 2 &&
photo.photo_index == 1 && photo.photo_name == "A second photo 🌿", 101)
photo.action = DIARY_CONTROL
photo.control = 29008
photo = DiaryRun(photo)
Expect(photo.photo_file == "photo.png" && photo.photo_index == 0, 102)
photo.control = 29010
photo = DiaryRun(photo)
Expect(photo.succeeded && photo.saved && photo.photo_count == 1 &&
photo.photo_file == "photo-two.bin" && photo.text == "Before after", 103)
photo.action = DIARY_ATTACH
photo.attachment_file = "../outside.bin"
photo.attachment_name = "Invalid"
photo = DiaryRun(photo)
Expect(!photo.succeeded && photo.photo_count == 1, 104)
photo = DiaryRun(DiaryMessage.{.action = DIARY_LOAD, .year = 2024, .month = 3, .day = 1})
Expect(photo.photo_count == 1 && photo.photo_name == "A second photo 🌿", 105)
Expect(DiaryStoreSave("2024-03-02.json", "damaged"), 92)
bad := DiaryRun(DiaryMessage.{.action = DIARY_LOAD, .year = 2024, .month = 3, .day = 2})
bad.action = DIARY_SAVE
Expect(bad.load_error && !DiaryRun(bad).succeeded, 93)
Expect(DiaryStoreRead("2024-03-02.json").data == "damaged", 94)
today := Dates.ParseDate(DiaryStoreClock()[:10])
tomorrow := Dates.Next(today, 1)
future := DiaryRun(DiaryMessage.{.action = DIARY_LOAD,
.year = tomorrow.year, .month = tomorrow.month, .day = tomorrow.day})
Expect(!future.succeeded, 410)
current := DiaryRun(DiaryMessage.{.action = DIARY_LOAD})
current.action = DIARY_EDIT
current.replacement = "Today's unsaved entry"
current = DiaryRun(current)
current.action = DIARY_CONTROL
current.control = 29002
current = DiaryRun(current)
Expect(!current.succeeded && current.day == today.day &&
current.month == today.month && current.year == today.year &&
current.text == "Today's unsaved entry" && !current.saved, 411)
current.control = 29003
current = DiaryRun(current)
current.control = 29012
current = DiaryRun(current)
Expect(!current.succeeded && current.calendar &&
current.month_year == today.year && current.month_number == today.month, 412)
if tomorrow.month == today.month {
current.control = 29100 + tomorrow.day
current = DiaryRun(current)
Expect(!current.succeeded && current.calendar && current.day == today.day, 413)
}
Expect(Dates.OnOrBefore(Dates.Date.{2024, 2, 29}, Dates.Date.{2024, 3, 1}) &&
!Dates.OnOrBefore(Dates.Date.{2024, 3, 1}, Dates.Date.{2024, 2, 29}) &&
!Dates.OnOrBefore(Dates.Date.{2023, 2, 29}, Dates.Date.{2024, 3, 1}) &&
Dates.OnOrBefore(Dates.Date.{2025, 12, 31}, Dates.Date.{2026, 1, 1}), 414)
current.action = DIARY_SAVE
current = DiaryRun(current)
Expect(current.saved && current.succeeded, 415)
original_owner := PackageText(AppTextFromCString(storage_user_id_buffer(), 64), "")
legacy := DiaryLegacyDirectory()
other: string = "different-diary-account"
AppCopyText(other, g_storage.user_id[:])
Expect(DiaryStoreRead("2024-02-28.json").missing, 416)
Expect(!DiaryStoreImportOwned(legacy), 417)
AppCopyText(original_owner, g_storage.user_id[:])
Expect(DiaryStoreRead("2024-02-28.json").data.count > 0, 418)
PackageFree(original_owner)
PackageFree(legacy)
}
CheckPackagedLocales :: () {
Expect(LocaleLanguageCount() == 12, 200)
for index: cast(s32)0..LocaleLanguageCount() - 1 {
Expect(PackagedLocaleMatches(LocaleLanguageAt(index).code), 201 + index)
}
// Leave a populated Japanese cache pointing into the old root, then
// replace it with freshly allocated package assets without switching locale.
Expect(LocaleUse("ja"), 213)
previous := LocaleText("close_button")
Expect(previous == "閉じる", 214)
Expect(CellsOpenPackage(ModuleTestText("inbe")), 215)
Expect(LocaleActiveCode() == "ja" && LocaleText("close_button") == "閉じる", 216)
Expect(PackagedLocaleMatches("ja"), 217)
Expect(LocaleUse("en"), 218)
}
#program_export
CheckModules :: () -> s32 {
lists := ModuleTestText("lists")
habits := ModuleTestText("habits")
practice := ModuleTestText("practice")
Expect(!CellsOpenBytes("", habits, practice), 1)
Expect(!CellsOpenBytes(lists, "ZIB", practice), 2)
Expect(!CellsOpenBytes(habits, lists, practice), 3)
// Bind the actual shared persistence effects without opening user data.
unused CellsStart(*app)
Expect(CellsOpenBytes(lists, habits, practice), 4)
Expect(CellsPackageSize(1) == lists.count && CellsPackageSize(2) == habits.count &&
CellsPackageSize(3) == practice.count && CellsPackageSize(4) == 0, 229)
Expect(CellsRoute(ScreenEList) && CellsRoute(ScreenHabits) &&
CellsRoute(ScreenStart) && CellsRoute(ScreenMeditation) &&
!CellsRoute(ScreenSettings), 5)
CheckRootConfiguration()
CheckPackagedLocales()
Expect(ModuleTestSeedOld(), 6)
data_init()
Expect(storage_db_handle() != null, 7)
AppCopyText(AppTextFromCString(storage_user_id_buffer(), 128), user_id[:])
CheckDiary()
habits_init_with_defaults(*app.habits, 0)
Expect(ModuleTestCount("SELECT COUNT(*) FROM sessions WHERE id='old-session'") == 1, 8)
Expect(ModuleTestCount("SELECT COUNT(*) FROM settings WHERE key='language' AND value='es'") == 1, 9)
// The existing upgrade rewrites non-UUID IDs together with their day rows.
Expect(ModuleTestCount("SELECT COUNT(*) FROM habits h JOIN habit_days d ON d.habit_id=h.id WHERE h.name='Old habit' AND h.user_id='module-user' AND length(h.id)=36 AND h.id<>'old-habit' AND d.local_date=20260925 AND d.completed=1 AND d.count=1") == 1, 10)
old_index := FindHabit("Old habit")
Expect(old_index >= 0, 44)
if old_index >= 0 {
AppCopyText(AppTextFromCString(*app.habits.items[old_index].id[0], HABIT_ID_SIZE), migrated_habit_id[:])
}
edit := ListsMessage.{.action = LISTS_COMMIT, .editing_item = -2, .title = "Trip"}
edit = ListsRun(edit)
Expect(edit.succeeded && edit.reload && edit.sync, 11)
AppCopyText(edit.selected_id, list_id[:])
unused storage_elist_load(cast(*void)*app.elist)
Expect(app.elist.list_count == 1, 12)
edit = ListsMessage.{.action = LISTS_COMMIT, .editing_item = -1,
.selected_list = 0, .list_count = 1, .selected_id = AppTextFromCString(*list_id[0], 37),
.title = "Passport"}
Expect(ListsRun(edit).succeeded, 13)
unused storage_elist_load(cast(*void)*app.elist)
AppCopyText(AppTextFromCString(*app.elist.items[0].id[0], 37), item_id[:])
edit.action = LISTS_SET_DONE
edit.id = AppTextFromCString(*item_id[0], 37)
edit.parent_id = AppTextFromCString(*list_id[0], 37)
edit.index = 0
edit.item_count = 1
edit.done = 1
edit.comment = "Keep relationship"
edit.now = 2.0
edit = ListsRun(edit)
Expect(edit.succeeded && edit.completing_until == 2.45, 14)
edit.action = LISTS_VISIBLE
edit = ListsRun(edit)
Expect(edit.visible, 15)
edit.now = 3.0
Expect(!ListsRun(edit).visible, 16)
unused storage_elist_load(cast(*void)*app.elist)
AppCopyText(AppTextFromCString(*item_id[0], 37), app.elist.completing_item[:])
app.elist.completing_until = 2.45
Expect(ListsRefreshVisibility(*app.elist, 2.1) && app.elist.item_visibility[0] && app.elist.visibility_deadline == 2.45, 51)
runs := ModuleTestRuns()
Expect(ListsRefreshVisibility(*app.elist, 2.2) && ModuleTestRuns() == runs, 52)
Expect(ListsRefreshVisibility(*app.elist, 2.5) && !app.elist.item_visibility[0] && ModuleTestRuns() == runs + 1, 53)
app.elist.show_completed = 1
Expect(ListsRefreshVisibility(*app.elist, 3.0) && app.elist.item_visibility[0] && ModuleTestRuns() == runs + 2, 54)
app.elist.show_completed = 0
unused storage_elist_load(cast(*void)*app.elist)
Expect(!app.elist.visibility_valid && ListsRefreshVisibility(*app.elist, 3.0) && !app.elist.item_visibility[0], 55)
// A returned text view lasts until the next call to that feature. This
// selection follows several visibility calls, so start a fresh message.
edit = ListsMessage.{.action = LISTS_SELECT, .index = 1, .list_count = 2}
edit = ListsRun(edit)
Expect(edit.selected_list == 1 && edit.reset_input && edit.reset_scroll && edit.show_completed == 0, 17)
habit := HabitsMessageFor(*app, HABITS_COMMIT)
habit.active = true
habit.is_new = true
habit.name = " \t "
habit = HabitsRun(habit)
Expect(habit.name_error == 1 && !habit.cancel_edit, 18)
// Reuse the rejected response, including its old validation error.
habit.name = "Reading"
habit.description = "Shared habit"
habit.weekdays = 127
habit.counter_enabled = 1
habit.counter_target = 3
habit.reminder_hour = 9
habit = HabitsRun(habit)
Expect(habit.succeeded && habit.name_error == 0 && habit.cancel_edit, 19)
habit.action = HABITS_COMMIT
habit.count = app.habits.count
habit.name = "Reading"
habit = HabitsRun(habit)
Expect(habit.name_error == 2, 20)
habit.name = "Writing"
habit = HabitsRun(habit)
Expect(habit.succeeded && habit.name_error == 0 && habit.cancel_edit, 21)
reading_index := FindHabit("Reading")
Expect(reading_index >= 0, 45)
if reading_index >= 0 {
AppCopyText(AppTextFromCString(*app.habits.items[reading_index].id[0], HABIT_ID_SIZE), edited_habit_id[:])
habit = HabitsMessageFor(*app, HABITS_COMMIT)
habit.active = true
habit.index = reading_index
habit.name = "Reading"
habit.description = "Edited through Habits bundle"
habit.counter_enabled = 1
habit.counter_target = 7
habit.weekdays = 31
habit = HabitsRun(habit)
Expect(habit.succeeded && habit.cancel_edit, 46)
Expect(ModuleTestCount("SELECT COUNT(*) FROM habits WHERE name='Reading' AND description='Edited through Habits bundle' AND counter_enabled=1 AND counter_target=7 AND weekdays=31") == 1, 47)
}
habit.action = HABITS_DAY_ACTION
habit.index = 0
habit.count = app.habits.count
habit.scheduled = true
habit.counter_enabled = 0
habit.linked_count = 1
habit.question_count = 2
habit = HabitsRun(habit)
Expect(habit.day_action == HABITS_DAY_LINKED && habit.block_click, 22)
habit.linked_count = 0
habit = HabitsRun(habit)
Expect(habit.day_action == HABITS_DAY_QUESTIONS && habit.block_click, 48)
habit.question_count = 0
habit = HabitsRun(habit)
Expect(habit.day_action == HABITS_DAY_TOGGLE, 49)
habit.action = HABITS_HISTORY
habit.selected = 0
habit = HabitsRun(habit)
Expect(habit.screen_mode == HABITS_SCREEN_HISTORY && habit.view_mode == HABIT_VIEW_CALENDAR, 23)
step := PracticeMessage.{.action = PRACTICE_MEDITATION_STEP,
.remaining_seconds = 2, .elapsed_ms = 1500}
step = PracticeRun(step)
Expect(step.succeeded && step.remaining_seconds == 1 && step.remainder_ms == 500 && !step.completed, 24)
step.paused = true
Expect(PracticeRun(step).remaining_seconds == 1, 25)
step.paused = false
step.elapsed_ms = 500
step = PracticeRun(step)
Expect(step.completed && step.remaining_seconds == 0, 26)
step = PracticeMessage.{.action = PRACTICE_BREATH_STEP}
step.breathing = BreathSessionInit()
step.breathing.phase = BreathPhaseNext
step.breathing.round = step.breathing.max_rounds - 1
step.breathing.breath_half_ticks = 1
i: s32 = 0
while i < 120 && !step.completed {
step = PracticeRun(step)
i += 1
}
Expect(step.succeeded && step.completed, 27)
step = PracticeMessage.{.action = PRACTICE_SALUTATION_STEP}
step.salutation = SalutationStart(SalutationInit(step.salutation))
step.elapsed = 1000.0
step = PracticeRun(step)
Expect(step.succeeded && step.completed, 28)
step = PracticeMessage.{.action = PRACTICE_PATTERNS_STEP}
step.patterns.active = 1
step.patterns.duration_minutes = 1
step.patterns.elapsed_seconds = 59
step = PracticeRun(step)
Expect(step.succeeded && step.completed && step.patterns.elapsed_seconds == 60, 29)
rounds: [1]s32 = .[42]
Expect(storage_save_session(*rounds[0], 1, null, 0) != 0, 30)
Expect(ModuleTestCount("SELECT COUNT(*) FROM elist_items i JOIN elist_lists l ON l.id=i.list_id WHERE i.user_id=l.user_id AND i.title='Passport' AND i.comment='Keep relationship' AND i.done=1") == 1, 31)
habits_free(*app.habits)
storage_close()
CellsClose()
Expect(CellsOpenPackage(ModuleTestText("inbe")), 32)
Expect(storage_init("module-data") != 0, 33)
Expect(AppTextFromCString(storage_user_id_buffer(), 128) == AppTextFromCString(*user_id[0], 128), 34)
unused storage_elist_load(cast(*void)*app.elist)
habits_init_with_defaults(*app.habits, 0)
Expect(app.elist.item_count == 1 && app.elist.items[0].done == 1 && app.habits.count >= 3, 35)
Expect(ModuleTestCount("SELECT COUNT(*) FROM sessions") == 2, 36)
Expect(ModuleTestCount("SELECT COUNT(DISTINCT entity_type) FROM sync_outbox WHERE entity_type IN ('elist_list','elist_item','habit','session')") == 4, 37)
Expect(ModuleTestCount("SELECT COUNT(*) FROM habits h JOIN habit_days d ON d.habit_id=h.id WHERE h.name='Old habit' AND h.user_id='module-user' AND length(h.id)=36 AND d.local_date=20260925 AND d.completed=1 AND d.count=1") == 1, 43)
old_index = FindHabit("Old habit")
reading_index = FindHabit("Reading")
Expect(old_index >= 0 && reading_index >= 0, 48)
if old_index >= 0 && reading_index >= 0 {
Expect(AppTextFromCString(*app.habits.items[old_index].id[0], HABIT_ID_SIZE) == AppTextFromCString(*migrated_habit_id[0], HABIT_ID_SIZE), 49)
Expect(AppTextFromCString(*app.habits.items[reading_index].id[0], HABIT_ID_SIZE) == AppTextFromCString(*edited_habit_id[0], HABIT_ID_SIZE) && app.habits.items[reading_index].counter_target == 7, 50)
}
// A valid bundle with a mismatched host schema must fail before saving.
Expect(CellsOpenBytes(ModuleTestText("wrong-record"), habits, practice), 38)
edit = ListsMessage.{.action = LISTS_COMMIT, .editing_item = -2, .title = "Unsafe"}
Expect(!ListsRun(edit).succeeded, 39)
Expect(ModuleTestCount("SELECT COUNT(*) FROM elist_lists") == 1, 40)
Expect(CellsOpenBytes(ModuleTestText("wrong-scalar"), habits, practice), 41)
Expect(!ListsRun(edit).succeeded, 42)
app.elist.visibility_valid = false
Expect(!ListsRefreshVisibility(*app.elist, 3.0), 56)
Expect(CellsOpenPackage(ModuleTestText("inbe")), 100)
lumi_result := CheckLumiConversation()
Expect(lumi_result == 0, lumi_result)
habits_free(*app.habits)
storage_close()
CellsClose()
ModuleTestUseCache(true)
cached_opens := ModuleTestBundleOpens()
Expect(CellsOpen(), 96)
// The root and its six cells, then the five cached cell packages.
Expect(ModuleTestBundleOpens() - cached_opens == 12, 103)
for index: cast(s32)1..cast(s32)5 {
Expect(ModuleTestCachedReads(index) > 0, 97)
}
Expect(CellsInstalledMask() == APP_ALL, 98)
Expect(CellsPackageSize(1) == lists.count && CellsPackageSize(2) == habits.count, 235)
CellsClose()
ModuleTestCorruptCachedLists(true)
Expect(CellsOpen() && CellsInstalledMask() == APP_ALL, 104)
Expect(CellsPackageSize(1) == lists.count, 236)
Expect(ListsRun(ListsMessage.{.action = LISTS_ROUTE, .screen = ScreenEList}).succeeded, 105)
CellsClose()
ModuleTestCorruptCachedLists(false)
ModuleTestUseCache(false)
before_opens := ModuleTestBundleOpens()
Expect(CellsOpenPackage(ModuleTestText("inbe")), 101)
Expect(ModuleTestBundleOpens() - before_opens == 7, 102)
CellsClose()
Expect(CellsPackageSize(0) == 0 && CellsPackageSize(1) == 0, 237)
return failures
}