|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectui#6444 — the built-in fault warnings of `evaluate` are rate limited to |
| 11 | + * ONE line per authored source, like both sibling reporters already are. |
| 12 | + * |
| 13 | + * ## The defect, measured on the built evaluator at `830ed5803` |
| 14 | + * |
| 15 | + * `evaluateCondition('${nosuchroot.x > 1}')` three times in a row produced |
| 16 | + * **3** console lines, where the `{ dialect: 'cel' }` envelope produced **1** |
| 17 | + * for the same three calls. `evaluate` is the hottest of the three paths — |
| 18 | + * `SchemaRenderer` calls it for every `properties.*` value, every `props.*` |
| 19 | + * value and `content`, for every node, on every render — so ONE broken `${…}` |
| 20 | + * prop in a 200-row list was 200 console lines per render, and 200 more on the |
| 21 | + * next one. |
| 22 | + * |
| 23 | + * ## What these cells have to measure, and why one direction is not enough |
| 24 | + * |
| 25 | + * "Two faults from the same source log once" passes just as well if the dedupe |
| 26 | + * silenced everything; "two faults from different sources log twice" passes |
| 27 | + * just as well if it deduped nothing. Only the pair measures the GRANULARITY, |
| 28 | + * so both are pinned below — plus the cell that discriminates the granularity |
| 29 | + * actually chosen: the same source across MANY DIFFERENT SCOPES is still one |
| 30 | + * line. That third cell is the card's open point (source text alone vs source + |
| 31 | + * scope) settled as a measurement: the 200-row flood is one authored source |
| 32 | + * against 200 distinct scopes, so a scope-sensitive key emits all 200 lines |
| 33 | + * again and fixes nothing. |
| 34 | + * |
| 35 | + * Every cell also asserts that the evaluation REALLY happened and returned its |
| 36 | + * documented fail-soft value — a "only one warn" pin passes vacuously if the |
| 37 | + * evaluator stopped being called or stopped faulting. |
| 38 | + */ |
| 39 | + |
| 40 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 41 | + |
| 42 | +type EvaluatorModule = typeof import('../ExpressionEvaluator.js'); |
| 43 | + |
| 44 | +/** |
| 45 | + * The dedupe under test is MODULE state, and module state outlives a test case: |
| 46 | + * without this reset the second cell to fault on a given source reads the first |
| 47 | + * cell's entry, sees silence, and passes having checked nothing. `vi.resetModules()` |
| 48 | + * + a fresh dynamic import gives every cell its own `Set` — and the last cell in |
| 49 | + * this file proves that this reset genuinely resets, rather than assuming it. |
| 50 | + * (Cells also use distinct source texts, so the discipline does not rest on the |
| 51 | + * hook alone.) |
| 52 | + */ |
| 53 | +let mod: EvaluatorModule; |
| 54 | +let warn: ReturnType<typeof vi.spyOn>; |
| 55 | + |
| 56 | +beforeEach(async () => { |
| 57 | + vi.resetModules(); |
| 58 | + mod = await import('../ExpressionEvaluator.js'); |
| 59 | + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 60 | +}); |
| 61 | + |
| 62 | +afterEach(() => { |
| 63 | + vi.restoreAllMocks(); |
| 64 | +}); |
| 65 | + |
| 66 | +const SCOPE = { record: { id: 1, status: 'open' }, data: { total: 99 } }; |
| 67 | +const make = (scope: Record<string, unknown> = SCOPE) => new mod.ExpressionEvaluator(scope); |
| 68 | +const lines = () => warn.mock.calls.map((c: unknown[]) => String(c[0])); |
| 69 | + |
| 70 | +describe('#6444 — one authoring mistake, one loud line', () => { |
| 71 | + it('SAME source, three evaluations: ONE line — and all three evaluations really ran and really faulted', () => { |
| 72 | + const src = '${nosuchroot.dedupe_same > 1}'; |
| 73 | + const e = make(); |
| 74 | + |
| 75 | + const verdicts = [e.evaluateCondition(src), e.evaluateCondition(src), e.evaluateCondition(src)]; |
| 76 | + |
| 77 | + // The fault happened every time: the documented fail-OPEN verdict, three times. |
| 78 | + expect(verdicts).toEqual([true, true, true]); |
| 79 | + // And the evaluator is still doing its job in the same run — so the single |
| 80 | + // line is a rate limit, not a stopped evaluator. |
| 81 | + expect(e.evaluate('Total: ${data.total}')).toBe('Total: 99'); |
| 82 | + expect(warn).toHaveBeenCalledTimes(1); |
| 83 | + expect(lines()[0]).toBe('Failed to evaluate expression: ${nosuchroot.dedupe_same > 1}'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('DIFFERENT sources: each still gets its own line — the dedupe is not blanket silence', () => { |
| 87 | + const e = make(); |
| 88 | + |
| 89 | + expect(e.evaluateCondition('${nosuchroot.distinct_a > 1}')).toBe(true); |
| 90 | + expect(e.evaluateCondition('${nosuchroot.distinct_b > 1}')).toBe(true); |
| 91 | + |
| 92 | + expect(warn).toHaveBeenCalledTimes(2); |
| 93 | + expect(lines()).toEqual([ |
| 94 | + 'Failed to evaluate expression: ${nosuchroot.distinct_a > 1}', |
| 95 | + 'Failed to evaluate expression: ${nosuchroot.distinct_b > 1}', |
| 96 | + ]); |
| 97 | + }); |
| 98 | + |
| 99 | + it('THE OPEN POINT: one source across 200 DIFFERENT scopes is ONE line — the 200-row list from the card', () => { |
| 100 | + // This is the cell that separates the two candidate keyings. Keyed on the |
| 101 | + // source TEXT (what shipped) this is 1; keyed on source + scope it is 200, |
| 102 | + // which is the defect verbatim. |
| 103 | + const src = 'Row ${record.id}: ${nosuchroot.row_total}'; |
| 104 | + const rendered: string[] = []; |
| 105 | + for (let row = 0; row < 200; row++) { |
| 106 | + rendered.push(make({ record: { id: row }, data: { total: 99 } }).evaluate(src) as string); |
| 107 | + } |
| 108 | + |
| 109 | + expect(warn).toHaveBeenCalledTimes(1); |
| 110 | + expect(lines()[0]).toBe('Expression evaluation failed for: nosuchroot.row_total'); |
| 111 | + |
| 112 | + // 200 real evaluations against 200 real scopes: the healthy half of the |
| 113 | + // template interpolates each row's OWN id, and the faulting half returns |
| 114 | + // its source verbatim (the documented fail-soft value for a template part). |
| 115 | + expect(rendered).toHaveLength(200); |
| 116 | + expect(rendered[0]).toBe('Row 0: ${nosuchroot.row_total}'); |
| 117 | + expect(rendered[199]).toBe('Row 199: ${nosuchroot.row_total}'); |
| 118 | + expect(new Set(rendered).size).toBe(200); |
| 119 | + }); |
| 120 | + |
| 121 | + it('the two fault SITES report independently and keep their own message text', () => { |
| 122 | + // `evaluate` has two built-in fault paths: one PART of a multi-part |
| 123 | + // template failing, and the WHOLE expression failing. They are different |
| 124 | + // faults, so the dedupe key is tagged with the site and neither can |
| 125 | + // silence the other. Message texts are unchanged from before #6444. |
| 126 | + const e = make(); |
| 127 | + |
| 128 | + expect(e.evaluate('a ${nosuchroot.site_part} b')).toBe('a ${nosuchroot.site_part} b'); |
| 129 | + expect(e.evaluate('${nosuchroot.site_whole}')).toBe('${nosuchroot.site_whole}'); |
| 130 | + |
| 131 | + expect(lines()).toEqual([ |
| 132 | + 'Expression evaluation failed for: nosuchroot.site_part', |
| 133 | + 'Failed to evaluate expression: ${nosuchroot.site_whole}', |
| 134 | + ]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('a HEALTHY expression never warns — the silence that makes one line mean something', () => { |
| 138 | + const e = make(); |
| 139 | + expect(e.evaluate('Total: ${data.total}')).toBe('Total: 99'); |
| 140 | + expect(e.evaluate('${record.status}')).toBe('open'); |
| 141 | + expect(e.evaluateCondition("${record.status == 'open'}")).toBe(true); |
| 142 | + expect(e.evaluateCondition("${record.status == 'closed'}")).toBe(false); |
| 143 | + expect(warn).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe('#6444 — the rate limit governs the built-in line ONLY', () => { |
| 148 | + it('`onFault` still fires on EVERY fault for the same source — the passback contract is unmoved', () => { |
| 149 | + // `fieldRules.ts` documents this for the canonical engine ("independent of |
| 150 | + // … the one-time-warning dedupe: it fires on every fault, so a caller doing |
| 151 | + // its own warn-once bookkeeping keeps control of it"). A caller that reports |
| 152 | + // per node must not have its faults swallowed by THIS module's bookkeeping. |
| 153 | + const src = '${nosuchroot.passback > 1}'; |
| 154 | + const reasons: string[] = []; |
| 155 | + const e = make(); |
| 156 | + |
| 157 | + for (let i = 0; i < 3; i++) { |
| 158 | + expect(e.evaluateCondition(src, { onFault: (r) => reasons.push(r) })).toBe(true); |
| 159 | + } |
| 160 | + |
| 161 | + expect(reasons).toHaveLength(3); |
| 162 | + expect(reasons.every((r) => r.includes('nosuchroot'))).toBe(true); |
| 163 | + // Supplying `onFault` still suppresses the built-in line entirely (#6038). |
| 164 | + expect(warn).not.toHaveBeenCalled(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('`throwOnError` still throws on EVERY evaluation — the fail-closed signal is not rate limited', () => { |
| 168 | + const src = '${nosuchroot.failclosed > 1}'; |
| 169 | + const e = make(); |
| 170 | + |
| 171 | + expect(() => e.evaluateCondition(src, { throwOnError: true })).toThrow(); |
| 172 | + expect(() => e.evaluateCondition(src, { throwOnError: true })).toThrow(); |
| 173 | + expect(warn).not.toHaveBeenCalled(); |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +describe('#6444 — the module reset this file depends on', () => { |
| 178 | + it('the dedupe survives new evaluator INSTANCES, and `vi.resetModules()` genuinely clears it', async () => { |
| 179 | + // Both halves matter. The first is why the fix works at all: a 200-row list |
| 180 | + // builds a fresh evaluator per row, so a per-instance Set would dedupe |
| 181 | + // nothing. The second is why every other cell in this file is a real |
| 182 | + // measurement rather than a reading of the previous cell's leftover entry. |
| 183 | + const src = '${nosuchroot.reset_probe > 1}'; |
| 184 | + const first = await import('../ExpressionEvaluator.js'); |
| 185 | + |
| 186 | + expect(new first.ExpressionEvaluator(SCOPE).evaluateCondition(src)).toBe(true); |
| 187 | + expect(new first.ExpressionEvaluator(SCOPE).evaluateCondition(src)).toBe(true); |
| 188 | + expect(warn).toHaveBeenCalledTimes(1); // distinct instances, one line |
| 189 | + |
| 190 | + vi.resetModules(); |
| 191 | + const second = await import('../ExpressionEvaluator.js'); |
| 192 | + expect(second.ExpressionEvaluator).not.toBe(first.ExpressionEvaluator); // really a new module |
| 193 | + |
| 194 | + expect(new second.ExpressionEvaluator(SCOPE).evaluateCondition(src)).toBe(true); |
| 195 | + expect(warn).toHaveBeenCalledTimes(2); // fresh Set → the SAME source warns again |
| 196 | + }); |
| 197 | +}); |
0 commit comments