|
| 1 | +module gui |
| 2 | + |
| 3 | +fn test_fuzzy_score_exact_match() { |
| 4 | + assert list_core_fuzzy_score('open', 'open') == 0 |
| 5 | +} |
| 6 | + |
| 7 | +fn test_fuzzy_score_subsequence() { |
| 8 | + s := list_core_fuzzy_score('open file', 'of') |
| 9 | + assert s > 0 |
| 10 | +} |
| 11 | + |
| 12 | +fn test_fuzzy_score_no_match() { |
| 13 | + assert list_core_fuzzy_score('open', 'z') == -1 |
| 14 | +} |
| 15 | + |
| 16 | +fn test_fuzzy_score_empty_query() { |
| 17 | + assert list_core_fuzzy_score('anything', '') == 0 |
| 18 | +} |
| 19 | + |
| 20 | +fn test_fuzzy_score_empty_candidate() { |
| 21 | + assert list_core_fuzzy_score('', 'q') == -1 |
| 22 | +} |
| 23 | + |
| 24 | +fn test_fuzzy_score_case_insensitive() { |
| 25 | + // 'O' at 0, 'F' at 4 → gap 3. |
| 26 | + s1 := list_core_fuzzy_score('OpenFile', 'of') |
| 27 | + s2 := list_core_fuzzy_score('openfile', 'OF') |
| 28 | + assert s1 >= 0 |
| 29 | + assert s1 == s2 |
| 30 | +} |
| 31 | + |
| 32 | +fn test_filter_empty_query() { |
| 33 | + items := [ |
| 34 | + ListCoreItem{ |
| 35 | + id: 'a' |
| 36 | + label: 'alpha' |
| 37 | + }, |
| 38 | + ListCoreItem{ |
| 39 | + id: 'h' |
| 40 | + label: 'heading' |
| 41 | + is_subheading: true |
| 42 | + }, |
| 43 | + ListCoreItem{ |
| 44 | + id: 'b' |
| 45 | + label: 'beta' |
| 46 | + }, |
| 47 | + ] |
| 48 | + result := list_core_filter(items, '') |
| 49 | + assert result.len == 3 |
| 50 | + assert result == [0, 1, 2] |
| 51 | +} |
| 52 | + |
| 53 | +fn test_filter_ranks_by_score() { |
| 54 | + items := [ |
| 55 | + ListCoreItem{ |
| 56 | + id: 'a' |
| 57 | + label: 'x_o_p_e_n' |
| 58 | + }, |
| 59 | + ListCoreItem{ |
| 60 | + id: 'b' |
| 61 | + label: 'open' |
| 62 | + }, |
| 63 | + ] |
| 64 | + result := list_core_filter(items, 'open') |
| 65 | + assert result.len == 2 |
| 66 | + // Exact match (score 0) should come first. |
| 67 | + assert result[0] == 1 |
| 68 | + assert result[1] == 0 |
| 69 | +} |
| 70 | + |
| 71 | +fn test_filter_skips_subheadings() { |
| 72 | + items := [ |
| 73 | + ListCoreItem{ |
| 74 | + id: 'h' |
| 75 | + label: 'open group' |
| 76 | + is_subheading: true |
| 77 | + }, |
| 78 | + ListCoreItem{ |
| 79 | + id: 'a' |
| 80 | + label: 'open file' |
| 81 | + }, |
| 82 | + ] |
| 83 | + result := list_core_filter(items, 'open') |
| 84 | + assert result.len == 1 |
| 85 | + assert result[0] == 1 |
| 86 | +} |
| 87 | + |
| 88 | +fn test_visible_range_empty() { |
| 89 | + first, last := list_core_visible_range(0, 20, 100, 0) |
| 90 | + assert first == 0 |
| 91 | + assert last == -1 |
| 92 | +} |
| 93 | + |
| 94 | +fn test_visible_range_basic() { |
| 95 | + first, last := list_core_visible_range(50, 20, 100, 0) |
| 96 | + assert first == 0 |
| 97 | + assert last > 0 |
| 98 | + assert last < 50 |
| 99 | +} |
| 100 | + |
| 101 | +fn test_visible_range_scroll() { |
| 102 | + first_a, _ := list_core_visible_range(50, 20, 100, 0) |
| 103 | + first_b, _ := list_core_visible_range(50, 20, 100, -200) |
| 104 | + assert first_b > first_a |
| 105 | +} |
| 106 | + |
| 107 | +fn test_visible_range_clamp() { |
| 108 | + _, last := list_core_visible_range(10, 20, 100, -9999) |
| 109 | + assert last <= 9 |
| 110 | +} |
| 111 | + |
| 112 | +fn test_navigate_keys() { |
| 113 | + assert list_core_navigate(.up, 5, 2) == .move_up |
| 114 | + assert list_core_navigate(.down, 5, 2) == .move_down |
| 115 | + assert list_core_navigate(.enter, 5, 2) == .select_item |
| 116 | + assert list_core_navigate(.escape, 5, 2) == .dismiss |
| 117 | + assert list_core_navigate(.home, 5, 2) == .first |
| 118 | + assert list_core_navigate(.end, 5, 2) == .last |
| 119 | +} |
| 120 | + |
| 121 | +fn test_navigate_empty() { |
| 122 | + assert list_core_navigate(.up, 0, 0) == .none |
| 123 | + assert list_core_navigate(.down, 0, 0) == .none |
| 124 | +} |
| 125 | + |
| 126 | +fn test_row_height_estimate() { |
| 127 | + style := TextStyle{ |
| 128 | + size: 14 |
| 129 | + } |
| 130 | + pad := Padding{ |
| 131 | + top: 2 |
| 132 | + bottom: 3 |
| 133 | + } |
| 134 | + assert list_core_row_height_estimate(style, pad) == 19 |
| 135 | +} |
| 136 | + |
| 137 | +fn test_to_lower_byte() { |
| 138 | + assert to_lower_byte(u8(0x41)) == u8(0x61) // A -> a |
| 139 | + assert to_lower_byte(u8(0x5A)) == u8(0x7A) // Z -> z |
| 140 | + assert to_lower_byte(u8(0x61)) == u8(0x61) // a -> a |
| 141 | + assert to_lower_byte(u8(0x30)) == u8(0x30) // 0 unchanged |
| 142 | +} |
| 143 | + |
| 144 | +fn test_apply_nav_move_up() { |
| 145 | + next, changed := list_core_apply_nav(.move_up, 3, 10) |
| 146 | + assert next == 2 |
| 147 | + assert changed == true |
| 148 | +} |
| 149 | + |
| 150 | +fn test_apply_nav_move_up_at_top() { |
| 151 | + next, changed := list_core_apply_nav(.move_up, 0, 10) |
| 152 | + assert next == 0 |
| 153 | + assert changed == false |
| 154 | +} |
| 155 | + |
| 156 | +fn test_apply_nav_move_down() { |
| 157 | + next, changed := list_core_apply_nav(.move_down, 3, 10) |
| 158 | + assert next == 4 |
| 159 | + assert changed == true |
| 160 | +} |
| 161 | + |
| 162 | +fn test_apply_nav_move_down_at_bottom() { |
| 163 | + next, changed := list_core_apply_nav(.move_down, 9, 10) |
| 164 | + assert next == 9 |
| 165 | + assert changed == false |
| 166 | +} |
| 167 | + |
| 168 | +fn test_apply_nav_first() { |
| 169 | + next, changed := list_core_apply_nav(.first, 5, 10) |
| 170 | + assert next == 0 |
| 171 | + assert changed == true |
| 172 | +} |
| 173 | + |
| 174 | +fn test_apply_nav_last() { |
| 175 | + next, changed := list_core_apply_nav(.last, 0, 10) |
| 176 | + assert next == 9 |
| 177 | + assert changed == true |
| 178 | +} |
| 179 | + |
| 180 | +fn test_apply_nav_none() { |
| 181 | + next, changed := list_core_apply_nav(.none, 5, 10) |
| 182 | + assert next == 5 |
| 183 | + assert changed == false |
| 184 | +} |
| 185 | + |
| 186 | +fn test_prepare_filters_and_clamps() { |
| 187 | + items := [ |
| 188 | + ListCoreItem{ |
| 189 | + id: 'a' |
| 190 | + label: 'alpha' |
| 191 | + }, |
| 192 | + ListCoreItem{ |
| 193 | + id: 'b' |
| 194 | + label: 'beta' |
| 195 | + }, |
| 196 | + ListCoreItem{ |
| 197 | + id: 'c' |
| 198 | + label: 'gamma' |
| 199 | + }, |
| 200 | + ] |
| 201 | + p := list_core_prepare(items, 'al', 99) |
| 202 | + assert p.items.len == 1 |
| 203 | + assert p.items[0].id == 'a' |
| 204 | + assert p.ids == ['a'] |
| 205 | + assert p.hl == 0 // clamped from 99 |
| 206 | +} |
| 207 | + |
| 208 | +fn test_prepare_empty_query_returns_all() { |
| 209 | + items := [ |
| 210 | + ListCoreItem{ |
| 211 | + id: 'a' |
| 212 | + label: 'alpha' |
| 213 | + }, |
| 214 | + ListCoreItem{ |
| 215 | + id: 'b' |
| 216 | + label: 'beta' |
| 217 | + }, |
| 218 | + ] |
| 219 | + p := list_core_prepare(items, '', 1) |
| 220 | + assert p.items.len == 2 |
| 221 | + assert p.hl == 1 |
| 222 | +} |
| 223 | + |
| 224 | +fn test_prepare_excludes_subheadings_from_ids() { |
| 225 | + items := [ |
| 226 | + ListCoreItem{ |
| 227 | + id: 'h' |
| 228 | + label: 'heading' |
| 229 | + is_subheading: true |
| 230 | + }, |
| 231 | + ListCoreItem{ |
| 232 | + id: 'a' |
| 233 | + label: 'alpha' |
| 234 | + }, |
| 235 | + ] |
| 236 | + p := list_core_prepare(items, '', 0) |
| 237 | + // Items includes subheading for rendering. |
| 238 | + assert p.items.len == 2 |
| 239 | + // IDs excludes subheading. |
| 240 | + assert p.ids.len == 1 |
| 241 | + assert p.ids[0] == 'a' |
| 242 | +} |
0 commit comments