|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27351] The nested-relation condition refusal at `where` states what to |
| 5 | + * write instead INSIDE the 500 characters a client reads, for every reason it |
| 6 | + * refuses a condition. |
| 7 | + * |
| 8 | + * The cap stays: `mapDataError` (`@objectstack/types`) cuts a 4xx message at |
| 9 | + * `CLIENT_MESSAGE_MAX` with `…`. So `relation-filter-lowering.ts` orders each |
| 10 | + * reason's words head, the short reason, the route, then "NOT applied" and any |
| 11 | + * reasoning, and this file reads every reason the way a client does — through |
| 12 | + * `mapDataError` — and requires the whole route in what survives the cut, |
| 13 | + * ahead of "NOT applied". Run against the words as they stood on `origin/main` |
| 14 | + * `cf218eed1`, the second-level and dotted-key routes are cut on the card's |
| 15 | + * fixture, and every reason's route is cut on the stress fixture. |
| 16 | + * |
| 17 | + * Two fixtures. The card's — a 21-character object, a 21-character field, a |
| 18 | + * 16-character target, three keys, a path through a nested `$and` / `$or` — |
| 19 | + * must end each route {@link MARGIN} characters inside the cut. The stress |
| 20 | + * one — a 27-character object and target, a 32-character field, four 11- to |
| 21 | + * 15-character keys, a three-deep path, single- and multi-valued — must keep |
| 22 | + * each route whole inside the cut. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect } from 'vitest'; |
| 26 | +import { mapDataError } from '@objectstack/types'; |
| 27 | +import { narrowNumberComparands } from './number-comparand-declared-type-door.js'; |
| 28 | + |
| 29 | +/** The characters a client reads before the `…`: `CLIENT_MESSAGE_MAX - 1`. */ |
| 30 | +const CLIENT_CUT = 499; |
| 31 | +/** How far inside the cut every route on the card's fixture must end. */ |
| 32 | +const MARGIN = 40; |
| 33 | + |
| 34 | +interface Fixture { |
| 35 | + readonly object: string; |
| 36 | + readonly target: string; |
| 37 | + readonly field: string; |
| 38 | + readonly keys: readonly string[]; |
| 39 | + readonly multiple: boolean; |
| 40 | + /** Wraps the condition in the fixture's path. */ |
| 41 | + readonly wrap: (field: string, condition: unknown) => unknown; |
| 42 | + /** The latest a route may end. */ |
| 43 | + readonly limit: number; |
| 44 | +} |
| 45 | + |
| 46 | +const CARD: Fixture = { |
| 47 | + object: 'customer_support_case', |
| 48 | + target: 'customer_account', |
| 49 | + field: 'primary_account_owner', |
| 50 | + keys: ['region', 'tier', 'status'], |
| 51 | + multiple: false, |
| 52 | + wrap: (field, condition) => ({ $and: [{ amount: 1 }, { $or: [{ [field]: condition }] }] }), |
| 53 | + limit: CLIENT_CUT - MARGIN, |
| 54 | +}; |
| 55 | + |
| 56 | +const STRESS: Fixture = { |
| 57 | + object: 'customer_support_case_entry', |
| 58 | + target: 'enterprise_customer_account', |
| 59 | + field: 'primary_customer_account_manager', |
| 60 | + keys: ['region_code', 'service_tier', 'account_status', 'billing_country'], |
| 61 | + multiple: false, |
| 62 | + wrap: (field, condition) => ({ |
| 63 | + $and: [{ amount: 1 }, { $or: [{ $and: [{ amount: 2 }, { [field]: condition }] }] }], |
| 64 | + }), |
| 65 | + limit: CLIENT_CUT, |
| 66 | +}; |
| 67 | + |
| 68 | +const STRESS_MULTI: Fixture = { ...STRESS, multiple: true }; |
| 69 | + |
| 70 | +/** The opening and the closing words of each reason's route. */ |
| 71 | +function routes(f: Fixture): Record<string, readonly [string, string]> { |
| 72 | + const ids = f.multiple ? '"$contains": ID } }, one per id.' : '"$in": [ID, …] } }.'; |
| 73 | + return { |
| 74 | + secondLevel: [`Read '${f.target}' with { "parent_account": { … } }`, ids], |
| 75 | + dottedKey: [`Read '${f.target}' with that condition`, ids], |
| 76 | + empty: ['Name one (', '"$null": false } }.'], |
| 77 | + undeclaredKey: ['Name a field it declares', '"FIELD": VALUE } }.'], |
| 78 | + unregistered: ['Match ', ids], |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +/** The refusal a client reads: status, code, the bounded `error` text, and the full message. */ |
| 83 | +function clientReads(fn: () => unknown): { status: number; code: unknown; text: string; full: string } { |
| 84 | + let thrown: unknown; |
| 85 | + try { fn(); } catch (e) { thrown = e; } |
| 86 | + expect(thrown, 'the door refused').toBeInstanceOf(Error); |
| 87 | + const { status, body } = mapDataError(thrown); |
| 88 | + return { status, code: body.code, text: String(body.error), full: (thrown as Error).message }; |
| 89 | +} |
| 90 | + |
| 91 | +/** The route is whole in what the client reads, ends by the fixture's limit, and precedes "NOT applied". */ |
| 92 | +function expectRouteInside(f: Fixture, fn: () => unknown, [open, close]: readonly [string, string]): void { |
| 93 | + const read = clientReads(fn); |
| 94 | + expect(read.status).toBe(400); |
| 95 | + expect(read.code).toBe('INVALID_FILTER'); |
| 96 | + const start = read.full.indexOf(open); |
| 97 | + expect(start, read.full).toBeGreaterThan(0); |
| 98 | + const closeAt = read.full.indexOf(close, start); |
| 99 | + expect(closeAt, read.full).toBeGreaterThan(start); |
| 100 | + const end = closeAt + close.length; |
| 101 | + expect(read.text, 'the client text holds the whole route').toContain(read.full.slice(start, end)); |
| 102 | + expect(end, `the route ends at ${end}; the limit is ${f.limit}`).toBeLessThanOrEqual(f.limit); |
| 103 | + const notApplied = read.full.indexOf(' was NOT applied'); |
| 104 | + expect(notApplied, read.full).toBeGreaterThan(end - 1); |
| 105 | +} |
| 106 | + |
| 107 | +/** Refuse `condition` beneath the fixture's relation field, at `where`. */ |
| 108 | +function refusal(f: Fixture, condition: Record<string, unknown>, registered = true) { |
| 109 | + const related = { |
| 110 | + fields: { |
| 111 | + ...Object.fromEntries(f.keys.map((key) => [key, { name: key, type: 'text' }])), |
| 112 | + parent_account: { name: 'parent_account', type: 'lookup', reference: f.target }, |
| 113 | + }, |
| 114 | + }; |
| 115 | + const schema = { |
| 116 | + fields: { |
| 117 | + [f.field]: { name: f.field, type: 'lookup', reference: f.target, ...(f.multiple ? { multiple: true } : {}) }, |
| 118 | + amount: { name: 'amount', type: 'number' }, |
| 119 | + }, |
| 120 | + }; |
| 121 | + const schemaOf = (name: string) => (registered && name === f.target ? related : undefined); |
| 122 | + return () => narrowNumberComparands(f.object, 'find', schema, f.wrap(f.field, condition), 'where', { schemaOf }); |
| 123 | +} |
| 124 | + |
| 125 | +const conditionOf = (f: Fixture) => Object.fromEntries(f.keys.map((key) => [key, 'x'])); |
| 126 | + |
| 127 | +describe('[#27351] the nested-relation condition refusal: every route lands inside the client cap', () => { |
| 128 | + for (const [label, f] of [['the card', CARD], ['stress, single-valued', STRESS], ['stress, multi-valued', STRESS_MULTI]] as const) { |
| 129 | + describe(label, () => { |
| 130 | + const route = routes(f); |
| 131 | + const condition = conditionOf(f); |
| 132 | + |
| 133 | + it('a second-level relation', () => { |
| 134 | + expectRouteInside(f, refusal(f, { ...condition, parent_account: { [f.keys[0]]: 'x' } }), route.secondLevel); |
| 135 | + }); |
| 136 | + |
| 137 | + it('a dotted key', () => { |
| 138 | + expectRouteInside(f, refusal(f, { ...condition, 'parent_account.name': 'x' }), route.dottedKey); |
| 139 | + }); |
| 140 | + |
| 141 | + it('an empty condition', () => { |
| 142 | + expectRouteInside(f, refusal(f, {}), route.empty); |
| 143 | + }); |
| 144 | + |
| 145 | + it('a key the related object does not declare', () => { |
| 146 | + expectRouteInside(f, refusal(f, { ...condition, regio: 'x' }), route.undeclaredKey); |
| 147 | + }); |
| 148 | + |
| 149 | + it('a related object not registered here', () => { |
| 150 | + expectRouteInside(f, refusal(f, condition, false), route.unregistered); |
| 151 | + }); |
| 152 | + }); |
| 153 | + } |
| 154 | +}); |
0 commit comments