|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30377] A roll-up summary a package contributes BY EXTENSION, over a child |
| 5 | + * of an ENABLED package, is current again the moment its package is |
| 6 | + * re-enabled. |
| 7 | + * |
| 8 | + * While a package is disabled its `objectExtensions` fields are withheld from |
| 9 | + * the served object fold (#30304), so the roll-up index — built from that fold |
| 10 | + * — does not hold the package's summary, and the child writes made during the |
| 11 | + * disable recompute nothing. The child here belongs to the enabled app, so |
| 12 | + * those writes land; the stored summary column keeps the value it had when the |
| 13 | + * package was switched off. Re-enabling serves the field again, so without a |
| 14 | + * recompute it serves that stale value until the next write of a child. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 18 | +import { ObjectQL } from './engine.js'; |
| 19 | + |
| 20 | +function makeDriver() { |
| 21 | + const stores = new Map<string, Map<string, any>>(); |
| 22 | + const storeFor = (o: string) => { |
| 23 | + let s = stores.get(o); |
| 24 | + if (!s) { s = new Map(); stores.set(o, s); } |
| 25 | + return s; |
| 26 | + }; |
| 27 | + const checkOp = (value: any, cond: any): boolean => { |
| 28 | + if (cond === null || typeof cond !== 'object' || Array.isArray(cond) || cond instanceof Date) return value === cond; |
| 29 | + return Object.entries(cond).every(([op, target]: [string, any]) => { |
| 30 | + switch (op) { |
| 31 | + case '$eq': return value === target; |
| 32 | + case '$ne': return value !== target; |
| 33 | + case '$gt': return value > target; |
| 34 | + case '$gte': return value >= target; |
| 35 | + case '$lt': return value < target; |
| 36 | + case '$lte': return value <= target; |
| 37 | + case '$in': return Array.isArray(target) && target.includes(value); |
| 38 | + case '$nin': return Array.isArray(target) && !target.includes(value); |
| 39 | + default: return true; |
| 40 | + } |
| 41 | + }); |
| 42 | + }; |
| 43 | + const matches = (row: any, where: any): boolean => { |
| 44 | + if (!where || typeof where !== 'object') return true; |
| 45 | + return Object.entries(where).every(([k, v]: [string, any]) => { |
| 46 | + if (k === '$and') return (v as any[]).every((w) => matches(row, w)); |
| 47 | + if (k === '$or') return (v as any[]).some((w) => matches(row, w)); |
| 48 | + if (k === '$not') return !matches(row, v); |
| 49 | + return checkOp(row?.[k], v); |
| 50 | + }); |
| 51 | + }; |
| 52 | + let n = 0; |
| 53 | + /** Parent-row writes, by object, so a case can count them. */ |
| 54 | + const updates: Array<{ object: string; id: string; data: Record<string, unknown> }> = []; |
| 55 | + /** Ids whose next `update` throws, to drive a recompute failure. */ |
| 56 | + const failUpdateOf = new Set<string>(); |
| 57 | + const driver: any = { |
| 58 | + name: 'memory', version: '0.0.0', supports: {}, |
| 59 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 60 | + async find(object: string, ast: any) { |
| 61 | + const rows = Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where)); |
| 62 | + const page = typeof ast?.limit === 'number' ? rows.slice(0, ast.limit) : rows; |
| 63 | + return page.map((r) => ({ ...r })); |
| 64 | + }, |
| 65 | + async findOne(object: string, ast: any) { |
| 66 | + for (const r of storeFor(object).values()) if (matches(r, ast?.where)) return { ...r }; |
| 67 | + return null; |
| 68 | + }, |
| 69 | + async create(object: string, data: Record<string, unknown>) { |
| 70 | + n += 1; |
| 71 | + const id = (data.id as string) ?? `r_${n}`; |
| 72 | + const row = { ...data, id }; |
| 73 | + storeFor(object).set(id, row); |
| 74 | + return { ...row }; |
| 75 | + }, |
| 76 | + async update(object: string, id: string, data: Record<string, unknown>) { |
| 77 | + if (failUpdateOf.has(id)) throw new Error(`fake driver: update of ${id} refused`); |
| 78 | + updates.push({ object, id, data }); |
| 79 | + const s = storeFor(object); |
| 80 | + const row = { ...s.get(id), ...data, id }; |
| 81 | + s.set(id, row); |
| 82 | + return { ...row }; |
| 83 | + }, |
| 84 | + async updateMany(object: string, ast: any, data: Record<string, unknown>) { |
| 85 | + const s = storeFor(object); |
| 86 | + let count = 0; |
| 87 | + for (const [id, r] of Array.from(s.entries())) if (matches(r, ast?.where)) { s.set(id, { ...r, ...data, id }); count += 1; } |
| 88 | + return count; |
| 89 | + }, |
| 90 | + async delete(object: string, id: string) { return storeFor(object).delete(id); }, |
| 91 | + async deleteMany(object: string, ast: any) { |
| 92 | + const s = storeFor(object); |
| 93 | + let count = 0; |
| 94 | + for (const [id, r] of Array.from(s.entries())) if (matches(r, ast?.where)) { s.delete(id); count += 1; } |
| 95 | + return count; |
| 96 | + }, |
| 97 | + async count() { return 0; }, |
| 98 | + async bulkCreate(object: string, rows: Record<string, unknown>[]) { return Promise.all(rows.map((r) => this.create(object, r))); }, |
| 99 | + async bulkUpdate() { return []; }, async bulkDelete() {}, |
| 100 | + async beginTransaction() { return { __trx: true, commit: async () => {}, rollback: async () => {} }; }, |
| 101 | + async commit() {}, async rollback() {}, |
| 102 | + }; |
| 103 | + return { driver, storeFor, updates, failUpdateOf }; |
| 104 | +} |
| 105 | + |
| 106 | +const APP = 'com.qa.rollup.app'; |
| 107 | +const MODULE = 'com.qa.rollup.notes'; |
| 108 | +const ACCOUNT = 'qr_account'; |
| 109 | +const NOTE = 'qr_note'; |
| 110 | +const TOTAL = 'qr_note_total'; |
| 111 | + |
| 112 | +const manifest = (id: string, type: string) => |
| 113 | + ({ id, name: id, version: '1.0.0', type, namespace: 'qr', ...(type === 'module' ? { dependencies: { [APP]: '^1.0.0' } } : {}) }) as any; |
| 114 | + |
| 115 | +describe('a roll-up a disabled package contributes by extension, over an enabled package\'s child', () => { |
| 116 | + let engine: ObjectQL; |
| 117 | + let storeFor: ReturnType<typeof makeDriver>['storeFor']; |
| 118 | + let driver: ReturnType<typeof makeDriver>; |
| 119 | + let logger: Record<'debug' | 'info' | 'warn' | 'error', ReturnType<typeof vi.fn>> & Record<string, unknown>; |
| 120 | + |
| 121 | + beforeEach(async () => { |
| 122 | + logger = { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn(), trace: vi.fn(), fatal: vi.fn() }; |
| 123 | + logger.child = () => logger; |
| 124 | + engine = new ObjectQL({ logger }); |
| 125 | + const d = makeDriver(); |
| 126 | + driver = d; |
| 127 | + storeFor = d.storeFor; |
| 128 | + engine.registerDriver(d.driver, true); |
| 129 | + await engine.init(); |
| 130 | + const registry = engine.registry; |
| 131 | + registry.installPackage(manifest(APP, 'app'), 'code'); |
| 132 | + registry.installPackage(manifest(MODULE, 'module'), 'code'); |
| 133 | + // The app owns both the parent and the child. |
| 134 | + registry.registerObject({ |
| 135 | + name: ACCOUNT, label: 'Account', |
| 136 | + fields: { name: { name: 'name', type: 'text', label: 'Name' } }, |
| 137 | + } as any, APP, 'qr', 'own'); |
| 138 | + registry.registerObject({ |
| 139 | + name: NOTE, label: 'Note', |
| 140 | + fields: { |
| 141 | + amount: { name: 'amount', type: 'number', label: 'Amount' }, |
| 142 | + account: { name: 'account', type: 'lookup', label: 'Account', reference: ACCOUNT }, |
| 143 | + }, |
| 144 | + } as any, APP, 'qr', 'own'); |
| 145 | + // The module adds the roll-up to the app's parent, by extension. |
| 146 | + registry.registerObject({ |
| 147 | + name: ACCOUNT, |
| 148 | + fields: { |
| 149 | + [TOTAL]: { name: TOTAL, type: 'summary', label: 'Note total', summaryOperations: { object: NOTE, field: 'amount', function: 'sum' } }, |
| 150 | + }, |
| 151 | + } as any, MODULE, 'qr', 'extend'); |
| 152 | + }); |
| 153 | + |
| 154 | + const stored = (id: string) => storeFor(ACCOUNT).get(id)?.[TOTAL]; |
| 155 | + |
| 156 | + it('is current after re-enable, with no further child write', async () => { |
| 157 | + const acme = await engine.insert(ACCOUNT, { name: 'acme' }); |
| 158 | + const beta = await engine.insert(ACCOUNT, { name: 'beta' }); |
| 159 | + const first = await engine.insert(NOTE, { account: acme.id, amount: 10 }); |
| 160 | + expect(stored(acme.id), 'precondition: the roll-up tracks while the module is enabled').toBe(10); |
| 161 | + |
| 162 | + engine.registry.disablePackage(MODULE); |
| 163 | + // The child belongs to the enabled app: its writes land during the disable. |
| 164 | + await engine.insert(NOTE, { account: acme.id, amount: 5 }); |
| 165 | + await engine.update(NOTE, { id: first.id, amount: 20 }); |
| 166 | + await engine.insert(NOTE, { account: beta.id, amount: 7 }); |
| 167 | + |
| 168 | + engine.registry.enablePackage(MODULE); |
| 169 | + await engine.summaryRefreshSettled(); |
| 170 | + |
| 171 | + const served = await engine.findOne(ACCOUNT, { where: { id: acme.id }, context: { isSystem: true } }); |
| 172 | + expect(served?.[TOTAL]).toBe(25); |
| 173 | + expect(stored(beta.id)).toBe(7); |
| 174 | + }); |
| 175 | + |
| 176 | + it('writes no parent row on an enable when no child moved during the disable', async () => { |
| 177 | + const acme = await engine.insert(ACCOUNT, { name: 'acme' }); |
| 178 | + await engine.insert(NOTE, { account: acme.id, amount: 10 }); |
| 179 | + engine.registry.disablePackage(MODULE); |
| 180 | + driver.updates.length = 0; |
| 181 | + |
| 182 | + engine.registry.enablePackage(MODULE); |
| 183 | + await engine.summaryRefreshSettled(); |
| 184 | + |
| 185 | + expect(driver.updates.filter((u) => u.object === ACCOUNT)).toEqual([]); |
| 186 | + expect(stored(acme.id)).toBe(10); |
| 187 | + }); |
| 188 | + |
| 189 | + it('brings every other parent current when one parent\'s recompute fails, and says so at error', async () => { |
| 190 | + const acme = await engine.insert(ACCOUNT, { name: 'acme' }); |
| 191 | + const beta = await engine.insert(ACCOUNT, { name: 'beta' }); |
| 192 | + engine.registry.disablePackage(MODULE); |
| 193 | + await engine.insert(NOTE, { account: acme.id, amount: 5 }); |
| 194 | + await engine.insert(NOTE, { account: beta.id, amount: 7 }); |
| 195 | + driver.failUpdateOf.add(acme.id); |
| 196 | + |
| 197 | + engine.registry.enablePackage(MODULE); |
| 198 | + await engine.summaryRefreshSettled(); |
| 199 | + |
| 200 | + expect(stored(beta.id)).toBe(7); |
| 201 | + expect(stored(acme.id)).toBe(0); |
| 202 | + const said = logger.error.mock.calls.map((c) => String(c[0])).filter((m) => m.startsWith('[summary-refresh]')); |
| 203 | + expect(said).toHaveLength(1); |
| 204 | + expect(said[0]).toContain(`${ACCOUNT}/${acme.id}`); |
| 205 | + // The operator's re-run, once the cause is cleared. |
| 206 | + driver.failUpdateOf.clear(); |
| 207 | + const report = await engine.refreshSummariesEnabledBy(MODULE); |
| 208 | + expect(report.failures).toEqual([]); |
| 209 | + expect(stored(acme.id)).toBe(5); |
| 210 | + }); |
| 211 | +}); |
| 212 | + |
| 213 | +describe('SchemaRegistry.onPackageEnabled', () => { |
| 214 | + it('announces an enable of a disabled package, and not one of a package already enabled', () => { |
| 215 | + const engine = new ObjectQL(); |
| 216 | + const registry = engine.registry; |
| 217 | + registry.installPackage(manifest(APP, 'app'), 'code'); |
| 218 | + const heard: string[] = []; |
| 219 | + const stop = registry.onPackageEnabled((id) => { heard.push(id); }); |
| 220 | + |
| 221 | + registry.enablePackage(APP); |
| 222 | + expect(heard).toEqual([]); |
| 223 | + registry.disablePackage(APP); |
| 224 | + registry.enablePackage(APP); |
| 225 | + expect(heard).toEqual([APP]); |
| 226 | + stop(); |
| 227 | + registry.disablePackage(APP); |
| 228 | + registry.enablePackage(APP); |
| 229 | + expect(heard).toEqual([APP]); |
| 230 | + }); |
| 231 | +}); |
0 commit comments