|
| 1 | +/** |
| 2 | + * v3 operator capability gating + registration constraints. |
| 3 | + * |
| 4 | + * Every v3 operator gates on the CONCRETE domain's query capabilities |
| 5 | + * (derived from the catalog by codec id) before building any AST, and |
| 6 | + * throws `EncryptionOperatorError` naming the column, domain, operator, |
| 7 | + * and missing capability. Trait dispatch alone is not enough: traits |
| 8 | + * are per-codec, and a caller reaching the operator descriptor directly |
| 9 | + * (or through a custom builder) must still be stopped when the domain |
| 10 | + * cannot answer the operator. |
| 11 | + * |
| 12 | + * Also pins decision 1b's registration constraint: the v3 descriptor |
| 13 | + * stands alone (a v3-only adapter builds cleanly), and co-registering |
| 14 | + * the v2 and v3 descriptors throws — the framework's flat |
| 15 | + * `OperationRegistry` rejects two descriptors sharing the |
| 16 | + * `cipherstashEq` method name. v2 and v3 are separate entry points, |
| 17 | + * never composed into one client. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, expect, it } from 'vitest' |
| 21 | +import { createCipherstashRuntimeDescriptor } from '../../src/exports/runtime' |
| 22 | +import { |
| 23 | + cipherstashV3Asc, |
| 24 | + cipherstashV3QueryOperations, |
| 25 | + EncryptionOperatorError, |
| 26 | +} from '../../src/v3/operators-v3' |
| 27 | +import { |
| 28 | + assembleV3ExecutionContext, |
| 29 | + BOOLEAN_CODEC_ID, |
| 30 | + callOperator, |
| 31 | + columnAccessorV3, |
| 32 | + emptySdk, |
| 33 | + getOperator, |
| 34 | + JSON_CODEC_ID, |
| 35 | + makeV3Adapter, |
| 36 | + TABLE, |
| 37 | + TEXT_EQ_CODEC_ID, |
| 38 | + TEXT_STORAGE_CODEC_ID, |
| 39 | + v3RuntimeDescriptor, |
| 40 | +} from './operator-lowering-v3.helpers' |
| 41 | + |
| 42 | +describe('v3 operator capability gating', () => { |
| 43 | + it('equality requires the equality capability — storage-only eql_v3_text rejects cipherstashEq', () => { |
| 44 | + expect(() => |
| 45 | + callOperator( |
| 46 | + getOperator('cipherstashEq'), |
| 47 | + columnAccessorV3(TABLE, 'note', TEXT_STORAGE_CODEC_ID), |
| 48 | + 'x', |
| 49 | + ), |
| 50 | + ).toThrow(EncryptionOperatorError) |
| 51 | + }) |
| 52 | + |
| 53 | + it('names the column, domain, operator, and missing capability in the diagnostic', () => { |
| 54 | + try { |
| 55 | + callOperator( |
| 56 | + getOperator('cipherstashEq'), |
| 57 | + columnAccessorV3(TABLE, 'note', TEXT_STORAGE_CODEC_ID), |
| 58 | + 'x', |
| 59 | + ) |
| 60 | + expect.unreachable('cipherstashEq on a storage-only column must throw') |
| 61 | + } catch (error) { |
| 62 | + expect(error).toBeInstanceOf(EncryptionOperatorError) |
| 63 | + const operatorError = error as EncryptionOperatorError |
| 64 | + expect(operatorError.message).toContain('cipherstashEq') |
| 65 | + expect(operatorError.message).toContain('equality') |
| 66 | + expect(operatorError.message).toContain('"note"') |
| 67 | + expect(operatorError.message).toContain('public.eql_v3_text') |
| 68 | + expect(operatorError.context).toEqual({ |
| 69 | + columnName: 'note', |
| 70 | + tableName: TABLE, |
| 71 | + operator: 'cipherstashEq', |
| 72 | + }) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + it('comparison requires order/range — text_eq rejects cipherstashGt', () => { |
| 77 | + expect(() => |
| 78 | + callOperator( |
| 79 | + getOperator('cipherstashGt'), |
| 80 | + columnAccessorV3(TABLE, 'nickname', TEXT_EQ_CODEC_ID), |
| 81 | + 'x', |
| 82 | + ), |
| 83 | + ).toThrow(/order\/range/) |
| 84 | + }) |
| 85 | + |
| 86 | + it('free-text requires the match index — text_eq rejects cipherstashIlike', () => { |
| 87 | + expect(() => |
| 88 | + callOperator( |
| 89 | + getOperator('cipherstashIlike'), |
| 90 | + columnAccessorV3(TABLE, 'nickname', TEXT_EQ_CODEC_ID), |
| 91 | + 'x', |
| 92 | + ), |
| 93 | + ).toThrow(/free-text/) |
| 94 | + }) |
| 95 | + |
| 96 | + it('JSON containment requires searchableJson — text_eq rejects cipherstashJsonContains', () => { |
| 97 | + expect(() => |
| 98 | + callOperator( |
| 99 | + getOperator('cipherstashJsonContains'), |
| 100 | + columnAccessorV3(TABLE, 'nickname', TEXT_EQ_CODEC_ID), |
| 101 | + { role: 'admin' }, |
| 102 | + ), |
| 103 | + ).toThrow(/JSON containment/) |
| 104 | + }) |
| 105 | + |
| 106 | + it('eql_v3_json is searchableJson-only — rejects cipherstashEq', () => { |
| 107 | + expect(() => |
| 108 | + callOperator( |
| 109 | + getOperator('cipherstashEq'), |
| 110 | + columnAccessorV3(TABLE, 'payload', JSON_CODEC_ID), |
| 111 | + { role: 'admin' }, |
| 112 | + ), |
| 113 | + ).toThrow(/equality/) |
| 114 | + }) |
| 115 | + |
| 116 | + it('storage-only eql_v3_boolean rejects every search operator', () => { |
| 117 | + for (const method of [ |
| 118 | + 'cipherstashEq', |
| 119 | + 'cipherstashNe', |
| 120 | + 'cipherstashGt', |
| 121 | + 'cipherstashIlike', |
| 122 | + 'cipherstashJsonContains', |
| 123 | + ]) { |
| 124 | + expect(() => |
| 125 | + callOperator( |
| 126 | + getOperator(method), |
| 127 | + columnAccessorV3(TABLE, 'active', BOOLEAN_CODEC_ID), |
| 128 | + true, |
| 129 | + ), |
| 130 | + ).toThrow(EncryptionOperatorError) |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + it('ordering helpers gate on order/range — text_eq rejects cipherstashV3Asc', () => { |
| 135 | + expect(() => |
| 136 | + cipherstashV3Asc(columnAccessorV3(TABLE, 'nickname', TEXT_EQ_CODEC_ID)), |
| 137 | + ).toThrow(/order\/range/) |
| 138 | + }) |
| 139 | + |
| 140 | + it('rejects a non-v3 codec id (v2 columns are the wrong entry point)', () => { |
| 141 | + expect(() => |
| 142 | + callOperator( |
| 143 | + getOperator('cipherstashEq'), |
| 144 | + columnAccessorV3(TABLE, 'email', 'cipherstash/string@1'), |
| 145 | + 'x', |
| 146 | + ), |
| 147 | + ).toThrow(/not a .*v3 domain/) |
| 148 | + }) |
| 149 | + |
| 150 | + it('rejects a self expression with no codec binding', () => { |
| 151 | + expect(() => |
| 152 | + callOperator( |
| 153 | + getOperator('cipherstashEq'), |
| 154 | + { |
| 155 | + buildAst: () => { |
| 156 | + throw new Error('unreachable') |
| 157 | + }, |
| 158 | + }, |
| 159 | + 'x', |
| 160 | + ), |
| 161 | + ).toThrow(/missing a CodecRef/) |
| 162 | + }) |
| 163 | +}) |
| 164 | + |
| 165 | +describe('v3 descriptor registration (decision 1b)', () => { |
| 166 | + it('a v3-only adapter builds and registers its operation set cleanly', () => { |
| 167 | + expect(() => makeV3Adapter()).not.toThrow() |
| 168 | + // The execution context is what assembles the flat operation |
| 169 | + // registry from `queryOperations()` — a v3-only stack registers the |
| 170 | + // full `cipherstash*` method set without collision. |
| 171 | + const registered = Object.keys( |
| 172 | + assembleV3ExecutionContext().queryOperations.entries(), |
| 173 | + ) |
| 174 | + expect(registered).toEqual( |
| 175 | + expect.arrayContaining([...Object.keys(cipherstashV3QueryOperations())]), |
| 176 | + ) |
| 177 | + }) |
| 178 | + |
| 179 | + it('co-registering the v2 and v3 descriptors throws on the shared method names', () => { |
| 180 | + // Both descriptors define `cipherstashEq` (and eleven siblings); the |
| 181 | + // flat, method-keyed OperationRegistry disallows override. This is |
| 182 | + // WHY v2 and v3 are separate entry points that are never composed |
| 183 | + // into one client. The registry is assembled when the execution |
| 184 | + // context is built against a contract, so the collision surfaces |
| 185 | + // there rather than at adapter construction. |
| 186 | + expect(() => |
| 187 | + assembleV3ExecutionContext([ |
| 188 | + createCipherstashRuntimeDescriptor({ sdk: emptySdk() }), |
| 189 | + v3RuntimeDescriptor(), |
| 190 | + ]), |
| 191 | + ).toThrow(/already registered/) |
| 192 | + }) |
| 193 | +}) |
0 commit comments