package main import "./provider" import "./session" import "./ui/input" import "./ui/picker" import "./ui/theme" import "core:fmt" import "core:os" import "core:strings" import "core:testing" Request_Transition_Case :: struct { sequence: string, stream_active: bool, compacting: bool, want_state: Request_State, want_effect: Effect_Kind, want_mode: input.Picker_Mode, } app_has_file :: proc(state: ^input.State, name: string) -> bool { for file in state.files { if file.name == name { return true } } return false } @(test) application_request_transitions_are_exclusive :: proc(t: ^testing.T) { cases := []Request_Transition_Case { {sequence = "\x18", want_state = .Prefix}, {sequence = "\x18e", want_state = .Idle, want_effect = .Open_Editor}, {sequence = "\x18m", want_state = .Idle, want_effect = .Fetch_Models, want_mode = .Models}, {sequence = "\x18t", want_state = .Idle, want_effect = .Fetch_Variants, want_mode = .Variants}, {sequence = "\x18x"}, {sequence = "/new\n", want_state = .Idle, want_effect = .Start_New_Session}, {sequence = "/compact\n", want_state = .Idle, want_effect = .Prepare_Compaction}, {sequence = "/models\n", want_state = .Idle, want_effect = .Fetch_Models, want_mode = .Models}, {sequence = "/session\n", want_state = .Idle, want_effect = .Fetch_Sessions, want_mode = .Sessions}, {sequence = "/variants\n", want_state = .Idle, want_effect = .Fetch_Variants, want_mode = .Variants}, {sequence = "\x18e", stream_active = true}, {sequence = "\x18m", stream_active = true}, {sequence = "\x18m", stream_active = true, compacting = true}, } for test_case in cases { model := init() if test_case.stream_active do model.stream.active = true model.compacting = test_case.compacting effects: [dynamic]Effect for key in transmute([]byte)test_case.sequence { produced := update(&model, Message{kind = .Key_Pressed, key = key}) append(&effects, ..produced[:]) delete(produced) } testing.expect_value(t, model.request_state, test_case.want_state) testing.expect_value(t, model.compacting, test_case.compacting) testing.expect_value(t, len(effects) == 1, test_case.want_effect != .None) if test_case.want_effect != .None { testing.expect_value(t, effects[0].kind, test_case.want_effect) if test_case.want_effect == .Fetch_Models || test_case.want_effect == .Fetch_Sessions || test_case.want_effect == .Fetch_Variants { testing.expect(t, model.catalog_loading) testing.expect_value(t, model.input_state.picker_mode, test_case.want_mode) } } for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) input.destroy(&model.input_state) } } @(test) application_request_states_are_consumed_once :: proc(t: ^testing.T) { states := []Request_State { .Prefix, .Editor, .New_Session, .Compact, .Models, .Sessions, .Variants, } for state in states { model := init() model.request_state = state testing.expect(t, take_request_state(&model, state)) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect(t, !take_request_state(&model, state)) input.destroy(&model.input_state) } model := init() testing.expect(t, !take_request_state(&model, .Idle)) testing.expect_value(t, model.request_state, Request_State.Idle) input.destroy(&model.input_state) } @(test) prefix_commands_edit_quit_and_consume_unknown_keys :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } update(&model, Message{kind = .Key_Pressed, key = 24}) testing.expect_value(t, model.request_state, Request_State.Prefix) effects := update(&model, Message{kind = .Key_Pressed, key = 'e'}) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Open_Editor) delete(effects) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, model.input_state.len, 0) model.request_state = .Idle update(&model, Message{kind = .Key_Pressed, key = 24}) update(&model, Message{kind = .Key_Pressed, key = 'x'}) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, model.input_state.len, 0) update(&model, Message{kind = .Key_Pressed, key = 24}) update(&model, Message{kind = .Key_Pressed, key = 'q'}) testing.expect(t, !model.running) } @(test) prefix_t_opens_variant_picker_request :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } update(&model, Message{kind = .Key_Pressed, key = 24}) testing.expect_value(t, model.request_state, Request_State.Prefix) effects := update(&model, Message{kind = .Key_Pressed, key = 't'}) defer delete(effects) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Fetch_Variants) testing.expect(t, model.catalog_loading) testing.expect_value(t, model.input_state.len, 0) } @(test) prefix_model_picker_cancel_restores_prompt_and_cursor :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } _ = input.replace(&model.input_state, transmute([]byte)string("draft")) model.input_state.position = 2 update(&model, Message{kind = .Key_Pressed, key = 24}) effects := update(&model, Message{kind = .Key_Pressed, key = 'm'}) delete(effects) choices: [dynamic]Model_Choice model_open_model_picker(&model, choices, {}) for character in transmute([]byte)string("filter") { update(&model, Message{kind = .Key_Pressed, key = character}) } update(&model, Message{kind = .Key_Pressed, key = 27}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "draft") testing.expect_value(t, model.input_state.position, 2) testing.expect(t, !model.input_state.saved_prompt_valid) } @(test) prefix_variant_picker_cancel_restores_empty_prompt :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } update(&model, Message{kind = .Key_Pressed, key = 24}) effects := update(&model, Message{kind = .Key_Pressed, key = 't'}) delete(effects) choices: [dynamic]Variant_Choice model_open_variant_picker(&model, choices, "") for character in transmute([]byte)string("filter") { update(&model, Message{kind = .Key_Pressed, key = character}) } update(&model, Message{kind = .Key_Pressed, key = 27}) testing.expect_value(t, model.input_state.len, 0) testing.expect_value(t, model.input_state.position, 0) testing.expect(t, !model.input_state.saved_prompt_valid) } @(test) plain_prefix_command_letters_still_insert :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } update(&model, Message{kind = .Key_Pressed, key = 'e'}) update(&model, Message{kind = .Key_Pressed, key = 'q'}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "eq") } @(test) empty_and_whitespace_prompts_are_not_submitted :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect(t, !model.stream.active) testing.expect(t, !model.input_state.loading) testing.expect_value(t, len(model.session.messages), 0) testing.expect(t, input.replace(&model.input_state, transmute([]byte)string(" \n "))) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect(t, !model.stream.active) testing.expect(t, !model.input_state.loading) testing.expect_value(t, model.input_state.len, 0) testing.expect_value(t, len(model.session.messages), 0) } @(test) submitted_prompts_are_trimmed :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } defer stream_destroy(&model.stream) defer agent_destroy(&model.agent) testing.expect( t, session.init( &model.session, {provider = provider.Provider_Id("openrouter"), id = "test-model"}, ".", 0, ), ) defer session.destroy(&model.session) testing.expect( t, input.replace(&model.input_state, transmute([]byte)string(" hello\nworld ")), ) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect(t, model.stream.active) testing.expect(t, model.input_state.loading) testing.expect_value(t, string(model.stream.prompt[:model.stream.prompt_len]), "hello\nworld") testing.expect_value(t, len(model.session.messages), 1) testing.expect_value(t, model.session.messages[0].content, "hello\nworld") testing.expect_value(t, model.input_state.len, 0) } @(test) ctrl_c_cancels_active_response_and_exits_while_idle :: proc(t: ^testing.T) { model := init() defer { delete(model.queued_prompt); stream_destroy(&model.stream) } model.stream.active = true model.stream.cancel_pending = true model.input_state.loading = true model.queued_prompt = strings.clone("queued") update(&model, Message{kind = .Key_Pressed, key = 3}) testing.expect(t, model.running) testing.expect(t, !model.stream.active) testing.expect(t, !model.stream.cancel_pending) testing.expect(t, !model.input_state.loading) testing.expect_value(t, model.queued_prompt, "") update(&model, Message{kind = .Key_Pressed, key = 3}) testing.expect(t, !model.running) } @(test) escape_interrupt_clears_queued_prompt :: proc(t: ^testing.T) { model := init() defer { delete(model.queued_prompt); stream_destroy(&model.stream); input.destroy(&model.input_state) } model.stream.active = true model.input_state.loading = true model.queued_prompt = strings.clone("queued") update(&model, Message{kind = .Key_Pressed, key = 27}) testing.expect(t, model.stream.active) testing.expect(t, model.stream.cancel_pending) testing.expect_value(t, model.queued_prompt, "queued") update(&model, Message{kind = .Key_Pressed, key = 27}) testing.expect(t, !model.stream.active) testing.expect(t, !model.stream.cancel_pending) testing.expect(t, !model.input_state.loading) testing.expect_value(t, model.queued_prompt, "") } @(test) ctrl_c_clears_prompt_before_exiting :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } testing.expect(t, input.replace(&model.input_state, transmute([]byte)string("/models"))) update(&model, Message{kind = .Key_Pressed, key = 3}) testing.expect(t, model.running) testing.expect_value(t, model.input_state.len, 0) testing.expect(t, !model.input_state.command_autocomplete) update(&model, Message{kind = .Key_Pressed, key = 3}) testing.expect(t, !model.running) } @(test) ctrl_d_quits_only_when_prompt_is_empty :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } testing.expect(t, input.replace(&model.input_state, transmute([]byte)string("draft"))) update(&model, Message{kind = .Key_Pressed, key = 4}) testing.expect(t, model.running) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "draft") testing.expect(t, input.replace(&model.input_state, transmute([]byte)string(""))) update(&model, Message{kind = .Key_Pressed, key = 4}) testing.expect(t, !model.running) } @(test) ctrl_c_closes_picker_and_clears_filter :: proc(t: ^testing.T) { model := init() choices := make([dynamic]Model_Choice, 0, 1) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("model"), }, }, ) model_open_model_picker(&model, choices, {}) testing.expect(t, input.replace(&model.input_state, transmute([]byte)string("filter"))) update(&model, Message{kind = .Key_Pressed, key = 3}) testing.expect(t, model.running) testing.expect(t, !model.input_state.picker.active) testing.expect_value(t, model.input_state.len, 0) model_close_picker(&model) input.destroy(&model.input_state) } @(test) prompt_editing_keys_move_and_delete_around_cursor :: proc(t: ^testing.T) { state: input.State defer input.destroy(&state) testing.expect(t, input.replace(&state, transmute([]byte)string("first\nsecond\nthird"))) state.position = 8 input.move_home(&state) testing.expect_value(t, state.position, 6) input.move_end(&state) testing.expect_value(t, state.position, 12) input.move_left(&state) input.move_left(&state) testing.expect_value(t, state.position, 10) input.move_right(&state) testing.expect_value(t, state.position, 11) input.delete_after_cursor(&state) testing.expect_value(t, string(state.bytes[:state.len]), "first\nsecon\nthird") testing.expect_value(t, state.position, 11) state.position = 0 input.move_left(&state) testing.expect_value(t, state.position, 0) state.position = state.len input.move_right(&state) input.delete_after_cursor(&state) testing.expect_value(t, state.position, state.len) } @(test) up_and_down_move_between_rendered_prompt_rows :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state); delete(model.session.messages) } append(&model.session.messages, session.Message{role = "user", content = "previous"}) model.input_state.width = 6 testing.expect(t, input.replace(&model.input_state, transmute([]byte)string("abcdef\nxy"))) model.input_state.position = 5 update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, model.input_state.position, 1) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.position, 5) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.position, 8) } @(test) up_at_start_recalls_user_messages_and_keeps_cursor_at_start :: proc(t: ^testing.T) { model := init() defer { delete(model.session.messages); input.destroy(&model.input_state) } append(&model.session.messages, session.Message{role = "user", content = "first\nmessage"}, session.Message{role = "assistant", content = "reply"}, session.Message{role = "user", content = "summary", context_summary = true}, session.Message{role = "user", content = "/models"}, session.Message{role = "tool", content = "result"}, ) _ = input.replace(&model.input_state, transmute([]byte)string("existing draft")) model.input_state.position = 0 update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "/models") testing.expect_value(t, model.input_state.position, 0) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "first\nmessage") testing.expect_value(t, model.input_state.position, 0) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "first\nmessage") testing.expect_value(t, model.input_state.position, 0) model.input_state.width = 20 sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw(&sb, &model.input_state, 0, 0, false, false, "") testing.expect_value(t, model.input_state.cursor_row, 0) // Editing starts a fresh search, rather than continuing from the oldest entry. update(&model, Message{kind = .Key_Pressed, key = 'x'}) update(&model, Message{kind = .Key_Pressed, key = 1}) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "/models") testing.expect_value(t, model.input_state.position, 0) } @(test) up_at_start_with_no_user_history_preserves_draft :: proc(t: ^testing.T) { model := init() defer { delete(model.session.messages); input.destroy(&model.input_state) } update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, model.input_state.len, 0) append(&model.session.messages, session.Message{role = "assistant", content = "reply"}, session.Message{role = "user", content = "summary", context_summary = true}, ) _ = input.replace(&model.input_state, transmute([]byte)string("draft")) model.input_state.position = 0 update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "draft") testing.expect_value(t, model.input_state.position, 0) } @(test) down_at_end_recalls_newer_messages_then_clears_input :: proc(t: ^testing.T) { model := init() defer { delete(model.session.messages); input.destroy(&model.input_state) } append(&model.session.messages, session.Message{role = "user", content = "first\nmessage"}, session.Message{role = "assistant", content = "reply"}, session.Message{role = "user", content = "summary", context_summary = true}, session.Message{role = "user", content = ""}, session.Message{role = "tool", content = "result"}, session.Message{role = "user", content = "/models"}, ) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "first\nmessage") // Down away from the end still moves within the multiline input. model.input_state.width = 40 update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "first\nmessage") testing.expect(t, model.input_state.position > 0) update(&model, Message{kind = .Key_Pressed, key = 5}) // Ctrl+E update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "/models") testing.expect_value(t, model.input_state.position, model.input_state.len) // Cursor navigation also preserves history when switching back to Up. update(&model, Message{kind = .Key_Pressed, special_key = .Home}) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "first\nmessage") update(&model, Message{kind = .Key_Pressed, key = 5}) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.len, 0) testing.expect_value(t, model.input_state.position, 0) testing.expect_value(t, model.input_history_before, 0) testing.expect(t, !model.input_state.command_autocomplete) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.len, 0) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "/models") } @(test) down_does_not_clear_an_unrelated_or_edited_draft :: proc(t: ^testing.T) { model := init() defer { delete(model.session.messages); input.destroy(&model.input_state) } append(&model.session.messages, session.Message{role = "user", content = "previous"}) _ = input.replace(&model.input_state, transmute([]byte)string("draft")) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "draft") update(&model, Message{kind = .Key_Pressed, key = 1}) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) update(&model, Message{kind = .Key_Pressed, key = 5}) update(&model, Message{kind = .Key_Pressed, key = '!'}) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "previous!") } @(test) up_and_down_select_autocomplete_before_moving_cursor :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } testing.expect(t, input.replace(&model.input_state, transmute([]byte)string("/"))) testing.expect_value(t, input.autocomplete_rows(&model.input_state), 6) position := model.input_state.position update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.selected, 1) testing.expect_value(t, model.input_state.position, position) update(&model, Message{kind = .Key_Pressed, special_key = .Up}) testing.expect_value(t, model.input_state.selected, 0) testing.expect_value(t, model.input_state.position, position) } @(test) new_command_requests_a_fresh_session :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } for character in transmute([]byte)string("/new") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect(t, input.is_new_command(&model.input_state)) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) defer { for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) } testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Start_New_Session) testing.expect(t, !model.stream.active) testing.expect_value(t, model.input_state.len, 0) } @(test) compact_command_requests_a_summary :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } for character in transmute([]byte)string("/compact") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect(t, input.is_compact_command(&model.input_state)) testing.expect(t, !model.input_state.command_autocomplete) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) defer { for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) } testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Prepare_Compaction) testing.expect(t, !model.stream.active) testing.expect_value(t, model.input_state.len, 0) } @(test) models_command_opens_picker_request :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } for character in transmute([]byte)string("/models") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect(t, input.is_models_command(&model.input_state)) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) defer delete(effects) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Fetch_Models) testing.expect(t, model.catalog_loading) testing.expect(t, !model.stream.active) testing.expect_value(t, model.input_state.len, 0) } @(test) session_command_opens_picker_request :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } for character in transmute([]byte)string("/session") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect(t, input.is_session_command(&model.input_state)) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) defer delete(effects) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Fetch_Sessions) testing.expect(t, !model.stream.active) testing.expect_value(t, model.input_state.len, 0) } @(test) variants_command_opens_picker_request :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } for character in transmute([]byte)string("/variants") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect(t, input.is_variants_command(&model.input_state)) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) defer delete(effects) testing.expect_value(t, model.request_state, Request_State.Idle) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Fetch_Variants) testing.expect_value(t, model.input_state.len, 0) } @(test) variant_picker_selects_default_and_named_variants :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } choices := make([dynamic]Variant_Choice, 0, 3) append(&choices, Variant_Choice{id = strings.clone(""), label = strings.clone("default")}) append(&choices, Variant_Choice{id = strings.clone("low"), label = strings.clone("low")}) append(&choices, Variant_Choice{id = strings.clone("high"), label = strings.clone("high")}) model_open_variant_picker(&model, choices, "low") testing.expect_value(t, model.input_state.picker.selected, 1) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) if testing.expect(t, len(effects) == 1) { testing.expect_value(t, effects[0].kind, Effect_Kind.Select_Variant) testing.expect_value(t, effects[0].variant, "high") } for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) testing.expect(t, !model.input_state.picker.active) } @(test) variant_picker_renders_reserved_row_when_filter_has_no_matches :: proc(t: ^testing.T) { state := input.State { width = 40, } choices := make([dynamic]Variant_Choice, 0, 2) append(&choices, Variant_Choice{id = strings.clone(""), label = strings.clone("Default")}) append(&choices, Variant_Choice{id = strings.clone("low"), label = strings.clone("low")}) app_open_variant_picker(&state, choices[:], "") testing.expect(t, input.replace(&state, transmute([]byte)string("foobar"))) testing.expect_value(t, input.picker_rows(&state), 0) testing.expect_value(t, input.below_rows(&state, false), 1) sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw(&sb, &state, 0, 0, false, false, "") testing.expect(t, strings.contains(strings.to_string(sb), "No matching variants")) input.destroy(&state) variant_choices_destroy(&choices) } @(test) session_picker_navigates_filters_and_emits_restore_effect :: proc(t: ^testing.T) { model := init() choices := make([dynamic]Session_Choice, 0, 2) append( &choices, Session_Choice{id = strings.clone("session-1"), label = strings.clone("session-1 first")}, ) append( &choices, Session_Choice{id = strings.clone("session-2"), label = strings.clone("session-2 second")}, ) model_open_session_picker(&model, choices, "session-1") update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.picker.selected, 1) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect_value(t, len(effects), 1) testing.expect_value(t, effects[0].kind, Effect_Kind.Restore_Session) testing.expect_value(t, effects[0].session_id, "session-2") for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) testing.expect(t, !model.input_state.picker.active) testing.expect_value(t, model.picker_event.kind, picker.Event_Kind.None) model_close_picker(&model) input.destroy(&model.input_state) } @(test) model_picker_navigates_selects_and_cancels :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } choices := make([dynamic]Model_Choice, 0, 2) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("first"), }, }, ) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("second"), }, }, ) model_open_model_picker( &model, choices, {provider = provider.Provider_Id("test"), id = "first"}, ) update(&model, Message{kind = .Key_Pressed, special_key = .Down}) testing.expect_value(t, model.input_state.picker.selected, 1) update(&model, Message{kind = .Key_Pressed, key = 16}) testing.expect_value(t, model.input_state.picker.selected, 0) update(&model, Message{kind = .Key_Pressed, key = 14}) testing.expect_value(t, model.input_state.picker.selected, 1) effects := update(&model, Message{kind = .Key_Pressed, key = '\n'}) if testing.expect(t, len(effects) == 1) { testing.expect_value(t, effects[0].kind, Effect_Kind.Select_Model) testing.expect_value(t, effects[0].model_ref.id, "second") } for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) testing.expect(t, !model.input_state.picker.active) choices = make([dynamic]Model_Choice, 0, 1) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("cancelled"), }, }, ) model_open_model_picker(&model, choices, {}) update(&model, Message{kind = .Key_Pressed, key = 27}) testing.expect(t, !model.input_state.picker.active) } @(test) model_picker_selects_current_provider_and_filters_labels :: proc(t: ^testing.T) { model := init() choices := make([dynamic]Model_Choice, 0, 2) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("openrouter")), id = strings.clone("gpt-5.5"), }, label = strings.clone("openrouter/gpt-5.5"), }, ) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("codex")), id = strings.clone("gpt-5.5"), }, label = strings.clone("codex/gpt-5.5"), }, ) model_open_model_picker( &model, choices, {provider = provider.Provider_Id("codex"), id = "gpt-5.5"}, ) testing.expect_value(t, model.input_state.picker.selected, 1) for character in transmute([]byte)string("codex") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect_value(t, input.picker_rows(&model.input_state), 1) choice, found := app_model_choice(&model, 0) testing.expect(t, found) testing.expect_value(t, string(choice.ref.provider), "codex") model_close_picker(&model) input.destroy(&model.input_state) } @(test) model_picker_filters_with_prompt_text :: proc(t: ^testing.T) { model := init() choices := make([dynamic]Model_Choice, 0, 3) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("anthropic/claude"), }, }, ) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("openai/gpt-5"), }, }, ) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("openai/gpt-4"), }, }, ) model_open_model_picker(&model, choices, {}) for character in transmute([]byte)string("GPT-5") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect_value(t, model.input_state.len, 5) testing.expect_value(t, input.picker_rows(&model.input_state), 1) choice, found := app_model_choice(&model, 0) testing.expect(t, found) testing.expect_value(t, choice.ref.id, "openai/gpt-5") update(&model, Message{kind = .Key_Pressed, key = 127}) testing.expect_value(t, input.picker_rows(&model.input_state), 2) update(&model, Message{kind = .Key_Pressed, key = 27}) testing.expect_value(t, model.input_state.len, 0) model_close_picker(&model) input.destroy(&model.input_state) } @(test) model_picker_filter_matches_non_contiguous_characters :: proc(t: ^testing.T) { model := init() choices := make([dynamic]Model_Choice, 0, 2) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("openai/gpt-5.6-luna"), }, }, ) append( &choices, Model_Choice { ref = { provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("anthropic/claude"), }, }, ) model_open_model_picker(&model, choices, {}) for character in transmute([]byte)string("opelua") { update(&model, Message{kind = .Key_Pressed, key = character}) } testing.expect_value(t, input.picker_rows(&model.input_state), 1) choice, found := app_model_choice(&model, 0) testing.expect(t, found) testing.expect_value(t, choice.ref.id, "openai/gpt-5.6-luna") model_close_picker(&model) input.destroy(&model.input_state) } @(test) model_picker_borrows_choices_until_close_and_reopen :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } choices := make([dynamic]Model_Choice) append(&choices, Model_Choice { ref = {provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("first")}, label = strings.clone("first label"), }) model_open_model_picker(&model, choices, {}) testing.expect_value(t, len(model.model_choices), 1) testing.expect_value(t, model.input_state.picker.items[0].label, "first label") testing.expect(t, model.input_state.picker.borrows_strings) choice_label := transmute([]byte)model.model_choices[0].label item_label := transmute([]byte)model.input_state.picker.items[0].label choice_id := transmute([]byte)model.model_choices[0].ref.id item_id := transmute([]byte)model.input_state.picker.items[0].id testing.expect(t, &choice_label[0] == &item_label[0]) testing.expect(t, &choice_id[0] == &item_id[0]) choices = make([dynamic]Model_Choice) append(&choices, Model_Choice { ref = {provider = provider.Provider_Id(strings.clone("test")), id = strings.clone("reopened")}, }) model_open_model_picker(&model, choices, {}) testing.expect_value(t, model.input_state.picker.items[0].label, "reopened") model_close_picker(&model) testing.expect_value(t, len(model.model_choices), 0) testing.expect(t, !model.input_state.picker.active) _, found := app_model_choice(&model, 0) testing.expect(t, !found) variants := make([dynamic]Variant_Choice) append(&variants, Variant_Choice{id = strings.clone("high"), label = strings.clone("high")}) model_open_variant_picker(&model, variants, "") // The picker clones its UI snapshot; the domain data can be destroyed independently. variant_choices_destroy(&model.variant_choices) testing.expect_value(t, model.input_state.picker.items[0].label, "high") _, found = app_variant_choice(&model, 0) testing.expect(t, !found) } @(test) invalid_picker_submit_closes_domain_choices :: proc(t: ^testing.T) { model := init() defer { model_close_picker(&model); input.destroy(&model.input_state) } choices := make([dynamic]Variant_Choice) append(&choices, Variant_Choice{id = strings.clone("low"), label = strings.clone("low")}) model_open_variant_picker(&model, choices, "") model.picker_event = picker.Event{kind = .Submit, token = 999} effects := update(&model, Message{kind = .No_Input}) for index in 0 ..< len(effects) do effect_destroy(&effects[index]) delete(effects) testing.expect_value(t, len(model.variant_choices), 0) testing.expect(t, !model.input_state.picker.active) } @(test) editor_content_replaces_prompt_atomically :: proc(t: ^testing.T) { state: input.State defer input.destroy(&state) testing.expect(t, input.replace(&state, transmute([]byte)string("first\r\nsecond\n"))) testing.expect_value(t, string(state.bytes[:state.len]), "first\nsecond") testing.expect_value(t, state.position, state.len) testing.expect(t, input.replace(&state, transmute([]byte)string("café\tnaïve\n"))) testing.expect_value(t, string(state.bytes[:state.len]), "café\tnaïve") testing.expect_value(t, state.position, state.len) original := string(state.bytes[:state.len]) testing.expect(t, !input.replace(&state, transmute([]byte)string("unsafe\x1bcontent"))) testing.expect_value(t, string(state.bytes[:state.len]), original) invalid_utf8 := [2]byte{'a', 0xff} testing.expect(t, !input.replace(&state, invalid_utf8[:])) testing.expect_value(t, string(state.bytes[:state.len]), original) large: [dynamic]byte defer delete(large) resize(&large, 2 * 1024) for index in 0 ..< len(large) { large[index] = index % 2 == 0 ? 'a' : '\n' } testing.expect(t, input.replace(&state, large[:])) testing.expect_value(t, state.len, len(large) - 1) testing.expect_value(t, state.position, state.len) } @(test) unicode_prompt_editing_keeps_runes_atomic :: proc(t: ^testing.T) { state: input.State defer input.destroy(&state) testing.expect(t, input.replace(&state, transmute([]byte)string("aé中b"))) input.move_left(&state) testing.expect_value(t, state.position, len("aé中")) input.move_left(&state) testing.expect_value(t, state.position, len("aé")) input.delete_before_cursor(&state) testing.expect_value(t, string(state.bytes[:state.len]), "a中b") testing.expect_value(t, state.position, len("a")) input.delete_after_cursor(&state) testing.expect_value(t, string(state.bytes[:state.len]), "ab") } @(test) unicode_and_tabs_render_with_display_width :: proc(t: ^testing.T) { state := input.State{width = 20} defer input.destroy(&state) testing.expect(t, input.replace(&state, transmute([]byte)string("café\t中"))) sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw(&sb, &state, 0, 0, false, false, "") output := strings.to_string(sb) testing.expect(t, strings.contains(output, "café 中")) testing.expect(t, !strings.contains(output, "\t")) testing.expect_value(t, state.cursor, uint(10)) } @(test) editor_uses_colored_marker_and_indented_continuation :: proc(t: ^testing.T) { state := input.State { width = 8, len = 7, position = 7, } defer input.destroy(&state) input.reserve(&state, 7) copy(state.bytes[:], transmute([]u8)string("abcdefg")) sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw(&sb, &state, 0, 0, false, false, "") output := strings.to_string(sb) testing.expect( t, strings.contains(output, theme.DEFAULT.marker + "┃ " + theme.DEFAULT.text + "abcdef"), ) testing.expect(t, strings.contains(output, "\n\x1b[2K" + theme.DEFAULT.marker + "┃ " + theme.DEFAULT.text + "g\n")) testing.expect(t, !strings.contains(output, "┌")) testing.expect(t, !strings.contains(output, "└")) testing.expect(t, !strings.contains(output, "│")) testing.expect(t, strings.has_suffix(output, "\x1b[1A\x1b[4G")) strings.builder_reset(&sb) input.draw(&sb, &state, 0, 0, false, true, "") testing.expect( t, strings.contains( strings.to_string(sb), theme.DEFAULT.marker_prefix + "┃ " + theme.DEFAULT.text + "abcdef", ), ) } @(test) editor_marks_each_newline_separated_line :: proc(t: ^testing.T) { state := input.State {width = 20} defer input.destroy(&state) testing.expect(t, input.replace(&state, transmute([]byte)string("first\nsecond"))) sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw(&sb, &state, 0, 0, false, false, "") output := strings.to_string(sb) marker := theme.DEFAULT.marker + "┃ " + theme.DEFAULT.text testing.expect_value(t, strings.count(output, marker), 2) } @(test) editor_status_is_gray_and_right_aligned :: proc(t: ^testing.T) { state := input.State { width = 32, } sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw_input_status(&sb, &state, "model | 12/100 tokens") output := strings.to_string(sb) testing.expect_value( t, output, "\x1b[2K" + theme.DEFAULT.status + " model | 12/100 tokens" + theme.DEFAULT.reset, ) } @(test) task_progress_truncation_uses_terminal_columns :: proc(t: ^testing.T) { sb := strings.builder_make() defer strings.builder_destroy(&sb) draw_task_progress(&sb, "界界界", 5) testing.expect_value(t, strings.to_string(sb), "界...") } @(test) task_progress_adds_ellipsis_within_its_width :: proc(t: ^testing.T) { sb := strings.builder_make() defer strings.builder_destroy(&sb) draw_task_progress(&sb, "1234567890123456789012345", 21) testing.expect_value(t, strings.to_string(sb), "123456789012345678...") } @(test) task_progress_ignores_terminal_sequences :: proc(t: ^testing.T) { sb := strings.builder_make() defer strings.builder_destroy(&sb) draw_task_progress(&sb, theme.DEFAULT.muted + "Thought: Inspecting files" + theme.DEFAULT.reset, 80) testing.expect_value(t, strings.to_string(sb), "Thought: Inspecting files") } @(test) task_block_renders_prompt_and_latest_two_lines :: proc(t: ^testing.T) { sb := strings.builder_make() defer strings.builder_destroy(&sb) draw_task_block(&sb, "Inspect\nthe parser", "Reading files", "Checking tests", 40) testing.expect_value( t, strings.to_string(sb), theme.DEFAULT.tool_name + "[task]" + theme.DEFAULT.reset + " " + theme.DEFAULT.tool_parameter + "Inspect the parser" + "\r\n\x1b[2K" + theme.DEFAULT.tool_parameter + "│ Reading files" + "\r\n\x1b[2K" + theme.DEFAULT.tool_parameter + "└ Checking tests" + theme.DEFAULT.reset, ) } @(test) task_preview_keeps_parameter_color_for_nested_thoughts_and_tools :: proc(t: ^testing.T) { sb := strings.builder_make() defer strings.builder_destroy(&sb) draw_task_block( &sb, "Explore project", theme.DEFAULT.muted + "~ Thought: Inspecting codebase" + theme.DEFAULT.reset, theme.DEFAULT.tool_name + "[activate_skill]" + theme.DEFAULT.reset + " explore-codebase", 80, ) output := strings.to_string(sb) testing.expect(t, strings.contains(output, "\r\n\x1b[2K" + theme.DEFAULT.tool_parameter + "│ ~ Thought: Inspecting codebase")) testing.expect(t, strings.contains(output, "\r\n\x1b[2K" + theme.DEFAULT.tool_parameter + "└ [activate_skill] explore-codebase" + theme.DEFAULT.reset)) } @(test) file_picker_recurses_and_respects_ignore_files :: proc(t: ^testing.T) { root, root_error := os.make_directory_temp("", "memoria-files-*", context.allocator) testing.expect(t, root_error == nil) if root_error != nil { return } defer { _ = os.remove_all(root) delete(root) } nested, _ := os.join_path([]string{root, "nested"}, context.allocator) defer delete(nested) build, _ := os.join_path([]string{root, "build"}, context.allocator) defer delete(build) git_directory, _ := os.join_path([]string{root, ".git"}, context.allocator) defer delete(git_directory) deep, _ := os.join_path([]string{nested, "deep"}, context.allocator) defer delete(deep) testing.expect(t, os.make_directory_all(nested) == nil) testing.expect(t, os.make_directory_all(build) == nil) testing.expect(t, os.make_directory_all(git_directory) == nil) testing.expect(t, os.make_directory_all(deep) == nil) paths := []string { "visible.txt", "ignored.txt", "keep.log", "drop.log", "build/output.txt", "nested/visible.odin", "nested/hidden.tmp", "nested/deep/secret7.txt", ".git/config", } for path in paths { full_path, _ := os.join_path([]string{root, path}, context.allocator) testing.expect(t, os.write_entire_file(full_path, "test") == nil) delete(full_path) } gitignore, _ := os.join_path([]string{root, ".gitignore"}, context.allocator) defer delete(gitignore) ignore, _ := os.join_path([]string{nested, ".ignore"}, context.allocator) defer delete(ignore) testing.expect( t, os.write_entire_file( gitignore, "ignored.txt\nbuild/\n*.log\n!keep.log\nnested/**/secret[0-9].txt\n", ) == nil, ) testing.expect(t, os.write_entire_file(ignore, "*.tmp\n") == nil) state: input.State defer input.destroy(&state) app_load_files(&state, root) testing.expect(t, app_has_file(&state, "visible.txt")) testing.expect(t, app_has_file(&state, "keep.log")) testing.expect(t, app_has_file(&state, "nested/visible.odin")) testing.expect(t, !app_has_file(&state, "ignored.txt")) testing.expect(t, !app_has_file(&state, "drop.log")) testing.expect(t, !app_has_file(&state, "build/output.txt")) testing.expect(t, !app_has_file(&state, "nested/hidden.tmp")) testing.expect(t, !app_has_file(&state, "nested/deep/secret7.txt")) testing.expect(t, !app_has_file(&state, ".git/config")) } @(test) file_picker_reload_replaces_owned_cache :: proc(t: ^testing.T) { root, err := os.make_directory_temp("", "memoria-reload-*", context.allocator) testing.expect(t, err == nil) if err != nil do return defer { _ = os.remove_all(root); delete(root) } path, _ := os.join_path([]string{root, "fresh.txt"}, context.allocator) defer delete(path) testing.expect(t, os.write_entire_file(path, "test") == nil) state: input.State defer input.destroy(&state) append(&state.files, input.File_Choice{name = strings.clone("old.txt")}) app_load_files(&state, root) testing.expect_value(t, len(state.files), 1) testing.expect(t, app_has_file(&state, "fresh.txt")) testing.expect(t, !app_has_file(&state, "old.txt")) app_load_files(&state, root) testing.expect_value(t, len(state.files), 1) } @(test) file_picker_shows_eight_rows_but_navigates_every_match :: proc(t: ^testing.T) { state := input.State { width = 40, len = 1, position = 1, picker_mode = .Files, } input.reserve(&state, 1) state.bytes[0] = '@' defer input.destroy(&state) for index in 0 ..< 12 { append(&state.files, input.File_Choice{name = fmt.aprintf("file%02d.txt", index)}) } testing.expect_value(t, input.autocomplete_rows(&state), 12) testing.expect_value(t, input.below_rows(&state, false), input.FILE_PICKER_ROWS) for _ in 0 ..< 10 { input.select_next(&state) } testing.expect_value(t, state.selected, 10) sb := strings.builder_make() defer strings.builder_destroy(&sb) input.draw(&sb, &state, 0, 0, false, false, "") output := strings.to_string(sb) testing.expect(t, strings.contains(output, theme.DEFAULT.selection + "file10.txt" + theme.DEFAULT.reset)) testing.expect(t, !strings.contains(output, "file00.txt")) testing.expect(t, input.accept_autocomplete(&state)) testing.expect_value(t, string(state.bytes[:state.len]), "@file10.txt ") testing.expect_value(t, len(state.files), 0) } @(test) file_picker_releases_cache_on_exit_and_reloads_on_reentry :: proc(t: ^testing.T) { root, err := os.make_directory_temp("", "memoria-file-cache-*", context.allocator) testing.expect(t, err == nil) if err != nil do return defer { _ = os.remove_all(root); delete(root) } path, _ := os.join_path([]string{root, "visible.txt"}, context.allocator) defer delete(path) testing.expect(t, os.write_entire_file(path, "test") == nil) state: input.State defer input.destroy(&state) _ = input.replace(&state, transmute([]byte)string("before @vis")) app_update_file_completion(&state, .None, root) testing.expect(t, app_has_file(&state, "visible.txt")) _ = input.insert_byte(&state, 'i') app_update_file_completion(&state, .Files, root) testing.expect(t, app_has_file(&state, "visible.txt")) state.position = 0 app_update_file_completion(&state, .Files, root) testing.expect_value(t, len(state.files), 0) state.position = state.len app_update_file_completion(&state, .None, root) testing.expect(t, app_has_file(&state, "visible.txt")) _ = input.insert_byte(&state, ' ') app_update_file_completion(&state, .Files, root) testing.expect_value(t, len(state.files), 0) _ = input.replace(&state, transmute([]byte)string("@vis")) app_update_file_completion(&state, .None, root) testing.expect(t, app_has_file(&state, "visible.txt")) _ = input.replace(&state, transmute([]byte)string("")) testing.expect_value(t, len(state.files), 0) } @(test) queued_prompt_preserves_second_draft_through_stream_events :: proc(t: ^testing.T) { model := init() defer { delete(model.queued_prompt); stream_destroy(&model.stream); input.destroy(&model.input_state) } stream_start(&model.stream, transmute([]byte)string("first")) _ = input.replace(&model.input_state, transmute([]byte)string(" next\nturn ")) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect_value(t, model.queued_prompt, "next\nturn") testing.expect_value(t, model.input_state.len, 0) _ = input.replace(&model.input_state, transmute([]byte)string(" /quit ")) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect(t, model.running) testing.expect_value(t, model.queued_prompt, "next\nturn") testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), " /quit ") _ = input.replace(&model.input_state, transmute([]byte)string(" ")) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect_value(t, model.queued_prompt, "next\nturn") _ = input.replace(&model.input_state, transmute([]byte)string(" replacement ")) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect_value(t, model.queued_prompt, "next\nturn") _ = input.replace(&model.input_state, transmute([]byte)string("draft")) update(&model, Message{kind = .Stream_Event, stream_event = Stream_Event{kind = .Tool_Calls}}) testing.expect_value(t, take_finished_queue(&model, false), "") testing.expect_value(t, model.queued_prompt, "next\nturn") update(&model, Message{kind = .Stream_Event, stream_event = Stream_Event{kind = .Finished}}) queued := take_finished_queue(&model, true) testing.expect_value(t, queued, "next\nturn") testing.expect_value(t, model.input_state.len, 5) delete(queued) } @(test) queued_prompt_failure_cancel_edit_and_discard :: proc(t: ^testing.T) { kinds := [?]Stream_Event_Kind{.Failed, .Cancelled} for kind in kinds { model := init() stream_start(&model.stream, transmute([]byte)string("first")) model.queued_prompt = strings.clone("saved") update(&model, Message{kind = .Stream_Event, stream_event = Stream_Event{kind = kind}}) testing.expect_value(t, take_finished_queue(&model, false), "") testing.expect_value(t, model.queued_prompt, "saved") _ = input.replace(&model.input_state, transmute([]byte)string("draft")) update(&model, Message{kind = .Key_Pressed, key = '\n'}) testing.expect_value(t, model.queued_prompt, "saved") testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "draft") update(&model, Message{kind = .Key_Pressed, key = 24}) update(&model, Message{kind = .Key_Pressed, key = 'r'}) testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "draft") model.input_state.len = 0 model.input_state.position = 0 update(&model, Message{kind = .Key_Pressed, key = 24}) update(&model, Message{kind = .Key_Pressed, key = 'r'}) testing.expect_value(t, model.queued_prompt, "") testing.expect_value(t, string(model.input_state.bytes[:model.input_state.len]), "saved") model.queued_prompt = strings.clone("discard") update(&model, Message{kind = .Key_Pressed, key = 24}) update(&model, Message{kind = .Key_Pressed, key = 'd'}) testing.expect_value(t, model.queued_prompt, "") input.destroy(&model.input_state) stream_destroy(&model.stream) } } @(test) queued_banner_tracks_input_rows_and_cursor :: proc(t: ^testing.T) { model := init() defer { delete(model.queued_prompt); input.destroy(&model.input_state) } state := Render_State{} sb := strings.builder_make() defer strings.builder_destroy(&sb) render_into(&sb, &model, 10, &state) model.queued_prompt = strings.clone("long\nqueued prompt") strings.builder_reset(&sb) render_into(&sb, &model, 10, &state) testing.expect_value(t, state.input_rows, 2) testing.expect_value(t, state.cursor_row, 1) output := strings.to_string(sb) testing.expect(t, strings.contains(output, "Queued")) testing.expect(t, strings.contains(output, "\x1b[1L")) testing.expect(t, !strings.contains(output, "\x1b[1B\r\x1b[1L")) _ = input.replace(&model.input_state, transmute([]byte)string("123456789")) strings.builder_reset(&sb) render_into(&sb, &model, 10, &state) testing.expect_value(t, state.input_rows, input.row_count(&model.input_state) + 1) testing.expect_value(t, state.cursor_row, model.input_state.cursor_row + 1) delete(model.queued_prompt) model.queued_prompt = "" strings.builder_reset(&sb) render_into(&sb, &model, 10, &state) testing.expect_value(t, state.input_rows, input.row_count(&model.input_state)) testing.expect_value(t, state.cursor_row, model.input_state.cursor_row) }