|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { FieldSelector } from '../../src/core/fieldSelector'; |
| 3 | +import { TableConfig } from '../../src/types/table'; |
| 4 | +import type { Column, SQL } from 'drizzle-orm'; |
| 5 | + |
| 6 | +// ── Minimal column stubs ── |
| 7 | +// FieldSelector only checks presence in `selection` object keys — |
| 8 | +// the actual Drizzle Column value doesn't matter for this unit test. |
| 9 | +function makeCol(name: string): SQL { |
| 10 | + return { name } as unknown as SQL; |
| 11 | +} |
| 12 | + |
| 13 | +const selector = new FieldSelector(); |
| 14 | + |
| 15 | +// ── Base-only config ── |
| 16 | +const baseConfig: TableConfig = { |
| 17 | + name: 'users', |
| 18 | + base: 'users', |
| 19 | + columns: [ |
| 20 | + { name: 'id', type: 'uuid', hidden: false, sortable: true, filterable: true }, |
| 21 | + { name: 'name', type: 'string', hidden: false, sortable: true, filterable: true }, |
| 22 | + { name: 'secret', type: 'string', hidden: true, sortable: false, filterable: false }, |
| 23 | + ], |
| 24 | +}; |
| 25 | + |
| 26 | +// ── Config with a join ── |
| 27 | +const joinConfig: TableConfig = { |
| 28 | + name: 'orders', |
| 29 | + base: 'orders', |
| 30 | + columns: [ |
| 31 | + { name: 'id', type: 'number', hidden: false, sortable: true, filterable: true }, |
| 32 | + { name: 'status', type: 'string', hidden: false, sortable: true, filterable: true }, |
| 33 | + ], |
| 34 | + joins: [{ |
| 35 | + table: 'users', |
| 36 | + type: 'left', |
| 37 | + on: 'orders.user_id = users.id', |
| 38 | + columns: [ |
| 39 | + { name: 'email', type: 'string', hidden: false, sortable: true, filterable: true }, |
| 40 | + { name: 'role', type: 'string', hidden: false, sortable: false, filterable: true }, |
| 41 | + { name: 'internalNote', type: 'string', hidden: true, sortable: false, filterable: false }, |
| 42 | + ], |
| 43 | + }], |
| 44 | +}; |
| 45 | + |
| 46 | +// ── Full selection objects (simulates what queryBuilder.buildSelect returns) ── |
| 47 | +const baseSelection = { |
| 48 | + id: makeCol('id'), |
| 49 | + name: makeCol('name'), |
| 50 | + secret: makeCol('secret'), |
| 51 | +}; |
| 52 | + |
| 53 | +const joinSelection = { |
| 54 | + id: makeCol('id'), |
| 55 | + status: makeCol('status'), |
| 56 | + email: makeCol('email'), |
| 57 | + role: makeCol('role'), |
| 58 | + internalNote: makeCol('internalNote'), |
| 59 | +}; |
| 60 | + |
| 61 | +// ═══════════════════════════════════════════════════════════ |
| 62 | +// applyFieldSelection — base table only |
| 63 | +// ═══════════════════════════════════════════════════════════ |
| 64 | +describe('FieldSelector.applyFieldSelection — base columns', () => { |
| 65 | + it('returns full selection when no fields requested', () => { |
| 66 | + const result = selector.applyFieldSelection(baseSelection, undefined, baseConfig); |
| 67 | + expect(Object.keys(result)).toEqual(['id', 'name', 'secret']); |
| 68 | + }); |
| 69 | + |
| 70 | + it('narrows selection to requested base columns', () => { |
| 71 | + const result = selector.applyFieldSelection(baseSelection, ['name'], baseConfig); |
| 72 | + // PK guard always injects 'id'; exact membership must be correct |
| 73 | + expect(Object.keys(result).sort()).toEqual(['id', 'name'].sort()); |
| 74 | + }); |
| 75 | + |
| 76 | + it('excludes hidden base columns even when explicitly requested', () => { |
| 77 | + const result = selector.applyFieldSelection(baseSelection, ['secret'], baseConfig); |
| 78 | + // 'secret' is hidden — filtered is empty after the loop, but the PK guard |
| 79 | + // injects 'id', so the result contains only the primary key. |
| 80 | + expect(Object.keys(result)).toEqual(['id']); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +// ═══════════════════════════════════════════════════════════ |
| 85 | +// applyFieldSelection — join columns (the previously-broken path) |
| 86 | +// ═══════════════════════════════════════════════════════════ |
| 87 | +describe('FieldSelector.applyFieldSelection — join columns (bug fix)', () => { |
| 88 | + it('includes a valid join column in the narrowed selection', () => { |
| 89 | + const result = selector.applyFieldSelection(joinSelection, ['id', 'email'], joinConfig); |
| 90 | + // Exact: only the requested fields plus PK (already included in request) |
| 91 | + expect(Object.keys(result).sort()).toEqual(['email', 'id'].sort()); |
| 92 | + }); |
| 93 | + |
| 94 | + it('includes base and join columns together', () => { |
| 95 | + const result = selector.applyFieldSelection(joinSelection, ['status', 'email', 'role'], joinConfig); |
| 96 | + // Exact: requested fields + PK guard injects 'id' |
| 97 | + expect(Object.keys(result).sort()).toEqual(['email', 'id', 'role', 'status'].sort()); |
| 98 | + }); |
| 99 | + |
| 100 | + it('excludes a hidden join column even if requested', () => { |
| 101 | + const result = selector.applyFieldSelection(joinSelection, ['internalNote'], joinConfig); |
| 102 | + // 'internalNote' is hidden — filtered is empty after the loop, but the PK guard |
| 103 | + // injects 'id', so the result contains only the primary key. |
| 104 | + expect(Object.keys(result)).toEqual(['id']); |
| 105 | + }); |
| 106 | + |
| 107 | + it('includes join columns from a nested join', () => { |
| 108 | + const nestedConfig: TableConfig = { |
| 109 | + name: 'orders', |
| 110 | + base: 'orders', |
| 111 | + columns: [ |
| 112 | + { name: 'id', type: 'number', hidden: false, sortable: true, filterable: true }, |
| 113 | + ], |
| 114 | + joins: [{ |
| 115 | + table: 'users', |
| 116 | + type: 'left', |
| 117 | + on: 'orders.user_id = users.id', |
| 118 | + columns: [], |
| 119 | + joins: [{ |
| 120 | + table: 'profiles', |
| 121 | + type: 'left', |
| 122 | + on: 'users.id = profiles.user_id', |
| 123 | + columns: [ |
| 124 | + { name: 'avatarUrl', type: 'string', hidden: false, sortable: false, filterable: false }, |
| 125 | + ], |
| 126 | + }], |
| 127 | + }], |
| 128 | + }; |
| 129 | + |
| 130 | + const selection = { |
| 131 | + id: makeCol('id'), |
| 132 | + avatarUrl: makeCol('avatarUrl'), |
| 133 | + }; |
| 134 | + |
| 135 | + const result = selector.applyFieldSelection(selection, ['id', 'avatarUrl'], nestedConfig); |
| 136 | + // Exact: both requested fields (no PK injection since 'id' already in request) |
| 137 | + expect(Object.keys(result).sort()).toEqual(['avatarUrl', 'id'].sort()); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +// ═══════════════════════════════════════════════════════════ |
| 142 | +// filterResponseFields — post-fetch field filtering (unchanged by this fix) |
| 143 | +// ═══════════════════════════════════════════════════════════ |
| 144 | +describe('FieldSelector.filterResponseFields', () => { |
| 145 | + const rows = [ |
| 146 | + { id: 1, name: 'Alice', email: 'alice@example.com', secret: 'x' }, |
| 147 | + { id: 2, name: 'Bob', email: 'bob@example.com', secret: 'y' }, |
| 148 | + ]; |
| 149 | + |
| 150 | + it('returns all rows unchanged when no fields requested', () => { |
| 151 | + const result = selector.filterResponseFields(rows, undefined); |
| 152 | + expect(result).toEqual(rows); |
| 153 | + }); |
| 154 | + |
| 155 | + it('strips fields not in requested set', () => { |
| 156 | + const result = selector.filterResponseFields(rows, ['id', 'email']); |
| 157 | + expect(result[0]).toEqual({ id: 1, email: 'alice@example.com' }); |
| 158 | + expect(result[1]).toEqual({ id: 2, email: 'bob@example.com' }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('silently ignores requested fields not present in row', () => { |
| 162 | + const result = selector.filterResponseFields(rows, ['id', 'nonexistent']); |
| 163 | + expect(result[0]).toEqual({ id: 1 }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments