|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20918] A `$not` over a multi-valued lookup gets the engine's rows on BOTH |
| 5 | + * analytics strategies, at the cube read and at the dataset door. |
| 6 | + * |
| 7 | + * The `where` door makes every leaf of a `$not` operand total before negating |
| 8 | + * it (#5146): `{ $not: { owners: { $contains: 'u1' } } }` is lowered with a |
| 9 | + * `{ owners: { $null: false } }` conjunct beside the `$contains`, so a row |
| 10 | + * whose `owners` holds no value is answered by the negation instead of falling |
| 11 | + * into SQL's UNKNOWN. The engine-aggregate strategy handed that guard to the |
| 12 | + * engine as `{ $ne: null }`, and the negative-polarity escape (#5298) as the |
| 13 | + * bare `{ owners: null }`. `driver-sql` refuses both over a multi-valued |
| 14 | + * lookup's JSON column, so the engine-aggregate path answered |
| 15 | + * `400 INVALID_FILTER` where the engine and the native strategy answer rows. |
| 16 | + * The guard now reaches the engine as `{ $null: false }` / `{ $null: true }`, |
| 17 | + * the spellings the engine's own lowering emits for the same guard. |
| 18 | + * |
| 19 | + * The composition is the shipped one: `SecurityPlugin` over a real `ObjectQL` |
| 20 | + * on a real `SqlDriver` (SQLite), and `AnalyticsServicePlugin` over the same |
| 21 | + * engine as its `'data'` service. Two compositions, one per strategy: |
| 22 | + * |
| 23 | + * - `native` — the plugin's own capabilities, so `NativeSQLStrategy` answers |
| 24 | + * (it compiles its own SQL, and its answers are unchanged by this fix); |
| 25 | + * - `objectql` — the capabilities narrowed to the engine-aggregate path, so |
| 26 | + * `ObjectQLStrategy` answers every query. |
| 27 | + * |
| 28 | + * Two faces per composition: the cube read (`AnalyticsService.query`, what |
| 29 | + * `POST /api/v1/analytics/query` relays) over the object's inferred cube, and |
| 30 | + * the dataset door through this package's own route |
| 31 | + * (`POST /api/v1/analytics/dataset/query`, the caller's filter as |
| 32 | + * `selection.runtimeFilter`). |
| 33 | + * |
| 34 | + * ## Measured on the base (`9b0de7de7`), this fixture |
| 35 | + * |
| 36 | + * | `where` | `engine.find` | native | objectql (base) | |
| 37 | + * |:--|:--|:--|:--| |
| 38 | + * | `{ $not: { owners: { $contains: 'u1' } } }` | b, d, e | b, d, e | 400 `INVALID_FILTER` (`$ne`) | |
| 39 | + * | `{ $not: { owners: { $notContains: 'u1' } } }` | a, c | a, c | 400 (bare equality) | |
| 40 | + * | `{ owners: { $notContains: 'u1' } }` | b, d, e | b, d, e | 400 (bare equality) | |
| 41 | + * | `{ owners: { $null: false } }` | a, b, c, d | a, b, c, d | 400 (`$ne`) | |
| 42 | + * | `{ owners: { $null: true } }` | e | e | 400 (bare equality) | |
| 43 | + * | `{ $not: { owner: { $contains: 'u1' } } }` (single-valued) | b, d, e | b, d, e | b, d, e | |
| 44 | + * |
| 45 | + * The engine's answer, computed in the same test over the same rows, is the |
| 46 | + * reference for every cell. SQLite only: the strategy's spelling is the |
| 47 | + * change, and the driver that refused it is `driver-sql`. |
| 48 | + */ |
| 49 | + |
| 50 | +import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; |
| 51 | +import type { FilterCondition } from '@objectstack/spec/data'; |
| 52 | +import { PermissionSetSchema } from '@objectstack/spec/security'; |
| 53 | +import { ObjectQL } from '@objectstack/objectql'; |
| 54 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 55 | +import { SecurityPlugin } from '@objectstack/plugin-security'; |
| 56 | +import { AnalyticsServicePlugin, type AnalyticsService } from '@objectstack/service-analytics'; |
| 57 | +import { RestServer } from './rest-server'; |
| 58 | + |
| 59 | +const OBJECT = 'rest_an_notmv_ledger'; |
| 60 | +const OWNER = 'rest_an_notmv_owner'; |
| 61 | + |
| 62 | +const SYS_CTX = { isSystem: true, userId: 'usr_system' }; |
| 63 | + |
| 64 | +const MEMBER_SET = PermissionSetSchema.parse({ |
| 65 | + name: 'member_default', |
| 66 | + label: 'Member', |
| 67 | + objects: { '*': { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true } }, |
| 68 | +}); |
| 69 | + |
| 70 | +const MEMBER_CTX = { userId: 'usr_member', positions: [], permissions: [MEMBER_SET.name], posture: 'MEMBER' }; |
| 71 | + |
| 72 | +const OWNERS = [ |
| 73 | + { id: 'u1', region: 'NA' }, |
| 74 | + { id: 'u2', region: 'EU' }, |
| 75 | + { id: 'u3', region: 'AP' }, |
| 76 | +]; |
| 77 | +/** `e` holds no value in either relation: the row the `$not` guard is about. */ |
| 78 | +const ROWS = [ |
| 79 | + { id: 'd1', title: 'a', owner: 'u1', owners: ['u1'] }, |
| 80 | + { id: 'd2', title: 'b', owner: 'u2', owners: ['u2'] }, |
| 81 | + { id: 'd3', title: 'c', owner: 'u1', owners: ['u2', 'u1'] }, |
| 82 | + { id: 'd4', title: 'd', owner: 'u3', owners: ['u3'] }, |
| 83 | + { id: 'd5', title: 'e', owner: null, owners: null }, |
| 84 | +]; |
| 85 | +const TITLE_OF = new Map(ROWS.map((r) => [r.id, r.title])); |
| 86 | + |
| 87 | +/** |
| 88 | + * Conditions the engine SERVES, with the rows it answers on this fixture |
| 89 | + * (asserted too, so a moved reference cannot pass silently). Every face must |
| 90 | + * give the engine's rows. |
| 91 | + */ |
| 92 | +const SERVED: ReadonlyArray<readonly [string, FilterCondition, string[]]> = [ |
| 93 | + ['$not over $contains, multi-valued', { $not: { owners: { $contains: 'u1' } } }, ['b', 'd', 'e']], |
| 94 | + ['$not over $notContains, multi-valued', { $not: { owners: { $notContains: 'u1' } } }, ['a', 'c']], |
| 95 | + ['$notContains, multi-valued', { owners: { $notContains: 'u1' } }, ['b', 'd', 'e']], |
| 96 | + ['has a value, multi-valued', { owners: { $null: false } }, ['a', 'b', 'c', 'd']], |
| 97 | + ['has no value, multi-valued', { owners: { $null: true } }, ['e']], |
| 98 | + ['CONTROL $not over $contains, single-valued', { $not: { owner: { $contains: 'u1' } } }, ['b', 'd', 'e']], |
| 99 | +]; |
| 100 | + |
| 101 | +/** The inline dataset the dataset door queries: the ledger, grouped by title. */ |
| 102 | +const DATASET = { |
| 103 | + name: 'not_multivalue_inline', |
| 104 | + label: 'Not multi-valued inline', |
| 105 | + object: OBJECT, |
| 106 | + dimensions: [{ name: 'title_dim', field: 'title', type: 'string' }], |
| 107 | + measures: [{ name: 'row_count', aggregate: 'count' }], |
| 108 | +}; |
| 109 | + |
| 110 | +const quiet: any = { debug() {}, info() {}, warn() {}, error() {}, child() { return quiet; } }; |
| 111 | + |
| 112 | +function createMockServer() { |
| 113 | + const noop = () => {}; |
| 114 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 115 | +} |
| 116 | + |
| 117 | +function mockProtocol() { |
| 118 | + return { |
| 119 | + getDiscovery: async () => ({ version: 'v0', routes: { data: '', metadata: '' } }), |
| 120 | + getMetaTypes: async () => [], |
| 121 | + getMetaItems: async () => [], |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +function makeRes() { |
| 126 | + const res: any = { |
| 127 | + statusCode: 200, |
| 128 | + body: undefined as any, |
| 129 | + header: () => res, |
| 130 | + status: (code: number) => { res.statusCode = code; return res; }, |
| 131 | + json: (body: unknown) => { res.body = body; return res; }, |
| 132 | + end: () => res, |
| 133 | + }; |
| 134 | + return res; |
| 135 | +} |
| 136 | + |
| 137 | +type Thrown = { code?: string; status?: number; statusCode?: number; message?: string } | null; |
| 138 | +const envelopeOf = (e: Thrown) => ({ code: e?.code, status: e?.status ?? e?.statusCode }); |
| 139 | +const titlesOf = (rows: ReadonlyArray<Record<string, unknown>>, key: string) => |
| 140 | + rows.map((r) => String(r[key])).sort(); |
| 141 | + |
| 142 | +interface Harness { |
| 143 | + engine: ObjectQL; |
| 144 | + service: AnalyticsService; |
| 145 | + dataset: (runtimeFilter: FilterCondition) => Promise<{ status: number; body: any }>; |
| 146 | +} |
| 147 | + |
| 148 | +/** Boot the shipped composition; `caps` narrows the strategy set to the engine-aggregate path. */ |
| 149 | +async function boot(caps?: 'objectql'): Promise<Harness> { |
| 150 | + const engine = new ObjectQL({ logger: quiet } as any); |
| 151 | + engine.registerDriver( |
| 152 | + new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any), |
| 153 | + true, |
| 154 | + ); |
| 155 | + await engine.init(); |
| 156 | + engine.registerApp({ |
| 157 | + id: 'com.objectstack.qa.analytics-not-multivalue-20918', |
| 158 | + name: 'Analytics $not over a multi-valued lookup', |
| 159 | + version: '1.0.0', |
| 160 | + type: 'plugin', |
| 161 | + scope: 'system', |
| 162 | + objects: [ |
| 163 | + { |
| 164 | + name: OWNER, |
| 165 | + label: 'Owner', |
| 166 | + sharingModel: 'public_read_write', |
| 167 | + fields: { region: { name: 'region', type: 'text' } }, |
| 168 | + }, |
| 169 | + { |
| 170 | + name: OBJECT, |
| 171 | + label: 'Ledger', |
| 172 | + sharingModel: 'public_read_write', |
| 173 | + fields: { |
| 174 | + title: { name: 'title', type: 'text' }, |
| 175 | + owner: { name: 'owner', type: 'lookup', reference: OWNER }, |
| 176 | + owners: { name: 'owners', type: 'lookup', reference: OWNER, multiple: true }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + ], |
| 180 | + } as never); |
| 181 | + await engine.syncSchemas(); |
| 182 | + |
| 183 | + const services: Record<string, unknown> = { |
| 184 | + manifest: { register: vi.fn() }, |
| 185 | + objectql: engine, |
| 186 | + data: engine, |
| 187 | + metadata: { |
| 188 | + get: async (_type: string, name: string) => engine.getSchema(name) ?? null, |
| 189 | + list: async () => [MEMBER_SET], |
| 190 | + }, |
| 191 | + }; |
| 192 | + const ctx: any = { |
| 193 | + logger: quiet, |
| 194 | + hook: () => {}, |
| 195 | + registerService: (name: string, svc: unknown) => { services[name] = svc; }, |
| 196 | + replaceService: (name: string, svc: unknown) => { services[name] = svc; }, |
| 197 | + getService: (name: string) => { |
| 198 | + if (!(name in services)) throw new Error(`service not registered: ${name}`); |
| 199 | + return services[name]; |
| 200 | + }, |
| 201 | + }; |
| 202 | + const security = new SecurityPlugin({ fallbackPermissionSet: 'member_default' }); |
| 203 | + await security.init(ctx); |
| 204 | + await security.start(ctx); |
| 205 | + vi.spyOn((engine as unknown as { logger: { warn: () => void } }).logger, 'warn').mockImplementation(() => undefined); |
| 206 | + |
| 207 | + await engine.insert(OWNER, OWNERS.map((r) => ({ ...r })), { context: SYS_CTX } as never); |
| 208 | + await engine.insert(OBJECT, ROWS.map((r) => ({ ...r })), { context: SYS_CTX } as never); |
| 209 | + |
| 210 | + await new AnalyticsServicePlugin( |
| 211 | + caps === 'objectql' |
| 212 | + ? { queryCapabilities: () => ({ nativeSql: false, objectqlAggregate: true, inMemory: false }) } |
| 213 | + : {}, |
| 214 | + ).init(ctx); |
| 215 | + const service = services.analytics as AnalyticsService; |
| 216 | + |
| 217 | + const rest = new RestServer( |
| 218 | + createMockServer() as any, mockProtocol() as any, { api: { requireAuth: false } } as any, |
| 219 | + undefined, undefined, undefined, undefined, undefined, undefined, undefined, |
| 220 | + undefined, undefined, undefined, undefined, |
| 221 | + async () => service, |
| 222 | + ); |
| 223 | + (rest as any).resolveExecCtx = async () => MEMBER_CTX; |
| 224 | + rest.registerRoutes(); |
| 225 | + const route = rest.getRoutes().find((r: any) => r.method === 'POST' && r.path === '/api/v1/analytics/dataset/query'); |
| 226 | + expect(route).toBeDefined(); |
| 227 | + const dataset = async (runtimeFilter: FilterCondition) => { |
| 228 | + const res = makeRes(); |
| 229 | + const selection = { measures: ['row_count'], dimensions: ['title_dim'], runtimeFilter }; |
| 230 | + const body = JSON.parse(JSON.stringify({ dataset: DATASET, selection })); |
| 231 | + await route!.handler({ method: 'POST', params: {}, headers: {}, body, query: {} } as any, res); |
| 232 | + return { status: res.statusCode, body: res.body }; |
| 233 | + }; |
| 234 | + return { engine, service, dataset }; |
| 235 | +} |
| 236 | + |
| 237 | +/** The engine's answer for a `where`, as the member: its titles, or its refusal. */ |
| 238 | +async function engineAnswer(engine: ObjectQL, where: FilterCondition) { |
| 239 | + return engine.find(OBJECT, { where, context: MEMBER_CTX } as never).then( |
| 240 | + (rows: any[]) => ({ titles: rows.map((r) => TITLE_OF.get(r.id) as string).sort() }), |
| 241 | + (e: Thrown) => ({ refused: envelopeOf(e) }), |
| 242 | + ); |
| 243 | +} |
| 244 | + |
| 245 | +const cubeQuery = (where: FilterCondition) => |
| 246 | + ({ cube: OBJECT, measures: ['count'], dimensions: ['title'], where }) as never; |
| 247 | + |
| 248 | +for (const strategy of ['native', 'objectql'] as const) { |
| 249 | + describe(`[#20918] $not over a multi-valued lookup answers the engine's rows — ${strategy} composition`, () => { |
| 250 | + let h: Harness; |
| 251 | + |
| 252 | + beforeAll(async () => { |
| 253 | + h = await boot(strategy === 'objectql' ? 'objectql' : undefined); |
| 254 | + }, 60_000); |
| 255 | + |
| 256 | + afterAll(async () => { |
| 257 | + try { await h?.engine.destroy(); } catch { /* noop */ } |
| 258 | + }); |
| 259 | + |
| 260 | + for (const [label, where, rows] of SERVED) { |
| 261 | + it(`${label}: the cube read and the dataset door answer the engine's rows`, async () => { |
| 262 | + const reference = await engineAnswer(h.engine, where); |
| 263 | + expect(reference, `${label}: the engine`).toEqual({ titles: rows }); |
| 264 | + |
| 265 | + const cube = await h.service.query(cubeQuery(where), MEMBER_CTX as never).then( |
| 266 | + (r) => ({ titles: titlesOf(r.rows, 'title') }), |
| 267 | + (e: Thrown) => ({ refused: envelopeOf(e), message: e?.message }), |
| 268 | + ); |
| 269 | + expect(cube, `${label}: the cube read`).toEqual(reference); |
| 270 | + |
| 271 | + const ds = await h.dataset(where); |
| 272 | + expect(ds.status, `${label}: the dataset door ${JSON.stringify(ds.body)}`).toBe(200); |
| 273 | + expect(titlesOf(ds.body.rows, 'title_dim'), `${label}: the dataset door`).toEqual(rows); |
| 274 | + }); |
| 275 | + } |
| 276 | + }); |
| 277 | +} |
0 commit comments