Open History to see the last commit
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 // Node resolves the module URL to the real path, and on macOS the temp
40 // directory itself sits behind a symlink (`/var` -> `/private/var`).
41 expect(
42 isEntrypoint({
43 moduleUrl: NodeURL.pathToFileURL(NodeFS.realpathSync(real)).href,
44 entryPath: link,
45 runtimeMain: undefined,
46 }),
47 ).toBe(true);
48 },
49 );
50
51 it("stays false for an imported module that is not the entrypoint", () => {
52 const dir = makeTempDir();
53 const entry = NodePath.join(dir, "bin.mjs");
54 const imported = NodePath.join(dir, "cli.mjs");
55 NodeFS.writeFileSync(entry, "");
56 NodeFS.writeFileSync(imported, "");
57
58 expect(
59 isEntrypoint({
60 moduleUrl: NodeURL.pathToFileURL(imported).href,
61 entryPath: entry,
62 runtimeMain: undefined,
63 }),
64 ).toBe(false);
65 });
66});