|
| 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 | + * Dashboard date presets read the spec's window table, not a local copy. |
| 11 | + * |
| 12 | + * `dashboard-filters` used to keep its own preset → bounds table |
| 13 | + * (`PRESET_RANGES`) holding the spec's thirteen windows in the `{current_*}` |
| 14 | + * period spelling. It now lowers every preset through |
| 15 | + * `DATE_RANGE_PRESET_MACRO_WINDOWS`, which spells the current period with the |
| 16 | + * bare aliases (`{month_start}`). These pins hold three things: |
| 17 | + * |
| 18 | + * 1. every preset emits exactly the spec's window, so there is one table; |
| 19 | + * 2. the new spelling resolves to the same calendar days as the retired one |
| 20 | + * on every preset, on reference dates that straddle week, month, quarter |
| 21 | + * and year boundaries, so the rows a dashboard returns are unchanged, and |
| 22 | + * so is the comparison window and trend label a metric widget derives |
| 23 | + * from the same condition; |
| 24 | + * 3. a stored dashboard that carries the `{current_*}` spelling itself, as a |
| 25 | + * custom range or as a bare macro default, still filters. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 29 | +import { DATE_RANGE_PRESET_MACRO_WINDOWS } from '@objectstack/spec/data'; |
| 30 | +import { |
| 31 | + buildFilterCondition, |
| 32 | + resolveDashboardFilterDefs, |
| 33 | + resetDashboardFilterWarnings, |
| 34 | + DATE_RANGE_FILTER_NAME, |
| 35 | + DATE_RANGE_PRESETS, |
| 36 | + type DashboardFilterDef, |
| 37 | +} from '../dashboard-filters'; |
| 38 | +import { resolveDateMacros } from '../date-macros'; |
| 39 | +import { shiftFilterByCompareTo, compareToTrendLabelKey } from '../compare-to'; |
| 40 | + |
| 41 | +const dateDef: DashboardFilterDef = { |
| 42 | + name: DATE_RANGE_FILTER_NAME, |
| 43 | + field: 'created_at', |
| 44 | + type: 'dateRange', |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * The retired local table, verbatim from `dashboard-filters.ts` at main |
| 49 | + * 39951d2a — the behaviour this change must keep. |
| 50 | + */ |
| 51 | +const RETIRED_PRESET_RANGES: Record<string, { from: string; to: string }> = { |
| 52 | + today: { from: '{today}', to: '{today}' }, |
| 53 | + yesterday: { from: '{yesterday}', to: '{yesterday}' }, |
| 54 | + this_week: { from: '{current_week_start}', to: '{current_week_end}' }, |
| 55 | + last_week: { from: '{last_week_start}', to: '{last_week_end}' }, |
| 56 | + this_month: { from: '{current_month_start}', to: '{current_month_end}' }, |
| 57 | + last_month: { from: '{last_month_start}', to: '{last_month_end}' }, |
| 58 | + this_quarter: { from: '{current_quarter_start}', to: '{current_quarter_end}' }, |
| 59 | + last_quarter: { from: '{last_quarter_start}', to: '{last_quarter_end}' }, |
| 60 | + this_year: { from: '{current_year_start}', to: '{current_year_end}' }, |
| 61 | + last_year: { from: '{last_year_start}', to: '{last_year_end}' }, |
| 62 | + last_7_days: { from: '{7_days_ago}', to: '{today}' }, |
| 63 | + last_30_days: { from: '{30_days_ago}', to: '{today}' }, |
| 64 | + last_90_days: { from: '{90_days_ago}', to: '{today}' }, |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Reference instants (local time, which is what `resolveDateMacros` reads): |
| 69 | + * a mid-quarter Wednesday, a Sunday that closes a week, a Monday that opens |
| 70 | + * one, the last day of a quarter, the first day of a year, and a leap day. |
| 71 | + */ |
| 72 | +const REFERENCE_NOWS = [ |
| 73 | + new Date(2026, 6, 15, 10, 30), |
| 74 | + new Date(2026, 6, 19, 23, 59), |
| 75 | + new Date(2026, 6, 20, 0, 0), |
| 76 | + new Date(2026, 8, 30, 12, 0), |
| 77 | + new Date(2027, 0, 1, 0, 0), |
| 78 | + new Date(2028, 1, 29, 8, 0), |
| 79 | +]; |
| 80 | + |
| 81 | +describe('dashboard date presets lower through the spec window table', () => { |
| 82 | + let warn: ReturnType<typeof vi.spyOn>; |
| 83 | + beforeEach(() => { |
| 84 | + resetDashboardFilterWarnings(); |
| 85 | + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 86 | + }); |
| 87 | + afterEach(() => { |
| 88 | + warn.mockRestore(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('covers the same thirteen presets the retired table did', () => { |
| 92 | + expect([...DATE_RANGE_PRESETS].sort()).toEqual(Object.keys(RETIRED_PRESET_RANGES).sort()); |
| 93 | + expect(DATE_RANGE_PRESETS).toHaveLength(13); |
| 94 | + }); |
| 95 | + |
| 96 | + it.each([...DATE_RANGE_PRESETS])('emits the spec window for %s', (preset) => { |
| 97 | + const [start, end] = DATE_RANGE_PRESET_MACRO_WINDOWS[preset]; |
| 98 | + expect(buildFilterCondition(dateDef, { preset })).toEqual({ $gte: start, $lte: end }); |
| 99 | + expect(warn).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it.each([...DATE_RANGE_PRESETS])( |
| 103 | + '%s resolves to the same days under the retired {current_*} spelling', |
| 104 | + (preset) => { |
| 105 | + const retired = RETIRED_PRESET_RANGES[preset]; |
| 106 | + const retiredCondition = { $gte: retired.from, $lte: retired.to }; |
| 107 | + const condition = buildFilterCondition(dateDef, { preset }) as { $gte: string; $lte: string }; |
| 108 | + for (const now of REFERENCE_NOWS) { |
| 109 | + const resolved = resolveDateMacros(condition, now); |
| 110 | + // A real date on both bounds, not a token passed through unresolved. |
| 111 | + expect(resolved.$gte, `${preset} @ ${now.toString()}`).toMatch(/^\d{4}-\d{2}-\d{2}$/); |
| 112 | + expect(resolved.$lte, `${preset} @ ${now.toString()}`).toMatch(/^\d{4}-\d{2}-\d{2}$/); |
| 113 | + expect(resolved, `${preset} @ ${now.toString()}`).toEqual(resolveDateMacros(retiredCondition, now)); |
| 114 | + } |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + it.each([...DATE_RANGE_PRESETS])( |
| 119 | + '%s keeps its previous-period window and trend label for a compareTo widget', |
| 120 | + (preset) => { |
| 121 | + const retired = RETIRED_PRESET_RANGES[preset]; |
| 122 | + const retiredFilter = { created_at: { $gte: retired.from, $lte: retired.to } }; |
| 123 | + const filter = { created_at: buildFilterCondition(dateDef, { preset }) }; |
| 124 | + for (const now of REFERENCE_NOWS) { |
| 125 | + for (const kind of ['previousPeriod', 'previousYear'] as const) { |
| 126 | + expect(shiftFilterByCompareTo(filter, { kind }, now), `${preset} ${kind} @ ${now.toString()}`).toEqual( |
| 127 | + shiftFilterByCompareTo(retiredFilter, { kind }, now), |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + expect(compareToTrendLabelKey({ kind: 'previousPeriod' }, filter)).toBe( |
| 132 | + compareToTrendLabelKey({ kind: 'previousPeriod' }, retiredFilter), |
| 133 | + ); |
| 134 | + }, |
| 135 | + ); |
| 136 | + |
| 137 | + it('an unknown preset name still misses, prototype member names included', () => { |
| 138 | + for (const preset of ['last_60_days', 'This_Month', 'constructor', 'toString', '__proto__']) { |
| 139 | + warn.mockClear(); |
| 140 | + expect(buildFilterCondition(dateDef, { preset }), preset).toBeUndefined(); |
| 141 | + expect(warn, preset).toHaveBeenCalledTimes(1); |
| 142 | + expect(String(warn.mock.calls[0][0]), preset).toContain(`unknown date range preset "${preset}"`); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + it('a non-preset date default is not lifted to a preset, prototype member names included', () => { |
| 147 | + for (const defaultValue of ['last_60_days', 'constructor', 'toString']) { |
| 148 | + const [def] = resolveDashboardFilterDefs({ |
| 149 | + globalFilters: [{ name: 'when', field: 'created_at', type: 'date', defaultValue }], |
| 150 | + } as any); |
| 151 | + expect(def.defaultValue, defaultValue).toBe(defaultValue); |
| 152 | + } |
| 153 | + const [def] = resolveDashboardFilterDefs({ |
| 154 | + globalFilters: [{ name: 'when', field: 'created_at', type: 'date', defaultValue: 'this_quarter' }], |
| 155 | + } as any); |
| 156 | + expect(def.defaultValue).toEqual({ preset: 'this_quarter' }); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe('a stored dashboard written in the {current_*} spelling still filters', () => { |
| 161 | + beforeEach(() => resetDashboardFilterWarnings()); |
| 162 | + |
| 163 | + it('a custom range bounded by {current_*} tokens selects the same days as the preset', () => { |
| 164 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 165 | + try { |
| 166 | + const stored = { from: '{current_month_start}', to: '{current_month_end}' }; |
| 167 | + const condition = buildFilterCondition(dateDef, stored); |
| 168 | + expect(condition).toEqual({ $gte: '{current_month_start}', $lte: '{current_month_end}' }); |
| 169 | + for (const now of REFERENCE_NOWS) { |
| 170 | + expect(resolveDateMacros(condition, now)).toEqual( |
| 171 | + resolveDateMacros(buildFilterCondition(dateDef, { preset: 'this_month' }), now), |
| 172 | + ); |
| 173 | + } |
| 174 | + expect(warn).not.toHaveBeenCalled(); |
| 175 | + } finally { |
| 176 | + warn.mockRestore(); |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + it('a date global filter defaulting to a {current_*} token keeps it and passes it through', () => { |
| 181 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 182 | + try { |
| 183 | + const [def] = resolveDashboardFilterDefs({ |
| 184 | + globalFilters: [ |
| 185 | + { name: 'opened', field: 'created_at', type: 'date', defaultValue: '{current_quarter_start}' }, |
| 186 | + ], |
| 187 | + } as any); |
| 188 | + expect(def.defaultValue).toBe('{current_quarter_start}'); |
| 189 | + const condition = buildFilterCondition(def, def.defaultValue); |
| 190 | + expect(condition).toBe('{current_quarter_start}'); |
| 191 | + const now = new Date(2026, 6, 15, 10, 30); |
| 192 | + expect(resolveDateMacros(condition, now)).toBe('2026-07-01'); |
| 193 | + expect(resolveDateMacros('{quarter_start}', now)).toBe('2026-07-01'); |
| 194 | + expect(warn).not.toHaveBeenCalled(); |
| 195 | + } finally { |
| 196 | + warn.mockRestore(); |
| 197 | + } |
| 198 | + }); |
| 199 | +}); |
0 commit comments