1// @effect-diagnostics nodeBuiltinImport:off - entrypoint detection is a Node filesystem boundary.
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6
7import { describe, expect, it } from "vite-plus/test";
8
9import { isEntrypoint } from "./entrypoint.ts";
10import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
11
12const makeTempDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-entrypoint-test-"));
13
14describe("isEntrypoint", () => {
15 it("matches the entrypoint path when the runtime has no import.meta.main", () => {
16 // Node 22.16, 22.17 and 23.11 are inside `engines.node` but leave it undefined.
17 const dir = makeTempDir();
18 const entry = NodePath.join(dir, "bin.mjs");
19 NodeFS.writeFileSync(entry, "");
20
21 expect(
22 isEntrypoint({
23 moduleUrl: NodeURL.pathToFileURL(entry).href,
24 entryPath: entry,
25 runtimeMain: undefined,
26 }),
27 ).toBe(true);
28 });
29
30 it.skipIf(!symlinksSupported)(
31 "matches through a symlinked entrypoint, as npm and npx install it",
32 () => {
33 const dir = makeTempDir();
34 const real = NodePath.join(dir, "bin.mjs");
35 const link = NodePath.join(dir, "t3");
36 NodeFS.writeFileSync(real, "");
37 NodeFS.symlinkSync(real, link);
38
39 // macOS temp directories can be symlinks; Node resolves module URLs to real paths.
40 expect(
41 isEntrypoint({
42 moduleUrl: NodeURL.pathToFileURL(NodeFS.realpathSync(real)).href,
43 entryPath: link,
44 runtimeMain: undefined,
45 }),
46 ).toBe(true);
47 },
48 );
49
50 it("stays false for an imported module that is not the entrypoint", () => {
51 const dir = makeTempDir();
52 const entry = NodePath.join(dir, "bin.mjs");
53 const imported = NodePath.join(dir, "cli.mjs");
54 NodeFS.writeFileSync(entry, "");
55 NodeFS.writeFileSync(imported, "");
56
57 expect(
58 isEntrypoint({
59 moduleUrl: NodeURL.pathToFileURL(imported).href,
60 entryPath: entry,
61 runtimeMain: undefined,
62 }),
63 ).toBe(false);
64 });
65});