File size: 3,016 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";

// Regression guard for #3883: the @omniroute/opencode-plugin must ship an
// ESM-only bundle. OpenCode's Bun-based plugin loader resolves the package
// `main`/`exports` and applies CJS-to-ESM interop on a dual CJS bundle, which
// turns `mod.default` into the whole exports namespace, fails V1 plugin
// detection, and makes the loader throw "Plugin export is not a function".
// Shipping ESM-only (with a `./runtime` subpath) keeps `mod.default` the V1
// plugin object so the loader registers it. Do NOT re-introduce a CJS bundle.

const PKG_URL = new URL("../../../@omniroute/opencode-plugin/package.json", import.meta.url);
const TSUP_URL = new URL("../../../@omniroute/opencode-plugin/tsup.config.ts", import.meta.url);

function readJson(url: URL): Record<string, unknown> {
  return JSON.parse(readFileSync(fileURLToPath(url), "utf8"));
}

test("opencode-plugin package.json ships ESM-only (no CJS bundle)", () => {
  const pkg = readJson(PKG_URL);

  // main must point at the ESM build, never the .cjs bundle.
  assert.equal(pkg.main, "./dist/index.js", "main must be the ESM build");
  assert.equal(
    typeof pkg.main === "string" && pkg.main.endsWith(".cjs"),
    false,
    "main must not be a .cjs bundle"
  );
  // The legacy dual-bundle `module` field should be gone.
  assert.equal("module" in pkg, false, "the dual-bundle `module` field must be removed");

  const exports = pkg.exports as Record<string, unknown> | undefined;
  assert.ok(exports && typeof exports === "object", "exports map must exist");

  const root = exports["."] as Record<string, unknown>;
  assert.ok(root && typeof root === "object", 'exports["."] must exist');
  assert.equal(root.import, "./dist/index.js", 'exports["."].import must be the ESM build');
  // A `require` condition would re-introduce the CJS path the loader chokes on.
  assert.equal("require" in root, false, 'exports["."] must not declare a `require` condition');

  // The `./runtime` subpath used by the OpenCode loader must resolve to ESM.
  const runtime = exports["./runtime"] as Record<string, unknown>;
  assert.ok(runtime && typeof runtime === "object", 'exports["./runtime"] must exist');
  assert.equal(runtime.import, "./dist/index.js", 'exports["./runtime"].import must be ESM');
});

test("opencode-plugin tsup config builds ESM-only with cjsInterop disabled", () => {
  const tsup = readFileSync(fileURLToPath(TSUP_URL), "utf8");

  const formatMatch = tsup.match(/format:\s*\[([^\]]*)\]/);
  assert.ok(formatMatch, "tsup config must declare a format array");
  const formats = formatMatch[1]
    .split(",")
    .map((s) => s.trim().replace(/['"]/g, ""))
    .filter(Boolean);
  assert.deepEqual(formats, ["esm"], "tsup must build esm only (no cjs)");

  assert.equal(
    /cjsInterop:\s*true/.test(tsup),
    false,
    "cjsInterop must not be true for an ESM-only build"
  );
});