|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #38923 — `os migrate meta --stored` is not green over a stored row the |
| 5 | + * runtime serves invalid. |
| 6 | + * |
| 7 | + * The pass (`migrateStoredMetadata`) judges each row it reads with the |
| 8 | + * runtime's own verdict and reports `servedInvalid`; `storedMigrationClean` |
| 9 | + * then answers false. This pins the command's half: the exit is non-zero, |
| 10 | + * `--json` carries the count and the per-row verdict beside `clean` (added, |
| 11 | + * nothing renamed), and the human output names the row and says why the run |
| 12 | + * is red — and a report with none stays green. |
| 13 | + * |
| 14 | + * The boot is replaced, so no database is opened; the command's own control |
| 15 | + * flow runs for real, the formatter and the verdict included. |
| 16 | + */ |
| 17 | + |
| 18 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 19 | +import { dirname, resolve } from 'node:path'; |
| 20 | +import { fileURLToPath } from 'node:url'; |
| 21 | +import { stripVTControlCharacters } from 'node:util'; |
| 22 | +import MigrateMeta from './meta.js'; |
| 23 | +// The packages the command reaches through dynamic imports, loaded at module top so the |
| 24 | +// first load is paid during collection, never inside a clocked case (`check:test-source-alias`). |
| 25 | +import '@objectstack/cloud-connection'; |
| 26 | +import '@objectstack/driver-sql'; |
| 27 | +import '@objectstack/driver-turso'; |
| 28 | +import '@objectstack/metadata'; |
| 29 | +import '@objectstack/metadata-protocol'; |
| 30 | +import '@objectstack/platform-objects/plugin'; |
| 31 | +import '@objectstack/plugin-hono-server'; |
| 32 | +import '@objectstack/plugin-security'; |
| 33 | +import '@objectstack/rest'; |
| 34 | +import '@objectstack/service-automation'; |
| 35 | +import '@objectstack/service-datasource'; |
| 36 | +import '@objectstack/service-settings'; |
| 37 | +import '@objectstack/service-storage'; |
| 38 | + |
| 39 | +const boot = vi.hoisted(() => vi.fn()); |
| 40 | +vi.mock('../../utils/schema-migrate.js', () => ({ bootSchemaStack: boot })); |
| 41 | +vi.mock('../../utils/migrate-occupancy-gate.js', () => ({ |
| 42 | + OCCUPANCY_HINT: 'occupancy hint', |
| 43 | + probeMigrationTarget: vi.fn(async () => ({ status: 'free' })), |
| 44 | +})); |
| 45 | +vi.mock('../../utils/data-migration-plugins.js', () => ({ buildDataMigrationPlugins: vi.fn(async () => []) })); |
| 46 | + |
| 47 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 48 | +const CLI_ROOT = resolve(HERE, '..', '..', '..'); |
| 49 | +/** oclif builds its whole command table on the first `run()` in a process. */ |
| 50 | +const RUN_TIMEOUT = 60_000; |
| 51 | + |
| 52 | +/** The stored view of the card: a TODO, nothing to convert, and served invalid. */ |
| 53 | +const VIEW_ROW = { |
| 54 | + id: 'r_7', |
| 55 | + type: 'view', |
| 56 | + name: 'lead_grid', |
| 57 | + organizationId: null, |
| 58 | + packageId: 'app.crm', |
| 59 | + state: 'active' as const, |
| 60 | + outcome: 'skipped' as const, |
| 61 | + notices: [], |
| 62 | + todos: [{ |
| 63 | + conversionId: 'view-bulk-actions-to-action-selection', |
| 64 | + surface: 'view.list.bulkActions', |
| 65 | + from: '["merge_leads"]', |
| 66 | + path: 'views[0].list.bulkActions', |
| 67 | + reason: 'This stored view still carries the retired `bulkActions`.', |
| 68 | + message: 'left as stored', |
| 69 | + }], |
| 70 | + reason: 'the conversion chain rewrites nothing here', |
| 71 | + servedInvalid: { path: 'list.bulkActions', message: '`view.list.bulkActions` was removed in @objectstack/spec 19.0.0' }, |
| 72 | +}; |
| 73 | + |
| 74 | +function reportOf(servedInvalid: boolean) { |
| 75 | + return { |
| 76 | + apply: false, |
| 77 | + protocol: '19.0.0', |
| 78 | + scanned: 2, |
| 79 | + canonical: 1, |
| 80 | + pending: 0, |
| 81 | + rewritten: 0, |
| 82 | + skipped: 1, |
| 83 | + failed: 0, |
| 84 | + servedInvalid: servedInvalid ? 1 : 0, |
| 85 | + rows: [servedInvalid ? VIEW_ROW : { ...VIEW_ROW, servedInvalid: undefined }], |
| 86 | + decisionModeReview: [], |
| 87 | + viewAllFieldsReview: [], |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +const migrateStoredMetadata = vi.fn(); |
| 92 | +let written: string[]; |
| 93 | +let stdout: ReturnType<typeof vi.spyOn>; |
| 94 | +let stderr: ReturnType<typeof vi.spyOn>; |
| 95 | +let log: ReturnType<typeof vi.spyOn>; |
| 96 | + |
| 97 | +beforeEach(() => { |
| 98 | + migrateStoredMetadata.mockReset(); |
| 99 | + boot.mockReset(); |
| 100 | + boot.mockResolvedValue({ |
| 101 | + kernel: { getService: (name: string) => (name === 'protocol' ? { migrateStoredMetadata } : undefined) }, |
| 102 | + dbLabel: 'file:test.db', |
| 103 | + tableAbsent: () => false, |
| 104 | + shutdown: vi.fn(async () => {}), |
| 105 | + }); |
| 106 | + written = []; |
| 107 | + // `emitJson` awaits the write's DRAIN callback, so the double invokes it. |
| 108 | + stdout = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, enc?: unknown, cb?: unknown) => { |
| 109 | + written.push(String(chunk)); |
| 110 | + const done = typeof enc === 'function' ? enc : cb; |
| 111 | + if (typeof done === 'function') done(); |
| 112 | + return true; |
| 113 | + }) as typeof process.stdout.write); |
| 114 | + stderr = vi.spyOn(process.stderr, 'write').mockImplementation((() => true) as typeof process.stderr.write); |
| 115 | + log = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 116 | +}); |
| 117 | +afterEach(() => { |
| 118 | + stdout.mockRestore(); |
| 119 | + stderr.mockRestore(); |
| 120 | + log.mockRestore(); |
| 121 | +}); |
| 122 | + |
| 123 | +/** Run the command over `report`; the printed text, colour stripped, and the exit code it raised. */ |
| 124 | +async function run(argv: string[], report: ReturnType<typeof reportOf>) { |
| 125 | + migrateStoredMetadata.mockResolvedValue(report); |
| 126 | + let exit: number | undefined; |
| 127 | + try { |
| 128 | + await MigrateMeta.run(argv, { root: CLI_ROOT }); |
| 129 | + } catch (error: any) { |
| 130 | + exit = error?.oclif?.exit; |
| 131 | + if (exit === undefined) throw error; |
| 132 | + } |
| 133 | + const text = log.mock.calls.map((call: unknown[]) => stripVTControlCharacters(call.map(String).join(' '))).join('\n'); |
| 134 | + return { exit, text }; |
| 135 | +} |
| 136 | + |
| 137 | +describe('os migrate meta --stored — a row the runtime serves invalid makes the run non-green (#38923)', () => { |
| 138 | + it('exits 1, names the row by id with its fix, and says why the run is red', async () => { |
| 139 | + const { exit, text } = await run(['--stored'], reportOf(true)); |
| 140 | + |
| 141 | + expect(exit).toBe(1); |
| 142 | + expect(text).toContain('view/lead_grid [env-wide, package=app.crm] id=r_7 — list.bulkActions:'); |
| 143 | + expect(text).toContain('fix: correct the row (sys_metadata id r_7)'); |
| 144 | + expect(text).toContain('1 stored row(s) are served INVALID by the runtime on protocol 19.0.0'); |
| 145 | + expect(text).not.toMatch(/already on protocol/); |
| 146 | + }, RUN_TIMEOUT); |
| 147 | + |
| 148 | + it('--json adds `servedInvalid` beside `clean: false`, and the per-row verdict', async () => { |
| 149 | + const { exit } = await run(['--stored', '--json'], reportOf(true)); |
| 150 | + |
| 151 | + expect(exit).toBe(1); |
| 152 | + const out = JSON.parse(written.join('')); |
| 153 | + expect(out).toMatchObject({ clean: false, servedInvalid: 1, skipped: 1, pending: 0, failed: 0 }); |
| 154 | + expect(out.rows[0].servedInvalid).toEqual(VIEW_ROW.servedInvalid); |
| 155 | + }, RUN_TIMEOUT); |
| 156 | + |
| 157 | + it('CONTROL — the same skipped row served valid leaves the run green', async () => { |
| 158 | + const { exit } = await run(['--stored', '--json'], reportOf(false)); |
| 159 | + |
| 160 | + expect(exit).toBeUndefined(); |
| 161 | + expect(JSON.parse(written.join(''))).toMatchObject({ clean: true, servedInvalid: 0 }); |
| 162 | + }, RUN_TIMEOUT); |
| 163 | +}); |
0 commit comments