formualizer-cli 0.11.1

Source-preserving XLSX formula-cache recalculation command
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! `--now`, `--tz` and `--seed`, the JSON clock/seed echo, and help text.
use chrono::{NaiveDate, NaiveDateTime};
use formualizer_cli::run;
use serde_json::Value;
use std::{
    collections::BTreeMap,
    io::{Cursor, Read, Write},
    path::Path,
};
const MAIN: &str = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
const RELS: &str = "http://schemas.openxmlformats.org/package/2006/relationships";
const OFFICE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
const DEFAULT_SEED: u64 = 0xF0F0_D0D0_AAAA_5555;

/// A one-sheet package: A1 `TODAY()`, A2 `NOW()`, A3 `RAND()`,
/// A4 `RANDBETWEEN(1,1000000000)`, all with stale caches.
fn volatile_book() -> Vec<u8> {
    let rows = "<row r=\"1\"><c r=\"A1\"><f>TODAY()</f><v>0</v></c></row>\
        <row r=\"2\"><c r=\"A2\"><f>NOW()</f><v>0</v></c></row>\
        <row r=\"3\"><c r=\"A3\"><f>RAND()</f><v>0</v></c></row>\
        <row r=\"4\"><c r=\"A4\"><f>RANDBETWEEN(1,1000000000)</f><v>0</v></c></row>";
    let parts: BTreeMap<_, _> = [
        ("[Content_Types].xml", "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"xml\" ContentType=\"application/xml\"/><Override PartName=\"/xl/workbook.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"/><Override PartName=\"/xl/worksheets/sheet1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/></Types>".to_owned()),
        ("_rels/.rels", format!("<Relationships xmlns=\"{RELS}\"><Relationship Id=\"rId1\" Type=\"{OFFICE}/officeDocument\" Target=\"xl/workbook.xml\"/></Relationships>")),
        ("xl/workbook.xml", format!("<workbook xmlns=\"{MAIN}\" xmlns:r=\"{OFFICE}\"><sheets><sheet name=\"Sheet1\" sheetId=\"1\" r:id=\"rId1\"/></sheets></workbook>")),
        ("xl/_rels/workbook.xml.rels", format!("<Relationships xmlns=\"{RELS}\"><Relationship Id=\"rId1\" Type=\"{OFFICE}/worksheet\" Target=\"worksheets/sheet1.xml\"/></Relationships>")),
        ("xl/worksheets/sheet1.xml", format!("<worksheet xmlns=\"{MAIN}\"><dimension ref=\"A1:A4\"/><sheetData>{rows}</sheetData></worksheet>")),
    ].into_iter().collect();
    let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
    for (name, value) in parts {
        zip.start_file(name, zip::write::SimpleFileOptions::default())
            .unwrap();
        zip.write_all(value.as_bytes()).unwrap();
    }
    zip.finish().unwrap().into_inner()
}
fn invoke(args: &[&str]) -> (i32, String, String) {
    let (mut out, mut err) = (Vec::new(), Vec::new());
    let code = run(
        std::iter::once("formualizer").chain(args.iter().copied()),
        &mut out,
        &mut err,
        None,
    );
    (
        code,
        String::from_utf8(out).unwrap(),
        String::from_utf8(err).unwrap(),
    )
}
/// `recalc <input> --json <extra...>`: exit code and the single JSON report.
fn recalc(input: &Path, extra: &[&str]) -> (i32, Value) {
    let mut args = vec!["recalc", input.to_str().unwrap(), "--json"];
    args.extend(extra);
    let (code, out, err) = invoke(&args);
    assert!(err.is_empty(), "{err}");
    assert_eq!(out.lines().count(), 1, "{out}");
    (code, serde_json::from_str(&out).unwrap())
}
/// Cached numeric value of `cell` in the package at `path`.
fn cached(path: &Path, cell: &str) -> f64 {
    let mut zip = zip::ZipArchive::new(Cursor::new(std::fs::read(path).unwrap())).unwrap();
    let mut sheet = String::new();
    zip.by_name("xl/worksheets/sheet1.xml")
        .unwrap()
        .read_to_string(&mut sheet)
        .unwrap();
    let start = sheet.find(&format!("<c r=\"{cell}\"")).expect(cell);
    let c = &sheet[start..start + sheet[start..].find("</c>").unwrap()];
    let v = &c[c.find("<v>").expect(c) + 3..];
    v[..v.find("</v>").unwrap()].parse().unwrap()
}
fn serial(t: NaiveDateTime) -> f64 {
    let base = NaiveDate::from_ymd_opt(1899, 12, 30)
        .unwrap()
        .and_hms_opt(0, 0, 0)
        .unwrap();
    (t - base).num_milliseconds() as f64 / 86_400_000.0
}
fn at(text: &str) -> NaiveDateTime {
    NaiveDateTime::parse_from_str(text, "%Y-%m-%d %H:%M").unwrap()
}
/// TODAY/NOW caches equal the wall time `local` exactly.
fn assert_clock(path: &Path, local: NaiveDateTime, context: &str) {
    let today = serial(local.date().and_hms_opt(0, 0, 0).unwrap());
    assert_eq!(cached(path, "A1"), today, "TODAY {context}");
    assert!(
        (cached(path, "A2") - serial(local)).abs() < 1e-9,
        "NOW {context}: {} vs {}",
        cached(path, "A2"),
        serial(local)
    );
}

#[test]
fn now_and_tz_fix_today_and_now_across_a_date_boundary() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    let out = dir.path().join("out.xlsx");
    std::fs::write(&input, volatile_book()).unwrap();
    let o = out.to_str().unwrap();
    for (flags, local, echo_now, echo_tz) in [
        (
            vec!["--now", "2026-03-01T23:30:00Z"],
            "2026-03-01 23:30",
            "2026-03-01T23:30:00Z",
            "UTC",
        ),
        (
            vec!["--now", "2026-03-01T23:30:00Z", "--tz", "+01:00"],
            "2026-03-02 00:30",
            "2026-03-02T00:30:00+01:00",
            "+01:00",
        ),
        (
            vec!["--now", "2026-03-02T00:30:00+01:00"],
            "2026-03-02 00:30",
            "2026-03-02T00:30:00+01:00",
            "+01:00",
        ),
        (
            vec!["--now", "2026-03-02T00:30:00+01:00", "--tz", "utc"],
            "2026-03-01 23:30",
            "2026-03-01T23:30:00Z",
            "UTC",
        ),
        (
            vec!["--now", "2026-03-01T03:00:00Z", "--tz", "-05:00"],
            "2026-02-28 22:00",
            "2026-02-28T22:00:00-05:00",
            "-05:00",
        ),
        (
            vec!["--now=2026-03-01T03:00:00+00:00", "--tz=-05:00"],
            "2026-02-28 22:00",
            "2026-02-28T22:00:00-05:00",
            "-05:00",
        ),
    ] {
        let mut args = flags.clone();
        args.extend(["-o", o]);
        let (code, report) = recalc(&input, &args);
        assert_eq!(code, 0, "{report}");
        assert_clock(&out, at(local), &format!("{flags:?}"));
        assert_eq!(report["clock"]["now"], echo_now, "{flags:?}");
        assert_eq!(report["clock"]["timezone"], echo_tz, "{flags:?}");
        assert_eq!(report["clock"]["fixed"], true);
        assert_eq!(report["seed"], DEFAULT_SEED);
    }
}

#[test]
fn same_now_and_seed_are_byte_identical_and_seed_changes_rand() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    std::fs::write(&input, volatile_book()).unwrap();
    let run = |name: &str, extra: &[&str]| {
        let out = dir.path().join(name);
        let mut args = vec!["-o", out.to_str().unwrap()];
        args.extend(extra);
        assert_eq!(recalc(&input, &args).0, 0);
        out
    };
    let fixed = ["--now", "2026-01-31T09:00:00Z", "--seed", "42"];
    let (a, b) = (run("a.xlsx", &fixed), run("b.xlsx", &fixed));
    assert_eq!(std::fs::read(&a).unwrap(), std::fs::read(&b).unwrap());
    let c = run("c.xlsx", &["--now", "2026-01-31T09:00:00Z", "--seed", "43"]);
    assert_ne!(cached(&a, "A3"), cached(&c, "A3"));
    assert_ne!(cached(&a, "A4"), cached(&c, "A4"));
    assert_eq!(cached(&a, "A1"), cached(&c, "A1"));
    // RAND is reproducible without --seed, and the default is the echoed
    // seed. (Portable builds need --now for this workbook's TODAY/NOW.)
    let clock: &[&str] = if cfg!(feature = "system-clock") {
        &[]
    } else {
        &["--now", "2026-01-31T09:00:00Z"]
    };
    let (d, e) = (run("d.xlsx", clock), run("e.xlsx", clock));
    let seed = DEFAULT_SEED.to_string();
    let f = run("f.xlsx", &[clock, &["--seed", &seed]].concat());
    for cell in ["A3", "A4"] {
        assert_eq!(cached(&d, cell), cached(&e, cell));
        assert_eq!(cached(&d, cell), cached(&f, cell));
    }
}

#[test]
fn check_is_current_for_output_produced_with_the_same_flags() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    let out = dir.path().join("out.xlsx");
    std::fs::write(&input, volatile_book()).unwrap();
    let fixed = ["--now", "2026-01-31T09:00:00+02:00", "--seed", "7"];
    let mut args = vec!["-o", out.to_str().unwrap()];
    args.extend(fixed);
    assert_eq!(recalc(&input, &args).0, 0);
    let bytes = std::fs::read(&out).unwrap();
    let mut check = vec!["--check"];
    check.extend(fixed);
    let (code, report) = recalc(&out, &check);
    assert_eq!((code, report["status"].as_str()), (0, Some("current")));
    assert_eq!(report["clock"]["now"], "2026-01-31T09:00:00+02:00");
    // A different seed or instant makes the same output stale.
    for other in [
        ["--now", "2026-01-31T09:00:00+02:00", "--seed", "8"],
        ["--now", "2026-02-01T09:00:00+02:00", "--seed", "7"],
    ] {
        let mut check = vec!["--check"];
        check.extend(other);
        let (code, report) = recalc(&out, &check);
        assert_eq!((code, report["status"].as_str()), (3, Some("stale")));
    }
    // In-place recalculation with the same flags is a byte-identical no-op.
    let (code, report) = recalc(&out, &fixed);
    assert_eq!((code, report["status"].as_str()), (0, Some("unchanged")));
    assert_eq!(std::fs::read(&out).unwrap(), bytes);
}

#[test]
fn invalid_now_tz_and_seed_are_usage_errors() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    let bytes = volatile_book();
    std::fs::write(&input, &bytes).unwrap();
    let path = input.to_str().unwrap();
    for (flag, value) in [
        ("--now", "2026-01-31T09:00:00"),
        ("--now", "2026-01-31"),
        ("--now", "yesterday"),
        ("--tz", "Europe/Paris"),
        ("--tz", "Local"),
        ("--tz", "+24:00"),
        ("--tz", "+02:60"),
        ("--tz", "+2:00"),
        ("--tz", "0200"),
        ("--seed", "-1"),
        ("--seed", "18446744073709551616"),
        ("--seed", "abc"),
    ] {
        let assignment = format!("{flag}={value}");
        let (code, out, err) = invoke(&["recalc", path, &assignment, "--json"]);
        assert_eq!(code, 64, "{assignment}");
        assert!(err.is_empty(), "{assignment}: {err}");
        let report: Value = serde_json::from_str(&out).unwrap();
        assert_eq!(out.lines().count(), 1);
        assert_eq!(report["status"], "error");
        assert_eq!(report["clock"], Value::Null);
        assert_eq!(report["seed"], Value::Null);
        let message = report["message"].as_str().unwrap();
        assert!(message.contains(flag), "{assignment}: {message}");
        let (code, out, err) = invoke(&["recalc", path, &assignment]);
        assert_eq!(code, 64, "{assignment}");
        assert!(out.is_empty());
        assert!(err.contains("invalid value"), "{assignment}: {err}");
        assert!(err.contains("For more information"), "{err}");
    }
    let (_, out, _) = invoke(&["recalc", path, "--now", "2026-01-31T09:00:00", "--json"]);
    assert!(out.contains("offset or Z"), "{out}");
    assert_eq!(std::fs::read(&input).unwrap(), bytes);
}

#[test]
fn clock_and_seed_echo_present_when_computed_and_null_otherwise() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    std::fs::write(&input, volatile_book()).unwrap();
    #[cfg(feature = "system-clock")]
    for extra in [vec![], vec!["--check"]] {
        let (code, report) = recalc(&input, &extra);
        assert!(code == 0 || code == 3, "{report}");
        assert_eq!(report["seed"], DEFAULT_SEED);
        assert_eq!(report["clock"]["fixed"], false);
        assert_eq!(report["clock"]["timezone"], "Local");
        assert!(
            chrono::DateTime::parse_from_rfc3339(report["clock"]["now"].as_str().unwrap()).is_ok(),
            "{report}"
        );
    }
    let refused = dir.path().join("refused.xlsx");
    std::fs::write(&refused, b"PK\x03\x04 not really a zip").unwrap();
    let (code, report) = recalc(&refused, &["--now", "2026-01-31T09:00:00Z", "--seed", "1"]);
    assert!(code == 1 || code == 2, "{report}");
    assert_eq!(report["clock"], Value::Null);
    assert_eq!(report["seed"], Value::Null);
}

/// The reported instant, read in the reported zone, is what TODAY/NOW cached,
/// and `--now <clock.now>` (plus `--tz` for a fixed zone) replays the run.
#[cfg(feature = "system-clock")]
#[test]
fn system_clock_run_replays_byte_identically_from_its_echo() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    std::fs::write(&input, volatile_book()).unwrap();
    for extra in [vec![], vec!["--tz", "+05:30"], vec!["--tz", "UTC"]] {
        let first = dir.path().join("first.xlsx");
        let mut args = vec!["-o", first.to_str().unwrap()];
        args.extend(&extra);
        let (code, report) = recalc(&input, &args);
        assert_eq!(code, 0, "{report}");
        let clock = &report["clock"];
        assert_eq!(clock["fixed"], false);
        let now = clock["now"].as_str().unwrap();
        let seen = chrono::DateTime::parse_from_rfc3339(now).unwrap();
        if let [_, tz] = extra.as_slice() {
            assert_eq!(clock["timezone"], *tz);
            let offset = if *tz == "UTC" { "Z" } else { tz };
            assert!(now.ends_with(offset), "{now}");
        } else {
            assert_eq!(clock["timezone"], "Local");
        }
        assert_clock(&first, seen.naive_local(), now);
        let replay = dir.path().join("replay.xlsx");
        let seed = report["seed"].to_string();
        let mut args = vec![
            "-o",
            replay.to_str().unwrap(),
            "--now",
            now,
            "--seed",
            &seed,
        ];
        let tz = clock["timezone"].as_str().unwrap();
        if tz != "Local" {
            args.extend(["--tz", tz]);
        }
        let (code, replayed) = recalc(&input, &args);
        assert_eq!(code, 0, "{replayed}");
        assert_eq!(replayed["clock"]["now"], now);
        assert_eq!(replayed["clock"]["fixed"], true);
        assert_eq!(
            std::fs::read(&first).unwrap(),
            std::fs::read(&replay).unwrap(),
            "{extra:?}"
        );
    }
}

/// Builds without a system clock refuse TODAY/NOW, unless `--now` fixes it.
#[cfg(not(feature = "system-clock"))]
#[test]
fn now_admits_today_and_now_without_a_system_clock() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("in.xlsx");
    let bytes = volatile_book();
    std::fs::write(&input, &bytes).unwrap();
    let (code, report) = recalc(&input, &[]);
    assert_eq!(code, 2, "{report}");
    assert!(
        report["refusal"]["feature"]
            .as_str()
            .unwrap()
            .contains("wall clock")
    );
    assert_eq!(report["clock"], Value::Null);
    assert_eq!(std::fs::read(&input).unwrap(), bytes);
    let (code, report) = recalc(&input, &["--now", "2026-03-01T23:30:00-01:00"]);
    assert_eq!(code, 0, "{report}");
    assert_clock(&input, at("2026-03-01 23:30"), "portable");
    assert_eq!(report["clock"]["now"], "2026-03-01T23:30:00-01:00");
    assert_eq!(report["clock"]["fixed"], true);
}

fn assert_snapshot(name: &str, actual: &str) {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/snapshots")
        .join(name);
    if std::env::var_os("UPDATE_SNAPSHOTS").is_some() {
        std::fs::write(&path, actual).unwrap();
    }
    let expected = std::fs::read_to_string(&path).unwrap_or_default();
    assert_eq!(
        actual,
        expected,
        "{} differs; rerun with UPDATE_SNAPSHOTS=1 to accept",
        path.display()
    );
}

#[test]
fn help_text_snapshots() {
    for (args, name) in [
        (vec!["--help"], "help.txt"),
        (vec!["recalc", "--help"], "recalc-help.txt"),
    ] {
        let (code, out, err) = invoke(&args);
        assert_eq!(code, 0);
        assert!(err.is_empty());
        assert_snapshot(name, &out);
        // The docs site reproduces the help verbatim.
        let page = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../docs-site/content/docs/recalc-cli/cli-reference.mdx");
        if let Ok(site) = std::fs::read_to_string(&page) {
            let shown = format!("$ formualizer {}\n{out}", args.join(" "));
            assert!(site.contains(&shown), "{} is out of date", page.display());
        }
    }
    let (_, recalc_help, _) = invoke(&["help", "recalc"]);
    for needle in [
        "in place",
        "last step",
        "64",
        "130",
        "--now",
        "--tz",
        "--seed",
    ] {
        assert!(recalc_help.contains(needle), "{needle}");
    }
}