gpui-ai 0.8.0

AI-native UI components for GPUI applications, built on gpui-component.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::{cell::RefCell, rc::Rc};

use gpui::{
    AppContext as _, Context, Entity, InteractiveElement as _, KeyDownEvent, KeyUpEvent, Keystroke,
    Modifiers, MouseButton, ParentElement as _, Render, Styled as _, Subscription, TestAppContext,
    VisualTestContext, Window, point, px, size,
};
use gpui_ai::prelude::{
    ComparisonFeature, ComparisonItem, ComparisonItemState, ComparisonSnapshot,
    ComparisonSnapshotError, ComparisonTable, ComparisonTableEvent, ComparisonValue, Progressive,
};

#[test]
fn comparison_snapshot_uses_stable_ids_and_accepts_duplicate_visible_labels() {
    let snapshot = ComparisonSnapshot::try_new(
        [
            ComparisonItem::new("starter", "Creamery"),
            ComparisonItem::new("business", "Creamery").state(ComparisonItemState::Highlighted),
        ],
        [
            ComparisonFeature::new("price", "Monthly price").values([
                ComparisonValue::new("starter", "$12"),
                ComparisonValue::new("business", "$24"),
            ]),
            ComparisonFeature::new("support", "Priority support").values([
                ComparisonValue::included("starter", false),
                ComparisonValue::included("business", true),
            ]),
        ],
    )
    .expect("a small stable comparison should be valid");

    assert_eq!(snapshot.items()[0].id(), "starter");
    assert_eq!(snapshot.items()[1].id(), "business");
    assert_eq!(snapshot.items()[0].label(), snapshot.items()[1].label());
    assert_eq!(
        snapshot
            .feature("support")
            .and_then(|feature| feature.value("business"))
            .map(ComparisonValue::display),
        Some("Included")
    );
    assert_eq!(
        snapshot.items()[1].item_state(),
        ComparisonItemState::Highlighted
    );
}

#[test]
fn comparison_snapshot_rejects_duplicate_ids_dangling_values_and_unbounded_shapes() {
    assert_eq!(
        ComparisonSnapshot::try_new(
            [
                ComparisonItem::new("same", "First"),
                ComparisonItem::new("same", "Second"),
            ],
            [],
        ),
        Err(ComparisonSnapshotError::DuplicateItemId("same".into()))
    );

    assert_eq!(
        ComparisonSnapshot::try_new(
            [ComparisonItem::new("known", "Known")],
            [ComparisonFeature::new("feature", "Feature")
                .values([ComparisonValue::new("missing", "value")])],
        ),
        Err(ComparisonSnapshotError::UnknownItemId {
            feature_id: "feature".into(),
            item_id: "missing".into(),
        })
    );

    assert_eq!(
        ComparisonSnapshot::try_new(
            (0..13).map(|index| ComparisonItem::new(format!("item-{index}"), "Item")),
            [],
        ),
        Err(ComparisonSnapshotError::TooManyItems { maximum: 12 })
    );

    assert_eq!(
        ComparisonSnapshot::try_new(
            [ComparisonItem::new("item", "Item")],
            (0..129).map(|index| ComparisonFeature::new(format!("feature-{index}"), "Feature")),
        ),
        Err(ComparisonSnapshotError::TooManyFeatures { maximum: 128 })
    );
}

#[gpui::test]
fn controlled_item_selection_survives_reorder_and_clears_when_disabled(cx: &mut TestAppContext) {
    cx.update(gpui_ai::init);
    let (table, cx) = cx
        .add_window_view(|window, cx| ComparisonTable::new("plans", "Plan comparison", window, cx));

    let snapshot = |business_state| {
        ComparisonSnapshot::try_new(
            [
                ComparisonItem::new("starter", "Starter"),
                ComparisonItem::new("business", "Business").state(business_state),
            ],
            [ComparisonFeature::new("price", "Monthly price").values([
                ComparisonValue::new("starter", "$12"),
                ComparisonValue::new("business", "$24"),
            ])],
        )
        .expect("the fixture should stay inside the bounded contract")
    };

    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            table.set_snapshot(
                Progressive::complete(snapshot(ComparisonItemState::Highlighted)),
                window,
                cx,
            );
            table.set_selected_item("business", window, cx);
        });
    });
    table.read_with(cx, |table, _| {
        assert_eq!(table.selected_item_id(), Some("business"));
    });

    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            let reordered = ComparisonSnapshot::try_new(
                [
                    ComparisonItem::new("business", "Business")
                        .state(ComparisonItemState::Highlighted),
                    ComparisonItem::new("starter", "Starter"),
                ],
                [ComparisonFeature::new("price", "Monthly price").values([
                    ComparisonValue::new("business", "$24"),
                    ComparisonValue::new("starter", "$12"),
                ])],
            )
            .expect("the reordered fixture should be valid");
            table.set_snapshot(Progressive::complete(reordered), window, cx);
        });
    });
    table.read_with(cx, |table, _| {
        assert_eq!(table.selected_item_id(), Some("business"));
    });

    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            table.set_snapshot(
                Progressive::complete(snapshot(ComparisonItemState::Disabled)),
                window,
                cx,
            );
        });
    });
    table.read_with(cx, |table, _| {
        assert_eq!(table.selected_item_id(), None);
    });
}

struct ComparisonEventHarness {
    table: Entity<ComparisonTable>,
    events: Rc<RefCell<Vec<ComparisonTableEvent>>>,
    _subscription: Subscription,
}

impl ComparisonEventHarness {
    fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
        let table = cx.new(|cx| ComparisonTable::new("plans", "Plan comparison", window, cx));
        table.update(cx, |table, cx| {
            let snapshot = ComparisonSnapshot::try_new(
                [
                    ComparisonItem::new("starter", "Starter"),
                    ComparisonItem::new("business", "Business")
                        .state(ComparisonItemState::Highlighted),
                    ComparisonItem::new("legacy", "Legacy").state(ComparisonItemState::Disabled),
                ],
                [ComparisonFeature::new("price", "Monthly price").values([
                    ComparisonValue::new("starter", "$12"),
                    ComparisonValue::new("business", "$24"),
                    ComparisonValue::new("legacy", "$8"),
                ])],
            )
            .expect("the interaction fixture should be valid");
            table.set_snapshot(Progressive::complete(snapshot), window, cx);
            table.set_selected_item("starter", window, cx);
        });
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let subscription = cx.subscribe(&table, move |_, _, event, _| {
            captured.borrow_mut().push(event.clone());
        });
        Self {
            table,
            events,
            _subscription: subscription,
        }
    }
}

impl Render for ComparisonEventHarness {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl gpui::IntoElement {
        self.table.clone()
    }
}

struct ComparisonSelectionHarness {
    table: Entity<ComparisonTable>,
}

impl ComparisonSelectionHarness {
    fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
        let table = cx.new(|cx| {
            let mut table = ComparisonTable::new("literal", "Literal values", window, cx);
            let literal = "# plan *literal* <value> [x] | + - !";
            let snapshot = ComparisonSnapshot::try_new(
                [ComparisonItem::new("plan", "Plan")],
                [ComparisonFeature::new("syntax", "Syntax")
                    .values([ComparisonValue::new("plan", format!("{literal} {literal}"))])],
            )
            .expect("the literal fixture should be valid");
            table.set_snapshot(Progressive::complete(snapshot), window, cx);
            table
        });
        Self { table }
    }
}

impl Render for ComparisonSelectionHarness {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl gpui::IntoElement {
        gpui::div()
            .id("comparison-selection-root")
            .relative()
            .size_full()
            .child(gpui_base::TextSelectionLayer)
            .child(self.table.clone())
    }
}

fn activate_key(cx: &mut VisualTestContext, key: &str) {
    let keystroke = Keystroke::parse(key).expect("test key should parse");
    cx.simulate_event(KeyDownEvent {
        keystroke: keystroke.clone(),
        is_held: false,
        prefer_character_input: false,
    });
    cx.simulate_event(KeyUpEvent { keystroke });
    cx.run_until_parked();
}

#[gpui::test]
fn pointer_and_keyboard_selection_emit_stable_item_identity(cx: &mut TestAppContext) {
    cx.update(gpui_ai::init);
    let (harness, cx) = cx.add_window_view(ComparisonEventHarness::new);
    let cx: &mut VisualTestContext = cx;
    cx.simulate_resize(size(px(560.), px(320.)));
    cx.update(|window, cx| window.draw(cx).clear(cx));
    cx.run_until_parked();
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let business = cx
        .debug_bounds("comparison-item-control:plans:business")
        .expect("the stable comparison item control should render");
    cx.simulate_click(business.center(), Modifiers::default());
    assert_eq!(
        harness.read_with(cx, |harness, _| harness.events.borrow().clone()),
        [ComparisonTableEvent::SelectionRequested {
            id: "plans".into(),
            item_id: "business".into(),
        }]
    );

    harness.update(cx, |harness, _| harness.events.borrow_mut().clear());
    let table = harness.read_with(cx, |harness, _| harness.table.clone());
    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            table.focus_item("starter", window, cx);
        });
    });
    activate_key(cx, "right");
    activate_key(cx, "enter");
    assert_eq!(
        harness.read_with(cx, |harness, _| harness.events.borrow().clone()),
        [ComparisonTableEvent::SelectionRequested {
            id: "plans".into(),
            item_id: "business".into(),
        }]
    );
}

#[gpui::test]
fn long_bounded_comparison_keeps_the_last_item_horizontally_reachable(cx: &mut TestAppContext) {
    cx.update(gpui_ai::init);
    let (table, cx) = cx.add_window_view(|window, cx| {
        ComparisonTable::new("wide-plans", "Wide plan comparison", window, cx)
    });
    let cx: &mut VisualTestContext = cx;
    cx.simulate_resize(size(px(420.), px(280.)));
    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            let items = (0..12)
                .map(|index| {
                    ComparisonItem::new(
                        format!("plan-{index}"),
                        format!("Plan {index} with a deliberately long label"),
                    )
                })
                .collect::<Vec<_>>();
            let values = (0..12)
                .map(|index| ComparisonValue::new(format!("plan-{index}"), format!("${index}")))
                .collect::<Vec<_>>();
            let snapshot = ComparisonSnapshot::try_new(
                items,
                [ComparisonFeature::new("price", "Monthly price").values(values)],
            )
            .expect("the maximum bounded shape should be valid");
            table.set_snapshot(Progressive::complete(snapshot), window, cx);
        });
        window.draw(cx).clear(cx);
    });
    cx.run_until_parked();
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let viewport = cx
        .debug_bounds("comparison-table-root:wide-plans")
        .expect("the comparison viewport should render");
    let last = cx
        .debug_bounds("comparison-item-control:wide-plans:plan-11")
        .expect("the final bounded item should be constructed");
    assert!(last.left() >= viewport.right());

    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            table.focus_item("plan-11", window, cx);
        });
        window.draw(cx).clear(cx);
    });
    cx.run_until_parked();
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let last = cx
        .debug_bounds("comparison-item-control:wide-plans:plan-11")
        .expect("the final bounded item should remain mounted");
    assert!(
        last.left() >= viewport.left() && last.right() <= viewport.right(),
        "last={last:?}, viewport={viewport:?}"
    );
    let header = cx
        .debug_bounds("comparison-item-header:wide-plans:plan-11")
        .expect("the final header should remain mounted");
    let cell = cx
        .debug_bounds("comparison-cell:wide-plans:price:plan-11")
        .expect("the final value cell should remain mounted");
    assert!(
        (header.left() - cell.left()).abs() <= px(1.)
            && (header.right() - cell.right()).abs() <= px(1.),
        "header={header:?}, cell={cell:?}"
    );
    assert!(last.left() >= header.left() && last.right() <= header.right());
}

#[gpui::test]
fn maximum_feature_snapshot_keeps_the_final_stable_feature_vertically_reachable(
    cx: &mut TestAppContext,
) {
    cx.update(gpui_ai::init);
    let (table, cx) = cx.add_window_view(|window, cx| {
        ComparisonTable::new("tall-plans", "Tall plan comparison", window, cx)
    });
    let cx: &mut VisualTestContext = cx;
    cx.simulate_resize(size(px(420.), px(280.)));
    cx.update(|window, cx| {
        table.update(cx, |table, cx| {
            let snapshot = ComparisonSnapshot::try_new(
                [ComparisonItem::new("plan", "Plan")],
                (0..128).map(|index| {
                    ComparisonFeature::new(
                        format!("feature-{index}"),
                        if index == 64 {
                            "Feature 64 with a deliberately wrapping intermediate label".to_owned()
                        } else {
                            format!("Feature {index}")
                        },
                    )
                    .description(
                        "Selectable supporting detail that makes every feature row variable height",
                    )
                    .values([ComparisonValue::new("plan", format!("Value {index}"))])
                }),
            )
            .expect("the maximum bounded feature shape should be valid");
            table.set_snapshot(Progressive::complete(snapshot), window, cx);
        });
        window.draw(cx).clear(cx);
    });
    cx.run_until_parked();
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let viewport = cx
        .debug_bounds("comparison-table-root:tall-plans")
        .expect("the tall comparison viewport should render");
    // Reachability, not mounting: feature rows are virtualized, so a feature
    // 127 rows below the fold must cost nothing until the reader goes there.
    assert!(
        cx.debug_bounds("comparison-feature:tall-plans:feature-127")
            .is_none(),
        "a feature far below the viewport must not be constructed"
    );
    let first_row = cx
        .debug_bounds("comparison-feature:tall-plans:feature-0")
        .expect("the first feature should be on screen");
    assert!(
        first_row.top() < viewport.bottom() && first_row.bottom() > viewport.top(),
        "first_row={first_row:?}, viewport={viewport:?}"
    );

    // The wrapping label is the row-measurement stressor: it is taller than
    // its neighbours, so a list that measured rows uniformly would place it
    // wrong.
    table.update(cx, |table, cx| {
        table.scroll_to_feature("feature-64", cx);
    });
    cx.update(|window, cx| window.draw(cx).clear(cx));
    let intermediate = cx
        .debug_bounds("comparison-feature:tall-plans:feature-64")
        .expect("the intermediate described feature should be constructed on demand");
    assert!(
        intermediate.center().y >= viewport.top() && intermediate.center().y <= viewport.bottom(),
        "intermediate={intermediate:?}, viewport={viewport:?}"
    );
    assert!(
        intermediate.size.height > first_row.size.height,
        "the wrapping label must measure taller than a plain row: \
         intermediate={intermediate:?}, first_row={first_row:?}"
    );

    table.update(cx, |table, cx| {
        table.scroll_to_feature("feature-127", cx);
    });
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let final_row = cx
        .debug_bounds("comparison-feature:tall-plans:feature-127")
        .expect("the final feature should be constructed once it is revealed");
    assert!(
        final_row.top() >= viewport.top() - px(1.)
            && final_row.top() < viewport.bottom()
            && final_row.bottom() > viewport.top(),
        "final_row={final_row:?}, viewport={viewport:?}"
    );
    // Revealing the end of a long comparison must not drag the whole
    // snapshot back into the frame.
    assert!(
        cx.debug_bounds("comparison-feature:tall-plans:feature-0")
            .is_none(),
        "the first feature must fall out of the window once the last is revealed"
    );
}

#[gpui::test]
fn comparison_values_export_literal_selected_text(cx: &mut TestAppContext) {
    cx.update(gpui_ai::init);
    let (_, cx) = cx.add_window_view(ComparisonSelectionHarness::new);
    let cx: &mut VisualTestContext = cx;
    cx.simulate_resize(size(px(520.), px(260.)));
    cx.run_until_parked();
    cx.update(|window, cx| window.draw(cx).clear(cx));
    cx.run_until_parked();
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let cell = cx
        .debug_bounds("comparison-cell:literal:syntax:plan")
        .expect("the literal comparison cell should render");
    let from = point(cell.left() + px(10.), cell.top() + px(12.));
    let to = point(cell.right() - px(10.), cell.bottom() - px(12.));
    cx.simulate_mouse_down(from, MouseButton::Left, Modifiers::default());
    cx.update(|window, cx| window.draw(cx).clear(cx));
    cx.simulate_mouse_move(to, Some(MouseButton::Left), Modifiers::default());
    cx.update(|window, cx| window.draw(cx).clear(cx));
    cx.simulate_mouse_up(to, MouseButton::Left, Modifiers::default());
    cx.update(|window, cx| window.draw(cx).clear(cx));

    let selected = cx.update(gpui_base::TextSelection::selected_text);
    assert!(
        selected.contains("# plan *literal* <value> [x] | + - !"),
        "{selected:?}"
    );
}