|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#8570] The record lock's refusal, as a BATCH ROW — driven through the REAL |
| 5 | + * hook, a real {@link ObjectQL} and a real sqlite driver. |
| 6 | + * |
| 7 | + * ## The row this file exists for |
| 8 | + * |
| 9 | + * Measured on the real stack while #8502 was being verified, a bulk update of a |
| 10 | + * record this plugin holds locked answered: |
| 11 | + * |
| 12 | + * ```json |
| 13 | + * { "code": "RECORD_LOCKED", "message": "RECORD_LOCKED: record 'ok1' of 'm8502_task' is locked while an approval is in progress" } |
| 14 | + * ``` |
| 15 | + * |
| 16 | + * — a deliberate **409** shipping with no `httpStatus` at all, while sibling |
| 17 | + * rows of the same response carried one. `toRowApiError` read `err.status`, and |
| 18 | + * {@link lockedError} spells its refusal `statusCode`, which is the same |
| 19 | + * single-spelling defect that made `/api/v1/data` answer 500 to this very |
| 20 | + * refusal until #7525. |
| 21 | + * |
| 22 | + * ## Why the pin lives HERE |
| 23 | + * |
| 24 | + * `metadata-protocol` cannot import this plugin, and its own pins therefore |
| 25 | + * stand in for this producer with a double whose shape was measured. This file |
| 26 | + * is the half that needs no double: the error is raised by the actual |
| 27 | + * `beforeUpdate` hook `bindApprovalLockHook` binds, against an actual pending |
| 28 | + * `sys_approval_request` row, and the response row is built by the actual |
| 29 | + * `updateManyData` loop. If the hook ever re-spells its refusal — `.status`, |
| 30 | + * or a plain `Error` — this file goes red where a double would happily keep |
| 31 | + * asserting the old shape. |
| 32 | + * |
| 33 | + * The rig is `record-lock-multi-update.integration.test.ts`'s, for the same |
| 34 | + * reason it gives: the store is better-sqlite3 through `@objectstack/driver-sql`, |
| 35 | + * so the predicates are compiled and executed by the SQL builder rather than by |
| 36 | + * fixture code written by the same author as the assertion. |
| 37 | + */ |
| 38 | + |
| 39 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 40 | +import { ObjectQL } from '@objectstack/objectql'; |
| 41 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 42 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 43 | +import { resolveThrownHttpError } from '@objectstack/types'; |
| 44 | +import { bindApprovalLockHook } from './lifecycle-hooks.js'; |
| 45 | + |
| 46 | +const opportunity = { |
| 47 | + name: 'opportunity', |
| 48 | + label: 'Opportunity', |
| 49 | + fields: { |
| 50 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 51 | + name: { name: 'name', type: 'text' as const }, |
| 52 | + amount: { name: 'amount', type: 'number' as const }, |
| 53 | + approval_status: { name: 'approval_status', type: 'text' as const }, |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +/** The lock hook reads pending requests off this object. */ |
| 58 | +const approvalRequest = { |
| 59 | + name: 'sys_approval_request', |
| 60 | + label: 'Approval Request', |
| 61 | + fields: { |
| 62 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 63 | + object_name: { name: 'object_name', type: 'text' as const }, |
| 64 | + record_id: { name: 'record_id', type: 'text' as const }, |
| 65 | + status: { name: 'status', type: 'text' as const }, |
| 66 | + flow_run_id: { name: 'flow_run_id', type: 'text' as const }, |
| 67 | + node_config_json: { name: 'node_config_json', type: 'text' as const }, |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +describe('[#8570] a locked record\'s batch row carries the 409 the hook declared', () => { |
| 72 | + let engine: ObjectQL; |
| 73 | + let protocol: any; |
| 74 | + /** Held by a pending approval. */ |
| 75 | + let lockedId: string; |
| 76 | + /** Same object, no approval — the row that must still succeed. */ |
| 77 | + let freeId: string; |
| 78 | + |
| 79 | + afterEach(async () => { |
| 80 | + try { await engine?.destroy(); } catch { /* noop */ } |
| 81 | + }); |
| 82 | + |
| 83 | + beforeEach(async () => { |
| 84 | + engine = new ObjectQL(); |
| 85 | + engine.registerDriver(new SqlDriver({ |
| 86 | + client: 'better-sqlite3', |
| 87 | + connection: { filename: ':memory:' }, |
| 88 | + useNullAsDefault: true, |
| 89 | + }), true); |
| 90 | + await engine.init(); |
| 91 | + // `packageId` is REQUIRED by `registerObject` — passed rather than elided |
| 92 | + // so this file adds no raw `tsc` error to the package's TEST_DEBT ledger, |
| 93 | + // which is measured with the test layer in the program (the package's own |
| 94 | + // `typecheck` script excludes `**/*.test.ts`, so it cannot see this). |
| 95 | + for (const o of [opportunity, approvalRequest]) { |
| 96 | + engine.registry.registerObject(o as any, 'com.objectstack.test.8570'); |
| 97 | + } |
| 98 | + // Real DDL through the real path. |
| 99 | + await engine.syncSchemas(); |
| 100 | + |
| 101 | + lockedId = String((await engine.insert('opportunity', { name: 'Deal', amount: 100 })).id); |
| 102 | + freeId = String((await engine.insert('opportunity', { name: 'Other', amount: 100 })).id); |
| 103 | + await engine.insert('sys_approval_request', { |
| 104 | + object_name: 'opportunity', |
| 105 | + record_id: lockedId, |
| 106 | + status: 'pending', |
| 107 | + flow_run_id: 'run_1', |
| 108 | + node_config_json: JSON.stringify({ lockRecord: true, approvalStatusField: 'approval_status' }), |
| 109 | + }, { context: { isSystem: true } } as any); |
| 110 | + |
| 111 | + bindApprovalLockHook(engine as any); |
| 112 | + protocol = new ObjectStackProtocolImplementation(engine as any); |
| 113 | + }); |
| 114 | + |
| 115 | + it('the card\'s second row, verbatim, now carrying 409', async () => { |
| 116 | + const res: any = await protocol.updateManyData({ |
| 117 | + object: 'opportunity', |
| 118 | + records: [{ id: lockedId, data: { amount: 999 } }], |
| 119 | + }); |
| 120 | + |
| 121 | + expect(res.results[0].success).toBe(false); |
| 122 | + expect(res.results[0].errors[0]).toEqual({ |
| 123 | + code: 'RECORD_LOCKED', |
| 124 | + message: `RECORD_LOCKED: record '${lockedId}' of 'opportunity' is locked while an approval is in progress`, |
| 125 | + httpStatus: 409, |
| 126 | + }); |
| 127 | + |
| 128 | + // The refusal was a refusal: nothing reached the store. |
| 129 | + expect((await engine.findOne('opportunity', { where: { id: lockedId } }))?.amount).toBe(100); |
| 130 | + }); |
| 131 | + |
| 132 | + it('the hook really declares its 409 in the `statusCode` spelling ONLY', async () => { |
| 133 | + // Non-vacuity for the row above, taken off the REAL producer rather than |
| 134 | + // asserted about it: if `lockedError` ever grew a `.status`, the row would |
| 135 | + // be green against the pre-#8570 limb too, and this file would stop |
| 136 | + // measuring anything. |
| 137 | + let thrown: any = null; |
| 138 | + try { |
| 139 | + await engine.update('opportunity', { amount: 999 }, { where: { id: lockedId } } as any); |
| 140 | + } catch (e) { thrown = e; } |
| 141 | + |
| 142 | + expect(thrown).not.toBeNull(); |
| 143 | + expect(thrown.code).toBe('RECORD_LOCKED'); |
| 144 | + expect(thrown.statusCode).toBe(409); |
| 145 | + expect(thrown.status).toBeUndefined(); |
| 146 | + expect(Object.getOwnPropertyNames(thrown)).toEqual(['stack', 'message', 'code', 'statusCode']); |
| 147 | + // The production recogniser, on the real throw — and the field the row's |
| 148 | + // limb reads, which is what separates a declared refusal from a fault. |
| 149 | + expect(resolveThrownHttpError(thrown).declaredStatus).toBe(409); |
| 150 | + }); |
| 151 | + |
| 152 | + it('an unlocked row in the SAME batch still succeeds and carries no error', async () => { |
| 153 | + // The asymmetry the card is about is per-row, so the mixed response is the |
| 154 | + // shape a caller actually has to reconcile. |
| 155 | + const res: any = await protocol.updateManyData({ |
| 156 | + object: 'opportunity', |
| 157 | + records: [ |
| 158 | + { id: freeId, data: { amount: 555 } }, |
| 159 | + { id: lockedId, data: { amount: 999 } }, |
| 160 | + ], |
| 161 | + options: { continueOnError: true }, |
| 162 | + }); |
| 163 | + |
| 164 | + expect(res.results[0].success).toBe(true); |
| 165 | + expect(res.results[0].errors).toBeUndefined(); |
| 166 | + expect(res.results[1].errors[0].httpStatus).toBe(409); |
| 167 | + expect((await engine.findOne('opportunity', { where: { id: freeId } }))?.amount).toBe(555); |
| 168 | + }); |
| 169 | + |
| 170 | + it('the batchData upsert loop answers the same way — not just updateManyData', async () => { |
| 171 | + const res: any = await protocol.batchData({ |
| 172 | + object: 'opportunity', |
| 173 | + request: { operation: 'upsert', records: [{ id: lockedId, data: { amount: 999 } }] }, |
| 174 | + }); |
| 175 | + |
| 176 | + expect(res.results[0].errors[0].code).toBe('RECORD_LOCKED'); |
| 177 | + expect(res.results[0].errors[0].httpStatus).toBe(409); |
| 178 | + }); |
| 179 | +}); |
0 commit comments