|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20432] What the CLI does with the driver's new report-only drift op, |
| 5 | + * `unbuildable_index`: a declared index that can never be built, because a key |
| 6 | + * column is not a field of the object or is a virtual `formula` field. |
| 7 | + * |
| 8 | + * ## Why this lives in `packages/cli` |
| 9 | + * |
| 10 | + * The driver owns the entry. The CLI owns the three places it lands: |
| 11 | + * |
| 12 | + * - `os migrate plan` renders it through `renderPlan` / `driftTarget`, which |
| 13 | + * read `category`, `op.indexName`, `op.type` and `message` generically. |
| 14 | + * This file proves that the new op needs no CLI source change to be shown. |
| 15 | + * - `os migrate apply` hands it to `applyMigrationEntries`, which reports it |
| 16 | + * `skipped`: there is no reconciler arm, and none can exist. |
| 17 | + * - The artifact-pinned boot gate applies every non-destructive entry and |
| 18 | + * refuses the boot only on `category === 'destructive'`. A report-only |
| 19 | + * entry must WARN and let the boot continue. Refusing would take down |
| 20 | + * every deployment carrying a misspelt index column at `kernel:ready`, |
| 21 | + * over a declaration that no DDL can repair. That is the same asymmetry |
| 22 | + * `artifact-boot-migration.report-only-drift.test.ts` pins for |
| 23 | + * `manual_column_type_change`. |
| 24 | + * |
| 25 | + * Every entry here comes from the REAL driver (an in-memory SQLite |
| 26 | + * `SqlDriver`) and goes through the REAL gate and renderer. A hand-stamped |
| 27 | + * entry would stay green on the day the driver starts emitting the op as |
| 28 | + * `destructive`, because nothing would connect the two. |
| 29 | + * |
| 30 | + * ⚠️ `@objectstack/driver-sql` resolves through its `exports` to its **dist** |
| 31 | + * (no `resolve.alias` for it in this package, deliberately), so this file |
| 32 | + * reads the BUILT driver. A stale `dist/` makes it a verdict about build |
| 33 | + * state. The value import also puts it in the `integration` tier |
| 34 | + * (`vitest-tiers.ts`, KERNEL). |
| 35 | + */ |
| 36 | + |
| 37 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 38 | +import { SqlDriver, buildIndexName } from '@objectstack/driver-sql'; |
| 39 | +import { runArtifactBootMigrationGate } from './artifact-boot-migration.js'; |
| 40 | +import { driftTarget, groupByCategory, renderPlan, type SqlDriverLike } from './schema-migrate.js'; |
| 41 | + |
| 42 | +const T = 'os20432_boot'; |
| 43 | +const INDEX = buildIndexName(T, ['statsu'], true); |
| 44 | + |
| 45 | +/** A table whose only real column is `status`, declaring a UNIQUE over the misspelling `statsu`. */ |
| 46 | +const OBJECT = { |
| 47 | + name: T, |
| 48 | + tenancy: { enabled: false }, |
| 49 | + fields: { status: { type: 'text', maxLength: 64 } }, |
| 50 | + indexes: [{ fields: ['statsu'], unique: true as const }], |
| 51 | +}; |
| 52 | + |
| 53 | +async function syncedDriver(): Promise<SqlDriver> { |
| 54 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 55 | + // Quiet: the driver's own error line is pinned in driver-sql. This file is about the CLI's handling. |
| 56 | + (driver as any).logger = { debug() {}, info() {}, warn() {}, error() {} }; |
| 57 | + await driver.initObjects([OBJECT]); |
| 58 | + return driver; |
| 59 | +} |
| 60 | + |
| 61 | +describe('the unbuildable_index drift op through the CLI (#20432)', () => { |
| 62 | + let driver: SqlDriver | undefined; |
| 63 | + afterEach(async () => { |
| 64 | + vi.restoreAllMocks(); |
| 65 | + await driver?.disconnect().catch(() => {}); |
| 66 | + driver = undefined; |
| 67 | + }); |
| 68 | + |
| 69 | + it('the fixture is real: the driver reports exactly one unbuildable_index entry, needs_confirm', async () => { |
| 70 | + driver = await syncedDriver(); |
| 71 | + const drift = await driver.detectManagedDrift(); |
| 72 | + expect(drift.map((d) => d.op.type)).toEqual(['unbuildable_index']); |
| 73 | + expect(drift[0]).toMatchObject({ category: 'needs_confirm', op: { indexName: INDEX, unique: true } }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('the artifact boot gate warns about it and does NOT refuse the boot', async () => { |
| 77 | + driver = await syncedDriver(); |
| 78 | + const info: string[] = []; |
| 79 | + const warn: string[] = []; |
| 80 | + |
| 81 | + // The gate's two members, delegated to the REAL driver. `bootSchemaStack` |
| 82 | + // reaches the driver by duck type, and `SqlDriver` keeps `config` |
| 83 | + // protected, so the class itself is not assignable to `SqlDriverLike`. |
| 84 | + const real = driver; |
| 85 | + const gateDriver: SqlDriverLike = { |
| 86 | + detectManagedDrift: () => real.detectManagedDrift(), |
| 87 | + applyMigrationEntries: (entries, opts) => real.applyMigrationEntries(entries, opts), |
| 88 | + }; |
| 89 | + const verdict = await runArtifactBootMigrationGate({ |
| 90 | + driver: gateDriver, |
| 91 | + artifactDisplay: 'https://artifacts.example.com/app.json', |
| 92 | + info: (m) => info.push(m), |
| 93 | + warn: (m) => warn.push(m), |
| 94 | + }); |
| 95 | + |
| 96 | + expect(verdict.ok).toBe(true); |
| 97 | + expect(verdict.refusal).toBeUndefined(); |
| 98 | + expect(verdict.destructive).toEqual([]); |
| 99 | + // Handed to the driver, which declined it: skipped, never "migrated". |
| 100 | + expect(verdict.applied).toEqual([]); |
| 101 | + expect(verdict.skipped.map((d) => d.op.type)).toEqual(['unbuildable_index']); |
| 102 | + expect(info).toEqual([]); |
| 103 | + // …and the skip is not silent: one warn, carrying the driver's message. |
| 104 | + expect(warn).toHaveLength(1); |
| 105 | + expect(warn[0]).toContain(T); |
| 106 | + expect(warn[0]).toContain(INDEX); |
| 107 | + }); |
| 108 | + |
| 109 | + it('os migrate plan renders it with no CLI change: grouped needs_confirm, targeted by index name, tagged by op', async () => { |
| 110 | + driver = await syncedDriver(); |
| 111 | + const drift = await driver.detectManagedDrift(); |
| 112 | + |
| 113 | + expect(groupByCategory(drift).needs_confirm).toEqual(drift); |
| 114 | + expect(driftTarget(drift[0]!)).toBe(`${T} [${INDEX}]`); |
| 115 | + |
| 116 | + const lines: string[] = []; |
| 117 | + vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => { |
| 118 | + lines.push(args.map(String).join(' ')); |
| 119 | + }); |
| 120 | + renderPlan(drift); |
| 121 | + const out = lines.join('\n'); |
| 122 | + expect(out).toContain(`${T} [${INDEX}]`); |
| 123 | + expect(out).toContain('[unbuildable_index]'); |
| 124 | + expect(out).toContain(drift[0]!.message); |
| 125 | + }); |
| 126 | +}); |
0 commit comments