deno 2.9.0

Provides the deno executable
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
// Copyright 2018-2026 the Deno authors. MIT license.

//! Framework detection for `deno compile .`.
//!
//! Detects web frameworks (Next.js, Astro, Remix, SvelteKit, Nuxt, Fresh,
//! SolidStart, TanStack Start, Vite) and generates the appropriate
//! entrypoint and include paths so that `deno compile .` just works.

use std::path::Path;
use std::path::PathBuf;

use deno_core::error::AnyError;
use deno_core::serde_json;

/// Result of framework detection.
pub struct FrameworkDetection {
  /// Name of the detected framework (for display).
  pub name: &'static str,
  /// Generated entrypoint TypeScript/JavaScript code (production).
  pub entrypoint_code: String,
  /// Directories to include in the compiled binary.
  pub include_paths: Vec<String>,
  /// Optional build command to run before compilation (e.g. "next build").
  /// The command is run with the detected directory as cwd.
  pub build_command: Option<Vec<String>>,
}

impl FrameworkDetection {
  /// Directories (relative to the project root) where the framework keeps
  /// static assets like favicons. Used to auto-detect a desktop app icon
  /// when the user didn't supply `--icon`.
  pub fn static_asset_dirs(&self) -> &'static [&'static str] {
    match self.name {
      // Next.js App Router puts `favicon.ico` / `icon.*` directly in `app/`
      // (or `src/app/`); the Pages Router and older versions use `public/`.
      "Next.js" => &["public", "app", "src/app"],
      "Fresh" | "SvelteKit" => &["static"],
      _ => &["public"],
    }
  }
}

/// Search a framework's static asset directories for a favicon that can
/// double as the desktop app icon. Returns the first match in priority
/// order, restricted to file formats supported by `target_os` (so we
/// don't pick a `.png` for a Windows build where bundling would silently
/// drop it).
pub fn find_framework_favicon(
  dir: &Path,
  detection: &FrameworkDetection,
  target_os: &str,
) -> Option<PathBuf> {
  let exts: &[&str] = match target_os {
    "macos" => &["icns", "png"],
    "windows" => &["ico"],
    _ => &["png"],
  };
  let names = ["icon", "favicon", "apple-touch-icon", "logo"];
  for sub in detection.static_asset_dirs() {
    let base = dir.join(sub);
    if !base.is_dir() {
      continue;
    }
    for name in names {
      for ext in exts {
        let candidate = base.join(format!("{name}.{ext}"));
        if candidate.is_file() {
          return Some(candidate);
        }
      }
    }
  }
  None
}

/// Detect a web framework in the given directory.
///
/// Detection priority:
/// 1. Config-file based detection (highest priority)
/// 2. Package.json dependency-based detection
/// 3. deno.json import-based detection
pub fn detect_framework(
  dir: &Path,
) -> Result<Option<FrameworkDetection>, AnyError> {
  // --- Config-file based detection (highest priority) ---

  // Next.js: next.config.{js,mjs,ts}
  if has_config_file(dir, "next.config") {
    return Ok(Some(detect_nextjs(dir)?));
  }

  // Fresh: fresh.gen.ts or _fresh/
  if dir.join("fresh.gen.ts").exists() || dir.join("_fresh").is_dir() {
    return Ok(Some(detect_fresh(dir)));
  }

  // Astro: astro.config.{mjs,ts,js}
  if has_config_file(dir, "astro.config") {
    return Ok(Some(detect_astro(dir)));
  }

  // Nuxt: nuxt.config.{ts,js,mjs}
  if has_config_file(dir, "nuxt.config") {
    return Ok(Some(detect_nuxt(dir)));
  }

  // SvelteKit: svelte.config.{js,ts} — but only when there's positive
  // evidence of a Deno-targeted adapter or a recognized server output
  // shape. A bare svelte.config.* is not enough, since SvelteKit can be
  // built with many adapters (node, vercel, cloudflare, static, ...) that
  // do not produce `./.output/server/index.{ts,mjs}`.
  if has_config_file(dir, "svelte.config")
    && let Some(detection) = detect_sveltekit(dir)
  {
    return Ok(Some(detection));
  }

  // --- Package.json dependency-based detection ---
  if let Some(deps) = read_package_deps(dir) {
    // Remix
    if deps.has("@remix-run/react") || deps.has_dev("@remix-run/dev") {
      return Ok(Some(detect_remix(dir)));
    }

    // SolidStart
    if deps.has("@solidjs/start") {
      return Ok(Some(detect_nitro_framework(dir, "SolidStart")));
    }

    // TanStack Start
    if deps.has("@tanstack/react-start") || deps.has("@tanstack/solid-start") {
      return Ok(Some(detect_nitro_framework(dir, "TanStack Start")));
    }
  }

  // --- Vite (lowest priority among bundlers) ---
  // A generic Vite project, recognized by its config file or a `vite`
  // dependency. Many meta-frameworks build on Vite, but those are all matched
  // earlier (by their own config file or framework dependency), so reaching
  // here means a plain Vite app (SPA/MPA or a hand-rolled SSR server).
  let has_vite_dep = read_package_deps(dir)
    .map(|deps| deps.has("vite") || deps.has_dev("vite"))
    .unwrap_or(false);
  if has_config_file(dir, "vite.config") || has_vite_dep {
    return Ok(Some(detect_vite(dir)));
  }

  // --- deno.json import-based detection ---
  if let Some(imports) = read_deno_json_imports(dir)
    && imports
      .iter()
      .any(|i| i.starts_with("fresh") || i.starts_with("@fresh/core"))
  {
    return Ok(Some(detect_fresh(dir)));
  }

  Ok(None)
}

// --- Framework-specific detection ---

fn deno_exe() -> String {
  std::env::current_exe()
    .map(|p| p.display().to_string())
    .unwrap_or_else(|_| "deno".into())
}

fn deno_task_build() -> Vec<String> {
  vec![deno_exe(), "task".into(), "build".into()]
}

fn detect_nextjs(dir: &Path) -> Result<FrameworkDetection, AnyError> {
  let version = detect_package_version(dir, "next").unwrap_or(15);
  let entrypoint = format!(
    r#"// @ts-nocheck
import {{ nextStart }} from "npm:next@^{version}/dist/cli/next-start.js";
globalThis.addEventListener("unhandledrejection", (e) => {{
  console.error("[entrypoint] Unhandled rejection:", e.reason);
  if (e.reason?.stack) console.error("[entrypoint] Stack:", e.reason.stack);
}});
// Guard: skip for forked workers (child_process.fork sets NODE_CHANNEL_FD).
// Workers use override_main_module to run their target script directly.
if (!Deno.env.get("NODE_CHANNEL_FD")) {{
  // Use import.meta.dirname so paths resolve against the VFS in the
  // compiled binary rather than the runtime CWD.
  await nextStart({{ hostname: "0.0.0.0" }}, import.meta.dirname);
}}
"#,
  );
  // `next-server` serves files in `public/` at the URL root; without it
  // shipped, every `<img src="/foo.png">` 404s. Optional in the project
  // (some apps put nothing there), so only include when present.
  let mut include_paths = vec![".next".into()];
  if dir.join("public").is_dir() {
    include_paths.push("public".into());
  }
  Ok(FrameworkDetection {
    name: "Next.js",
    entrypoint_code: entrypoint,
    include_paths,
    build_command: Some(deno_task_build()),
  })
}

fn detect_astro(_dir: &Path) -> FrameworkDetection {
  FrameworkDetection {
    name: "Astro",
    entrypoint_code: "// @ts-nocheck\nimport \"./dist/server/entry.mjs\";\n"
      .into(),
    include_paths: vec!["dist".into()],
    build_command: Some(deno_task_build()),
  }
}

fn detect_fresh(dir: &Path) -> FrameworkDetection {
  // Fresh 2.x uses _fresh/server.js (build output) or imports @fresh/core
  // in deno.json. We intentionally do NOT use `fresh.gen.ts + deno.json`
  // as a heuristic because Fresh 1 projects also have both of those files.
  let is_fresh2 = dir.join("_fresh/server.js").exists()
    || read_deno_json_imports(dir)
      .map(|imports| imports.iter().any(|i| i.starts_with("@fresh/core")))
      .unwrap_or(false);
  if is_fresh2 {
    // `_fresh/snapshot.js` records static assets as `filePath:
    // "static/foo.png"` and `_fresh/server.js` constructs the
    // ProdBuildCache with `root = path.join(import.meta.dirname, "..")`,
    // so the runtime reads them via `<root>/static/...`. The `static/`
    // directory must therefore land in the VFS alongside `_fresh/` or
    // every image / font / video 404s. `static/` is conventional for
    // Fresh; if it doesn't exist the include is a harmless no-op.
    let mut include_paths = vec!["_fresh".into()];
    if dir.join("static").is_dir() {
      include_paths.push("static".into());
    }
    FrameworkDetection {
      name: "Fresh",
      entrypoint_code: r#"// @ts-nocheck
const mod = await import("./_fresh/server.js");
Deno.serve(mod.default.fetch);
"#
      .into(),
      include_paths,
      build_command: Some(vec![deno_exe(), "task".into(), "build".into()]),
    }
  } else {
    // Fresh 1.x — no build step needed, server-rendered
    FrameworkDetection {
      name: "Fresh",
      entrypoint_code: "// @ts-nocheck\nimport \"./main.ts\";\n".into(),
      include_paths: vec![],
      build_command: None,
    }
  }
}

fn detect_remix(dir: &Path) -> FrameworkDetection {
  // `remix-serve` serves files from `public/` at the URL root; ship it
  // when present so static assets resolve.
  let mut include_paths = vec!["build".into()];
  if dir.join("public").is_dir() {
    include_paths.push("public".into());
  }
  FrameworkDetection {
    name: "Remix",
    entrypoint_code:
      "// @ts-nocheck\nimport \"./node_modules/.bin/remix-serve\";\n".into(),
    include_paths,
    build_command: Some(deno_task_build()),
  }
}

fn detect_nuxt(dir: &Path) -> FrameworkDetection {
  detect_nitro_framework(dir, "Nuxt")
}

fn detect_sveltekit(dir: &Path) -> Option<FrameworkDetection> {
  // Prefer post-build evidence of a supported output shape, since that
  // proves which adapter was actually used.
  if dir.join(".deno-deploy/server.ts").exists() {
    return Some(FrameworkDetection {
      name: "SvelteKit",
      entrypoint_code: "// @ts-nocheck\nimport \"./.deno-deploy/server.ts\";\n"
        .into(),
      include_paths: vec![".deno-deploy".into()],
      build_command: Some(deno_task_build()),
    });
  }
  if dir.join(".output/server/index.ts").exists()
    || dir.join(".output/server/index.mjs").exists()
  {
    let ext = if dir.join(".output/server/index.ts").exists() {
      "ts"
    } else {
      "mjs"
    };
    return Some(FrameworkDetection {
      name: "SvelteKit",
      entrypoint_code: format!(
        "// @ts-nocheck\nimport \"./.output/server/index.{ext}\";\n"
      ),
      include_paths: vec![".output".into()],
      build_command: Some(deno_task_build()),
    });
  }
  // `svelte-adapter-deno` emits a `build/` directory whose `index.js`
  // boots the server and whose `handler.js` serves the static assets from
  // sibling `client`/`static`/`prerendered` directories. Those asset
  // directories aren't part of the module graph, so they must be included
  // explicitly or every request 404s in the compiled binary.
  if dir.join("build/index.js").exists()
    && dir.join("build/handler.js").exists()
  {
    return Some(FrameworkDetection {
      name: "SvelteKit",
      entrypoint_code: "// @ts-nocheck\nimport \"./build/index.js\";\n".into(),
      include_paths: sveltekit_build_includes(dir),
      build_command: Some(deno_task_build()),
    });
  }
  // No build artifacts yet — fall back to config inspection. We only
  // claim SvelteKit if the config references a supported adapter.
  let config_text =
    ["svelte.config.js", "svelte.config.ts", "svelte.config.mjs"]
      .iter()
      .find_map(|f| std::fs::read_to_string(dir.join(f)).ok())?;
  if config_text.contains("@deno/svelte-adapter") {
    return Some(FrameworkDetection {
      name: "SvelteKit",
      entrypoint_code: "// @ts-nocheck\nimport \"./.deno-deploy/server.ts\";\n"
        .into(),
      include_paths: vec![".deno-deploy".into()],
      build_command: Some(deno_task_build()),
    });
  }
  if config_text.contains("svelte-adapter-deno") {
    // Default `out` is `build`. Only `build/client` is guaranteed to exist
    // after the build, so include just that here; the post-build branch
    // above picks up `static`/`prerendered` when they're present.
    return Some(FrameworkDetection {
      name: "SvelteKit",
      entrypoint_code: "// @ts-nocheck\nimport \"./build/index.js\";\n".into(),
      include_paths: vec!["build/client".into()],
      build_command: Some(deno_task_build()),
    });
  }
  if config_text.contains("nitro") {
    return Some(FrameworkDetection {
      name: "SvelteKit",
      entrypoint_code:
        "// @ts-nocheck\nimport \"./.output/server/index.mjs\";\n".into(),
      include_paths: vec![".output".into()],
      build_command: Some(deno_task_build()),
    });
  }
  None
}

/// Existing asset directories under a `svelte-adapter-deno` `build/` output
/// that need to be embedded so the adapter's static file server can find them.
fn sveltekit_build_includes(dir: &Path) -> Vec<String> {
  ["client", "static", "prerendered"]
    .iter()
    .map(|sub| format!("build/{sub}"))
    .filter(|rel| dir.join(rel).is_dir())
    .collect()
}

/// Nuxt, SolidStart, TanStack Start all use Nitro with the `deno_server`
/// preset, outputting to `.output/server/index.{ts,mjs}`.
fn detect_nitro_framework(
  dir: &Path,
  name: &'static str,
) -> FrameworkDetection {
  let ext = if dir.join(".output/server/index.ts").exists() {
    "ts"
  } else {
    "mjs"
  };
  FrameworkDetection {
    name,
    entrypoint_code: format!(
      "// @ts-nocheck\nimport \"./.output/server/index.{ext}\";\n"
    ),
    include_paths: vec![".output".into()],
    build_command: Some(deno_task_build()),
  }
}

fn detect_vite(dir: &Path) -> FrameworkDetection {
  // SSR: a hand-written server entrypoint (`server.{js,ts,mjs}`) that boots the
  // Vite-built server bundle. Prefer it when present.
  if let Some(server_file) = ["server.js", "server.ts", "server.mjs"]
    .iter()
    .find(|f| dir.join(f).exists())
  {
    return FrameworkDetection {
      name: "Vite",
      entrypoint_code: format!("// @ts-nocheck\nimport \"./{server_file}\";\n"),
      include_paths: vec!["dist".into()],
      build_command: Some(deno_task_build()),
    };
  }

  // SPA / MPA: no server entrypoint. `vite build` emits a static site to
  // `dist/`; serve it over HTTP so the webview can load it, falling back to
  // `index.html` for client-side routes so a hard refresh on a history-API
  // route still resolves.
  FrameworkDetection {
    name: "Vite",
    entrypoint_code: r#"// @ts-nocheck
import { serveDir } from "jsr:@std/http/file-server";
// `vite build` emits a static site into `dist/`. Resolve it against the VFS in
// the compiled binary via import.meta.dirname rather than the runtime CWD.
const fsRoot = import.meta.dirname + "/dist";
Deno.serve(async (req) => {
  const res = await serveDir(req, { fsRoot, quiet: true });
  // SPA fallback: route unmatched HTML navigations back to index.html so
  // client-side routers keep working after a hard refresh.
  if (
    res.status === 404 &&
    req.method === "GET" &&
    (req.headers.get("accept") ?? "").includes("text/html")
  ) {
    const index = new Request(new URL("/index.html", req.url), {
      headers: req.headers,
    });
    return await serveDir(index, { fsRoot, quiet: true });
  }
  return res;
});
"#
    .into(),
    include_paths: vec!["dist".into()],
    build_command: Some(deno_task_build()),
  }
}

// --- Helpers ---

/// Check if a config file exists with any common extension.
fn has_config_file(dir: &Path, base_name: &str) -> bool {
  ["js", "mjs", "ts", "mts", "cjs"]
    .iter()
    .any(|ext| dir.join(format!("{base_name}.{ext}")).exists())
}

/// Read package.json dependencies.
fn read_package_deps(dir: &Path) -> Option<PackageDeps> {
  let content = std::fs::read_to_string(dir.join("package.json")).ok()?;
  let pkg: serde_json::Value = serde_json::from_str(&content).ok()?;
  Some(PackageDeps {
    deps: pkg
      .get("dependencies")
      .cloned()
      .unwrap_or(serde_json::Value::Object(Default::default())),
    dev_deps: pkg
      .get("devDependencies")
      .cloned()
      .unwrap_or(serde_json::Value::Object(Default::default())),
  })
}

struct PackageDeps {
  deps: serde_json::Value,
  dev_deps: serde_json::Value,
}

impl PackageDeps {
  fn has(&self, name: &str) -> bool {
    self.deps.get(name).is_some()
  }

  fn has_dev(&self, name: &str) -> bool {
    self.dev_deps.get(name).is_some()
  }
}

/// Extract the major version number from a package.json dependency.
fn detect_package_version(dir: &Path, package: &str) -> Option<u32> {
  let content = std::fs::read_to_string(dir.join("package.json")).ok()?;
  let pkg: serde_json::Value = serde_json::from_str(&content).ok()?;
  let ver_str = pkg
    .get("dependencies")
    .and_then(|d| d.get(package))
    .or_else(|| pkg.get("devDependencies").and_then(|d| d.get(package)))?
    .as_str()?;
  // Extract major version from "^16.1.6", "~15.0.0", "14.2.3", etc.
  ver_str
    .chars()
    .skip_while(|c: &char| !c.is_ascii_digit())
    .take_while(|c: &char| c.is_ascii_digit())
    .collect::<String>()
    .parse()
    .ok()
}

/// Read the `imports` keys from deno.json / deno.jsonc.
///
/// Uses JSONC-aware parsing so that commented deno.jsonc files are
/// handled correctly instead of silently failing detection.
fn read_deno_json_imports(dir: &Path) -> Option<Vec<String>> {
  let content = std::fs::read_to_string(dir.join("deno.json"))
    .or_else(|_| std::fs::read_to_string(dir.join("deno.jsonc")))
    .ok()?;
  let config: serde_json::Value =
    jsonc_parser::parse_to_serde_value(&content, &Default::default())
      .ok()
      .flatten()?;
  let imports = config.get("imports")?.as_object()?;
  Some(imports.keys().cloned().collect())
}

#[cfg(test)]
mod tests {
  use std::fs;

  use super::*;

  fn setup_dir() -> tempfile::TempDir {
    tempfile::tempdir().unwrap()
  }

  #[test]
  fn no_framework_empty_dir() {
    let dir = setup_dir();
    let result = detect_framework(dir.path()).unwrap();
    assert!(result.is_none());
  }

  // --- Config-file based detection ---

  #[test]
  fn detects_nextjs_with_config_js() {
    let dir = setup_dir();
    fs::write(dir.path().join("next.config.js"), "").unwrap();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"next":"^15.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Next.js");
    assert_eq!(det.include_paths, vec![".next"]);
    assert!(det.entrypoint_code.contains("next@^15"));
    // No .next dir => build_command is set
    assert!(det.build_command.is_some());
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn nextjs_always_builds() {
    let dir = setup_dir();
    fs::write(dir.path().join("next.config.js"), "").unwrap();
    fs::create_dir(dir.path().join(".next")).unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Next.js");
    assert!(det.build_command.is_some());
  }

  #[test]
  fn detects_nextjs_with_config_mjs() {
    let dir = setup_dir();
    fs::write(dir.path().join("next.config.mjs"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Next.js");
  }

  #[test]
  fn detects_nextjs_with_config_ts() {
    let dir = setup_dir();
    fs::write(dir.path().join("next.config.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Next.js");
  }

  #[test]
  fn nextjs_version_from_package_json() {
    let dir = setup_dir();
    fs::write(dir.path().join("next.config.js"), "").unwrap();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"next":"^14.2.3"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert!(det.entrypoint_code.contains("next@^14"));
  }

  #[test]
  fn nextjs_defaults_to_v15() {
    let dir = setup_dir();
    fs::write(dir.path().join("next.config.js"), "").unwrap();
    // no package.json
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert!(det.entrypoint_code.contains("next@^15"));
  }

  #[test]
  fn detects_fresh_gen_ts() {
    let dir = setup_dir();
    fs::write(dir.path().join("fresh.gen.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
    assert!(det.include_paths.is_empty());
    // no _fresh/server.js => Fresh 1.x
    assert!(det.entrypoint_code.contains("main.ts"));
  }

  #[test]
  fn detects_fresh2_with_server_js() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("_fresh")).unwrap();
    fs::write(dir.path().join("_fresh/server.js"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
    assert!(det.entrypoint_code.contains("_fresh/server.js"));
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn fresh1_has_no_build_command() {
    let dir = setup_dir();
    fs::write(dir.path().join("fresh.gen.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
    assert!(det.build_command.is_none());
  }

  #[test]
  fn fresh1_with_deno_json_stays_fresh1() {
    // Regression: fresh.gen.ts + deno.json (without @fresh/core import)
    // should be treated as Fresh 1, not Fresh 2.
    let dir = setup_dir();
    fs::write(dir.path().join("fresh.gen.ts"), "").unwrap();
    fs::write(
      dir.path().join("deno.json"),
      r#"{"tasks":{"start":"deno run -A main.ts"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
    // Fresh 1.x uses main.ts, not _fresh/server.js
    assert!(det.entrypoint_code.contains("main.ts"));
    assert!(det.build_command.is_none());
  }

  #[test]
  fn fresh2_detected_via_fresh_core_import() {
    let dir = setup_dir();
    fs::write(dir.path().join("fresh.gen.ts"), "").unwrap();
    fs::write(
      dir.path().join("deno.json"),
      r#"{"imports":{"@fresh/core":"jsr:@fresh/core@^2"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
    // Should be Fresh 2 because @fresh/core is in imports
    assert!(det.entrypoint_code.contains("_fresh/server.js"));
    assert!(det.build_command.is_some());
  }

  #[test]
  fn detects_astro() {
    let dir = setup_dir();
    fs::write(dir.path().join("astro.config.mjs"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Astro");
    assert!(det.entrypoint_code.contains("dist/server/entry.mjs"));
    assert_eq!(det.include_paths, vec!["dist"]);
    assert!(det.build_command.is_some());
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_nuxt() {
    let dir = setup_dir();
    fs::write(dir.path().join("nuxt.config.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Nuxt");
    assert_eq!(det.include_paths, vec![".output"]);
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_nuxt_with_ts_output() {
    let dir = setup_dir();
    fs::write(dir.path().join("nuxt.config.ts"), "").unwrap();
    fs::create_dir_all(dir.path().join(".output/server")).unwrap();
    fs::write(dir.path().join(".output/server/index.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert!(det.entrypoint_code.contains("index.ts"));
  }

  #[test]
  fn detects_sveltekit_deno_deploy() {
    let dir = setup_dir();
    fs::write(dir.path().join("svelte.config.js"), "").unwrap();
    fs::create_dir_all(dir.path().join(".deno-deploy")).unwrap();
    fs::write(dir.path().join(".deno-deploy/server.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SvelteKit");
    assert!(det.entrypoint_code.contains(".deno-deploy/server.ts"));
    assert_eq!(det.include_paths, vec![".deno-deploy"]);
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_sveltekit_nitro_from_built_output() {
    let dir = setup_dir();
    fs::write(dir.path().join("svelte.config.ts"), "").unwrap();
    fs::create_dir_all(dir.path().join(".output/server")).unwrap();
    fs::write(dir.path().join(".output/server/index.mjs"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SvelteKit");
    assert_eq!(det.include_paths, vec![".output"]);
    assert!(det.build_command.is_some());
  }

  #[test]
  fn detects_sveltekit_adapter_deno_from_config() {
    // `svelte-adapter-deno` emits a `build/` directory (not `.output`).
    let dir = setup_dir();
    fs::write(
      dir.path().join("svelte.config.js"),
      "import adapter from 'svelte-adapter-deno';\nexport default { kit: { adapter: adapter() } };\n",
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SvelteKit");
    assert!(det.entrypoint_code.contains("build/index.js"));
    assert_eq!(det.include_paths, vec!["build/client"]);
  }

  #[test]
  fn detects_sveltekit_adapter_deno_from_built_output() {
    // Post-build evidence: `build/index.js` + `build/handler.js` is the
    // `svelte-adapter-deno` output shape. Existing asset dirs are included.
    let dir = setup_dir();
    fs::write(dir.path().join("svelte.config.js"), "").unwrap();
    fs::create_dir_all(dir.path().join("build/client")).unwrap();
    fs::create_dir_all(dir.path().join("build/prerendered")).unwrap();
    fs::write(dir.path().join("build/index.js"), "").unwrap();
    fs::write(dir.path().join("build/handler.js"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SvelteKit");
    assert!(det.entrypoint_code.contains("build/index.js"));
    assert_eq!(det.include_paths, vec!["build/client", "build/prerendered"]);
    assert!(det.build_command.is_some());
  }

  #[test]
  fn detects_sveltekit_nitro_from_config() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("svelte.config.js"),
      "// uses a nitro-based adapter\nexport default { kit: {} };\n",
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SvelteKit");
    assert_eq!(det.include_paths, vec![".output"]);
  }

  #[test]
  fn does_not_detect_sveltekit_with_unknown_adapter() {
    // Regression: a SvelteKit project using e.g. adapter-vercel should
    // NOT be claimed by our detector — it would generate a wrong
    // entrypoint that imports a path that doesn't exist.
    let dir = setup_dir();
    fs::write(
      dir.path().join("svelte.config.js"),
      "import adapter from '@sveltejs/adapter-vercel';\nexport default { kit: { adapter: adapter() } };\n",
    )
    .unwrap();
    assert!(detect_framework(dir.path()).unwrap().is_none());
  }

  // --- Package.json dependency-based detection ---

  #[test]
  fn detects_remix_from_deps() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"@remix-run/react":"^2.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Remix");
    assert_eq!(det.include_paths, vec!["build"]);
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_remix_from_dev_deps() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"devDependencies":{"@remix-run/dev":"^2.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Remix");
  }

  #[test]
  fn detects_solidstart() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"@solidjs/start":"^1.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SolidStart");
    assert_eq!(det.include_paths, vec![".output"]);
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_tanstack_start_react() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"@tanstack/react-start":"^1.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "TanStack Start");
  }

  #[test]
  fn detects_tanstack_start_solid() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"@tanstack/solid-start":"^1.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "TanStack Start");
  }

  // --- Vite ---

  #[test]
  fn detects_vite_ssr_with_server_js() {
    let dir = setup_dir();
    fs::write(dir.path().join("vite.config.js"), "").unwrap();
    fs::write(dir.path().join("server.js"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Vite");
    assert!(det.entrypoint_code.contains("server.js"));
    assert_eq!(det.include_paths, vec!["dist"]);
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_vite_ssr_with_server_ts() {
    let dir = setup_dir();
    fs::write(dir.path().join("vite.config.ts"), "").unwrap();
    fs::write(dir.path().join("server.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Vite");
    assert!(det.entrypoint_code.contains("server.ts"));
  }

  #[test]
  fn detects_vite_spa_from_config_without_server_file() {
    // A plain Vite SPA (config file, no server.{js,ts,mjs}) serves the static
    // `dist/` build over HTTP rather than importing a server entrypoint.
    let dir = setup_dir();
    fs::write(dir.path().join("vite.config.js"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Vite");
    assert!(det.entrypoint_code.contains("serveDir"));
    assert!(det.entrypoint_code.contains("/dist"));
    assert!(det.entrypoint_code.contains("Deno.serve"));
    assert_eq!(det.include_paths, vec!["dist"]);
    let cmd = det.build_command.unwrap();
    assert_eq!(cmd[1..], vec!["task", "build"]);
  }

  #[test]
  fn detects_vite_from_package_dep() {
    // No config file, but `vite` in devDependencies — still a Vite project.
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"devDependencies":{"vite":"^5.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Vite");
    assert!(det.entrypoint_code.contains("serveDir"));
    assert_eq!(det.include_paths, vec!["dist"]);
  }

  #[test]
  fn detects_vite_ssr_when_dep_and_server_present() {
    // `vite` dependency plus a server entrypoint resolves to the SSR variant.
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"vite":"^5.0.0"}}"#,
    )
    .unwrap();
    fs::write(dir.path().join("server.ts"), "").unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Vite");
    assert!(det.entrypoint_code.contains("server.ts"));
  }

  // --- deno.json import-based detection ---

  #[test]
  fn detects_fresh_from_deno_json_imports() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("deno.json"),
      r#"{"imports":{"fresh":"jsr:@fresh/core"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
  }

  #[test]
  fn detects_fresh_from_deno_json_fresh_core() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("deno.json"),
      r#"{"imports":{"@fresh/core":"jsr:@fresh/core@^2"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
  }

  #[test]
  fn detects_fresh_from_deno_jsonc() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("deno.jsonc"),
      r#"{"imports":{"fresh":"jsr:@fresh/core"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
  }

  #[test]
  fn detects_fresh_from_commented_deno_jsonc() {
    // Regression: deno.jsonc with comments should still be parsed correctly.
    let dir = setup_dir();
    fs::write(
      dir.path().join("deno.jsonc"),
      "{\n  // This is a comment\n  \"imports\": {\n    \"fresh\": \"jsr:@fresh/core\"\n  }\n}\n",
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Fresh");
  }

  // --- Priority ---

  #[test]
  fn config_file_takes_priority_over_package_json() {
    let dir = setup_dir();
    // Has both next.config.js and remix in package.json
    fs::write(dir.path().join("next.config.js"), "").unwrap();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"@remix-run/react":"^2.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Next.js");
  }

  #[test]
  fn package_json_takes_priority_over_vite_ssr() {
    let dir = setup_dir();
    fs::write(dir.path().join("vite.config.js"), "").unwrap();
    fs::write(dir.path().join("server.js"), "").unwrap();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"@solidjs/start":"^1.0.0"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "SolidStart");
  }

  #[test]
  fn config_file_takes_priority_over_deno_json() {
    let dir = setup_dir();
    fs::write(dir.path().join("astro.config.mjs"), "").unwrap();
    fs::write(
      dir.path().join("deno.json"),
      r#"{"imports":{"fresh":"jsr:@fresh/core"}}"#,
    )
    .unwrap();
    let det = detect_framework(dir.path()).unwrap().unwrap();
    assert_eq!(det.name, "Astro");
  }

  // --- Helper unit tests ---

  #[test]
  fn has_config_file_various_extensions() {
    let dir = setup_dir();
    assert!(!has_config_file(dir.path(), "next.config"));
    fs::write(dir.path().join("next.config.mts"), "").unwrap();
    assert!(has_config_file(dir.path(), "next.config"));
  }

  #[test]
  fn detect_package_version_parses_caret() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"next":"^16.1.6"}}"#,
    )
    .unwrap();
    assert_eq!(detect_package_version(dir.path(), "next"), Some(16));
  }

  #[test]
  fn detect_package_version_parses_tilde() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"next":"~15.0.0"}}"#,
    )
    .unwrap();
    assert_eq!(detect_package_version(dir.path(), "next"), Some(15));
  }

  #[test]
  fn detect_package_version_parses_exact() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"next":"14.2.3"}}"#,
    )
    .unwrap();
    assert_eq!(detect_package_version(dir.path(), "next"), Some(14));
  }

  #[test]
  fn detect_package_version_from_dev_deps() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"devDependencies":{"next":"^13.0.0"}}"#,
    )
    .unwrap();
    assert_eq!(detect_package_version(dir.path(), "next"), Some(13));
  }

  #[test]
  fn detect_package_version_missing_package() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("package.json"),
      r#"{"dependencies":{"react":"^18.0.0"}}"#,
    )
    .unwrap();
    assert_eq!(detect_package_version(dir.path(), "next"), None);
  }

  #[test]
  fn detect_package_version_no_package_json() {
    let dir = setup_dir();
    assert_eq!(detect_package_version(dir.path(), "next"), None);
  }

  #[test]
  fn read_deno_json_imports_returns_keys() {
    let dir = setup_dir();
    fs::write(
      dir.path().join("deno.json"),
      r#"{"imports":{"foo":"jsr:@foo/bar","baz":"npm:baz"}}"#,
    )
    .unwrap();
    let mut imports = read_deno_json_imports(dir.path()).unwrap();
    imports.sort();
    assert_eq!(imports, vec!["baz", "foo"]);
  }

  #[test]
  fn read_deno_json_imports_no_file() {
    let dir = setup_dir();
    assert!(read_deno_json_imports(dir.path()).is_none());
  }

  #[test]
  fn read_deno_json_imports_no_imports_key() {
    let dir = setup_dir();
    fs::write(dir.path().join("deno.json"), r#"{"tasks":{}}"#).unwrap();
    assert!(read_deno_json_imports(dir.path()).is_none());
  }

  // --- Favicon discovery ---

  fn nextjs_detection() -> FrameworkDetection {
    FrameworkDetection {
      name: "Next.js",
      entrypoint_code: String::new(),
      include_paths: vec![],
      build_command: None,
    }
  }

  fn fresh_detection() -> FrameworkDetection {
    FrameworkDetection {
      name: "Fresh",
      entrypoint_code: String::new(),
      include_paths: vec![],
      build_command: None,
    }
  }

  #[test]
  fn finds_favicon_in_public_for_linux() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("public")).unwrap();
    fs::write(dir.path().join("public/favicon.png"), "").unwrap();
    let det = nextjs_detection();
    let p = find_framework_favicon(dir.path(), &det, "linux").unwrap();
    assert_eq!(p, dir.path().join("public/favicon.png"));
  }

  #[test]
  fn finds_ico_for_windows_but_not_png() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("public")).unwrap();
    fs::write(dir.path().join("public/favicon.png"), "").unwrap();
    let det = nextjs_detection();
    assert!(find_framework_favicon(dir.path(), &det, "windows").is_none());
    fs::write(dir.path().join("public/favicon.ico"), "").unwrap();
    let p = find_framework_favicon(dir.path(), &det, "windows").unwrap();
    assert_eq!(p, dir.path().join("public/favicon.ico"));
  }

  #[test]
  fn macos_prefers_icns_over_png() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("public")).unwrap();
    fs::write(dir.path().join("public/icon.png"), "").unwrap();
    fs::write(dir.path().join("public/icon.icns"), "").unwrap();
    let det = nextjs_detection();
    let p = find_framework_favicon(dir.path(), &det, "macos").unwrap();
    assert_eq!(p, dir.path().join("public/icon.icns"));
  }

  #[test]
  fn icon_name_preferred_over_favicon() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("public")).unwrap();
    fs::write(dir.path().join("public/favicon.png"), "").unwrap();
    fs::write(dir.path().join("public/icon.png"), "").unwrap();
    let det = nextjs_detection();
    let p = find_framework_favicon(dir.path(), &det, "linux").unwrap();
    assert_eq!(p, dir.path().join("public/icon.png"));
  }

  #[test]
  fn nextjs_app_router_favicon_in_app_dir() {
    let dir = setup_dir();
    // No public/, but app/favicon.ico — Next 13+ App Router layout.
    fs::create_dir_all(dir.path().join("app")).unwrap();
    fs::write(dir.path().join("app/favicon.ico"), "").unwrap();
    let det = nextjs_detection();
    let p = find_framework_favicon(dir.path(), &det, "windows").unwrap();
    assert_eq!(p, dir.path().join("app/favicon.ico"));
  }

  #[test]
  fn fresh_uses_static_dir() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("static")).unwrap();
    fs::write(dir.path().join("static/favicon.png"), "").unwrap();
    let det = fresh_detection();
    let p = find_framework_favicon(dir.path(), &det, "linux").unwrap();
    assert_eq!(p, dir.path().join("static/favicon.png"));
  }

  #[test]
  fn no_favicon_returns_none() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("public")).unwrap();
    let det = nextjs_detection();
    assert!(find_framework_favicon(dir.path(), &det, "linux").is_none());
  }

  #[test]
  fn public_takes_priority_over_app_for_nextjs() {
    let dir = setup_dir();
    fs::create_dir_all(dir.path().join("public")).unwrap();
    fs::create_dir_all(dir.path().join("app")).unwrap();
    fs::write(dir.path().join("public/favicon.png"), "").unwrap();
    fs::write(dir.path().join("app/icon.png"), "").unwrap();
    let det = nextjs_detection();
    let p = find_framework_favicon(dir.path(), &det, "linux").unwrap();
    // public/ is checked before app/.
    assert_eq!(p, dir.path().join("public/favicon.png"));
  }
}