|
| 1 | +/** |
| 2 | + * Programmatic/embedded SDK entry (`scripts/npm-sdk.js`) tests (issue #354). |
| 3 | + * |
| 4 | + * The published main package is a thin shim: the CLI `bin` (npm-shim.js) execs |
| 5 | + * the bundled Node, while `main` (npm-sdk.js) lets embedded consumers |
| 6 | + * `require("@colbymchenry/codegraph")` on their OWN Node by re-exporting the |
| 7 | + * compiled library that ships inside the per-platform optionalDependency |
| 8 | + * (@colbymchenry/codegraph-<target>/lib/dist/index.js). |
| 9 | + * |
| 10 | + * These tests stand up a temp main-package dir with a fake platform package as a |
| 11 | + * resolvable sibling, then require the SDK in a child process — so resolution, |
| 12 | + * the self-heal cache fallback, and the missing-bundle error are exercised |
| 13 | + * hermetically with no real bundle, network, or registry. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { spawnSync } from 'child_process'; |
| 18 | +import * as fs from 'fs'; |
| 19 | +import * as os from 'os'; |
| 20 | +import * as path from 'path'; |
| 21 | + |
| 22 | +const SDK_SRC = path.join(__dirname, '..', 'scripts', 'npm-sdk.js'); |
| 23 | +const target = `${process.platform}-${process.arch}`; |
| 24 | +const VERSION = '9.9.9-test'; |
| 25 | + |
| 26 | +function mkTmp(label: string): string { |
| 27 | + return fs.mkdtempSync(path.join(os.tmpdir(), `cg-sdk-${label}-`)); |
| 28 | +} |
| 29 | + |
| 30 | +// A temp node_modules with the main package (npm-sdk.js + package.json). The |
| 31 | +// fake platform package, when present, is written as a resolvable sibling so the |
| 32 | +// SDK's `require.resolve('@colbymchenry/codegraph-<target>/...')` walks to it. |
| 33 | +function makeConsumer(): { root: string; mainPkg: string } { |
| 34 | + const root = mkTmp('consumer'); |
| 35 | + const mainPkg = path.join(root, 'node_modules', '@colbymchenry', 'codegraph'); |
| 36 | + fs.mkdirSync(mainPkg, { recursive: true }); |
| 37 | + fs.copyFileSync(SDK_SRC, path.join(mainPkg, 'npm-sdk.js')); |
| 38 | + fs.writeFileSync( |
| 39 | + path.join(mainPkg, 'package.json'), |
| 40 | + JSON.stringify({ name: '@colbymchenry/codegraph', version: VERSION, main: 'npm-sdk.js' }) + '\n' |
| 41 | + ); |
| 42 | + return { root, mainPkg }; |
| 43 | +} |
| 44 | + |
| 45 | +// Write a fake compiled library that exports a sentinel, at the given lib/dist |
| 46 | +// root (used both for the platform package and the self-heal cache bundle). |
| 47 | +function writeFakeLib(libDistDir: string, sentinel: string): void { |
| 48 | + fs.mkdirSync(libDistDir, { recursive: true }); |
| 49 | + fs.writeFileSync( |
| 50 | + path.join(libDistDir, 'index.js'), |
| 51 | + `module.exports = { SENTINEL: ${JSON.stringify(sentinel)}, CodeGraph: function CodeGraph() {} };\n` |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function installPlatformPackage(root: string, sentinel: string): void { |
| 56 | + const pkgRoot = path.join(root, 'node_modules', '@colbymchenry', `codegraph-${target}`); |
| 57 | + writeFakeLib(path.join(pkgRoot, 'lib', 'dist'), sentinel); |
| 58 | + fs.writeFileSync( |
| 59 | + path.join(pkgRoot, 'package.json'), |
| 60 | + JSON.stringify({ name: `@colbymchenry/codegraph-${target}`, version: VERSION }) + '\n' |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +// require() the SDK in a child process so each case gets a fresh module cache. |
| 65 | +function requireSdk(mainPkg: string, env: Record<string, string> = {}) { |
| 66 | + const code = |
| 67 | + `try { const m = require(${JSON.stringify(path.join(mainPkg, 'npm-sdk.js'))});` + |
| 68 | + ` process.stdout.write(JSON.stringify({ sentinel: m.SENTINEL, cg: typeof m.CodeGraph })); }` + |
| 69 | + ` catch (e) { process.stderr.write(String(e && e.message || e)); process.exit(7); }`; |
| 70 | + const r = spawnSync(process.execPath, ['-e', code], { |
| 71 | + encoding: 'utf8', |
| 72 | + env: { ...process.env, ...env }, |
| 73 | + }); |
| 74 | + return { status: r.status, stdout: r.stdout, stderr: r.stderr }; |
| 75 | +} |
| 76 | + |
| 77 | +describe('npm-sdk programmatic entry', () => { |
| 78 | + it('re-exports the installed platform bundle library', () => { |
| 79 | + const { root, mainPkg } = makeConsumer(); |
| 80 | + installPlatformPackage(root, 'platform-lib'); |
| 81 | + // Isolate from any real self-healed cache on this machine. |
| 82 | + const r = requireSdk(mainPkg, { CODEGRAPH_INSTALL_DIR: path.join(root, '.empty-cache') }); |
| 83 | + expect(r.status).toBe(0); |
| 84 | + expect(JSON.parse(r.stdout)).toEqual({ sentinel: 'platform-lib', cg: 'function' }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('falls back to a self-healed cache bundle when the optional dep is absent', () => { |
| 88 | + const { root, mainPkg } = makeConsumer(); // no platform package installed |
| 89 | + const cacheDir = path.join(root, 'cache'); |
| 90 | + writeFakeLib( |
| 91 | + path.join(cacheDir, 'bundles', `${target}-${VERSION}`, 'lib', 'dist'), |
| 92 | + 'cache-lib' |
| 93 | + ); |
| 94 | + const r = requireSdk(mainPkg, { CODEGRAPH_INSTALL_DIR: cacheDir }); |
| 95 | + expect(r.status).toBe(0); |
| 96 | + expect(JSON.parse(r.stdout)).toEqual({ sentinel: 'cache-lib', cg: 'function' }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('throws an actionable error when no bundle is installed or cached', () => { |
| 100 | + const { root, mainPkg } = makeConsumer(); // no platform package, empty cache |
| 101 | + const r = requireSdk(mainPkg, { CODEGRAPH_INSTALL_DIR: path.join(root, '.empty-cache') }); |
| 102 | + expect(r.status).toBe(7); |
| 103 | + expect(r.stderr).toContain(`@colbymchenry/codegraph-${target}`); |
| 104 | + expect(r.stderr).toContain('not installed'); |
| 105 | + expect(r.stderr).toContain('registry.npmjs.org'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments