Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pkgs/id/crates/id-core/src/world_presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScreenCellParityCase>,
}

#[derive(Deserialize)]
struct ScreenCellParityCase {
name: String,
width: u16,
height: u16,
expected_valid: bool,
cells: Vec<ScreenCell>,
}

fn screen(value: Value) -> Result<Option<WorldSurface>> {
parse_surface(&value.to_string())
}
Expand All @@ -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, " ");
Expand Down
64 changes: 64 additions & 0 deletions pkgs/id/web/src/test-fixtures/screen-cell-parity.json
Original file line number Diff line number Diff line change
@@ -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 }
]
}
]
}
17 changes: 17 additions & 0 deletions pkgs/id/web/src/world-surface.test.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Loading