|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `POST /api/v1/batch` — an `update` / `delete` op whose row address names no |
| 5 | + * row is refused up front with the missing-id answer, on a real engine |
| 6 | + * (`ObjectQL` + sqlite `SqlDriver`), the real protocol and the real |
| 7 | + * `RestServer` route, with `objectQLProvider` wired so the cross-object door is |
| 8 | + * reachable rather than answering 501. |
| 9 | + * |
| 10 | + * The door's up-front check read `op.id == null && op.data?.id == null`, so an |
| 11 | + * address the engine does not take as an id — the empty string, `0`, a |
| 12 | + * non-scalar — passed it, reached `ql.update` / `ql.delete`, and the engine's |
| 13 | + * `Update requires an ID…` / `Delete requires an ID…` throw answered as a |
| 14 | + * server fault. And the check accepted `data.id` for a `delete`, whose write |
| 15 | + * arm never read it. Both are now judged once: the address each arm hands the |
| 16 | + * engine (`batchOpRowAddress`), against the engine's own by-id predicate. |
| 17 | + * |
| 18 | + * Measured on `origin/main` a43405d59 with this harness, before the fix: every |
| 19 | + * REFUSED row below answered `500` — `DATABASE_ERROR` on an update arm, |
| 20 | + * `INTERNAL_ERROR` on a delete arm — and wrote nothing (the transaction rolled |
| 21 | + * the batch back). They now answer `400 VALIDATION_FAILED` with the sentence a |
| 22 | + * missing id gets, before the transaction opens. |
| 23 | + * |
| 24 | + * Controls: a missing id answers as before; a real id updates and deletes as |
| 25 | + * before (by `id`, and an update by `data.id`); an update whose `data.id` is a |
| 26 | + * `{ $ref }` to an earlier create still resolves; a whitespace id is an id the |
| 27 | + * engine looks up, so it answers what an unknown id answers (`404`), not this |
| 28 | + * refusal. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 32 | +import { ObjectQL } from '@objectstack/objectql'; |
| 33 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 34 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 35 | +import { RestServer } from './rest-server'; |
| 36 | + |
| 37 | +const OBJ = { |
| 38 | + name: 'blank_id_task', label: 'Task', systemFields: false, |
| 39 | + fields: { |
| 40 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 41 | + title: { name: 'title', type: 'text' as const }, |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +const liveEngines: ObjectQL[] = []; |
| 46 | +afterEach(async () => { |
| 47 | + while (liveEngines.length) { |
| 48 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +function createMockServer() { |
| 53 | + const noop = () => {}; |
| 54 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 55 | +} |
| 56 | + |
| 57 | +function makeRes() { |
| 58 | + const res: any = { |
| 59 | + write: () => true, end: () => {}, |
| 60 | + header: () => res, |
| 61 | + status: (code: number) => { res._status = code; return res; }, |
| 62 | + json: (body: any) => { res._json = body; return res; }, |
| 63 | + }; |
| 64 | + return res; |
| 65 | +} |
| 66 | + |
| 67 | +async function boot() { |
| 68 | + const engine = new ObjectQL(); |
| 69 | + liveEngines.push(engine); |
| 70 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 71 | + engine.registerDriver(driver, true); |
| 72 | + await engine.init(); |
| 73 | + engine.registry.registerObject(OBJ as any); |
| 74 | + await engine.syncSchemas(); |
| 75 | + await engine.insert('blank_id_task', { id: 'row-1', title: 'Kept' }); |
| 76 | + |
| 77 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 78 | + const rest = new RestServer( |
| 79 | + createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any, |
| 80 | + undefined, undefined, undefined, undefined, // kernelManager, envRegistry, defaultEnvIdProvider, authServiceProvider |
| 81 | + (async () => engine) as any, // objectQLProvider (positional arg #8) |
| 82 | + ); |
| 83 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 84 | + rest.registerRoutes(); |
| 85 | + const route = rest.getRoutes().find((r: any) => r.method === 'POST' && r.path === '/api/v1/batch'); |
| 86 | + expect(route, 'POST /api/v1/batch').toBeDefined(); |
| 87 | + const transaction = vi.spyOn(engine, 'transaction'); |
| 88 | + const batch = async (operations: unknown[], extra: Record<string, unknown> = {}) => { |
| 89 | + const res = makeRes(); |
| 90 | + await route!.handler({ method: 'POST', params: {}, query: {}, headers: {}, body: { operations, ...extra } } as any, res); |
| 91 | + return { status: res._status ?? 200, body: res._json }; |
| 92 | + }; |
| 93 | + const row = (id: string) => engine.findOne('blank_id_task', { where: { id } } as any); |
| 94 | + return { batch, row, transaction }; |
| 95 | +} |
| 96 | + |
| 97 | +const O = 'blank_id_task'; |
| 98 | +const missingId = (action: 'update' | 'delete') => ({ |
| 99 | + error: `Operation '${action}' on '${O}' requires an id`, |
| 100 | + code: 'VALIDATION_FAILED', |
| 101 | +}); |
| 102 | + |
| 103 | +/** Every op here names no row the engine could address; the arm is the op's own. */ |
| 104 | +const REFUSED: ReadonlyArray<readonly [string, 'update' | 'delete', Record<string, unknown>]> = [ |
| 105 | + ['update, id ""', 'update', { id: '', data: { title: 'Changed' } }], |
| 106 | + ['update, data.id ""', 'update', { data: { id: '', title: 'Changed' } }], |
| 107 | + ['update, id "" beside a real data.id (the arm reads id first)', 'update', { id: '', data: { id: 'row-1', title: 'Changed' } }], |
| 108 | + ['update, data.id 0', 'update', { data: { id: 0, title: 'Changed' } }], |
| 109 | + ['update, data.id false', 'update', { data: { id: false, title: 'Changed' } }], |
| 110 | + ['update, data.id true', 'update', { data: { id: true, title: 'Changed' } }], |
| 111 | + ['update, data.id {}', 'update', { data: { id: {}, title: 'Changed' } }], |
| 112 | + ['update, data.id []', 'update', { data: { id: [], title: 'Changed' } }], |
| 113 | + ['delete, id ""', 'delete', { id: '' }], |
| 114 | + ['delete, id "" beside a real data.id', 'delete', { id: '', data: { id: 'row-1' } }], |
| 115 | + ['delete, a real id in data.id only (the delete arm never read data.id)', 'delete', { data: { id: 'row-1' } }], |
| 116 | +]; |
| 117 | + |
| 118 | +describe('POST /api/v1/batch: an update or delete op that names no row answers the missing-id 400', () => { |
| 119 | + let ctx: Awaited<ReturnType<typeof boot>>; |
| 120 | + beforeEach(async () => { ctx = await boot(); }); |
| 121 | + |
| 122 | + it.each(REFUSED)('%s → 400 VALIDATION_FAILED, before the transaction opens, nothing written', async (_l, action, op) => { |
| 123 | + const res = await ctx.batch([{ object: O, action, ...op }], { atomic: true }); |
| 124 | + expect({ status: res.status, body: res.body }).toEqual({ status: 400, body: missingId(action) }); |
| 125 | + expect(ctx.transaction).not.toHaveBeenCalled(); |
| 126 | + expect(await ctx.row('row-1')).toMatchObject({ id: 'row-1', title: 'Kept' }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('refuses the whole batch up front: an earlier create in it is never written', async () => { |
| 130 | + const res = await ctx.batch([ |
| 131 | + { object: O, action: 'create', data: { id: 'row-2', title: 'New' } }, |
| 132 | + { object: O, action: 'update', id: '', data: { title: 'Changed' } }, |
| 133 | + ]); |
| 134 | + expect({ status: res.status, body: res.body }).toEqual({ status: 400, body: missingId('update') }); |
| 135 | + expect(ctx.transaction).not.toHaveBeenCalled(); |
| 136 | + expect(await ctx.row('row-2')).toBeFalsy(); |
| 137 | + }); |
| 138 | + |
| 139 | + // ── controls: answers that do not move ──────────────────────────────────── |
| 140 | + |
| 141 | + it.each([ |
| 142 | + ['update', { data: { title: 'Changed' } }], |
| 143 | + ['delete', {}], |
| 144 | + ] as const)('control: %s with no id at all answers the same 400', async (action, op) => { |
| 145 | + const res = await ctx.batch([{ object: O, action, ...op }]); |
| 146 | + expect({ status: res.status, body: res.body }).toEqual({ status: 400, body: missingId(action) }); |
| 147 | + expect(await ctx.row('row-1')).toMatchObject({ title: 'Kept' }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('control: an update by id writes the row', async () => { |
| 151 | + const res = await ctx.batch([{ object: O, action: 'update', id: 'row-1', data: { title: 'By id' } }]); |
| 152 | + expect(res.status).toBe(200); |
| 153 | + expect(await ctx.row('row-1')).toMatchObject({ title: 'By id' }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('control: an update by data.id writes the row', async () => { |
| 157 | + const res = await ctx.batch([{ object: O, action: 'update', data: { id: 'row-1', title: 'By data.id' } }]); |
| 158 | + expect(res.status).toBe(200); |
| 159 | + expect(await ctx.row('row-1')).toMatchObject({ title: 'By data.id' }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('control: an update whose data.id is a $ref to an earlier create still resolves', async () => { |
| 163 | + const res = await ctx.batch([ |
| 164 | + { object: O, action: 'create', data: { id: 'row-3', title: 'Created' } }, |
| 165 | + { object: O, action: 'update', data: { id: { $ref: 0 }, title: 'Then updated' } }, |
| 166 | + ]); |
| 167 | + expect(res.status).toBe(200); |
| 168 | + expect(await ctx.row('row-3')).toMatchObject({ title: 'Then updated' }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('control: a delete by id deletes the row', async () => { |
| 172 | + const res = await ctx.batch([{ object: O, action: 'delete', id: 'row-1' }]); |
| 173 | + expect(res.status).toBe(200); |
| 174 | + expect(await ctx.row('row-1')).toBeFalsy(); |
| 175 | + }); |
| 176 | + |
| 177 | + it.each([ |
| 178 | + ['an unknown id', 'nope'], |
| 179 | + ['a whitespace id (an id the engine looks up)', ' '], |
| 180 | + ])('control: update and delete with %s answer 404 RECORD_NOT_FOUND, not this refusal', async (_l, id) => { |
| 181 | + for (const op of [{ action: 'update', id, data: { title: 'Changed' } }, { action: 'delete', id }]) { |
| 182 | + const res = await ctx.batch([{ object: O, ...op }]); |
| 183 | + expect([op.action, res.status, res.body?.code]).toEqual([op.action, 404, 'RECORD_NOT_FOUND']); |
| 184 | + } |
| 185 | + expect(await ctx.row('row-1')).toMatchObject({ title: 'Kept' }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments