|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// A scaffold's namespace has a paired customization namespace, so the first |
| 4 | +// `os validate` on a fresh project prints no `namespace-no-customization-pair` |
| 5 | +// warning about objects its author never wrote. |
| 6 | +// |
| 7 | +// The rule (ADR-0146 D3, `@objectstack/lint`'s `validateCustomizationNamespace`) |
| 8 | +// warns when `customizationNamespace(manifest.namespace)` answers |
| 9 | +// `base_namespace_too_long`. The verdict here is that same helper's, read from |
| 10 | +// the spec, so the scaffold is judged by the function the ensure door creates |
| 11 | +// the pair with, never by a number restated in this file. |
| 12 | +// |
| 13 | +// Drives the real CLI end to end via `tsx`: index.ts calls `program.parse()` on |
| 14 | +// import (the pattern of scaffold-namespace-extends-template.test.ts). |
| 15 | + |
| 16 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 17 | +import { spawnSync } from 'node:child_process'; |
| 18 | +import fs from 'node:fs'; |
| 19 | +import os from 'node:os'; |
| 20 | +import path from 'node:path'; |
| 21 | +import { fileURLToPath } from 'node:url'; |
| 22 | +import { customizationNamespace, validateObjectNamespacePrefix } from '@objectstack/spec/kernel'; |
| 23 | +import { readTemplateNamespace } from './rewrite-identity.js'; |
| 24 | + |
| 25 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 26 | +const PKG_ROOT = path.resolve(HERE, '..'); |
| 27 | +const REPO_ROOT = path.resolve(PKG_ROOT, '..', '..'); |
| 28 | +const TSX = path.join(REPO_ROOT, 'node_modules', '.bin', 'tsx'); |
| 29 | +const INDEX_TS = path.join(PKG_ROOT, 'src', 'index.ts'); |
| 30 | + |
| 31 | +/** Every `name: '…'` literal in the `*.ts` files under `dir`. */ |
| 32 | +function nameLiterals(dir: string): string[] { |
| 33 | + const names: string[] = []; |
| 34 | + for (const rel of fs.readdirSync(dir, { recursive: true, encoding: 'utf8' })) { |
| 35 | + if (!rel.endsWith('.ts')) continue; |
| 36 | + const text = fs.readFileSync(path.join(dir, rel), 'utf8'); |
| 37 | + for (const m of text.matchAll(/\bname:\s*(['"`])([a-z0-9_]+)\1/g)) names.push(m[2]); |
| 38 | + } |
| 39 | + return names.sort(); |
| 40 | +} |
| 41 | + |
| 42 | +let tmp: string | undefined; |
| 43 | +afterEach(() => { |
| 44 | + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); |
| 45 | + tmp = undefined; |
| 46 | +}); |
| 47 | + |
| 48 | +describe('a scaffold from a long project name', () => { |
| 49 | + it('`qa-console-first-paint` gets a namespace with a customization pair, and every object follows it', () => { |
| 50 | + const project = 'qa-console-first-paint'; |
| 51 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'create-objectstack-ns-pair-')); |
| 52 | + const run = spawnSync(TSX, [INDEX_TS, project, '--skip-install', '--skip-skills'], { |
| 53 | + cwd: tmp, |
| 54 | + encoding: 'utf8', |
| 55 | + }); |
| 56 | + expect(run.status, `${run.stdout}\n${run.stderr}`).toBe(0); |
| 57 | + |
| 58 | + const projectDir = path.join(tmp, project); |
| 59 | + const namespace = readTemplateNamespace(projectDir); |
| 60 | + expect(namespace).toBe('qa_console_fi'); |
| 61 | + expect(customizationNamespace(namespace)).toEqual({ ok: true, value: 'qa_console_fi_custom' }); |
| 62 | + |
| 63 | + // The config's `namespace:` literal is the same value the manifest carries. |
| 64 | + const config = fs.readFileSync(path.join(projectDir, 'objectstack.config.ts'), 'utf8'); |
| 65 | + expect(/\bnamespace:\s*'([^']+)'/.exec(config)?.[1]).toBe(namespace); |
| 66 | + |
| 67 | + // Every object the scaffold ships sits under the shortened namespace. |
| 68 | + const objects = nameLiterals(path.join(projectDir, 'src')).filter((n) => /_/.test(n)); |
| 69 | + expect(objects.length).toBeGreaterThan(0); |
| 70 | + for (const name of objects.filter((n) => n.startsWith('qa_'))) { |
| 71 | + expect(validateObjectNamespacePrefix(name, namespace as string), name).toBeNull(); |
| 72 | + } |
| 73 | + expect(objects.some((n) => n.startsWith(`${namespace}_`))).toBe(true); |
| 74 | + }, 30_000); |
| 75 | +}); |
0 commit comments