|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A BUILT consumer of `@objectstack/scenarios/score` (#30915): a throwaway |
| 5 | + * package that depends on this one, bundles its own entry with tsup (which |
| 6 | + * leaves a `dependencies` entry external), and is then loaded by plain Node, |
| 7 | + * the way a linked framework tree's consumer loads it at boot. Before the |
| 8 | + * package had a build step its exports named `.ts` files whose `.js` |
| 9 | + * specifiers only a TypeScript resolver follows, so this load failed with |
| 10 | + * ERR_MODULE_NOT_FOUND under both module kinds. |
| 11 | + * |
| 12 | + * The child process records every module it resolves (`module.registerHooks`), |
| 13 | + * so the narrow entry's runtime graph is read, not inferred: no |
| 14 | + * `@objectstack/objectql` or `@objectstack/plugin-sharing` file loads. |
| 15 | + * |
| 16 | + * Reads `dist/`: turbo runs this package's own `build` before its `test`. |
| 17 | + */ |
| 18 | + |
| 19 | +import { execFileSync } from 'node:child_process'; |
| 20 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; |
| 21 | +import { createRequire } from 'node:module'; |
| 22 | +import { tmpdir } from 'node:os'; |
| 23 | +import { dirname, join, resolve } from 'node:path'; |
| 24 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 25 | + |
| 26 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 27 | + |
| 28 | +const PKG = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); |
| 29 | +const REPO = resolve(PKG, '../../..'); |
| 30 | +const BUILT = ['score.js', 'score.cjs', 'score.d.ts', 'score.d.cts'].map((f) => join(PKG, 'dist', f)); |
| 31 | + |
| 32 | +const TSUP_PKG = createRequire(join(REPO, 'package.json')).resolve('tsup/package.json'); |
| 33 | +const TSUP_BIN = join(dirname(TSUP_PKG), (JSON.parse(readFileSync(TSUP_PKG, 'utf8')) as { bin: { tsup: string } }).bin.tsup); |
| 34 | + |
| 35 | +const ENTRY = ` |
| 36 | +import { scoreGraphShape } from '@objectstack/scenarios/score'; |
| 37 | +
|
| 38 | +export const shape = () => |
| 39 | + scoreGraphShape({ objects: [{ name: 'loan', fields: [{ name: 'title', type: 'text' }] }] }, { minObjects: 1 }); |
| 40 | +
|
| 41 | +export const loadScore = () => import('@objectstack/scenarios/score'); |
| 42 | +`; |
| 43 | + |
| 44 | +// Preloaded into the child: every URL Node resolves, printed as the last line. |
| 45 | +const RECORDER = ` |
| 46 | +const { registerHooks } = require('node:module'); |
| 47 | +const seen = []; |
| 48 | +registerHooks({ resolve(specifier, context, next) { const r = next(specifier, context); seen.push(r.url); return r; } }); |
| 49 | +process.on('exit', () => process.stdout.write('\\n' + JSON.stringify(seen) + '\\n')); |
| 50 | +`; |
| 51 | + |
| 52 | +let fixture = ''; |
| 53 | + |
| 54 | +function runNode(code: string, kind: 'module' | 'commonjs'): { out: Record<string, unknown>; resolved: string[] } { |
| 55 | + const stdout = execFileSync(process.execPath, ['--require', join(fixture, 'recorder.cjs'), `--input-type=${kind}`, '-e', code], { |
| 56 | + cwd: fixture, |
| 57 | + encoding: 'utf8', |
| 58 | + }); |
| 59 | + const lines = stdout.trim().split('\n'); |
| 60 | + return { out: JSON.parse(lines[0]!) as Record<string, unknown>, resolved: JSON.parse(lines.at(-1)!) as string[] }; |
| 61 | +} |
| 62 | + |
| 63 | +const probe = (mod: string) => ` |
| 64 | + const m = ${mod}; |
| 65 | + const s = await m.loadScore(); |
| 66 | + const r = m.shape(); |
| 67 | + console.log(JSON.stringify({ shape: typeof s.scoreGraphShape, run: typeof s.gradePersonaRun, tags: typeof s.scoreScenarioArtifact, score: r.score, total: r.total })); |
| 68 | +`; |
| 69 | + |
| 70 | +describe('a tsup-built consumer of @objectstack/scenarios/score', () => { |
| 71 | + beforeAll(() => { |
| 72 | + const missing = BUILT.filter((f) => !existsSync(f)); |
| 73 | + if (missing.length > 0) throw new Error(`build @objectstack/scenarios first (pnpm --filter @objectstack/scenarios build): missing ${missing.join(', ')}`); |
| 74 | + |
| 75 | + fixture = mkdtempSync(join(tmpdir(), 'scenarios-built-consumer-')); |
| 76 | + writeFileSync( |
| 77 | + join(fixture, 'package.json'), |
| 78 | + JSON.stringify({ name: 'scenarios-built-consumer', private: true, type: 'module', dependencies: { '@objectstack/scenarios': 'workspace:*' } }), |
| 79 | + ); |
| 80 | + mkdirSync(join(fixture, 'src')); |
| 81 | + writeFileSync(join(fixture, 'src/index.ts'), ENTRY); |
| 82 | + writeFileSync(join(fixture, 'recorder.cjs'), RECORDER); |
| 83 | + mkdirSync(join(fixture, 'node_modules/@objectstack'), { recursive: true }); |
| 84 | + symlinkSync(PKG, join(fixture, 'node_modules/@objectstack/scenarios'), 'dir'); |
| 85 | + |
| 86 | + execFileSync(process.execPath, [TSUP_BIN, 'src/index.ts', '--format', 'esm,cjs', '--target', 'es2020', '--silent'], { |
| 87 | + cwd: fixture, |
| 88 | + stdio: 'pipe', |
| 89 | + }); |
| 90 | + }, 120_000); |
| 91 | + |
| 92 | + afterAll(() => { |
| 93 | + if (fixture) rmSync(fixture, { recursive: true, force: true }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('leaves the scorer external: the bundle imports it, it does not carry it', () => { |
| 97 | + for (const out of ['dist/index.js', 'dist/index.cjs']) { |
| 98 | + const text = readFileSync(join(fixture, out), 'utf8'); |
| 99 | + expect(text, out).toContain('@objectstack/scenarios/score'); |
| 100 | + expect(text, out).not.toContain('PLATFORM_OBJECT_PREFIX'); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it.each([ |
| 105 | + ['ESM', 'module', `await import(${JSON.stringify('PLACEHOLDER')})`, 'dist/index.js'], |
| 106 | + ['CJS', 'commonjs', `require(${JSON.stringify('PLACEHOLDER')})`, 'dist/index.cjs'], |
| 107 | + ] as const)('loads under plain Node (%s) and reaches neither objectql nor plugin-sharing', (_kind, inputType, load, file) => { |
| 108 | + const target = inputType === 'module' ? pathToFileURL(join(fixture, file)).href : join(fixture, file); |
| 109 | + const body = probe(load.replace('PLACEHOLDER', target)); |
| 110 | + const { out, resolved } = runNode(inputType === 'module' ? body : `(async () => { ${body} })()`, inputType); |
| 111 | + |
| 112 | + expect(out).toEqual({ shape: 'function', run: 'function', tags: 'undefined', score: 1, total: 1 }); |
| 113 | + const scorer = resolved.filter((u) => u.includes('/qa/scenarios/dist/')); |
| 114 | + expect(scorer.length, 'the scorer is loaded from its dist').toBeGreaterThan(0); |
| 115 | + expect(resolved.filter((u) => u.includes('/qa/scenarios/src/'))).toEqual([]); |
| 116 | + expect(resolved.filter((u) => /objectql|plugin-sharing/.test(u))).toEqual([]); |
| 117 | + // Control: the recorder sees the scorer's own runtime imports. |
| 118 | + expect(resolved.some((u) => u.includes('/packages/core/'))).toBe(true); |
| 119 | + }); |
| 120 | +}); |
0 commit comments