|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30626] The `os migrate` value walks on a real engine (`ObjectQL` + sqlite |
| 5 | + * `SqlDriver`) while a module package that extends the walked object is |
| 6 | + * disabled. |
| 7 | + * |
| 8 | + * The walks scope by the served fold, which withholds a disabled package's |
| 9 | + * extension fields (#30304) while the storage fold keeps their columns and |
| 10 | + * values. Pinned: each walk's report names every in-scope column it skipped |
| 11 | + * that way (object, field, package id) and its plan prints it; with the |
| 12 | + * package enabled the same column is read and nothing is named. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 16 | +import { ObjectQL } from '@objectstack/objectql'; |
| 17 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 18 | +import { cleanBlankSingleValues, formatBlankSingleValuesReport } from './blank-single-values.js'; |
| 19 | +import { cleanBlankMultiValues, formatBlankMultiValuesReport } from './blank-multi-values.js'; |
| 20 | +import { migrateOptionCodes, formatOptionCodesReport } from './option-codes.js'; |
| 21 | +import { migrateTextScalars, formatTextScalarsReport } from './text-scalars.js'; |
| 22 | +import { withheldColumns, formatWithheldColumns } from '../../utils/migrate-withheld-columns.js'; |
| 23 | + |
| 24 | +const APP_PKG = 'com.qa.wx.app'; |
| 25 | +const MODULE_PKG = 'com.qa.wx.loyalty'; |
| 26 | +const OBJECT = 'wx_account'; |
| 27 | +const OPTS = [{ value: 'gold', label: 'Gold' }, { value: 'silver', label: 'Silver' }]; |
| 28 | + |
| 29 | +const manifest = (id: string, type: string) => |
| 30 | + ({ id, name: id, version: '1.0.0', type, namespace: 'wx', ...(type === 'module' ? { dependencies: { [APP_PKG]: '^1.0.0' } } : {}) }) as any; |
| 31 | +const OWNER = { |
| 32 | + name: OBJECT, label: 'Account', systemFields: false, |
| 33 | + fields: { |
| 34 | + id: { name: 'id', type: 'text', primaryKey: true }, |
| 35 | + name: { name: 'name', type: 'text' }, |
| 36 | + }, |
| 37 | +}; |
| 38 | +/** A column for each walk to rewrite, each added only by the module's extension. */ |
| 39 | +const EXTENSION = { |
| 40 | + name: OBJECT, |
| 41 | + fields: { |
| 42 | + wx_tier: { name: 'wx_tier', type: 'select', options: OPTS }, |
| 43 | + wx_level: { name: 'wx_level', type: 'select', options: [{ value: '3', label: 'Three' }] }, |
| 44 | + wx_tags: { name: 'wx_tags', type: 'multiselect', options: OPTS }, |
| 45 | + wx_note: { name: 'wx_note', type: 'text' }, |
| 46 | + }, |
| 47 | +}; |
| 48 | + |
| 49 | +const silent = { info: () => {}, warn: () => {} }; |
| 50 | +const live: ObjectQL[] = []; |
| 51 | +afterEach(async () => { |
| 52 | + while (live.length) { |
| 53 | + try { await live.pop()?.destroy(); } catch { /* noop */ } |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +async function boot() { |
| 58 | + const engine = new ObjectQL(); |
| 59 | + live.push(engine); |
| 60 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 61 | + engine.registerDriver(driver, true); |
| 62 | + await engine.init(); |
| 63 | + engine.registry.installPackage(manifest(APP_PKG, 'app'), 'code'); |
| 64 | + engine.registry.installPackage(manifest(MODULE_PKG, 'module'), 'code'); |
| 65 | + engine.registry.registerObject(OWNER as any, APP_PKG, 'wx', 'own'); |
| 66 | + engine.registry.registerObject(EXTENSION as any, MODULE_PKG, 'wx', 'extend'); |
| 67 | + await engine.syncSchemas(); |
| 68 | + // The legacy values each walk would rewrite, written below the engine's door. |
| 69 | + await driver.create(OBJECT, { id: 'r1', name: 'A', wx_tier: '', wx_level: 3, wx_tags: [], wx_note: 7 }); |
| 70 | + return engine; |
| 71 | +} |
| 72 | + |
| 73 | +/** `column`: the one this walk rewrites; `inScope`: every extension column its own rule covers. */ |
| 74 | +const WALKS = [ |
| 75 | + { label: 'blank-single-values', column: 'wx_tier', inScope: ['wx_level', 'wx_tier'], run: cleanBlankSingleValues, format: formatBlankSingleValuesReport }, |
| 76 | + { label: 'blank-multi-values', column: 'wx_tags', inScope: ['wx_tags'], run: cleanBlankMultiValues, format: formatBlankMultiValuesReport }, |
| 77 | + { label: 'option-codes', column: 'wx_level', inScope: ['wx_level', 'wx_tags', 'wx_tier'], run: migrateOptionCodes, format: formatOptionCodesReport }, |
| 78 | + { label: 'text-scalars', column: 'wx_note', inScope: ['wx_note'], run: migrateTextScalars, format: formatTextScalarsReport }, |
| 79 | +] as const; |
| 80 | + |
| 81 | +describe('os migrate value walks: a disabled module\'s extension columns are named (#30626)', () => { |
| 82 | + let engine: ObjectQL; |
| 83 | + beforeEach(async () => { engine = await boot(); }); |
| 84 | + |
| 85 | + for (const walk of WALKS) { |
| 86 | + it(`${walk.label}: the control — enabled, the column is read and nothing is named`, async () => { |
| 87 | + const report: any = await walk.run(engine as any, silent, {}); |
| 88 | + expect(report.columns.map((c: any) => `${c.object}.${c.field}`)).toContain(`${OBJECT}.${walk.column}`); |
| 89 | + expect(report.withheldColumns).toEqual([]); |
| 90 | + expect(walk.format(report).join('\n')).not.toContain(`(package ${MODULE_PKG})`); |
| 91 | + }); |
| 92 | + |
| 93 | + it(`${walk.label}: disabled, the plan names each skipped in-scope column with its package`, async () => { |
| 94 | + engine.registry.disablePackage(MODULE_PKG); |
| 95 | + const report: any = await walk.run(engine as any, silent, {}); |
| 96 | + expect(report.columns).toEqual([]); |
| 97 | + expect(report.withheldColumns).toEqual(walk.inScope.map((field) => ({ object: OBJECT, field, packageId: MODULE_PKG }))); |
| 98 | + expect(walk.format(report)).toContain(` • ${OBJECT}.${walk.column} (package ${MODULE_PKG})`); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + it('value-shapes (no column rule): every withheld field of the object is named', async () => { |
| 103 | + engine.registry.disablePackage(MODULE_PKG); |
| 104 | + const named = withheldColumns(engine as any, undefined); |
| 105 | + expect(named.map((c) => c.field)).toEqual(['wx_level', 'wx_note', 'wx_tags', 'wx_tier']); |
| 106 | + expect(named.every((c) => c.object === OBJECT && c.packageId === MODULE_PKG)).toBe(true); |
| 107 | + expect(formatWithheldColumns(named)).toHaveLength(5); |
| 108 | + }); |
| 109 | +}); |
0 commit comments