dioxus-hoverfx-core 0.1.0-alpha.3

Serializable worker-first cursor hover effect configuration for Dioxus.
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
use std::{
    borrow::Cow,
    collections::BTreeMap,
    sync::{Arc, Mutex},
    thread,
};

use serde::{Deserialize, Serialize};

use crate::{
    HOVERFX_PACKAGE_NAME, HOVERFX_PACKAGE_VERSION, HoverFxConfig, HoverFxDiagnosticVerbosity,
    HoverFxFallbackStrategy, HoverFxManifestFragment, HoverFxOutputReport, HoverFxPreset,
    HoverFxRoutePolicy, HoverFxSerializationFormat, explain_hoverfx, hoverfx_cache_key,
    hoverfx_manifest_fragment,
};

pub const DEFAULT_HOVERFX_CONFIG_ID: &str = "__DXH_CONFIG__";
pub const DEFAULT_HOVERFX_PREPAINT_STYLE_ID: &str = "__DXH_PREPAINT__";
pub const DEFAULT_HOVERFX_RUNTIME_ASSET_ID: &str = "hoverfx.runtime";
pub const DEFAULT_HOVERFX_WORKER_ASSET_ID: &str = "hoverfx.worker";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HoverFxDiagnosticContext {
    Build,
    Ssr,
    Runtime,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxDiagnosticVerbosityByContext {
    pub build: HoverFxDiagnosticVerbosity,
    pub ssr: HoverFxDiagnosticVerbosity,
    pub runtime: HoverFxDiagnosticVerbosity,
}

impl Default for HoverFxDiagnosticVerbosityByContext {
    fn default() -> Self {
        Self {
            build: HoverFxDiagnosticVerbosity::Detailed,
            ssr: HoverFxDiagnosticVerbosity::Summary,
            runtime: HoverFxDiagnosticVerbosity::Off,
        }
    }
}

impl HoverFxDiagnosticVerbosityByContext {
    pub fn for_context(&self, context: HoverFxDiagnosticContext) -> HoverFxDiagnosticVerbosity {
        match context {
            HoverFxDiagnosticContext::Build => self.build,
            HoverFxDiagnosticContext::Ssr => self.ssr,
            HoverFxDiagnosticContext::Runtime => self.runtime,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxRuntimeIds {
    pub config_id: String,
    pub prepaint_style_id: String,
    pub runtime_asset_id: String,
    pub worker_asset_id: String,
}

impl Default for HoverFxRuntimeIds {
    fn default() -> Self {
        Self {
            config_id: DEFAULT_HOVERFX_CONFIG_ID.to_string(),
            prepaint_style_id: DEFAULT_HOVERFX_PREPAINT_STYLE_ID.to_string(),
            runtime_asset_id: DEFAULT_HOVERFX_RUNTIME_ASSET_ID.to_string(),
            worker_asset_id: DEFAULT_HOVERFX_WORKER_ASSET_ID.to_string(),
        }
    }
}

impl HoverFxRuntimeIds {
    pub fn with_config_id(mut self, id: impl Into<String>) -> Self {
        self.config_id = id.into();
        self
    }

    pub fn with_prepaint_style_id(mut self, id: impl Into<String>) -> Self {
        self.prepaint_style_id = id.into();
        self
    }

    pub fn with_runtime_asset_id(mut self, id: impl Into<String>) -> Self {
        self.runtime_asset_id = id.into();
        self
    }

    pub fn with_worker_asset_id(mut self, id: impl Into<String>) -> Self {
        self.worker_asset_id = id.into();
        self
    }

    pub fn duplicate_ids(&self) -> Vec<String> {
        let mut seen = BTreeMap::<&str, usize>::new();
        for id in [
            self.config_id.as_str(),
            self.prepaint_style_id.as_str(),
            self.runtime_asset_id.as_str(),
            self.worker_asset_id.as_str(),
        ] {
            *seen.entry(id).or_default() += 1;
        }
        seen.into_iter()
            .filter_map(|(id, count)| (count > 1).then(|| id.to_string()))
            .collect()
    }

    pub fn deduped(mut self) -> Self {
        let mut counts = BTreeMap::<String, usize>::new();
        for id in [
            &mut self.config_id,
            &mut self.prepaint_style_id,
            &mut self.runtime_asset_id,
            &mut self.worker_asset_id,
        ] {
            let count = counts.entry(id.clone()).or_default();
            if *count > 0 {
                id.push('-');
                id.push_str(&(*count + 1).to_string());
            }
            *count += 1;
        }
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxAssetPaths {
    pub runtime_base_path: String,
    pub worker_base_path: String,
    pub runtime_asset_name: String,
    pub worker_asset_name: String,
}

impl Default for HoverFxAssetPaths {
    fn default() -> Self {
        Self {
            runtime_base_path: "/assets".to_string(),
            worker_base_path: "/assets".to_string(),
            runtime_asset_name: "dioxus-hoverfx.js".to_string(),
            worker_asset_name: "dioxus-hoverfx-worker.js".to_string(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HoverFxAssetBudgetCategory {
    RuntimeScript,
    WorkerScript,
    PrepaintStyle,
    ConfigJson,
    EffectCssVars,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxAssetBudgetEntry {
    pub category: HoverFxAssetBudgetCategory,
    pub bytes: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxAssetBudgetBridge {
    pub package: String,
    pub entries: Vec<HoverFxAssetBudgetEntry>,
}

impl HoverFxAssetBudgetBridge {
    pub fn total_bytes(&self) -> usize {
        self.entries.iter().map(|entry| entry.bytes).sum()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxBatchOptions {
    pub format: HoverFxSerializationFormat,
    pub deterministic_parallel: bool,
    pub sort_by_cache_key: bool,
}

impl Default for HoverFxBatchOptions {
    fn default() -> Self {
        Self {
            format: HoverFxSerializationFormat::StableJson,
            deterministic_parallel: false,
            sort_by_cache_key: true,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxBatchPayload {
    pub route: Option<String>,
    pub cache_key: String,
    pub json: String,
    pub bytes: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxBatchSerializationReport {
    pub package: String,
    pub deterministic_parallel: bool,
    pub payloads: Vec<HoverFxBatchPayload>,
    pub total_bytes: usize,
}

#[derive(Debug)]
pub struct HoverFxBorrowedView<'a> {
    pub config: &'a HoverFxConfig,
    pub policy: Cow<'a, HoverFxRoutePolicy>,
}

impl<'a> HoverFxBorrowedView<'a> {
    pub fn new(config: &'a HoverFxConfig, policy: &'a HoverFxRoutePolicy) -> Self {
        Self {
            config,
            policy: Cow::Borrowed(policy),
        }
    }

    pub fn with_owned_policy(config: &'a HoverFxConfig, policy: HoverFxRoutePolicy) -> Self {
        Self {
            config,
            policy: Cow::Owned(policy),
        }
    }

    pub fn manifest_fragment(&self) -> HoverFxManifestFragment {
        hoverfx_manifest_fragment(self.config, self.policy.as_ref())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxCssFragment {
    pub id: String,
    pub css: String,
    pub bytes: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxMotionPolicy {
    pub reduced_motion: String,
    pub view_transition: String,
    pub render_lane: String,
}

impl Default for HoverFxMotionPolicy {
    fn default() -> Self {
        Self {
            reduced_motion: "respect".to_string(),
            view_transition: "optional".to_string(),
            render_lane: "background".to_string(),
        }
    }
}

pub trait HoverFxMotionPolicyHook {
    fn apply(&self, policy: HoverFxMotionPolicy) -> HoverFxMotionPolicy;
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxRouteMetadata {
    pub route: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub cache_keys: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub packages: Vec<String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub labels: BTreeMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxOptimizerArtifact {
    pub id: String,
    pub content_hash: String,
    pub bytes: usize,
    pub minified: bool,
}

pub trait HoverFxOptimizerReuseHook {
    fn reuse_artifact(&self, artifact: &HoverFxOptimizerArtifact) -> Option<String>;
}

pub trait HoverFxCacheBackend {
    fn get(&self, key: &str) -> Option<String>;
    fn put(&self, key: String, value: String);
}

#[derive(Clone, Default)]
pub struct HoverFxMemoryCache {
    values: Arc<Mutex<BTreeMap<String, String>>>,
}

impl HoverFxCacheBackend for HoverFxMemoryCache {
    fn get(&self, key: &str) -> Option<String> {
        self.values.lock().ok()?.get(key).cloned()
    }

    fn put(&self, key: String, value: String) {
        if let Ok(mut values) = self.values.lock() {
            values.insert(key, value);
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxOffloadPlan {
    pub package: String,
    pub lane: String,
    pub serializable: bool,
    pub fallback: HoverFxFallbackStrategy,
    pub payload_cache_key: String,
    pub tasks: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxCompactDictionary {
    pub version: u8,
    pub terms: BTreeMap<String, u16>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxTraceEvent {
    pub name: String,
    pub cache_key: String,
    pub route: Option<String>,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HoverFxStrataMigrationPlan {
    pub package: String,
    pub route: Option<String>,
    pub steps: Vec<String>,
}

/// Serialize route-scoped HoverFX configs with stable cache keys.
///
/// ```rust
/// use dioxus_hoverfx_core::prelude::*;
///
/// let config = hoverfx().with_default_effect(HoverFxPreset::Spotlight.as_attr());
/// let policy = hoverfx_route_policy().route("/gallery");
/// let batch = hoverfx_serialize_batch([(&config, policy)], HoverFxBatchOptions::default())?;
///
/// assert_eq!(batch.payloads.len(), 1);
/// assert!(batch.total_bytes > 0);
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn hoverfx_serialize_batch<'a>(
    entries: impl IntoIterator<Item = (&'a HoverFxConfig, HoverFxRoutePolicy)>,
    options: HoverFxBatchOptions,
) -> serde_json::Result<HoverFxBatchSerializationReport> {
    let entries = entries.into_iter().collect::<Vec<_>>();
    let mut payloads = if options.deterministic_parallel && entries.len() > 1 {
        thread::scope(|scope| {
            let mut handles = Vec::new();
            for (config, policy) in entries {
                handles.push(scope.spawn(move || serialize_one(config, &policy, options.format)));
            }
            handles
                .into_iter()
                .map(|handle| handle.join().expect("HoverFX batch worker panicked"))
                .collect::<serde_json::Result<Vec<_>>>()
        })?
    } else {
        entries
            .into_iter()
            .map(|(config, policy)| serialize_one(config, &policy, options.format))
            .collect::<serde_json::Result<Vec<_>>>()?
    };
    if options.sort_by_cache_key {
        payloads.sort_by(|a, b| a.cache_key.cmp(&b.cache_key));
    }
    let total_bytes = payloads.iter().map(|payload| payload.bytes).sum();
    Ok(HoverFxBatchSerializationReport {
        package: HOVERFX_PACKAGE_NAME.to_string(),
        deterministic_parallel: options.deterministic_parallel,
        payloads,
        total_bytes,
    })
}

pub fn hoverfx_asset_budget_bridge(
    config: &HoverFxConfig,
    policy: &HoverFxRoutePolicy,
) -> HoverFxAssetBudgetBridge {
    let output = config.output_report(policy);
    HoverFxAssetBudgetBridge {
        package: HOVERFX_PACKAGE_NAME.to_string(),
        entries: vec![
            HoverFxAssetBudgetEntry {
                category: HoverFxAssetBudgetCategory::ConfigJson,
                bytes: output.config_bytes,
            },
            HoverFxAssetBudgetEntry {
                category: HoverFxAssetBudgetCategory::RuntimeScript,
                bytes: output.runtime_bytes,
            },
            HoverFxAssetBudgetEntry {
                category: HoverFxAssetBudgetCategory::EffectCssVars,
                bytes: output.style_bytes,
            },
        ],
    }
}

pub fn hoverfx_precomputed_css_fragments() -> Vec<HoverFxCssFragment> {
    HoverFxPreset::ALL
        .into_iter()
        .map(|preset| {
            let css = format!(
                "[data-dxh-effect=\"{}\"]{{--dxh-preset:\"{}\";}}",
                preset.as_attr(),
                preset.as_attr()
            );
            HoverFxCssFragment {
                id: preset.as_attr().to_string(),
                bytes: css.len(),
                css,
            }
        })
        .collect()
}

pub fn hoverfx_apply_motion_hook<H: HoverFxMotionPolicyHook>(
    policy: HoverFxMotionPolicy,
    hook: &H,
) -> HoverFxMotionPolicy {
    hook.apply(policy)
}

pub fn hoverfx_coalesce_route_metadata(
    route: impl Into<String>,
    fragments: impl IntoIterator<Item = HoverFxManifestFragment>,
) -> HoverFxRouteMetadata {
    let mut metadata = HoverFxRouteMetadata {
        route: route.into(),
        ..HoverFxRouteMetadata::default()
    };
    for fragment in fragments {
        metadata.cache_keys.push(fragment.cache_key);
        if !metadata.packages.contains(&fragment.package) {
            metadata.packages.push(fragment.package);
        }
        metadata.labels.extend(fragment.labels);
    }
    metadata.cache_keys.sort();
    metadata.packages.sort();
    metadata
}

pub fn hoverfx_optimizer_artifacts(report: &HoverFxOutputReport) -> Vec<HoverFxOptimizerArtifact> {
    [
        ("hoverfx.config", report.config_bytes, true),
        ("hoverfx.runtime", report.runtime_bytes, true),
        ("hoverfx.style", report.style_bytes, true),
    ]
    .into_iter()
    .map(|(id, bytes, minified)| HoverFxOptimizerArtifact {
        id: id.to_string(),
        content_hash: integration_hash_hex([id, &report.cache_key, &bytes.to_string()]),
        bytes,
        minified,
    })
    .collect()
}

pub fn hoverfx_cache_put_report<B: HoverFxCacheBackend>(
    cache: &B,
    report: &HoverFxOutputReport,
) -> String {
    let key = format!(
        "{}:{}:{}",
        HOVERFX_PACKAGE_NAME, HOVERFX_PACKAGE_VERSION, report.cache_key
    );
    cache.put(
        key.clone(),
        serde_json::to_string(report).unwrap_or_default(),
    );
    key
}

pub fn hoverfx_workertown_offload_plan(
    config: &HoverFxConfig,
    policy: &HoverFxRoutePolicy,
) -> HoverFxOffloadPlan {
    HoverFxOffloadPlan {
        package: HOVERFX_PACKAGE_NAME.to_string(),
        lane: "hoverfx".to_string(),
        serializable: true,
        fallback: policy.fallback,
        payload_cache_key: hoverfx_cache_key(config, policy.route.as_deref(), Some("workertown")),
        tasks: vec![
            "pointer-sampling".to_string(),
            "dirty-rect-render".to_string(),
            "tooltip-textfx-trigger".to_string(),
        ],
    }
}

pub fn hoverfx_compact_dictionary() -> HoverFxCompactDictionary {
    let mut terms = BTreeMap::new();
    for (index, term) in [
        "config",
        "runtime",
        "worker",
        "style",
        "route",
        "profile",
        "fallback",
        "workertown",
    ]
    .into_iter()
    .enumerate()
    {
        terms.insert(term.to_string(), index as u16);
    }
    HoverFxCompactDictionary { version: 1, terms }
}

pub fn hoverfx_trace_report(
    config: &HoverFxConfig,
    policy: &HoverFxRoutePolicy,
    mut emit: impl FnMut(HoverFxTraceEvent),
) {
    let explain = explain_hoverfx(config, policy);
    emit(HoverFxTraceEvent {
        name: "hoverfx.policy".to_string(),
        cache_key: explain.cache_key.clone(),
        route: policy.route.clone(),
        message: explain.runtime_decision,
    });
    emit(HoverFxTraceEvent {
        name: "hoverfx.output".to_string(),
        cache_key: explain.cache_key,
        route: policy.route.clone(),
        message: format!("{} config bytes", explain.output.config_bytes),
    });
}

pub fn hoverfx_strata_migration_plan(
    config: &HoverFxConfig,
    policy: &HoverFxRoutePolicy,
) -> HoverFxStrataMigrationPlan {
    HoverFxStrataMigrationPlan {
        package: HOVERFX_PACKAGE_NAME.to_string(),
        route: policy.route.clone(),
        steps: vec![
            "emit HoverFxManifestFragment from standalone config".to_string(),
            format!(
                "register cache key {}",
                config.cache_key(policy.route.as_deref())
            ),
            "attach runtime/style assets through Strata route metadata".to_string(),
            "preserve fallback and diagnostic context policy".to_string(),
        ],
    }
}

pub fn hoverfx_conformance_fixture() -> serde_json::Value {
    let config = HoverFxConfig::default();
    let policy = HoverFxRoutePolicy::default().route("/hoverfx");
    serde_json::json!({
        "manifest": hoverfx_manifest_fragment(&config, &policy),
        "dictionary": hoverfx_compact_dictionary(),
        "cssFragments": hoverfx_precomputed_css_fragments(),
    })
}

fn serialize_one(
    config: &HoverFxConfig,
    policy: &HoverFxRoutePolicy,
    format: HoverFxSerializationFormat,
) -> serde_json::Result<HoverFxBatchPayload> {
    let json = config.to_preferred_json(format)?;
    Ok(HoverFxBatchPayload {
        route: policy.route.clone(),
        cache_key: hoverfx_cache_key(config, policy.route.as_deref(), Some(format.as_attr())),
        bytes: json.len(),
        json,
    })
}

fn integration_hash_hex<'a>(parts: impl IntoIterator<Item = &'a str>) -> String {
    let mut hash = 0xcbf29ce484222325u64;
    for part in parts {
        for byte in part.as_bytes() {
            hash ^= u64::from(*byte);
            hash = hash.wrapping_mul(0x100000001b3);
        }
        hash ^= 0xff;
        hash = hash.wrapping_mul(0x100000001b3);
    }
    format!("{hash:016x}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{HoverFxDefinition, HoverFxPresetProfile, hoverfx_output_budget};

    #[test]
    fn batch_serialization_parallel_is_deterministic() {
        let config = HoverFxConfig::default()
            .with_effect(HoverFxDefinition::from_preset(HoverFxPreset::Tooltip));
        let a = HoverFxRoutePolicy::default().route("/a");
        let b = HoverFxRoutePolicy::default().route("/b");
        let serial = hoverfx_serialize_batch(
            [(&config, a.clone()), (&config, b.clone())],
            HoverFxBatchOptions::default(),
        )
        .unwrap();
        let parallel = hoverfx_serialize_batch(
            [(&config, a), (&config, b)],
            HoverFxBatchOptions {
                deterministic_parallel: true,
                ..HoverFxBatchOptions::default()
            },
        )
        .unwrap();

        assert_eq!(serial.payloads, parallel.payloads);
        assert!(parallel.deterministic_parallel);
    }

    #[test]
    fn asset_budget_duplicate_ids_cache_and_offload_are_reported() {
        let config = HoverFxConfig::default().with_profile(HoverFxPresetProfile::Aggressive);
        let policy = HoverFxRoutePolicy::default()
            .route("/hoverfx")
            .budget(hoverfx_output_budget().runtime_bytes(8));
        let report = config.output_report(&policy);
        let bridge = hoverfx_asset_budget_bridge(&config, &policy);
        let ids = HoverFxRuntimeIds::default().with_config_id(DEFAULT_HOVERFX_PREPAINT_STYLE_ID);
        let cache = HoverFxMemoryCache::default();
        let key = hoverfx_cache_put_report(&cache, &report);
        let offload = hoverfx_workertown_offload_plan(&config, &policy);

        assert!(bridge.total_bytes() >= report.config_bytes);
        assert_eq!(
            ids.duplicate_ids(),
            vec![DEFAULT_HOVERFX_PREPAINT_STYLE_ID.to_string()]
        );
        assert!(ids.deduped().duplicate_ids().is_empty());
        assert!(cache.get(&key).is_some());
        assert!(offload.serializable);
        assert!(offload.tasks.iter().any(|task| task == "dirty-rect-render"));
    }

    #[test]
    fn css_motion_metadata_optimizer_trace_and_migration_are_concrete() {
        struct ForceDisable;

        impl HoverFxMotionPolicyHook for ForceDisable {
            fn apply(&self, mut policy: HoverFxMotionPolicy) -> HoverFxMotionPolicy {
                policy.reduced_motion = "disable".to_string();
                policy
            }
        }

        let config = HoverFxConfig::default();
        let policy = HoverFxRoutePolicy::default().route("/hoverfx");
        let manifest = config.manifest_fragment(&policy);
        let metadata = hoverfx_coalesce_route_metadata("/hoverfx", [manifest]);
        let css = hoverfx_precomputed_css_fragments();
        let output = config.output_report(&policy);
        let artifacts = hoverfx_optimizer_artifacts(&output);
        let motion = hoverfx_apply_motion_hook(HoverFxMotionPolicy::default(), &ForceDisable);
        let dictionary = hoverfx_compact_dictionary();
        let migration = hoverfx_strata_migration_plan(&config, &policy);
        let mut traces = Vec::new();
        hoverfx_trace_report(&config, &policy, |event| traces.push(event));
        let fixture = hoverfx_conformance_fixture();

        assert_eq!(metadata.route, "/hoverfx");
        assert!(css.iter().any(|fragment| fragment.id == "tooltip"));
        assert!(
            artifacts
                .iter()
                .any(|artifact| artifact.id == "hoverfx.runtime")
        );
        assert_eq!(motion.reduced_motion, "disable");
        assert_eq!(dictionary.terms["runtime"], 1);
        assert!(migration.steps.iter().any(|step| step.contains("Strata")));
        assert_eq!(traces.len(), 2);
        assert!(fixture.get("manifest").is_some());
    }
}