|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #32768 — `ObjectQL#judgeFieldWriteLock`: the update's own `readonlyWhen` |
| 4 | +// verdict on one field of one record, asked without writing. A door that hands |
| 5 | +// out a promise to write a field later (the upload link) asks it first. |
| 6 | +// |
| 7 | +// Pinned against the write itself: for every row below, the judgement says |
| 8 | +// LOCKED exactly when an update of the field reports it dropped under |
| 9 | +// `readonly_when`, and nothing the judgement does reaches the driver's write |
| 10 | +// verbs. |
| 11 | + |
| 12 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 13 | +import type { DroppedFieldsEvent } from '@objectstack/spec/data'; |
| 14 | +import { ObjectQL } from './engine.js'; |
| 15 | + |
| 16 | +function makeDriver() { |
| 17 | + const stores = new Map<string, Map<string, any>>(); |
| 18 | + const writes: string[] = []; |
| 19 | + const storeFor = (o: string) => { |
| 20 | + let s = stores.get(o); |
| 21 | + if (!s) { s = new Map(); stores.set(o, s); } |
| 22 | + return s; |
| 23 | + }; |
| 24 | + const matches = (row: any, where: any): boolean => { |
| 25 | + if (!where || typeof where !== 'object') return true; |
| 26 | + return Object.entries(where).every(([k, v]: [string, any]) => { |
| 27 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 28 | + return row?.[k] === v; |
| 29 | + }); |
| 30 | + }; |
| 31 | + const driver: any = { |
| 32 | + name: 'memory', version: '0.0.0', supports: {}, |
| 33 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 34 | + async find(object: string, ast: any) { |
| 35 | + const rows = Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where)); |
| 36 | + return typeof ast?.limit === 'number' ? rows.slice(0, ast.limit) : rows; |
| 37 | + }, |
| 38 | + async findOne(object: string, ast: any) { |
| 39 | + for (const r of storeFor(object).values()) if (matches(r, ast?.where)) return r; |
| 40 | + return null; |
| 41 | + }, |
| 42 | + async create(object: string, data: Record<string, unknown>) { |
| 43 | + writes.push(`create ${object}`); |
| 44 | + const row = { ...data, id: data.id as string }; |
| 45 | + storeFor(object).set(row.id, row); |
| 46 | + return row; |
| 47 | + }, |
| 48 | + async update(object: string, id: string, data: Record<string, unknown>) { |
| 49 | + writes.push(`update ${object}`); |
| 50 | + const s = storeFor(object); |
| 51 | + const row = { ...s.get(id), ...data, id }; |
| 52 | + s.set(id, row); |
| 53 | + return row; |
| 54 | + }, |
| 55 | + async updateMany() { writes.push('updateMany'); return 0; }, |
| 56 | + async delete() { writes.push('delete'); return true; }, |
| 57 | + async count() { return 0; }, |
| 58 | + async bulkCreate() { writes.push('bulkCreate'); return []; }, |
| 59 | + async bulkUpdate() { writes.push('bulkUpdate'); return []; }, async bulkDelete() { writes.push('bulkDelete'); }, |
| 60 | + async beginTransaction() { return { __trx: true, commit: async () => {}, rollback: async () => {} }; }, |
| 61 | + async commit() {}, async rollback() {}, |
| 62 | + }; |
| 63 | + return { driver, storeFor, writes }; |
| 64 | +} |
| 65 | + |
| 66 | +const PHRASE = "read-only in the target record's current state (a TRUE `readonlyWhen` predicate)"; |
| 67 | + |
| 68 | +describe('judgeFieldWriteLock: the update\'s readonlyWhen verdict, without the write (#32768)', () => { |
| 69 | + let engine: ObjectQL; |
| 70 | + let d: ReturnType<typeof makeDriver>; |
| 71 | + let warns: string[]; |
| 72 | + |
| 73 | + beforeEach(async () => { |
| 74 | + warns = []; |
| 75 | + const logger: any = { |
| 76 | + warn: (m: string) => warns.push(String(m)), |
| 77 | + debug() {}, info() {}, error() {}, trace() {}, fatal() {}, |
| 78 | + child() { return logger; }, |
| 79 | + }; |
| 80 | + engine = new ObjectQL({ logger }); |
| 81 | + d = makeDriver(); |
| 82 | + engine.registerDriver(d.driver, true); |
| 83 | + await engine.init(); |
| 84 | + |
| 85 | + engine.registry.registerObject({ |
| 86 | + name: 'expense_claim', |
| 87 | + fields: { |
| 88 | + name: { type: 'text' }, |
| 89 | + status: { type: 'text' }, |
| 90 | + invoices: { type: 'file', multiple: true, readonlyWhen: "record.status == 'approved'" }, |
| 91 | + note: { type: 'text' }, |
| 92 | + broken: { type: 'text', readonlyWhen: 'record.no_such_field == 1' }, |
| 93 | + }, |
| 94 | + } as any, 'test'); |
| 95 | + engine.registry.registerObject({ |
| 96 | + name: 'expense_report', |
| 97 | + fields: { name: { type: 'text' }, status: { type: 'text' } }, |
| 98 | + } as any, 'test'); |
| 99 | + engine.registry.registerObject({ |
| 100 | + name: 'expense_line', |
| 101 | + fields: { |
| 102 | + report: { type: 'master_detail', reference: 'expense_report', required: true }, |
| 103 | + receipt: { type: 'file', readonlyWhen: "parent.status == 'paid'" }, |
| 104 | + }, |
| 105 | + } as any, 'test'); |
| 106 | + |
| 107 | + d.storeFor('expense_claim').set('c_draft', { id: 'c_draft', name: 'Lyon', status: 'draft', invoices: null, note: null, broken: null }); |
| 108 | + d.storeFor('expense_claim').set('c_approved', { id: 'c_approved', name: 'Rome', status: 'approved', invoices: null, note: null, broken: null }); |
| 109 | + d.storeFor('expense_report').set('r_paid', { id: 'r_paid', name: 'Q3', status: 'paid' }); |
| 110 | + d.storeFor('expense_report').set('r_open', { id: 'r_open', name: 'Q4', status: 'open' }); |
| 111 | + d.storeFor('expense_line').set('l_paid', { id: 'l_paid', report: 'r_paid', receipt: null }); |
| 112 | + d.storeFor('expense_line').set('l_open', { id: 'l_open', report: 'r_open', receipt: null }); |
| 113 | + }); |
| 114 | + |
| 115 | + /** What the update itself reports for `field` on `id`: dropped under `readonly_when`, or kept. */ |
| 116 | + async function updateDrops(object: string, id: string, field: string, value: unknown = 'f_new'): Promise<boolean> { |
| 117 | + const events: DroppedFieldsEvent[] = []; |
| 118 | + await engine.update(object, { [field]: value }, { |
| 119 | + where: { id }, |
| 120 | + context: { isSystem: true }, |
| 121 | + onFieldsDropped: (e: DroppedFieldsEvent) => { events.push(e); }, |
| 122 | + } as any); |
| 123 | + return events.some((e) => e.reason === 'readonly_when' && e.fields.includes(field)); |
| 124 | + } |
| 125 | + |
| 126 | + it('a field whose readonlyWhen holds now is locked, with the update\'s reason and its strict refusal\'s words', async () => { |
| 127 | + const lock = await engine.judgeFieldWriteLock('expense_claim', 'c_approved', 'invoices', { context: { isSystem: true } as any }); |
| 128 | + expect(lock).toEqual({ reason: 'readonly_when', phrase: PHRASE }); |
| 129 | + expect(d.writes).toEqual([]); |
| 130 | + expect(await updateDrops('expense_claim', 'c_approved', 'invoices', ['f_new'])).toBe(true); |
| 131 | + }); |
| 132 | + |
| 133 | + it('the same field while the condition is false is writable', async () => { |
| 134 | + expect(await engine.judgeFieldWriteLock('expense_claim', 'c_draft', 'invoices', { context: { isSystem: true } as any })).toBeNull(); |
| 135 | + expect(d.writes).toEqual([]); |
| 136 | + expect(await updateDrops('expense_claim', 'c_draft', 'invoices', ['f_new'])).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('a parent-scoped lock reads the master-detail header, as the update does', async () => { |
| 140 | + expect(await engine.judgeFieldWriteLock('expense_line', 'l_paid', 'receipt', { context: { isSystem: true } as any })) |
| 141 | + .toEqual({ reason: 'readonly_when', phrase: PHRASE }); |
| 142 | + expect(await engine.judgeFieldWriteLock('expense_line', 'l_open', 'receipt', { context: { isSystem: true } as any })).toBeNull(); |
| 143 | + expect(d.writes).toEqual([]); |
| 144 | + expect(await updateDrops('expense_line', 'l_paid', 'receipt')).toBe(true); |
| 145 | + expect(await updateDrops('expense_line', 'l_open', 'receipt')).toBe(false); |
| 146 | + }); |
| 147 | + |
| 148 | + it('a field that declares no readonlyWhen is writable and costs no read', async () => { |
| 149 | + const findOne = d.driver.findOne; |
| 150 | + let reads = 0; |
| 151 | + d.driver.findOne = async (...a: any[]) => { reads += 1; return findOne(...a); }; |
| 152 | + expect(await engine.judgeFieldWriteLock('expense_claim', 'c_approved', 'note', { context: { isSystem: true } as any })).toBeNull(); |
| 153 | + expect(reads).toBe(0); |
| 154 | + }); |
| 155 | + |
| 156 | + it('a predicate that faults refuses as the write refuses it, and writes nothing', async () => { |
| 157 | + const err = await engine.judgeFieldWriteLock('expense_claim', 'c_draft', 'broken', { context: { isSystem: true } as any }) |
| 158 | + .then(() => null, (e: unknown) => e as { code?: string; name?: string }); |
| 159 | + expect(err).not.toBeNull(); |
| 160 | + expect(err?.name).toBe('ValidationError'); |
| 161 | + expect(d.writes).toEqual([]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('a record that is not there answers as the by-id update answers it', async () => { |
| 165 | + const err = await engine.judgeFieldWriteLock('expense_claim', 'c_missing', 'invoices', { context: { isSystem: true } as any }) |
| 166 | + .then(() => null, (e: unknown) => e as { code?: string; status?: number }); |
| 167 | + expect(err).toMatchObject({ code: 'RECORD_NOT_FOUND', status: 404 }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('logs no strip line: nothing was written, so "ignoring incoming change" is not true', async () => { |
| 171 | + await engine.judgeFieldWriteLock('expense_claim', 'c_approved', 'invoices', { context: { isSystem: true } as any }); |
| 172 | + expect(warns.filter((w) => w.includes('ignoring incoming change'))).toEqual([]); |
| 173 | + }); |
| 174 | +}); |
0 commit comments