|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | +import { |
| 3 | + copyFileSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + writeFileSync, |
| 8 | +} from "node:fs"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import { dirname, join } from "node:path"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 13 | + |
| 14 | +const repoRoot = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 15 | +const cliPath = join(repoRoot, "dist/cli.js"); |
| 16 | + |
| 17 | +/** Node built-ins that may appear as bare imports in the bundled CLI. */ |
| 18 | +const NODE_BUILTINS = new Set([ |
| 19 | + "assert", |
| 20 | + "buffer", |
| 21 | + "child_process", |
| 22 | + "crypto", |
| 23 | + "events", |
| 24 | + "fs", |
| 25 | + "fs/promises", |
| 26 | + "module", |
| 27 | + "os", |
| 28 | + "path", |
| 29 | + "process", |
| 30 | + "readline", |
| 31 | + "readline/promises", |
| 32 | + "stream", |
| 33 | + "string_decoder", |
| 34 | + "tty", |
| 35 | + "url", |
| 36 | + "util", |
| 37 | +]); |
| 38 | + |
| 39 | +function writeMinimalScaffold(projectRoot: string, mexDir: string): void { |
| 40 | + const frontmatter = (name: string) => `--- |
| 41 | +name: ${name} |
| 42 | +description: test |
| 43 | +triggers: [] |
| 44 | +edges: [] |
| 45 | +last_updated: 2026-06-06 |
| 46 | +--- |
| 47 | +content |
| 48 | +`; |
| 49 | + |
| 50 | + writeFileSync(join(mexDir, "ROUTER.md"), frontmatter("router")); |
| 51 | + writeFileSync(join(mexDir, "AGENTS.md"), "# Agents\n[Project Name]\n"); |
| 52 | + for (const name of ["architecture", "stack", "conventions", "decisions", "setup"]) { |
| 53 | + writeFileSync(join(mexDir, "context", `${name}.md`), frontmatter(name)); |
| 54 | + } |
| 55 | + writeFileSync( |
| 56 | + join(mexDir, "patterns", "INDEX.md"), |
| 57 | + `${frontmatter("index")}\n| Pattern | Description |\n|---------|-------------|\n`, |
| 58 | + ); |
| 59 | + |
| 60 | + execSync("git init -q", { cwd: projectRoot }); |
| 61 | + execSync("git add -A", { cwd: projectRoot }); |
| 62 | + execSync('git -c user.email=test@test.com -c user.name=test commit -q -m init', { |
| 63 | + cwd: projectRoot, |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +describe("bundled CLI (Windows/WSL issue #10)", () => { |
| 68 | + beforeAll(() => { |
| 69 | + execSync("npm run build", { cwd: repoRoot, stdio: "pipe" }); |
| 70 | + }, 120_000); |
| 71 | + |
| 72 | + it("does not leave npm package imports in dist/cli.js", () => { |
| 73 | + const source = readFileSync(cliPath, "utf8"); |
| 74 | + const externalImports = [ |
| 75 | + ...source.matchAll(/^import\s+.+\s+from\s+["']([^./][^"']*)["']/gm), |
| 76 | + ] |
| 77 | + .map((match) => match[1]) |
| 78 | + .filter((name) => !name.startsWith("node:") && !NODE_BUILTINS.has(name)); |
| 79 | + |
| 80 | + expect(externalImports).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("runs --version without .mex/node_modules", () => { |
| 84 | + const tmp = mkdtempSync(join(tmpdir(), "mex-bundle-")); |
| 85 | + const mexDir = join(tmp, ".mex"); |
| 86 | + mkdirSync(join(mexDir, "dist"), { recursive: true }); |
| 87 | + copyFileSync(cliPath, join(mexDir, "dist/cli.js")); |
| 88 | + copyFileSync(join(repoRoot, "package.json"), join(mexDir, "package.json")); |
| 89 | + |
| 90 | + const out = execSync("node dist/cli.js --version", { |
| 91 | + cwd: mexDir, |
| 92 | + encoding: "utf8", |
| 93 | + }); |
| 94 | + |
| 95 | + const pkg = JSON.parse(readFileSync(join(repoRoot, "package.json"), "utf8")) as { |
| 96 | + version: string; |
| 97 | + }; |
| 98 | + expect(out.trim()).toBe(pkg.version); |
| 99 | + }); |
| 100 | + |
| 101 | + it("runs check --quiet on a minimal scaffold without node_modules", () => { |
| 102 | + const tmp = mkdtempSync(join(tmpdir(), "mex-bundle-check-")); |
| 103 | + const mexDir = join(tmp, ".mex"); |
| 104 | + mkdirSync(join(mexDir, "dist"), { recursive: true }); |
| 105 | + mkdirSync(join(mexDir, "context"), { recursive: true }); |
| 106 | + mkdirSync(join(mexDir, "patterns"), { recursive: true }); |
| 107 | + |
| 108 | + copyFileSync(cliPath, join(mexDir, "dist/cli.js")); |
| 109 | + copyFileSync(join(repoRoot, "package.json"), join(mexDir, "package.json")); |
| 110 | + writeMinimalScaffold(tmp, mexDir); |
| 111 | + |
| 112 | + const out = execSync("node .mex/dist/cli.js check --quiet", { |
| 113 | + cwd: tmp, |
| 114 | + encoding: "utf8", |
| 115 | + }); |
| 116 | + |
| 117 | + expect(out.trim()).toMatch(/^mex: drift score \d+\/100/); |
| 118 | + }); |
| 119 | +}); |
0 commit comments