ferriki 1.2.0

Reusable native Rust syntax highlighter with TextMate grammars
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
671
672
673
674
//! Cache planning for the Node host. Network I/O stays in Node's fetch runtime.

use std::borrow::Cow;
use std::collections::{BTreeSet, HashMap};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use ferriki_asset_gen::{ReleaseManifest, decode_language_manifest, decode_theme_manifest};

use crate::asset_settings::{AssetSettingsInput, ResolvedAssetSettings, resolve_asset_settings};
use crate::{
    AssetDigest, AssetMetadata, AssetSource, Error, ErrorKind, Result, StandardAssetCatalogs,
};

/// Settings supplied by the private N-API host. Unset fields use the same
/// environment and platform defaults as the Rust `remote` feature.
#[derive(Clone, Debug, Default)]
pub struct NodeAssetOptions {
    pub regex_prefilter: Option<bool>,
    pub remote: Option<bool>,
    pub base_url: Option<String>,
    pub cache_dir: Option<PathBuf>,
}

#[derive(Debug)]
pub struct PlannedAsset {
    pub path: String,
    pub digest: String,
    pub size: u64,
    pub url: String,
}

/// Manifest-backed Node asset host. It only plans downloads and reads the
/// digest-addressed cache; JavaScript owns transport and cache writes.
pub struct NodeAssetHost {
    precompiled: bool,
    release: ReleaseManifest,
    settings: ResolvedAssetSettings,
    language_manifest: Vec<u8>,
    theme_manifest: Vec<u8>,
    languages: HashMap<String, (String, Vec<String>)>,
    language_aliases: HashMap<String, String>,
    themes: HashMap<String, String>,
}

impl NodeAssetHost {
    /// Reads the packaged catalog manifests and release manifest below `root`.
    pub fn from_root(root: &Path, options: &NodeAssetOptions) -> Result<Self> {
        let read = |relative: &str| {
            std::fs::read(root.join(relative)).map_err(|source| {
                Error::new(
                    ErrorKind::AssetIo,
                    format!("Cannot read `{}`.", root.join(relative).display()),
                )
                .with_source(source)
            })
        };
        let language_manifest = read("languages/manifest.fkindex")?;
        let theme_manifest = read("themes/manifest.fkindex")?;
        let release = ReleaseManifest::from_json(&String::from_utf8_lossy(&read(
            ferriki_asset_gen::RELEASE_MANIFEST_FILE,
        )?))
        .map_err(|source| {
            Error::new(
                ErrorKind::AssetFormat,
                "Failed to parse the release manifest.",
            )
            .with_source(source)
        })?;
        let format = |source| {
            Error::new(
                ErrorKind::AssetFormat,
                "Failed to decode a catalog manifest.",
            )
            .with_source(source)
        };
        let mut languages = HashMap::new();
        let mut language_aliases = HashMap::new();
        for entry in decode_language_manifest(&language_manifest)
            .map_err(format)?
            .entries
        {
            let path = catalog_path("languages", &entry.asset_file)?;
            for alias in &entry.aliases {
                language_aliases.insert(alias.clone(), entry.id.clone());
            }
            languages.insert(entry.id, (path, entry.embedded_langs));
        }
        let themes = decode_theme_manifest(&theme_manifest)
            .map_err(format)?
            .entries
            .into_iter()
            .map(|entry| Ok((entry.id, catalog_path("themes", &entry.asset_file)?)))
            .collect::<Result<_>>()?;
        let settings = resolve_asset_settings(
            AssetSettingsInput {
                remote: options.remote,
                base_url: options.base_url.clone(),
                cache_dir: options.cache_dir.clone(),
                commit: None,
                release_commit: release.commit.clone(),
                compiled_commit: option_env!("FERRIKI_RELEASE_COMMIT"),
                cache_hint: "set FERRIKI_CACHE_DIR or pass an explicit Node assets.cacheDir",
            },
            |key| std::env::var(key).ok(),
        )?;

        Ok(Self {
            precompiled: options.regex_prefilter.unwrap_or(true),
            release,
            settings,
            language_manifest,
            theme_manifest,
            languages,
            language_aliases,
            themes,
        })
    }

    /// Catalogs read only the verified digest-addressed cache. Missing bytes
    /// stay an `AssetUnavailable` error on synchronous Node paths.
    pub fn catalogs(&self) -> Result<StandardAssetCatalogs> {
        let assets_by_digest = self
            .release
            .assets
            .iter()
            .map(|(path, asset)| {
                Ok((
                    asset.sha256.parse::<AssetDigest>()?,
                    NodeCachedAsset {
                        path: path.clone(),
                        size: asset.size,
                    },
                ))
            })
            .collect::<Result<HashMap<_, _>>>()?;
        StandardAssetCatalogs::from_release_manifest(
            &self.language_manifest,
            &self.theme_manifest,
            &self.release,
            NodeAssetSource {
                cache_dir: self.settings.cache_dir.clone(),
                assets_by_digest,
            },
        )
    }

    pub fn cache_dir(&self) -> &Path {
        &self.settings.cache_dir
    }

    /// Plans cache misses without network access or payload decoding.
    pub fn plan(&self, languages: &[String], themes: &[String]) -> Result<Vec<PlannedAsset>> {
        let prepared_roots: BTreeSet<&str> = languages
            .iter()
            .map(|requested| {
                self.language_aliases
                    .get(requested.as_str())
                    .map_or(requested.as_str(), String::as_str)
            })
            .collect();
        let mut paths = BTreeSet::new();
        let mut pending: Vec<&str> = languages.iter().map(String::as_str).collect();
        let mut visited = BTreeSet::new();
        while let Some(requested) = pending.pop() {
            let id = self
                .language_aliases
                .get(requested)
                .map_or(requested, String::as_str);
            let Some((path, embedded)) = self.languages.get(id) else {
                continue;
            };
            if !visited.insert(id) {
                continue;
            }
            paths.insert(path.clone());
            let bytecode = path.replace(".fkgram", ".fkbc");
            if self.precompiled
                && prepared_roots.contains(id)
                && self.release.assets.get(&bytecode).is_some_and(|asset| {
                    asset.format_version == ferriki_textmate::PRECOMPILED_PATTERNS_VERSION
                })
            {
                paths.insert(bytecode);
            }
            pending.extend(embedded.iter().map(String::as_str));
        }
        paths.extend(
            themes
                .iter()
                .filter_map(|theme| self.themes.get(theme).cloned()),
        );

        let mut result = Vec::new();
        for path in paths {
            let optional = path.ends_with(".fkbc");
            let asset = self.release.assets.get(&path).ok_or_else(|| {
                Error::new(
                    ErrorKind::AssetFormat,
                    format!("Release metadata is missing `{path}`."),
                )
            })?;
            let digest = AssetDigest::from_str(&asset.sha256)?;
            let metadata = AssetMetadata::new(digest.clone(), asset.size, asset.format_version);
            let cache_path = self.settings.cache_dir.join(digest.as_str());
            let cached = match read_cache_entry(&cache_path, digest.as_str(), asset.size) {
                Ok(cached) => cached,
                Err(_) if optional => continue,
                Err(error) => return Err(error),
            };
            let corrupt = match cached {
                CacheRead::Bytes(bytes) => match if optional {
                    metadata.verify_bytes(&bytes)
                } else {
                    metadata.verify(&bytes)
                } {
                    Ok(()) => continue,
                    Err(error) if error.kind() == ErrorKind::AssetIntegrity => true,
                    Err(error) => return Err(error),
                },
                CacheRead::Missing => false,
                CacheRead::WrongSize => true,
            };
            if corrupt && let Err(error) = remove_corrupt_cache_entry(&cache_path) {
                if optional {
                    continue;
                }
                return Err(error);
            }

            if !self.settings.remote {
                if optional {
                    continue;
                }
                return Err(Error::new(
                    ErrorKind::AssetUnavailable,
                    format!(
                        "Asset {path} is not cached in {} and remote assets are turned off. \
                         Allow remote assets, point FERRIKI_ASSETS_BASE_URL at a mirror, or pre-populate the cache.",
                        self.settings.cache_dir.display()
                    ),
                ));
            }
            result.push(PlannedAsset {
                path: path.to_owned(),
                digest: digest.as_str().to_owned(),
                size: asset.size,
                url: crate::asset_settings::asset_url(
                    self.release.storage_layout.as_deref(),
                    &self.settings,
                    &path,
                    digest.as_str(),
                )?,
            });
        }
        Ok(result)
    }
}

struct NodeCachedAsset {
    path: String,
    size: u64,
}

struct NodeAssetSource {
    cache_dir: PathBuf,
    assets_by_digest: HashMap<AssetDigest, NodeCachedAsset>,
}

impl AssetSource for NodeAssetSource {
    fn read(&self, digest: &AssetDigest) -> Result<Cow<'_, [u8]>> {
        let Some(asset) = self.assets_by_digest.get(digest) else {
            return Err(Error::new(
                ErrorKind::AssetUnavailable,
                format!("Asset {} is not part of this release.", digest.as_str()),
            ));
        };
        let cache_path = self.cache_dir.join(digest.as_str());
        match read_cache_entry(&cache_path, digest.as_str(), asset.size)? {
            CacheRead::Bytes(bytes) => Ok(Cow::Owned(bytes)),
            CacheRead::WrongSize => Err(Error::new(
                ErrorKind::AssetIntegrity,
                format!(
                    "Asset {} failed its SHA-256 or byte-size check.",
                    digest.as_str()
                ),
            )),
            CacheRead::Missing => Err(Error::new(
                ErrorKind::AssetUnavailable,
                format!(
                    "Asset {} is not cached in {}. Load it through the asynchronous \
                     createHighlighter, loadLanguage or loadTheme path first, or pre-populate the cache.",
                    asset.path,
                    self.cache_dir.display()
                ),
            )),
        }
    }
}

enum CacheRead {
    Missing,
    WrongSize,
    Bytes(Vec<u8>),
}

/// Checks the opened file's manifest-pinned size before allocating its bytes,
/// then caps the read in case the cache entry grows after the metadata check.
fn read_cache_entry(path: &Path, digest: &str, expected_size: u64) -> Result<CacheRead> {
    let file = match std::fs::File::open(path) {
        Ok(file) => file,
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => {
            return Ok(CacheRead::Missing);
        }
        Err(source) => {
            return Err(Error::new(
                ErrorKind::AssetIo,
                format!("Cannot read asset {digest} from the cache."),
            )
            .with_source(source));
        }
    };
    let file_metadata = file.metadata().map_err(|source| {
        Error::new(
            ErrorKind::AssetIo,
            format!("Cannot read asset {digest} from the cache."),
        )
        .with_source(source)
    })?;
    if !file_metadata.is_file() {
        return Err(Error::new(
            ErrorKind::AssetIo,
            format!("Cannot read asset {digest} from the cache: expected a regular file."),
        ));
    }
    if file_metadata.len() != expected_size {
        drop(file);
        return Ok(CacheRead::WrongSize);
    }

    let capacity = usize::try_from(expected_size).map_err(|source| {
        Error::new(
            ErrorKind::AssetIo,
            format!("Cannot read asset {digest} from the cache."),
        )
        .with_source(source)
    })?;
    let mut bytes = Vec::new();
    bytes.try_reserve_exact(capacity).map_err(|source| {
        Error::new(
            ErrorKind::AssetIo,
            format!("Cannot allocate cached asset {digest}."),
        )
        .with_source(source)
    })?;
    let mut reader = file.take(expected_size.saturating_add(1));
    reader.read_to_end(&mut bytes).map_err(|source| {
        Error::new(
            ErrorKind::AssetIo,
            format!("Cannot read asset {digest} from the cache."),
        )
        .with_source(source)
    })?;
    drop(reader);
    if bytes.len() as u64 != expected_size {
        return Ok(CacheRead::WrongSize);
    }
    Ok(CacheRead::Bytes(bytes))
}

fn catalog_path(catalog: &str, file: &str) -> Result<String> {
    if file.is_empty() || file == "." || file == ".." || file.contains('/') || file.contains('\\') {
        return Err(Error::new(
            ErrorKind::AssetFormat,
            format!("Invalid Ferriki asset filename `{file}`."),
        ));
    }
    Ok(format!("{catalog}/{file}"))
}

fn remove_corrupt_cache_entry(path: &Path) -> Result<()> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        // Concurrent planners may both observe the same corrupt entry before
        // either removes it. The first removal is enough for both to refetch.
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(source) => Err(Error::new(
            ErrorKind::AssetIo,
            format!("Cannot replace corrupt cached asset `{}`.", path.display()),
        )
        .with_source(source)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Value;
    use std::sync::atomic::{AtomicUsize, Ordering};

    static NEXT_TEMP: AtomicUsize = AtomicUsize::new(0);

    fn temp_dir(label: &str) -> PathBuf {
        std::env::temp_dir().join(format!(
            "ferriki-node-assets-{label}-{}-{}",
            std::process::id(),
            NEXT_TEMP.fetch_add(1, Ordering::Relaxed)
        ))
    }

    fn fixture_root() -> (PathBuf, PathBuf) {
        let source = Path::new(env!("CARGO_MANIFEST_DIR")).join("assets/standard");
        let root = temp_dir("root");
        std::fs::create_dir_all(root.join("languages")).expect("language directory");
        std::fs::create_dir_all(root.join("themes")).expect("theme directory");
        for relative in ["languages/manifest.fkindex", "themes/manifest.fkindex"] {
            std::fs::copy(source.join(relative), root.join(relative)).expect("copy manifest");
        }
        let mut release: Value = serde_json::from_slice(
            &std::fs::read(source.join(ferriki_asset_gen::RELEASE_MANIFEST_FILE))
                .expect("release manifest"),
        )
        .expect("release JSON");
        release["commit"] = Value::String("0123456789abcdef0123456789abcdef01234567".to_owned());
        std::fs::write(
            root.join(ferriki_asset_gen::RELEASE_MANIFEST_FILE),
            serde_json::to_vec(&release).expect("serialize release"),
        )
        .expect("write release manifest");
        (source, root)
    }

    fn host(root: &Path, cache_dir: &Path) -> NodeAssetHost {
        NodeAssetHost::from_root(
            root,
            &NodeAssetOptions {
                regex_prefilter: None,
                remote: Some(true),
                base_url: Some("https://mirror.example/".to_owned()),
                cache_dir: Some(cache_dir.to_path_buf()),
            },
        )
        .expect("asset host")
    }

    #[test]
    fn plans_embedded_alias_assets_and_skips_verified_cache_entries() {
        let (source, root) = fixture_root();
        let cache = temp_dir("cache");
        std::fs::create_dir_all(&cache).expect("cache directory");
        let host = host(&root, &cache);

        let nord = host
            .release
            .assets
            .get("themes/nord.fktheme")
            .expect("nord metadata");
        std::fs::copy(source.join("themes/nord.fktheme"), cache.join(&nord.sha256))
            .expect("seed verified cache entry");
        let vue = host
            .release
            .assets
            .get("languages/vue.fkgram")
            .expect("vue metadata");
        std::fs::write(cache.join(&vue.sha256), b"corrupt").expect("write corrupt cache entry");

        let plan = host
            .plan(
                &["vue".to_owned(), "vue".to_owned(), "ts".to_owned()],
                &["nord".to_owned()],
            )
            .expect("download plan");
        let paths: BTreeSet<_> = plan.iter().map(|asset| asset.path.as_str()).collect();
        let mut disabled = self::host(&root, &cache);
        disabled.precompiled = false;
        assert!(
            disabled
                .plan(&["vue".to_owned()], &[])
                .unwrap()
                .iter()
                .all(|asset| !asset.path.ends_with(".fkbc")),
            "Off should not download unused Eager group images"
        );

        assert!(paths.contains("languages/vue.fkgram"));
        assert!(paths.contains("languages/typescript.fkgram"));
        let vue_only = host.plan(&["vue".to_owned()], &[]).expect("root-only plan");
        assert!(
            vue_only
                .iter()
                .any(|asset| asset.path == "languages/typescript.fkgram")
        );
        assert!(
            !vue_only
                .iter()
                .any(|asset| asset.path == "languages/typescript.fkbc"),
            "the root archive already carries dependency groups"
        );
        assert!(paths.contains("languages/vue.fkbc"));
        assert!(paths.contains("languages/typescript.fkbc"));
        assert!(!paths.contains("themes/nord.fktheme"));
        assert_eq!(
            plan.len(),
            paths.len(),
            "aliases and repeat requests deduplicate"
        );
        assert!(
            !cache.join(&vue.sha256).exists(),
            "corrupt bytes are removed for refetch"
        );
        for asset in &plan {
            let file = asset.path.strip_prefix("languages/").unwrap();
            let (prefix, id) = if let Some(id) = file.strip_suffix(".fkbc") {
                ("", id)
            } else {
                ("", file.strip_suffix(".fkgram").unwrap())
            };
            assert_eq!(
                asset.url,
                format!(
                    "https://mirror.example/{prefix}{id}/{}",
                    crate::asset_settings::short_hash(&asset.digest).unwrap()
                )
            );
        }

        let _ = std::fs::remove_dir_all(&root);
        let _ = std::fs::remove_dir_all(&cache);
    }

    #[cfg(unix)]
    #[test]
    fn optional_corruption_in_a_read_only_cache_keeps_valid_grammar_usable() {
        use std::os::unix::fs::PermissionsExt;
        let (source, root) = fixture_root();
        let cache = temp_dir("read-only-optional");
        std::fs::create_dir_all(&cache).unwrap();
        let host = host(&root, &cache);
        let grammar = &host.release.assets["languages/json.fkgram"];
        let bytecode = &host.release.assets["languages/json.fkbc"];
        std::fs::copy(
            source.join("languages/json.fkgram"),
            cache.join(&grammar.sha256),
        )
        .unwrap();
        let expected = std::fs::read(source.join("languages/json.fkbc")).unwrap();
        let mut same_size_corrupt = expected;
        same_size_corrupt[0] ^= 0xff;
        for invalid in [b"truncated".to_vec(), same_size_corrupt] {
            let entry = cache.join(&bytecode.sha256);
            std::fs::write(&entry, invalid).unwrap();
            std::fs::set_permissions(&cache, std::fs::Permissions::from_mode(0o555)).unwrap();
            let result = host.plan(&["json".to_owned()], &[]);
            std::fs::set_permissions(&cache, std::fs::Permissions::from_mode(0o755)).unwrap();
            let plan = result.expect("an unusable optional entry must not block a valid grammar");
            if entry.exists() {
                assert!(
                    plan.is_empty(),
                    "an unrepairable optional entry falls back to source"
                );
            } else {
                // Privileged test processes can repair even mode-0555 directories.
                assert_eq!(plan.len(), 1);
                assert!(plan[0].path.ends_with(".fkbc"));
            }
        }
        std::fs::remove_dir_all(cache).unwrap();
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn planner_rejects_wrong_size_cache_entries_before_reading_them() {
        let (source, root) = fixture_root();
        let cache = temp_dir("wrong-size-cache");
        std::fs::create_dir_all(&cache).expect("cache directory");
        let host = host(&root, &cache);
        let asset = host
            .release
            .assets
            .get("themes/nord.fktheme")
            .expect("nord metadata");
        let cache_path = cache.join(&asset.sha256);

        let sparse = std::fs::File::create(&cache_path).expect("cache entry");
        sparse
            .set_len(asset.size + 64 * 1024 * 1024)
            .expect("sparse oversized cache entry");
        drop(sparse);

        let plan = host
            .plan(&[], &["nord".to_owned()])
            .expect("oversized cache entry becomes a miss");
        assert_eq!(plan.len(), 1);
        assert_eq!(plan[0].path, "themes/nord.fktheme");
        assert!(
            !cache_path.exists(),
            "oversized entry is removed for refetch"
        );

        let expected = std::fs::read(source.join("themes/nord.fktheme")).expect("theme bytes");
        std::fs::write(&cache_path, &expected[..expected.len() - 1])
            .expect("truncated cache entry");
        let plan = host
            .plan(&[], &["nord".to_owned()])
            .expect("truncated cache entry becomes a miss");
        assert_eq!(plan.len(), 1);
        assert!(
            !cache_path.exists(),
            "truncated entry is removed for refetch"
        );

        let mut same_size_corrupt = expected;
        same_size_corrupt[0] ^= 0xff;
        std::fs::write(&cache_path, &same_size_corrupt).expect("same-size corrupt cache entry");
        let plan = host
            .plan(&[], &["nord".to_owned()])
            .expect("digest mismatch becomes a miss");
        assert_eq!(plan.len(), 1);
        assert!(
            !cache_path.exists(),
            "digest-mismatched entry is removed for refetch"
        );

        let _ = std::fs::remove_dir_all(&root);
        let _ = std::fs::remove_dir_all(&cache);
    }

    #[test]
    fn synchronous_node_cache_reads_bound_size_and_load_valid_assets() {
        let (source, root) = fixture_root();
        let valid_cache = temp_dir("sync-valid-cache");
        std::fs::create_dir_all(&valid_cache).expect("valid cache directory");
        let asset_path = "themes/nord.fktheme";
        let bytes = std::fs::read(source.join(asset_path)).expect("theme bytes");
        let asset = host(&root, &valid_cache)
            .release
            .assets
            .get(asset_path)
            .expect("nord metadata")
            .clone();
        std::fs::write(valid_cache.join(&asset.sha256), &bytes).expect("valid cache entry");

        let catalogs = host(&root, &valid_cache).catalogs().expect("catalogs");
        catalogs
            .themes
            .load_asset("nord")
            .expect("verified cached theme");

        let oversized_cache = temp_dir("sync-oversized-cache");
        std::fs::create_dir_all(&oversized_cache).expect("oversized cache directory");
        let cache_path = oversized_cache.join(&asset.sha256);
        let sparse = std::fs::File::create(&cache_path).expect("cache entry");
        sparse
            .set_len(asset.size + 64 * 1024 * 1024)
            .expect("sparse oversized cache entry");
        drop(sparse);

        let catalogs = host(&root, &oversized_cache).catalogs().expect("catalogs");
        let error = catalogs
            .themes
            .load_asset("nord")
            .expect_err("oversized cache entry fails before decoding");
        assert_eq!(error.kind(), ErrorKind::AssetIntegrity);
        assert!(
            cache_path.exists(),
            "synchronous loads leave cache repair to planning"
        );

        let _ = std::fs::remove_dir_all(&root);
        let _ = std::fs::remove_dir_all(&valid_cache);
        let _ = std::fs::remove_dir_all(&oversized_cache);
    }
}