An open source design system that's fully customizable and agent ready
TypeScript
test(cli): build's tree and the no-npm checks load their code natively (#10337)
Christopher Chedeauaaa33dfff2d5
diff --git a/packages/cli/api/build/tree.test.mjs b/packages/cli/api/build/tree.test.mjsindex c1be73fe5a..c536beb652 100644--- a/packages/cli/api/build/tree.test.mjs+++ b/packages/cli/api/build/tree.test.mjs@@ -7,42 +7,73 @@* block count is build's own, and the blocks it names are pinned per phrase.*/+import * as fs from 'node:fs';+import * as os from 'node:os';import * as path from 'node:path';import {fileURLToPath} from 'node:url';-import {afterAll, beforeAll, describe, expect, it, vi} from 'vitest';+import {afterAll, beforeAll, describe, expect, it} from 'vitest';+import {+ requireNatively,+ stubBuiltin,+} from '../../test-utils/native-require.mjs';-// vi.mock calls are hoisted above the imports; the stub maker is hoisted with them.-const {blocked} = vi.hoisted(() => ({- /** @param {string} name */- blocked: name => async (/** @type {() => Promise<any>} */ load) => {- const real = await load();- /** @type {Record<string, unknown>} */- const stub = {};- for (const [key, value] of Object.entries(real)) {- stub[key] =- typeof value === 'function'- ? () => {- throw new Error(`build reached ${name}.${key}`);- }- : value;- }- return {...stub, default: stub};- },-}));-vi.mock('node:http', blocked('http'));-vi.mock('node:https', blocked('https'));-vi.mock('node:net', blocked('net'));-vi.mock('node:dns', blocked('dns'));-vi.mock('node:child_process', blocked('child_process'));-vi.mock('http', blocked('http'));-vi.mock('https', blocked('https'));-vi.mock('net', blocked('net'));-vi.mock('dns', blocked('dns'));-vi.mock('child_process', blocked('child_process'));+/** The built-ins build must not reach: the network and subprocesses. */+const OFFLINE = ['http', 'https', 'net', 'dns', 'child_process'];++/** @param {string} what */+const reached = what => () => {+ throw new Error(`build reached ${what}`);+};++// From before build loads, every function they export throws, so a function+// build keeps from its first load throws too. Their classes stay, because+// Node builds on them, but each class's connect, listen, spawn and query+// throws.+const restoreOffline = OFFLINE.map(name =>+ stubBuiltin(`node:${name}`, real =>+ Object.fromEntries(+ Object.entries(real)+ .filter(+ ([key, value]) => typeof value === 'function' && !/^[A-Z]/u.test(key),+ )+ .map(([key]) => [key, reached(`${name}.${key}`)]),+ ),+ ),+);++const require = requireNatively(import.meta.url);+const {Socket, Server} = require('node:net');+const {ChildProcess} = require('node:child_process');+const {Resolver} = require('node:dns');+/** @type {[string, any, string][]} */+const STARTS = [+ ['net.Socket.prototype.connect', Socket.prototype, 'connect'],+ ['net.Server.prototype.listen', Server.prototype, 'listen'],+ [+ 'child_process.ChildProcess.prototype.spawn',+ ChildProcess.prototype,+ 'spawn',+ ],+ ...Object.getOwnPropertyNames(Resolver.prototype)+ .filter(key => /^(?:resolve|reverse)/u.test(key))+ .map(+ key =>+ /** @type {[string, any, string]} */ ([+ `dns.Resolver.prototype.${key}`,+ Resolver.prototype,+ key,+ ]),+ ),+];+const ownStarts = STARTS.map(([, target, key]) => target[key]);+for (const [what, target, key] of STARTS) target[key] = reached(what);-const {build} = await import('./build.mjs');-const {buildKit} = await import('./kit/kit.mjs');-const {loadBlockPicker, withListingCache} = await import('./_adapter.mjs');+/** @type {typeof import('./build.mjs')} */+const {build} = require('./build.mjs');+/** @type {typeof import('./kit/kit.mjs')} */+const {buildKit} = require('./kit/kit.mjs');+/** @type {typeof import('./_adapter.mjs')} */+const {loadBlockPicker, withListingCache} = require('./_adapter.mjs');const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)),@@ -122,6 +153,49 @@ beforeAll(() => {});afterAll(() => {globalThis.fetch = realFetch;+ for (const restore of restoreOffline) restore();+ STARTS.forEach(([, target, key], index) => {+ target[key] = ownStarts[index];+ });+});++describe('the offline guard', () => {+ it('throws on each network and subprocess call that natively loaded code makes', () => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-offline-'));+ try {+ const file = path.join(dir, 'calls.mjs');+ fs.writeFileSync(+ file,+ [+ "import {request} from 'node:http';",+ "import {get} from 'node:https';",+ "import {connect} from 'node:net';",+ "import {lookup} from 'node:dns';",+ "import {ChildProcess, spawnSync} from 'node:child_process';",+ "import {Socket} from 'node:net';",+ 'export const calls = {',+ " 'http.request': () => request('http://127.0.0.1:9'),",+ " 'https.get': () => get('https://127.0.0.1:9'),",+ " 'net.connect': () => connect(9, '127.0.0.1'),",+ " 'dns.lookup': () => lookup('localhost', () => {}),",+ " 'child_process.spawnSync': () => spawnSync('true'),",+ " 'net.Socket.prototype.connect': () =>",+ " new Socket().connect(9, '127.0.0.1'),",+ " 'child_process.ChildProcess.prototype.spawn': () =>",+ " new ChildProcess().spawn({file: 'true', args: ['true']}),",+ '};',+ '',+ ].join('\n'),+ );+ /** @type {{calls: Record<string, () => unknown>}} */+ const {calls} = require(file);+ for (const [name, call] of Object.entries(calls)) {+ expect(call, name).toThrow(`build reached ${name}`);+ }+ } finally {+ fs.rmSync(dir, {recursive: true, force: true});+ }+ });});describe('build tree', () => {diff --git a/packages/cli/api/integration/example-checks.no-npm.test.mjs b/packages/cli/api/integration/example-checks.no-npm.test.mjsindex b037cdbef6..b702e0eac7 100644--- a/packages/cli/api/integration/example-checks.no-npm.test.mjs+++ b/packages/cli/api/integration/example-checks.no-npm.test.mjs@@ -10,17 +10,41 @@ import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';import {Command} from 'commander';-import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';+import {+ afterAll,+ afterEach,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from 'vitest';+import {+ requireNatively,+ stubBuiltin,+} from '../../test-utils/native-require.mjs';-vi.mock('../../foundation/release/cache.mjs', () => ({- npmCommand: () => {- throw new Error('npm is not installed');+/** An npm entry file `npmCommand` looks for: the `npm` link or npm's CLI. */+const NPM_ENTRY = /[\\/](?:npm|npm\.cmd|npm-cli\.js)$/u;++// npm is not installed: each check for an npm entry file throws, so+// `npmCommand` throws before it names one. The stub is in place before the+// code under test loads.+const restoreFs = stubBuiltin('node:fs', real => ({+ existsSync: (/** @type {import('node:fs').PathLike} */ file) => {+ if (NPM_ENTRY.test(String(file))) throw new Error('npm is not installed');+ return real.existsSync(file);},}));+afterAll(restoreFs);-const {validateIntegration} = await import('./validate-integration.mjs');-const {integrationCheck} = await import('./check/check.mjs');-const {registerDoctor} = await import('../../clients/cli/commands/doctor.mjs');+const require = requireNatively(import.meta.url);+/** @type {typeof import('./validate-integration.mjs')} */+const {validateIntegration} = require('./validate-integration.mjs');+/** @type {typeof import('./check/check.mjs')} */+const {integrationCheck} = require('./check/check.mjs');+/** @type {typeof import('../../clients/cli/commands/doctor.mjs')} */+const {registerDoctor} = require('../../clients/cli/commands/doctor.mjs');/** @type {string} */let dir;@@ -65,6 +89,12 @@ afterEach(() => {});describe('template assets when npm gives no answer', () => {+ it('finds no npm from natively loaded code', () => {+ /** @type {typeof import('../../foundation/release/cache.mjs')} */+ const {npmCommand} = require('../../foundation/release/cache.mjs');+ expect(() => npmCommand()).toThrow('npm is not installed');+ });+it('reports an asset no entry can reach and lists the others as not checked', async () => {packageWith(['templates', '!templates/img']);const {data} = await validateIntegration(undefined, {cwd: dir});diff --git a/packages/cli/test-utils/native-require.mjs b/packages/cli/test-utils/native-require.mjsindex 100ee1b955..19760e3e49 100644--- a/packages/cli/test-utils/native-require.mjs+++ b/packages/cli/test-utils/native-require.mjs@@ -1,16 +1,18 @@// Copyright (c) Meta Platforms, Inc. and affiliates./**- * @file Loads a test's code under test natively, as the CLI itself loads.+ * @file Loads a test's code under test natively, as the CLI itself loads, and+ * stubs a Node built-in for that code.*- * @input The test file's `import.meta.url`.+ * @input The test file's `import.meta.url`; a built-in's name and stubs.* @output A `require` that resolves from the test file and loads ES modules- * with Node's own loader.- * @position Test helper for a test whose code under test reads the docs- * catalog. `native-require.test.mjs` checks every test that uses it.+ * with Node's own loader; a function that puts a stubbed built-in back.+ * @position Test helper for a test whose code under test loads many modules:+ * the docs catalog, or fresh fixture modules. `native-require.test.mjs`+ * checks every test that uses it.*/-import {createRequire} from 'node:module';+import {createRequire, syncBuiltinESMExports} from 'node:module';/*** A `require` for the test file at `fromUrl`. The modules it loads, and every@@ -21,12 +23,60 @@ import {createRequire} from 'node:module';* concurrent files of a full run wait there for seconds. Vitest's mocks and* module resets do not reach a module that loads natively. So a test that* uses this helper calls none of them, and `native-require.test.mjs` checks- * that. Each test file runs in a process of its own: the forks pool, with- * `isolate` on. So a native module never carries state from one file to the- * next. With `isolate` off, these files would share the CLI's module state.+ * that. It stubs a Node built-in with {@link stubBuiltin}. Each test file+ * runs in a process of its own: the forks pool, with `isolate` on. So a+ * native module never carries state from one file to the next. With+ * `isolate` off, these files would share the CLI's module state.* @param {string} fromUrl the test file's `import.meta.url`* @returns {NodeJS.Require}*/export function requireNatively(fromUrl) {return createRequire(fromUrl);}++/**+ * Replaces exports of the Node built-in `name`, for natively loaded code too,+ * until the returned function puts them back. A test that loads natively+ * stubs a built-in this way, not with `vi.mock`.+ *+ * The stubs go on the built-in's one module object, which every loader+ * shares. Node then copies them to the built-in's ES module exports, so an+ * `import {spawnSync} from 'node:child_process'` sees them too. They apply+ * to the whole test process, the runner's own code included, until restored.+ * So a stub passes through each call it does not target. It relies on each+ * test file running in a process of its own, as {@link requireNatively}+ * does.+ * @param {string} name a built-in, such as `node:child_process`+ * @param {(real: Record<string, any>) => Record<string, unknown>} stubs the+ * replacements, by export name, given the built-in's own exports+ * @returns {() => void} puts the built-in's own exports back+ */+export function stubBuiltin(name, stubs) {+ /** @type {Record<string, any>} */+ const builtin = createRequire(import.meta.url)(name);+ const replacements = stubs({...builtin});+ // Some exports are getters, so each is redefined and later put back whole.+ const own = Object.keys(replacements).map(+ key =>+ /** @type {const} */ ([+ key,+ Object.getOwnPropertyDescriptor(builtin, key),+ ]),+ );+ for (const [key, value] of Object.entries(replacements)) {+ Object.defineProperty(builtin, key, {+ value,+ writable: true,+ configurable: true,+ enumerable: true,+ });+ }+ syncBuiltinESMExports();+ return () => {+ for (const [key, descriptor] of own) {+ if (descriptor) Object.defineProperty(builtin, key, descriptor);+ else delete builtin[key];+ }+ syncBuiltinESMExports();+ };+}diff --git a/packages/cli/test-utils/native-require.test.mjs b/packages/cli/test-utils/native-require.test.mjsindex 206bb89d0d..fd76517aed 100644--- a/packages/cli/test-utils/native-require.test.mjs+++ b/packages/cli/test-utils/native-require.test.mjs@@ -11,6 +11,8 @@ import * as fs from 'node:fs';import * as path from 'node:path';import {fileURLToPath} from 'node:url';import {parse} from '@babel/parser';+import * as os from 'node:os';+import {requireNatively, stubBuiltin} from './native-require.mjs';const HERE = path.dirname(fileURLToPath(import.meta.url));const CLI = path.resolve(HERE, '..');@@ -124,3 +126,36 @@ describe('requireNatively', () => {});});});++describe('stubBuiltin', () => {+ it('reaches natively loaded code, by name and by namespace, until restored', () => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-stub-builtin-'));+ try {+ const file = path.join(dir, 'uses-os.mjs');+ fs.writeFileSync(+ file,+ [+ "import {hostname} from 'node:os';",+ "import * as osModule from 'node:os';",+ 'export const named = () => hostname();',+ 'export const viaNamespace = () => osModule.hostname();',+ '',+ ].join('\n'),+ );+ /** @type {{named: () => string, viaNamespace: () => string}} */+ const uses = requireNatively(import.meta.url)(file);+ const restore = stubBuiltin('node:os', () => ({hostname: () => 'stub'}));+ try {+ expect([uses.named(), uses.viaNamespace()]).toEqual(['stub', 'stub']);+ } finally {+ restore();+ }+ expect([uses.named(), uses.viaNamespace()]).toEqual([+ os.hostname(),+ os.hostname(),+ ]);+ } finally {+ fs.rmSync(dir, {recursive: true, force: true});+ }+ });+});