|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os migrate meta --json`: a run that replayed no step says so, in the same |
| 5 | + * words the human run does. |
| 6 | + * |
| 7 | + * ## The defect these pin |
| 8 | + * |
| 9 | + * `composeMigrationChain` keeps the majors `m > fromMajor && m <= toMajor`, so |
| 10 | + * an empty range (`--from T` with `T` the terminus, or `--from N --to N`) and |
| 11 | + * an inverted one (`--from T+1`, which defaults `--to` to `T`) both compose zero |
| 12 | + * steps. The human run answers that as an empty range: "this run replayed |
| 13 | + * nothing — that is not a finding that the metadata is canonical". The JSON run |
| 14 | + * answered it exactly like a clean replay — `applied: []`, `todos: []`, |
| 15 | + * `schemaValid: true`, exit 0 — so an upgrade script reading the JSON with a |
| 16 | + * wrong `--from` was told its metadata had been migrated. |
| 17 | + * |
| 18 | + * So the JSON answer now carries `replayedNothing` whenever no step ran, read |
| 19 | + * by the one function the human run renders. A run that replayed a step carries |
| 20 | + * no such member, and its payload keeps the shape it always had. |
| 21 | + * |
| 22 | + * ⛔ No expectation here hard-codes a major. Every range is derived from |
| 23 | + * `MIGRATION_MAJORS` and `PROTOCOL_MAJOR`, so the file stays true one major later. |
| 24 | + * |
| 25 | + * In-process over `MigrateMeta.run`: no process is spawned and no kernel is |
| 26 | + * booted, so this file sits in the `unit` tier by behaviour. |
| 27 | + */ |
| 28 | + |
| 29 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 30 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 31 | +import { tmpdir } from 'node:os'; |
| 32 | +import { join, resolve } from 'node:path'; |
| 33 | +import { fileURLToPath } from 'node:url'; |
| 34 | +import { stripVTControlCharacters } from 'node:util'; |
| 35 | +import { MIGRATION_MAJORS, MIGRATION_SUPPORT_FLOOR } from '@objectstack/spec/migrations'; |
| 36 | +import { PROTOCOL_MAJOR } from '@objectstack/spec/kernel'; |
| 37 | +import MigrateMeta from '../src/commands/migrate/meta.js'; |
| 38 | + |
| 39 | +const CLI_ROOT = resolve(fileURLToPath(import.meta.url), '..', '..'); |
| 40 | + |
| 41 | +/** Where the chain ends when `--to` is not given, computed as the command computes it. */ |
| 42 | +const TERMINUS = Math.max(PROTOCOL_MAJOR, ...MIGRATION_MAJORS); |
| 43 | + |
| 44 | +/** One below the terminus: a range that holds the terminus's own step. */ |
| 45 | +const BELOW = TERMINUS - 1; |
| 46 | + |
| 47 | +/** A canonical stack: nothing in it is a retired spelling. */ |
| 48 | +const CANONICAL_CONFIG = ` |
| 49 | +export default { |
| 50 | + manifest: { id: 'com.example.replayed-nothing', name: 'Replayed Nothing', version: '1.0.0', type: 'app' }, |
| 51 | + objects: [{ name: 'rn_ticket', label: 'Ticket', fields: { title: { type: 'text', label: 'Title' } } }], |
| 52 | +}; |
| 53 | +`; |
| 54 | + |
| 55 | +/** Every member a replay's payload carried before this change, in order (`hops` only under `--step`). */ |
| 56 | +const REPLAY_KEYS = [ |
| 57 | + 'from', 'to', 'protocolVersion', 'applied', 'todos', 'absentTodos', |
| 58 | + 'specChanges', 'schemaValid', 'dataMigrations', 'duration', |
| 59 | +]; |
| 60 | + |
| 61 | +interface Run { |
| 62 | + stdout: string; |
| 63 | + exitCode: number | undefined; |
| 64 | +} |
| 65 | + |
| 66 | +let dir: string; |
| 67 | +const runs = new Map<string, Promise<Run>>(); |
| 68 | + |
| 69 | +/** Run the real command in-process once per distinct invocation, capturing stdout and any exit. */ |
| 70 | +function runMeta(flags: string[]): Promise<Run> { |
| 71 | + const key = flags.join(' '); |
| 72 | + const hit = runs.get(key); |
| 73 | + if (hit) return hit; |
| 74 | + const started = (async (): Promise<Run> => { |
| 75 | + const out: string[] = []; |
| 76 | + const priorExitCode = process.exitCode; |
| 77 | + const write = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, ...rest: unknown[]) => { |
| 78 | + out.push(String(chunk)); |
| 79 | + const done = rest.find((r) => typeof r === 'function') as (() => void) | undefined; |
| 80 | + done?.(); |
| 81 | + return true; |
| 82 | + }) as never); |
| 83 | + const log = vi.spyOn(console, 'log').mockImplementation((...a: unknown[]) => { out.push(a.join(' ')); }); |
| 84 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 85 | + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 86 | + let exitCode: number | undefined; |
| 87 | + try { |
| 88 | + await MigrateMeta.run([join(dir, 'objectstack.config.ts'), ...flags], { root: CLI_ROOT }); |
| 89 | + } catch (e: any) { |
| 90 | + if (typeof e?.oclif?.exit !== 'number') throw e; |
| 91 | + exitCode = e.oclif.exit; |
| 92 | + } finally { |
| 93 | + write.mockRestore(); |
| 94 | + log.mockRestore(); |
| 95 | + warn.mockRestore(); |
| 96 | + error.mockRestore(); |
| 97 | + if (exitCode === undefined && typeof process.exitCode === 'number' && process.exitCode !== 0) { |
| 98 | + exitCode = process.exitCode; |
| 99 | + } |
| 100 | + process.exitCode = priorExitCode; |
| 101 | + } |
| 102 | + return { stdout: stripVTControlCharacters(out.join('\n')), exitCode }; |
| 103 | + })(); |
| 104 | + runs.set(key, started); |
| 105 | + return started; |
| 106 | +} |
| 107 | + |
| 108 | +const json = async (flags: string[]) => { |
| 109 | + const run = await runMeta([...flags, '--json']); |
| 110 | + return { exitCode: run.exitCode, payload: JSON.parse(run.stdout) }; |
| 111 | +}; |
| 112 | + |
| 113 | +beforeAll(() => { |
| 114 | + dir = mkdtempSync(join(tmpdir(), 'os-meta-replayed-nothing-')); |
| 115 | + writeFileSync(join(dir, 'objectstack.config.ts'), CANONICAL_CONFIG); |
| 116 | +}); |
| 117 | + |
| 118 | +afterAll(() => { |
| 119 | + try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ } |
| 120 | +}); |
| 121 | + |
| 122 | +describe('os migrate meta --json: a run that replayed no step says so', () => { |
| 123 | + it('derives its ranges from a registry that has a step at the terminus', () => { |
| 124 | + // Anti-vacuity: without a step at the terminus `--from BELOW` is no replay, |
| 125 | + // and the control case below would read an empty range as a real one. |
| 126 | + expect(MIGRATION_MAJORS).toContain(TERMINUS); |
| 127 | + expect(BELOW).toBeGreaterThanOrEqual(MIGRATION_SUPPORT_FLOOR); |
| 128 | + }); |
| 129 | + |
| 130 | + it('an empty range at the terminus (`--from T`) carries the marker, with no wider range', async () => { |
| 131 | + const { exitCode, payload } = await json(['--from', String(TERMINUS)]); |
| 132 | + expect(exitCode).toBeUndefined(); |
| 133 | + expect(payload.from).toBe(TERMINUS); |
| 134 | + expect(payload.to).toBe(TERMINUS); |
| 135 | + expect(payload.applied).toEqual([]); |
| 136 | + expect(payload.todos).toEqual([]); |
| 137 | + expect(payload.replayedNothing).toEqual({ reason: 'empty-range', widestRange: null }); |
| 138 | + }, 120_000); |
| 139 | + |
| 140 | + it('an explicit empty range (`--from T --to T`) carries the same marker', async () => { |
| 141 | + const { exitCode, payload } = await json(['--from', String(TERMINUS), '--to', String(TERMINUS)]); |
| 142 | + expect(exitCode).toBeUndefined(); |
| 143 | + expect(payload.replayedNothing).toEqual({ reason: 'empty-range', widestRange: null }); |
| 144 | + }, 120_000); |
| 145 | + |
| 146 | + it('an inverted range (`--from T+1`) carries the marker, and its reason says the range is inverted', async () => { |
| 147 | + const { exitCode, payload } = await json(['--from', String(TERMINUS + 1)]); |
| 148 | + // Not refused: an inverted range is answered, never gated. |
| 149 | + expect(exitCode).toBeUndefined(); |
| 150 | + expect(payload.from).toBe(TERMINUS + 1); |
| 151 | + expect(payload.to).toBe(TERMINUS); |
| 152 | + expect(payload.replayedNothing).toEqual({ reason: 'inverted-range', widestRange: null }); |
| 153 | + }, 120_000); |
| 154 | + |
| 155 | + it('an inverted range whose --to is below the terminus offers no wider range either', async () => { |
| 156 | + // `--from T --to T-1` and `--from T+1 --to T-1`: a "widest range" from a |
| 157 | + // `--from` at or above the terminus is itself empty or inverted, so there |
| 158 | + // is no wider range to name, whatever `--to` says. |
| 159 | + for (const from of [TERMINUS, TERMINUS + 1]) { |
| 160 | + const { exitCode, payload } = await json(['--from', String(from), '--to', String(BELOW)]); |
| 161 | + expect(exitCode).toBeUndefined(); |
| 162 | + expect(payload.from).toBe(from); |
| 163 | + expect(payload.to).toBe(BELOW); |
| 164 | + expect(payload.replayedNothing).toEqual({ reason: 'inverted-range', widestRange: null }); |
| 165 | + } |
| 166 | + }, 120_000); |
| 167 | + |
| 168 | + it('an empty range below the terminus names what the widest range holds, as the human run does', async () => { |
| 169 | + const flags = ['--from', String(BELOW), '--to', String(BELOW)]; |
| 170 | + const { exitCode, payload } = await json(flags); |
| 171 | + expect(exitCode).toBeUndefined(); |
| 172 | + expect(payload.replayedNothing?.reason).toBe('empty-range'); |
| 173 | + const widest = payload.replayedNothing?.widestRange; |
| 174 | + expect(widest).toMatchObject({ from: BELOW, to: TERMINUS }); |
| 175 | + expect(typeof widest.applied).toBe('number'); |
| 176 | + expect(typeof widest.manual).toBe('number'); |
| 177 | + // One reading, two faces: the counts the human run prints are the JSON's. |
| 178 | + const human = (await runMeta(flags)).stdout; |
| 179 | + if (widest.applied === 0 && widest.manual === 0) { |
| 180 | + expect(human).toContain(`(protocol ${BELOW} → ${TERMINUS}) has nothing for this stack either`); |
| 181 | + } else { |
| 182 | + expect(human).toContain( |
| 183 | + `Protocol ${BELOW} → ${TERMINUS} has ${widest.applied} mechanical and ${widest.manual} manual change(s)`, |
| 184 | + ); |
| 185 | + } |
| 186 | + }, 120_000); |
| 187 | + |
| 188 | + it('the human run names the inverted range too', async () => { |
| 189 | + const { stdout, exitCode } = await runMeta(['--from', String(TERMINUS + 1)]); |
| 190 | + expect(exitCode).toBeUndefined(); |
| 191 | + expect(stdout).toContain(`--from ${TERMINUS + 1} is above --to ${TERMINUS}`); |
| 192 | + expect(stdout).toContain('this run replayed nothing'); |
| 193 | + }, 120_000); |
| 194 | + |
| 195 | + it('a real replay (`--from T-1`) carries no marker and keeps the payload it always had', async () => { |
| 196 | + const { exitCode, payload } = await json(['--from', String(BELOW)]); |
| 197 | + expect(exitCode).toBeUndefined(); |
| 198 | + expect(payload.from).toBe(BELOW); |
| 199 | + expect(payload.to).toBe(TERMINUS); |
| 200 | + expect(Object.keys(payload)).toEqual(REPLAY_KEYS); |
| 201 | + }, 120_000); |
| 202 | +}); |
0 commit comments