|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN — every standalone manifest `os init` and `os create` emit carries the |
| 5 | + * npm `overrides` block, built from the one vitest constant. |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * vitest 4.1.12 shipped a self-contradicting peer set (it peers |
| 10 | + * `@vitest/browser-webdriverio ^4.1.11`, whose only release peers `vitest: |
| 11 | + * 4.1.11` exactly). npm 10 — the npm Node 22 bundles — reaches it through |
| 12 | + * OPTIONAL `vitest` peers in the `@objectstack/cli` closure, even with vitest |
| 13 | + * pinned to `~4.0.0` at the project root, and its arborist crashes with |
| 14 | + * `Cannot read properties of null (reading 'edgesOut')`. Measured 2026-10-16: |
| 15 | + * `os init -t app`, `-t plugin` and `-t empty` all failed `npm install` on npm |
| 16 | + * 10.9.9; with `"overrides": { "vitest": "~4.0.0" }` all of them, and `os |
| 17 | + * create plugin`, installed on npm 10.9.9 and npm 11.21.1. The full reading is |
| 18 | + * on `SCAFFOLD_NPM_OVERRIDES` in `src/commands/init.ts`. |
| 19 | + * |
| 20 | + * ## What is asserted, and why no range is written down here |
| 21 | + * |
| 22 | + * Every expectation is DERIVED from the exported constants, never a literal: |
| 23 | + * a pin comparing against `'~4.0.0'` would stay green on a tree where the |
| 24 | + * constant moved and one emission was left behind — the drift it exists to |
| 25 | + * catch. |
| 26 | + * |
| 27 | + * 1. Each emission's `overrides.vitest` IS `SCAFFOLD_VITEST_RANGE`. |
| 28 | + * 2. Where an emission also declares `vitest` directly, the two are equal — |
| 29 | + * npm refuses an override that differs from the direct dependency's own |
| 30 | + * spec (`EOVERRIDE`), so a mismatch is an install failure of its own. |
| 31 | + * 3. `os create plugin --in-repo` emits NO `overrides`: npm reads the field |
| 32 | + * only from the root manifest and pnpm never reads it, so inside the |
| 33 | + * workspace it would be a declaration nothing honours. |
| 34 | + * |
| 35 | + * That the emitted manifests really install is not decidable here; the npm |
| 36 | + * matrix above is the measurement, and `create-objectstack`'s bundled template |
| 37 | + * reaches the same constant through `scripts/sync-scaffold-emission-policy.mjs` |
| 38 | + * (`pnpm check:scaffold-emission-policy`). |
| 39 | + */ |
| 40 | + |
| 41 | +import { describe, it, expect } from 'vitest'; |
| 42 | +import { |
| 43 | + renderScaffoldPackageJson, |
| 44 | + SCAFFOLD_NPM_OVERRIDES, |
| 45 | + SCAFFOLD_VITEST_RANGE, |
| 46 | + TEMPLATES, |
| 47 | +} from '../src/commands/init.js'; |
| 48 | +import { DEFAULT_PLACEMENT, templates } from '../src/commands/create.js'; |
| 49 | + |
| 50 | +const PROBE_NAME = 'overrides-probe'; |
| 51 | + |
| 52 | +type Manifest = { |
| 53 | + dependencies?: Record<string, string>; |
| 54 | + devDependencies?: Record<string, string>; |
| 55 | + overrides?: Record<string, string>; |
| 56 | +}; |
| 57 | + |
| 58 | +/** Every standalone manifest the two commands emit, derived from the live maps. */ |
| 59 | +function standaloneManifests(): Array<{ id: string; manifest: Manifest }> { |
| 60 | + const out: Array<{ id: string; manifest: Manifest }> = []; |
| 61 | + for (const [key, template] of Object.entries(TEMPLATES)) { |
| 62 | + out.push({ id: `os init -t ${key}`, manifest: renderScaffoldPackageJson(PROBE_NAME, template) as Manifest }); |
| 63 | + } |
| 64 | + for (const [key, template] of Object.entries(templates)) { |
| 65 | + const render = template.filesFor(DEFAULT_PLACEMENT)['package.json']; |
| 66 | + if (!render) continue; |
| 67 | + out.push({ id: `os create ${key}`, manifest: render(PROBE_NAME) as Manifest }); |
| 68 | + } |
| 69 | + return out; |
| 70 | +} |
| 71 | + |
| 72 | +describe('npm overrides in every standalone scaffold manifest', () => { |
| 73 | + it('harvests every emission, and the default placement IS standalone (control)', () => { |
| 74 | + expect(DEFAULT_PLACEMENT).toBe('standalone'); |
| 75 | + const ids = standaloneManifests().map((m) => m.id); |
| 76 | + expect(ids).toEqual(expect.arrayContaining(['os init -t app', 'os init -t plugin', 'os create plugin'])); |
| 77 | + // Assertion 2 below must range over something, or it certifies nothing. |
| 78 | + const direct = standaloneManifests().filter((m) => m.manifest.devDependencies?.vitest !== undefined); |
| 79 | + expect(direct.map((m) => m.id)).toEqual(expect.arrayContaining(['os init -t plugin', 'os create plugin'])); |
| 80 | + }); |
| 81 | + |
| 82 | + it('the override map is built from the vitest constant', () => { |
| 83 | + expect(SCAFFOLD_NPM_OVERRIDES.vitest).toBe(SCAFFOLD_VITEST_RANGE); |
| 84 | + expect(Object.isFrozen(SCAFFOLD_NPM_OVERRIDES)).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it.each(standaloneManifests())('$id emits overrides.vitest = SCAFFOLD_VITEST_RANGE', ({ manifest }) => { |
| 88 | + expect(manifest.overrides).toEqual({ ...SCAFFOLD_NPM_OVERRIDES }); |
| 89 | + expect(manifest.overrides?.vitest).toBe(SCAFFOLD_VITEST_RANGE); |
| 90 | + }); |
| 91 | + |
| 92 | + it.each(standaloneManifests())( |
| 93 | + '$id: every override naming a direct dependency equals that dependency (npm EOVERRIDE)', |
| 94 | + ({ manifest }) => { |
| 95 | + const direct = { ...(manifest.dependencies ?? {}), ...(manifest.devDependencies ?? {}) }; |
| 96 | + for (const [name, range] of Object.entries(manifest.overrides ?? {})) { |
| 97 | + if (direct[name] === undefined) continue; |
| 98 | + expect(range, `overrides.${name} vs the direct ${name} dependency`).toBe(direct[name]); |
| 99 | + } |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + it('os create --in-repo emits no overrides block — npm and pnpm both ignore it in a workspace member', () => { |
| 104 | + const render = templates.plugin.filesFor('in-repo')['package.json']; |
| 105 | + const manifest = render(PROBE_NAME) as Manifest; |
| 106 | + // Control: the in-repo manifest is a real one, with vitest declared. |
| 107 | + expect(manifest.devDependencies?.vitest).toBe(SCAFFOLD_VITEST_RANGE); |
| 108 | + expect(manifest).not.toHaveProperty('overrides'); |
| 109 | + }); |
| 110 | +}); |
0 commit comments