|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os migrate meta`: the DEFAULT `--to`, pinned in the window #17134 was found |
| 5 | + * in, with that window held open on purpose. |
| 6 | + * |
| 7 | + * ## Why this file exists beside `migrate-meta-default-range.test.ts` |
| 8 | + * |
| 9 | + * The default under test is `Math.max(PROTOCOL_MAJOR, ...MIGRATION_MAJORS)`. |
| 10 | + * The default it replaced is `PROTOCOL_MAJOR`. The two differ only while the |
| 11 | + * registry carries a step PAST the runtime major, which is the window each |
| 12 | + * major's line spends accumulating the next major's conversions. Measured at |
| 13 | + * protocol 18 on #22130's branch: `MIGRATION_MAJORS` is `[17, 18]`, and no |
| 14 | + * conversion registers `toMajor: 19`. Both defaults are 18, so no run of the |
| 15 | + * installed CLI tells them apart. The spawned file's anti-vacuity line |
| 16 | + * (`TERMINUS > PROTOCOL_MAJOR`) would read `18 > 18` there. |
| 17 | + * |
| 18 | + * So the window is held open here rather than waited for. The real command |
| 19 | + * runs in-process over the real registry, and only the runtime's protocol |
| 20 | + * major is replaced: by the rename step's AUTHORING major (`toMajor - 1`). |
| 21 | + * That is the world `@objectstack/spec@17.4.0` shipped in and the card was |
| 22 | + * measured in. In it, the old default composes `N → N` and lists nothing, |
| 23 | + * while the default under test reaches the rename's step and lists all five |
| 24 | + * rewrites. The anti-vacuity assertion is the spawned file's own line, applied |
| 25 | + * to this window, where it holds in every release instead of only while the |
| 26 | + * live registry runs ahead. |
| 27 | + * |
| 28 | + * ⛔ The replacement is the module's two protocol constants and nothing else. |
| 29 | + * The registry, the conversions, the schemas and the loader are the installed |
| 30 | + * ones, so the five rewrites and the schema verdict are the real chain's. |
| 31 | + * |
| 32 | + * In-process over `MigrateMeta.run`: no process is spawned and no kernel is |
| 33 | + * booted, so this file sits in the `unit` tier by behaviour. |
| 34 | + */ |
| 35 | + |
| 36 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 37 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 38 | +import { tmpdir } from 'node:os'; |
| 39 | +import { join, resolve } from 'node:path'; |
| 40 | +import { fileURLToPath } from 'node:url'; |
| 41 | +import { stripVTControlCharacters } from 'node:util'; |
| 42 | +import { MIGRATIONS_BY_MAJOR, MIGRATION_MAJORS } from '@objectstack/spec/migrations'; |
| 43 | +import { PROTOCOL_MAJOR } from '@objectstack/spec/kernel'; |
| 44 | +import MigrateMeta from '../src/commands/migrate/meta.js'; |
| 45 | + |
| 46 | +const { RENAME_CONVERSION } = vi.hoisted(() => ({ |
| 47 | + RENAME_CONVERSION: 'dashboard-refresh-interval-to-refresh-interval-seconds', |
| 48 | +})); |
| 49 | + |
| 50 | +/** |
| 51 | + * The window: the runtime still on the major the tombstone's source was |
| 52 | + * authored against, with the rename's step already registered. Derived from |
| 53 | + * the registry, never written down. A missing step is a loud failure here, |
| 54 | + * because the window would otherwise be undefined. |
| 55 | + */ |
| 56 | +vi.mock('@objectstack/spec/kernel', async (importOriginal) => { |
| 57 | + const actual = await importOriginal<typeof import('@objectstack/spec/kernel')>(); |
| 58 | + const { MIGRATIONS_BY_MAJOR: steps } = await import('@objectstack/spec/migrations'); |
| 59 | + const step = Object.values(steps).find((s) => s.conversionIds.includes(RENAME_CONVERSION)); |
| 60 | + if (!step) throw new Error(`no registered migration step carries ${RENAME_CONVERSION}`); |
| 61 | + const major = step.toMajor - 1; |
| 62 | + return { ...actual, PROTOCOL_MAJOR: major, PROTOCOL_VERSION: `${major}.0.0` }; |
| 63 | +}); |
| 64 | + |
| 65 | +const CLI_ROOT = resolve(fileURLToPath(import.meta.url), '..', '..'); |
| 66 | + |
| 67 | +const RENAME_STEP = Object.values(MIGRATIONS_BY_MAJOR).find((s) => s.conversionIds.includes(RENAME_CONVERSION))!; |
| 68 | + |
| 69 | +/** The tombstone's `N`, and in this window also the runtime's major. */ |
| 70 | +const AUTHORED = RENAME_STEP.toMajor - 1; |
| 71 | + |
| 72 | +/** The default under test, computed as the command computes it, in this window. */ |
| 73 | +const TERMINUS = Math.max(PROTOCOL_MAJOR, ...MIGRATION_MAJORS); |
| 74 | + |
| 75 | +/** The spawned file's reproduction: five dashboards authoring the retired key. */ |
| 76 | +const RETIRED_KEY_CONFIG = ` |
| 77 | +export default { |
| 78 | + manifest: { id: 'com.example.default-range-window', name: 'Default Range Window', version: '1.0.0', type: 'app' }, |
| 79 | + objects: [{ name: 'dw_ticket', label: 'Ticket', fields: { title: { type: 'text', label: 'Title' } } }], |
| 80 | + dashboards: [ |
| 81 | + { name: 'kpi_a', label: 'KPI A', widgets: [], refreshInterval: 300 }, |
| 82 | + { name: 'kpi_b', label: 'KPI B', widgets: [], refreshInterval: 60 }, |
| 83 | + { name: 'kpi_c', label: 'KPI C', widgets: [], refreshInterval: 120 }, |
| 84 | + { name: 'kpi_d', label: 'KPI D', widgets: [], refreshInterval: 900 }, |
| 85 | + { name: 'kpi_e', label: 'KPI E', widgets: [], refreshInterval: 30 }, |
| 86 | + ], |
| 87 | +}; |
| 88 | +`; |
| 89 | + |
| 90 | +interface Run { |
| 91 | + stdout: string; |
| 92 | + stderr: string; |
| 93 | + exitCode: number | undefined; |
| 94 | +} |
| 95 | + |
| 96 | +let dir: string; |
| 97 | +const runs = new Map<string, Promise<Run>>(); |
| 98 | + |
| 99 | +/** Run the real command in-process once per distinct invocation, capturing both streams and any exit. */ |
| 100 | +function runMeta(flags: string[]): Promise<Run> { |
| 101 | + const key = flags.join(' '); |
| 102 | + const hit = runs.get(key); |
| 103 | + if (hit) return hit; |
| 104 | + const started = (async (): Promise<Run> => { |
| 105 | + const out: string[] = []; |
| 106 | + const err: string[] = []; |
| 107 | + const priorExitCode = process.exitCode; |
| 108 | + const write = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, ...rest: unknown[]) => { |
| 109 | + out.push(String(chunk)); |
| 110 | + const done = rest.find((r) => typeof r === 'function') as (() => void) | undefined; |
| 111 | + done?.(); |
| 112 | + return true; |
| 113 | + }) as never); |
| 114 | + const log = vi.spyOn(console, 'log').mockImplementation((...a: unknown[]) => { out.push(a.join(' ')); }); |
| 115 | + const warn = vi.spyOn(console, 'warn').mockImplementation((...a: unknown[]) => { err.push(a.join(' ')); }); |
| 116 | + const error = vi.spyOn(console, 'error').mockImplementation((...a: unknown[]) => { err.push(a.join(' ')); }); |
| 117 | + let exitCode: number | undefined; |
| 118 | + try { |
| 119 | + await MigrateMeta.run([join(dir, 'objectstack.config.ts'), ...flags], { root: CLI_ROOT }); |
| 120 | + } catch (e: any) { |
| 121 | + if (typeof e?.oclif?.exit !== 'number') throw e; |
| 122 | + exitCode = e.oclif.exit; |
| 123 | + } finally { |
| 124 | + write.mockRestore(); |
| 125 | + log.mockRestore(); |
| 126 | + warn.mockRestore(); |
| 127 | + error.mockRestore(); |
| 128 | + if (exitCode === undefined && typeof process.exitCode === 'number' && process.exitCode !== 0) { |
| 129 | + exitCode = process.exitCode; |
| 130 | + } |
| 131 | + process.exitCode = priorExitCode; |
| 132 | + } |
| 133 | + return { |
| 134 | + stdout: stripVTControlCharacters(out.join('\n')), |
| 135 | + stderr: stripVTControlCharacters(err.join('\n')), |
| 136 | + exitCode, |
| 137 | + }; |
| 138 | + })(); |
| 139 | + runs.set(key, started); |
| 140 | + return started; |
| 141 | +} |
| 142 | + |
| 143 | +beforeAll(() => { |
| 144 | + dir = mkdtempSync(join(tmpdir(), 'os-meta-default-window-')); |
| 145 | + writeFileSync(join(dir, 'objectstack.config.ts'), RETIRED_KEY_CONFIG); |
| 146 | +}); |
| 147 | + |
| 148 | +afterAll(() => { |
| 149 | + try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ } |
| 150 | +}); |
| 151 | + |
| 152 | +describe('os migrate meta: the default --to in the window the card was found in (#17134)', () => { |
| 153 | + it('runs in that window: the runtime major is the tombstone\'s authoring major', () => { |
| 154 | + // The replacement reached the module the command reads. Without this, a |
| 155 | + // mock that silently missed would leave the cases below on the live major. |
| 156 | + expect(PROTOCOL_MAJOR).toBe(AUTHORED); |
| 157 | + expect(MIGRATION_MAJORS).toContain(RENAME_STEP.toMajor); |
| 158 | + }); |
| 159 | + |
| 160 | + it('defaults --to to the highest major this build has a step for, not the runtime major', async () => { |
| 161 | + const { stdout, exitCode } = await runMeta(['--from', String(AUTHORED), '--json']); |
| 162 | + const parsed = JSON.parse(stdout); |
| 163 | + expect(exitCode).toBeUndefined(); |
| 164 | + expect(parsed.from).toBe(AUTHORED); |
| 165 | + expect(parsed.to).toBe(TERMINUS); |
| 166 | + // ⛔ Anti-vacuity. If the terminus ever equalled PROTOCOL_MAJOR the line |
| 167 | + // above would hold for the very default this card exists to replace, so the |
| 168 | + // premise is asserted rather than assumed: this is the line that speaks up |
| 169 | + // when a major ships and the registry has no entry past it yet. |
| 170 | + expect(TERMINUS, 'the registry carries a step past the runtime major').toBeGreaterThan(PROTOCOL_MAJOR); |
| 171 | + }, 120_000); |
| 172 | + |
| 173 | + it('lists every retired-key rewrite for the tombstone\'s `--from`, with no --to given', async () => { |
| 174 | + const parsed = JSON.parse((await runMeta(['--from', String(AUTHORED), '--json'])).stdout); |
| 175 | + const renames = parsed.applied.filter((a: any) => a.conversionId === RENAME_CONVERSION); |
| 176 | + expect(renames.map((a: any) => a.path)).toEqual([ |
| 177 | + 'dashboards[0].refreshIntervalSeconds', |
| 178 | + 'dashboards[1].refreshIntervalSeconds', |
| 179 | + 'dashboards[2].refreshIntervalSeconds', |
| 180 | + 'dashboards[3].refreshIntervalSeconds', |
| 181 | + 'dashboards[4].refreshIntervalSeconds', |
| 182 | + ]); |
| 183 | + for (const r of renames) { |
| 184 | + expect(r.from).toBe('refreshInterval'); |
| 185 | + expect(r.to).toBe('refreshIntervalSeconds'); |
| 186 | + } |
| 187 | + expect(parsed.schemaValid).toBe(true); |
| 188 | + }, 120_000); |
| 189 | + |
| 190 | + it('the human run prints the rewrites instead of the canonical verdict', async () => { |
| 191 | + const { stdout, exitCode } = await runMeta(['--from', String(AUTHORED)]); |
| 192 | + expect(exitCode).toBeUndefined(); |
| 193 | + expect(stdout).toContain('Applied 5 mechanical change(s)'); |
| 194 | + expect(stdout).toContain(RENAME_CONVERSION); |
| 195 | + // ⛔ The whole sentence, never the phrase: step-18 semantic entries open a |
| 196 | + // `replacement` with "Nothing to migrate to, because …". |
| 197 | + expect(stdout).not.toContain('Nothing to migrate — the metadata is already canonical'); |
| 198 | + }, 120_000); |
| 199 | +}); |
0 commit comments