|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#3455] Extends the single-write drop-observability of #3431 to the BULK |
| 4 | +// write paths. Each bulk method must (a) surface the same LEGAL strips |
| 5 | +// (static `readonly` #2948 / `readonlyWhen` #3042 / #3043 create ingress) that |
| 6 | +// single-write now reports, and (b) thread the caller's execution `context` to |
| 7 | +// the engine so RLS/FLS/`readonlyWhen` run under the caller — a gap the |
| 8 | +// pre-#3455 `updateManyData`/`batchData` loops had. Channels: |
| 9 | +// - updateManyData / batchData → per-row `droppedFields` on each result row; |
| 10 | +// - insertManyData → per-row `droppedFields` on each outcome; |
| 11 | +// - createManyData → aggregated top-level `droppedFields` (its |
| 12 | +// response has no per-row slot; the insert strip is schema-uniform). |
| 13 | + |
| 14 | +import { describe, it, expect, vi } from 'vitest'; |
| 15 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 16 | + |
| 17 | +const SCHEMA = { |
| 18 | + name: 'approval_case', |
| 19 | + fields: { |
| 20 | + title: { name: 'title', type: 'text' }, |
| 21 | + approval_status: { name: 'approval_status', type: 'text', readonly: true, defaultValue: 'draft' }, |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +describe('updateManyData — per-row droppedFields + context threading (#3455)', () => { |
| 26 | + it('surfaces per-row engine strips and threads the caller context to each update', async () => { |
| 27 | + const update = vi.fn(async (object: string, data: any, options?: any) => { |
| 28 | + // Only the second row forges the readonly field → only it drops. |
| 29 | + if (data.approval_status !== undefined) { |
| 30 | + options?.onFieldsDropped?.({ object, fields: ['approval_status'], reason: 'readonly' }); |
| 31 | + } |
| 32 | + return { id: options.where.id, title: data.title }; |
| 33 | + }); |
| 34 | + const engine = { registry: { getObject: () => SCHEMA }, update, findOne: vi.fn(async () => null) }; |
| 35 | + const p = new ObjectStackProtocolImplementation(engine as any); |
| 36 | + |
| 37 | + const ctx = { userId: 'u1' }; |
| 38 | + const res: any = await p.updateManyData({ |
| 39 | + object: 'approval_case', |
| 40 | + records: [ |
| 41 | + { id: 'rec-1', data: { title: 'A' } }, |
| 42 | + { id: 'rec-2', data: { title: 'B', approval_status: 'approved' } }, |
| 43 | + ], |
| 44 | + context: ctx, |
| 45 | + } as any); |
| 46 | + |
| 47 | + // Row 0 dropped nothing → no droppedFields key; row 1 dropped approval_status. |
| 48 | + expect(res.results[0]).not.toHaveProperty('droppedFields'); |
| 49 | + expect(res.results[1].droppedFields).toEqual([ |
| 50 | + { object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }, |
| 51 | + ]); |
| 52 | + // [#3455] The pre-fix loop never threaded context — assert every engine |
| 53 | + // call now runs under the caller's principal. |
| 54 | + expect(update).toHaveBeenCalledTimes(2); |
| 55 | + expect(update.mock.calls[0][2].context).toBe(ctx); |
| 56 | + expect(update.mock.calls[1][2].context).toBe(ctx); |
| 57 | + expect(res.succeeded).toBe(2); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('createManyData — aggregated top-level droppedFields (#3455)', () => { |
| 62 | + function makeProtocol() { |
| 63 | + const engine = { |
| 64 | + registry: { getObject: (n: string) => (n === 'approval_case' ? SCHEMA : undefined) }, |
| 65 | + insert: vi.fn(async (_object: string, rows: any[]) => rows.map((r, i) => ({ id: `rec-${i + 1}`, ...r }))), |
| 66 | + }; |
| 67 | + return { p: new ObjectStackProtocolImplementation(engine as any), engine }; |
| 68 | + } |
| 69 | + |
| 70 | + it('aggregates the schema-uniform ingress strip across rows into one event', async () => { |
| 71 | + const { p } = makeProtocol(); |
| 72 | + const res: any = await p.createManyData({ |
| 73 | + object: 'approval_case', |
| 74 | + records: [ |
| 75 | + { title: 'A', approval_status: 'approved' }, |
| 76 | + { title: 'B', approval_status: 'approved' }, |
| 77 | + ], |
| 78 | + context: { userId: 'u1' }, |
| 79 | + }); |
| 80 | + // Union, not one-event-per-row: both rows dropped the same readonly field. |
| 81 | + expect(res.droppedFields).toEqual([ |
| 82 | + { object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }, |
| 83 | + ]); |
| 84 | + expect(res.count).toBe(2); |
| 85 | + expect(res.records[0]).not.toHaveProperty('approval_status'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('omits droppedFields when no row seeds a readonly field', async () => { |
| 89 | + const { p } = makeProtocol(); |
| 90 | + const res: any = await p.createManyData({ |
| 91 | + object: 'approval_case', |
| 92 | + records: [{ title: 'A' }, { title: 'B' }], |
| 93 | + context: { userId: 'u1' }, |
| 94 | + }); |
| 95 | + expect(res).not.toHaveProperty('droppedFields'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('a system-context bulk create keeps the field — no strip, no droppedFields', async () => { |
| 99 | + const { p } = makeProtocol(); |
| 100 | + const res: any = await p.createManyData({ |
| 101 | + object: 'approval_case', |
| 102 | + records: [{ title: 'A', approval_status: 'approved' }], |
| 103 | + context: { isSystem: true }, |
| 104 | + }); |
| 105 | + expect(res).not.toHaveProperty('droppedFields'); |
| 106 | + expect(res.records[0].approval_status).toBe('approved'); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('insertManyData — per-row droppedFields on outcomes (#3455)', () => { |
| 111 | + it('attaches the ingress strip to the matching outcome row only', async () => { |
| 112 | + const insertMany = vi.fn(async (_object: string, rows: any[]) => |
| 113 | + rows.map((r, i) => ({ ok: true, record: { id: `rec-${i + 1}`, ...r } })), |
| 114 | + ); |
| 115 | + const engine = { registry: { getObject: () => SCHEMA }, insertMany }; |
| 116 | + const p = new ObjectStackProtocolImplementation(engine as any); |
| 117 | + |
| 118 | + const res: any = await p.insertManyData({ |
| 119 | + object: 'approval_case', |
| 120 | + records: [ |
| 121 | + { title: 'A' }, |
| 122 | + { title: 'B', approval_status: 'approved' }, |
| 123 | + ], |
| 124 | + context: { userId: 'u1' }, |
| 125 | + }); |
| 126 | + |
| 127 | + expect(res.outcomes[0]).not.toHaveProperty('droppedFields'); |
| 128 | + expect(res.outcomes[1].droppedFields).toEqual([ |
| 129 | + { object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }, |
| 130 | + ]); |
| 131 | + // The strip really removed the field from what the engine inserted. |
| 132 | + expect(insertMany.mock.calls[0][1][1]).not.toHaveProperty('approval_status'); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe('batchData — per-row droppedFields + context threading (#3455)', () => { |
| 137 | + it('create rows surface the ingress strip and honour a system context', async () => { |
| 138 | + const insert = vi.fn(async (_object: string, data: any, _options?: any) => ({ id: 'rec-1', ...data })); |
| 139 | + const engine = { registry: { getObject: () => SCHEMA }, insert, update: vi.fn(), findOne: vi.fn() }; |
| 140 | + const p = new ObjectStackProtocolImplementation(engine as any); |
| 141 | + |
| 142 | + const res: any = await p.batchData({ |
| 143 | + object: 'approval_case', |
| 144 | + request: { |
| 145 | + operation: 'create', |
| 146 | + records: [{ data: { title: 'A', approval_status: 'approved' } }], |
| 147 | + }, |
| 148 | + context: { userId: 'u1' }, |
| 149 | + } as any); |
| 150 | + |
| 151 | + expect(res.results[0].droppedFields).toEqual([ |
| 152 | + { object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }, |
| 153 | + ]); |
| 154 | + expect(res.results[0].record).not.toHaveProperty('approval_status'); |
| 155 | + // [#3455] context is threaded to the insert (was hard-coded undefined before). |
| 156 | + expect(insert.mock.calls[0][2].context).toEqual({ userId: 'u1' }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('a system-context batch create is exempt from the strip (context now threaded to it)', async () => { |
| 160 | + const insert = vi.fn(async (_object: string, data: any) => ({ id: 'rec-1', ...data })); |
| 161 | + const engine = { registry: { getObject: () => SCHEMA }, insert, update: vi.fn(), findOne: vi.fn() }; |
| 162 | + const p = new ObjectStackProtocolImplementation(engine as any); |
| 163 | + |
| 164 | + const res: any = await p.batchData({ |
| 165 | + object: 'approval_case', |
| 166 | + request: { operation: 'create', records: [{ data: { title: 'A', approval_status: 'approved' } }] }, |
| 167 | + context: { isSystem: true }, |
| 168 | + } as any); |
| 169 | + |
| 170 | + expect(res.results[0]).not.toHaveProperty('droppedFields'); |
| 171 | + expect(res.results[0].record.approval_status).toBe('approved'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('update rows surface the engine strip and keep droppedFields when returnRecords=false', async () => { |
| 175 | + const update = vi.fn(async (object: string, _data: any, options?: any) => { |
| 176 | + options?.onFieldsDropped?.({ object, fields: ['approval_status'], reason: 'readonly' }); |
| 177 | + return { id: options.where.id }; |
| 178 | + }); |
| 179 | + const engine = { registry: { getObject: () => SCHEMA }, update, insert: vi.fn(), findOne: vi.fn() }; |
| 180 | + const p = new ObjectStackProtocolImplementation(engine as any); |
| 181 | + |
| 182 | + const res: any = await p.batchData({ |
| 183 | + object: 'approval_case', |
| 184 | + request: { |
| 185 | + operation: 'update', |
| 186 | + records: [{ id: 'rec-1', data: { approval_status: 'approved' } }], |
| 187 | + options: { returnRecords: false }, |
| 188 | + }, |
| 189 | + context: { userId: 'u1' }, |
| 190 | + } as any); |
| 191 | + |
| 192 | + // returnRecords:false drops `record` but MUST keep the warning. |
| 193 | + expect(res.results[0]).not.toHaveProperty('record'); |
| 194 | + expect(res.results[0].droppedFields).toEqual([ |
| 195 | + { object: 'approval_case', fields: ['approval_status'], reason: 'readonly' }, |
| 196 | + ]); |
| 197 | + expect(update.mock.calls[0][2].context).toEqual({ userId: 'u1' }); |
| 198 | + }); |
| 199 | +}); |
0 commit comments