返回 CodeWhale
tests.rs
根目录 / crates / tui / src / tui / ambient_life / tests.rs
1 use super::*;
2 use ratatui::text::Span;
3
4 fn dot_cameo(area: Rect, age: u128, activity: AmbientActivity) -> (Buffer, AmbientFrameStats) {
5 let mut buf = Buffer::empty(area);
6 let mut stats = AmbientFrameStats::default();
7 pet_cameo::paint(
8 area,
9 &mut buf,
10 Color::Cyan,
11 &[],
12 1.0,
13 WhaleCameo {
14 elapsed_ms: Some(age),
15 anchor_x: area.x + area.width / 2,
16 anchor_y: area.y + area.height / 2,
17 },
18 activity,
19 &mut stats,
20 );
21 (buf, stats)
22 }
23
24 #[test]
25 fn dot_cameo_is_bounded_and_independent_of_draw_history() {
26 let area = Rect::new(7, 11, 80, 24);
27 let (first, stats) = dot_cameo(area, 500, AmbientActivity::Baseline);
28 assert!(stats.marks_painted > 0);
29 dot_cameo(area, 1_900, AmbientActivity::Subagents);
30 dot_cameo(Rect::new(0, 0, 140, 40), 100, AmbientActivity::Baseline);
31 assert_eq!(first, dot_cameo(area, 500, AmbientActivity::Baseline).0);
32 assert_ne!(first, dot_cameo(area, 900, AmbientActivity::Baseline).0);
33 assert_eq!(
34 dot_cameo(area, 2_400, AmbientActivity::Baseline).1,
35 AmbientFrameStats::default()
36 );
37 assert_eq!(
38 dot_cameo(area, u128::MAX, AmbientActivity::Subagents).1,
39 AmbientFrameStats::default()
40 );
41 }
42
43 #[test]
44 fn dot_cameo_withholds_whole_body_when_label_touches_wide_text() {
45 let area = Rect::new(7, 11, 80, 24);
46 let (clear, clear_stats) = dot_cameo(area, 500, AmbientActivity::Baseline);
47 let label_cell = clear
48 .content
49 .iter()
50 .position(|cell| cell.symbol() == "o")
51 .unwrap();
52 let (x, y) = (
53 label_cell % area.width as usize,
54 label_cell / area.width as usize,
55 );
56 let mut lines = vec![Line::from(""); area.height as usize];
57 lines[y] = Line::from(format!("{}鲸", " ".repeat(x)));
58 let mut buf = Buffer::empty(area);
59 buf.set_line(area.x, area.y + y as u16, &lines[y], area.width);
60 let before = buf.clone();
61 let mut stats = AmbientFrameStats::default();
62 pet_cameo::paint(
63 area,
64 &mut buf,
65 Color::Cyan,
66 &lines,
67 1.0,
68 WhaleCameo {
69 elapsed_ms: Some(500),
70 anchor_x: area.x + 40,
71 anchor_y: area.y + 12,
72 },
73 AmbientActivity::Baseline,
74 &mut stats,
75 );
76 assert_eq!(
77 before, buf,
78 "a label collision must withhold every particle too"
79 );
80 assert_eq!(stats.marks_skipped_text, clear_stats.marks_built);
81 assert_eq!(stats.marks_painted, 0);
82 }
83
84 #[test]
85 fn narrow_pod_members_are_withheld_instead_of_stacked() {
86 let area = Rect::new(0, 0, 40, 12);
87 let (buf, stats) = dot_cameo(area, 500, AmbientActivity::Subagents);
88 let text: String = buf.content.iter().map(|cell| cell.symbol()).collect();
89 assert_eq!(text.matches("agent · pod").count(), 1);
90 assert!(stats.marks_clipped > 0);
91 assert_eq!(stats.marks_built, stats.marks_painted + stats.marks_clipped);
92 }
93
94 #[test]
95 fn pet_settle_clock_does_not_extend_the_ocean_light_pulse() {
96 let area = Rect::new(0, 0, 80, 24);
97 let ramp = ocean::OceanRamp::for_theme(&codewhale_palette::UNDERWATER_UI_THEME).unwrap();
98 let column = |age| {
99 OceanColumn::new(
100 ramp,
101 area,
102 900,
103 age,
104 crate::tui::underwater::ShellPhase::Done,
105 false,
106 500,
107 40,
108 )
109 };
110 let settled = column(None);
111 let pet_tail = column(Some(900));
112 assert_eq!(pet_tail.completion_elapsed_ms(), Some(900));
113 assert_eq!(pet_tail.ramp_fingerprint(), settled.ramp_fingerprint());
114 for y in 0..area.height {
115 assert_eq!(pet_tail.color_at_y(y), settled.color_at_y(y));
116 }
117 }
118
119 #[test]
120 fn pet_widget_keeps_unknown_distinct_from_sleep_and_keeps_its_label() {
121 use pet_sim::{ChannelId, PetSim, PetState};
122 use ratatui::widgets::Widget;
123 let mut sim = PetSim::whale();
124 let mut state = PetState {
125 channel: ChannelId::Other,
126 lit: 0.3,
127 ..PetState::rest()
128 };
129 sim.step(1.0 / 30.0, &state, false, 1.0);
130 assert!(!sim.frame.hollow, "sleep is still observed");
131 state.observed = 0.15;
132 sim.step(1.0 / 30.0, &state, false, 1.0);
133 assert!(sim.frame.hollow);
134 let mut buf = Buffer::empty(Rect::new(0, 0, 40, 8));
135 pet_widget::PetWidget {
136 sim: &sim,
137 state: &state,
138 }
139 .render(buf.area, &mut buf);
140 let text: String = buf.content.iter().map(|cell| cell.symbol()).collect();
141 assert!(text.contains("other · drift · resting"));
142 let mut narrow = Buffer::empty(Rect::new(0, 0, 18, 6));
143 let before = narrow.clone();
144 pet_widget::PetWidget {
145 sim: &sim,
146 state: &state,
147 }
148 .render(narrow.area, &mut narrow);
149 assert_eq!(narrow, before, "never truncate away the uncertainty cue");
150 }
151
152 #[test]
153 fn ambient_min_dimensions_allow_small_windows() {
154 const {
155 assert!(AMBIENT_MIN_WIDTH < 68);
156 assert!(AMBIENT_MIN_HEIGHT < 15);
157 }
158 }
159
160 #[test]
161 fn occupied_text_bounds_skips_string_join() {
162 let line = Line::from(vec![Span::raw(" hello "), Span::raw("world ")]);
163 let (start, end) = occupied_text_bounds(&line).expect("bounds");
164 assert_eq!(start, 2);
165 assert!(end > start);
166 }
167
168 fn habitat_theme() -> codewhale_ratatui::Theme {
169 let caps = crate::tui::color_compat::ColorCompatBackend::new(
170 std::io::sink(),
171 codewhale_palette::ColorDepth::TrueColor,
172 codewhale_palette::PaletteMode::Dark,
173 )
174 .native_ocean_caps();
175 codewhale_ratatui::Theme::new(caps)
176 .ground(codewhale_ratatui::Ground::Ocean)
177 .tui_palette(codewhale_ratatui::TuiPalette::Underwater)
178 }
179
180 fn ocean_frame(
181 area: Rect,
182 elapsed_ms: u128,
183 presence: f32,
184 whale: WhaleCameo,
185 activity: AmbientActivity,
186 ) -> (Buffer, AmbientFrameStats) {
187 let mut buf = Buffer::empty(area);
188 buf.set_style(
189 area,
190 ratatui::style::Style::default().bg(Color::Rgb(9, 34, 65)),
191 );
192 let stats = render_ambient_life(
193 area,
194 &mut buf,
195 &habitat_theme(),
196 Color::Rgb(106, 174, 242),
197 &[],
198 elapsed_ms,
199 presence,
200 whale,
201 activity,
202 &[],
203 );
204 (buf, stats)
205 }
206
207 #[test]
208 fn reduced_motion_builds_and_paints_no_ambient_creatures() {
209 let area = Rect::new(0, 0, 80, 24);
210 let whale = WhaleCameo {
211 elapsed_ms: Some(500),
212 anchor_x: 40,
213 anchor_y: 16,
214 };
215 let (still, stats) = ocean_frame(area, 900, 0.0, whale, AmbientActivity::Subagents);
216 let mut water = Buffer::empty(area);
217 water.set_style(
218 area,
219 ratatui::style::Style::default().bg(Color::Rgb(9, 34, 65)),
220 );
221 assert_eq!(still, water);
222 assert_eq!(stats, AmbientFrameStats::default());
223 let (alive, _) = ocean_frame(
224 area,
225 900,
226 1.0,
227 WhaleCameo::default(),
228 AmbientActivity::Baseline,
229 );
230 assert_ne!(alive, water, "full presence paints the kit habitat");
231 }
232
233 #[test]
234 fn subagent_activity_surfaces_a_whale_pod() {
235 let whale = WhaleCameo {
236 elapsed_ms: Some(500),
237 anchor_x: 50,
238 anchor_y: 20,
239 };
240 let area = Rect::new(0, 0, 100, 30);
241 let baseline = ocean_frame(area, 12_000, 1.0, whale, AmbientActivity::Baseline).1;
242 let pod = ocean_frame(area, 12_000, 1.0, whale, AmbientActivity::Subagents).1;
243 assert!(
244 pod.marks_painted > baseline.marks_painted,
245 "a subagent pod must surface more whale marks ({}) than the single cameo ({})",
246 pod.marks_painted,
247 baseline.marks_painted,
248 );
249 }
250
250 lines RUST