|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#6919] The RLS predicate grammar is stated on THREE faces of |
| 5 | + * `rls.zod.ts`, and they must not drift apart again. |
| 6 | + * |
| 7 | + * The three: |
| 8 | + * |
| 9 | + * 1. **module docblock** — the `ObjectStack RLS:` bullet list. `build-docs.ts` |
| 10 | + * publishes the module block verbatim as the opening prose of |
| 11 | + * `content/docs/references/security/rls.mdx`, so this face is READ BY USERS. |
| 12 | + * 2. **property docblock** — the TSDoc block above `using`. No generator reads |
| 13 | + * property-level TSDoc, so this face is read only by whoever opens the file |
| 14 | + * (often an AI author, ADR-0033) — which is exactly why it rotted unnoticed. |
| 15 | + * 3. **`.describe()` on `using`** — also published (it renders into the same |
| 16 | + * page's property table). |
| 17 | + * |
| 18 | + * They have now drifted twice. Both times the same way: a face froze a |
| 19 | + * *snapshot* of the compiler as a **closed enumeration with a count** |
| 20 | + * ("Exactly four forms compile"; "equality, set-membership, always-true"), |
| 21 | + * the compiler grew, and the prose stayed. #6762 / PR #6918 fixed faces 1 and |
| 22 | + * 3; #6919 rewrote face 2 and deleted the interim `⚠️ STALE` marker PR #6918 |
| 23 | + * had parked on it. Nothing compared the three, so face 2 contradicted face 3 |
| 24 | + * — on the same property — across two majors. |
| 25 | + * |
| 26 | + * ⛔ Scope: **the claim shape, not the wording.** Rephrasing a sentence, |
| 27 | + * reordering the bullets, or adding a newly-supported form is free. Reverting |
| 28 | + * any face to a fixed-count / closed-set claim, dropping an operator that |
| 29 | + * enforces, or dropping the fail-closed statement is not. |
| 30 | + * |
| 31 | + * Why a source-text pin is the right instrument here, given #6987's warning |
| 32 | + * that source-scanning pins nail the wrong number when the pinned fact lives |
| 33 | + * outside the source: the fact pinned here IS text — three prose faces of one |
| 34 | + * file agreeing with each other. Reading the source is not a proxy for the |
| 35 | + * fact, it is the fact. (The behavioural half — which predicates actually |
| 36 | + * lower — is owned by `isSupportedRlsExpression`'s own tests in |
| 37 | + * `@objectstack/formula`; this file must not restate it, and deliberately does |
| 38 | + * not import a runtime package.) |
| 39 | + */ |
| 40 | + |
| 41 | +import fs from 'node:fs'; |
| 42 | +import path from 'node:path'; |
| 43 | +import url from 'node:url'; |
| 44 | + |
| 45 | +import { describe, it, expect } from 'vitest'; |
| 46 | + |
| 47 | +import { RowLevelSecurityPolicySchema } from './rls.zod'; |
| 48 | + |
| 49 | +const HERE = path.dirname(url.fileURLToPath(import.meta.url)); |
| 50 | +const SOURCE = path.resolve(HERE, 'rls.zod.ts'); |
| 51 | + |
| 52 | +const source = fs.readFileSync(SOURCE, 'utf8'); |
| 53 | + |
| 54 | +/** |
| 55 | + * Face 1 — the `ObjectStack RLS:` bullets of the module docblock. |
| 56 | + * |
| 57 | + * Read out of the raw source rather than out of the generated `.mdx`: the |
| 58 | + * source is what a reader of the file sees AND what the generator copies, so |
| 59 | + * one read covers both surfaces and it cannot go green because a regen was |
| 60 | + * forgotten. |
| 61 | + */ |
| 62 | +function moduleFace(): string { |
| 63 | + const blockEnd = source.indexOf('*/'); |
| 64 | + const block = source.slice(0, blockEnd); |
| 65 | + const anchor = block.indexOf('ObjectStack RLS:'); |
| 66 | + expect(anchor, '`ObjectStack RLS:` heading not found in the module docblock').toBeGreaterThan(-1); |
| 67 | + |
| 68 | + // Collect the run of `* - ` bullets that follows, stopping when it ends. |
| 69 | + const lines: string[] = []; |
| 70 | + for (const line of block.slice(anchor).split('\n')) { |
| 71 | + if (/^\s*\*\s*-\s+\S/.test(line)) { lines.push(line); continue; } |
| 72 | + if (lines.length > 0) break; |
| 73 | + } |
| 74 | + return lines.join('\n'); |
| 75 | +} |
| 76 | + |
| 77 | +/** Face 2 — the TSDoc block immediately above the `using` property. */ |
| 78 | +function propertyFace(): string { |
| 79 | + const decl = source.indexOf('\n using: z.string()'); |
| 80 | + expect(decl, '`using: z.string()` declaration not found').toBeGreaterThan(-1); |
| 81 | + const end = source.lastIndexOf('*/', decl); |
| 82 | + const start = source.lastIndexOf('/**', end); |
| 83 | + expect(start, 'no TSDoc block found above `using`').toBeGreaterThan(-1); |
| 84 | + return source.slice(start, end + 2); |
| 85 | +} |
| 86 | + |
| 87 | +/** Face 3 — the `.describe()` carried by the `using` property. */ |
| 88 | +function describeFace(): string { |
| 89 | + const shape = (RowLevelSecurityPolicySchema as unknown as { shape: Record<string, { description?: string }> }).shape; |
| 90 | + return shape.using?.description ?? ''; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Every operator the reference compiler lowers, spelled the way all three |
| 95 | + * faces spell it (backticked, canonical CEL). Adding a row here when the |
| 96 | + * compiler grows is the intended maintenance: it turns "the docs are stale" |
| 97 | + * from something nobody notices into a red test. |
| 98 | + */ |
| 99 | +const ENFORCING_OPERATORS = ['`==`', '`!=`', '`<`', '`<=`', '`>`', '`>=`', '`in`', '`&&`', '`||`'] as const; |
| 100 | + |
| 101 | +/** A count attached to the accepted set — the exact defect that recurred. */ |
| 102 | +const FIXED_COUNT_CLAIM = |
| 103 | + /\b(?:exactly|precisely|only|just)\s+(?:\d+|one|two|three|four|five|six|seven|eight|nine|ten)\s+(?:forms?|shapes?|expressions?|predicates?)\b/i; |
| 104 | + |
| 105 | +/** The same defect spelled without the adverb ("four forms compile"). */ |
| 106 | +const BARE_COUNT_CLAIM = |
| 107 | + /\b(?:\d+|one|two|three|four|five|six|seven|eight|nine|ten)\s+(?:forms?|shapes?)\s+(?:compile|lower|are\s+supported)\b/i; |
| 108 | + |
| 109 | +const FACES: ReadonlyArray<readonly [string, string]> = [ |
| 110 | + ['module docblock', moduleFace()], |
| 111 | + ['property docblock', propertyFace()], |
| 112 | + ['.describe()', describeFace()], |
| 113 | +]; |
| 114 | + |
| 115 | +describe('[#6919] rls.zod.ts states one predicate grammar on all three faces', () => { |
| 116 | + it('finds all three faces at all (anti-vacuity)', () => { |
| 117 | + // Every assertion below is a search over a string. An empty haystack would |
| 118 | + // make the negative ones pass forever the day someone moves a block. |
| 119 | + for (const [name, text] of FACES) { |
| 120 | + expect(text.length, `${name} face came back empty — the extractor no longer finds it`) |
| 121 | + .toBeGreaterThan(200); |
| 122 | + } |
| 123 | + expect(propertyFace()).toContain('Supported expression grammar'); |
| 124 | + }); |
| 125 | + |
| 126 | + it.each(FACES.map(([name, text]) => ({ name, text })))( |
| 127 | + 'the $name face states no fixed count of accepted forms', |
| 128 | + ({ name, text }) => { |
| 129 | + // ⛔ #6919: replacing "four" with the current number is the SAME defect — |
| 130 | + // the grammar is "whatever lowers to a filter", not a numbered list. |
| 131 | + expect(FIXED_COUNT_CLAIM.test(text), `${name} re-introduced a counted accepted set`).toBe(false); |
| 132 | + expect(BARE_COUNT_CLAIM.test(text), `${name} re-introduced a counted accepted set`).toBe(false); |
| 133 | + }, |
| 134 | + ); |
| 135 | + |
| 136 | + it.each(FACES.map(([name, text]) => ({ name, text })))( |
| 137 | + 'the $name face does not re-assert a retracted under-statement', |
| 138 | + ({ name, text }) => { |
| 139 | + // The two literal sentences #6762 / #6918 / #6919 removed. |
| 140 | + expect(text, `${name} re-asserts that only \`=\` compares`).not.toMatch(/comparison\s+operators?\s+other\s+than/i); |
| 141 | + expect(text, `${name} re-asserts the closed three-item grammar`) |
| 142 | + .not.toMatch(/equality,\s*set-membership,\s*always-true/i); |
| 143 | + }, |
| 144 | + ); |
| 145 | + |
| 146 | + it.each(FACES.map(([name, text]) => ({ name, text })))( |
| 147 | + 'the $name face still says the compiler fails closed', |
| 148 | + ({ name, text }) => { |
| 149 | + // The one safety-relevant sentence. A face that drops it turns a |
| 150 | + // "matches zero rows" contract into an unstated one. |
| 151 | + expect(text, `${name} no longer states the fail-closed contract`).toMatch(/fails?\s+closed/i); |
| 152 | + }, |
| 153 | + ); |
| 154 | + |
| 155 | + it('the property docblock and `.describe()` name the same operator set', () => { |
| 156 | + // The pair that literally contradicted each other on one property (#6919). |
| 157 | + const property = propertyFace(); |
| 158 | + const described = describeFace(); |
| 159 | + for (const op of ENFORCING_OPERATORS) { |
| 160 | + expect(property, `property docblock stopped naming ${op}`).toContain(op); |
| 161 | + expect(described, `.describe() stopped naming ${op}`).toContain(op); |
| 162 | + } |
| 163 | + // The allow-all is a literal, not an operator, but it is the form most |
| 164 | + // often dropped when someone "tidies" the list. |
| 165 | + expect(property).toContain('`true`'); |
| 166 | + expect(described).toContain('`true`'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('the module face describes an open grammar, not a closed list', () => { |
| 170 | + // This face is ONE published line, so it cannot enumerate operators. What |
| 171 | + // it must not do is name a finite set of categories again: it has to carry |
| 172 | + // the composition operators, which are what a closed "equality / |
| 173 | + // set-membership / always-true" list always omits. |
| 174 | + const module = moduleFace(); |
| 175 | + expect(module).toContain('`&&`'); |
| 176 | + expect(module).toContain('`||`'); |
| 177 | + expect(module).toMatch(/comparisons/i); |
| 178 | + expect(module).toMatch(/set-membership/i); |
| 179 | + }); |
| 180 | + |
| 181 | + it('carries no `STALE` marker on any face', () => { |
| 182 | + // PR #6918 parked a `⚠️ STALE` marker on the property block as an interim |
| 183 | + // measure and #6919 removed it with the rewrite. A marker coming back is a |
| 184 | + // signal that the faces disagree again — which is what this pin is for. |
| 185 | + for (const [name, text] of FACES) { |
| 186 | + expect(text, `${name} carries a STALE marker again`).not.toMatch(/\bSTALE\b/); |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + it('presents CEL as the canonical spelling, SQL as the deprecated bridge', () => { |
| 191 | + // ADR-0058 D1. `sqlPredicateToCel` is `@deprecated`; a face that leads with |
| 192 | + // SQL sends an author to the dialect we are migrating off. |
| 193 | + expect(propertyFace()).toMatch(/canonical CEL/); |
| 194 | + expect(describeFace()).toMatch(/canonical CEL/); |
| 195 | + expect(moduleFace()).toMatch(/CEL/); |
| 196 | + }); |
| 197 | +}); |
0 commit comments