|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #23816 — `os migrate organization-ownership` declares, at both of its boots, |
| 5 | + * that a datasource whose transport cannot defer DDL (the Turso REMOTE |
| 6 | + * transport) boots with schema sync skipped instead |
| 7 | + * (`deferredDdlUnsupported: 'skip-schema-sync'`), and the apply reads the |
| 8 | + * ledger's presence from the catalog when its boot measured no table absent. |
| 9 | + * |
| 10 | + * Pinned on the command's own boot call, with `bootSchemaStack` stubbed: the |
| 11 | + * posture itself is `schema-migrate.remote-posture.test.ts`'s, and the |
| 12 | + * end-to-end run on a real `sqld` is |
| 13 | + * `organization-ownership.remote-face.integration.test.ts` (opt-in, through |
| 14 | + * `OS_TEST_SQLD_BIN`). Without these two opt-ins the plan stops at its boot on |
| 15 | + * every hosted tenant again, and nothing else in CI would say so. |
| 16 | + */ |
| 17 | + |
| 18 | +import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 19 | +import { mkdtempSync, writeFileSync } from 'node:fs'; |
| 20 | +import { tmpdir } from 'node:os'; |
| 21 | +import { join } from 'node:path'; |
| 22 | +import { fileURLToPath } from 'node:url'; |
| 23 | + |
| 24 | +const boot = vi.hoisted(() => vi.fn(async (_opts: Record<string, unknown>): Promise<unknown> => { |
| 25 | + throw new Error('the database was opened'); |
| 26 | +})); |
| 27 | +vi.mock('../../utils/schema-migrate.js', () => ({ bootSchemaStack: boot })); |
| 28 | +// The occupancy gate is the siblings' stub (`organization-ownership.apply-flags.test.ts`). |
| 29 | +vi.mock('../../utils/migrate-occupancy-gate.js', () => ({ |
| 30 | + OCCUPANCY_STOP_HINT: 'occupancy hint', |
| 31 | + probeMigrationTarget: vi.fn(async () => ({ status: 'free' })), |
| 32 | +})); |
| 33 | + |
| 34 | +type Command = (typeof import('./organization-ownership.js'))['default']; |
| 35 | +let MigrateOrganizationOwnership: Command; |
| 36 | + |
| 37 | +beforeAll(async () => { |
| 38 | + ({ default: MigrateOrganizationOwnership } = await import('./organization-ownership.js')); |
| 39 | +}, 60_000); |
| 40 | + |
| 41 | +const CASE_TIMEOUT_MS = 60_000; |
| 42 | +const CLI_ROOT = fileURLToPath(new URL('../../..', import.meta.url)); |
| 43 | + |
| 44 | +const PLAN_DIR = mkdtempSync(join(tmpdir(), 'os-23816-posture-')); |
| 45 | +const PLAN_FILE = join(PLAN_DIR, 'plan.json'); |
| 46 | +writeFileSync(PLAN_FILE, JSON.stringify({ |
| 47 | + kind: 'organization-ownership-plan', |
| 48 | + ceremony: 'ADR-0131 D10', |
| 49 | + generatedAt: '2026-10-16T00:00:00Z', |
| 50 | + database: 'libsql://tenant.example.turso.io', |
| 51 | + inventoryDigest: 'f'.repeat(64), |
| 52 | + posture: 'single', |
| 53 | +})); |
| 54 | + |
| 55 | +async function runJson(argv: string[]): Promise<{ payload: Record<string, unknown>; exitCode: unknown }> { |
| 56 | + const chunks: string[] = []; |
| 57 | + const savedExit = process.exitCode; |
| 58 | + vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, ...rest: unknown[]) => { |
| 59 | + chunks.push(String(chunk)); |
| 60 | + const cb = rest.find((a) => typeof a === 'function') as (() => void) | undefined; |
| 61 | + if (cb) cb(); |
| 62 | + return true; |
| 63 | + }) as typeof process.stdout.write); |
| 64 | + vi.spyOn(process.stderr, 'write').mockImplementation((() => true) as typeof process.stderr.write); |
| 65 | + try { |
| 66 | + await MigrateOrganizationOwnership.run(argv, { root: CLI_ROOT }); |
| 67 | + } catch (error) { |
| 68 | + if ((error as { oclif?: unknown }).oclif === undefined) throw error; |
| 69 | + } |
| 70 | + const exitCode = process.exitCode; |
| 71 | + process.exitCode = savedExit; |
| 72 | + vi.restoreAllMocks(); |
| 73 | + return { payload: JSON.parse(chunks.join('').trim()) as Record<string, unknown>, exitCode }; |
| 74 | +} |
| 75 | + |
| 76 | +afterEach(() => { |
| 77 | + boot.mockReset(); |
| 78 | + boot.mockImplementation(async () => { |
| 79 | + throw new Error('the database was opened'); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +const POSTURE = { deferSchemaDdl: true, deferredDdlUnsupported: 'skip-schema-sync' }; |
| 84 | + |
| 85 | +describe('both ceremony boots declare the skip-schema-sync posture', () => { |
| 86 | + it('the plan boot: deferred, read-only, and skip-schema-sync where deferral is refused', async () => { |
| 87 | + await runJson(['--json', '--out', join(PLAN_DIR, 'never-written.json')]); |
| 88 | + expect(boot).toHaveBeenCalledTimes(1); |
| 89 | + expect(boot.mock.calls[0]![0]).toEqual(expect.objectContaining({ ...POSTURE, readOnlyProbe: true })); |
| 90 | + }, CASE_TIMEOUT_MS); |
| 91 | + |
| 92 | + it('the apply boot: deferred, the served composition, and skip-schema-sync where deferral is refused', async () => { |
| 93 | + await runJson(['--apply', '--plan-file', PLAN_FILE, '--backup-taken', '--json', '--out', join(PLAN_DIR, 'never-report.json')]); |
| 94 | + expect(boot).toHaveBeenCalledTimes(1); |
| 95 | + expect(boot.mock.calls[0]![0]).toEqual(expect.objectContaining({ ...POSTURE, composeHostStack: true })); |
| 96 | + }, CASE_TIMEOUT_MS); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('the apply reads the ledger from the catalog when its boot skipped schema sync', () => { |
| 100 | + /** A booted stack as `bootSchemaStack` answers it, over a raw-SQL seam that answers `catalogRows`. */ |
| 101 | + function stackWith(schemaSync: 'skipped' | 'deferred', opts: { catalogRows: unknown[]; tableAbsent: boolean }) { |
| 102 | + const execute = vi.fn(async (sql: string) => (/pragma_table_info/.test(sql) ? opts.catalogRows : [])); |
| 103 | + return { |
| 104 | + execute, |
| 105 | + stack: { |
| 106 | + schemaSync, |
| 107 | + driver: { name: 'com.objectstack.driver.turso', dialectName: 'sqlite', config: { client: 'better-sqlite3' }, execute }, |
| 108 | + dbLabel: 'libsql://tenant.example.turso.io', |
| 109 | + tableAbsent: vi.fn(() => opts.tableAbsent), |
| 110 | + kernel: { getService: () => { throw new Error('no kernel in this pin'); } }, |
| 111 | + composition: { securityPlugin: false }, |
| 112 | + allObjects: () => [], |
| 113 | + shutdown: async () => {}, |
| 114 | + }, |
| 115 | + }; |
| 116 | + } |
| 117 | + const apply = () => runJson(['--apply', '--plan-file', PLAN_FILE, '--backup-taken', '--json', '--out', join(PLAN_DIR, 'never-report.json')]); |
| 118 | + |
| 119 | + it('skipped, and the catalog has no sys_migration: refused ledger-absent, whatever tableAbsent says', async () => { |
| 120 | + const { stack, execute } = stackWith('skipped', { catalogRows: [], tableAbsent: false }); |
| 121 | + boot.mockImplementation(async () => stack); |
| 122 | + const { payload } = await apply(); |
| 123 | + expect(payload).toMatchObject({ error: 'apply_refused', reason: 'ledger-absent', table: 'sys_migration' }); |
| 124 | + expect(execute).toHaveBeenCalledWith(expect.stringContaining('pragma_table_info'), ['sys_migration']); |
| 125 | + expect(stack.tableAbsent).not.toHaveBeenCalled(); |
| 126 | + }, CASE_TIMEOUT_MS); |
| 127 | + |
| 128 | + it('skipped, and the catalog has sys_migration: not refused for the ledger', async () => { |
| 129 | + const { stack } = stackWith('skipped', { catalogRows: [{ column_name: 'id' }], tableAbsent: true }); |
| 130 | + boot.mockImplementation(async () => stack); |
| 131 | + const { payload } = await apply(); |
| 132 | + expect(payload.reason).not.toBe('ledger-absent'); |
| 133 | + }, CASE_TIMEOUT_MS); |
| 134 | + |
| 135 | + it('the control: a deferred boot answers from tableAbsent, without a catalog read', async () => { |
| 136 | + const { stack, execute } = stackWith('deferred', { catalogRows: [{ column_name: 'id' }], tableAbsent: true }); |
| 137 | + boot.mockImplementation(async () => stack); |
| 138 | + const { payload } = await apply(); |
| 139 | + expect(payload).toMatchObject({ error: 'apply_refused', reason: 'ledger-absent' }); |
| 140 | + expect(execute).not.toHaveBeenCalledWith(expect.stringContaining('pragma_table_info'), ['sys_migration']); |
| 141 | + }, CASE_TIMEOUT_MS); |
| 142 | +}); |
0 commit comments