|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36895 — no server package builds a Studio address by hand, and |
| 5 | + * the review links the `objectstack-mcp-build` skill's evals quote are the |
| 6 | + * ones this package builds. |
| 7 | + * |
| 8 | + * ── Why ────────────────────────────────────────────────────────────────────── |
| 9 | + * |
| 10 | + * The Studio redesign (epic #36890) moves Studio's addresses one item type at |
| 11 | + * a time. A `/studio/...` string written out in a server package would keep |
| 12 | + * the old address after the move, and an AI handed it would send a person to |
| 13 | + * the wrong page. `studio-review-routes.ts` is the one place a server spells |
| 14 | + * one; the Console's builder (`studioAddress.ts` in the app-shell package) |
| 15 | + * is held by the same kind of pin in the ui workspace. |
| 16 | + * |
| 17 | + * ── What it judges ─────────────────────────────────────────────────────────── |
| 18 | + * |
| 19 | + * 1. Every `.ts` source under `packages/` (tests, declaration files and build |
| 20 | + * output excepted), parsed: a string or template literal that starts with |
| 21 | + * `/studio` (then its end, `/`, `?` or `#`) is a hand-built address. Prose |
| 22 | + * in comments is free. |
| 23 | + * 2. The eval file of the published skill (it is governed: it is read, never |
| 24 | + * edited here): every Studio address it quotes equals what |
| 25 | + * `studioChangesReviewRoute` / `studioItemReviewRoute` return for the |
| 26 | + * package, type and name the address names, so the two cannot drift. |
| 27 | + * |
| 28 | + * ── Controls ───────────────────────────────────────────────────────────────── |
| 29 | + * |
| 30 | + * The walk must reach the two server producers, the matcher must fire on the |
| 31 | + * one allowed file, and the eval file must yield its quoted addresses (seven |
| 32 | + * on the commit this landed: five Changes links, two item reviews). Zero in |
| 33 | + * any of them means the instrument is broken, not that the tree is clean. |
| 34 | + * |
| 35 | + * The radius: `packages/**` `.ts` (already this package's declared glob, the |
| 36 | + * extension boundary is the one `driver-error-classification.callers.test.ts` |
| 37 | + * records) plus the one eval file, declared beside it. |
| 38 | + */ |
| 39 | + |
| 40 | +import { describe, expect, it } from 'vitest'; |
| 41 | +import { existsSync, readdirSync, readFileSync } from 'node:fs'; |
| 42 | +import { dirname, join, relative, resolve, sep } from 'node:path'; |
| 43 | +import ts from 'typescript'; |
| 44 | +import { studioChangesReviewRoute, studioItemReviewRoute } from './studio-review-routes.js'; |
| 45 | + |
| 46 | +/** |
| 47 | + * This package is CJS-typed, so `import.meta` is not available: walk up from |
| 48 | + * the CWD to this package's own manifest (`driver-error-classification.callers.test.ts` |
| 49 | + * says why), then reach the repo root by arithmetic. |
| 50 | + */ |
| 51 | +function findUp(marker: (dir: string) => boolean, what: string): string { |
| 52 | + let dir = process.cwd(); |
| 53 | + for (;;) { |
| 54 | + if (marker(dir)) return dir; |
| 55 | + const parent = dirname(dir); |
| 56 | + if (parent === dir) throw new Error(`could not locate ${what} walking up from ${process.cwd()}`); |
| 57 | + dir = parent; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const PACKAGE_ROOT = findUp((dir) => { |
| 62 | + const manifest = join(dir, 'package.json'); |
| 63 | + if (!existsSync(manifest)) return false; |
| 64 | + const { name } = JSON.parse(readFileSync(manifest, 'utf8')) as { name?: string }; |
| 65 | + return name === '@objectstack/types'; |
| 66 | +}, 'the @objectstack/types package root'); |
| 67 | + |
| 68 | +const REPO_ROOT = resolve(PACKAGE_ROOT, '../..'); |
| 69 | + |
| 70 | +const SCANNED_TREE = join(REPO_ROOT, 'packages'); |
| 71 | + |
| 72 | +/** The skill eval whose quoted review links must equal the builder's. */ |
| 73 | +const EVAL_FILE = join(REPO_ROOT, 'skills/objectstack-mcp-build/evals/draft-door-procedure.json'); |
| 74 | + |
| 75 | +/** The one server file that spells a Studio address. */ |
| 76 | +const ALLOWED = 'packages/types/src/studio-review-routes.ts'; |
| 77 | + |
| 78 | +const SKIP_DIRS = new Set(['node_modules', 'dist', 'build', 'coverage', '.turbo', '.next', '__tests__']); |
| 79 | + |
| 80 | +const SOURCE_FILE = (name: string): boolean => |
| 81 | + name.endsWith('.ts') && !name.endsWith('.d.ts') && !/\.(?:test|spec)\.ts$/.test(name); |
| 82 | + |
| 83 | +const STUDIO_LITERAL = /^\/studio(?:$|[/?#])/; |
| 84 | + |
| 85 | +function sourceFilesUnder(root: string): string[] { |
| 86 | + const out: string[] = []; |
| 87 | + const walk = (dir: string): void => { |
| 88 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 89 | + if (entry.isDirectory()) { |
| 90 | + if (!SKIP_DIRS.has(entry.name)) walk(join(dir, entry.name)); |
| 91 | + continue; |
| 92 | + } |
| 93 | + if (entry.isFile() && SOURCE_FILE(entry.name)) out.push(join(dir, entry.name)); |
| 94 | + } |
| 95 | + }; |
| 96 | + walk(root); |
| 97 | + return out; |
| 98 | +} |
| 99 | + |
| 100 | +function handBuiltStudioAddresses(fileName: string, text: string): string[] { |
| 101 | + if (!text.includes('/studio')) return []; |
| 102 | + const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); |
| 103 | + const hits: string[] = []; |
| 104 | + const visit = (node: ts.Node): void => { |
| 105 | + const literalText = |
| 106 | + ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node) |
| 107 | + ? node.text |
| 108 | + : ts.isTemplateExpression(node) |
| 109 | + ? node.head.text |
| 110 | + : null; |
| 111 | + if (literalText !== null && STUDIO_LITERAL.test(literalText)) { |
| 112 | + const { line } = source.getLineAndCharacterOfPosition(node.getStart(source)); |
| 113 | + hits.push(`${line + 1}: ${node.getText(source).replace(/\s+/g, ' ').slice(0, 120)}`); |
| 114 | + } |
| 115 | + ts.forEachChild(node, visit); |
| 116 | + }; |
| 117 | + visit(source); |
| 118 | + return hits; |
| 119 | +} |
| 120 | + |
| 121 | +/** Every Studio address quoted in a text, up to the first quote, space, backtick or paren. */ |
| 122 | +function quotedStudioAddresses(text: string): string[] { |
| 123 | + return [...text.matchAll(/\/studio\/[^\s"'`)]+/g)].map((m) => m[0]); |
| 124 | +} |
| 125 | + |
| 126 | +/** What the builder returns for the package, type and name an address names. */ |
| 127 | +function rebuilt(address: string): string | null { |
| 128 | + const changes = /^\/studio\/([^/?#]+)\/data\?changes=review$/.exec(address); |
| 129 | + if (changes) return studioChangesReviewRoute(decodeURIComponent(changes[1])); |
| 130 | + const item = /^\/studio\/([^/?#]+)\/metadata\/([^/?#]+)\/([^/?#]+)\?review=1$/.exec(address); |
| 131 | + if (item) { |
| 132 | + return studioItemReviewRoute(decodeURIComponent(item[1]), decodeURIComponent(item[2]), decodeURIComponent(item[3])); |
| 133 | + } |
| 134 | + return null; |
| 135 | +} |
| 136 | + |
| 137 | +describe('no server package builds a Studio address by hand (objectstack#36895)', () => { |
| 138 | + it('the matcher sees each spelling and spares prose and look-alikes', () => { |
| 139 | + const scan = (text: string) => handBuiltStudioAddresses('x.ts', text); |
| 140 | + expect(scan("const a = '/studio';")).toHaveLength(1); |
| 141 | + expect(scan('const b = `/studio/${pkg}/data?changes=review`;')).toHaveLength(1); |
| 142 | + expect(scan("const c = 'see (/studio/x)';")).toEqual([]); |
| 143 | + expect(scan("const d = '/api/v1/studio/runtime-config';")).toEqual([]); |
| 144 | + expect(scan('// links to /studio/crm/data\nconst e = 1;')).toEqual([]); |
| 145 | + expect(scan("const f = '/studios';")).toEqual([]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('only the shared module spells one', () => { |
| 149 | + const files = sourceFilesUnder(SCANNED_TREE); |
| 150 | + const rel = (file: string) => relative(REPO_ROOT, file).split(sep).join('/'); |
| 151 | + const names = new Set(files.map(rel)); |
| 152 | + // Controls: the walk reaches both server producers, and the matcher fires |
| 153 | + // on the allowed module. |
| 154 | + expect(names.has('packages/runtime/src/domains/mcp-authoring.ts')).toBe(true); |
| 155 | + expect(names.has('packages/cli/src/commands/meta/register.ts')).toBe(true); |
| 156 | + expect(handBuiltStudioAddresses(ALLOWED, readFileSync(join(REPO_ROOT, ALLOWED), 'utf8')).length).toBe(2); |
| 157 | + |
| 158 | + const offenders = files |
| 159 | + .filter((file) => rel(file) !== ALLOWED) |
| 160 | + .flatMap((file) => handBuiltStudioAddresses(file, readFileSync(file, 'utf8')).map((hit) => `${rel(file)}:${hit}`)); |
| 161 | + expect(offenders).toEqual([]); |
| 162 | + }); |
| 163 | + |
| 164 | + it("the skill evals quote the builder's review links, byte for byte", () => { |
| 165 | + const quoted = quotedStudioAddresses(readFileSync(EVAL_FILE, 'utf8')); |
| 166 | + // Control: the eval file still quotes its review links. |
| 167 | + expect(quoted.length).toBeGreaterThanOrEqual(7); |
| 168 | + for (const address of quoted) expect(rebuilt(address), address).toBe(address); |
| 169 | + }); |
| 170 | +}); |
0 commit comments