|
| 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, vi } from 'vitest'; |
| 10 | +import { resolveContextTokens, resolveFilterPlaceholders } from '../filter-tokens'; |
| 11 | + |
| 12 | +const SCOPE = { currentUserId: 'usr_42', currentOrgId: 'org_7', onUnresolved: null }; |
| 13 | + |
| 14 | +describe('resolveContextTokens', () => { |
| 15 | + // The exact failure from framework #3574: a dashboard widget's filter is a |
| 16 | + // MongoDB-style OBJECT, and the predecessor helper bailed on non-arrays — |
| 17 | + // so the token reached SQL verbatim and the widget rendered 0. |
| 18 | + it('resolves inside an object-shaped widget filter', () => { |
| 19 | + expect( |
| 20 | + resolveContextTokens({ owner: '{current_user_id}', status: 'open' }, SCOPE), |
| 21 | + ).toEqual({ owner: 'usr_42', status: 'open' }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('resolves inside an array-shaped list-view filter', () => { |
| 25 | + expect( |
| 26 | + resolveContextTokens( |
| 27 | + [{ field: 'owner', operator: 'equals', value: '{current_user_id}' }], |
| 28 | + SCOPE, |
| 29 | + ), |
| 30 | + ).toEqual([{ field: 'owner', operator: 'equals', value: 'usr_42' }]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('resolves inside triple-shaped filters', () => { |
| 34 | + expect(resolveContextTokens([['owner', '=', '{current_user_id}']], SCOPE)).toEqual([ |
| 35 | + ['owner', '=', 'usr_42'], |
| 36 | + ]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('reaches nested $and / $or branches and operator objects', () => { |
| 40 | + expect( |
| 41 | + resolveContextTokens( |
| 42 | + { |
| 43 | + $and: [ |
| 44 | + { status: 'open' }, |
| 45 | + { $or: [{ owner: '{current_user_id}' }, { org: '{current_org_id}' }] }, |
| 46 | + ], |
| 47 | + reviewer: { $in: ['{current_user_id}', 'usr_9'] }, |
| 48 | + }, |
| 49 | + SCOPE, |
| 50 | + ), |
| 51 | + ).toEqual({ |
| 52 | + $and: [{ status: 'open' }, { $or: [{ owner: 'usr_42' }, { org: 'org_7' }] }], |
| 53 | + reviewer: { $in: ['usr_42', 'usr_9'] }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('accepts the ${token} spelling', () => { |
| 58 | + expect(resolveContextTokens({ owner: '${current_user_id}' }, SCOPE)).toEqual({ |
| 59 | + owner: 'usr_42', |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('leaves ordinary values, partial braces and non-strings alone', () => { |
| 64 | + const input = { |
| 65 | + name: 'Acme {Corp}', |
| 66 | + note: 'owner is {current_user_id} today', |
| 67 | + count: 3, |
| 68 | + active: true, |
| 69 | + missing: null, |
| 70 | + }; |
| 71 | + expect(resolveContextTokens(input, SCOPE)).toEqual(input); |
| 72 | + }); |
| 73 | + |
| 74 | + it('passes date macros through untouched — they are a separate vocabulary', () => { |
| 75 | + expect(resolveContextTokens({ created_at: { $gte: '{today}' } }, SCOPE)).toEqual({ |
| 76 | + created_at: { $gte: '{today}' }, |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('passes nav-only context-selector ids through silently', () => { |
| 81 | + const warn = vi.fn(); |
| 82 | + expect( |
| 83 | + resolveContextTokens({ pkg: '{active_package}' }, { ...SCOPE, onUnresolved: warn }), |
| 84 | + ).toEqual({ pkg: '{active_package}' }); |
| 85 | + expect(warn).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('does not mutate its input', () => { |
| 89 | + const input = { owner: '{current_user_id}' }; |
| 90 | + resolveContextTokens(input, SCOPE); |
| 91 | + expect(input).toEqual({ owner: '{current_user_id}' }); |
| 92 | + }); |
| 93 | + |
| 94 | + // Leaving the token yields an empty result set. Dropping the clause would |
| 95 | + // WIDEN the result set, which is far worse than silently narrowing. |
| 96 | + it('leaves a known token intact when scope has no value, and warns', () => { |
| 97 | + const warn = vi.fn(); |
| 98 | + expect( |
| 99 | + resolveContextTokens({ owner: '{current_user_id}' }, { onUnresolved: warn }), |
| 100 | + ).toEqual({ owner: '{current_user_id}' }); |
| 101 | + expect(warn).toHaveBeenCalledTimes(1); |
| 102 | + expect(warn.mock.calls[0][0]).toContain('current_user_id'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('warns with a suggestion on near-miss spellings', () => { |
| 106 | + const warn = vi.fn(); |
| 107 | + resolveContextTokens( |
| 108 | + { a: '{current_user}', b: '{user_id}', c: '{org_id}' }, |
| 109 | + { ...SCOPE, onUnresolved: warn }, |
| 110 | + ); |
| 111 | + expect(warn).toHaveBeenCalledTimes(3); |
| 112 | + expect(warn.mock.calls[0][0]).toContain('{current_user_id}'); |
| 113 | + expect(warn.mock.calls[2][0]).toContain('{current_org_id}'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('handles null / undefined filters', () => { |
| 117 | + expect(resolveContextTokens(null, SCOPE)).toBeNull(); |
| 118 | + expect(resolveContextTokens(undefined, SCOPE)).toBeUndefined(); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('resolveFilterPlaceholders', () => { |
| 123 | + // Calling only one resolver is the defect behind #3574 — dashboard widgets |
| 124 | + // expanded date macros and silently skipped session tokens. |
| 125 | + it('expands BOTH vocabularies in a single call', () => { |
| 126 | + const out = resolveFilterPlaceholders( |
| 127 | + { owner: '{current_user_id}', created_at: { $gte: '{today}' } }, |
| 128 | + SCOPE, |
| 129 | + new Date('2026-07-27T12:00:00Z'), |
| 130 | + ); |
| 131 | + expect(out.owner).toBe('usr_42'); |
| 132 | + expect(out.created_at.$gte).toMatch(/^\d{4}-\d{2}-\d{2}$/); |
| 133 | + expect(out.created_at.$gte).not.toBe('{today}'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('is a no-op on filters with no placeholders', () => { |
| 137 | + expect(resolveFilterPlaceholders({ status: 'open' }, SCOPE)).toEqual({ status: 'open' }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('handles null / undefined filters', () => { |
| 141 | + expect(resolveFilterPlaceholders(undefined, SCOPE)).toBeUndefined(); |
| 142 | + }); |
| 143 | +}); |
0 commit comments