Cotton code
63 lines · 3.6 KB · RustRaw
//! Fixture renderer for the invented household, never a server renderer.//! The generator records page figures independently of the submitted reading.//! Matching OCR and glyph readings are recorded fixtures (architect, 18 Oct//! 2026); no candidate or database text layer can supply the verifier's answer.use std::{collections::BTreeMap, sync::Mutex};use crate::mcp::handlers::money::statement_visual_check::{    StatementFigures, StatementVisualVerifier, VisualStatementReading,};#[derive(Default)]pub(super) struct RecordedPages(Mutex<BTreeMap<(String, String), StatementFigures>>);impl RecordedPages {    pub(super) fn figures(&self) -> BTreeMap<String, StatementFigures> {        self.0.lock().expect("synthetic page fixtures").iter()            .map(|((_, statement), figures)| (statement.clone(), figures.clone())).collect()    }    pub(super) fn record(&self, database: &str, statement: &str, figures: StatementFigures) {        self.0.lock().expect("synthetic page fixtures").insert((database.into(), statement.into()), figures);    }}impl StatementVisualVerifier for RecordedPages {    fn rendered_reading(&self, database: &str, statement: &str, _candidate: &StatementFigures) -> Result<VisualStatementReading, String> {        let figures = self.0.lock().map_err(|_| "synthetic page fixtures unavailable")?            .get(&(database.into(), statement.into())).cloned()            .ok_or("no recorded pages for this synthetic statement")?;        Ok(VisualStatementReading {            has_text_layer: true,            font_glyph_reading: Some(figures.clone()),            figures,        })    }}#[cfg(test)]mod tests {    use super::*;    use crate::mcp::handlers::money::statement_visual_check::{verify_visual_facts, VisualCheckError, VisualStatementHeader, VisualStatementLine};    use cotton_core::constants::LineDirection;    /// RULE: A synthetic renderer answers its recorded database/statement pages, never the candidate; an altered amount must fail the same comparison as a real decoy (18 Oct 2026, headless_seed red on 8455).    #[test]    fn recorded_pages_never_echo_the_submitted_candidate() {        let pages = RecordedPages::default();        let figures = StatementFigures {            header: VisualStatementHeader { page: 1, period_start: "2026-09-01".into(), period_end: "2026-09-30".into(), opening_balance_minor: Some(10000), closing_balance_minor: Some(9000) },            lines: vec![VisualStatementLine { page: 2, posted_on: "2026-09-20".into(), amount_minor: 1000, direction: LineDirection::Out, balance_after_minor: Some(9000), ignored: false }],        };        pages.record("synthetic-database", "synthetic-statement", figures.clone());        assert!(verify_visual_facts(Some(&pages), "synthetic-database", "synthetic-statement", &figures).is_ok());        let mut decoy = figures.clone();        decoy.lines[0].amount_minor += 1;        assert_eq!(verify_visual_facts(Some(&pages), "synthetic-database", "synthetic-statement", &decoy), Err(VisualCheckError::Page { page: 2, field: "amount" }));        let mut wrong_period = figures.clone();        wrong_period.header.period_start = "2026-08-01".into();        assert_eq!(verify_visual_facts(Some(&pages), "synthetic-database", "synthetic-statement", &wrong_period), Err(VisualCheckError::Header { page: 1, field: "period start" }));        assert_eq!(pages.rendered_reading("synthetic-database", "synthetic-statement", &decoy).unwrap().figures, figures);        assert!(pages.rendered_reading("another-database", "synthetic-statement", &figures).is_err());        assert!(pages.rendered_reading("synthetic-database", "another-statement", &figures).is_err());    }}