diff --git a/pkgs/id/crates/id-core/src/world_presentation.rs b/pkgs/id/crates/id-core/src/world_presentation.rs index f053e92c75..2b85750e17 100644 --- a/pkgs/id/crates/id-core/src/world_presentation.rs +++ b/pkgs/id/crates/id-core/src/world_presentation.rs @@ -796,8 +796,23 @@ fn style_sequence(fg: u8, bg: u8, bold: bool) -> String { #[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod tests { use super::*; + use serde::Deserialize; use serde_json::json; + #[derive(Deserialize)] + struct ScreenCellParityCorpus { + cases: Vec, + } + + #[derive(Deserialize)] + struct ScreenCellParityCase { + name: String, + width: u16, + height: u16, + expected_valid: bool, + cells: Vec, + } + fn screen(value: Value) -> Result> { parse_surface(&value.to_string()) } @@ -823,6 +838,43 @@ mod tests { ScreenSurface::from_cells(width, height, vec![cell(fill); width * height]).unwrap() } + #[test] + fn test_rust_screen_presenter_matches_the_shared_web_parity_corpus() { + let corpus: ScreenCellParityCorpus = serde_json::from_str(include_str!( + "../../../web/src/test-fixtures/screen-cell-parity.json" + )) + .unwrap(); + + let case_names: Vec<_> = corpus.cases.iter().map(|case| case.name.as_str()).collect(); + for required_case in [ + "CJK wide character", + "astral-plane emoji", + "ZWJ emoji sequence", + "warning sign with emoji variation selector", + "non-empty continuation cell", + "wide character declared with the wrong width", + ] { + assert!( + case_names.contains(&required_case), + "missing {required_case:?}" + ); + } + for case in corpus.cases { + let surface = ScreenSurface { + version: 1, + width: case.width, + height: case.height, + cells: case.cells, + }; + assert_eq!( + surface.render_ansi().is_some(), + case.expected_valid, + "Rust screen presenter verdict for {:?}", + case.name + ); + } + } + #[test] fn patch_parity_patched_client_equals_full_frame_client() { let prev = grid(4, 2, " "); diff --git a/pkgs/id/web/src/test-fixtures/screen-cell-parity.json b/pkgs/id/web/src/test-fixtures/screen-cell-parity.json new file mode 100644 index 0000000000..be70b7a45d --- /dev/null +++ b/pkgs/id/web/src/test-fixtures/screen-cell-parity.json @@ -0,0 +1,64 @@ +{ + "cases": [ + { + "name": "CJK wide character", + "width": 2, + "height": 1, + "expected_valid": true, + "cells": [ + { "symbol": "界", "fg": 0, "bg": 0, "bold": false, "columns": 2 }, + { "symbol": "", "fg": 0, "bg": 0, "bold": false, "columns": 0 } + ] + }, + { + "name": "astral-plane emoji", + "width": 2, + "height": 1, + "expected_valid": true, + "cells": [ + { "symbol": "😀", "fg": 0, "bg": 0, "bold": false, "columns": 2 }, + { "symbol": "", "fg": 0, "bg": 0, "bold": false, "columns": 0 } + ] + }, + { + "name": "ZWJ emoji sequence", + "width": 2, + "height": 1, + "expected_valid": true, + "cells": [ + { "symbol": "👩‍💻", "fg": 0, "bg": 0, "bold": false, "columns": 2 }, + { "symbol": "", "fg": 0, "bg": 0, "bold": false, "columns": 0 } + ] + }, + { + "name": "warning sign with emoji variation selector", + "width": 2, + "height": 1, + "expected_valid": true, + "cells": [ + { "symbol": "⚠️", "fg": 0, "bg": 0, "bold": false, "columns": 2 }, + { "symbol": "", "fg": 0, "bg": 0, "bold": false, "columns": 0 } + ] + }, + { + "name": "non-empty continuation cell", + "width": 2, + "height": 1, + "expected_valid": false, + "cells": [ + { "symbol": "界", "fg": 0, "bg": 0, "bold": false, "columns": 2 }, + { "symbol": "x", "fg": 0, "bg": 0, "bold": false, "columns": 0 } + ] + }, + { + "name": "wide character declared with the wrong width", + "width": 2, + "height": 1, + "expected_valid": false, + "cells": [ + { "symbol": "界", "fg": 0, "bg": 0, "bold": false, "columns": 1 }, + { "symbol": "x", "fg": 0, "bg": 0, "bold": false, "columns": 1 } + ] + } + ] +} diff --git a/pkgs/id/web/src/world-surface.test.ts b/pkgs/id/web/src/world-surface.test.ts index a2b0a44991..3cace264e9 100644 --- a/pkgs/id/web/src/world-surface.test.ts +++ b/pkgs/id/web/src/world-surface.test.ts @@ -1,11 +1,28 @@ import { describe, expect, it } from "vitest"; import { applyScreenPatch, renderWorldSurface, type ScreenSurface } from "./world-surface"; +import screenCellParityCorpus from "./test-fixtures/screen-cell-parity.json"; function makeScreen(cells: ScreenSurface["cells"]): ScreenSurface { return { kind: "screen", version: 1, width: cells.length, height: 1, cells }; } describe("world screen presenter", () => { + it("matches the recorded Rust verdicts for the shared screen-cell corpus", () => { + for (const testCase of screenCellParityCorpus.cases) { + const surface: ScreenSurface = { + kind: "screen", + version: 1, + width: testCase.width, + height: testCase.height, + cells: testCase.cells, + }; + expect( + renderWorldSurface(document.createElement("div"), surface), + `web screen presenter verdict for ${testCase.name}`, + ).toBe(testCase.expected_valid); + } + }); + it("applies revision-independent cell runs to the same validated surface as a full frame", () => { const previous: ScreenSurface = { kind: "screen",