|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN — `os create` refuses a project name npm refuses, BEFORE it writes. |
| 5 | + * |
| 6 | + * ## The defect |
| 7 | + * |
| 8 | + * Measured on `origin/main` e75a9040b02, driving the published entry: |
| 9 | + * |
| 10 | + * ``` |
| 11 | + * $ os create plugin "My App" |
| 12 | + * exit 0 — wrote ./plugin-My App/, manifest name "@objectstack/plugin-My App" |
| 13 | + * $ os init "My App" |
| 14 | + * exit 2 — "Project name must be lowercase", wrote NOTHING |
| 15 | + * ``` |
| 16 | + * |
| 17 | + * Both spellings are npm-invalid. One scaffolder refused before touching the |
| 18 | + * disk; the other emitted a directory and an unpublishable manifest with every |
| 19 | + * gate green, so the failure was deferred to `npm publish` in the terminal of |
| 20 | + * whoever ran it next. |
| 21 | + * |
| 22 | + * ## Why the refusal, and not just the message, is what is asserted |
| 23 | + * |
| 24 | + * `os init` refuses BEFORE the first write. A repair that refuses AFTER |
| 25 | + * `mkdirSync` has fixed the message and not the defect — the invalid directory |
| 26 | + * still lands. So every refusal case here asserts the directory is ABSENT, and |
| 27 | + * `accepts a valid name` is the positive control for that predicate: it runs |
| 28 | + * the same `scaffoldDir()` check against the same shape of temp directory and |
| 29 | + * finds the directory PRESENT. An absence assertion whose predicate cannot |
| 30 | + * fail is not evidence. |
| 31 | + * |
| 32 | + * ## Why the two commands do NOT refuse identically |
| 33 | + * |
| 34 | + * `os init`'s argument IS the package name. `os create`'s argument is composed |
| 35 | + * into a SCOPED one (`@objectstack/plugin-<name>`), and npm's 214-character |
| 36 | + * ceiling counts the scope — so a name that is legal for `init` can compose to |
| 37 | + * one npm refuses. `refuses a name only the composed length catches` pins that |
| 38 | + * asymmetry from both ends: the shared validator passes the name (asserted |
| 39 | + * directly), `create` refuses it, and `init` still accepts it. ⛔ Moving that |
| 40 | + * length rule into the shared validator would break `init` for a name npm |
| 41 | + * accepts; this test is what says so. |
| 42 | + * |
| 43 | + * Spawned through `bin/run-dev.js` + tsx, so the suite does not depend on |
| 44 | + * `packages/cli/dist` having been built — `@objectstack/cli#test` depends on |
| 45 | + * `^build` only (the reason `invocation-loudness.e2e.test.ts` spawns that way). |
| 46 | + */ |
| 47 | + |
| 48 | +import { describe, it, expect } from 'vitest'; |
| 49 | +import { execFile } from 'node:child_process'; |
| 50 | +import { existsSync, mkdtempSync, rmSync } from 'node:fs'; |
| 51 | +import { tmpdir } from 'node:os'; |
| 52 | +import { join, resolve } from 'node:path'; |
| 53 | +import { fileURLToPath } from 'node:url'; |
| 54 | +import { childEnv } from './helpers/serve-process.js'; |
| 55 | +import { |
| 56 | + emittedPackageName, |
| 57 | + templates, |
| 58 | + validateEmittedPackageName, |
| 59 | + DEFAULT_PLACEMENT, |
| 60 | + type ScaffoldPlacement, |
| 61 | +} from '../src/commands/create.js'; |
| 62 | +import { NPM_PACKAGE_NAME_MAX_LENGTH, validateProjectName } from '../src/commands/init.js'; |
| 63 | + |
| 64 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 65 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 66 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 67 | + |
| 68 | +/** oclif + tsx cold start with every command module loaded; ~2-10 s when healthy. */ |
| 69 | +const RUN_TIMEOUT_MS = 180_000; |
| 70 | + |
| 71 | +/** The card's input: a capital and a space, both npm-invalid. */ |
| 72 | +const INVALID_NAME = 'My App'; |
| 73 | +const VALID_NAME = 'my-app'; |
| 74 | + |
| 75 | +/** |
| 76 | + * A name the SHARED validator accepts and the composed one cannot: exactly at |
| 77 | + * `init`'s ceiling, so `@objectstack/plugin-` pushes it past the same ceiling. |
| 78 | + * Derived from the constant rather than written as a number, so a change to the |
| 79 | + * limit moves this case with it. |
| 80 | + */ |
| 81 | +const COMPOSED_TOO_LONG = 'a'.repeat(NPM_PACKAGE_NAME_MAX_LENGTH); |
| 82 | + |
| 83 | +interface Run { |
| 84 | + code: number; |
| 85 | + stdout: string; |
| 86 | + stderr: string; |
| 87 | +} |
| 88 | + |
| 89 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 90 | + return new Promise((resolvePromise) => { |
| 91 | + execFile( |
| 92 | + TSX, |
| 93 | + [CLI, ...args], |
| 94 | + { cwd, maxBuffer: 8 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 95 | + (err, stdout, stderr) => { |
| 96 | + resolvePromise({ |
| 97 | + // `err.code` is the real exit status; null/undefined means the child |
| 98 | + // was signalled — a different failure, never reported as 0. |
| 99 | + code: err |
| 100 | + ? typeof (err as { code?: unknown }).code === 'number' |
| 101 | + ? (err as unknown as { code: number }).code |
| 102 | + : 1 |
| 103 | + : 0, |
| 104 | + stdout: String(stdout), |
| 105 | + stderr: String(stderr), |
| 106 | + }); |
| 107 | + }, |
| 108 | + ); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +/** A fresh empty directory to scaffold into, removed by the caller. */ |
| 113 | +function workspace(): string { |
| 114 | + return mkdtempSync(join(tmpdir(), 'os-create-name-')); |
| 115 | +} |
| 116 | + |
| 117 | +/** The path `os create plugin <name>` writes, present or not. */ |
| 118 | +function scaffoldDir(cwd: string, name: string): string { |
| 119 | + return join(cwd, templates.plugin.dirName(name)); |
| 120 | +} |
| 121 | + |
| 122 | +describe('os create: a name npm refuses is refused before anything is written', () => { |
| 123 | + it( |
| 124 | + 'refuses the card\'s input and writes NOTHING', |
| 125 | + async () => { |
| 126 | + const cwd = workspace(); |
| 127 | + try { |
| 128 | + const run = await runCli(['create', 'plugin', INVALID_NAME], cwd); |
| 129 | + |
| 130 | + expect(run.code).not.toBe(0); |
| 131 | + // The message is the SHARED validator's own return value, not a second |
| 132 | + // copy of it written here — that identity is what stops the two |
| 133 | + // scaffolders drifting apart again. |
| 134 | + expect(validateProjectName(INVALID_NAME)).not.toBeNull(); |
| 135 | + expect(run.stderr).toContain(validateProjectName(INVALID_NAME)!); |
| 136 | + |
| 137 | + // The load-bearing half: refused BEFORE the first write. |
| 138 | + expect(existsSync(scaffoldDir(cwd, INVALID_NAME))).toBe(false); |
| 139 | + } finally { |
| 140 | + rmSync(cwd, { recursive: true, force: true }); |
| 141 | + } |
| 142 | + }, |
| 143 | + RUN_TIMEOUT_MS, |
| 144 | + ); |
| 145 | + |
| 146 | + it( |
| 147 | + 'accepts a valid name — the positive control for the absence check above', |
| 148 | + async () => { |
| 149 | + const cwd = workspace(); |
| 150 | + try { |
| 151 | + const run = await runCli(['create', 'plugin', VALID_NAME], cwd); |
| 152 | + |
| 153 | + expect(run.code).toBe(0); |
| 154 | + // Same predicate, same shape of directory, opposite verdict: the |
| 155 | + // absence assertion above is capable of failing. |
| 156 | + expect(existsSync(scaffoldDir(cwd, VALID_NAME))).toBe(true); |
| 157 | + } finally { |
| 158 | + rmSync(cwd, { recursive: true, force: true }); |
| 159 | + } |
| 160 | + }, |
| 161 | + RUN_TIMEOUT_MS, |
| 162 | + ); |
| 163 | + |
| 164 | + it( |
| 165 | + 'refuses a name only the COMPOSED length catches, which `os init` must keep accepting', |
| 166 | + async () => { |
| 167 | + const createCwd = workspace(); |
| 168 | + const initCwd = workspace(); |
| 169 | + try { |
| 170 | + // The shared validator passes it — so whatever refuses it below is the |
| 171 | + // composed-name rule and nothing else. |
| 172 | + expect(validateProjectName(COMPOSED_TOO_LONG)).toBeNull(); |
| 173 | + |
| 174 | + const created = await runCli(['create', 'plugin', COMPOSED_TOO_LONG], createCwd); |
| 175 | + expect(created.code).not.toBe(0); |
| 176 | + expect(created.stderr).toContain(String(NPM_PACKAGE_NAME_MAX_LENGTH)); |
| 177 | + expect(existsSync(scaffoldDir(createCwd, COMPOSED_TOO_LONG))).toBe(false); |
| 178 | + |
| 179 | + // ⛔ The asymmetry is correct, not a second defect: `init`'s argument is |
| 180 | + // the package name, so npm's ceiling is already measured against it. |
| 181 | + const inited = await runCli(['init', COMPOSED_TOO_LONG], initCwd); |
| 182 | + expect(inited.code).toBe(0); |
| 183 | + } finally { |
| 184 | + rmSync(createCwd, { recursive: true, force: true }); |
| 185 | + rmSync(initCwd, { recursive: true, force: true }); |
| 186 | + } |
| 187 | + }, |
| 188 | + RUN_TIMEOUT_MS, |
| 189 | + ); |
| 190 | +}); |
| 191 | + |
| 192 | +describe('os create: the composed package name is judged for every template', () => { |
| 193 | + const PLACEMENTS: ScaffoldPlacement[] = ['standalone', 'in-repo']; |
| 194 | + |
| 195 | + // Derived from the template map, never a list of `plugin` and `example`: a |
| 196 | + // third template must arrive already covered. |
| 197 | + for (const key of Object.keys(templates)) { |
| 198 | + for (const placement of PLACEMENTS) { |
| 199 | + it(`${key} / ${placement}: the emitted name is readable and judged`, () => { |
| 200 | + const emitted = emittedPackageName(templates[key], placement, VALID_NAME); |
| 201 | + expect(typeof emitted).toBe('string'); |
| 202 | + expect(emitted).toContain(VALID_NAME); |
| 203 | + expect(validateEmittedPackageName(emitted!)).toBeNull(); |
| 204 | + |
| 205 | + const overlong = emittedPackageName(templates[key], placement, COMPOSED_TOO_LONG); |
| 206 | + expect(overlong!.length).toBeGreaterThan(NPM_PACKAGE_NAME_MAX_LENGTH); |
| 207 | + expect(validateEmittedPackageName(overlong!)).toContain( |
| 208 | + String(NPM_PACKAGE_NAME_MAX_LENGTH), |
| 209 | + ); |
| 210 | + }); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + it('reads the name off the DEFAULT placement the same way the command does', () => { |
| 215 | + const emitted = emittedPackageName(templates.plugin, DEFAULT_PLACEMENT, VALID_NAME); |
| 216 | + expect(emitted).toBe(`@objectstack/plugin-${VALID_NAME}`); |
| 217 | + }); |
| 218 | +}); |
0 commit comments