|
| 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 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { ExpressionEvaluator } from '@object-ui/core'; |
| 11 | +import { evaluateVisibility } from './ExpressionProvider'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Regression: nav/area `visible` predicates arrive from the server as |
| 15 | + * `{ dialect: 'cel', source }` envelopes (the spec's ExpressionInputSchema |
| 16 | + * normalizes every authored string into that shape). evaluateVisibility used |
| 17 | + * to fall through to a blanket `return true` for anything that wasn't a |
| 18 | + * `${…}` template string — so a constant-false CEL predicate still rendered |
| 19 | + * the menu item for everyone, and "hide this nav item by role" was |
| 20 | + * unimplementable from app metadata. |
| 21 | + */ |
| 22 | + |
| 23 | +function makeEvaluator(user: Record<string, unknown>) { |
| 24 | + const context = { current_user: user, user, ctx: { user }, os: { user }, app: {}, data: {}, features: {} }; |
| 25 | + return new ExpressionEvaluator(context as any); |
| 26 | +} |
| 27 | + |
| 28 | +describe('evaluateVisibility', () => { |
| 29 | + const worker = makeEvaluator({ id: 'u1', positions: ['worker'] }); |
| 30 | + const orgAdmin = makeEvaluator({ id: 'u2', positions: ['org_admin'] }); |
| 31 | + |
| 32 | + it('keeps literal handling: booleans and "true"/"false" strings', () => { |
| 33 | + expect(evaluateVisibility(undefined, worker)).toBe(true); |
| 34 | + expect(evaluateVisibility(true, worker)).toBe(true); |
| 35 | + expect(evaluateVisibility('true', worker)).toBe(true); |
| 36 | + expect(evaluateVisibility(false, worker)).toBe(false); |
| 37 | + expect(evaluateVisibility('false', worker)).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('evaluates a CEL envelope against current_user (spec P`…` form)', () => { |
| 41 | + const visible = { dialect: 'cel', source: "'org_admin' in current_user.positions" }; |
| 42 | + expect(evaluateVisibility(visible, orgAdmin)).toBe(true); |
| 43 | + expect(evaluateVisibility(visible, worker)).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('still evaluates ${…} template expressions', () => { |
| 47 | + const evaluator = makeEvaluator({ role: 'admin' }); |
| 48 | + expect(evaluateVisibility("${user.role === 'admin'}", evaluator)).toBe(true); |
| 49 | + expect(evaluateVisibility("${user.role === 'guest'}", evaluator)).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('fails open (visible) on an unevaluable predicate', () => { |
| 53 | + expect(evaluateVisibility({ dialect: 'cel', source: 'not ] valid (' }, worker)).toBe(true); |
| 54 | + }); |
| 55 | +}); |
0 commit comments