|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os migrate plan` — what its human face says about the database it read. |
| 5 | + * |
| 6 | + * A plan boots read-only (schema DDL deferred, no file brought into |
| 7 | + * existence), but the SQL driver's connect still switches a SQLite file to the |
| 8 | + * journal mode it asks for, which rewrites the file header and no row. So no |
| 9 | + * line a plan prints says that it wrote nothing: the lines that vouch for the |
| 10 | + * database claim no row or table, and on a SQLite target each is followed by |
| 11 | + * the note naming the switch. Three texts make that claim: |
| 12 | + * |
| 13 | + * - the unmanaged-table section's closing line (`renderUnmanagedTables`); |
| 14 | + * - the declaration boot's write-guard note when a plugin's contract write |
| 15 | + * was refused ("— a plan writes no row or table"); |
| 16 | + * - the same note when a raw command was forwarded ("If those commands only |
| 17 | + * read, no row or table was written"). |
| 18 | + * |
| 19 | + * The two guard notes are the REAL guard's text: a `createDeclarationBootWriteGuard` |
| 20 | + * is armed over a fake driver, made to refuse one write or forward one raw |
| 21 | + * read, and disarmed. Only the boot, the occupancy probe and the unmanaged-table |
| 22 | + * READ are replaced; the command's control flow and the section's renderer run |
| 23 | + * for real. The `--json` document carries the same lines and no note. |
| 24 | + */ |
| 25 | + |
| 26 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 27 | +import { dirname, resolve } from 'node:path'; |
| 28 | +import { fileURLToPath } from 'node:url'; |
| 29 | +import { stripVTControlCharacters } from 'node:util'; |
| 30 | +import MigratePlan from './plan.js'; |
| 31 | +import { bootSchemaStack } from '../../utils/schema-migrate.js'; |
| 32 | +import { probeMigrationTarget } from '../../utils/migrate-occupancy-gate.js'; |
| 33 | +import { collectUnmanagedTables, type UnmanagedTablesReport } from '../../utils/unmanaged-tables.js'; |
| 34 | +import { createDeclarationBootWriteGuard } from '../../utils/schema-migration-plugins.js'; |
| 35 | +import { JOURNAL_MODE_SWITCH_NOTE } from '../../utils/sqlite-occupancy.js'; |
| 36 | + |
| 37 | +vi.mock('../../utils/schema-migrate.js', async (importOriginal) => ({ |
| 38 | + ...(await importOriginal<typeof import('../../utils/schema-migrate.js')>()), |
| 39 | + bootSchemaStack: vi.fn(), |
| 40 | +})); |
| 41 | +vi.mock('../../utils/migrate-occupancy-gate.js', () => ({ probeMigrationTarget: vi.fn() })); |
| 42 | +// The sweep's READ is replaced; its renderer is the real one. |
| 43 | +vi.mock('../../utils/unmanaged-tables.js', async (importOriginal) => ({ |
| 44 | + ...(await importOriginal<typeof import('../../utils/unmanaged-tables.js')>()), |
| 45 | + collectUnmanagedTables: vi.fn(), |
| 46 | +})); |
| 47 | +// `run()` ends the process when the plan does (#13027); here it returns instead. |
| 48 | +vi.mock('../../utils/one-shot-exit.js', () => ({ exitOneShotCommand: vi.fn(async () => undefined) })); |
| 49 | + |
| 50 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 51 | +const CLI_ROOT = resolve(HERE, '..', '..', '..'); |
| 52 | +/** oclif builds its whole command table on the first `run()` in a process. */ |
| 53 | +const RUN_TIMEOUT = 60_000; |
| 54 | + |
| 55 | +const SQLITE_CONFIG = { client: 'better-sqlite3', connection: { filename: '/srv/app/.objectstack/data/objectstack.db' } }; |
| 56 | +const POSTGRES_CONFIG = { client: 'pg', connection: 'postgres://db.example.com:5432/app' }; |
| 57 | + |
| 58 | +const UNMANAGED_LINE = |
| 59 | + 'They are reported for information only — nothing here drops them, and a plan writes no row or table.'; |
| 60 | +const ONE_UNMANAGED: UnmanagedTablesReport = { |
| 61 | + status: 'read', prefixes: ['sys_'], physicalTables: 3, tables: [{ table: 'sys_legacy_widget' }], |
| 62 | +}; |
| 63 | +const NONE_UNMANAGED: UnmanagedTablesReport = { status: 'read', prefixes: ['sys_'], physicalTables: 3, tables: [] }; |
| 64 | + |
| 65 | +/** A driver with its members on the PROTOTYPE, as every real driver's are. */ |
| 66 | +class FakeDriver { |
| 67 | + async create(_object: string, _data: Record<string, unknown>): Promise<Record<string, unknown>> { return {}; } |
| 68 | + async execute(_command: unknown): Promise<unknown> { return []; } |
| 69 | +} |
| 70 | + |
| 71 | +/** The real guard's note after `act` ran against a driver it armed. */ |
| 72 | +async function guardNote(act: (driver: FakeDriver) => Promise<unknown>): Promise<string> { |
| 73 | + const guard = createDeclarationBootWriteGuard(); |
| 74 | + const driver = new FakeDriver(); |
| 75 | + await (guard.plugin as { init(ctx: unknown): Promise<void> }).init({ |
| 76 | + getServices: () => new Map<string, unknown>([['driver.default', driver]]), |
| 77 | + }); |
| 78 | + await act(driver); |
| 79 | + const note = guard.disarm(); |
| 80 | + expect(note).not.toBeNull(); |
| 81 | + return note as string; |
| 82 | +} |
| 83 | + |
| 84 | +let warn: ReturnType<typeof vi.spyOn>; |
| 85 | +beforeEach(() => { |
| 86 | + // The guard warns once per refused or forwarded call; not under test here. |
| 87 | + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 88 | + vi.mocked(probeMigrationTarget).mockResolvedValue({ status: 'free' } as never); |
| 89 | +}); |
| 90 | +afterEach(() => { |
| 91 | + warn.mockRestore(); |
| 92 | + vi.mocked(bootSchemaStack).mockReset(); |
| 93 | + vi.mocked(collectUnmanagedTables).mockReset(); |
| 94 | +}); |
| 95 | + |
| 96 | +/** Boot onto a primary driver with `config`, composing `notes`. */ |
| 97 | +function wireBoot(config: Record<string, unknown>, notes: string[], unmanaged: UnmanagedTablesReport): void { |
| 98 | + vi.mocked(bootSchemaStack).mockResolvedValue({ |
| 99 | + // The primary datasource's driver, as `SchemaStack.driver` carries it. |
| 100 | + driver: { config }, |
| 101 | + drivers: [], |
| 102 | + dbLabel: 'test database', |
| 103 | + dbSource: { kind: 'flag' }, |
| 104 | + schemaSync: 'deferred', |
| 105 | + telemetryDatabase: null, |
| 106 | + managedTableCount: 3, |
| 107 | + pendingSchemaWork: [], |
| 108 | + detectManagedDrift: async () => [], |
| 109 | + allObjects: () => [], |
| 110 | + composition: { |
| 111 | + plugins: [], |
| 112 | + hostConfigPath: '/srv/app/objectstack.config.ts', |
| 113 | + hostConfigLoaded: true, |
| 114 | + hostConfigError: null, |
| 115 | + servedBoot: { mirrored: false, reason: 'not-composed' }, |
| 116 | + notes, |
| 117 | + coverage: null, |
| 118 | + }, |
| 119 | + shutdown: vi.fn(async () => {}), |
| 120 | + } as never); |
| 121 | + vi.mocked(collectUnmanagedTables).mockResolvedValue(unmanaged); |
| 122 | +} |
| 123 | + |
| 124 | +/** One human-mode plan; every line `console.log` received, colour stripped. */ |
| 125 | +async function runHuman(): Promise<string[]> { |
| 126 | + const log = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 127 | + try { |
| 128 | + await MigratePlan.run([], { root: CLI_ROOT }); |
| 129 | + return log.mock.calls |
| 130 | + .map((c: unknown[]) => stripVTControlCharacters(c.map(String).join(' '))) |
| 131 | + .join('\n') |
| 132 | + .split('\n'); |
| 133 | + } finally { |
| 134 | + log.mockRestore(); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** One `--json` plan; the document it emitted. */ |
| 139 | +async function runJson(): Promise<Record<string, any>> { |
| 140 | + const chunks: string[] = []; |
| 141 | + // `emitJson` awaits the write's DRAIN callback, so the double invokes it. |
| 142 | + const stdout = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, ...rest: unknown[]) => { |
| 143 | + chunks.push(String(chunk)); |
| 144 | + const done = rest.find((a) => typeof a === 'function') as ((e?: Error | null) => void) | undefined; |
| 145 | + done?.(null); |
| 146 | + return true; |
| 147 | + }) as typeof process.stdout.write); |
| 148 | + try { |
| 149 | + await MigratePlan.run(['--json'], { root: CLI_ROOT }); |
| 150 | + } finally { |
| 151 | + stdout.mockRestore(); |
| 152 | + } |
| 153 | + return JSON.parse(chunks.join('')) as Record<string, any>; |
| 154 | +} |
| 155 | + |
| 156 | +interface Text { |
| 157 | + name: string; |
| 158 | + /** The composed notes and the sweep's report this case boots with. */ |
| 159 | + arrange: () => Promise<{ notes: string[]; unmanaged: UnmanagedTablesReport }>; |
| 160 | + /** The printed line that carries the claim, without its print indent. */ |
| 161 | + line: (notes: string[]) => string; |
| 162 | + /** The claim as the text spells it. */ |
| 163 | + claim: string; |
| 164 | +} |
| 165 | + |
| 166 | +const TEXTS: Text[] = [ |
| 167 | + { |
| 168 | + name: 'the unmanaged-table section\'s closing line', |
| 169 | + arrange: async () => ({ notes: [], unmanaged: ONE_UNMANAGED }), |
| 170 | + line: () => UNMANAGED_LINE, |
| 171 | + claim: 'a plan writes no row or table', |
| 172 | + }, |
| 173 | + { |
| 174 | + name: 'the write guard\'s note on a refused contract write', |
| 175 | + arrange: async () => ({ |
| 176 | + notes: [await guardNote((d) => d.create('sys_metadata', { id: 'probe' }))], |
| 177 | + unmanaged: NONE_UNMANAGED, |
| 178 | + }), |
| 179 | + line: (notes) => notes[0]!, |
| 180 | + claim: 'Refused 1 write(s) during the declaration boot — a plan writes no row or table: create() on sys_metadata.', |
| 181 | + }, |
| 182 | + { |
| 183 | + name: 'the write guard\'s note on a forwarded raw command', |
| 184 | + arrange: async () => ({ |
| 185 | + notes: [await guardNote((d) => d.execute('SELECT count(*) FROM sys_metadata'))], |
| 186 | + unmanaged: NONE_UNMANAGED, |
| 187 | + }), |
| 188 | + line: (notes) => notes[0]!, |
| 189 | + claim: 'If those commands only read, no row or table was written; if one wrote, it landed.', |
| 190 | + }, |
| 191 | +]; |
| 192 | + |
| 193 | +describe('os migrate plan — the human face names no write it cannot vouch for', () => { |
| 194 | + for (const text of TEXTS) { |
| 195 | + it(`${text.name}: on a SQLite file it claims no row or table and is followed by the journal-mode note`, async () => { |
| 196 | + const { notes, unmanaged } = await text.arrange(); |
| 197 | + const expected = text.line(notes); |
| 198 | + expect(expected).toContain(text.claim); |
| 199 | + expect(expected).not.toContain('nothing was written'); |
| 200 | + expect(expected).not.toContain('writes nothing'); |
| 201 | + |
| 202 | + wireBoot(SQLITE_CONFIG, notes, unmanaged); |
| 203 | + const lines = await runHuman(); |
| 204 | + const printed = lines.filter((l) => l.includes(text.claim)); |
| 205 | + expect(printed, lines.join('\n')).toEqual([` ${expected} ${JOURNAL_MODE_SWITCH_NOTE}`]); |
| 206 | + // Once, after the claim — no other line of this plan carries it. |
| 207 | + expect(lines.join('\n').split(JOURNAL_MODE_SWITCH_NOTE)).toHaveLength(2); |
| 208 | + }, RUN_TIMEOUT); |
| 209 | + |
| 210 | + it(`${text.name}: on a target with no file header (PostgreSQL) it claims no row or table and names no switch`, async () => { |
| 211 | + const { notes, unmanaged } = await text.arrange(); |
| 212 | + const expected = text.line(notes); |
| 213 | + |
| 214 | + wireBoot(POSTGRES_CONFIG, notes, unmanaged); |
| 215 | + const lines = await runHuman(); |
| 216 | + const printed = lines.filter((l) => l.includes(text.claim)); |
| 217 | + expect(printed, lines.join('\n')).toEqual([` ${expected}`]); |
| 218 | + expect(lines.join('\n')).not.toContain(JOURNAL_MODE_SWITCH_NOTE); |
| 219 | + }, RUN_TIMEOUT); |
| 220 | + } |
| 221 | + |
| 222 | + it('--json carries the guard\'s note as it is, with no journal-mode note, on a SQLite file', async () => { |
| 223 | + const note = await guardNote((d) => d.create('sys_metadata', { id: 'probe' })); |
| 224 | + wireBoot(SQLITE_CONFIG, [note], ONE_UNMANAGED); |
| 225 | + const payload = await runJson(); |
| 226 | + expect(payload.composition.notes).toEqual([note]); |
| 227 | + expect(payload.unmanagedTables).toEqual(ONE_UNMANAGED); |
| 228 | + expect(JSON.stringify(payload)).not.toContain(JOURNAL_MODE_SWITCH_NOTE); |
| 229 | + }, RUN_TIMEOUT); |
| 230 | +}); |
0 commit comments