gpui-box-kit 0.1.1

GPUI Box Kit design-system components and interaction primitives
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! What a run is allowed to claim: `ToolCallCard`, `StepList`, `ThinkingBlock`.

use std::cell::RefCell;
use std::rc::Rc;

use gpui::{IntoElement, ParentElement, TestAppContext};
use gpui_kit::prelude::*;
use gpui_kit_semantics::Role;
use gpui_kit_testkit::harness::Harness;

type Calls = Rc<RefCell<Vec<String>>>;

const SECRET: &str = "sk-live-not-a-real-key";

// ----------------------------------------------------------- tool call card

fn five_states(cx: &mut TestAppContext) -> Harness {
    Harness::new(cx, gpui_kit::install, |_, _| {
        gpui::div()
            .child(
                ToolCallCard::new("call.pending", "workspace.search")
                    .state(ToolCallState::PendingApproval),
            )
            .child(
                ToolCallCard::new("call.running", "workspace.index")
                    .state(ToolCallState::Running)
                    .elapsed("4.2 s"),
            )
            .child(
                ToolCallCard::new("call.succeeded", "workspace.read")
                    .state(ToolCallState::succeeded("one line"))
                    .elapsed("0.3 s"),
            )
            .child(
                ToolCallCard::new("call.silent", "workspace.touch")
                    .state(ToolCallState::succeeded_silently())
                    .elapsed("0.1 s"),
            )
            .child(
                ToolCallCard::new("call.failed", "workspace.write")
                    .state(ToolCallState::failed("The path is read only."))
                    .elapsed("0.2 s"),
            )
            .child(
                ToolCallCard::new("call.refused", "shell.run")
                    .state(ToolCallState::refused("This workspace allows no shell.")),
            )
            .into_any_element()
    })
}

#[gpui::test]
fn each_of_the_five_states_presents_as_itself(cx: &mut TestAppContext) {
    let mut harness = five_states(cx);

    let states = [
        ("call.pending", "pending-approval"),
        ("call.running", "running"),
        ("call.succeeded", "succeeded"),
        ("call.failed", "failed"),
        ("call.refused", "refused"),
    ];
    for (id, expected) in states {
        let card = harness.node(id).expect("published");
        assert_eq!(card.role, Role::Group);
        assert_eq!(
            card.value.as_deref(),
            Some(expected),
            "{id} published the wrong state"
        );
    }

    assert!(
        harness.node("call.running").expect("published").busy,
        "a call in flight says so"
    );
    assert!(
        !harness.node("call.pending").expect("published").busy,
        "nothing is in flight while approval is outstanding"
    );

    // The words beside each card differ too, so the states are not five
    // identical cards distinguished only by a colour.
    let mut labels: Vec<String> = states
        .iter()
        .map(|(id, _)| {
            harness
                .node(&format!("{id}.state"))
                .expect("published")
                .text
                .clone()
                .expect("a state is named")
        })
        .collect();
    let before = labels.len();
    labels.sort();
    labels.dedup();
    assert_eq!(labels.len(), before, "two states share their wording");
}

#[gpui::test]
fn a_refusal_is_not_an_error_and_not_an_empty_result(cx: &mut TestAppContext) {
    let mut harness = five_states(cx);

    let refusal = harness.node("call.refused.refusal").expect("published");
    assert_eq!(
        refusal.text.as_deref(),
        Some("This workspace allows no shell."),
        "the host's words are shown as the host wrote them"
    );
    assert!(
        harness.node("call.refused.error").is_none(),
        "a refusal is not an error"
    );
    assert!(
        harness.node("call.refused.result").is_none(),
        "a refusal is not a result, empty or otherwise"
    );
    assert!(
        harness.node("call.refused.elapsed").is_none(),
        "nothing ran, so there is no duration to claim"
    );

    // The call that did run and returned nothing is a different card in a
    // different state, which is the confusion this component exists to stop.
    let silent = harness.node("call.silent").expect("published");
    assert_eq!(silent.value.as_deref(), Some("succeeded"));
    assert_eq!(
        harness
            .node("call.silent.result")
            .expect("published")
            .value
            .as_deref(),
        Some("nothing")
    );
    assert!(harness.node("call.refused").expect("published").value != silent.value);
}

#[gpui::test]
fn a_failed_call_keeps_its_error_on_screen(cx: &mut TestAppContext) {
    let mut harness = five_states(cx);

    let error = harness.node("call.failed.error").expect("published");
    assert_eq!(error.text.as_deref(), Some("The path is read only."));
    assert!(
        harness.node("call.failed.refusal").is_none(),
        "a failure is not a refusal"
    );
    assert_eq!(
        harness
            .node("call.failed.elapsed")
            .expect("published")
            .value
            .as_deref(),
        Some("0.2 s"),
        "a failure keeps everything else it knows too"
    );
}

#[gpui::test]
fn a_duration_nobody_stated_says_so_rather_than_reading_zero(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        ToolCallCard::new("call.running", "workspace.index")
            .state(ToolCallState::Running)
            .into_any_element()
    });

    assert_eq!(
        harness
            .node("call.running.elapsed")
            .expect("published")
            .value
            .as_deref(),
        Some("unknown")
    );
}

#[gpui::test]
fn a_truncated_body_says_how_much_it_left_out(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        gpui::div()
            .child(
                ToolCallCard::new("call.cut", "workspace.read")
                    .arguments(ToolBody::new("one\ntwo\nthree\nfour").max_lines(2))
                    .state(ToolCallState::succeeded(
                        ToolBody::new("alpha\nbeta\ngamma").max_lines(1),
                    )),
            )
            .child(
                ToolCallCard::new("call.whole", "workspace.read")
                    .arguments(ToolBody::new("one\ntwo"))
                    .state(ToolCallState::succeeded("only this")),
            )
            .into_any_element()
    });

    assert_eq!(
        harness
            .node("call.cut.arguments")
            .expect("published")
            .value
            .as_deref(),
        Some("2 of 4 lines shown")
    );
    assert_eq!(
        harness
            .node("call.cut.result")
            .expect("published")
            .value
            .as_deref(),
        Some("1 of 3 lines shown")
    );
    // A body that was not cut states its size rather than staying silent, so
    // "there is more" is read off the same line every time.
    assert_eq!(
        harness
            .node("call.whole.arguments")
            .expect("published")
            .value
            .as_deref(),
        Some("2 lines")
    );
    assert_eq!(
        harness
            .node("call.whole.result")
            .expect("published")
            .value
            .as_deref(),
        Some("1 line")
    );
}

#[gpui::test]
fn no_argument_or_result_content_reaches_the_semantic_tree(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        ToolCallCard::new("call.secret", "workspace.connect")
            .arguments(format!("{{ \"token\": \"{SECRET}\" }}"))
            .state(ToolCallState::succeeded(format!("connected with {SECRET}")))
            .into_any_element()
    });

    let snapshot = harness.snapshot();
    let leaked = snapshot.nodes.iter().any(|node| {
        [
            node.text.as_deref(),
            node.value.as_deref(),
            node.labels.as_deref(),
        ]
        .into_iter()
        .flatten()
        .any(|text| text.contains(SECRET))
    });
    assert!(
        !leaked,
        "caller-owned content must never reach a snapshot: only its shape"
    );
    assert_eq!(
        harness
            .node("call.secret.arguments")
            .expect("published")
            .value
            .as_deref(),
        Some("1 line"),
        "the shape is what is published instead"
    );
}

#[gpui::test]
fn only_a_failed_call_installs_the_retry_handler(cx: &mut TestAppContext) {
    let calls: Calls = Rc::new(RefCell::new(Vec::new()));
    let sink = Rc::clone(&calls);
    let mut harness = Harness::new(cx, gpui_kit::install, move |_, _| {
        let failed = Rc::clone(&sink);
        let succeeded = Rc::clone(&sink);
        let refused = Rc::clone(&sink);
        gpui::div()
            .child(
                ToolCallCard::new("call.failed", "workspace.write")
                    .state(ToolCallState::failed("The path is read only."))
                    .on_retry(move |_, _| failed.borrow_mut().push("failed".to_string())),
            )
            .child(
                ToolCallCard::new("call.succeeded", "workspace.read")
                    .state(ToolCallState::succeeded("one line"))
                    .on_retry(move |_, _| succeeded.borrow_mut().push("succeeded".to_string())),
            )
            .child(
                ToolCallCard::new("call.refused", "shell.run")
                    .state(ToolCallState::refused("No shell here."))
                    .on_retry(move |_, _| refused.borrow_mut().push("refused".to_string())),
            )
            .into_any_element()
    });

    assert!(
        harness.node("call.succeeded.retry").is_none(),
        "there is nothing to try again about a call that worked"
    );
    assert!(
        harness.node("call.refused.retry").is_none(),
        "a decision is not retried by the card that reports it"
    );
    harness.click("call.failed.retry");
    assert_eq!(*calls.borrow(), vec!["failed".to_string()]);
}

// ------------------------------------------------------------------ step list

fn run(length: RunLength) -> StepList {
    StepList::new("run.steps")
        .length(length)
        .step(Step::new("read", "Read the brief").state(StepState::Done))
        .step(Step::new("search", "Search the workspace").state(StepState::Running))
        .step(Step::new("summarise", "Summarise what was found"))
        .step(
            Step::new("publish", "Publish the summary")
                .state(StepState::Skipped("Publishing is turned off.".into())),
        )
        .step(
            Step::new("notify", "Notify the reviewers")
                .state(StepState::Failed("The service refused the request.".into())),
        )
}

#[gpui::test]
fn each_step_publishes_its_own_state_under_its_own_identity(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        run(RunLength::Known).into_any_element()
    });

    let expected = [
        ("run.steps.read", "done"),
        ("run.steps.search", "running"),
        ("run.steps.summarise", "pending"),
        ("run.steps.publish", "skipped"),
        ("run.steps.notify", "failed"),
    ];
    for (id, state) in expected {
        let step = harness.node(id).expect("published");
        assert_eq!(step.role, Role::Row);
        assert_eq!(step.value.as_deref(), Some(state));
    }
    assert!(harness.node("run.steps.search").expect("published").busy);
    assert_eq!(
        harness
            .node("run.steps")
            .expect("published")
            .value
            .as_deref(),
        Some("5")
    );
}

#[gpui::test]
fn a_skipped_step_and_a_failed_one_are_two_different_sentences(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        run(RunLength::Known).into_any_element()
    });

    let skipped = harness.node("run.steps.publish.reason").expect("published");
    assert_eq!(skipped.text.as_deref(), Some("Publishing is turned off."));
    assert_eq!(skipped.value.as_deref(), Some("skipped"));

    let failed = harness.node("run.steps.notify.reason").expect("published");
    assert_eq!(
        failed.text.as_deref(),
        Some("The service refused the request.")
    );
    assert_eq!(failed.value.as_deref(), Some("failed"));

    assert!(
        harness.node("run.steps.read.reason").is_none(),
        "a step that simply finished has nothing to explain"
    );
}

#[gpui::test]
fn a_run_of_unknown_length_renders_no_determinate_progress(cx: &mut TestAppContext) {
    let mut counted = Harness::new(cx, gpui_kit::install, |_, _| {
        run(RunLength::Known).into_any_element()
    });
    let known = counted.node("run.steps.progress").expect("published");
    assert_eq!(known.role, Role::Progress);
    assert_eq!(known.value_now, Some(0.2), "one of five steps is done");
    assert_eq!(known.value.as_deref(), Some("1 of 5"));

    let mut open = Harness::new(cx, gpui_kit::install, |_, _| {
        run(RunLength::Unknown).into_any_element()
    });
    let unknown = open.node("run.steps.progress").expect("published");
    assert!(unknown.busy, "the run is still going");
    assert_eq!(
        unknown.value_now, None,
        "a run nobody counted has no fraction to report"
    );
    assert_eq!(
        unknown.value.as_deref(),
        Some("1 step done"),
        "what is known is reported; what is not is not invented"
    );
}

#[gpui::test]
fn a_step_carries_the_tool_call_it_is_made_of(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        StepList::new("run.steps")
            .step(
                Step::new("search", "Search the workspace")
                    .state(StepState::Running)
                    .body(
                        ToolCallCard::new("run.steps.search.call", "workspace.search")
                            .arguments("{ \"query\": \"budget\" }")
                            .state(ToolCallState::Running),
                    ),
            )
            .into_any_element()
    });

    let call = harness.node("run.steps.search.call").expect("published");
    assert_eq!(call.value.as_deref(), Some("running"));
    assert_eq!(call.text.as_deref(), Some("workspace.search"));
}

// --------------------------------------------------------------- thinking

fn reasoning(cx: &mut TestAppContext) -> Harness {
    Harness::new(cx, gpui_kit::install, |_, _| {
        gpui::div()
            .child(ThinkingBlock::new(
                "turn.shut",
                Reasoning::present("Read both files before answering."),
            ))
            .child(
                ThinkingBlock::new(
                    "turn.open",
                    Reasoning::present("Read both files before answering."),
                )
                .expanded(true),
            )
            .child(ThinkingBlock::new(
                "turn.withheld",
                Reasoning::withheld("This connection does not hand over reasoning."),
            ))
            .child(ThinkingBlock::new("turn.absent", Reasoning::Absent))
            .into_any_element()
    })
}

#[gpui::test]
fn withheld_collapsed_and_absent_are_three_presentations(cx: &mut TestAppContext) {
    let mut harness = reasoning(cx);

    let shut = harness.node("turn.shut").expect("published");
    assert_eq!(shut.value.as_deref(), Some("present"));
    assert_eq!(shut.expanded, Some(false), "collapsed by default");
    assert!(
        harness.node("turn.shut.body").is_none(),
        "a closed block renders no body, so nothing invisible stays addressable"
    );

    let open = harness.node("turn.open").expect("published");
    assert_eq!(open.value.as_deref(), Some("present"));
    assert_eq!(open.expanded, Some(true));
    assert!(harness.node("turn.open.body").is_some());

    let withheld = harness.node("turn.withheld").expect("published");
    assert_eq!(withheld.value.as_deref(), Some("withheld"));
    assert_eq!(
        harness
            .node("turn.withheld.withheld")
            .expect("published")
            .text
            .as_deref(),
        Some("This connection does not hand over reasoning."),
        "whoever withheld it says why, in their own words"
    );

    let absent = harness.node("turn.absent").expect("published");
    assert_eq!(absent.value.as_deref(), Some("absent"));
    assert!(
        harness.node("turn.absent.withheld").is_none(),
        "reasoning nobody produced was not withheld by anybody"
    );

    let mut names = vec![
        shut.value.clone(),
        withheld.value.clone(),
        absent.value.clone(),
    ];
    names.dedup();
    assert_eq!(names.len(), 3, "three facts, three published names");
}

#[gpui::test]
fn the_type_keeps_the_two_absences_apart(cx: &mut TestAppContext) {
    // The API takes a `Reasoning`, never an `Option`, so "withheld" cannot be
    // reached by forgetting: a caller holding nothing has to say which of the
    // two nothings it holds. What a test can check is that the two are not
    // equal and do not render the same, and that reasoning which exists but is
    // empty is still reasoning that exists.
    assert_ne!(Reasoning::withheld("policy"), Reasoning::Absent);
    assert_ne!(Reasoning::present(String::new()), Reasoning::Absent);
    assert_eq!(Reasoning::present(String::new()).as_str(), "present");

    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        ThinkingBlock::new("turn.empty", Reasoning::present(String::new()))
            .expanded(true)
            .into_any_element()
    });
    assert_eq!(
        harness
            .node("turn.empty")
            .expect("published")
            .value
            .as_deref(),
        Some("present"),
        "an empty answer is an answer"
    );
}

#[gpui::test]
fn a_block_with_nothing_to_open_installs_no_handler(cx: &mut TestAppContext) {
    let calls: Calls = Rc::new(RefCell::new(Vec::new()));
    let sink = Rc::clone(&calls);
    let mut harness = Harness::new(cx, gpui_kit::install, move |_, _| {
        let withheld = Rc::clone(&sink);
        let absent = Rc::clone(&sink);
        let present = Rc::clone(&sink);
        gpui::div()
            .child(
                ThinkingBlock::new(
                    "turn.withheld",
                    Reasoning::withheld("This connection does not hand over reasoning."),
                )
                .on_toggle(move |next, _, _| {
                    withheld.borrow_mut().push(format!("withheld:{next}"))
                }),
            )
            .child(
                ThinkingBlock::new("turn.absent", Reasoning::Absent).on_toggle(
                    move |next, _, _| absent.borrow_mut().push(format!("absent:{next}")),
                ),
            )
            .child(
                ThinkingBlock::new("turn.present", Reasoning::present("Because.")).on_toggle(
                    move |next, _, _| present.borrow_mut().push(format!("present:{next}")),
                ),
            )
            .into_any_element()
    });

    assert_eq!(
        harness.node("turn.withheld").expect("published").role,
        Role::Text,
        "there is nothing to operate, so it is not a button"
    );
    assert_eq!(
        harness.node("turn.absent").expect("published").role,
        Role::Text
    );
    assert_eq!(
        harness.node("turn.present").expect("published").role,
        Role::Button
    );

    harness.click("turn.withheld");
    harness.click("turn.absent");
    assert!(
        calls.borrow().is_empty(),
        "a block that cannot open installs no handler at all"
    );

    harness.click("turn.present");
    assert_eq!(*calls.borrow(), vec!["present:true".to_string()]);
}

#[gpui::test]
fn reasoning_text_stays_out_of_the_snapshot(cx: &mut TestAppContext) {
    let mut harness = Harness::new(cx, gpui_kit::install, |_, _| {
        ThinkingBlock::new(
            "turn.open",
            Reasoning::present(format!("The token is {SECRET}, so use it.")),
        )
        .expanded(true)
        .into_any_element()
    });

    let snapshot = harness.snapshot();
    assert!(
        !snapshot
            .nodes
            .iter()
            .any(|node| [node.text.as_deref(), node.value.as_deref()]
                .into_iter()
                .flatten()
                .any(|text| text.contains(SECRET))),
        "a model's reasoning is caller-owned content and never published"
    );
}