npm-sdk.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import { describe, it, expect } from 'vitest';
  16. import { spawnSync } from 'child_process';
  17. import * as fs from 'fs';
  18. import * as os from 'os';
  19. import * as path from 'path';
  20. const SDK_SRC = path.join(__dirname, '..', 'scripts', 'npm-sdk.js');
  21. const target = `${process.platform}-${process.arch}`;
  22. const VERSION = '9.9.9-test';
  23. function mkTmp(label: string): string {
  24. return fs.mkdtempSync(path.join(os.tmpdir(), `cg-sdk-${label}-`));
  25. }
  26. // A temp node_modules with the main package (npm-sdk.js + package.json). The
  27. // fake platform package, when present, is written as a resolvable sibling so the
  28. // SDK's `require.resolve('@colbymchenry/codegraph-<target>/...')` walks to it.
  29. function makeConsumer(): { root: string; mainPkg: string } {
  30. const root = mkTmp('consumer');
  31. const mainPkg = path.join(root, 'node_modules', '@colbymchenry', 'codegraph');
  32. fs.mkdirSync(mainPkg, { recursive: true });
  33. fs.copyFileSync(SDK_SRC, path.join(mainPkg, 'npm-sdk.js'));
  34. fs.writeFileSync(
  35. path.join(mainPkg, 'package.json'),
  36. JSON.stringify({ name: '@colbymchenry/codegraph', version: VERSION, main: 'npm-sdk.js' }) + '\n'
  37. );
  38. return { root, mainPkg };
  39. }
  40. // Write a fake compiled library that exports a sentinel, at the given lib/dist
  41. // root (used both for the platform package and the self-heal cache bundle).
  42. function writeFakeLib(libDistDir: string, sentinel: string): void {
  43. fs.mkdirSync(libDistDir, { recursive: true });
  44. fs.writeFileSync(
  45. path.join(libDistDir, 'index.js'),
  46. `module.exports = { SENTINEL: ${JSON.stringify(sentinel)}, CodeGraph: function CodeGraph() {} };\n`
  47. );
  48. }
  49. function installPlatformPackage(root: string, sentinel: string): void {
  50. const pkgRoot = path.join(root, 'node_modules', '@colbymchenry', `codegraph-${target}`);
  51. writeFakeLib(path.join(pkgRoot, 'lib', 'dist'), sentinel);
  52. fs.writeFileSync(
  53. path.join(pkgRoot, 'package.json'),
  54. JSON.stringify({ name: `@colbymchenry/codegraph-${target}`, version: VERSION }) + '\n'
  55. );
  56. }
  57. // require() the SDK in a child process so each case gets a fresh module cache.
  58. function requireSdk(mainPkg: string, env: Record<string, string> = {}) {
  59. const code =
  60. `try { const m = require(${JSON.stringify(path.join(mainPkg, 'npm-sdk.js'))});` +
  61. ` process.stdout.write(JSON.stringify({ sentinel: m.SENTINEL, cg: typeof m.CodeGraph })); }` +
  62. ` catch (e) { process.stderr.write(String(e && e.message || e)); process.exit(7); }`;
  63. const r = spawnSync(process.execPath, ['-e', code], {
  64. encoding: 'utf8',
  65. env: { ...process.env, ...env },
  66. });
  67. return { status: r.status, stdout: r.stdout, stderr: r.stderr };
  68. }
  69. describe('npm-sdk programmatic entry', () => {
  70. it('re-exports the installed platform bundle library', () => {
  71. const { root, mainPkg } = makeConsumer();
  72. installPlatformPackage(root, 'platform-lib');
  73. // Isolate from any real self-healed cache on this machine.
  74. const r = requireSdk(mainPkg, { CODEGRAPH_INSTALL_DIR: path.join(root, '.empty-cache') });
  75. expect(r.status).toBe(0);
  76. expect(JSON.parse(r.stdout)).toEqual({ sentinel: 'platform-lib', cg: 'function' });
  77. });
  78. it('falls back to a self-healed cache bundle when the optional dep is absent', () => {
  79. const { root, mainPkg } = makeConsumer(); // no platform package installed
  80. const cacheDir = path.join(root, 'cache');
  81. writeFakeLib(
  82. path.join(cacheDir, 'bundles', `${target}-${VERSION}`, 'lib', 'dist'),
  83. 'cache-lib'
  84. );
  85. const r = requireSdk(mainPkg, { CODEGRAPH_INSTALL_DIR: cacheDir });
  86. expect(r.status).toBe(0);
  87. expect(JSON.parse(r.stdout)).toEqual({ sentinel: 'cache-lib', cg: 'function' });
  88. });
  89. it('throws an actionable error when no bundle is installed or cached', () => {
  90. const { root, mainPkg } = makeConsumer(); // no platform package, empty cache
  91. const r = requireSdk(mainPkg, { CODEGRAPH_INSTALL_DIR: path.join(root, '.empty-cache') });
  92. expect(r.status).toBe(7);
  93. expect(r.stderr).toContain(`@colbymchenry/codegraph-${target}`);
  94. expect(r.stderr).toContain('not installed');
  95. expect(r.stderr).toContain('registry.npmjs.org');
  96. });
  97. });