|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#5347] `$null` takes a boolean. A non-boolean is refused on BOTH faces. |
| 5 | + * |
| 6 | + * # What was measured |
| 7 | + * |
| 8 | + * `FieldOperatorsSchema` declares `$null: z.boolean()`, and nothing between an |
| 9 | + * authored `where` and a driver validates against it. Given one row with |
| 10 | + * `stage: 'won'` (id 1) and one with `stage: null` (id 2), the filter |
| 11 | + * `{ stage: { $null: 'yes' } }` produced THREE answers: |
| 12 | + * |
| 13 | + * | face | compiled to | rows | |
| 14 | + * |---|---|---| |
| 15 | + * | `driver-sql` / `driver-sqlite-wasm` / Turso local | `IS NULL` (anything but `false`) | `["2"]` | |
| 16 | + * | this driver's live path (mingo), `driver-mongodb` | `IS NOT NULL` (anything but `true`) | `["1"]` | |
| 17 | + * | this driver's reference matcher | nothing at all | `["1","2"]` | |
| 18 | + * |
| 19 | + * The third row is the one #5347 could not see. It measured a fixture with no |
| 20 | + * null-valued row, where "matches every row" and "IS NOT NULL" are the same |
| 21 | + * answer; adding id 2 separates them. The matcher's arm is two conditionals — |
| 22 | + * `target === true && …` and `target === false && …` — and a third value |
| 23 | + * satisfies neither, so the constraint silently stopped constraining. That is |
| 24 | + * the widening direction, which on an RLS read scope is a permission bypass and |
| 25 | + * not a degraded filter (#3948) — and it is precisely the divergence #5324/#5328 |
| 26 | + * built the single shape gate to make impossible. |
| 27 | + * |
| 28 | + * # Why these tests assert through BOTH faces |
| 29 | + * |
| 30 | + * The rule lives in exactly one function (`assertFilterConditionShape`) and both |
| 31 | + * faces call it. A regression that re-forks them fails HERE rather than being |
| 32 | + * discovered by a conformance table that only exercises one — the same reason |
| 33 | + * `memory-filter-vocabulary-refusal.test.ts` doubles every case. |
| 34 | + */ |
| 35 | + |
| 36 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 37 | +import type { FilterCondition } from '@objectstack/spec/data'; |
| 38 | + |
| 39 | +import { InMemoryDriver } from './memory-driver.js'; |
| 40 | +import { match } from './memory-matcher.js'; |
| 41 | + |
| 42 | +interface WireBearingError extends Error { |
| 43 | + code?: string; |
| 44 | + status?: number; |
| 45 | +} |
| 46 | + |
| 47 | +const ROWS = [ |
| 48 | + { id: '1', stage: 'won', score: 10 }, |
| 49 | + // The null-valued row that separates "IS NOT NULL" from "no constraint". |
| 50 | + { id: '2', stage: null, score: 20 }, |
| 51 | +]; |
| 52 | + |
| 53 | +/** |
| 54 | + * The exact leading sentence `driver-sql` produces for this condition, copied |
| 55 | + * from `sql-driver.ts`. A literal rather than an import: driver-memory does not |
| 56 | + * depend on driver-sql (and must not), so the twin invariant #4436 established |
| 57 | + * is held by pinning the other side's wording here. |
| 58 | + */ |
| 59 | +const DRIVER_SQL_LEADING_SENTENCE = (field: string) => |
| 60 | + `Operator "$null" on field "${field}" requires a boolean comparand (true or false).`; |
| 61 | + |
| 62 | +describe('[#5347] $null requires a boolean comparand, on both filter faces', () => { |
| 63 | + let driver: InMemoryDriver; |
| 64 | + |
| 65 | + beforeEach(async () => { |
| 66 | + driver = new InMemoryDriver({ persistence: false }); |
| 67 | + await driver.syncSchema('deal', { |
| 68 | + fields: { |
| 69 | + id: { type: 'text', name: 'id' }, |
| 70 | + stage: { type: 'text', name: 'stage' }, |
| 71 | + score: { type: 'number', name: 'score' }, |
| 72 | + }, |
| 73 | + } as any); |
| 74 | + for (const row of ROWS) await driver.create('deal', row); |
| 75 | + }); |
| 76 | + |
| 77 | + const findIds = async (where: unknown): Promise<string[]> => { |
| 78 | + const rows = await driver.find('deal', { |
| 79 | + object: 'deal', |
| 80 | + fields: ['id'], |
| 81 | + where: where as FilterCondition, |
| 82 | + }); |
| 83 | + return (rows as any[]).map((r) => String(r.id)).sort(); |
| 84 | + }; |
| 85 | + |
| 86 | + const matchIds = (where: unknown): string[] => |
| 87 | + ROWS.filter((row) => match(row, where as any)).map((r) => r.id).sort(); |
| 88 | + |
| 89 | + const refusalOfFind = async (where: unknown): Promise<WireBearingError> => { |
| 90 | + try { |
| 91 | + await findIds(where); |
| 92 | + } catch (e) { |
| 93 | + return e as WireBearingError; |
| 94 | + } |
| 95 | + throw new Error('expected the live query path to refuse this filter, but it resolved'); |
| 96 | + }; |
| 97 | + |
| 98 | + const refusalOfMatch = (where: unknown): WireBearingError => { |
| 99 | + try { |
| 100 | + matchIds(where); |
| 101 | + } catch (e) { |
| 102 | + return e as WireBearingError; |
| 103 | + } |
| 104 | + throw new Error('expected the reference matcher to refuse this filter, but it answered'); |
| 105 | + }; |
| 106 | + |
| 107 | + const NON_BOOLEAN: Array<[label: string, value: unknown]> = [ |
| 108 | + ["the string 'yes'", 'yes'], |
| 109 | + ['the number 1', 1], |
| 110 | + ['the number 0', 0], |
| 111 | + ['null', null], |
| 112 | + ['undefined', undefined], |
| 113 | + ['an object', {}], |
| 114 | + // The trap: `"false"` is truthy, so it compiled to the OPPOSITE of what its |
| 115 | + // author meant on driver-sql — and to the opposite of THAT here. |
| 116 | + ["the STRING 'false'", 'false'], |
| 117 | + ]; |
| 118 | + |
| 119 | + for (const [label, value] of NON_BOOLEAN) { |
| 120 | + it(`the live query path refuses ${label}`, async () => { |
| 121 | + const err = await refusalOfFind({ stage: { $null: value } }); |
| 122 | + expect(err.code).toBe('INVALID_FILTER'); |
| 123 | + expect(err.status).toBe(400); |
| 124 | + expect(err.message).toContain(DRIVER_SQL_LEADING_SENTENCE('stage')); |
| 125 | + expect(err.message).toContain('filter.stage.$null'); |
| 126 | + }); |
| 127 | + |
| 128 | + it(`the reference matcher refuses ${label}, identically`, () => { |
| 129 | + const err = refusalOfMatch({ stage: { $null: value } }); |
| 130 | + expect(err.code).toBe('INVALID_FILTER'); |
| 131 | + expect(err.status).toBe(400); |
| 132 | + expect(err.message).toContain(DRIVER_SQL_LEADING_SENTENCE('stage')); |
| 133 | + expect(err.message).toContain('filter.stage.$null'); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + it('both faces refuse it inside a combinator, at the position that names it', async () => { |
| 138 | + for (const [where, path] of [ |
| 139 | + [{ $and: [{ stage: { $null: 'yes' } }] }, 'filter.$and[0].stage.$null'], |
| 140 | + [{ $or: [{ stage: 'won' }, { stage: { $null: 1 } }] }, 'filter.$or[1].stage.$null'], |
| 141 | + [{ $not: { stage: { $null: 'yes' } } }, 'filter.$not.stage.$null'], |
| 142 | + ] as Array<[unknown, string]>) { |
| 143 | + const findErr = await refusalOfFind(where); |
| 144 | + expect(findErr.code).toBe('INVALID_FILTER'); |
| 145 | + expect(findErr.message).toContain(path); |
| 146 | + const matchErr = refusalOfMatch(where); |
| 147 | + expect(matchErr.message).toBe(findErr.message); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + it('a satisfiable sibling does not let the malformed one through', async () => { |
| 152 | + // The gate is a walk, not an evaluation: `{ stage: 'won' }` matches, and |
| 153 | + // `{}` is the TRUE identity, yet neither short-circuits the refusal. |
| 154 | + for (const where of [ |
| 155 | + { $or: [{ stage: 'won' }, { stage: { $null: 'x' } }] }, |
| 156 | + { $or: [{}, { stage: { $null: 'x' } }] }, |
| 157 | + ]) { |
| 158 | + expect((await refusalOfFind(where)).code).toBe('INVALID_FILTER'); |
| 159 | + expect(refusalOfMatch(where).code).toBe('INVALID_FILTER'); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it('true and false are unchanged on both faces, line by line', async () => { |
| 164 | + expect(await findIds({ stage: { $null: true } })).toEqual(['2']); |
| 165 | + expect(matchIds({ stage: { $null: true } })).toEqual(['2']); |
| 166 | + expect(await findIds({ stage: { $null: false } })).toEqual(['1']); |
| 167 | + expect(matchIds({ stage: { $null: false } })).toEqual(['1']); |
| 168 | + }); |
| 169 | + |
| 170 | + it('the ordinary vocabulary is untouched on both faces', async () => { |
| 171 | + expect(await findIds({ stage: 'won' })).toEqual(['1']); |
| 172 | + expect(matchIds({ stage: 'won' })).toEqual(['1']); |
| 173 | + expect(await findIds({ score: { $between: [5, 15] } })).toEqual(['1']); |
| 174 | + expect(matchIds({ score: { $between: [5, 15] } })).toEqual(['1']); |
| 175 | + expect(await findIds({ $or: [{ stage: 'won' }, { score: 20 }] })).toEqual(['1', '2']); |
| 176 | + expect(matchIds({ $or: [{ stage: 'won' }, { score: 20 }] })).toEqual(['1', '2']); |
| 177 | + expect(await findIds({})).toEqual(['1', '2']); |
| 178 | + }); |
| 179 | + |
| 180 | + it('$exists is deliberately NOT tightened here', () => { |
| 181 | + // #5347 ruled on `$null` alone. `$exists` diverges on its own axis (#5299 |
| 182 | + // holds the open question of what "exists" means for a null-valued key), so |
| 183 | + // it keeps today's answers rather than being settled as a rider. |
| 184 | + expect(matchIds({ stage: { $exists: 'yes' } })).toEqual(['1']); |
| 185 | + }); |
| 186 | +}); |
0 commit comments