|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { filterSheetRows } from '@/tools/google_sheets/filter' |
| 6 | + |
| 7 | +const VALUES: unknown[][] = [ |
| 8 | + ['Name', 'Email', 'Status', 'Score'], |
| 9 | + ['Alice', 'alice@example.com', 'Active', '90'], |
| 10 | + ['Bob', 'bob@test.com', 'Closed', '40'], |
| 11 | + ['Carol', 'carol@example.com', 'Active', '7'], |
| 12 | +] |
| 13 | + |
| 14 | +describe('filterSheetRows', () => { |
| 15 | + it('passes values through unchanged when no filter column is provided', () => { |
| 16 | + const result = filterSheetRows(VALUES, {}) |
| 17 | + expect(result.applied).toBe(false) |
| 18 | + expect(result.values).toBe(VALUES) |
| 19 | + expect(result.totalRows).toBe(3) |
| 20 | + }) |
| 21 | + |
| 22 | + it('passes through when filterValue is empty', () => { |
| 23 | + const result = filterSheetRows(VALUES, { filterColumn: 'Status', filterValue: '' }) |
| 24 | + expect(result.applied).toBe(false) |
| 25 | + expect(result.values).toBe(VALUES) |
| 26 | + }) |
| 27 | + |
| 28 | + it('defaults to case-insensitive contains', () => { |
| 29 | + const result = filterSheetRows(VALUES, { filterColumn: 'status', filterValue: 'active' }) |
| 30 | + expect(result.applied).toBe(true) |
| 31 | + expect(result.columnFound).toBe(true) |
| 32 | + expect(result.values).toEqual([VALUES[0], VALUES[1], VALUES[3]]) |
| 33 | + expect(result.matchedRows).toBe(2) |
| 34 | + }) |
| 35 | + |
| 36 | + it('matches column names case-insensitively and trims whitespace', () => { |
| 37 | + const result = filterSheetRows(VALUES, { |
| 38 | + filterColumn: ' EMAIL ', |
| 39 | + filterValue: 'example.com', |
| 40 | + }) |
| 41 | + expect(result.columnFound).toBe(true) |
| 42 | + expect(result.matchedRows).toBe(2) |
| 43 | + }) |
| 44 | + |
| 45 | + it('supports exact and not_equals', () => { |
| 46 | + expect( |
| 47 | + filterSheetRows(VALUES, { |
| 48 | + filterColumn: 'Status', |
| 49 | + filterValue: 'Active', |
| 50 | + filterMatchType: 'exact', |
| 51 | + }).matchedRows |
| 52 | + ).toBe(2) |
| 53 | + expect( |
| 54 | + filterSheetRows(VALUES, { |
| 55 | + filterColumn: 'Status', |
| 56 | + filterValue: 'Active', |
| 57 | + filterMatchType: 'not_equals', |
| 58 | + }).matchedRows |
| 59 | + ).toBe(1) |
| 60 | + }) |
| 61 | + |
| 62 | + it('supports starts_with, ends_with, and not_contains', () => { |
| 63 | + expect( |
| 64 | + filterSheetRows(VALUES, { |
| 65 | + filterColumn: 'Email', |
| 66 | + filterValue: 'bob', |
| 67 | + filterMatchType: 'starts_with', |
| 68 | + }).matchedRows |
| 69 | + ).toBe(1) |
| 70 | + expect( |
| 71 | + filterSheetRows(VALUES, { |
| 72 | + filterColumn: 'Email', |
| 73 | + filterValue: '.com', |
| 74 | + filterMatchType: 'ends_with', |
| 75 | + }).matchedRows |
| 76 | + ).toBe(3) |
| 77 | + expect( |
| 78 | + filterSheetRows(VALUES, { |
| 79 | + filterColumn: 'Email', |
| 80 | + filterValue: 'example.com', |
| 81 | + filterMatchType: 'not_contains', |
| 82 | + }).matchedRows |
| 83 | + ).toBe(1) |
| 84 | + }) |
| 85 | + |
| 86 | + it('compares numerically for ordering operators (not substring)', () => { |
| 87 | + const gt = filterSheetRows(VALUES, { |
| 88 | + filterColumn: 'Score', |
| 89 | + filterValue: '50', |
| 90 | + filterMatchType: 'gt', |
| 91 | + }) |
| 92 | + expect(gt.matchedRows).toBe(1) |
| 93 | + expect(gt.values).toEqual([VALUES[0], VALUES[1]]) |
| 94 | + |
| 95 | + expect( |
| 96 | + filterSheetRows(VALUES, { |
| 97 | + filterColumn: 'Score', |
| 98 | + filterValue: '40', |
| 99 | + filterMatchType: 'gte', |
| 100 | + }).matchedRows |
| 101 | + ).toBe(2) |
| 102 | + expect( |
| 103 | + filterSheetRows(VALUES, { |
| 104 | + filterColumn: 'Score', |
| 105 | + filterValue: '40', |
| 106 | + filterMatchType: 'lt', |
| 107 | + }).matchedRows |
| 108 | + ).toBe(1) |
| 109 | + expect( |
| 110 | + filterSheetRows(VALUES, { |
| 111 | + filterColumn: 'Score', |
| 112 | + filterValue: '40', |
| 113 | + filterMatchType: 'lte', |
| 114 | + }).matchedRows |
| 115 | + ).toBe(2) |
| 116 | + }) |
| 117 | + |
| 118 | + it('orders negative numbers correctly', () => { |
| 119 | + const temps: unknown[][] = [ |
| 120 | + ['City', 'Temp'], |
| 121 | + ['A', '-5'], |
| 122 | + ['B', '0'], |
| 123 | + ['C', '-12'], |
| 124 | + ['D', '3'], |
| 125 | + ] |
| 126 | + expect( |
| 127 | + filterSheetRows(temps, { filterColumn: 'Temp', filterValue: '-5', filterMatchType: 'gte' }) |
| 128 | + .matchedRows |
| 129 | + ).toBe(3) |
| 130 | + expect( |
| 131 | + filterSheetRows(temps, { filterColumn: 'Temp', filterValue: '0', filterMatchType: 'lt' }) |
| 132 | + .matchedRows |
| 133 | + ).toBe(2) |
| 134 | + }) |
| 135 | + |
| 136 | + it('excludes blank and non-numeric cells from numeric comparisons', () => { |
| 137 | + const scores: unknown[][] = [ |
| 138 | + ['Name', 'Score'], |
| 139 | + ['Alice', '90'], |
| 140 | + ['Bob', ''], |
| 141 | + ['Carol', 'N/A'], |
| 142 | + ['Dan', '60'], |
| 143 | + ] |
| 144 | + const result = filterSheetRows(scores, { |
| 145 | + filterColumn: 'Score', |
| 146 | + filterValue: '50', |
| 147 | + filterMatchType: 'gt', |
| 148 | + }) |
| 149 | + expect(result.matchedRows).toBe(2) |
| 150 | + expect(result.values).toEqual([scores[0], scores[1], scores[4]]) |
| 151 | + }) |
| 152 | + |
| 153 | + it('falls back to lexicographic ordering when values are not numeric (ISO dates)', () => { |
| 154 | + const dated: unknown[][] = [ |
| 155 | + ['Task', 'Due'], |
| 156 | + ['A', '2026-01-15'], |
| 157 | + ['B', '2026-03-01'], |
| 158 | + ['C', '2025-12-31'], |
| 159 | + ] |
| 160 | + const result = filterSheetRows(dated, { |
| 161 | + filterColumn: 'Due', |
| 162 | + filterValue: '2026-01-01', |
| 163 | + filterMatchType: 'gte', |
| 164 | + }) |
| 165 | + expect(result.matchedRows).toBe(2) |
| 166 | + }) |
| 167 | + |
| 168 | + it('reports columnFound=false and leaves values unchanged when the column is missing', () => { |
| 169 | + const result = filterSheetRows(VALUES, { |
| 170 | + filterColumn: 'Nonexistent', |
| 171 | + filterValue: 'x', |
| 172 | + }) |
| 173 | + expect(result.applied).toBe(false) |
| 174 | + expect(result.columnFound).toBe(false) |
| 175 | + expect(result.values).toBe(VALUES) |
| 176 | + expect(result.totalRows).toBe(3) |
| 177 | + }) |
| 178 | + |
| 179 | + it('handles a header-only sheet without error', () => { |
| 180 | + const headerOnly: unknown[][] = [['Name', 'Status']] |
| 181 | + const result = filterSheetRows(headerOnly, { filterColumn: 'Status', filterValue: 'Active' }) |
| 182 | + expect(result.applied).toBe(false) |
| 183 | + expect(result.totalRows).toBe(0) |
| 184 | + expect(result.values).toBe(headerOnly) |
| 185 | + }) |
| 186 | + |
| 187 | + it('treats missing cells as empty strings', () => { |
| 188 | + const sparse: unknown[][] = [['Name', 'Status'], ['Alice'], ['Bob', 'Active']] |
| 189 | + const result = filterSheetRows(sparse, { |
| 190 | + filterColumn: 'Status', |
| 191 | + filterValue: 'Active', |
| 192 | + filterMatchType: 'exact', |
| 193 | + }) |
| 194 | + expect(result.matchedRows).toBe(1) |
| 195 | + expect(result.values).toEqual([sparse[0], sparse[2]]) |
| 196 | + }) |
| 197 | + |
| 198 | + it('always retains the header row in filtered output', () => { |
| 199 | + const result = filterSheetRows(VALUES, { |
| 200 | + filterColumn: 'Status', |
| 201 | + filterValue: 'no-match', |
| 202 | + filterMatchType: 'exact', |
| 203 | + }) |
| 204 | + expect(result.values).toEqual([VALUES[0]]) |
| 205 | + expect(result.matchedRows).toBe(0) |
| 206 | + }) |
| 207 | +}) |
0 commit comments