|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A cascade dependent's stale-roll-up refusal never answers for a delete that |
| 5 | + * did not land. |
| 6 | + * |
| 7 | + * `SummaryRecomputeError` (`ERR_SUMMARY_RECOMPUTE`) means "this write is |
| 8 | + * stored, only a parent's roll-up recompute failed", and every answering |
| 9 | + * surface (the data doors, the action / sandbox / flow writers) turns it into |
| 10 | + * the write's own success. A by-id delete walks its `master_detail` |
| 11 | + * dependents first, each through the public `delete()`. When one dependent's |
| 12 | + * delete raises that refusal (its roll-up into ANOTHER parent cannot be |
| 13 | + * recomputed), the walk stops before the parent's own row is removed, so the |
| 14 | + * parent delete did not land — under the `'atomic'` plan the transaction then |
| 15 | + * rolls the dependent back too. The refusal used to leave the parent's |
| 16 | + * `delete()` unchanged, and the caller was told the delete succeeded. |
| 17 | + * |
| 18 | + * Pinned here: the parent delete refuses with an error that is NOT the |
| 19 | + * stale-roll-up refusal, on the atomic plan (nothing stored changes) and on a |
| 20 | + * runtime that cannot roll back (the parent is still stored); a refusal that |
| 21 | + * escapes a caller's own `transaction()` that rolls back is converted the same |
| 22 | + * way. Controls: a delete of the dependent itself — its own write landed, only |
| 23 | + * its recompute failed — still answers `ERR_SUMMARY_RECOMPUTE` carrying its |
| 24 | + * result, and so does a caller's transaction that catches it and commits. |
| 25 | + * |
| 26 | + * The driver rolls back for real (a snapshot per transaction), and refuses |
| 27 | + * every write to `bucket` while `refuseBucket` is set: that is the roll-up |
| 28 | + * recompute's parent update, refused without a transient message, so no retry |
| 29 | + * runs. |
| 30 | + */ |
| 31 | + |
| 32 | +import { describe, it, expect } from 'vitest'; |
| 33 | +import { ObjectQL } from './engine.js'; |
| 34 | + |
| 35 | +type Row = Record<string, unknown>; |
| 36 | + |
| 37 | +function matches(row: Row, where: any): boolean { |
| 38 | + if (!where || typeof where !== 'object') return true; |
| 39 | + return Object.entries(where).every(([k, v]) => { |
| 40 | + if (k === '$and') return (v as any[]).every((w) => matches(row, w)); |
| 41 | + if (k === '$or') return (v as any[]).some((w) => matches(row, w)); |
| 42 | + if (k.startsWith('$')) return true; |
| 43 | + if (v && typeof v === 'object' && !Array.isArray(v)) { |
| 44 | + const ops = v as Record<string, unknown>; |
| 45 | + if ('$in' in ops) return (ops.$in as unknown[]).map(String).includes(String(row[k])); |
| 46 | + if ('$eq' in ops) return String(row[k]) === String(ops.$eq); |
| 47 | + if ('$ne' in ops) return String(row[k]) !== String(ops.$ne); |
| 48 | + return true; |
| 49 | + } |
| 50 | + return row[k] === v || (row[k] != null && v != null && String(row[k]) === String(v)); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +function makeDriver(transactional: boolean) { |
| 55 | + let stores = new Map<string, Map<string, Row>>(); |
| 56 | + const storeFor = (o: string) => stores.get(o) ?? (stores.set(o, new Map()), stores.get(o)!); |
| 57 | + const begun: unknown[] = []; |
| 58 | + const rolledBack: unknown[] = []; |
| 59 | + const snapshots = new Map<unknown, typeof stores>(); |
| 60 | + const state = { refuseBucket: false }; |
| 61 | + let n = 0; |
| 62 | + const driver: any = { |
| 63 | + name: 'primary', version: '0.0.0', supports: {}, begun, rolledBack, state, |
| 64 | + rowsOf: (o: string) => Array.from(storeFor(o).values()), |
| 65 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 66 | + async syncSchema() {}, |
| 67 | + async find(o: string, ast: any) { |
| 68 | + const rows = Array.from(storeFor(o).values()).filter((r) => matches(r, ast?.where)); |
| 69 | + return typeof ast?.limit === 'number' ? rows.slice(0, ast.limit) : rows; |
| 70 | + }, |
| 71 | + async findOne(o: string, ast: any) { return (await this.find(o, ast))[0] ?? null; }, |
| 72 | + async create(o: string, data: Row) { |
| 73 | + const row = { ...data, id: (data.id as string) ?? `${o}_${++n}` }; |
| 74 | + storeFor(o).set(String(row.id), row); |
| 75 | + return row; |
| 76 | + }, |
| 77 | + async update(o: string, id: string, data: Row) { |
| 78 | + if (state.refuseBucket && o === 'bucket') throw new Error('bucket store refused the write'); |
| 79 | + const row = { ...storeFor(o).get(String(id)), ...data, id }; |
| 80 | + storeFor(o).set(String(id), row); |
| 81 | + return row; |
| 82 | + }, |
| 83 | + async delete(o: string, id: string) { return storeFor(o).delete(String(id)); }, |
| 84 | + async count(o: string, ast: any) { return (await this.find(o, ast)).length; }, |
| 85 | + async bulkCreate(o: string, rows: Row[]) { return Promise.all(rows.map((r) => this.create(o, r))); }, |
| 86 | + async bulkUpdate() { return []; }, async bulkDelete() {}, |
| 87 | + }; |
| 88 | + if (transactional) { |
| 89 | + driver.beginTransaction = async () => { |
| 90 | + const handle = { trx: begun.length + 1 }; |
| 91 | + snapshots.set(handle, new Map(Array.from(stores, ([o, s]) => [o, new Map(Array.from(s, ([k, v]) => [k, { ...v }]))]))); |
| 92 | + begun.push(handle); |
| 93 | + return handle; |
| 94 | + }; |
| 95 | + driver.commit = async (h: unknown) => { snapshots.delete(h); }; |
| 96 | + driver.rollback = async (h: unknown) => { stores = snapshots.get(h) ?? stores; rolledBack.push(h); }; |
| 97 | + } |
| 98 | + return driver; |
| 99 | +} |
| 100 | + |
| 101 | +async function makeEngine(transactional: boolean) { |
| 102 | + const log = () => {}; |
| 103 | + const engine = new ObjectQL({ logger: { debug: log, info: log, warn: log, error: log } } as any); |
| 104 | + const driver = makeDriver(transactional); |
| 105 | + engine.registerDriver(driver, true); |
| 106 | + await engine.init(); |
| 107 | + const text = (name: string) => ({ name, type: 'text' }); |
| 108 | + engine.registry.registerObject({ name: 'acct', label: 'acct', fields: { id: { ...text('id'), primaryKey: true }, name: text('name') } } as any, '__test__'); |
| 109 | + engine.registry.registerObject({ |
| 110 | + name: 'bucket', label: 'bucket', |
| 111 | + fields: { |
| 112 | + id: { ...text('id'), primaryKey: true }, name: text('name'), |
| 113 | + line_count: { name: 'line_count', type: 'summary', summaryOperations: { object: 'line', function: 'count' } }, |
| 114 | + }, |
| 115 | + } as any, '__test__'); |
| 116 | + engine.registry.registerObject({ |
| 117 | + name: 'line', label: 'line', |
| 118 | + fields: { |
| 119 | + id: { ...text('id'), primaryKey: true }, name: text('name'), |
| 120 | + acct: { name: 'acct', type: 'master_detail', reference: 'acct', deleteBehavior: 'cascade' }, |
| 121 | + bucket: { name: 'bucket', type: 'lookup', reference: 'bucket' }, |
| 122 | + }, |
| 123 | + } as any, '__test__'); |
| 124 | + await engine.insert('acct', { id: 'a1', name: 'A' }); |
| 125 | + await engine.insert('bucket', { id: 'b1', name: 'B' }); |
| 126 | + await engine.insert('line', { id: 'l1', name: 'L1', acct: 'a1', bucket: 'b1' }); |
| 127 | + await engine.insert('line', { id: 'l2', name: 'L2', acct: 'a1', bucket: 'b1' }); |
| 128 | + driver.begun.length = 0; |
| 129 | + driver.rolledBack.length = 0; |
| 130 | + driver.state.refuseBucket = true; |
| 131 | + return { engine, driver }; |
| 132 | +} |
| 133 | + |
| 134 | +const ids = (rows: Row[]) => rows.map((r) => r.id).sort(); |
| 135 | + |
| 136 | +describe('a cascade dependent\'s ERR_SUMMARY_RECOMPUTE never answers for a parent delete that did not land', () => { |
| 137 | + it('atomic plan: the parent delete refuses with another error, and nothing stored changes', async () => { |
| 138 | + const { engine, driver } = await makeEngine(true); |
| 139 | + const err: any = await engine.delete('acct', { where: { id: 'a1' } } as any).then(() => null, (e: unknown) => e); |
| 140 | + expect(err).toBeInstanceOf(Error); |
| 141 | + expect(err.code).toBeUndefined(); |
| 142 | + expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 143 | + expect(err.written).toBeUndefined(); |
| 144 | + expect(driver.rolledBack).toHaveLength(1); |
| 145 | + expect(ids(driver.rowsOf('acct'))).toEqual(['a1']); |
| 146 | + expect(ids(driver.rowsOf('line'))).toEqual(['l1', 'l2']); |
| 147 | + }); |
| 148 | + |
| 149 | + it('a runtime that cannot roll back: the parent is still stored, and the caller is refused', async () => { |
| 150 | + const { engine, driver } = await makeEngine(false); |
| 151 | + const err: any = await engine.delete('acct', { where: { id: 'a1' } } as any).then(() => null, (e: unknown) => e); |
| 152 | + expect(err).toBeInstanceOf(Error); |
| 153 | + expect(err.code).toBeUndefined(); |
| 154 | + expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 155 | + expect(ids(driver.rowsOf('acct'))).toEqual(['a1']); |
| 156 | + // The dependent the walk reached first is gone: nothing can roll it back. |
| 157 | + expect(ids(driver.rowsOf('line'))).toEqual(['l2']); |
| 158 | + }); |
| 159 | + |
| 160 | + it('a caller\'s own transaction that the refusal rolls back: refused with another error, nothing stored', async () => { |
| 161 | + const { engine, driver } = await makeEngine(true); |
| 162 | + const err: any = await engine |
| 163 | + .transaction(async (trx: any) => engine.delete('line', { where: { id: 'l1' }, context: trx } as any)) |
| 164 | + .then(() => null, (e: unknown) => e); |
| 165 | + expect(err).toBeInstanceOf(Error); |
| 166 | + expect(err.code).toBeUndefined(); |
| 167 | + expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 168 | + expect(driver.rolledBack).toHaveLength(1); |
| 169 | + expect(ids(driver.rowsOf('line'))).toEqual(['l1', 'l2']); |
| 170 | + }); |
| 171 | + |
| 172 | + it('the same through a scoped context\'s transaction (ctx.api)', async () => { |
| 173 | + const { engine, driver } = await makeEngine(true); |
| 174 | + const scoped = (engine as any).createContext({ isSystem: true }); |
| 175 | + const err: any = await scoped |
| 176 | + .transaction(async (trx: any) => trx.object('line').deleteById('l1')) |
| 177 | + .then(() => null, (e: unknown) => e); |
| 178 | + expect(err).toBeInstanceOf(Error); |
| 179 | + expect(err.code).toBeUndefined(); |
| 180 | + expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 181 | + expect(driver.rolledBack).toHaveLength(1); |
| 182 | + expect(ids(driver.rowsOf('line'))).toEqual(['l1', 'l2']); |
| 183 | + }); |
| 184 | + |
| 185 | + it('CONTROL the dependent\'s own delete landed: ERR_SUMMARY_RECOMPUTE, carrying its result', async () => { |
| 186 | + const { engine, driver } = await makeEngine(true); |
| 187 | + const err: any = await engine.delete('line', { where: { id: 'l1' } } as any).then(() => null, (e: unknown) => e); |
| 188 | + expect(err?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 189 | + expect(err.written).toBe(true); |
| 190 | + expect(ids(driver.rowsOf('line'))).toEqual(['l2']); |
| 191 | + }); |
| 192 | + |
| 193 | + it('CONTROL a caller\'s transaction that catches the refusal commits, and the refusal stays true', async () => { |
| 194 | + const { engine, driver } = await makeEngine(true); |
| 195 | + let caught: any; |
| 196 | + await engine.transaction(async (trx: any) => { |
| 197 | + await engine.delete('line', { where: { id: 'l1' }, context: trx } as any).catch((e: unknown) => { caught = e; }); |
| 198 | + }); |
| 199 | + expect(caught?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 200 | + expect(driver.rolledBack).toHaveLength(0); |
| 201 | + expect(ids(driver.rowsOf('line'))).toEqual(['l2']); |
| 202 | + }); |
| 203 | +}); |
0 commit comments