c7abc8c718
- A connection's chat keeps the server's component: SystemChat goes through the same translation and styles as the action bar, and player and disguised chat are decorated from their chat type's translation, parameters and style (ChatType.Bound.decorate), so colours, bold, hex colours, and the client's language are kept (#1372, #1389). - Components parse `click_event` (suggest_command, copy_to_clipboard; never open_file from a server) and `hover_event` show_text (#1377), and the chat screen draws the hovered part's tooltip (#1381). - `§` codes in chat colour the text with Chat Colors on and are stripped with it off (ComponentRenderUtils.wrapComponents) (#1379). - A command's operator broadcast skips the player whose command it was on the integrated server too (#1373), and /give fires InventoryChangeTrigger before its message, whose announcement is sent first (#1375). - The control socket's `messages` gives the chat lines as components; lan_e2e `styles` checks a styled tellraw, the advancement order and the missing echo for the host and the guest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
330 lines
12 KiB
Rust
330 lines
12 KiB
Rust
use super::*;
|
|
use crate::{font::Font, hud::Rect};
|
|
use std::{collections::HashMap, path::Path};
|
|
|
|
fn font() -> Font {
|
|
// The missing-glyph metrics let these tests run without game assets.
|
|
Font::load(
|
|
Path::new("/nonexistent-horizon-chat-test-resources"),
|
|
&HashMap::new(),
|
|
&[],
|
|
(1, 1),
|
|
(0, 0),
|
|
Rect {
|
|
u0: 0.0,
|
|
v0: 0.0,
|
|
u1: 1.0,
|
|
v1: 1.0,
|
|
w: 1,
|
|
h: 1,
|
|
},
|
|
)
|
|
}
|
|
|
|
fn add(chat: &mut Chat, font: &Font, content: Text) {
|
|
for content in font.wrap_components(&content, chat.max_width()) {
|
|
chat.lines.insert(
|
|
0,
|
|
Line {
|
|
added: 0,
|
|
content,
|
|
indicator: None,
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn clicking_seed_copies_the_raw_value_only_inside_the_link() {
|
|
let font = font();
|
|
for seed in [1, -42, i64::MIN, i64::MAX] {
|
|
let mut chat = Chat::default();
|
|
let seed_text = Text::copyable(seed.to_string()).with_color(text::GOLD);
|
|
assert_eq!(seed_text.plain(), format!("[{seed}]"));
|
|
assert_eq!(seed_text.spans[0].style.color, Some(text::GOLD));
|
|
assert_eq!(seed_text.spans[1].style.color, Some(text::GREEN));
|
|
assert_eq!(seed_text.spans[2].style.color, Some(text::GOLD));
|
|
add(&mut chat, &font, Text::literal("Seed: ").append(seed_text));
|
|
let expected = Some(ClickEvent::CopyToClipboard(seed.to_string()));
|
|
let start = 4.0 + f64::from(font.width("Seed: ["));
|
|
let end = start + f64::from(font.width(&seed.to_string()));
|
|
assert_eq!(chat.click_at(&font, 360, start, 315.0), expected);
|
|
assert_eq!(chat.click_at(&font, 360, end - 0.5, 315.0), expected);
|
|
for (x, y) in [
|
|
(start - 0.5, 315.0),
|
|
(end, 315.0),
|
|
(3.0, 315.0),
|
|
(start, 320.0),
|
|
(start, 310.0),
|
|
] {
|
|
assert_eq!(chat.click_at(&font, 360, x, y), None, "{x}, {y}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn click_targets_match_rendered_spans_when_scaled_wrapped_and_scrolled() {
|
|
let font = font();
|
|
let screenshot = ClickEvent::OpenFile("/tmp/My screenshots/世界.png".into());
|
|
for scale in [0.5, 0.75, 1.0] {
|
|
for line_spacing in [0.0, 0.5, 1.0] {
|
|
let mut chat = Chat::default();
|
|
chat.options.scale = scale;
|
|
chat.options.width = 0.0;
|
|
chat.options.line_spacing = line_spacing;
|
|
for _ in 0..20 {
|
|
add(
|
|
&mut chat,
|
|
&font,
|
|
Text::literal("Saved screenshot as ").append(Text::styled(
|
|
"2026-10-14_18.55.14.png",
|
|
Style {
|
|
click: Some(screenshot.clone()),
|
|
bold: Some(true),
|
|
underlined: Some(true),
|
|
..Style::default()
|
|
},
|
|
)),
|
|
);
|
|
}
|
|
for scroll in [0, 3, 12] {
|
|
chat.scroll = scroll;
|
|
let mut links = 0;
|
|
for draw in chat.layout(361, 0, true) {
|
|
if let GuiDraw::Text { text, x, y, .. } = draw {
|
|
let mut at = x as f64;
|
|
for (c, style) in text.chars() {
|
|
let width = f64::from(font.advance(c, style));
|
|
let cursor_x = (4.0 + at + width / 2.0) * scale;
|
|
let cursor_y = (y as f64 + 3.0) * scale;
|
|
assert_eq!(
|
|
chat.click_at(&font, 361, cursor_x, cursor_y),
|
|
style.click,
|
|
"scale={scale}, spacing={line_spacing}, scroll={scroll}, char={c}"
|
|
);
|
|
links += usize::from(style.click.is_some());
|
|
at += width;
|
|
}
|
|
}
|
|
}
|
|
assert!(links > 0);
|
|
// The history above the focused viewport is not clickable.
|
|
let top = ((361 - 40) as f64 / scale).floor()
|
|
- chat.lines_per_page(true) as f64 * chat.options.line_height() as f64;
|
|
assert_eq!(
|
|
chat.click_at(&font, 361, 10.0 * scale, (top - 1.0) * scale),
|
|
None
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn locate_coordinates_fill_chat_with_teleport_without_submitting() {
|
|
let font = font();
|
|
let m = Measure(&font);
|
|
let registries = Registries::default();
|
|
let commands = Commands {
|
|
dispatcher: crate::command::vanilla::game_dispatcher(),
|
|
registries: ®istries,
|
|
};
|
|
// Structures and POIs use relative Y; biome results include absolute Y.
|
|
for (y, expected) in [
|
|
(None, "/tp @s -1136 ~ 848"),
|
|
(Some(73), "/tp @s -1136 73 848"),
|
|
] {
|
|
let link = Text::teleport_link(-1136, y, 848);
|
|
assert_eq!(link.component()["click_event"]["action"], "suggest_command");
|
|
assert_eq!(link.component()["click_event"]["command"], expected);
|
|
let mut chat = Chat::default();
|
|
add(&mut chat, &font, Text::literal("Found at ").append(link));
|
|
let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360);
|
|
let x = 4.0 + f64::from(font.width("Found at "));
|
|
assert!(!screen.click(&m, &commands, x, 315.0));
|
|
let Some(ClickEvent::SuggestCommand(command)) = chat.click_at(&font, 360, x, 315.0) else {
|
|
panic!("locate coordinates must suggest a command");
|
|
};
|
|
screen.suggest_command(&m, &commands, &command);
|
|
assert_eq!(screen.value(), expected);
|
|
assert_eq!(screen.input.cursor, expected.chars().count());
|
|
assert_eq!(screen.input.highlight, screen.input.cursor);
|
|
// Submission is a separate, explicit Enter press.
|
|
assert_eq!(
|
|
screen.key(
|
|
&m,
|
|
&commands,
|
|
&[],
|
|
winit::keyboard::KeyCode::Enter,
|
|
false,
|
|
false
|
|
),
|
|
ChatAction::Submit(expected.to_owned())
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn history_arrows_recall_messages_and_restore_the_draft() {
|
|
use winit::keyboard::KeyCode::{ArrowDown, ArrowUp};
|
|
|
|
let font = font();
|
|
let m = Measure(&font);
|
|
let registries = Registries::default();
|
|
let commands = Commands {
|
|
dispatcher: crate::command::vanilla::game_dispatcher(),
|
|
registries: ®istries,
|
|
};
|
|
let history = vec!["first message".to_owned(), "latest message".to_owned()];
|
|
let mut screen = ChatScreen::new(&m, &commands, "unfinished draft", history.len(), 640, 360);
|
|
for (key, expected) in [
|
|
(ArrowUp, "latest message"),
|
|
(ArrowUp, "first message"),
|
|
(ArrowUp, "first message"),
|
|
(ArrowDown, "latest message"),
|
|
(ArrowDown, "unfinished draft"),
|
|
(ArrowDown, "unfinished draft"),
|
|
] {
|
|
assert_eq!(
|
|
screen.key(&m, &commands, &history, key, false, false),
|
|
ChatAction::None
|
|
);
|
|
assert_eq!(screen.value(), expected);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn mod_commands_are_coloured_and_complete_like_vanilla_ones() {
|
|
let font = font();
|
|
let m = Measure(&font);
|
|
// Allow Commands off: a mod's command needs no level.
|
|
let registries = Registries::default();
|
|
let vanilla = crate::command::vanilla::game_dispatcher();
|
|
let modded = vanilla.with_mod_commands(["where", "coords"]);
|
|
let red = |screen: &ChatScreen| {
|
|
screen
|
|
.format(screen.value(), 0)
|
|
.spans
|
|
.iter()
|
|
.any(|s| s.style.color == Some(text::RED))
|
|
};
|
|
let typed = |dispatcher: &crate::command::Dispatcher, line: &str| {
|
|
let c = Commands {
|
|
dispatcher,
|
|
registries: ®istries,
|
|
};
|
|
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360);
|
|
screen.type_text(&m, &c, line);
|
|
screen
|
|
};
|
|
// Unknown without the mod's tree (#456).
|
|
assert!(red(&typed(vanilla, "/coords")));
|
|
for line in ["/coords", "/where", "/where me and you"] {
|
|
assert!(!red(&typed(&modded, line)), "{line}");
|
|
}
|
|
let c = Commands {
|
|
dispatcher: &modded,
|
|
registries: ®istries,
|
|
};
|
|
let mut screen = typed(&modded, "/coo");
|
|
screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false);
|
|
assert_eq!(screen.value(), "/coords");
|
|
}
|
|
|
|
#[test]
|
|
fn ask_server_nodes_wait_for_the_servers_completions() {
|
|
// ClientSuggestionProvider.customSuggestion: an `ask_server` node at the
|
|
// cursor sends the command so far, and the list is done with the reply.
|
|
let font = font();
|
|
let m = Measure(&font);
|
|
let registries = Registries {
|
|
permission: 4,
|
|
..Registries::default()
|
|
};
|
|
let mut d = crate::command::vanilla::game_dispatcher().clone();
|
|
let child = |d: &crate::command::Dispatcher, n: usize, name: &str| {
|
|
d.nodes[n]
|
|
.children
|
|
.iter()
|
|
.copied()
|
|
.find(|&c| d.nodes[c].name() == name)
|
|
.expect(name)
|
|
};
|
|
let id = child(&d, child(&d, child(&d, 0, "bossbar"), "get"), "id");
|
|
d.nodes[id].suggests = Some(crate::command::Suggests::AskServer);
|
|
let c = Commands {
|
|
dispatcher: &d,
|
|
registries: ®istries,
|
|
};
|
|
let tab = |screen: &mut ChatScreen| {
|
|
screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false);
|
|
};
|
|
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360);
|
|
screen.type_text(&m, &c, "/bossbar get pt");
|
|
let (request, sent) = screen.take_request().expect("the server is asked");
|
|
assert_eq!(sent, "/bossbar get pt");
|
|
assert_eq!(screen.take_request(), None);
|
|
// Nothing to complete until the reply.
|
|
tab(&mut screen);
|
|
assert_eq!(screen.value(), "/bossbar get pt");
|
|
// A reply to an older request is dropped; the range is in UTF-16 units.
|
|
let reply = |text: &str| vec![(text.to_owned(), None)];
|
|
screen.server_suggestions(&m, &c, request - 1, (13, 2), reply("pt6:old"));
|
|
tab(&mut screen);
|
|
assert_eq!(screen.value(), "/bossbar get pt");
|
|
screen.server_suggestions(&m, &c, request, (13, 2), reply("pt6:check"));
|
|
tab(&mut screen);
|
|
assert_eq!(screen.value(), "/bossbar get pt6:check");
|
|
// Without the server's provider the local list is done at once.
|
|
let local = crate::command::vanilla::game_dispatcher();
|
|
let c = Commands {
|
|
dispatcher: local,
|
|
registries: ®istries,
|
|
};
|
|
let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360);
|
|
screen.type_text(&m, &c, "/bossbar get pt");
|
|
assert_eq!(screen.take_request(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_codes_colour_the_line_or_vanish_without_chat_colours() {
|
|
let mut chat = Chat::default();
|
|
let red = Text::literal("\u{a7}cRED");
|
|
let shown = chat.displayed(&red);
|
|
assert_eq!(shown.plain(), "RED");
|
|
assert!(shown.chars().all(|(_, s)| s.color == Some(0xFF5555)));
|
|
chat.options.colors = false;
|
|
let shown = chat.displayed(&red);
|
|
assert_eq!(shown.plain(), "RED");
|
|
assert!(shown.chars().all(|(_, s)| s.color.is_none()));
|
|
// A lone section sign or an unknown code stays as written.
|
|
assert_eq!(
|
|
chat.displayed(&Text::literal("a\u{a7}zb")).plain(),
|
|
"a\u{a7}zb"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_server_component_keeps_its_click_and_hover_events() {
|
|
let font = font();
|
|
let json = serde_json::json!({
|
|
"text": "CLICK", "color": "green", "bold": true,
|
|
"click_event": {"action": "suggest_command", "command": "/say clicked"},
|
|
"hover_event": {"action": "show_text", "value": {"text": "Tip", "color": "aqua"}},
|
|
});
|
|
let line = crate::component::to_text(&json, &mut |s| Text::literal(s));
|
|
let mut chat = Chat::default();
|
|
add(&mut chat, &font, line);
|
|
let x = 4.0 + f64::from(font.width("CL"));
|
|
let Some(ClickEvent::SuggestCommand(command)) = chat.click_at(&font, 360, x, 315.0) else {
|
|
panic!("the line must suggest its command");
|
|
};
|
|
assert_eq!(command, "/say clicked");
|
|
let hover = chat.hover_at(&font, 360, x, 315.0).expect("a hover text");
|
|
assert_eq!(hover.plain(), "Tip");
|
|
assert_eq!(hover.spans[0].style.color, Some(text::AQUA));
|
|
// Past the (bold, so wider) text, nothing.
|
|
let past = 4.0 + f64::from(font.width("CLICK")) + 20.0;
|
|
assert!(chat.hover_at(&font, 360, past, 315.0).is_none());
|
|
}
|