pite-scene 0.1.0

TOML scene format, instantiation, validation and binary cache for Pite.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Binary scene cache: TOML stays source of truth; `_cache/` holds
//! content-hash-keyed, version-stamped binaries.
//!
//! Pure logic lives here (hash, paths, encode/decode, stale matching);
//! `lib.rs` owns the filesystem flow (`load_cached`).

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

use crate::{SceneDoc, SceneInstance, SceneNode, SceneScript, FORMAT_VERSION};

/// Cache container version. A bump rebuilds every cache from TOML
/// instead of misreading it.
pub const CACHE_VERSION: u32 = 1;

/// Sibling directory next to each scene file holding its binaries.
pub const CACHE_DIR_NAME: &str = "_cache";

/// Extension for cache files: `<stem>.<content-hash>.bin`.
pub const CACHE_EXT: &str = "bin";

/// Magic prefix of every cache file; anything else is corruption.
const MAGIC: &[u8; 8] = b"PITSCN01";

/// Header carried by every cache file: which container and scene
/// versions it was written with, plus the exact source it came from.
#[derive(Debug, Clone, PartialEq)]
pub struct CacheHeader {
    pub cache_version: u32,
    pub format_version: u32,
    pub source_len: u64,
    pub source_hash: u64,
}

/// How a scene load resolved: cache hit, clean miss (no usable cache),
/// or rebuild (cache existed but was stale/corrupt/mismatched).
#[derive(Debug, Clone, PartialEq)]
pub enum CacheStatus {
    Hit,
    Miss,
    Rebuilt(String),
}

impl std::fmt::Display for CacheStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CacheStatus::Hit => write!(f, "hit"),
            CacheStatus::Miss => write!(f, "miss"),
            CacheStatus::Rebuilt(reason) => write!(f, "rebuilt ({reason})"),
        }
    }
}

/// FNV-1a 64-bit over bytes (same function as `pite-assets`, zero deps).
pub fn fnv1a_u64(bytes: &[u8]) -> u64 {
    let mut hash: u64 = 14695981039346656037;
    for b in bytes {
        hash ^= u64::from(*b);
        hash = hash.wrapping_mul(1099511628211);
    }
    hash
}

/// Lowercase hex content hash used in cache filenames.
pub fn content_hash(bytes: &[u8]) -> String {
    format!("{:016x}", fnv1a_u64(bytes))
}

/// Cache path for a scene file + hex content hash:
/// `<scene_dir>/_cache/<stem>.<hash>.bin`. The hash keys the cache,
/// so any source change is a different file (old ones get pruned).
pub fn cache_path_for(scene_path: &Path, source_hash_hex: &str) -> PathBuf {
    let dir = scene_path.parent().unwrap_or_else(|| Path::new("."));
    let stem = scene_path
        .file_stem()
        .and_then(|s| s.to_str())
        .filter(|s| !s.is_empty())
        .unwrap_or("scene");
    dir.join(CACHE_DIR_NAME)
        .join(format!("{stem}.{source_hash_hex}.{CACHE_EXT}"))
}

/// Remove sibling caches for `scene_path` whose hash differs from
/// `keep_hash_hex`; returns removed paths. Best-effort: unreadable
/// dirs or failed removals are ignored, never errors.
pub fn prune_stale(scene_path: &Path, keep_hash_hex: &str) -> Vec<PathBuf> {
    let dir = scene_path
        .parent()
        .unwrap_or_else(|| Path::new("."))
        .join(CACHE_DIR_NAME);
    let stem = scene_path
        .file_stem()
        .and_then(|s| s.to_str())
        .filter(|s| !s.is_empty())
        .unwrap_or("scene");
    let keep = format!("{stem}.{keep_hash_hex}.{CACHE_EXT}");
    let prefix = format!("{stem}.");
    let suffix = format!(".{CACHE_EXT}");
    let mut removed = Vec::new();
    let Ok(entries) = std::fs::read_dir(&dir) else {
        return removed;
    };
    for entry in entries.flatten() {
        let name = entry.file_name().to_string_lossy().into_owned();
        if name == keep || !name.starts_with(&prefix) || !name.ends_with(&suffix) {
            continue;
        }
        let path = entry.path();
        if std::fs::remove_file(&path).is_ok() {
            removed.push(path);
        }
    }
    removed.sort();
    removed
}

fn put_u32(out: &mut Vec<u8>, v: u32) {
    out.extend_from_slice(&v.to_le_bytes());
}

fn put_u64(out: &mut Vec<u8>, v: u64) {
    out.extend_from_slice(&v.to_le_bytes());
}

fn put_i64(out: &mut Vec<u8>, v: i64) {
    out.extend_from_slice(&v.to_le_bytes());
}

fn put_f64(out: &mut Vec<u8>, v: f64) {
    out.extend_from_slice(&v.to_le_bytes());
}

fn put_str(out: &mut Vec<u8>, s: &str) {
    put_u32(out, s.len() as u32);
    out.extend_from_slice(s.as_bytes());
}

fn put_opt_str(out: &mut Vec<u8>, value: &Option<String>) {
    out.push(u8::from(value.is_some()));
    if let Some(s) = value {
        put_str(out, s);
    }
}

/// `toml::Value` tags. Tables sort by key on write (they already do:
/// `toml::Map` is a `BTreeMap`), so encoding is deterministic.
fn put_toml_value(out: &mut Vec<u8>, value: &toml::Value) {
    match value {
        toml::Value::String(s) => {
            out.push(0);
            put_str(out, s);
        }
        toml::Value::Integer(i) => {
            out.push(1);
            put_i64(out, *i);
        }
        toml::Value::Float(f) => {
            out.push(2);
            put_f64(out, *f);
        }
        toml::Value::Boolean(b) => {
            out.push(3);
            out.push(u8::from(*b));
        }
        toml::Value::Array(items) => {
            out.push(4);
            put_u32(out, items.len() as u32);
            for item in items {
                put_toml_value(out, item);
            }
        }
        toml::Value::Table(table) => {
            out.push(5);
            put_u32(out, table.len() as u32);
            for (k, v) in table {
                put_str(out, k);
                put_toml_value(out, v);
            }
        }
        toml::Value::Datetime(dt) => {
            out.push(6);
            put_str(out, &dt.to_string());
        }
    }
}

fn put_script(out: &mut Vec<u8>, script: &Option<SceneScript>) {
    out.push(u8::from(script.is_some()));
    if let Some(s) = script {
        put_str(out, &s.path);
        put_str(out, &s.class);
    }
}

fn put_node(out: &mut Vec<u8>, node: &SceneNode) {
    put_str(out, &node.id);
    put_str(out, &node.type_name);
    put_str(out, &node.name);
    put_opt_str(out, &node.parent);
    let mut keys: Vec<&String> = node.props.keys().collect();
    keys.sort();
    put_u32(out, keys.len() as u32);
    for k in keys {
        put_str(out, k);
        put_toml_value(out, &node.props[k]);
    }
    put_script(out, &node.script);
}

fn put_instance(out: &mut Vec<u8>, inst: &SceneInstance) {
    put_str(out, &inst.scene);
    put_opt_str(out, &inst.parent);
    put_str(out, &inst.prefix);
    let mut keys: Vec<&String> = inst.overrides.keys().collect();
    keys.sort();
    put_u32(out, keys.len() as u32);
    for k in keys {
        put_str(out, k);
        put_toml_value(out, &inst.overrides[k]);
    }
}

fn put_doc(out: &mut Vec<u8>, doc: &SceneDoc) {
    put_u32(out, doc.format_version);
    put_str(out, &doc.root);
    put_u32(out, doc.node.len() as u32);
    for node in &doc.node {
        put_node(out, node);
    }
    put_u32(out, doc.instance.len() as u32);
    for inst in &doc.instance {
        put_instance(out, inst);
    }
}

/// Encode a scene document as a cache payload stamped with the live
/// [`CACHE_VERSION`] and [`FORMAT_VERSION`].
pub fn encode(doc: &SceneDoc, source_hash: u64, source_len: u64) -> Vec<u8> {
    encode_with(doc, source_hash, source_len, CACHE_VERSION, FORMAT_VERSION)
}

/// Encode with explicit stamps (test hook: simulates version bumps
/// without touching the live constants).
pub fn encode_with(
    doc: &SceneDoc,
    source_hash: u64,
    source_len: u64,
    cache_version: u32,
    format_version: u32,
) -> Vec<u8> {
    let mut out = Vec::new();
    out.extend_from_slice(MAGIC);
    put_u32(&mut out, cache_version);
    put_u32(&mut out, format_version);
    put_u64(&mut out, source_len);
    put_u64(&mut out, source_hash);
    put_doc(&mut out, doc);
    out
}

struct Cursor<'a> {
    b: &'a [u8],
    pos: usize,
}

impl<'a> Cursor<'a> {
    fn take(&mut self, n: usize) -> Result<&'a [u8]> {
        let end = self
            .pos
            .checked_add(n)
            .context("scene cache is corrupt (truncated)")?;
        if end > self.b.len() {
            anyhow::bail!("scene cache is corrupt (truncated)");
        }
        let slice = &self.b[self.pos..end];
        self.pos = end;
        Ok(slice)
    }

    fn u8(&mut self) -> Result<u8> {
        Ok(self.take(1)?[0])
    }

    fn u32(&mut self) -> Result<u32> {
        let b: [u8; 4] = self.take(4)?.try_into().expect("take(4) is 4 bytes");
        Ok(u32::from_le_bytes(b))
    }

    fn u64(&mut self) -> Result<u64> {
        let b: [u8; 8] = self.take(8)?.try_into().expect("take(8) is 8 bytes");
        Ok(u64::from_le_bytes(b))
    }

    fn i64(&mut self) -> Result<i64> {
        let b: [u8; 8] = self.take(8)?.try_into().expect("take(8) is 8 bytes");
        Ok(i64::from_le_bytes(b))
    }

    fn f64(&mut self) -> Result<f64> {
        let b: [u8; 8] = self.take(8)?.try_into().expect("take(8) is 8 bytes");
        Ok(f64::from_le_bytes(b))
    }

    fn str(&mut self) -> Result<String> {
        let len = self.u32()? as usize;
        let bytes = self.take(len)?;
        String::from_utf8(bytes.to_vec()).context("scene cache is corrupt (bad UTF-8)")
    }

    fn opt_str(&mut self) -> Result<Option<String>> {
        match self.u8()? {
            0 => Ok(None),
            1 => Ok(Some(self.str()?)),
            tag => anyhow::bail!("scene cache is corrupt (bad option tag {tag})"),
        }
    }
}

fn get_toml_value(cur: &mut Cursor<'_>) -> Result<toml::Value> {
    match cur.u8()? {
        0 => Ok(toml::Value::String(cur.str()?)),
        1 => Ok(toml::Value::Integer(cur.i64()?)),
        2 => Ok(toml::Value::Float(cur.f64()?)),
        3 => Ok(toml::Value::Boolean(match cur.u8()? {
            0 => false,
            1 => true,
            tag => anyhow::bail!("scene cache is corrupt (bad bool tag {tag})"),
        })),
        4 => {
            let len = cur.u32()? as usize;
            let mut items = Vec::with_capacity(len.min(1024));
            for _ in 0..len {
                items.push(get_toml_value(cur)?);
            }
            Ok(toml::Value::Array(items))
        }
        5 => {
            let len = cur.u32()? as usize;
            let mut table = toml::map::Map::new();
            for _ in 0..len {
                let k = cur.str()?;
                let v = get_toml_value(cur)?;
                table.insert(k, v);
            }
            Ok(toml::Value::Table(table))
        }
        6 => {
            let raw = cur.str()?;
            let dt: toml::value::Datetime = raw
                .parse()
                .context("scene cache is corrupt (bad datetime)")?;
            Ok(toml::Value::Datetime(dt))
        }
        tag => anyhow::bail!("scene cache is corrupt (bad value tag {tag})"),
    }
}

fn get_script(cur: &mut Cursor<'_>) -> Result<Option<SceneScript>> {
    match cur.u8()? {
        0 => Ok(None),
        1 => Ok(Some(SceneScript {
            path: cur.str()?,
            class: cur.str()?,
        })),
        tag => anyhow::bail!("scene cache is corrupt (bad script tag {tag})"),
    }
}

fn get_node(cur: &mut Cursor<'_>) -> Result<SceneNode> {
    let id = cur.str()?;
    let type_name = cur.str()?;
    let name = cur.str()?;
    let parent = cur.opt_str()?;
    let prop_len = cur.u32()? as usize;
    let mut props = std::collections::BTreeMap::new();
    for _ in 0..prop_len {
        let k = cur.str()?;
        let v = get_toml_value(cur)?;
        props.insert(k, v);
    }
    let script = get_script(cur)?;
    Ok(SceneNode {
        id,
        type_name,
        name,
        parent,
        props,
        script,
    })
}

fn get_instance(cur: &mut Cursor<'_>) -> Result<SceneInstance> {
    let scene = cur.str()?;
    let parent = cur.opt_str()?;
    let prefix = cur.str()?;
    let len = cur.u32()? as usize;
    let mut overrides = std::collections::BTreeMap::new();
    for _ in 0..len {
        let k = cur.str()?;
        let v = get_toml_value(cur)?;
        overrides.insert(k, v);
    }
    Ok(SceneInstance {
        scene,
        parent,
        prefix,
        overrides,
    })
}

fn get_doc(cur: &mut Cursor<'_>) -> Result<SceneDoc> {
    let format_version = cur.u32()?;
    let root = cur.str()?;
    let node_len = cur.u32()? as usize;
    let mut node = Vec::with_capacity(node_len.min(1024));
    for _ in 0..node_len {
        node.push(get_node(cur)?);
    }
    let inst_len = cur.u32()? as usize;
    let mut instance = Vec::with_capacity(inst_len.min(64));
    for _ in 0..inst_len {
        instance.push(get_instance(cur)?);
    }
    Ok(SceneDoc {
        format_version,
        root,
        node,
        instance,
    })
}

/// Decode a cache payload. Bad magic, truncation, trailing bytes, or a
/// version stamp that is not the live one all fail loudly — the caller
/// falls back to TOML and says so, never silently reinterprets.
pub fn decode(bytes: &[u8]) -> Result<(SceneDoc, CacheHeader)> {
    let mut cur = Cursor { b: bytes, pos: 0 };
    let magic = cur
        .take(MAGIC.len())
        .context("scene cache is corrupt (truncated header)")?;
    if magic != MAGIC {
        anyhow::bail!("scene cache is corrupt (bad magic)");
    }
    let header = CacheHeader {
        cache_version: cur
            .u32()
            .context("scene cache is corrupt (truncated header)")?,
        format_version: cur
            .u32()
            .context("scene cache is corrupt (truncated header)")?,
        source_len: cur
            .u64()
            .context("scene cache is corrupt (truncated header)")?,
        source_hash: cur
            .u64()
            .context("scene cache is corrupt (truncated header)")?,
    };
    if header.cache_version != CACHE_VERSION {
        anyhow::bail!(
            "scene cache has cache version {} (engine uses {CACHE_VERSION})",
            header.cache_version
        );
    }
    if header.format_version != FORMAT_VERSION {
        anyhow::bail!(
            "scene cache has scene format_version {} (engine uses {FORMAT_VERSION})",
            header.format_version
        );
    }
    let doc = get_doc(&mut cur)?;
    if cur.pos != bytes.len() {
        anyhow::bail!(
            "scene cache is corrupt ({} trailing bytes)",
            bytes.len() - cur.pos
        );
    }
    Ok((doc, header))
}

#[cfg(test)]
mod tests {
    use super::*;

    const SCENE: &str = r#"
format_version = 1
root = "root"

[[node]]
id = "root"
type = "Node2D"
name = "Main"

[[node]]
id = "player"
type = "Sprite2D"
name = "Player"
parent = "root"

[node.props]
texture = "res://assets/player.png"
position = [100.0, 200.0]
visible = true
lives = 3
speed = 1.5
tags = ["a", "b"]
stamped = 1979-05-27T07:32:00Z

[node.props.extra]
level = 2

[node.script]
path = "res://scripts/player.py"
class = "Player"

[[instance]]
scene = "res://scenes/enemy.pitescene"
parent = "root"
prefix = "e1_"

[instance.overrides]
"sprite.position" = [300.0, 120.0]
"#;

    fn tmpdir(tag: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("pite-cache-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn write_scene(dir: &Path, text: &str) -> PathBuf {
        let path = dir.join("main.pitescene");
        std::fs::write(&path, text).unwrap();
        path
    }

    fn cache_file_for(scene: &Path) -> PathBuf {
        let bytes = std::fs::read(scene).unwrap();
        cache_path_for(scene, &content_hash(&bytes))
    }

    #[test]
    fn binary_round_trip_preserves_doc() {
        let doc = crate::parse_scene_str(SCENE).unwrap();
        let bytes = std::fs::read("Cargo.toml")
            .map(|b| b.len() as u64)
            .unwrap_or(0);
        let (again, header) = decode(&encode(&doc, 0x1234, bytes)).unwrap();
        assert_eq!(doc, again);
        assert_eq!(header.cache_version, CACHE_VERSION);
        assert_eq!(header.format_version, FORMAT_VERSION);
    }

    #[test]
    fn miss_writes_cache_then_hit_reads() {
        let dir = tmpdir("miss-hit");
        let scene = write_scene(&dir, SCENE);
        let (doc, status, warning) = crate::load_cached(&scene).unwrap();
        assert_eq!(status, CacheStatus::Miss);
        assert!(
            warning.is_none(),
            "clean miss is transparent, got {warning:?}"
        );
        assert!(
            cache_file_for(&scene).is_file(),
            "miss must write the cache"
        );
        let (again, status, warning) = crate::load_cached(&scene).unwrap();
        assert_eq!(status, CacheStatus::Hit);
        assert!(warning.is_none(), "hit is transparent, got {warning:?}");
        assert_eq!(doc, again);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn source_change_invalidates_and_prunes() {
        let dir = tmpdir("invalidate");
        let scene = write_scene(&dir, SCENE);
        crate::load_cached(&scene).unwrap();
        let old_cache = cache_file_for(&scene);
        assert!(old_cache.is_file());
        let changed = format!(
            "{SCENE}\n[[node]]\nid = \"extra\"\ntype = \"Timer\"\nname = \"Extra\"\nparent = \"root\"\n"
        );
        std::fs::write(&scene, &changed).unwrap();
        let (doc, status, warning) = crate::load_cached(&scene).unwrap();
        assert_eq!(status, CacheStatus::Miss);
        assert!(
            warning.is_none(),
            "source change is a clean miss, got {warning:?}"
        );
        assert!(doc.node.iter().any(|n| n.id == "extra"));
        assert!(!old_cache.exists(), "stale hash file must be pruned");
        assert!(cache_file_for(&scene).is_file());
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn cache_version_bump_rebuilds_loudly() {
        let dir = tmpdir("cache-bump");
        let scene = write_scene(&dir, SCENE);
        crate::load_cached(&scene).unwrap();
        let cpath = cache_file_for(&scene);
        let doc = crate::parse_scene_str(SCENE).unwrap();
        let bytes = std::fs::read(&scene).unwrap();
        let stale = encode_with(
            &doc,
            fnv1a_u64(&bytes),
            bytes.len() as u64,
            CACHE_VERSION + 1,
            FORMAT_VERSION,
        );
        std::fs::write(&cpath, &stale).unwrap();
        let (again, status, warning) = crate::load_cached(&scene).unwrap();
        assert!(matches!(status, CacheStatus::Rebuilt(_)), "got {status:?}");
        let warning = warning.expect("version bump must fall back loudly");
        assert!(warning.contains("cache version"), "got {warning:?}");
        assert_eq!(doc, again);
        let (_, status, warning) = crate::load_cached(&scene).unwrap();
        assert_eq!(status, CacheStatus::Hit);
        assert!(warning.is_none());
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn format_version_bump_rebuilds_loudly() {
        let dir = tmpdir("format-bump");
        let scene = write_scene(&dir, SCENE);
        crate::load_cached(&scene).unwrap();
        let cpath = cache_file_for(&scene);
        let doc = crate::parse_scene_str(SCENE).unwrap();
        let bytes = std::fs::read(&scene).unwrap();
        let stale = encode_with(
            &doc,
            fnv1a_u64(&bytes),
            bytes.len() as u64,
            CACHE_VERSION,
            FORMAT_VERSION + 1,
        );
        std::fs::write(&cpath, &stale).unwrap();
        let (again, status, warning) = crate::load_cached(&scene).unwrap();
        assert!(matches!(status, CacheStatus::Rebuilt(_)), "got {status:?}");
        let warning = warning.expect("format bump must fall back loudly");
        assert!(warning.contains("format_version"), "got {warning:?}");
        assert_eq!(doc, again);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn corrupt_cache_falls_back_loudly() {
        let dir = tmpdir("corrupt");
        let scene = write_scene(&dir, SCENE);
        crate::load_cached(&scene).unwrap();
        let cpath = cache_file_for(&scene);
        std::fs::write(&cpath, b"definitely not a scene cache").unwrap();
        let (doc, status, warning) = crate::load_cached(&scene).unwrap();
        assert!(matches!(status, CacheStatus::Rebuilt(_)), "got {status:?}");
        let warning = warning.expect("corruption must fall back loudly");
        assert!(warning.contains("corrupt"), "got {warning:?}");
        assert_eq!(doc, crate::parse_scene_str(SCENE).unwrap());
        let (_, status, warning) = crate::load_cached(&scene).unwrap();
        assert_eq!(status, CacheStatus::Hit);
        assert!(
            warning.is_none(),
            "rewritten cache must be valid, got {warning:?}"
        );
        std::fs::remove_dir_all(&dir).ok();
    }
}