diff --git a/pkgs/id/crates/id-core/tests/screen_parity.rs b/pkgs/id/crates/id-core/tests/screen_parity.rs new file mode 100644 index 0000000000..46e2114b66 --- /dev/null +++ b/pkgs/id/crates/id-core/tests/screen_parity.rs @@ -0,0 +1,184 @@ +//! Web/terminal parity harness for #796 item 9-2 (issue #8340). +//! +//! The checked-in corpus at `pkgs/id/test-data/screen-parity-corpus.json` +//! is the oracle: its verdicts were generated from the real Rust +//! presenter (`ScreenSurface::text_projection` plus the per-cell +//! fg/bg/bold the terminal frame renders), never hand-written. This +//! test asserts the Rust presenter still produces exactly the corpus +//! verdicts; the vitest twin (`web/src/world-surface.parity.test.ts`) +//! asserts the web renderer produces the same verdicts from the same +//! corpus, so the two presenters cannot drift apart silently. +//! +//! Regenerate the corpus after an intentional presenter change with: +//! `SCREEN_PARITY_GENERATE=1 cargo test -p id-core --test screen_parity` +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use std::path::PathBuf; + +use id_core::world_presentation::{ScreenCell, ScreenSurface, StyleToken}; +use serde_json::{json, Value}; + +fn cell(symbol: &str, fg: u8, bg: u8, bold: bool, columns: u8) -> ScreenCell { + ScreenCell { + symbol: symbol.to_owned(), + fg, + bg, + bold, + columns, + token: None, + } +} + +fn token_cell(symbol: &str, fg: u8, bg: u8, bold: bool, token: StyleToken) -> ScreenCell { + ScreenCell { + symbol: symbol.to_owned(), + fg, + bg, + bold, + columns: 1, + token: Some(token), + } +} + +fn blank() -> ScreenCell { + cell(" ", 0, 0, false, 1) +} + +fn blanks(n: usize) -> Vec { + (0..n).map(|_| blank()).collect() +} + +/// The corpus cases: (name, width, height, input cells). Inputs only — +/// every verdict in the corpus file is presenter-generated. +fn cases() -> Vec<(String, usize, usize, Vec)> { + let mut plain = vec![cell("H", 0, 0, false, 1), cell("i", 0, 0, false, 1)]; + plain.extend(blanks(6)); + plain.extend(blanks(2)); + plain.extend([cell("o", 0, 0, false, 1), cell("k", 0, 0, false, 1)]); + plain.extend(blanks(4)); + + let styled = vec![ + cell("R", 196, 0, true, 1), + cell("G", 46, 0, false, 1), + cell("B", 0, 21, false, 1), + token_cell("W", 11, 0, true, StyleToken::Warning), + token_cell("C", 14, 0, false, StyleToken::Code), + blank(), + ]; + + let mut wide = vec![ + cell("a", 0, 0, false, 1), + cell("\u{754c}", 0, 0, false, 2), + cell("", 0, 0, false, 0), + cell("b", 0, 0, false, 1), + ]; + wide.extend(blanks(2)); + + let emoji = vec![ + cell("\u{1f600}", 0, 0, false, 2), + cell("", 0, 0, false, 0), + cell("\u{2708}\u{fe0f}", 0, 0, false, 2), + cell("", 0, 0, false, 0), + cell("z", 0, 0, false, 1), + ]; + + let mut mixed = vec![ + cell("\u{754c}", 9, 0, false, 2), + cell("", 9, 0, false, 0), + cell("x", 0, 0, false, 1), + cell("!", 10, 0, true, 1), + ]; + mixed.extend(blanks(6)); + mixed.extend([ + token_cell("d", 10, 0, false, StyleToken::Success), + cell("o", 0, 0, false, 1), + cell("n", 0, 0, false, 1), + cell("e", 0, 0, false, 1), + ]); + mixed.extend(blanks(6)); + + vec![ + ("plain_text".to_owned(), 8, 2, plain), + ("styled_row".to_owned(), 6, 1, styled), + ("wide_glyph".to_owned(), 6, 1, wide), + ("emoji_sequences".to_owned(), 5, 1, emoji), + ("blank_screen".to_owned(), 4, 2, blanks(8)), + ("mixed_rows".to_owned(), 10, 2, mixed), + ] +} + +/// The presenter's verdict for one surface: the text projection plus +/// every presented (non-continuation) cell's symbol and effective +/// style, in row-major order — exactly what the web test extracts +/// from the rendered DOM. +fn verdict(surface: &ScreenSurface) -> Value { + let width = usize::from(surface.width); + let mut cells = Vec::new(); + for (index, c) in surface.cells.iter().enumerate() { + if c.columns == 0 { + continue; + } + cells.push(json!({ + "symbol": c.symbol, + "fg": c.fg, + "bg": c.bg, + "bold": c.bold, + "columns": c.columns, + "row": index / width, + "col": index % width, + })); + } + json!({ "text": surface.text_projection(), "cells": cells }) +} + +fn corpus_path() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../test-data/screen-parity-corpus.json") +} + +fn build_cases() -> Vec { + cases() + .into_iter() + .map(|(name, width, height, cells)| { + let surface = ScreenSurface::from_cells(width, height, cells) + .expect("corpus case must build"); + json!({ + "name": name, + "width": width, + "height": height, + "cells": serde_json::to_value(&surface.cells).unwrap(), + "verdict": verdict(&surface), + }) + }) + .collect() +} + +#[test] +fn screen_parity_corpus_matches_rust_presenter() { + let built = build_cases(); + if std::env::var_os("SCREEN_PARITY_GENERATE").is_some() { + let corpus = json!({ + "oracle": "Rust presenter id_core::world_presentation::ScreenSurface: text_projection + per-cell fg/bg/bold (#8340, #796 item 9-2). Generated by crates/id-core/tests/screen_parity.rs; do not hand-edit verdicts.", + "cases": built, + }); + let path = corpus_path(); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + let mut text = serde_json::to_string_pretty(&corpus).unwrap(); + text.push('\n'); + std::fs::write(&path, text).unwrap(); + return; + } + let raw = std::fs::read_to_string(corpus_path()) + .expect("screen-parity corpus must be checked in at pkgs/id/test-data/"); + let corpus: Value = serde_json::from_str(&raw).unwrap(); + assert_eq!( + corpus["cases"], json!(built), + "Rust presenter verdicts drifted from the checked-in parity corpus; \ + if the change is intentional, regenerate with SCREEN_PARITY_GENERATE=1 \ + and record the divergence on #8340" + ); + // The presenter must also be able to frame every corpus screen. + for (name, width, height, cells) in cases() { + let surface = ScreenSurface::from_cells(width, height, cells).unwrap(); + assert!(surface.render_ansi().is_some(), "case {name} must render"); + } +} diff --git a/pkgs/id/test-data/screen-parity-corpus.json b/pkgs/id/test-data/screen-parity-corpus.json new file mode 100644 index 0000000000..1ec16e4d57 --- /dev/null +++ b/pkgs/id/test-data/screen-parity-corpus.json @@ -0,0 +1,1020 @@ +{ + "cases": [ + { + "cells": [ + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "H" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "i" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "o" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "k" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + } + ], + "height": 2, + "name": "plain_text", + "verdict": { + "cells": [ + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "H" + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "i" + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 3, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 5, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 6, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 7, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": "o" + }, + { + "bg": 0, + "bold": false, + "col": 3, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": "k" + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 5, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 6, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 7, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + } + ], + "text": "Hi\n ok" + }, + "width": 8 + }, + { + "cells": [ + { + "bg": 0, + "bold": true, + "columns": 1, + "fg": 196, + "symbol": "R" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 46, + "symbol": "G" + }, + { + "bg": 21, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "B" + }, + { + "bg": 0, + "bold": true, + "columns": 1, + "fg": 11, + "symbol": "W", + "token": "warning" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 14, + "symbol": "C", + "token": "code" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + } + ], + "height": 1, + "name": "styled_row", + "verdict": { + "cells": [ + { + "bg": 0, + "bold": true, + "col": 0, + "columns": 1, + "fg": 196, + "row": 0, + "symbol": "R" + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 1, + "fg": 46, + "row": 0, + "symbol": "G" + }, + { + "bg": 21, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "B" + }, + { + "bg": 0, + "bold": true, + "col": 3, + "columns": 1, + "fg": 11, + "row": 0, + "symbol": "W" + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 14, + "row": 0, + "symbol": "C" + }, + { + "bg": 0, + "bold": false, + "col": 5, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + } + ], + "text": "RGBWC" + }, + "width": 6 + }, + { + "cells": [ + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "a" + }, + { + "bg": 0, + "bold": false, + "columns": 2, + "fg": 0, + "symbol": "界" + }, + { + "bg": 0, + "bold": false, + "columns": 0, + "fg": 0, + "symbol": "" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "b" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + } + ], + "height": 1, + "name": "wide_glyph", + "verdict": { + "cells": [ + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "a" + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 2, + "fg": 0, + "row": 0, + "symbol": "界" + }, + { + "bg": 0, + "bold": false, + "col": 3, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "b" + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 5, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + } + ], + "text": "a界b" + }, + "width": 6 + }, + { + "cells": [ + { + "bg": 0, + "bold": false, + "columns": 2, + "fg": 0, + "symbol": "😀" + }, + { + "bg": 0, + "bold": false, + "columns": 0, + "fg": 0, + "symbol": "" + }, + { + "bg": 0, + "bold": false, + "columns": 2, + "fg": 0, + "symbol": "✈️" + }, + { + "bg": 0, + "bold": false, + "columns": 0, + "fg": 0, + "symbol": "" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "z" + } + ], + "height": 1, + "name": "emoji_sequences", + "verdict": { + "cells": [ + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 2, + "fg": 0, + "row": 0, + "symbol": "😀" + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 2, + "fg": 0, + "row": 0, + "symbol": "✈️" + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "z" + } + ], + "text": "😀✈️z" + }, + "width": 5 + }, + { + "cells": [ + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + } + ], + "height": 2, + "name": "blank_screen", + "verdict": { + "cells": [ + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 3, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 3, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + } + ], + "text": "\n" + }, + "width": 4 + }, + { + "cells": [ + { + "bg": 0, + "bold": false, + "columns": 2, + "fg": 9, + "symbol": "界" + }, + { + "bg": 0, + "bold": false, + "columns": 0, + "fg": 9, + "symbol": "" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "x" + }, + { + "bg": 0, + "bold": true, + "columns": 1, + "fg": 10, + "symbol": "!" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 10, + "symbol": "d", + "token": "success" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "o" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "n" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": "e" + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "columns": 1, + "fg": 0, + "symbol": " " + } + ], + "height": 2, + "name": "mixed_rows", + "verdict": { + "cells": [ + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 2, + "fg": 9, + "row": 0, + "symbol": "界" + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": "x" + }, + { + "bg": 0, + "bold": true, + "col": 3, + "columns": 1, + "fg": 10, + "row": 0, + "symbol": "!" + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 5, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 6, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 7, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 8, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 9, + "columns": 1, + "fg": 0, + "row": 0, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 0, + "columns": 1, + "fg": 10, + "row": 1, + "symbol": "d" + }, + { + "bg": 0, + "bold": false, + "col": 1, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": "o" + }, + { + "bg": 0, + "bold": false, + "col": 2, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": "n" + }, + { + "bg": 0, + "bold": false, + "col": 3, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": "e" + }, + { + "bg": 0, + "bold": false, + "col": 4, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 5, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 6, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 7, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 8, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + }, + { + "bg": 0, + "bold": false, + "col": 9, + "columns": 1, + "fg": 0, + "row": 1, + "symbol": " " + } + ], + "text": "界x!\ndone" + }, + "width": 10 + } + ], + "oracle": "Rust presenter id_core::world_presentation::ScreenSurface: text_projection + per-cell fg/bg/bold (#8340, #796 item 9-2). Generated by crates/id-core/tests/screen_parity.rs; do not hand-edit verdicts." +} diff --git a/pkgs/id/web/src/world-surface.parity.test.ts b/pkgs/id/web/src/world-surface.parity.test.ts new file mode 100644 index 0000000000..c8bb21445f --- /dev/null +++ b/pkgs/id/web/src/world-surface.parity.test.ts @@ -0,0 +1,139 @@ +/** + * Web/terminal parity harness for #796 item 9-2 (issue #8340). + * + * The checked-in corpus at `pkgs/id/test-data/screen-parity-corpus.json` + * is the oracle: its verdicts were generated from the real Rust + * presenter (crates/id-core/tests/screen_parity.rs). This test renders + * every corpus screen with the web renderer and asserts the DOM it + * produces carries exactly the corpus verdicts — same text projection, + * same presented cells with the same effective fg/bg/bold — so the + * web renderer and the Rust presenter cannot drift apart silently. + */ +import { existsSync, readFileSync } from "node:fs"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { renderWorldSurface, type ScreenSurface } from "./world-surface"; + +type CorpusCell = { + symbol: string; + fg: number; + bg: number; + bold: boolean; + columns: number; + row: number; + col: number; +}; + +type CorpusCase = { + name: string; + width: number; + height: number; + cells: unknown[]; + verdict: { text: string; cells: CorpusCell[] }; +}; + +// The corpus lives at pkgs/id/test-data/, one level above this +// package; vitest runs from the package root, and import.meta.url is +// not a file: URL under the vitest module runner, so resolve from +// the working directory with an explicit search list. +const corpusCandidates = [ + path.resolve(process.cwd(), "../test-data/screen-parity-corpus.json"), + path.resolve(process.cwd(), "test-data/screen-parity-corpus.json"), +]; +const corpusPath = corpusCandidates.find((candidate) => existsSync(candidate)); +if (!corpusPath) { + throw new Error(`screen-parity corpus not found; tried ${corpusCandidates.join(", ")}`); +} +const corpus = JSON.parse(readFileSync(corpusPath, "utf8")) as { cases: CorpusCase[] }; + +/** The standard xterm-256 palette as hex strings (index 0 = default). */ +function xtermHex(index: number): string | null { + if (index === 0) return null; + const basic = [ + "#000000", "#800000", "#008000", "#808000", "#000080", "#800080", "#008080", "#c0c0c0", + "#808080", "#ff0000", "#00ff00", "#ffff00", "#0000ff", "#ff00ff", "#00ffff", "#ffffff", + ]; + if (index < 16) return basic[index] ?? null; + if (index <= 231) { + const value = index - 16; + const level = (component: number) => (component === 0 ? 0 : 55 + component * 40); + const parts = [level(Math.floor(value / 36)), level(Math.floor((value % 36) / 6)), level(value % 6)]; + return `#${parts.map((part) => part.toString(16).padStart(2, "0")).join("")}`; + } + const gray = 8 + (index - 232) * 10; + return `#${gray.toString(16).padStart(2, "0").repeat(3)}`; +} + +// Parity is judged at the presented-colour level: the xterm palette +// maps several indices to the same hex (bright basic colours equal +// the cube corners, e.g. 11 and 226 are both #ffff00), so a hex can +// never be reverse-mapped to one index. The web side therefore +// compares the span's presented hex against the corpus index's hex. +function presentedHex(cssColor: string): string | null { + return cssColor ? cssColor.toLowerCase() : null; +} + +describe("screen parity corpus (Rust presenter oracle)", () => { + it("carries the six recorded cases", () => { + expect(corpus.cases.map((entry) => entry.name)).toEqual([ + "plain_text", + "styled_row", + "wide_glyph", + "emoji_sequences", + "blank_screen", + "mixed_rows", + ]); + }); + + for (const entry of corpus.cases) { + it(`web renderer matches the Rust verdict: ${entry.name}`, () => { + const surface = { + kind: "screen", + version: 1, + width: entry.width, + height: entry.height, + cells: entry.cells, + } as unknown as ScreenSurface; + const root = document.createElement("div"); + expect(renderWorldSurface(root, surface)).toBe(true); + + const spans = Array.from(root.querySelectorAll("span")); + const extracted = spans.map((span) => { + const gridColumn = span.style.gridColumn; + const match = /^(\d+) \/ span (\d+)$/.exec(gridColumn); + if (!match) throw new Error(`unparsable gridColumn ${gridColumn}`); + return { + symbol: span.textContent ?? "", + fgHex: presentedHex(span.style.color), + bgHex: presentedHex(span.style.backgroundColor), + bold: span.style.fontWeight === "bold", + columns: Number(match[2]), + row: Number(span.style.gridRow) - 1, + col: Number(match[1]) - 1, + }; + }); + extracted.sort((a, b) => a.row - b.row || a.col - b.col); + const expected = entry.verdict.cells.map((cell) => ({ + symbol: cell.symbol, + fgHex: xtermHex(cell.fg), + bgHex: xtermHex(cell.bg), + bold: cell.bold, + columns: cell.columns, + row: cell.row, + col: cell.col, + })); + expect(extracted).toEqual(expected); + + const rows: string[] = []; + for (let row = 0; row < entry.height; row++) { + const line = extracted + .filter((cell) => cell.row === row) + .sort((a, b) => a.col - b.col) + .map((cell) => cell.symbol) + .join(""); + rows.push(line.trimEnd()); + } + expect(rows.join("\n")).toBe(entry.verdict.text); + }); + } +});