|
| 1 | +/** |
| 2 | + * Dual ESM/CJS build compatibility tests |
| 3 | + * |
| 4 | + * Verifies that both the ESM and CJS builds exist and work correctly, |
| 5 | + * so consumers using either module system get a working package. |
| 6 | + * |
| 7 | + * See: https://github.com/github/copilot-sdk/issues/528 |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, expect, it } from "vitest"; |
| 11 | +import { existsSync } from "node:fs"; |
| 12 | +import { execFileSync } from "node:child_process"; |
| 13 | +import { join } from "node:path"; |
| 14 | + |
| 15 | +const distDir = join(import.meta.dirname, "../dist"); |
| 16 | + |
| 17 | +describe("Dual ESM/CJS build (#528)", () => { |
| 18 | + it("ESM dist file should exist", () => { |
| 19 | + expect(existsSync(join(distDir, "index.js"))).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it("CJS dist file should exist", () => { |
| 23 | + expect(existsSync(join(distDir, "cjs/index.js"))).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it("CJS build is requireable and exports CopilotClient", () => { |
| 27 | + const script = ` |
| 28 | + const sdk = require(${JSON.stringify(join(distDir, "cjs/index.js"))}); |
| 29 | + if (typeof sdk.CopilotClient !== 'function') { |
| 30 | + console.error('CopilotClient is not a function'); |
| 31 | + process.exit(1); |
| 32 | + } |
| 33 | + console.log('CJS require: OK'); |
| 34 | + `; |
| 35 | + const output = execFileSync(process.execPath, ["--eval", script], { |
| 36 | + encoding: "utf-8", |
| 37 | + timeout: 10000, |
| 38 | + cwd: join(import.meta.dirname, ".."), |
| 39 | + }); |
| 40 | + expect(output).toContain("CJS require: OK"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("CJS build resolves bundled CLI path", () => { |
| 44 | + const script = ` |
| 45 | + const sdk = require(${JSON.stringify(join(distDir, "cjs/index.js"))}); |
| 46 | + const client = new sdk.CopilotClient({ autoStart: false }); |
| 47 | + console.log('CJS CLI resolved: OK'); |
| 48 | + `; |
| 49 | + const output = execFileSync(process.execPath, ["--eval", script], { |
| 50 | + encoding: "utf-8", |
| 51 | + timeout: 10000, |
| 52 | + cwd: join(import.meta.dirname, ".."), |
| 53 | + }); |
| 54 | + expect(output).toContain("CJS CLI resolved: OK"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("ESM build resolves bundled CLI path", () => { |
| 58 | + const esmPath = join(distDir, "index.js"); |
| 59 | + const script = ` |
| 60 | + import { pathToFileURL } from 'node:url'; |
| 61 | + const sdk = await import(pathToFileURL(${JSON.stringify(esmPath)}).href); |
| 62 | + const client = new sdk.CopilotClient({ autoStart: false }); |
| 63 | + console.log('ESM CLI resolved: OK'); |
| 64 | + `; |
| 65 | + const output = execFileSync(process.execPath, ["--input-type=module", "--eval", script], { |
| 66 | + encoding: "utf-8", |
| 67 | + timeout: 10000, |
| 68 | + cwd: join(import.meta.dirname, ".."), |
| 69 | + }); |
| 70 | + expect(output).toContain("ESM CLI resolved: OK"); |
| 71 | + }); |
| 72 | +}); |
0 commit comments