wakaru 1.8.0

Public Rust API for the Wakaru JavaScript decompiler
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
use std::collections::{HashMap, HashSet};
use std::path::Path;

use anyhow::anyhow;

use crate::decompile::diagnostic_from_core;
use crate::error::{from_core_driver_error, Error, ErrorKind, Result};
use crate::options::{
    DceMode, ModuleMode, RewriteLevel, ScopeHoistMode, UnmatchedInput, UnpackOptions,
};
use crate::output::{
    BundleFormat, Diagnostic, EntryStatus, InputAction, InputDetection, InputId, InputReceipt,
    InputReport, ModuleOutput, ModuleStatus, SourceSpan, UnpackOutput,
};
use crate::source::Source;

pub fn unpack(inputs: Vec<Source>, options: UnpackOptions) -> Result<UnpackOutput> {
    let mut job = UnpackJob::new(options)?;
    for input in inputs {
        job.push(input)?;
    }
    job.finish()
}

pub struct UnpackJob {
    options: UnpackOptions,
    reports: Vec<InputReport>,
    retained: Vec<RetainedInput>,
    unmatched_error: Option<String>,
}

struct RetainedInput {
    id: InputId,
    prepared: wakaru_core::driver::PreparedUnpackInput,
    preserve: bool,
}

impl std::fmt::Debug for UnpackJob {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UnpackJob")
            .field("options", &self.options)
            .field("reports", &self.reports)
            .field("retained_count", &self.retained.len())
            .field("unmatched_error", &self.unmatched_error)
            .finish()
    }
}

struct ProcessedInput {
    id: InputId,
    filename: String,
}

impl UnpackJob {
    pub fn new(options: UnpackOptions) -> Result<Self> {
        if matches!(options.modules(), ModuleMode::Raw) && options.output_source_maps() {
            return Err(Error::new(
                ErrorKind::InvalidOptions,
                None,
                anyhow!("raw unpack mode does not support output source maps"),
            ));
        }
        Ok(Self {
            options,
            reports: Vec::new(),
            retained: Vec::new(),
            unmatched_error: None,
        })
    }

    pub fn push(&mut self, input: Source) -> Result<InputReceipt> {
        self.push_with_unmatched(input, self.options.unmatched())
    }

    /// Detects and prepares one input using an input-specific plain-source
    /// policy while retaining every other job option.
    ///
    /// This lets a single cross-module job process explicit files while
    /// treating directory-walk candidates as detection-only inputs.
    pub fn push_with_unmatched(
        &mut self,
        input: Source,
        unmatched: UnmatchedInput,
    ) -> Result<InputReceipt> {
        if input.source_map().is_some() {
            return Err(Error::new(
                ErrorKind::InvalidOptions,
                Some(input.filename().to_string()),
                anyhow!("input source maps are not supported by unpack"),
            ));
        }

        let parts = input.into_parts();
        let input_filename = parts.filename.clone();
        let prepared = wakaru_core::driver::prepare_unpack_input(
            parts.filename,
            parts.code,
            self.options.scope_hoist() != ScopeHoistMode::Disabled,
            matches!(self.options.modules(), ModuleMode::Decompile(_))
                && unmatched == UnmatchedInput::Process,
        )
        .map_err(|error| {
            let kind = from_core_driver_error(error.kind());
            Error::new(kind, Some(input_filename), error.into_inner())
        })?;
        let detection = map_prepared_detection(prepared.detection());
        let id = InputId::from_index(self.reports.len());
        let (action, retain, preserve) = match detection {
            InputDetection::Structural(_) | InputDetection::HeuristicScopeHoisted => {
                (InputAction::Unpacked, true, false)
            }
            InputDetection::Plain => match unmatched {
                UnmatchedInput::Skip => (InputAction::Skipped, false, false),
                UnmatchedInput::Process => (InputAction::Processed, true, false),
                UnmatchedInput::Preserve => (InputAction::Preserved, true, true),
                UnmatchedInput::Error => {
                    self.unmatched_error
                        .get_or_insert_with(|| prepared.filename().to_string());
                    (InputAction::Processed, false, false)
                }
            },
        };

        self.reports.push(InputReport {
            id,
            filename: prepared.filename().to_string(),
            detection,
            action,
            module_indices: Vec::new(),
        });
        if retain {
            self.retained.push(RetainedInput {
                id,
                prepared,
                preserve,
            });
        }

        Ok(InputReceipt { id, detection })
    }

    pub fn finish(mut self) -> Result<UnpackOutput> {
        if self.reports.is_empty() {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                None,
                anyhow!("at least one input is required"),
            ));
        }
        if let Some(filename) = self.unmatched_error {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                Some(filename.clone()),
                anyhow!("input {filename:?} is not a recognized bundle"),
            ));
        }

        let mut processed = Vec::new();
        let mut preserved = Vec::new();
        for input in self.retained {
            if input.preserve {
                preserved.push(input);
            } else {
                processed.push(input);
            }
        }

        let mut modules = Vec::new();
        let mut diagnostics = Vec::new();
        if !processed.is_empty() {
            let processed_meta = processed
                .iter()
                .map(|input| ProcessedInput {
                    id: input.id,
                    filename: input.prepared.filename().to_string(),
                })
                .collect::<Vec<_>>();
            let core_output = run_core_unpack(processed, &self.options)?;
            let converted = convert_core_output(
                core_output,
                &processed_meta,
                &mut self.reports,
                matches!(self.options.modules(), ModuleMode::Raw),
            );
            modules = converted.modules;
            diagnostics = converted.diagnostics;
        }

        append_preserved_modules(&mut modules, &mut self.reports, preserved);

        Ok(UnpackOutput {
            modules,
            inputs: self.reports,
            diagnostics,
        })
    }
}

fn map_prepared_detection(
    detection: wakaru_core::driver::PreparedInputDetection,
) -> InputDetection {
    match detection {
        wakaru_core::driver::PreparedInputDetection::Bundle(format) => map_bundle_detection(format),
        wakaru_core::driver::PreparedInputDetection::ScopeHoisted => {
            InputDetection::HeuristicScopeHoisted
        }
        wakaru_core::driver::PreparedInputDetection::Plain => InputDetection::Plain,
    }
}

fn map_bundle_detection(format: wakaru_core::BundleFormat) -> InputDetection {
    match format {
        wakaru_core::BundleFormat::Webpack5 => InputDetection::Structural(BundleFormat::Webpack5),
        wakaru_core::BundleFormat::Webpack4 => InputDetection::Structural(BundleFormat::Webpack4),
        wakaru_core::BundleFormat::Browserify => {
            InputDetection::Structural(BundleFormat::Browserify)
        }
        wakaru_core::BundleFormat::ClosureModuleManager => {
            InputDetection::Structural(BundleFormat::ClosureModuleManager)
        }
        wakaru_core::BundleFormat::Metro => InputDetection::Structural(BundleFormat::Metro),
        wakaru_core::BundleFormat::SystemJs => InputDetection::Structural(BundleFormat::SystemJs),
        wakaru_core::BundleFormat::Esbuild => InputDetection::Structural(BundleFormat::Esbuild),
        wakaru_core::BundleFormat::Amd => InputDetection::Structural(BundleFormat::Amd),
        wakaru_core::BundleFormat::ScopeHoisted => InputDetection::HeuristicScopeHoisted,
    }
}

fn run_core_unpack(
    inputs: Vec<RetainedInput>,
    options: &UnpackOptions,
) -> Result<wakaru_core::UnpackOutput> {
    let span = tracing::info_span!("public_unpack_core");
    let _enter = span.enter();
    let (level, dce_mode, raw) = match options.modules() {
        ModuleMode::Raw => (RewriteLevel::Standard, DceMode::Off, true),
        ModuleMode::Decompile(rewrite) => (rewrite.level(), rewrite.dce(), false),
    };
    let core_options = wakaru_core::DecompileOptions {
        filename: inputs[0].prepared.filename().to_string(),
        sourcemap: None,
        dce_mode: dce_mode.into_core(),
        level: level.into_core(),
        heuristic_split: options.scope_hoist() != ScopeHoistMode::Disabled,
        diagnostics: !raw && options.diagnostics(),
        emit_source_map: options.output_source_maps(),
    };
    let core_inputs = inputs.into_iter().map(|input| input.prepared).collect();

    let result = wakaru_core::driver::unpack_prepared_inputs(
        core_inputs,
        core_options,
        raw,
        options.scope_hoist() == ScopeHoistMode::Recursive,
    );
    result.map_err(|error| Error::new(ErrorKind::Internal, None, error))
}

struct ConvertedOutput {
    modules: Vec<ModuleOutput>,
    diagnostics: Vec<Diagnostic>,
}

fn convert_core_output(
    output: wakaru_core::UnpackOutput,
    processed: &[ProcessedInput],
    reports: &mut [InputReport],
    raw: bool,
) -> ConvertedOutput {
    let span = tracing::info_span!("public_unpack_convert_output");
    let _enter = span.enter();
    let source_maps: HashMap<_, _> = output.source_maps.into_iter().collect();
    let provenance: HashMap<_, _> = output
        .provenance
        .into_iter()
        .map(|provenance| (provenance.filename.clone(), provenance))
        .collect();
    let input_by_name: HashMap<&str, InputId> = processed
        .iter()
        .map(|input| (input.filename.as_str(), input.id))
        .collect();
    let only_input = (processed.len() == 1).then_some(processed[0].id);
    let failed: HashSet<&str> = output
        .warnings
        .iter()
        .filter(|warning| warning.kind == wakaru_core::UnpackWarningKind::DecompileFailed)
        .map(|warning| warning.filename.as_str())
        .collect();

    let modules: Vec<_> = output
        .modules
        .into_iter()
        .enumerate()
        .map(|(index, (filename, code))| {
            let module_provenance = provenance.get(&filename);
            let provenance_input = module_provenance.and_then(|provenance| {
                if let Some(position) = wakaru_core::driver::prepared_input_index(&provenance.input)
                {
                    processed.get(position).map(|input| input.id)
                } else if provenance.input.is_empty() {
                    only_input
                } else {
                    input_by_name.get(provenance.input.as_str()).copied()
                }
            });
            let spans = module_provenance
                .map(|provenance| {
                    provenance_input
                        .into_iter()
                        .flat_map(|input| {
                            provenance
                                .ranges
                                .iter()
                                .map(move |&(start, end)| SourceSpan { input, start, end })
                        })
                        .collect::<Vec<_>>()
                })
                .unwrap_or_default();
            let mut associated: HashSet<_> = spans.iter().map(|span| span.input).collect();
            if associated.is_empty() {
                if let Some(input) = provenance_input {
                    associated.insert(input);
                } else {
                    associated.extend(processed.iter().map(|input| input.id));
                }
            }
            for input in associated {
                reports[input.get() as usize].module_indices.push(index);
            }
            let entry = entry_status_from_provenance(
                module_provenance.map(|provenance| provenance.is_entry),
                &spans,
                reports,
            );
            ModuleOutput {
                source_map: source_maps.get(&filename).cloned(),
                entry,
                status: if failed.contains(filename.as_str()) {
                    ModuleStatus::DecompileFailed
                } else if raw {
                    ModuleStatus::Raw
                } else {
                    ModuleStatus::Decompiled
                },
                filename,
                code,
                provenance: spans,
            }
        })
        .collect();

    let module_by_filename: HashMap<&str, usize> = modules
        .iter()
        .enumerate()
        .map(|(index, module)| (module.filename.as_str(), index))
        .collect();
    let diagnostics = output
        .warnings
        .into_iter()
        .map(|warning| {
            let module = module_by_filename.get(warning.filename.as_str()).copied();
            let mut diagnostic = diagnostic_from_core(warning, module);
            diagnostic.input = module.and_then(|index| {
                let mut inputs = modules[index].provenance.iter().map(|span| span.input);
                let first = inputs.next()?;
                inputs.all(|input| input == first).then_some(first)
            });
            diagnostic
        })
        .collect();

    ConvertedOutput {
        modules,
        diagnostics,
    }
}

fn entry_status_from_provenance(
    is_entry: Option<bool>,
    spans: &[SourceSpan],
    reports: &[InputReport],
) -> EntryStatus {
    let Some(is_entry) = is_entry else {
        return EntryStatus::Unknown;
    };
    let mut associated = spans.iter().filter_map(|span| {
        reports
            .get(span.input.get() as usize)
            .map(|report| report.detection)
    });
    let Some(first) = associated.next() else {
        return EntryStatus::Unknown;
    };
    if !entry_status_is_definitive(first) || !associated.all(entry_status_is_definitive) {
        return EntryStatus::Unknown;
    }
    if is_entry {
        EntryStatus::Entry
    } else {
        EntryStatus::NonEntry
    }
}

fn entry_status_is_definitive(detection: InputDetection) -> bool {
    matches!(
        detection,
        InputDetection::Structural(
            BundleFormat::Webpack5
                | BundleFormat::Webpack4
                | BundleFormat::Browserify
                | BundleFormat::ClosureModuleManager
                | BundleFormat::Metro
        )
    )
}

fn append_preserved_modules(
    modules: &mut Vec<ModuleOutput>,
    reports: &mut [InputReport],
    preserved: Vec<RetainedInput>,
) {
    let mut seen: HashSet<String> = modules
        .iter()
        .map(|module| module.filename.to_lowercase())
        .collect();
    for input in preserved {
        let (source_filename, source_code) = input
            .prepared
            .into_plain_source()
            .expect("only plain inputs can be preserved");
        let filename = unique_preserved_filename(&source_filename, &mut seen);
        let source_len = source_code.len() as u32;
        let index = modules.len();
        reports[input.id.get() as usize].module_indices.push(index);
        modules.push(ModuleOutput {
            filename,
            code: source_code,
            source_map: None,
            provenance: vec![SourceSpan {
                input: input.id,
                start: 0,
                end: source_len,
            }],
            entry: EntryStatus::Unknown,
            status: ModuleStatus::Preserved,
        });
    }
}

fn unique_preserved_filename(filename: &str, seen: &mut HashSet<String>) -> String {
    let basename = Path::new(filename)
        .file_name()
        .and_then(|name| name.to_str())
        .filter(|name| !name.is_empty())
        .unwrap_or("module.js");
    let mut candidate = basename.to_string();
    let mut suffix = 2;
    while !seen.insert(candidate.to_lowercase()) {
        let path = Path::new(basename);
        let stem = path
            .file_stem()
            .and_then(|value| value.to_str())
            .unwrap_or("module");
        let extension = path
            .extension()
            .and_then(|value| value.to_str())
            .unwrap_or("js");
        candidate = format!("{stem}_{suffix}.{extension}");
        suffix += 1;
    }
    candidate
}

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

    const CLOSURE_MODULE_MANAGER_FIXTURE: &str =
        include_str!("../../core/tests/bundles/closure-module-manager/synthetic.js");
    const METRO_FIXTURE: &str = r#"
        __d(function(global, require, importDefault, importAll, module, exports, dependencyMap) {
            module.exports = 1;
        }, 1, [], "index.js");
        __r(1);
    "#;

    #[test]
    fn bundle_level_scope_hoisted_detection_is_mapped_without_panicking() {
        assert_eq!(
            map_prepared_detection(wakaru_core::driver::PreparedInputDetection::Bundle(
                wakaru_core::BundleFormat::ScopeHoisted,
            )),
            InputDetection::HeuristicScopeHoisted
        );
    }

    #[test]
    fn reports_closure_module_manager_detection() {
        let mut job = UnpackJob::new(UnpackOptions::default().with_modules(ModuleMode::Raw))
            .expect("options should be valid");
        let receipt = job
            .push(Source::new(
                "closure-bundle.js",
                CLOSURE_MODULE_MANAGER_FIXTURE,
            ))
            .expect("Closure bundle should be detected");

        assert_eq!(
            receipt.detection,
            InputDetection::Structural(BundleFormat::ClosureModuleManager)
        );
        let output = job.finish().expect("Closure bundle should unpack");
        assert!(!output.modules.is_empty());
        assert_eq!(output.inputs[0].detection, receipt.detection);
    }

    #[test]
    fn reports_metro_detection() {
        let mut job = UnpackJob::new(UnpackOptions::default().with_modules(ModuleMode::Raw))
            .expect("options should be valid");
        let receipt = job
            .push(Source::new("metro-bundle.js", METRO_FIXTURE))
            .expect("Metro bundle should be detected");

        assert_eq!(
            receipt.detection,
            InputDetection::Structural(BundleFormat::Metro)
        );
        assert_eq!(BundleFormat::Metro.as_str(), "metro");
        let output = job.finish().expect("Metro bundle should unpack");
        assert!(!output.modules.is_empty());
        assert!(
            output
                .modules
                .iter()
                .any(|module| module.entry == EntryStatus::Entry),
            "Metro run statements establish entry status"
        );
        assert_eq!(output.inputs[0].detection, receipt.detection);
    }

    #[test]
    fn entry_status_is_unknown_without_definitive_detector_knowledge() {
        let span = SourceSpan {
            input: InputId::from_index(0),
            start: 0,
            end: 1,
        };
        let report = |format| InputReport {
            id: InputId::from_index(0),
            filename: "bundle.js".to_string(),
            detection: InputDetection::Structural(format),
            action: InputAction::Unpacked,
            module_indices: Vec::new(),
        };

        assert_eq!(
            entry_status_from_provenance(
                Some(false),
                std::slice::from_ref(&span),
                &[report(BundleFormat::Webpack5)],
            ),
            EntryStatus::NonEntry
        );
        assert_eq!(
            entry_status_from_provenance(
                Some(false),
                std::slice::from_ref(&span),
                &[report(BundleFormat::Esbuild)],
            ),
            EntryStatus::Unknown
        );
        assert_eq!(
            entry_status_from_provenance(
                Some(true),
                std::slice::from_ref(&span),
                &[report(BundleFormat::Amd)],
            ),
            EntryStatus::Unknown
        );
        assert_eq!(
            entry_status_from_provenance(Some(true), &[], &[report(BundleFormat::Metro)]),
            EntryStatus::Unknown
        );
    }

    #[test]
    fn per_push_unmatched_policy_composes_explicit_and_candidate_inputs() {
        let mut job = UnpackJob::new(
            UnpackOptions::default()
                .with_scope_hoist(ScopeHoistMode::Disabled)
                .with_unmatched(UnmatchedInput::Process),
        )
        .expect("options should be valid");

        job.push(Source::new("explicit.js", "const explicit = 1;"))
            .expect("explicit source should be processed");
        job.push_with_unmatched(
            Source::new("candidate.js", "const candidate = 2;"),
            UnmatchedInput::Skip,
        )
        .expect("plain candidate should be skipped");

        let output = job.finish().expect("mixed intake should finish");
        assert_eq!(output.inputs[0].action, InputAction::Processed);
        assert_eq!(output.inputs[1].action, InputAction::Skipped);
        assert_eq!(output.inputs[0].module_indices, vec![0]);
        assert!(output.inputs[1].module_indices.is_empty());
        assert_eq!(output.modules.len(), 1);
        assert!(output.modules[0].code.contains("explicit"));
    }

    #[test]
    fn all_skipped_inputs_return_an_empty_successful_output() {
        let mut job = UnpackJob::new(
            UnpackOptions::default()
                .with_scope_hoist(ScopeHoistMode::Disabled)
                .with_unmatched(UnmatchedInput::Skip),
        )
        .expect("options should be valid");
        let receipt = job
            .push(Source::new("plain.js", "const value = 1;"))
            .expect("plain input should be accepted");
        assert_eq!(receipt.id.get(), 0);
        assert_eq!(receipt.detection, InputDetection::Plain);

        let output = job.finish().expect("all-skipped is a valid result");
        assert!(output.modules.is_empty());
        assert_eq!(output.inputs.len(), 1);
        assert_eq!(output.inputs[0].action, InputAction::Skipped);
    }

    #[test]
    fn unmatched_error_is_deferred_until_finish() {
        let mut job = UnpackJob::new(
            UnpackOptions::default()
                .with_scope_hoist(ScopeHoistMode::Disabled)
                .with_unmatched(UnmatchedInput::Error),
        )
        .expect("options should be valid");
        let first = job
            .push(Source::new("plain.js", "const value = 1;"))
            .expect("plain detection should not fail push");
        let second = job
            .push(Source::new("also-plain.js", "const other = 2;"))
            .expect("job remains usable");
        assert_eq!(first.id.get(), 0);
        assert_eq!(second.id.get(), 1);

        let error = job.finish().expect_err("plain input should fail the job");
        assert_eq!(error.kind(), ErrorKind::InvalidInput);
        assert_eq!(error.input_filename(), Some("plain.js"));
    }

    #[test]
    fn unrecoverable_input_parse_is_a_typed_push_error() {
        let mut job =
            UnpackJob::new(UnpackOptions::default()).expect("default options should be valid");
        let error = job
            .push(Source::new("broken.js", "function ("))
            .expect_err("invalid input should fail during push");

        assert_eq!(error.kind(), ErrorKind::Parse);
        assert_eq!(error.input_filename(), Some("broken.js"));
    }

    #[test]
    fn preserved_plain_input_is_returned_without_rewriting() {
        let source = "var untouched = 1;";
        let output = unpack(
            vec![Source::new("plain.js", source)],
            UnpackOptions::default()
                .with_scope_hoist(ScopeHoistMode::Disabled)
                .with_unmatched(UnmatchedInput::Preserve),
        )
        .expect("preserve should succeed");

        assert_eq!(output.modules.len(), 1);
        assert_eq!(output.modules[0].code, source);
        assert_eq!(output.modules[0].status, ModuleStatus::Preserved);
        assert_eq!(output.inputs[0].module_indices, vec![0]);
    }

    #[test]
    fn raw_output_maps_are_rejected_during_job_creation() {
        let error = UnpackJob::new(
            UnpackOptions::default()
                .with_modules(ModuleMode::Raw)
                .with_output_source_maps(true),
        )
        .expect_err("invalid combination should fail");
        assert_eq!(error.kind(), ErrorKind::InvalidOptions);
    }

    #[test]
    fn duplicate_input_filenames_keep_distinct_ids_and_provenance() {
        let output = unpack(
            vec![
                Source::new("same.js", "export const first = 1;"),
                Source::new("same.js", "export const second = 2;"),
            ],
            UnpackOptions::default().with_scope_hoist(ScopeHoistMode::Disabled),
        )
        .expect("duplicate physical filenames should remain distinguishable");

        assert_eq!(output.inputs.len(), 2);
        assert_eq!(output.inputs[0].id.get(), 0);
        assert_eq!(output.inputs[1].id.get(), 1);
        assert_eq!(output.inputs[0].module_indices.len(), 1);
        assert_eq!(output.inputs[1].module_indices.len(), 1);
        assert_ne!(
            output.inputs[0].module_indices,
            output.inputs[1].module_indices
        );
        let first_module = output.inputs[0].module_indices[0];
        let second_module = output.inputs[1].module_indices[0];
        assert_eq!(output.modules[first_module].provenance[0].input.get(), 0);
        assert_eq!(output.modules[second_module].provenance[0].input.get(), 1);
    }

    #[test]
    fn provenance_less_synthesized_module_is_associated_with_all_processed_inputs() {
        let processed = vec![
            ProcessedInput {
                id: InputId::from_index(0),
                filename: "first.js".to_string(),
            },
            ProcessedInput {
                id: InputId::from_index(1),
                filename: "second.js".to_string(),
            },
        ];
        let mut reports = processed
            .iter()
            .map(|input| InputReport {
                id: input.id,
                filename: input.filename.clone(),
                detection: InputDetection::Plain,
                action: InputAction::Processed,
                module_indices: Vec::new(),
            })
            .collect::<Vec<_>>();
        let output = wakaru_core::UnpackOutput {
            modules: vec![("synthesized.js".to_string(), "export {};".to_string())],
            ..Default::default()
        };

        let converted = convert_core_output(output, &processed, &mut reports, false);

        assert_eq!(converted.modules.len(), 1);
        assert!(converted.modules[0].provenance.is_empty());
        assert_eq!(reports[0].module_indices, vec![0]);
        assert_eq!(reports[1].module_indices, vec![0]);
    }

    #[test]
    fn parse_recovery_is_reported_without_optional_diagnostics() {
        let output = unpack(
            vec![Source::new(
                "duplicate-label.js",
                "label: label: break label;",
            )],
            UnpackOptions::default().with_scope_hoist(ScopeHoistMode::Disabled),
        )
        .expect("recoverable input should produce output");

        assert!(output
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == crate::DiagnosticCode::InputParseRecovered));
    }
}