sailfork / t3code-github / apps/ server/ src/ entrypoint.test.ts

// @effect-diagnostics nodeBuiltinImport:off - entrypoint detection is a Node filesystem boundary.
import * as NodeFS from "node:fs";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import * as NodeURL from "node:url";

import { describe, expect, it } from "vite-plus/test";

import { isEntrypoint } from "./entrypoint.ts";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";

const makeTempDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-entrypoint-test-"));

describe("isEntrypoint", () => {
  it("matches the entrypoint path when the runtime has no import.meta.main", () => {
    // Node 22.16, 22.17 and 23.11 are inside `engines.node` but leave it undefined.
    const dir = makeTempDir();
    const entry = NodePath.join(dir, "bin.mjs");
    NodeFS.writeFileSync(entry, "");

    expect(
      isEntrypoint({
        moduleUrl: NodeURL.pathToFileURL(entry).href,
        entryPath: entry,
        runtimeMain: undefined,
      }),
    ).toBe(true);
  });

  it.skipIf(!symlinksSupported)(
    "matches through a symlinked entrypoint, as npm and npx install it",
    () => {
      const dir = makeTempDir();
      const real = NodePath.join(dir, "bin.mjs");
      const link = NodePath.join(dir, "t3");
      NodeFS.writeFileSync(real, "");
      NodeFS.symlinkSync(real, link);

      // macOS temp directories can be symlinks; Node resolves module URLs to real paths.
      expect(
        isEntrypoint({
          moduleUrl: NodeURL.pathToFileURL(NodeFS.realpathSync(real)).href,
          entryPath: link,
          runtimeMain: undefined,
        }),
      ).toBe(true);
    },
  );

  it("stays false for an imported module that is not the entrypoint", () => {
    const dir = makeTempDir();
    const entry = NodePath.join(dir, "bin.mjs");
    const imported = NodePath.join(dir, "cli.mjs");
    NodeFS.writeFileSync(entry, "");
    NodeFS.writeFileSync(imported, "");

    expect(
      isEntrypoint({
        moduleUrl: NodeURL.pathToFileURL(imported).href,
        entryPath: entry,
        runtimeMain: undefined,
      }),
    ).toBe(false);
  });
});