|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A retired key's `tsc` diagnostic names the retirement. |
| 5 | + * |
| 6 | + * `retiredKey()` makes a removal audible in two channels: the parse (the |
| 7 | + * prescription) and `tsc`. Until this pin the `tsc` channel only said |
| 8 | + * `Type 'string[]' is not assignable to type 'undefined'` — audible, but it |
| 9 | + * named no retirement, and an upgrading author read forty of them as typing |
| 10 | + * bugs. The tombstone's input type now carries a fixed sentence |
| 11 | + * (`RetiredKeySchema` in `retired-key.ts`), and this file measures what `tsc` |
| 12 | + * actually prints for an authored retired key. |
| 13 | + * |
| 14 | + * ## Why the compiler API |
| 15 | + * |
| 16 | + * A `@ts-expect-error` proves only THAT the line fails, never what the |
| 17 | + * diagnostic says — and the text is the whole payload here. So each probe |
| 18 | + * below is compiled with `ts.createProgram` against this package's real |
| 19 | + * source (`@objectstack/spec` is mapped to `src/` through `paths`) and the |
| 20 | + * assertion reads the diagnostic itself. The probes are in-memory overlays; |
| 21 | + * nothing is written to disk. |
| 22 | + * |
| 23 | + * ## What is asserted, and what is not |
| 24 | + * |
| 25 | + * The diagnostic code, the line (the retired key's own), and the two named |
| 26 | + * subjects the sentence exists to deliver: the house `[REMOVED]` marker and the |
| 27 | + * door that prints the per-key prescription, `os validate`. The sentence's |
| 28 | + * remaining wording is deliberately not pinned. |
| 29 | + * |
| 30 | + * The cases span both object modes and both value kinds, because `tsc` words |
| 31 | + * them differently: an array value reads as a missing property (TS2741), a |
| 32 | + * string as a plain non-assignable type (TS2322). `PageSchema` and |
| 33 | + * `FieldSchema` are `strictObject`s; `IndexSchema` is a plain `z.object`. |
| 34 | + * |
| 35 | + * ⚠️ Anti-vacuity: a harness that resolves nothing reports import errors or |
| 36 | + * nothing at all, and both look like "no retirement text". The control probe |
| 37 | + * authors the same stack WITHOUT the retired keys and must compile with ZERO |
| 38 | + * diagnostics, which proves `@objectstack/spec` resolved and `defineStack` |
| 39 | + * type-checks the literal. |
| 40 | + */ |
| 41 | + |
| 42 | +import { describe, it, expect } from 'vitest'; |
| 43 | +import ts from 'typescript'; |
| 44 | +import { dirname, resolve } from 'node:path'; |
| 45 | +import { fileURLToPath } from 'node:url'; |
| 46 | + |
| 47 | +const SRC_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| 48 | +const PROBE_DIR = resolve(SRC_DIR, 'shared/__retired_key_tsc_probes__'); |
| 49 | + |
| 50 | +/** The stack every probe authors; `retired` splices retired keys into it. */ |
| 51 | +function stackProbe(retired: { page?: string; field?: string; index?: string }): string { |
| 52 | + return [ |
| 53 | + "import { defineStack } from '@objectstack/spec';", |
| 54 | + '', |
| 55 | + 'export const stack = defineStack({', |
| 56 | + " manifest: { id: 'com.example.probe', name: 'probe', version: '1.0.0', type: 'app' },", |
| 57 | + ' objects: [', |
| 58 | + ' {', |
| 59 | + " name: 'deal',", |
| 60 | + " label: 'Deal',", |
| 61 | + ' fields: {', |
| 62 | + " amount: { type: 'number', label: 'Amount' },", |
| 63 | + ` stage: { type: 'text', label: 'Stage'${retired.field ?? ''} },`, |
| 64 | + ' },', |
| 65 | + ` indexes: [{ fields: ['amount']${retired.index ?? ''} }],`, |
| 66 | + ' },', |
| 67 | + ' ],', |
| 68 | + ` pages: [{ name: 'home_page', label: 'Home', type: 'home'${retired.page ?? ''} }],`, |
| 69 | + '});', |
| 70 | + '', |
| 71 | + ].join('\n'); |
| 72 | +} |
| 73 | + |
| 74 | +const PROBES = { |
| 75 | + control: stackProbe({}), |
| 76 | + // HotCRM's measured case: `page.assignedProfiles` (strictObject, array value). |
| 77 | + page: stackProbe({ page: ", assignedProfiles: ['sales_rep']" }), |
| 78 | + // A rename retirement on a strictObject, string value. |
| 79 | + field: stackProbe({ field: ", conditionalRequired: 'amount > 0'" }), |
| 80 | + // A non-strict `z.object` tombstone, string value. |
| 81 | + index: stackProbe({ index: ", type: 'btree'" }), |
| 82 | +} as const; |
| 83 | + |
| 84 | +type ProbeName = keyof typeof PROBES; |
| 85 | + |
| 86 | +function compileProbes(): Map<ProbeName, readonly ts.Diagnostic[]> { |
| 87 | + const paths = new Map<string, string>(); |
| 88 | + for (const [name, text] of Object.entries(PROBES)) paths.set(resolve(PROBE_DIR, `${name}.ts`), text); |
| 89 | + |
| 90 | + const options: ts.CompilerOptions = { |
| 91 | + target: ts.ScriptTarget.ES2022, |
| 92 | + module: ts.ModuleKind.ESNext, |
| 93 | + moduleResolution: ts.ModuleResolutionKind.Bundler, |
| 94 | + strict: true, |
| 95 | + skipLibCheck: true, |
| 96 | + noEmit: true, |
| 97 | + types: ['node'], |
| 98 | + paths: { |
| 99 | + '@objectstack/spec': [resolve(SRC_DIR, 'index.ts')], |
| 100 | + '@objectstack/spec/*': [resolve(SRC_DIR, '*/index.ts')], |
| 101 | + }, |
| 102 | + }; |
| 103 | + |
| 104 | + const host = ts.createCompilerHost(options, true); |
| 105 | + const realGetSourceFile = host.getSourceFile.bind(host); |
| 106 | + const realFileExists = host.fileExists.bind(host); |
| 107 | + const realReadFile = host.readFile.bind(host); |
| 108 | + host.getSourceFile = (fileName, languageVersion, onError, shouldCreate) => { |
| 109 | + const overlay = paths.get(resolve(fileName)); |
| 110 | + return overlay === undefined |
| 111 | + ? realGetSourceFile(fileName, languageVersion, onError, shouldCreate) |
| 112 | + : ts.createSourceFile(fileName, overlay, languageVersion, true); |
| 113 | + }; |
| 114 | + host.fileExists = (fileName) => paths.has(resolve(fileName)) || realFileExists(fileName); |
| 115 | + host.readFile = (fileName) => paths.get(resolve(fileName)) ?? realReadFile(fileName); |
| 116 | + |
| 117 | + const program = ts.createProgram([...paths.keys()], options, host); |
| 118 | + const out = new Map<ProbeName, readonly ts.Diagnostic[]>(); |
| 119 | + for (const name of Object.keys(PROBES) as ProbeName[]) { |
| 120 | + const file = program.getSourceFile(resolve(PROBE_DIR, `${name}.ts`)); |
| 121 | + if (file === undefined) throw new Error(`probe ${name} did not enter the program`); |
| 122 | + out.set(name, [...program.getSyntacticDiagnostics(file), ...program.getSemanticDiagnostics(file)]); |
| 123 | + } |
| 124 | + return out; |
| 125 | +} |
| 126 | + |
| 127 | +/** `L<line> TS<code>: <message>`, one per diagnostic. */ |
| 128 | +function render(diagnostics: readonly ts.Diagnostic[]): string[] { |
| 129 | + return diagnostics.map((d) => { |
| 130 | + const line = d.file && d.start !== undefined ? d.file.getLineAndCharacterOfPosition(d.start).line + 1 : 0; |
| 131 | + return `L${line} TS${d.code}: ${ts.flattenDiagnosticMessageText(d.messageText, ' ')}`; |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +/** The 1-based line of `needle` in a probe's text. */ |
| 136 | +function lineOf(name: ProbeName, needle: string): number { |
| 137 | + const lines = PROBES[name].split('\n'); |
| 138 | + const index = lines.findIndex((l) => l.includes(needle)); |
| 139 | + if (index < 0) throw new Error(`${needle} is not in probe ${name}`); |
| 140 | + return index + 1; |
| 141 | +} |
| 142 | + |
| 143 | +describe('a retired key names its retirement in the tsc diagnostic', () => { |
| 144 | + const diagnostics = compileProbes(); |
| 145 | + |
| 146 | + it('control: the same stack without retired keys compiles clean (the harness resolves the spec)', () => { |
| 147 | + expect(render(diagnostics.get('control')!)).toEqual([]); |
| 148 | + }); |
| 149 | + |
| 150 | + const cases: ReadonlyArray<[ProbeName, string, number]> = [ |
| 151 | + ['page', 'assignedProfiles', 2741], |
| 152 | + ['field', 'conditionalRequired', 2322], |
| 153 | + ['index', "type: 'btree'", 2322], |
| 154 | + ]; |
| 155 | + |
| 156 | + it.each(cases)('%s: writing %s fails with TS%i, and the message says [REMOVED] and names `os validate`', (name, key, code) => { |
| 157 | + const rendered = render(diagnostics.get(name)!); |
| 158 | + expect(rendered, 'exactly one diagnostic: the retired key').toHaveLength(1); |
| 159 | + const [only] = rendered; |
| 160 | + expect(only.startsWith(`L${lineOf(name, key)} TS${code}: `), only).toBe(true); |
| 161 | + expect(only).toContain('[REMOVED]'); |
| 162 | + expect(only).toContain('`os validate`'); |
| 163 | + // The form this replaced named no retirement at all. |
| 164 | + expect(only).not.toContain("to type 'undefined'"); |
| 165 | + }); |
| 166 | +}); |
0 commit comments