|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29377] `UNIQUE_NULL_DISTINCT_CASES` lands ahead of every driver that runs |
| 5 | + * it — each driver's adoption is its own lane's PR — so between now and then |
| 6 | + * nothing executes the table, and a wrong `expected` would sit quietly until a |
| 7 | + * driver author trusted it. This file executes it, against a reference store |
| 8 | + * written from the declared rule: a row whose unique key has a NULL (`null` or |
| 9 | + * absent) in ANY key part is exempt; `''` is a value. |
| 10 | + * |
| 11 | + * The second job is proving the table is not VACUOUS: each way a driver can |
| 12 | + * get the rule wrong — the sparse index that indexes `null` (driver-mongodb |
| 13 | + * before #29339), folding `null` and absent onto one value, treating `''` as a |
| 14 | + * NULL, keying a compound on one column, enforcing nothing, refusing and |
| 15 | + * keeping the row — must fail at least one case. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect } from 'vitest'; |
| 19 | +import { ObjectSchema } from './object.zod'; |
| 20 | +import { |
| 21 | + UNIQUE_NULL_DISTINCT_CASES, |
| 22 | + UNIQUE_NULL_DISTINCT_OBJECTS, |
| 23 | + uniqueNullDistinctDivergence, |
| 24 | + type UniqueNullDistinctCase, |
| 25 | +} from './unique-null-distinct-conformance'; |
| 26 | + |
| 27 | +// ── A reference store, from the declared rule ──────────────────────────────── |
| 28 | + |
| 29 | +type Row = Readonly<Record<string, unknown>>; |
| 30 | + |
| 31 | +/** The ways a driver can get NULL-distinct `unique` wrong — `faithful` is the rule. */ |
| 32 | +type Reading = |
| 33 | + | 'faithful' |
| 34 | + | 'null-is-a-value' // a sparse index: an explicit null is indexed, only a missing field is skipped |
| 35 | + | 'null-and-absent-fold' // every NULL is one shared value |
| 36 | + | 'empty-string-is-null' // '' exempts the row like a NULL |
| 37 | + | 'first-part-only' // a compound key built over its first column alone |
| 38 | + | 'not-enforced' // the constraint is declared and never checked |
| 39 | + | 'refuses-and-keeps'; // the duplicate is refused, and landed anyway |
| 40 | + |
| 41 | +/** The key parts of each object's one unique constraint. */ |
| 42 | +const KEY_PARTS: Readonly<Record<UniqueNullDistinctCase['object'], readonly string[]>> = { |
| 43 | + unique_null_single: ['code'], |
| 44 | + unique_null_compound: UNIQUE_NULL_DISTINCT_OBJECTS.unique_null_compound.indexes[0].fields, |
| 45 | +}; |
| 46 | + |
| 47 | +const ABSENT = Symbol('absent'); |
| 48 | + |
| 49 | +/** One key part as `reading` sees it: `null` when it exempts the row, else the value it keys on. */ |
| 50 | +function part(row: Row, field: string, reading: Reading): unknown { |
| 51 | + if (!Object.prototype.hasOwnProperty.call(row, field) || row[field] === undefined) { |
| 52 | + return reading === 'null-and-absent-fold' ? ABSENT : null; |
| 53 | + } |
| 54 | + const value = row[field]; |
| 55 | + if (value === null) return reading === 'null-is-a-value' || reading === 'null-and-absent-fold' ? ABSENT : null; |
| 56 | + if (value === '' && reading === 'empty-string-is-null') return null; |
| 57 | + return value; |
| 58 | +} |
| 59 | + |
| 60 | +/** The row's key under `reading`, or `null` when the row is exempt from the constraint. */ |
| 61 | +function keyOf(object: UniqueNullDistinctCase['object'], row: Row, reading: Reading): string | null { |
| 62 | + const fields = reading === 'first-part-only' ? KEY_PARTS[object].slice(0, 1) : KEY_PARTS[object]; |
| 63 | + const parts = fields.map((f) => part(row, f, reading)); |
| 64 | + if (parts.some((p) => p === null)) return null; |
| 65 | + return JSON.stringify(parts.map((p) => (p === ABSENT ? '<<NULL>>' : p))); |
| 66 | +} |
| 67 | + |
| 68 | +class UniqueRefusal extends Error { |
| 69 | + // The shape `driver-memory` throws, which `isUniqueViolationError` recognises. |
| 70 | + readonly code = 'UNIQUE_VIOLATION'; |
| 71 | +} |
| 72 | + |
| 73 | +/** A store holding both objects, one unique constraint each, as `reading` enforces them. */ |
| 74 | +class ReferenceStore { |
| 75 | + private readonly rows = new Map<string, Row[]>(); |
| 76 | + |
| 77 | + constructor(private readonly reading: Reading) {} |
| 78 | + |
| 79 | + create(object: UniqueNullDistinctCase['object'], row: Row): void { |
| 80 | + const table = this.rows.get(object) ?? []; |
| 81 | + this.rows.set(object, table); |
| 82 | + const key = keyOf(object, row, this.reading); |
| 83 | + const collides = key !== null && this.reading !== 'not-enforced' |
| 84 | + && table.some((other) => keyOf(object, other, this.reading) === key); |
| 85 | + if (collides && this.reading !== 'refuses-and-keeps') throw new UniqueRefusal('unique violation'); |
| 86 | + table.push(row); |
| 87 | + if (collides) throw new UniqueRefusal('unique violation'); |
| 88 | + } |
| 89 | + |
| 90 | + countLabelled(object: UniqueNullDistinctCase['object'], label: string): number { |
| 91 | + return (this.rows.get(object) ?? []).filter((r) => r.label === label).length; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Run the consumption recipe the case-set documents, on one store for every |
| 97 | + * case, in table order: first admitted, second admitted or refused, count by |
| 98 | + * label, judge. Returns the names of the cases that diverge. |
| 99 | + */ |
| 100 | +function divergent(reading: Reading, cases: readonly UniqueNullDistinctCase[] = UNIQUE_NULL_DISTINCT_CASES): string[] { |
| 101 | + const store = new ReferenceStore(reading); |
| 102 | + const out: string[] = []; |
| 103 | + for (const c of cases) { |
| 104 | + let firstAdmitted = true; |
| 105 | + try { |
| 106 | + store.create(c.object, c.first); |
| 107 | + } catch { |
| 108 | + firstAdmitted = false; |
| 109 | + } |
| 110 | + let second: 'admitted' | 'refused' = 'admitted'; |
| 111 | + try { |
| 112 | + store.create(c.object, c.second); |
| 113 | + } catch (e) { |
| 114 | + // The recipe's judge: only a recognised unique refusal is a refusal. |
| 115 | + if (!(e instanceof UniqueRefusal)) throw e; |
| 116 | + second = 'refused'; |
| 117 | + } |
| 118 | + const verdict = uniqueNullDistinctDivergence(c, second, store.countLabelled(c.object, c.name)); |
| 119 | + if (!firstAdmitted || verdict !== null) out.push(c.name); |
| 120 | + } |
| 121 | + return out; |
| 122 | +} |
| 123 | + |
| 124 | +// ── The table, executed ────────────────────────────────────────────────────── |
| 125 | + |
| 126 | +describe('UNIQUE_NULL_DISTINCT_CASES — the table agrees with the declared rule', () => { |
| 127 | + for (const c of UNIQUE_NULL_DISTINCT_CASES) { |
| 128 | + it(c.name, () => { |
| 129 | + expect(divergent('faithful', [c])).toEqual([]); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + it('every case on ONE store, in table order, still agrees: no case collides with another', () => { |
| 134 | + expect(divergent('faithful')).toEqual([]); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('UNIQUE_NULL_DISTINCT_CASES — each wrong reading of the rule fails the table', () => { |
| 139 | + it('a sparse index (null indexed like a value) fails the two-nulls rows', () => { |
| 140 | + expect(divergent('null-is-a-value')).toEqual( |
| 141 | + expect.arrayContaining(['single_null_null', 'compound_one_null', 'compound_leading_null']), |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('folding null and absent onto one value fails the absent rows too', () => { |
| 146 | + expect(divergent('null-and-absent-fold')).toEqual( |
| 147 | + expect.arrayContaining(['single_absent_absent', 'single_null_absent', 'compound_one_absent']), |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it("treating '' as a NULL fails exactly the empty-string row", () => { |
| 152 | + expect(divergent('empty-string-is-null')).toEqual(['single_empty_string']); |
| 153 | + }); |
| 154 | + |
| 155 | + it('a compound key built over its first column alone fails', () => { |
| 156 | + expect(divergent('first-part-only')).toEqual( |
| 157 | + expect.arrayContaining(['compound_differs_in_one_part']), |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it('a constraint that is never checked fails every refused row', () => { |
| 162 | + expect(divergent('not-enforced')).toEqual( |
| 163 | + UNIQUE_NULL_DISTINCT_CASES.filter((c) => c.expected === 'refused').map((c) => c.name), |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it('a refusal that lands the row anyway fails every refused row, on the count', () => { |
| 168 | + expect(divergent('refuses-and-keeps')).toEqual( |
| 169 | + UNIQUE_NULL_DISTINCT_CASES.filter((c) => c.expected === 'refused').map((c) => c.name), |
| 170 | + ); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('uniqueNullDistinctDivergence — the shared judge', () => { |
| 175 | + const admitted = UNIQUE_NULL_DISTINCT_CASES.find((c) => c.expected === 'admitted') as UniqueNullDistinctCase; |
| 176 | + const refused = UNIQUE_NULL_DISTINCT_CASES.find((c) => c.expected === 'refused') as UniqueNullDistinctCase; |
| 177 | + |
| 178 | + it('answers null only on the expected answer AND the expected count', () => { |
| 179 | + expect(uniqueNullDistinctDivergence(admitted, 'admitted', 2)).toBeNull(); |
| 180 | + expect(uniqueNullDistinctDivergence(refused, 'refused', 1)).toBeNull(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('names the case, both rows and what was observed when it diverges', () => { |
| 184 | + const d = uniqueNullDistinctDivergence(admitted, 'refused', 1); |
| 185 | + expect(d).toContain(admitted.name); |
| 186 | + expect(d).toContain(JSON.stringify(admitted.second)); |
| 187 | + expect(uniqueNullDistinctDivergence(admitted, 'admitted', 1)).not.toBeNull(); |
| 188 | + expect(uniqueNullDistinctDivergence(refused, 'refused', 2)).not.toBeNull(); |
| 189 | + expect(uniqueNullDistinctDivergence(refused, 'admitted', 2)).not.toBeNull(); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +describe('UNIQUE_NULL_DISTINCT_CASES — the table is well-formed', () => { |
| 194 | + it('names are unique and each row is labelled with its case', () => { |
| 195 | + const names = UNIQUE_NULL_DISTINCT_CASES.map((c) => c.name); |
| 196 | + expect(new Set(names).size).toBe(names.length); |
| 197 | + for (const c of UNIQUE_NULL_DISTINCT_CASES) { |
| 198 | + expect(c.first.label, c.name).toBe(c.name); |
| 199 | + expect(c.second.label, c.name).toBe(c.name); |
| 200 | + } |
| 201 | + }); |
| 202 | + |
| 203 | + it('every non-NULL key value belongs to one case, so a suite may share one table', () => { |
| 204 | + const owner = new Map<string, string>(); |
| 205 | + for (const c of UNIQUE_NULL_DISTINCT_CASES) { |
| 206 | + for (const row of [c.first, c.second]) { |
| 207 | + for (const f of KEY_PARTS[c.object]) { |
| 208 | + const v = row[f]; |
| 209 | + if (v === null || v === undefined) continue; |
| 210 | + const k = `${c.object}.${f}=${JSON.stringify(v)}`; |
| 211 | + expect(owner.get(k) ?? c.name, k).toBe(c.name); |
| 212 | + owner.set(k, c.name); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + it('both answers are represented on both objects', () => { |
| 219 | + for (const object of Object.keys(UNIQUE_NULL_DISTINCT_OBJECTS)) { |
| 220 | + const answers = new Set(UNIQUE_NULL_DISTINCT_CASES.filter((c) => c.object === object).map((c) => c.expected)); |
| 221 | + expect([...answers].sort(), object).toEqual(['admitted', 'refused']); |
| 222 | + } |
| 223 | + }); |
| 224 | + |
| 225 | + it('the fixture objects are authorable: ObjectSchema accepts each, scope stated', () => { |
| 226 | + for (const o of Object.values(UNIQUE_NULL_DISTINCT_OBJECTS)) { |
| 227 | + const parsed = ObjectSchema.safeParse(o); |
| 228 | + expect(parsed.success, `${o.name}: ${JSON.stringify(parsed.error?.issues)}`).toBe(true); |
| 229 | + } |
| 230 | + }); |
| 231 | +}); |
0 commit comments