|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0139 D1, E1] The engine's comparand door and a `ReadableIds` leaf. |
| 5 | + * |
| 6 | + * Three answers, each before any driver is asked: |
| 7 | + * |
| 8 | + * - **An unbranded copy is refused `INVALID_FILTER` / 400** — a `where` holding |
| 9 | + * the leaf after a JSON round-trip or a structured clone, and the |
| 10 | + * plain-object spelling a request could write, are a plain object in a list |
| 11 | + * slot. That refusal existed before the leaf did; it is THE refusal. |
| 12 | + * - **A branded leaf passes the door and is refused `INTERNAL_ERROR` / 500** — |
| 13 | + * the engine does not compose the leaf yet (ADR-0139 D2), and no driver |
| 14 | + * evaluates it as written: measured at `a2e94c2a05` with the leaf handed |
| 15 | + * straight to each driver, `driver-memory` threw a raw `TypeError`, |
| 16 | + * `driver-sql` (SQLite) answered `500 DATABASE_ERROR` and |
| 17 | + * `driver-mongodb`'s `translateFilter` emitted it unchanged. So the leaf |
| 18 | + * stops at the door's seam, on every verb and at every position. |
| 19 | + * - **A list keeps working** — the lit control: the recording driver IS |
| 20 | + * reachable, so a refusal's "no read" means the refusal, not a dead harness. |
| 21 | + * |
| 22 | + * The recording driver is this package's witness shape (the in-memory |
| 23 | + * driver's test consumers are a closed census): the door answers before a |
| 24 | + * driver is resolved, so what matters is that no read is recorded. |
| 25 | + */ |
| 26 | + |
| 27 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 28 | +import { ReadableIds } from '@objectstack/spec/data'; |
| 29 | +import { ObjectQL } from './engine.js'; |
| 30 | + |
| 31 | +interface SeenCall { verb: string; object: string; ast: any } |
| 32 | + |
| 33 | +function makeRecordingDriver() { |
| 34 | + const calls: SeenCall[] = []; |
| 35 | + const driver: any = { |
| 36 | + name: 'recording', version: '0.0.0', supports: {}, |
| 37 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 38 | + async find(object: string, ast: any) { calls.push({ verb: 'find', object, ast }); return []; }, |
| 39 | + async findOne(object: string, ast: any) { calls.push({ verb: 'findOne', object, ast }); return null; }, |
| 40 | + async count(object: string, ast: any) { calls.push({ verb: 'count', object, ast }); return 0; }, |
| 41 | + async aggregate(object: string, ast: any) { calls.push({ verb: 'aggregate', object, ast }); return []; }, |
| 42 | + async create(object: string, data: Record<string, unknown>) { calls.push({ verb: 'create', object, ast: data }); return data; }, |
| 43 | + async update(object: string, _id: string, data: Record<string, unknown>) { calls.push({ verb: 'update', object, ast: data }); return data; }, |
| 44 | + async updateMany(object: string, ast: any) { calls.push({ verb: 'updateMany', object, ast }); return 0; }, |
| 45 | + async delete(object: string) { calls.push({ verb: 'delete', object, ast: null }); return true; }, |
| 46 | + async deleteMany(object: string, ast: any) { calls.push({ verb: 'deleteMany', object, ast }); return 0; }, |
| 47 | + async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; }, |
| 48 | + async commit() {}, async rollback() {}, |
| 49 | + }; |
| 50 | + return { driver, calls }; |
| 51 | +} |
| 52 | + |
| 53 | +const CHILD = { |
| 54 | + name: 'rid_child', |
| 55 | + label: 'ReadableIds probe child', |
| 56 | + fields: { |
| 57 | + object_name: { name: 'object_name', type: 'text' }, |
| 58 | + record_id: { name: 'record_id', type: 'text' }, |
| 59 | + note: { name: 'note', type: 'text' }, |
| 60 | + amount: { name: 'amount', type: 'number' }, |
| 61 | + parent: { name: 'parent', type: 'lookup', reference: 'rid_parent' }, |
| 62 | + }, |
| 63 | +}; |
| 64 | +const PARENT = { |
| 65 | + name: 'rid_parent', |
| 66 | + label: 'ReadableIds probe parent', |
| 67 | + fields: { title: { name: 'title', type: 'text' } }, |
| 68 | +}; |
| 69 | + |
| 70 | +type Thrown = (Error & { code?: string; status?: number }) | null; |
| 71 | +const refusalOf = async (p: Promise<unknown>): Promise<Thrown> => p.then(() => null, (e: any) => e); |
| 72 | +const envelopeOf = (err: Thrown) => ({ code: err?.code, status: err?.status }); |
| 73 | + |
| 74 | +const INVALID_FILTER = { code: 'INVALID_FILTER', status: 400 }; |
| 75 | +const UNCOMPOSED = { code: 'INTERNAL_ERROR', status: 500 }; |
| 76 | + |
| 77 | +describe('[ADR-0139 D1] the engine comparand door and a ReadableIds leaf', () => { |
| 78 | + let engine: ObjectQL; |
| 79 | + let calls: SeenCall[]; |
| 80 | + |
| 81 | + beforeEach(async () => { |
| 82 | + const rec = makeRecordingDriver(); |
| 83 | + calls = rec.calls; |
| 84 | + engine = new ObjectQL(); |
| 85 | + engine.registerDriver(rec.driver, true); |
| 86 | + await engine.init(); |
| 87 | + engine.registry.registerObject(PARENT as any, 'test'); |
| 88 | + engine.registry.registerObject(CHILD as any, 'test'); |
| 89 | + calls.length = 0; |
| 90 | + }); |
| 91 | + |
| 92 | + it('a JSON round-trip of a where holding the leaf is refused INVALID_FILTER / 400 — no driver call', async () => { |
| 93 | + const where = { object_name: 'rid_parent', record_id: { $in: ReadableIds.of('rid_parent') } }; |
| 94 | + const wire = JSON.parse(JSON.stringify(where)); |
| 95 | + const err = await refusalOf(engine.find('rid_child', { where: wire })); |
| 96 | + expect(envelopeOf(err)).toEqual(INVALID_FILTER); |
| 97 | + expect(err!.message).toMatch(/^find\('rid_child'\): Operator "\$in" on field "record_id" requires an ARRAY of values\. Received object/); |
| 98 | + expect(calls, 'the refusal precedes the driver').toHaveLength(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a structured clone and a spread of the leaf are refused INVALID_FILTER / 400 — no driver call', async () => { |
| 102 | + const leaf = ReadableIds.of('rid_parent'); |
| 103 | + for (const copy of [structuredClone(leaf), { ...leaf }]) { |
| 104 | + const err = await refusalOf(engine.find('rid_child', { where: { record_id: { $in: copy } } })); |
| 105 | + expect(envelopeOf(err)).toEqual(INVALID_FILTER); |
| 106 | + } |
| 107 | + expect(calls).toHaveLength(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('the plain-object spelling is refused INVALID_FILTER / 400 on every read verb — no driver call', async () => { |
| 111 | + const where = { record_id: { $in: { object: 'rid_parent' } } }; |
| 112 | + const answers = await Promise.all([ |
| 113 | + refusalOf(engine.find('rid_child', { where })), |
| 114 | + refusalOf(engine.findOne('rid_child', { where })), |
| 115 | + refusalOf(engine.count('rid_child', { where })), |
| 116 | + refusalOf(engine.aggregate('rid_child', { where, aggregations: [{ function: 'count', alias: 'n' }] })), |
| 117 | + ]); |
| 118 | + for (const err of answers) expect(envelopeOf(err)).toEqual(INVALID_FILTER); |
| 119 | + expect(calls).toHaveLength(0); |
| 120 | + }); |
| 121 | + |
| 122 | + it('a branded leaf passes the door and is refused INTERNAL_ERROR / 500 as uncomposed, naming its position and object — no driver call', async () => { |
| 123 | + const err = await refusalOf(engine.find('rid_child', { |
| 124 | + where: { object_name: 'rid_parent', record_id: { $in: ReadableIds.of('rid_parent') } }, |
| 125 | + })); |
| 126 | + expect(envelopeOf(err)).toEqual(UNCOMPOSED); |
| 127 | + expect(err!.message).toMatch( |
| 128 | + /^find\('rid_child'\): where\.record_id\.\$in holds a ReadableIds leaf, "the ids of 'rid_parent' this caller may read", and this engine does not compose that leaf yet\. The query was NOT run and no driver was asked/, |
| 129 | + ); |
| 130 | + expect(calls, 'the leaf never reaches a driver').toHaveLength(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('the uncomposed leaf is refused under $and / $or / $not and on every verb — no driver call', async () => { |
| 134 | + const leaf = () => ReadableIds.of('rid_parent'); |
| 135 | + const cases: ReadonlyArray<readonly [string, () => Promise<unknown>, string]> = [ |
| 136 | + ['find $or', () => engine.find('rid_child', { where: { $or: [{ note: 'x' }, { record_id: { $in: leaf() } }] } }), 'where.$or[1].record_id.$in'], |
| 137 | + ['find $not', () => engine.find('rid_child', { where: { $not: { record_id: { $in: leaf() } } } }), 'where.$not.record_id.$in'], |
| 138 | + ['find $and', () => engine.find('rid_child', { where: { $and: [{ object_name: 'rid_parent' }, { record_id: { $in: leaf() } }] } }), 'where.$and[1].record_id.$in'], |
| 139 | + ['findOne', () => engine.findOne('rid_child', { where: { record_id: { $in: leaf() } } }), 'where.record_id.$in'], |
| 140 | + ['count', () => engine.count('rid_child', { where: { record_id: { $in: leaf() } } }), 'where.record_id.$in'], |
| 141 | + ['aggregate where', () => engine.aggregate('rid_child', { where: { record_id: { $in: leaf() } }, aggregations: [{ function: 'count', alias: 'n' }] }), 'where.record_id.$in'], |
| 142 | + ['aggregate aggregation filter', () => engine.aggregate('rid_child', { aggregations: [{ function: 'count', alias: 'n', filter: { record_id: { $in: leaf() } } }] }), 'aggregations[0].filter.record_id.$in'], |
| 143 | + ['update multi', () => engine.update('rid_child', { note: 'y' }, { where: { record_id: { $in: leaf() } }, multi: true }), 'where.record_id.$in'], |
| 144 | + ['delete multi', () => engine.delete('rid_child', { where: { record_id: { $in: leaf() } }, multi: true }), 'where.record_id.$in'], |
| 145 | + ]; |
| 146 | + for (const [label, run, at] of cases) { |
| 147 | + const err = await refusalOf(run()); |
| 148 | + expect(envelopeOf(err), label).toEqual(UNCOMPOSED); |
| 149 | + expect(err!.message, label).toContain(`: ${at} holds a ReadableIds leaf`); |
| 150 | + } |
| 151 | + expect(calls, 'no verb reached the driver').toHaveLength(0); |
| 152 | + }); |
| 153 | + |
| 154 | + it('a leaf at a position the shape door does not judge is refused too — equality, a list member, a range endpoint, a nested-relation condition', async () => { |
| 155 | + const leaf = () => ReadableIds.of('rid_parent'); |
| 156 | + const cases: ReadonlyArray<readonly [Record<string, unknown>, string]> = [ |
| 157 | + [{ record_id: leaf() }, 'where.record_id'], |
| 158 | + [{ record_id: { $eq: leaf() } }, 'where.record_id.$eq'], |
| 159 | + [{ record_id: { $in: ['p1', leaf()] } }, 'where.record_id.$in[1]'], |
| 160 | + [{ amount: { $between: [1, leaf()] } }, 'where.amount.$between[1]'], |
| 161 | + [{ parent: { title: leaf() } }, 'where.parent.title'], |
| 162 | + ]; |
| 163 | + for (const [where, at] of cases) { |
| 164 | + const err = await refusalOf(engine.find('rid_child', { where })); |
| 165 | + expect(envelopeOf(err), at).toEqual(UNCOMPOSED); |
| 166 | + expect(err!.message, at).toContain(`: ${at} holds a ReadableIds leaf`); |
| 167 | + } |
| 168 | + expect(calls).toHaveLength(0); |
| 169 | + }); |
| 170 | + |
| 171 | + it('LIT CONTROL — a list comparand on the same field reaches the driver', async () => { |
| 172 | + await engine.find('rid_child', { where: { record_id: { $in: ['p1', 'p2'] } } }); |
| 173 | + expect(calls.map((c) => c.verb)).toEqual(['find']); |
| 174 | + expect(calls[0].ast.where).toEqual({ record_id: { $in: ['p1', 'p2'] } }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments