fallow-core 3.32.0

Internal detector backend for fallow-engine and fallow-api
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
//! Playwright test runner plugin.
//!
//! Detects Playwright projects and marks test files and config as entry points.

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

use rustc_hash::FxHashMap;

use oxc_ast::ast::{Expression, ObjectExpression, ObjectPropertyKind};

use super::config_parser;
use super::{Plugin, PluginResult};
use crate::scripts;

/// Test entry patterns for a project without a config, or with a config whose
/// test locations are not statically known. The directory patterns also keep
/// the helpers in the conventional test directories.
const DEFAULT_TEST_ENTRY_PATTERNS: &[&str] = &[
    "**/*.spec.{ts,tsx,js,jsx}",
    "**/*.test.{ts,tsx,js,jsx}",
    "tests/**/*.{ts,tsx,js,jsx}",
    "e2e/**/*.{ts,tsx,js,jsx}",
];

/// The file-name patterns of [`DEFAULT_TEST_ENTRY_PATTERNS`]. A config with a
/// static `testDir` keeps them, because other test runners (for example
/// `node --test`) select files with the same names and do not always have
/// their own plugin.
const TEST_FILE_NAME_PATTERNS: &[&str] =
    &["**/*.spec.{ts,tsx,js,jsx}", "**/*.test.{ts,tsx,js,jsx}"];

/// The file extensions of the Playwright default `testMatch`,
/// `**/*.@(spec|test).?(c|m)[jt]s?(x)`.
const TEST_FILE_EXTENSIONS: &str = "{ts,tsx,js,jsx,mts,cts,mjs,cjs}";

define_plugin!(
    struct PlaywrightPlugin => "playwright",
    enablers: &["@playwright/test"],
    entry_patterns: DEFAULT_TEST_ENTRY_PATTERNS,
    config_patterns: &["playwright.config.{ts,js}"],
    always_used: &["playwright.config.{ts,js}"],
    tooling_dependencies: &["@playwright/test", "playwright"],
    fixture_glob_patterns: &[
        "**/fixtures/**/*.{ts,tsx,js,jsx,json}",
        "e2e/fixtures/**/*.{ts,tsx,js,jsx,json}",
    ],
    resolve_config(config_path, source, root) {
        let mut result = PluginResult::default();

        let config_dir = config_path
            .parent()
            .filter(|parent| parent.is_absolute())
            .unwrap_or(root);

        let imports = config_parser::extract_imports(source, config_path);
        for imp in &imports {
            let dep = crate::resolve::extract_package_name(imp);
            result.referenced_dependencies.push(dep);
        }

        if let Some(setup) =
            config_parser::extract_config_string(source, config_path, &["globalSetup"])
        {
            result
                .setup_files
                .push(config_dir.join(setup.trim_start_matches("./")));
        }
        if let Some(teardown) =
            config_parser::extract_config_string(source, config_path, &["globalTeardown"])
        {
            result
                .setup_files
                .push(config_dir.join(teardown.trim_start_matches("./")));
        }

        let (web_deps, web_setup) = collect_web_server(source, config_path, root, config_dir);
        result.referenced_dependencies.extend(web_deps);
        result.setup_files.extend(web_setup);

        // Every config states its full set of test entries, so that two
        // configs in one project keep the entries of both. A config without
        // static test locations restates the defaults.
        match test_entry_patterns(source, config_path, root, config_dir) {
            Some(test_dir_patterns) => {
                result.extend_entry_patterns(TEST_FILE_NAME_PATTERNS.iter().copied());
                result.extend_entry_patterns(test_dir_patterns);
            }
            None => result.extend_entry_patterns(DEFAULT_TEST_ENTRY_PATTERNS.iter().copied()),
        }
        result.replace_entry_patterns = true;
        result.accumulate_config_entry_patterns = true;

        result
    },
);

/// A `testDir` or `testMatch` value of one config scope.
#[derive(Clone, Copy)]
enum ScopeValue<T> {
    Absent,
    Static(T),
    Dynamic,
}

impl<T: Clone> ScopeValue<T> {
    /// The value of a project, which inherits the top-level value when it
    /// does not set its own.
    fn or_inherit(&self, top: &Self) -> Self {
        match self {
            Self::Absent => top.clone(),
            other => other.clone(),
        }
    }
}

/// The test locations of the top-level config or of one project.
#[derive(Clone)]
struct TestScope {
    test_dir: ScopeValue<String>,
    test_match: ScopeValue<Vec<String>>,
}

/// Entry patterns for the test files that the config selects, as project-root
/// relative globs.
///
/// Playwright collects test files only below `testDir`. Each scope (the
/// top-level config, or each `projects[]` element) gives one directory. A
/// project inherits `testDir` and `testMatch` from the top level. A glob
/// `testMatch` applies below `testDir`, with a `**/` prefix as Playwright adds
/// one. A regular expression or other non-static `testMatch` keeps every
/// script file below `testDir`. Unknown scopes add the conventional test
/// directories while preserving entry patterns from known scopes.
fn test_entry_patterns(
    source: &str,
    config_path: &Path,
    root: &Path,
    config_dir: &Path,
) -> Option<Vec<String>> {
    let scopes = config_parser::extract_from_source(source, config_path, |program| {
        if let Some(arguments) = config_parser::find_config_merge_arguments(program) {
            return Some(config_test_scopes(
                scope_from(
                    merged_property(&arguments, "testDir"),
                    merged_property(&arguments, "testMatch"),
                ),
                merged_projects(&arguments),
            ));
        }
        let config = config_parser::find_config_object(program)?;
        Some(config_test_scopes(
            scope_of(config),
            scope_property(config, "projects"),
        ))
    })?;

    let mut patterns = Vec::new();
    for scope in scopes {
        let test_dir = match (scope.test_dir, &scope.test_match) {
            (ScopeValue::Static(test_dir), _) => test_dir,
            // Playwright uses the config directory when `testDir` is not set.
            // Only a static `testMatch` narrows that directory enough to give
            // entries; the conventional directories stay as a fallback.
            (ScopeValue::Absent, ScopeValue::Static(_)) => {
                patterns.extend(conventional_test_dir_patterns());
                ".".to_string()
            }
            _ => {
                patterns.extend(conventional_test_dir_patterns());
                continue;
            }
        };
        let Some(prefix) = test_dir_prefix(&test_dir, root, config_dir) else {
            patterns.extend(conventional_test_dir_patterns());
            continue;
        };
        match scope.test_match {
            ScopeValue::Absent => {
                let glob =
                    case_insensitive_glob(&format!("*.{{spec,test}}.{TEST_FILE_EXTENSIONS}"));
                patterns.push(format!("{prefix}**/{glob}"));
            }
            ScopeValue::Static(globs) => {
                for glob in globs {
                    let glob = glob.strip_prefix("**/").unwrap_or(&glob);
                    let glob = case_insensitive_glob(glob);
                    patterns.push(format!("{prefix}**/{glob}"));
                }
            }
            ScopeValue::Dynamic => {
                patterns.push(format!("{prefix}**/*.{TEST_FILE_EXTENSIONS}"));
            }
        }
    }
    Some(patterns)
}

/// The conventional test directory patterns of [`DEFAULT_TEST_ENTRY_PATTERNS`].
fn conventional_test_dir_patterns() -> impl Iterator<Item = String> {
    DEFAULT_TEST_ENTRY_PATTERNS
        .iter()
        .skip(TEST_FILE_NAME_PATTERNS.len())
        .map(|pattern| (*pattern).to_string())
}

/// The value of `key` in a `defineConfig(a, b, ...)` call. Playwright merges
/// the arguments from left to right, so the last argument that sets the key
/// wins. An unresolved argument can set the key, so it makes the value
/// dynamic unless a later argument sets the key.
fn merged_property<'a>(
    arguments: &[Option<&'a ObjectExpression<'a>>],
    key: &str,
) -> ScopeValue<&'a Expression<'a>> {
    for argument in arguments.iter().rev() {
        let Some(object) = argument else {
            return ScopeValue::Dynamic;
        };
        match scope_property(object, key) {
            ScopeValue::Absent => {}
            value => return value,
        }
    }
    ScopeValue::Absent
}

/// The `projects` of a `defineConfig(a, b, ...)` call. Playwright merges the
/// projects of the arguments by name, so the projects are static only when
/// one argument gives them.
fn merged_projects<'a>(
    arguments: &[Option<&'a ObjectExpression<'a>>],
) -> ScopeValue<&'a Expression<'a>> {
    let mut projects = ScopeValue::Absent;
    for argument in arguments {
        let Some(object) = argument else {
            return ScopeValue::Dynamic;
        };
        match (scope_property(object, "projects"), &projects) {
            (ScopeValue::Absent, _) => {}
            (value, ScopeValue::Absent) => projects = value,
            _ => return ScopeValue::Dynamic,
        }
    }
    projects
}

/// Known projects keep their entries even when another project is dynamic.
/// Unknown projects also keep the top-level scope and conventional defaults.
fn config_test_scopes<'a>(
    top: TestScope,
    projects: ScopeValue<&'a Expression<'a>>,
) -> Vec<TestScope> {
    let inherited = TestScope {
        test_dir: top.test_dir.clone(),
        test_match: ScopeValue::Dynamic,
    };
    let unknown = TestScope {
        test_dir: ScopeValue::Dynamic,
        test_match: ScopeValue::Dynamic,
    };
    let projects = match projects {
        ScopeValue::Absent => return vec![top],
        ScopeValue::Static(expr) => expr,
        ScopeValue::Dynamic => return vec![inherited, unknown],
    };
    let Some(projects) = config_parser::array_expression(projects) else {
        return vec![inherited, unknown];
    };
    if projects.elements.is_empty() {
        return vec![top];
    }
    let mut scopes = Vec::new();
    for element in &projects.elements {
        let Some(project) = element
            .as_expression()
            .and_then(config_parser::object_expression)
        else {
            scopes.extend([inherited.clone(), unknown.clone()]);
            continue;
        };
        let own = scope_of(project);
        if matches!(own.test_dir, ScopeValue::Dynamic) {
            scopes.push(TestScope {
                test_dir: top.test_dir.clone(),
                test_match: own.test_match.or_inherit(&top.test_match),
            });
        }
        scopes.push(TestScope {
            test_dir: own.test_dir.or_inherit(&top.test_dir),
            test_match: own.test_match.or_inherit(&top.test_match),
        });
    }
    scopes
}

fn scope_of(obj: &ObjectExpression<'_>) -> TestScope {
    scope_from(
        scope_property(obj, "testDir"),
        scope_property(obj, "testMatch"),
    )
}

fn scope_from(
    test_dir: ScopeValue<&Expression<'_>>,
    test_match: ScopeValue<&Expression<'_>>,
) -> TestScope {
    let test_dir = match test_dir {
        ScopeValue::Absent => ScopeValue::Absent,
        ScopeValue::Dynamic => ScopeValue::Dynamic,
        ScopeValue::Static(expr) => config_parser::expression_to_path_string(expr)
            .map_or(ScopeValue::Dynamic, ScopeValue::Static),
    };
    let test_match = match test_match {
        ScopeValue::Absent => ScopeValue::Absent,
        ScopeValue::Dynamic => ScopeValue::Dynamic,
        ScopeValue::Static(expr) => {
            static_globs(expr).map_or(ScopeValue::Dynamic, ScopeValue::Static)
        }
    };
    TestScope {
        test_dir,
        test_match,
    }
}

/// The last assignment wins. A later unresolved spread or computed key can
/// override this field, so it cannot safely narrow the test entries.
fn scope_property<'a>(obj: &'a ObjectExpression<'a>, key: &str) -> ScopeValue<&'a Expression<'a>> {
    for property in obj.properties.iter().rev() {
        match property {
            ObjectPropertyKind::SpreadProperty(_) => return ScopeValue::Dynamic,
            ObjectPropertyKind::ObjectProperty(property) => {
                if property.computed {
                    return ScopeValue::Dynamic;
                }
                if property.key.static_name().is_some_and(|name| name == key) {
                    return ScopeValue::Static(&property.value);
                }
            }
        }
    }
    ScopeValue::Absent
}

/// Extglob group openers. Playwright matches `testMatch` with extglob
/// support, for example `**/*.@(spec|test).ts`, but the entry glob matcher
/// reads these characters literally, so such a glob matches no file.
const EXTGLOB_OPENERS: [&str; 5] = ["@(", "?(", "+(", "*(", "!("];

/// The globs of a string or string-array `testMatch`. Returns `None` for a
/// regular expression, a non-literal value, a glob that names a path outside
/// `testDir`, a glob with an extglob group, or a glob with a directory part.
///
/// Playwright adds a `**/` prefix and matches the glob against the absolute
/// file path. Thus the directory part of a glob such as `tests/**/*.e2e.ts`
/// can match `testDir` itself or a parent directory of `testDir`. A glob that
/// is relative to `testDir` cannot show that match, so such a `testMatch`
/// keeps every script file below `testDir`.
fn static_globs(expr: &Expression<'_>) -> Option<Vec<String>> {
    let globs = match config_parser::array_expression(expr) {
        Some(array) => array
            .elements
            .iter()
            .map(|element| config_parser::expression_to_path_string(element.as_expression()?))
            .collect::<Option<Vec<_>>>()?,
        None => vec![config_parser::expression_to_path_string(expr)?],
    };
    let in_test_dir = |glob: &String| {
        !glob.is_empty() && !glob.starts_with('/') && !glob.split('/').any(|part| part == "..")
    };
    let file_name_only = |glob: &String| !glob.strip_prefix("**/").unwrap_or(glob).contains('/');
    let usable = |glob: &String| {
        in_test_dir(glob)
            && file_name_only(glob)
            && glob.is_ascii()
            && !glob.contains(['[', '\\'])
            && !EXTGLOB_OPENERS.iter().any(|opener| glob.contains(opener))
    };
    (!globs.is_empty() && globs.iter().all(usable)).then_some(globs)
}

/// Playwright matches filenames without case sensitivity. Globset has no
/// per-pattern flag in entry rules, so expand ASCII letters into character
/// classes. Patterns with existing classes or escapes use the broad fallback.
fn case_insensitive_glob(glob: &str) -> String {
    let mut pattern = String::new();
    for ch in glob.chars() {
        if ch.is_ascii_alphabetic() {
            pattern.push('[');
            pattern.push(ch.to_ascii_lowercase());
            pattern.push(ch.to_ascii_uppercase());
            pattern.push(']');
        } else {
            pattern.push(ch);
        }
    }
    pattern
}

/// `testDir` as a project-root-relative directory prefix with a trailing `/`,
/// or an empty prefix for the root itself. `testDir` is relative to the
/// config file directory. Returns `None` for a directory outside the root.
fn test_dir_prefix(test_dir: &str, root: &Path, config_dir: &Path) -> Option<String> {
    let dir = config_parser::lexical_normalize(
        &config_dir.join(config_parser::path_from_config_string(test_dir)),
    );
    let relative = dir
        .strip_prefix(config_parser::lexical_normalize(root))
        .ok()?;
    let relative = globset::escape(&config_parser::path_to_config_string(relative));
    Some(if relative.is_empty() {
        String::new()
    } else {
        format!("{relative}/")
    })
}

/// Parse Playwright `webServer.command` entries (object and array forms) into
/// referenced dependencies and reachable setup files.
///
/// Each command is run through the shared script parser ([`scripts::analyze_command`]),
/// so invoked npm binaries are credited as dependencies and local file arguments are
/// seeded as support entry files exactly as they would be in a package.json script.
/// `config_dir` is the directory of the config file: file arguments resolve there by
/// default, matching Playwright's `webServer.cwd` default. A `webServer.cwd` (per
/// object, or per array element) overrides that base, resolved relative to `config_dir`
/// (an absolute cwd replaces it). `root` is the project root, used only for
/// binary-to-package resolution (it owns `node_modules`). Commands that delegate to a
/// package manager (`npm run start`, `yarn dev`) credit nothing, since the underlying
/// script's own dependencies are analyzed separately.
fn collect_web_server(
    source: &str,
    config_path: &Path,
    root: &Path,
    config_dir: &Path,
) -> (Vec<String>, Vec<PathBuf>) {
    let mut commands: Vec<(String, Option<String>)> = Vec::new();

    if let Some(command) =
        config_parser::extract_config_command(source, config_path, &["webServer", "command"])
    {
        let cwd = config_parser::extract_config_string(source, config_path, &["webServer", "cwd"]);
        commands.push((command, cwd));
    }

    commands.extend(config_parser::extract_config_array_object_command_pairs(
        source,
        config_path,
        &["webServer"],
        "command",
        "cwd",
    ));

    let mut referenced_dependencies = Vec::new();
    let mut setup_files = Vec::new();

    for (command, cwd) in commands {
        let analysis = scripts::analyze_command(&command, root, &FxHashMap::default());
        referenced_dependencies.extend(analysis.used_packages);

        let base = cwd.map_or_else(
            || config_dir.to_path_buf(),
            |dir| config_dir.join(dir.trim_start_matches("./")),
        );
        for file in analysis
            .config_files
            .into_iter()
            .chain(analysis.entry_files)
        {
            setup_files.push(base.join(file.trim_start_matches("./")));
        }
    }

    (referenced_dependencies, setup_files)
}

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

    #[test]
    fn resolve_config_global_setup() {
        let source = r#"
            export default {
                globalSetup: "./global-setup.ts"
            };
        "#;
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/global-setup.ts")]
        );
    }

    #[test]
    fn resolve_config_global_teardown() {
        let source = r#"
            export default {
                globalTeardown: "./global-teardown.ts"
            };
        "#;
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/global-teardown.ts")]
        );
    }

    #[test]
    fn resolve_config_both_setup_and_teardown() {
        let source = r#"
            export default {
                globalSetup: "./setup.ts",
                globalTeardown: "./teardown.ts"
            };
        "#;
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert_eq!(
            result.setup_files,
            vec![
                Path::new("/project/setup.ts"),
                Path::new("/project/teardown.ts"),
            ]
        );
    }

    #[test]
    fn resolve_config_imports() {
        let source = r#"
            import { defineConfig, devices } from '@playwright/test';
            export default defineConfig({
                globalSetup: "./setup.ts"
            });
        "#;
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"@playwright/test".to_string())
        );
        assert_eq!(result.setup_files, vec![Path::new("/project/setup.ts")]);
    }

    #[test]
    fn resolve_config_empty() {
        let source = r"export default {};";
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert!(result.setup_files.is_empty());
        assert!(result.referenced_dependencies.is_empty());
    }

    #[test]
    fn resolve_config_setup_strips_dot_slash() {
        let source = r#"
            export default {
                globalSetup: "./tests/global-setup.ts"
            };
        "#;
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/tests/global-setup.ts")]
        );
    }

    #[test]
    fn resolve_config_setup_without_dot_slash() {
        let source = r#"
            export default {
                globalSetup: "tests/global-setup.ts"
            };
        "#;
        let plugin = PlaywrightPlugin;
        let result = plugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        );
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/tests/global-setup.ts")]
        );
    }

    #[test]
    fn fixture_patterns_are_set() {
        let plugin = PlaywrightPlugin;
        assert!(!plugin.fixture_glob_patterns().is_empty());
    }

    fn resolve(source: &str) -> PluginResult {
        PlaywrightPlugin.resolve_config(
            Path::new("playwright.config.ts"),
            source,
            Path::new("/project"),
        )
    }

    #[test]
    fn web_server_object_command_credits_cli_dependency() {
        let source = r#"
            export default {
                webServer: { command: "srvx --port 3000", url: "http://localhost:3000" }
            };
        "#;
        let result = resolve(source);
        assert!(
            result.referenced_dependencies.contains(&"srvx".to_string()),
            "srvx CLI binary should be credited, got {:?}",
            result.referenced_dependencies
        );
        assert!(
            result.setup_files.is_empty(),
            "a flag-only command seeds no files, got {:?}",
            result.setup_files
        );
    }

    #[test]
    fn web_server_template_command_credits_pnpm_exec_cli_dependency() {
        let source = r"
            const PORT = 3000;
            export default {
                webServer: {
                    command: `pnpm build && pnpm exec srvx --prod --port ${PORT} --hostname 127.0.0.1`
                }
            };
        ";
        let result = resolve(source);
        assert!(
            result.referenced_dependencies.contains(&"srvx".to_string()),
            "srvx CLI binary should be credited from pnpm exec template command, got {:?}",
            result.referenced_dependencies
        );
    }

    #[test]
    fn web_server_array_template_command_credits_pnpm_exec_cli_dependency() {
        let source = r"
            const PORT = 3000;
            export default {
                webServer: [
                    {
                        command: `pnpm exec srvx --prod --port ${PORT}`,
                    },
                ],
            };
        ";
        let result = resolve(source);
        assert!(
            result.referenced_dependencies.contains(&"srvx".to_string()),
            "srvx CLI binary should be credited from array template command, got {:?}",
            result.referenced_dependencies
        );
    }

    #[test]
    fn web_server_array_node_runner_seeds_file_and_credits_runner() {
        let source = r#"
            export default {
                webServer: [{ command: "tsx scripts/e2e-server.ts" }]
            };
        "#;
        let result = resolve(source);
        assert!(
            result.referenced_dependencies.contains(&"tsx".to_string()),
            "tsx node runner should be credited, got {:?}",
            result.referenced_dependencies
        );
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/scripts/e2e-server.ts")]
        );
    }

    #[test]
    fn web_server_object_command_honors_cwd() {
        let source = r#"
            export default {
                webServer: { command: "node server.js", cwd: "packages/api" }
            };
        "#;
        let result = resolve(source);
        assert!(
            !result.referenced_dependencies.contains(&"node".to_string()),
            "node must not be credited as a dependency, got {:?}",
            result.referenced_dependencies
        );
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/packages/api/server.js")],
            "server.js must resolve under webServer.cwd"
        );
    }

    #[test]
    fn web_server_array_per_element_cwd() {
        let source = r#"
            export default {
                webServer: [
                    { command: "tsx scripts/api.ts", cwd: "packages/api" },
                    { command: "tsx scripts/web.ts" }
                ]
            };
        "#;
        let result = resolve(source);
        assert!(
            result
                .setup_files
                .contains(&PathBuf::from("/project/packages/api/scripts/api.ts"))
        );
        assert!(
            result
                .setup_files
                .contains(&PathBuf::from("/project/scripts/web.ts"))
        );
    }

    #[test]
    fn web_server_package_manager_delegation_is_noop() {
        let source = r#"
            export default {
                webServer: { command: "npm run start" }
            };
        "#;
        let result = resolve(source);
        assert!(
            result.referenced_dependencies.is_empty(),
            "npm run delegation must not credit a phantom dependency, got {:?}",
            result.referenced_dependencies
        );
        assert!(result.setup_files.is_empty());
    }

    #[test]
    fn web_server_and_global_setup_coexist() {
        let source = r#"
            export default {
                globalSetup: "./setup.ts",
                webServer: { command: "tsx scripts/e2e-server.ts" }
            };
        "#;
        let result = resolve(source);
        assert!(
            result
                .setup_files
                .contains(&PathBuf::from("/project/setup.ts"))
        );
        assert!(
            result
                .setup_files
                .contains(&PathBuf::from("/project/scripts/e2e-server.ts"))
        );
    }

    #[test]
    fn web_server_strips_leading_dot_slash_in_file_args() {
        let source = r#"
            export default {
                webServer: { command: "tsx ./scripts/e2e-server.ts" }
            };
        "#;
        let result = resolve(source);
        assert_eq!(
            result.setup_files,
            vec![Path::new("/project/scripts/e2e-server.ts")]
        );
    }

    #[test]
    fn no_web_server_seeds_nothing() {
        let source = r#"
            export default {
                globalSetup: "./setup.ts"
            };
        "#;
        let result = resolve(source);
        assert_eq!(result.setup_files, vec![Path::new("/project/setup.ts")]);
    }

    /// Build a platform-absolute path from a `/project/...`-style logical path.
    /// On Windows a leading-slash path lacks a drive and is NOT absolute, so the
    /// `config_path.parent().is_absolute()` gate in `resolve_config` would fall
    /// back to the root and drop the nested config directory. The registry
    /// always passes a genuinely-absolute config path at runtime (drive-rooted
    /// on Windows), so these tests must do the same. On Unix this is the identity.
    fn abs(logical: &str) -> PathBuf {
        #[cfg(windows)]
        {
            PathBuf::from(format!("C:{}", logical.replace('/', "\\")))
        }
        #[cfg(not(windows))]
        {
            PathBuf::from(logical)
        }
    }

    /// Resolve with an absolute, nested config path (as the registry passes at
    /// runtime), to exercise the config-file-directory base.
    fn resolve_at(config_path: &str, source: &str) -> PluginResult {
        PlaywrightPlugin.resolve_config(&abs(config_path), source, &abs("/project"))
    }

    #[test]
    fn web_server_file_args_resolve_from_nested_config_dir_not_root() {
        let source = r#"
            export default {
                webServer: { command: "tsx scripts/e2e-server.ts" }
            };
        "#;
        let result = resolve_at("/project/apps/web/playwright.config.ts", source);
        assert_eq!(
            result.setup_files,
            vec![abs("/project/apps/web/scripts/e2e-server.ts")],
            "nested-config file args must resolve under the config directory, not the project root"
        );
    }

    #[test]
    fn web_server_nested_config_cwd_resolves_relative_to_config_dir() {
        let source = r#"
            export default {
                webServer: { command: "tsx scripts/server.ts", cwd: "api" }
            };
        "#;
        let result = resolve_at("/project/apps/web/playwright.config.ts", source);
        assert_eq!(
            result.setup_files,
            vec![abs("/project/apps/web/api/scripts/server.ts")],
            "cwd must resolve relative to the config directory"
        );
    }

    fn entry_patterns(result: &PluginResult) -> Vec<&str> {
        result
            .entry_patterns
            .iter()
            .map(|rule| rule.pattern.as_str())
            .collect()
    }

    fn default_patterns() -> Vec<&'static str> {
        DEFAULT_TEST_ENTRY_PATTERNS.to_vec()
    }

    /// The patterns that the config adds after the kept file-name patterns.
    fn test_dir_patterns(result: &PluginResult) -> Vec<&str> {
        let patterns = entry_patterns(result);
        let (file_names, test_dir) = patterns.split_at(TEST_FILE_NAME_PATTERNS.len());
        assert_eq!(file_names, TEST_FILE_NAME_PATTERNS);
        test_dir.to_vec()
    }

    #[test]
    fn test_dir_replaces_default_test_directories() {
        let result = resolve(r"export default defineConfig({ testDir: './ui' });");
        assert!(result.replace_entry_patterns);
        let patterns = entry_patterns(&result);
        assert!(
            !patterns.contains(&"tests/**/*.{ts,tsx,js,jsx}")
                && !patterns.contains(&"e2e/**/*.{ts,tsx,js,jsx}"),
            "a static testDir drops the default test directories, got {patterns:?}"
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "ui/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}"
            ]
        );
    }

    #[test]
    fn test_dir_resolves_from_nested_config_dir() {
        let result = resolve_at(
            "/project/e2e/playwright.config.ts",
            r"export default defineConfig({ testDir: './ui' });",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "e2e/ui/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}"
            ]
        );
    }

    #[test]
    fn test_dir_dot_selects_the_config_dir() {
        let result = resolve_at(
            "/project/e2e/playwright.config.ts",
            r"export default { testDir: '.' };",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "e2e/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}"
            ]
        );
    }

    #[test]
    fn test_dir_with_dirname_join() {
        let result = resolve(
            r"
            import path from 'node:path';
            export default { testDir: path.join(__dirname, 'specs') };
        ",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "specs/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}"
            ]
        );
    }

    #[test]
    fn project_test_dirs_each_give_entries() {
        let result = resolve(
            r"export default defineConfig({
                projects: [{ testDir: './a' }, { testDir: './b' }],
            });",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "a/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}",
                "b/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}",
            ]
        );
    }

    #[test]
    fn project_inherits_top_level_test_dir_and_test_match() {
        let result = resolve(
            r"export default defineConfig({
                testDir: './tests',
                testMatch: '*.e2e.ts',
                projects: [{ name: 'chromium' }, { testDir: './smoke' }],
            });",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "tests/**/*.[eE]2[eE].[tT][sS]",
                "smoke/**/*.[eE]2[eE].[tT][sS]"
            ]
        );
    }

    #[test]
    fn glob_test_match_applies_below_test_dir() {
        let result = resolve(
            r"export default {
                testDir: './ui',
                testMatch: ['**/*.flow.ts', '*.check.ts'],
            };",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "ui/**/*.[fF][lL][oO][wW].[tT][sS]",
                "ui/**/*.[cC][hH][eE][cC][kK].[tT][sS]"
            ]
        );
    }

    #[test]
    fn test_match_with_test_dir_name_keeps_files_in_test_dir() {
        let result = resolve(
            r"export default {
                testDir: './tests',
                testMatch: 'tests/**/*.e2e.ts',
            };",
        );
        let patterns = test_dir_patterns(&result);
        assert_eq!(patterns, vec!["tests/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}"]);
        let matcher = globset::Glob::new(patterns[0]).unwrap().compile_matcher();
        assert!(matcher.is_match("tests/login.e2e.ts"));
    }

    #[test]
    fn test_match_with_directory_part_keeps_every_script_below_test_dir() {
        let result = resolve(
            r"export default {
                testDir: './e2e',
                testMatch: ['**/*.flow.ts', 'e2e/*.pw.ts'],
            };",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec!["e2e/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}"]
        );
    }

    #[test]
    fn extglob_test_match_keeps_every_script_below_test_dir() {
        let result = resolve(
            r"export default {
                testDir: './e2e',
                testMatch: '**/*.@(e2e|smoke).ts',
            };",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec!["e2e/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}"]
        );
    }

    #[test]
    fn extglob_in_one_test_match_glob_keeps_every_script_below_test_dir() {
        let result = resolve(
            r"export default {
                testDir: './e2e',
                testMatch: ['**/*.flow.ts', '**/*.@(spec|test).?(c|m)[jt]s?(x)'],
            };",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec!["e2e/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}"]
        );
    }

    #[test]
    fn regex_test_match_keeps_every_script_below_test_dir() {
        let result = resolve(
            r"export default {
                testDir: './ui',
                projects: [
                    { name: 'setup', testMatch: /global\.setup\.ts/ },
                    { name: 'chromium' },
                ],
            };",
        );
        assert_eq!(
            test_dir_patterns(&result),
            vec![
                "ui/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}",
                "ui/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}",
            ]
        );
    }

    #[test]
    fn missing_test_dir_restates_default_entries() {
        let result = resolve(r"export default defineConfig({ timeout: 1000 });");
        assert!(result.replace_entry_patterns);
        assert_eq!(entry_patterns(&result), default_patterns());
    }

    #[test]
    fn missing_test_dir_applies_test_match_below_config_dir() {
        let result = resolve_at(
            "/project/web/playwright.config.ts",
            r"export default defineConfig({ testMatch: '*.e2e.ts' });",
        );
        assert_eq!(
            test_dir_patterns(&result),
            [
                DEFAULT_TEST_ENTRY_PATTERNS[TEST_FILE_NAME_PATTERNS.len()..].to_vec(),
                vec!["web/**/*.[eE]2[eE].[tT][sS]"],
            ]
            .concat()
        );
    }

    #[test]
    fn define_config_arguments_merge_with_last_wins() {
        for (source, selected, rejected) in [
            (
                "export default defineConfig({ testDir: './a' }, { testDir: './ui', testMatch: '*.pw.ts' });",
                "ui/live.pw.ts",
                "a/live.pw.ts",
            ),
            (
                "export default defineConfig({ testDir: './ui', testMatch: '*.pw.ts' }, { timeout: 1000 });",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "const base = { testMatch: '*.pw.ts' }; export default defineConfig(base, { testDir: './ui' });",
                "ui/live.pw.ts",
                "ui/live.other.ts",
            ),
            (
                "import base from './base'; export default defineConfig(base, { testDir: './ui' });",
                "ui/live.other.ts",
                "outside/live.other.ts",
            ),
            (
                "import base from './base'; export default defineConfig({ testDir: './wrong' }, base);",
                "tests/live.pw.ts",
                "outside/live.other.ts",
            ),
            (
                "const config = defineConfig(shared, { testDir: './ui' }); export default config;",
                "ui/live.other.ts",
                "outside/live.other.ts",
            ),
            (
                "export default defineConfig({ testDir: './ui', projects: [{ testDir: './a' }] }, { projects: [{ testDir: './b' }] });",
                "ui/live.other.ts",
                "outside/live.other.ts",
            ),
        ] {
            let result = resolve(source);
            let matchers: Vec<_> = result
                .entry_patterns
                .iter()
                .map(|rule| {
                    globset::GlobBuilder::new(&rule.pattern)
                        .literal_separator(true)
                        .build()
                        .unwrap()
                        .compile_matcher()
                })
                .collect();
            assert!(
                matchers.iter().any(|matcher| matcher.is_match(selected)),
                "the config selects {selected}: {source}"
            );
            assert!(
                !matchers.iter().any(|matcher| matcher.is_match(rejected)),
                "the config does not select {rejected}: {source}"
            );
        }
    }

    #[test]
    fn project_without_test_dir_restates_default_entries() {
        let result = resolve(
            r"export default defineConfig({
                projects: [{ testDir: './a' }, { name: 'chromium' }],
            });",
        );
        assert_eq!(
            test_dir_patterns(&result),
            [
                vec!["a/**/*.{[sS][pP][eE][cC],[tT][eE][sS][tT]}.{[tT][sS],[tT][sS][xX],[jJ][sS],[jJ][sS][xX],[mM][tT][sS],[cC][tT][sS],[mM][jJ][sS],[cC][jJ][sS]}"],
                DEFAULT_TEST_ENTRY_PATTERNS[TEST_FILE_NAME_PATTERNS.len()..].to_vec(),
            ]
            .concat()
        );
    }

    #[test]
    fn dynamic_test_dir_restates_default_entries() {
        let result = resolve(r"export default { testDir: process.env.TEST_DIR };");
        assert_eq!(entry_patterns(&result), default_patterns());
    }

    #[test]
    fn test_dir_outside_root_restates_default_entries() {
        let result = resolve(r"export default { testDir: '../outside' };");
        assert_eq!(entry_patterns(&result), default_patterns());
    }

    #[test]
    fn config_overrides_keep_the_selected_files() {
        for (source, selected, rejected) in [
            (
                "export default { testDir: './wrong', testDir: './ui', testMatch: '*.pw.ts' };",
                "ui/live.pw.ts",
                "wrong/live.pw.ts",
            ),
            (
                "export default { testDir: './ui', testMatch: '*.wrong.ts', testMatch: '*.pw.ts' };",
                "ui/live.pw.ts",
                "ui/live.wrong.ts",
            ),
            (
                "export default { projects: [{testDir:'./wrong'}], projects: [{testDir:'./ui', testMatch:'*.pw.ts'}] };",
                "ui/live.pw.ts",
                "wrong/live.pw.ts",
            ),
            (
                "export default { projects: [{...shared, testDir:'./ui'}] };",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default {...shared, testDir:'./ui', testMatch:'*.pw.ts'};",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default {testDir:'./ui', testMatch:'*.pw.ts', projects:[...sharedProjects]};",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default {testDir:'./ui',testMatch:'*.wrong.ts',projects:sharedProjects};",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default {projects:[{testDir:'./ui',testMatch:'*.pw.ts'},...sharedProjects]};",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default {testDir:'./ui',testMatch:'*.pw.ts',projects:[{name:'chromium',...devices['Desktop Chrome']}]};",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default {testDir:'./ui',testMatch:'*.wrong.ts',projects:[{...shared}]};",
                "ui/live.pw.ts",
                "outside/live.pw.ts",
            ),
            (
                "export default { testDir:'./ui', [key]:'./tests', testMatch:'*.pw.ts', projects:[{name:'chromium'}] };",
                "tests/live.pw.ts",
                "outside/live.pw.ts",
            ),
        ] {
            let result = resolve(source);
            let matchers: Vec<_> = result
                .entry_patterns
                .iter()
                .map(|rule| {
                    globset::GlobBuilder::new(&rule.pattern)
                        .literal_separator(true)
                        .build()
                        .unwrap()
                        .compile_matcher()
                })
                .collect();
            assert!(
                matchers.iter().any(|matcher| matcher.is_match(selected)),
                "the config selects {selected}: {source}"
            );
            assert!(
                !matchers.iter().any(|matcher| matcher.is_match(rejected)),
                "the config does not select {rejected}: {source}"
            );
        }
    }

    #[test]
    fn global_setup_resolves_from_nested_config_dir() {
        let source = r#"
            export default {
                globalSetup: "./setup.ts"
            };
        "#;
        let result = resolve_at("/project/apps/web/playwright.config.ts", source);
        assert_eq!(
            result.setup_files,
            vec![abs("/project/apps/web/setup.ts")],
            "globalSetup must resolve under the config directory"
        );
    }
}