|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The engine's own sort door and the Data API's sort door answer one question |
| 5 | + * — is this field's TYPE computed on read, with no column to order by — from |
| 6 | + * one source, `isVirtualSearchField` (`@objectstack/spec/data`). |
| 7 | + * |
| 8 | + * - The engine door, `assertOrderByIsMaterializable`, guards a caller that |
| 9 | + * reaches `ObjectQL.find` / `findOne` directly (a hook, a flow). |
| 10 | + * - The Data API door, `assertSortFieldsExist`'s third verdict |
| 11 | + * (`@objectstack/metadata-protocol`), guards everything reaching `findData`. |
| 12 | + * |
| 13 | + * The engine door used to test a `'formula'` literal of its own while the |
| 14 | + * Data API door read the spec's set. Both held `formula` alone, so they agreed |
| 15 | + * only while the spec's set did not grow. The day it grows without the engine, |
| 16 | + * the Data API refuses the new type and an in-process `find` sends the ORDER BY |
| 17 | + * to a driver with no column for it, which drops the sort and answers success. |
| 18 | + * |
| 19 | + * So this file copies no verdict table. For EVERY member of the spec's |
| 20 | + * `FieldType` it asks both doors and holds them to each other. It tells the |
| 21 | + * Data API door's third verdict from its fourth (an author's |
| 22 | + * `sortable: false`, read through the projection) without reading a sentence: |
| 23 | + * the sort names the field under test AND a companion that declares |
| 24 | + * `sortable: false`, and the third verdict names the computed field alone, |
| 25 | + * where the fourth names the companion. The engine door does not judge |
| 26 | + * `sortable: false` at all, so the companion never moves its answer. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, vi } from 'vitest'; |
| 30 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 31 | +import { FieldType } from '@objectstack/spec/data'; |
| 32 | +import { assertOrderByIsMaterializable } from './engine-top-level.js'; |
| 33 | + |
| 34 | +const OBJECT = 'engine_sort_agreement'; |
| 35 | + |
| 36 | +/** Every field type the spec knows, as the enum lists them. */ |
| 37 | +const FIELD_TYPES: readonly string[] = FieldType.options; |
| 38 | + |
| 39 | +const fieldOf = (type: string) => `f_${type}`; |
| 40 | + |
| 41 | +/** A stored column whose author declared it unsortable: the Data API door's fourth verdict, by construction. */ |
| 42 | +const COMPANION = 'declared_unsortable'; |
| 43 | + |
| 44 | +const DEFINITION = { |
| 45 | + name: OBJECT, |
| 46 | + fields: { |
| 47 | + ...Object.fromEntries(FIELD_TYPES.map((type) => [fieldOf(type), { name: fieldOf(type), type }])), |
| 48 | + [COMPANION]: { name: COMPANION, type: 'text', sortable: false }, |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +const orderByOf = (type: string) => [ |
| 53 | + { field: fieldOf(type), order: 'asc' as const }, |
| 54 | + { field: COMPANION, order: 'asc' as const }, |
| 55 | +]; |
| 56 | + |
| 57 | +type Refusal = { code?: unknown; status?: unknown; field?: unknown; fields?: unknown }; |
| 58 | +const envelopeOf = (err: unknown) => { |
| 59 | + const r = err as Refusal; |
| 60 | + return { code: r.code, status: r.status, field: r.field, fields: r.fields }; |
| 61 | +}; |
| 62 | + |
| 63 | +/** The engine door's answer: the refusal it threw, or null when it lets the sort through. */ |
| 64 | +function engineDoor(type: string) { |
| 65 | + try { |
| 66 | + assertOrderByIsMaterializable(OBJECT, 'find', DEFINITION, orderByOf(type)); |
| 67 | + } catch (err) { |
| 68 | + return envelopeOf(err); |
| 69 | + } |
| 70 | + return null; |
| 71 | +} |
| 72 | + |
| 73 | +/** The Data API door's answer to the same sort, over the same definition. */ |
| 74 | +async function dataApiDoor(type: string) { |
| 75 | + const find = vi.fn(async () => []); |
| 76 | + const engine = { registry: { getObject: (name: string) => (name === OBJECT ? DEFINITION : undefined) }, find }; |
| 77 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 78 | + try { |
| 79 | + await protocol.findData({ object: OBJECT, query: { orderBy: orderByOf(type) } }); |
| 80 | + } catch (err) { |
| 81 | + return envelopeOf(err); |
| 82 | + } |
| 83 | + return null; |
| 84 | +} |
| 85 | + |
| 86 | +/** The computed-type verdict, as an envelope: what both doors answer for a computed field. */ |
| 87 | +const computedVerdict = (type: string) => ({ |
| 88 | + code: 'INVALID_SORT', status: 400, field: fieldOf(type), fields: [fieldOf(type)], |
| 89 | +}); |
| 90 | + |
| 91 | +const engineCallsComputed = (type: string) => engineDoor(type) !== null; |
| 92 | + |
| 93 | +describe('the engine sort door and the Data API sort door agree on which field types are computed', () => { |
| 94 | + it('the table is not empty on either side: the engine door refuses some types and lets others through', () => { |
| 95 | + // Without both, every row below would agree about nothing. |
| 96 | + expect(FIELD_TYPES.filter(engineCallsComputed), 'field types the engine door refuses').not.toEqual([]); |
| 97 | + expect(FIELD_TYPES.filter((type) => !engineCallsComputed(type)), 'field types the engine door lets through') |
| 98 | + .not.toEqual([]); |
| 99 | + }); |
| 100 | + |
| 101 | + it.each(FIELD_TYPES)('a `%s` field: the engine door refuses it exactly when the Data API door calls it computed', async (type) => { |
| 102 | + const engine = engineDoor(type); |
| 103 | + const dataApi = await dataApiDoor(type); |
| 104 | + // The Data API door always refuses this sort (the companion is unsortable); |
| 105 | + // the verdict that fires says which side of the question the type is on. |
| 106 | + const dataApiCallsComputed = JSON.stringify(dataApi?.fields) === JSON.stringify([fieldOf(type)]); |
| 107 | + expect({ type, engineCallsComputed: engine !== null }).toEqual({ type, engineCallsComputed: dataApiCallsComputed }); |
| 108 | + if (dataApiCallsComputed) { |
| 109 | + // One wire envelope on both doors for the one condition. |
| 110 | + expect(engine).toEqual(computedVerdict(type)); |
| 111 | + expect(dataApi).toEqual(computedVerdict(type)); |
| 112 | + } else { |
| 113 | + // CONTROL: the Data API refused for the companion, by its fourth verdict. |
| 114 | + expect(dataApi).toEqual({ code: 'INVALID_SORT', status: 400, field: COMPANION, fields: [COMPANION] }); |
| 115 | + } |
| 116 | + }); |
| 117 | +}); |
0 commit comments