From 7d4e29372f55dd82a45408b7b29c9e90fbf1783f Mon Sep 17 00:00:00 2001 From: FredApps Date: Sun, 15 Nov 2026 21:48:56 +0100 Subject: [PATCH] Native JSON workbook: golden bytes before the move to craft-libs A workbook with text, numbers, a formula, bold, a number format and a macro is saved as native JSON. Its bytes, the bytes saved after a newer-format macro is kept, and the error for a non-workbook file are golden files written by the current code. Opening the golden file as .json and .scjson and saving it again gives it back. --- crates/engine/src/tests.rs | 1 + crates/engine/src/tests/json_golden.rs | 80 ++++ crates/engine/tests/golden/.gitattributes | 2 + crates/engine/tests/golden/bad.error.txt | 1 + crates/engine/tests/golden/book.json | 440 +++++++++++++++++++++ crates/engine/tests/golden/kept.json | 448 ++++++++++++++++++++++ 6 files changed, 972 insertions(+) create mode 100644 crates/engine/src/tests/json_golden.rs create mode 100644 crates/engine/tests/golden/.gitattributes create mode 100644 crates/engine/tests/golden/bad.error.txt create mode 100644 crates/engine/tests/golden/book.json create mode 100644 crates/engine/tests/golden/kept.json diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 747c0cd52..5262a988f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6494,6 +6494,7 @@ fn a_script_cannot_switch_windows() { mod controls; mod equation; +mod json_golden; mod macros; mod objects; mod python_cells; diff --git a/crates/engine/src/tests/json_golden.rs b/crates/engine/src/tests/json_golden.rs new file mode 100644 index 000000000..66680221d --- /dev/null +++ b/crates/engine/src/tests/json_golden.rs @@ -0,0 +1,80 @@ +//! Byte-for-byte checks on the native JSON workbook (`.json`, `.scjson`). The files in +//! `crates/engine/tests/golden` were written before the format moved to `craft-gridcraft-format` +//! (storytold/craft-libs). Saving the same workbook must give the same bytes, and opening a +//! golden file and saving it again must give it back. +//! `GRIDCRAFT_BLESS_GOLDEN=1` rewrites them. Only do that for a deliberate format change. + +use std::path::PathBuf; + +use serde_json::json; + +use super::s; +use crate::Session; + +fn golden(name: &str) -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("golden").join(name) +} + +fn check(name: &str, actual: &[u8]) { + let path = golden(name); + if std::env::var_os("GRIDCRAFT_BLESS_GOLDEN").is_some() { + std::fs::write(&path, actual).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + return; + } + let expected = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + assert!(expected == actual, "{name} changed ({} bytes, golden has {})", actual.len(), expected.len()); +} + +fn save(s: &mut Session) -> Vec { + let r = s.execute("file.saveBytes", json!({"format": "json"})).unwrap(); + crate::io::base64_decode(r["base64"].as_str().unwrap()).unwrap() +} + +fn open(s: &mut Session, name: &str, bytes: &[u8]) { + s.execute("file.open", json!({"name": name, "base64": crate::io::base64_encode(bytes)})).unwrap(); +} + +/// A workbook with text, numbers, a formula, a style, a number format, a second sheet and a macro. +fn book() -> Session { + let mut s = s(); + for (cell, input) in [("A1", "Item"), ("B1", "Qty"), ("A2", "Äpfel ✓"), ("B2", "3.5"), ("A3", "Pears"), ("B3", "2"), ("B4", "=SUM(B2:B3)")] { + s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap(); + } + s.execute("home.bold", json!({"range": "A1:B1"})).unwrap(); + s.execute("home.numberFormat", json!({"range": "B2:B4", "format": "0.00"})).unwrap(); + s.execute("macro.save", json!({"name": "Fill", "steps": [{"command": "cell.set", "params": {"cell": "C1", "input": "x"}}]})).unwrap(); + s +} + +#[test] +fn a_save_writes_the_golden_workbook_bytes() { + let mut s = book(); + check("book.json", &save(&mut s)); +} + +#[test] +fn the_golden_workbook_opens_and_saves_back_unchanged() { + let bytes = std::fs::read(golden("book.json")).unwrap(); + for name in ["book.json", "book.scjson"] { + let mut s = s(); + open(&mut s, name, &bytes); + assert_eq!(super::v(&s, "B4"), gridcraft_core::Value::Number(5.5), "{name}"); + assert!(save(&mut s) == bytes, "{name} saved back differently"); + } +} + +#[test] +fn macros_in_a_newer_format_are_written_back_as_the_file_held_them() { + // The save that splices kept macros back into `macros` goes through a JSON value. + let mut v: serde_json::Value = serde_json::from_slice(&std::fs::read(golden("book.json")).unwrap()).unwrap(); + v["macros"].as_array_mut().unwrap().push(json!({"name": "Future", "v": gridcraft_model::MACRO_VERSION + 1, "extra": [1, 2]})); + let mut s = s(); + open(&mut s, "kept.json", &serde_json::to_vec_pretty(&v).unwrap()); + check("kept.json", &save(&mut s)); +} + +#[test] +fn what_is_not_a_json_workbook_says_so() { + let err = crate::io::open_bytes("bad.json", b"{\"sheets\": 7}").unwrap_err().to_string(); + check("bad.error.txt", err.as_bytes()); +} diff --git a/crates/engine/tests/golden/.gitattributes b/crates/engine/tests/golden/.gitattributes new file mode 100644 index 000000000..c5d2e24da --- /dev/null +++ b/crates/engine/tests/golden/.gitattributes @@ -0,0 +1,2 @@ +# Golden files are compared byte for byte: no line-ending conversion on checkout. +* -text diff --git a/crates/engine/tests/golden/bad.error.txt b/crates/engine/tests/golden/bad.error.txt new file mode 100644 index 000000000..1b8a6dd14 --- /dev/null +++ b/crates/engine/tests/golden/bad.error.txt @@ -0,0 +1 @@ +not a GridCraft JSON workbook: invalid type: integer `7`, expected a sequence at line 1 column 12 \ No newline at end of file diff --git a/crates/engine/tests/golden/book.json b/crates/engine/tests/golden/book.json new file mode 100644 index 000000000..403189aa7 --- /dev/null +++ b/crates/engine/tests/golden/book.json @@ -0,0 +1,440 @@ +{ + "sheets": [ + { + "name": "Sheet1", + "cells": { + "bands": { + "0": { + "rows": { + "0": { + "0": { + "value": { + "t": "Text", + "v": "Item" + }, + "formula": null, + "style": 1 + }, + "1": { + "value": { + "t": "Text", + "v": "Qty" + }, + "formula": null, + "style": 1 + } + }, + "1": { + "0": { + "value": { + "t": "Text", + "v": "Äpfel ✓" + }, + "formula": null, + "style": 0 + }, + "1": { + "value": { + "t": "Number", + "v": 3.5 + }, + "formula": null, + "style": 2 + } + }, + "2": { + "0": { + "value": { + "t": "Text", + "v": "Pears" + }, + "formula": null, + "style": 0 + }, + "1": { + "value": { + "t": "Number", + "v": 2.0 + }, + "formula": null, + "style": 2 + } + }, + "3": { + "1": { + "value": { + "t": "Number", + "v": 5.5 + }, + "formula": { + "text": "SUM(B2:B3)", + "array": null + }, + "style": 2 + } + } + } + } + }, + "len": 7 + }, + "cols": {}, + "rows": {}, + "default_col_width": 64.0, + "default_row_height": 20.0, + "merges": [], + "freeze": null, + "tab_color": null, + "visibility": "Visible", + "show_gridlines": true, + "show_headings": true, + "show_formulas": false, + "show_zeros": true, + "show_outline_symbols": true, + "summary_below": true, + "summary_right": true, + "right_to_left": false, + "zoom": 100, + "cond_formats": [], + "validations": [], + "tables": [], + "autofilter": null, + "comments": {}, + "hyperlinks": {}, + "charts": [], + "images": [], + "shapes": [], + "sparklines": [], + "slicers": [], + "pivots": [], + "protection": null, + "print": { + "orientation": "Portrait", + "paper": "Letter", + "margins": [ + 0.7, + 0.7, + 0.75, + 0.75, + 0.3, + 0.3 + ], + "print_area": null, + "title_rows": null, + "title_cols": null, + "scale": 100, + "fit_to": null, + "gridlines": false, + "headings": false, + "center_h": false, + "center_v": false, + "header": "", + "footer": "", + "row_breaks": [], + "col_breaks": [], + "different_first": false, + "different_odd_even": false, + "first_header": "", + "first_footer": "", + "even_header": "", + "even_footer": "", + "hf_scale_with_doc": true, + "hf_align_with_margins": true, + "page_order": "downThenOver" + }, + "view_active": { + "row": 0, + "col": 0 + }, + "view_top_left": { + "row": 0, + "col": 0 + }, + "view": "normal" + } + ], + "names": [], + "styles": { + "styles": [ + { + "font": { + "name": "Calibri", + "size": 11.0, + "bold": false, + "italic": false, + "underline": "None", + "strike": false, + "color": "Auto", + "vert": "Baseline", + "charset": null, + "family": null, + "minor": true + }, + "fill": { + "pattern": "None", + "fg": "Auto", + "bg": "Auto" + }, + "borders": { + "left": { + "style": "None", + "color": "Auto" + }, + "right": { + "style": "None", + "color": "Auto" + }, + "top": { + "style": "None", + "color": "Auto" + }, + "bottom": { + "style": "None", + "color": "Auto" + }, + "diag_down": { + "style": "None", + "color": "Auto" + }, + "diag_up": { + "style": "None", + "color": "Auto" + } + }, + "align": { + "h": "General", + "v": "Bottom", + "wrap": false, + "shrink": false, + "indent": 0, + "rotation": 0, + "reading_order": null + }, + "num_fmt": "General", + "protection": { + "locked": true, + "hidden": false + }, + "dxf_stated": { + "bold": false, + "italic": false, + "strike": false, + "underline": false, + "size": false, + "name": false, + "scheme": null, + "protection": false + } + }, + { + "font": { + "name": "Calibri", + "size": 11.0, + "bold": true, + "italic": false, + "underline": "None", + "strike": false, + "color": "Auto", + "vert": "Baseline", + "charset": null, + "family": null, + "minor": true + }, + "fill": { + "pattern": "None", + "fg": "Auto", + "bg": "Auto" + }, + "borders": { + "left": { + "style": "None", + "color": "Auto" + }, + "right": { + "style": "None", + "color": "Auto" + }, + "top": { + "style": "None", + "color": "Auto" + }, + "bottom": { + "style": "None", + "color": "Auto" + }, + "diag_down": { + "style": "None", + "color": "Auto" + }, + "diag_up": { + "style": "None", + "color": "Auto" + } + }, + "align": { + "h": "General", + "v": "Bottom", + "wrap": false, + "shrink": false, + "indent": 0, + "rotation": 0, + "reading_order": null + }, + "num_fmt": "General", + "protection": { + "locked": true, + "hidden": false + }, + "dxf_stated": { + "bold": false, + "italic": false, + "strike": false, + "underline": false, + "size": false, + "name": false, + "scheme": null, + "protection": false + } + }, + { + "font": { + "name": "Calibri", + "size": 11.0, + "bold": false, + "italic": false, + "underline": "None", + "strike": false, + "color": "Auto", + "vert": "Baseline", + "charset": null, + "family": null, + "minor": true + }, + "fill": { + "pattern": "None", + "fg": "Auto", + "bg": "Auto" + }, + "borders": { + "left": { + "style": "None", + "color": "Auto" + }, + "right": { + "style": "None", + "color": "Auto" + }, + "top": { + "style": "None", + "color": "Auto" + }, + "bottom": { + "style": "None", + "color": "Auto" + }, + "diag_down": { + "style": "None", + "color": "Auto" + }, + "diag_up": { + "style": "None", + "color": "Auto" + } + }, + "align": { + "h": "General", + "v": "Bottom", + "wrap": false, + "shrink": false, + "indent": 0, + "rotation": 0, + "reading_order": null + }, + "num_fmt": "0.00", + "protection": { + "locked": true, + "hidden": false + }, + "dxf_stated": { + "bold": false, + "italic": false, + "strike": false, + "underline": false, + "size": false, + "name": false, + "scheme": null, + "protection": false + } + } + ] + }, + "theme": { + "name": "Craft", + "colors": [ + 16777215, + 0, + 15263976, + 927809, + 1400962, + 15298866, + 1665828, + 1023701, + 10496915, + 5154606, + 4618374, + 9855101 + ], + "major_font": "Calibri", + "minor_font": "Calibri", + "major_font_ea": "", + "minor_font_ea": "", + "major_font_cs": "", + "minor_font_cs": "", + "major_font_scripts": [], + "minor_font_scripts": [] + }, + "date_system": "D1900", + "calc": { + "mode": "Automatic", + "iterative": false, + "max_iterations": 100, + "max_change": 0.001, + "precision_as_displayed": false, + "r1c1": false + }, + "active_sheet": 0, + "props": { + "title": "", + "subject": "", + "author": "GridCraft User", + "company": "", + "keywords": "", + "description": "" + }, + "protected_structure": false, + "custom_lists": [], + "cell_styles": [], + "vba_project": null, + "macros": [ + { + "v": 1, + "name": "Fill", + "description": "", + "inputs": [], + "steps": [ + { + "type": "command", + "id": "cell.set", + "params": { + "cell": "C1", + "input": "x" + } + } + ], + "output": { + "onConflict": "ask" + }, + "permissions": [] + } + ] +} \ No newline at end of file diff --git a/crates/engine/tests/golden/kept.json b/crates/engine/tests/golden/kept.json new file mode 100644 index 000000000..0cbc313cf --- /dev/null +++ b/crates/engine/tests/golden/kept.json @@ -0,0 +1,448 @@ +{ + "active_sheet": 0, + "calc": { + "iterative": false, + "max_change": 0.001, + "max_iterations": 100, + "mode": "Automatic", + "precision_as_displayed": false, + "r1c1": false + }, + "cell_styles": [], + "custom_lists": [], + "date_system": "D1900", + "macros": [ + { + "description": "", + "inputs": [], + "name": "Fill", + "output": { + "onConflict": "ask" + }, + "permissions": [], + "steps": [ + { + "id": "cell.set", + "params": { + "cell": "C1", + "input": "x" + }, + "type": "command" + } + ], + "v": 1 + }, + { + "extra": [ + 1, + 2 + ], + "name": "Future", + "v": 2 + } + ], + "names": [], + "props": { + "author": "GridCraft User", + "company": "", + "description": "", + "keywords": "", + "subject": "", + "title": "" + }, + "protected_structure": false, + "sheets": [ + { + "autofilter": null, + "cells": { + "bands": { + "0": { + "rows": { + "0": { + "0": { + "formula": null, + "style": 1, + "value": { + "t": "Text", + "v": "Item" + } + }, + "1": { + "formula": null, + "style": 1, + "value": { + "t": "Text", + "v": "Qty" + } + } + }, + "1": { + "0": { + "formula": null, + "style": 0, + "value": { + "t": "Text", + "v": "Äpfel ✓" + } + }, + "1": { + "formula": null, + "style": 2, + "value": { + "t": "Number", + "v": 3.5 + } + } + }, + "2": { + "0": { + "formula": null, + "style": 0, + "value": { + "t": "Text", + "v": "Pears" + } + }, + "1": { + "formula": null, + "style": 2, + "value": { + "t": "Number", + "v": 2.0 + } + } + }, + "3": { + "1": { + "formula": { + "array": null, + "text": "SUM(B2:B3)" + }, + "style": 2, + "value": { + "t": "Number", + "v": 5.5 + } + } + } + } + } + }, + "len": 7 + }, + "charts": [], + "cols": {}, + "comments": {}, + "cond_formats": [], + "default_col_width": 64.0, + "default_row_height": 20.0, + "freeze": null, + "hyperlinks": {}, + "images": [], + "merges": [], + "name": "Sheet1", + "pivots": [], + "print": { + "center_h": false, + "center_v": false, + "col_breaks": [], + "different_first": false, + "different_odd_even": false, + "even_footer": "", + "even_header": "", + "first_footer": "", + "first_header": "", + "fit_to": null, + "footer": "", + "gridlines": false, + "header": "", + "headings": false, + "hf_align_with_margins": true, + "hf_scale_with_doc": true, + "margins": [ + 0.699999988079071, + 0.699999988079071, + 0.75, + 0.75, + 0.30000001192092896, + 0.30000001192092896 + ], + "orientation": "Portrait", + "page_order": "downThenOver", + "paper": "Letter", + "print_area": null, + "row_breaks": [], + "scale": 100, + "title_cols": null, + "title_rows": null + }, + "protection": null, + "right_to_left": false, + "rows": {}, + "shapes": [], + "show_formulas": false, + "show_gridlines": true, + "show_headings": true, + "show_outline_symbols": true, + "show_zeros": true, + "slicers": [], + "sparklines": [], + "summary_below": true, + "summary_right": true, + "tab_color": null, + "tables": [], + "validations": [], + "view": "normal", + "view_active": { + "col": 0, + "row": 0 + }, + "view_top_left": { + "col": 0, + "row": 0 + }, + "visibility": "Visible", + "zoom": 100 + } + ], + "styles": { + "styles": [ + { + "align": { + "h": "General", + "indent": 0, + "reading_order": null, + "rotation": 0, + "shrink": false, + "v": "Bottom", + "wrap": false + }, + "borders": { + "bottom": { + "color": "Auto", + "style": "None" + }, + "diag_down": { + "color": "Auto", + "style": "None" + }, + "diag_up": { + "color": "Auto", + "style": "None" + }, + "left": { + "color": "Auto", + "style": "None" + }, + "right": { + "color": "Auto", + "style": "None" + }, + "top": { + "color": "Auto", + "style": "None" + } + }, + "dxf_stated": { + "bold": false, + "italic": false, + "name": false, + "protection": false, + "scheme": null, + "size": false, + "strike": false, + "underline": false + }, + "fill": { + "bg": "Auto", + "fg": "Auto", + "pattern": "None" + }, + "font": { + "bold": false, + "charset": null, + "color": "Auto", + "family": null, + "italic": false, + "minor": true, + "name": "Calibri", + "size": 11.0, + "strike": false, + "underline": "None", + "vert": "Baseline" + }, + "num_fmt": "General", + "protection": { + "hidden": false, + "locked": true + } + }, + { + "align": { + "h": "General", + "indent": 0, + "reading_order": null, + "rotation": 0, + "shrink": false, + "v": "Bottom", + "wrap": false + }, + "borders": { + "bottom": { + "color": "Auto", + "style": "None" + }, + "diag_down": { + "color": "Auto", + "style": "None" + }, + "diag_up": { + "color": "Auto", + "style": "None" + }, + "left": { + "color": "Auto", + "style": "None" + }, + "right": { + "color": "Auto", + "style": "None" + }, + "top": { + "color": "Auto", + "style": "None" + } + }, + "dxf_stated": { + "bold": false, + "italic": false, + "name": false, + "protection": false, + "scheme": null, + "size": false, + "strike": false, + "underline": false + }, + "fill": { + "bg": "Auto", + "fg": "Auto", + "pattern": "None" + }, + "font": { + "bold": true, + "charset": null, + "color": "Auto", + "family": null, + "italic": false, + "minor": true, + "name": "Calibri", + "size": 11.0, + "strike": false, + "underline": "None", + "vert": "Baseline" + }, + "num_fmt": "General", + "protection": { + "hidden": false, + "locked": true + } + }, + { + "align": { + "h": "General", + "indent": 0, + "reading_order": null, + "rotation": 0, + "shrink": false, + "v": "Bottom", + "wrap": false + }, + "borders": { + "bottom": { + "color": "Auto", + "style": "None" + }, + "diag_down": { + "color": "Auto", + "style": "None" + }, + "diag_up": { + "color": "Auto", + "style": "None" + }, + "left": { + "color": "Auto", + "style": "None" + }, + "right": { + "color": "Auto", + "style": "None" + }, + "top": { + "color": "Auto", + "style": "None" + } + }, + "dxf_stated": { + "bold": false, + "italic": false, + "name": false, + "protection": false, + "scheme": null, + "size": false, + "strike": false, + "underline": false + }, + "fill": { + "bg": "Auto", + "fg": "Auto", + "pattern": "None" + }, + "font": { + "bold": false, + "charset": null, + "color": "Auto", + "family": null, + "italic": false, + "minor": true, + "name": "Calibri", + "size": 11.0, + "strike": false, + "underline": "None", + "vert": "Baseline" + }, + "num_fmt": "0.00", + "protection": { + "hidden": false, + "locked": true + } + } + ] + }, + "theme": { + "colors": [ + 16777215, + 0, + 15263976, + 927809, + 1400962, + 15298866, + 1665828, + 1023701, + 10496915, + 5154606, + 4618374, + 9855101 + ], + "major_font": "Calibri", + "major_font_cs": "", + "major_font_ea": "", + "major_font_scripts": [], + "minor_font": "Calibri", + "minor_font_cs": "", + "minor_font_ea": "", + "minor_font_scripts": [], + "name": "Craft" + }, + "vba_project": null +} \ No newline at end of file