Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T04:28:36.474Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

example_tests.rs

132 lines · 4.4 KB · rust
1//! Tests for the input composition. Require the `test-support` feature:
2//!
3//! ```sh
4//! cargo test -p gpui --example view_example --features test-support
5//! ```
6
7#[cfg(test)]
8mod tests {
9    use gpui::{Context, Entity, KeyBinding, TestAppContext, Window, prelude::*};
10
11    use crate::example_editor::Editor;
12    use crate::example_input::Input;
13    use crate::{Backspace, Delete, End, Home, Left, Right};
14
15    /// Two inputs, each backed by an editor we own (so the test can focus and
16    /// read them). Proves data flows through the shared `String` and that
17    /// sibling inputs stay isolated.
18    struct Harness {
19        a: Entity<Editor>,
20        b: Entity<Editor>,
21    }
22
23    impl Render for Harness {
24        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
25            gpui::div()
26                .child(Input::editor(self.a.clone()))
27                .child(Input::editor(self.b.clone()))
28        }
29    }
30
31    fn bind_keys(cx: &mut TestAppContext) {
32        cx.update(|cx| {
33            cx.bind_keys([
34                KeyBinding::new("backspace", Backspace, None),
35                KeyBinding::new("delete", Delete, None),
36                KeyBinding::new("left", Left, None),
37                KeyBinding::new("right", Right, None),
38                KeyBinding::new("home", Home, None),
39                KeyBinding::new("end", End, None),
40            ]);
41        });
42    }
43
44    fn setup(
45        cx: &mut TestAppContext,
46    ) -> (
47        Entity<Editor>,
48        Entity<String>,
49        Entity<String>,
50        &mut gpui::VisualTestContext,
51    ) {
52        bind_keys(cx);
53
54        let (harness, cx) = cx.add_window_view(|window, cx| {
55            let a_value = cx.new(|_| String::new());
56            let b_value = cx.new(|_| String::new());
57            let a = cx.new(|cx| Editor::over(a_value, window, cx));
58            let b = cx.new(|cx| Editor::over(b_value, window, cx));
59            Harness { a, b }
60        });
61
62        let a = cx.read_entity(&harness, |h, _| h.a.clone());
63        let b = cx.read_entity(&harness, |h, _| h.b.clone());
64        let a_value = cx.read_entity(&a, |e, _| e.value.clone());
65        let b_value = cx.read_entity(&b, |e, _| e.value.clone());
66
67        // Focus the first input's editor.
68        cx.update(|window, cx| {
69            let focus_handle = a.read(cx).focus_handle.clone();
70            window.focus(&focus_handle, cx);
71        });
72
73        (a, a_value, b_value, cx)
74    }
75
76    #[gpui::test]
77    fn typing_updates_the_shared_string(cx: &mut TestAppContext) {
78        let (editor, a_value, _b_value, cx) = setup(cx);
79
80        cx.simulate_input("hello");
81
82        cx.read_entity(&a_value, |value, _| assert_eq!(value, "hello"));
83        cx.read_entity(&editor, |editor, _| assert_eq!(editor.cursor, 5));
84    }
85
86    #[gpui::test]
87    fn sibling_inputs_are_isolated(cx: &mut TestAppContext) {
88        let (_editor, a_value, b_value, cx) = setup(cx);
89
90        cx.simulate_input("x");
91
92        cx.read_entity(&a_value, |value, _| assert_eq!(value, "x"));
93        cx.read_entity(&b_value, |value, _| {
94            assert_eq!(value, "", "typing in input A must not touch input B")
95        });
96    }
97
98    #[gpui::test]
99    fn external_writes_clamp_the_cursor(cx: &mut TestAppContext) {
100        let (editor, a_value, _b_value, cx) = setup(cx);
101
102        cx.simulate_input("hello");
103        cx.read_entity(&editor, |editor, _| assert_eq!(editor.cursor, 5));
104
105        // Write the shared value from outside the editor. The old cursor (5)
106        // now points into the middle of a multi-byte character; the editor's
107        // observation must clamp it back onto a boundary.
108        cx.update(|_, cx| {
109            a_value.update(cx, |value, cx| {
110                *value = "日本".to_string();
111                cx.notify();
112            })
113        });
114
115        cx.read_entity(&a_value, |value, _| assert_eq!(value, "日本"));
116        cx.read_entity(&editor, |editor, _| {
117            assert_eq!(editor.cursor, 3, "cursor must clamp to a char boundary");
118        });
119    }
120
121    #[gpui::test]
122    fn arrows_move_the_cursor(cx: &mut TestAppContext) {
123        let (editor, _a_value, _b_value, cx) = setup(cx);
124
125        cx.simulate_input("abc");
126        cx.read_entity(&editor, |editor, _| assert_eq!(editor.cursor, 3));
127
128        cx.simulate_keystrokes("left left");
129        cx.read_entity(&editor, |editor, _| assert_eq!(editor.cursor, 1));
130    }
131}
132
Served at tenant.openagents/omega Member data and write actions are omitted.