Files
breeze/tests/habit_form_behavior.zi

209 lines
9.4 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/app/app_types.zi";
#import, file "../src/app/modal_rules.zi";
#import, file "../src/storage/data.zi";
#import, file "../src/storage/db.zi";
#import, file "../src/storage/storage_core.zi";
#import, file "../src/storage/storage_habits.zi";
#import, file "../src/habits/habit_types.zi";
#import, file "../src/habits/habit_memory.zi";
#import, file "../src/habits/habit_model.zi";
#import, file "../src/habits/habit_calendar.zi";
#import, file "../src/habits/habit_schedule.zi";
#import, file "../src/core/types.zi";
#import, file "../src/screens/habits/edit.zi";
#import, file "apps_test_fixture.zi";
#import, file "background_audio_stub.zi";
using HabitSyncMode;
app_state: InnerBreeze;
syncs: s32;
left_route: s32 = -1;
// Application hooks the form calls when it saves.
#program_export "app_auto_sync"
host_auto_sync :: (app: *InnerBreeze) -> s32 {
unused app
syncs += 1
return 0
}
#program_export "save_settings"
host_save_settings :: (app: *InnerBreeze) {
unused app
}
#program_export "app_apply_nav_route"
host_apply_nav_route :: (app: *InnerBreeze, route: s32) {
unused app
left_route = route
}
#program_export "meditation_music_stop"
host_stop_music :: (app: *InnerBreeze) {
unused app
}
set_text :: (target: []u8, text: string) {
unused AppCopyText(text, target)
}
#program_export
Check :: () -> s32 {
guard := support_begin("/tmp/inbe-habit-form-zi-test/")
if guard != 0 { return guard }
root := make_root("habit-form")
check_true("init db", storage_init(root) != 0)
app_state = InnerBreeze.{}
app := *app_state
if !LoadTestApps(app) {
storage_close()
CloseTestApps()
return 101
}
habit_edit_begin_new(app)
check_int("new habit opens the form", app.breathing.screen, ScreenHabitEdit)
check_true("the name starts empty", app.habit_edit.text[0] == cast(u8)0)
check_int("no reminder by default", app.habit_edit.reminder_hour, -1)
check_true("a new habit is planned on every weekday",
app.habit_edit.schedule.repeat == HABIT_REPEAT_WEEKLY &&
app.habit_edit.schedule.weekdays == 127)
check_true("an untouched new habit has no changes", !habit_edit_changed(app))
habit_edit_commit(app)
check_true("an empty name keeps the form open", app.habit_edit.active != 0 && app.habit_edit.name_error == 1)
check_int("nothing was added", app.habits.count, 0)
check_int("nothing was stored", storage_habit_count(), 0)
set_text(app.habit_edit.text[:], " \t ")
habit_edit_commit(app)
check_true("a blank name keeps the form open", app.habit_edit.active != 0 && app.habits.count == 0)
set_text(app.habit_edit.text[:], "Test counter")
set_text(app.habit_edit.description[:], "Only for testing")
app.habit_edit.counter_enabled = 1
app.habit_edit.counter_target = 250
app.habit_edit.sync_activity = 3
app.habit_edit.schedule.weekdays = 31
app.habit_edit.reminder_hour = 9
habit_edit_commit(app)
check_true("a valid habit is added and the form closes", app.habits.count == 1 && app.habit_edit.active == 0)
check_int("the habit is stored", storage_habit_count(), 1)
check_int("sync ran once", syncs, 1)
check_int("counter enabled", app.habits.items[0].counter_enabled, 1)
check_int("a daily target above 99", app.habits.items[0].counter_target, 250)
check_int("linked activities", app.habits.items[0].sync_activity, 3)
check_int("linking is independent of the counter", app.habits.items[0].sync_mode, 1)
check_int("weekdays", app.habits.items[0].weekdays, 31)
check_int("the habit has its schedule", app.habits.items[0].schedule_count, 1)
check_int("planned on a Friday", habit_scheduled_day(*app.habits.items[0], 20261016), 1)
check_int("not planned on a Saturday", habit_scheduled_day(*app.habits.items[0], 20261017), 0)
check_int("reminder", app.habits.items[0].reminder_hour, 9)
check_true("description", TextFromCString(*app.habits.items[0].description[0]) == "Only for testing")
habit_edit_begin_new(app)
set_text(app.habit_edit.text[:], "Test counter")
habit_edit_commit(app)
check_true("a duplicate name is refused", app.habit_edit.active != 0 && app.habit_edit.name_error == 2 && app.habits.count == 1)
habit_edit_cancel(app)
check_int("cancelling stores nothing", storage_habit_count(), 1)
check_int("cancelling returns to the overview", app.habits.screen_mode, HABITS_SCREEN_OVERVIEW)
habit_edit_begin(app, 0)
check_true("editing opens the form for the habit", app.breathing.screen == ScreenHabitEdit && app.habit_edit.is_new == 0)
check_true("an unchanged habit needs no prompt", !habit_edit_changed(app))
app.habit_edit.focused = 1
app.habit_edit.cursor = 2
app.habit_edit.more_colors = 1
check_true("moving the cursor and opening controls do not change the habit", !habit_edit_changed(app))
check_true("the form shows the habit's settings", app.habit_edit.counter_enabled != 0 &&
app.habit_edit.counter_target == 250 && app.habit_edit.sync_activity == 3 &&
app.habit_edit.schedule.repeat == HABIT_REPEAT_WEEKLY &&
app.habit_edit.schedule.weekdays == 31)
set_text(app.habit_edit.text[:], "Unsaved edit")
check_true("typing changes the habit", habit_edit_changed(app))
set_text(app.habit_edit.text[:], "Test counter")
check_true("reverting the edit clears its changes", !habit_edit_changed(app))
set_text(app.habit_edit.text[:], "Unsaved edit")
habit_edit_back(app)
check_true("Back asks about unsaved changes", app.modal.active != 0 &&
app.modal.type == ModalType.ModalHabitUnsaved && app.habit_edit.active != 0 &&
app.breathing.screen == ScreenHabitEdit)
habit_edit_resolve_leave(app, 1)
check_true("keep editing preserves the draft", app.modal.active == 0 &&
TextFromCString(*app.habit_edit.text[0]) == "Unsaved edit" && app.habit_edit.active != 0)
habit_edit_back(app)
habit_edit_resolve_leave(app, 3)
check_true("cancelling an edit keeps the habit", TextFromCString(*app.habits.items[0].name[0]) == "Test counter")
check_true("discard returns to the overview", app.breathing.screen == ScreenHabits && app.habit_edit.active == 0)
habit_edit_begin(app, 0)
app.habit_edit.counter_enabled = 0
// The editor page chooses the repeat; the host keeps the schedule it sends.
app.habit_edit.schedule.repeat = HABIT_REPEAT_DAILY
HabitScheduleNormalize(*app.habit_edit.schedule, habits_today_index())
app.habit_edit.reminder_hour = -1
habit_edit_commit(app)
check_true("turning the counter off leaves the linking alone",
app.habits.items[0].counter_enabled == 0 && app.habits.items[0].sync_activity == 3)
check_true("the schedule and reminder are cleared",
app.habits.items[0].weekdays == 0 && app.habits.items[0].reminder_hour == -1)
check_true("the linked habit keeps its earlier schedule",
app.habits.items[0].schedule_count == 2 &&
app.habits.items[0].schedules[1].repeat == HABIT_REPEAT_DAILY)
// A formula that does not read keeps the form open and the habit as it was.
habit_edit_begin(app, 0)
app.habit_edit.schedule.repeat = HABIT_REPEAT_FORMULA
unused HabitScheduleSetFormula(*app.habit_edit.schedule, "days %")
habit_edit_commit(app)
check_true("an unreadable formula keeps the form open",
app.habit_edit.active != 0 && app.habits.items[0].schedule_count == 2)
habit_edit_cancel(app)
check_int("sync ran for the edit", syncs, 2)
habit_edit_begin(app, 0)
set_text(app.habit_edit.text[:], "")
habit_edit_back(app)
habit_edit_resolve_leave(app, 2)
check_true("Save validates before leaving", app.habit_edit.active != 0 &&
app.habit_edit.name_error == 1 && app.breathing.screen == ScreenHabitEdit)
set_text(app.habit_edit.text[:], "Saved changes")
habit_edit_back(app)
habit_edit_resolve_leave(app, 2)
check_true("Save keeps changes and returns to the overview", app.habit_edit.active == 0 &&
app.breathing.screen == ScreenHabits &&
TextFromCString(*app.habits.items[0].name[0]) == "Saved changes")
check_int("saving from the prompt syncs once", syncs, 3)
habit_edit_begin(app, 0)
set_text(app.habit_edit.description[:], "A navigation draft")
check_true("leaving for another app asks first", habit_edit_request_leave(app, 4))
habit_edit_resolve_leave(app, 1)
check_true("Keep editing cancels the destination", left_route == -1 && app.habit_edit.active != 0)
check_true("another navigation attempt asks again", habit_edit_request_leave(app, 4))
habit_edit_resolve_leave(app, 3)
check_true("Discard continues to the requested destination", left_route == 4 && app.habit_edit.active == 0)
habit_edit_begin(app, 0)
app.habit_edit.counter_enabled = 1
app.habit_edit.counter_target = 0
app.habit_edit.custom_coins = true
app.habit_edit.coin_halves = -4
habit_edit_commit(app)
check_int("saving a zero target", app.habits.items[0].counter_target, 0)
habit_edit_begin(app, 0)
check_int("reopening a zero target", app.habit_edit.counter_target, 0)
check_true("reopening custom negative coins", app.habit_edit.custom_coins &&
app.habit_edit.coin_halves == -4)
check_true("a zero target has no unsaved changes", !habit_edit_changed(app))
habit_edit_cancel(app)
habits_free(*app.habits)
CloseTestApps()
storage_close()
return failures
}