|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20867] This package's two readers of a date-bucket key agree with |
| 5 | + * `@objectstack/core`'s one writer, `bucketDateKey`, for a year in 0001..0999. |
| 6 | + * |
| 7 | + * - `formatDateBucket` (`dimension-labels.ts`) relabels a date dimension's |
| 8 | + * grouped key on the published `queryDataset` path. Its year check admitted |
| 9 | + * only 1000..9999, so the key `0050` was read as epoch seconds and labelled |
| 10 | + * `1970`; a month or day key was re-spelled with its year unpadded (`50-06`, |
| 11 | + * `50-06-15`). It now labels every key the writer writes as written, and |
| 12 | + * what it relabels is spelled by the writer. |
| 13 | + * - `bucketDate` (`preview-evaluator.ts`) keys a drafted seed row on the draft |
| 14 | + * preview path. It spelled every key itself: the year unpadded (`50`, |
| 15 | + * `50-Q2`, `50-06`, `50-06-15`), and the week as the Monday's |
| 16 | + * `YYYY-MM-DD` where the runtime writes the ISO week label (`0050-W24`, |
| 17 | + * `2026-W25`). It now delegates to `bucketDateKey`, so the preview adopts the |
| 18 | + * runtime's ISO week label and a drafted chart keys a row the way the same |
| 19 | + * dataset does once published. |
| 20 | + * |
| 21 | + * Pins: 0050 (with 0050-01-01, whose ISO week is `0049-W52`) and 0999, with a |
| 22 | + * 2026 control. Every expected key is spelled literally. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, vi } from 'vitest'; |
| 26 | +import { bucketDateKey } from '@objectstack/core'; |
| 27 | +import { applyInMemoryAggregation } from '@objectstack/objectql'; |
| 28 | +import { DatasetSchema } from '@objectstack/spec/ui'; |
| 29 | +import type { ExecutionContext } from '@objectstack/spec/kernel'; |
| 30 | +import { AnalyticsService } from '../analytics-service.js'; |
| 31 | +import { formatDateBucket } from '../dimension-labels.js'; |
| 32 | +import { bucketDate } from '../preview-evaluator.js'; |
| 33 | + |
| 34 | +const GRANULARITIES = ['year', 'quarter', 'month', 'week', 'day'] as const; |
| 35 | +type Granularity = (typeof GRANULARITIES)[number]; |
| 36 | + |
| 37 | +/** The key the writer gives each instant, spelled literally (the reference). */ |
| 38 | +const WRITTEN: ReadonlyArray<readonly [string, Record<Granularity, string>]> = [ |
| 39 | + ['0050-06-15T10:00:00.000Z', { year: '0050', quarter: '0050-Q2', month: '0050-06', week: '0050-W24', day: '0050-06-15' }], |
| 40 | + ['0050-01-01T10:00:00.000Z', { year: '0050', quarter: '0050-Q1', month: '0050-01', week: '0049-W52', day: '0050-01-01' }], |
| 41 | + ['0999-06-15T10:00:00.000Z', { year: '0999', quarter: '0999-Q2', month: '0999-06', week: '0999-W24', day: '0999-06-15' }], |
| 42 | + // The control. |
| 43 | + ['2026-06-15T10:00:00.000Z', { year: '2026', quarter: '2026-Q2', month: '2026-06', week: '2026-W25', day: '2026-06-15' }], |
| 44 | +]; |
| 45 | + |
| 46 | +describe('[#20867] the reference: core writes these keys', () => { |
| 47 | + it.each(WRITTEN)('%s', (instant, keys) => { |
| 48 | + for (const g of GRANULARITIES) expect(bucketDateKey(instant, g), g).toBe(keys[g]); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('[#20867] formatDateBucket labels every key the writer writes as written', () => { |
| 53 | + it.each([ |
| 54 | + ['0050', 'year'], |
| 55 | + ['0050-06', 'month'], |
| 56 | + ['0050-06-15', 'day'], |
| 57 | + ] as const)('%s (%s) is labelled as written', (key, g) => { |
| 58 | + expect(formatDateBucket(key, g)).toBe(key); |
| 59 | + }); |
| 60 | + |
| 61 | + it.each(WRITTEN)('every granularity of %s', (_instant, keys) => { |
| 62 | + for (const g of GRANULARITIES) expect(formatDateBucket(keys[g], g), g).toBe(keys[g]); |
| 63 | + }); |
| 64 | + |
| 65 | + it.each([ |
| 66 | + // A raw value a date dimension groups by when no bucket was applied; the |
| 67 | + // label is the key the writer gives the same instant in UTC. |
| 68 | + ['0050-06-15T10:00:00.000Z', { year: '0050', quarter: '0050-Q2', month: '0050-06', day: '0050-06-15' }], |
| 69 | + ['0050-06-15', { year: '0050', quarter: '0050-Q2', month: '0050-06', day: '0050-06-15' }], |
| 70 | + // The control. |
| 71 | + ['2026-06-15T10:00:00.000Z', { year: '2026', quarter: '2026-Q2', month: '2026-06', day: '2026-06-15' }], |
| 72 | + ] as const)('relabels the raw value %s with the year in four digits', (raw, keys) => { |
| 73 | + for (const g of ['year', 'quarter', 'month', 'day'] as const) expect(formatDateBucket(raw, g), g).toBe(keys[g]); |
| 74 | + expect(formatDateBucket(raw, undefined)).toBe(keys.day); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe('[#20867] bucketDate delegates to the writer', () => { |
| 79 | + it.each(WRITTEN)('%s', (instant, keys) => { |
| 80 | + const ms = Date.parse(instant); |
| 81 | + for (const g of GRANULARITIES) { |
| 82 | + expect(bucketDate(instant, g), g).toBe(keys[g]); |
| 83 | + // The other two forms the writer reads, and the published path buckets. |
| 84 | + expect(bucketDate(ms, g), `${g} (epoch ms)`).toBe(keys[g]); |
| 85 | + expect(bucketDate(new Date(ms), g), `${g} (Date)`).toBe(keys[g]); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('resolves the calendar day in the reference zone, as the writer does', () => { |
| 90 | + // Sunday 0050-01-02 in UTC is Monday 0050-01-03 in Asia/Shanghai. |
| 91 | + expect(bucketDate('0050-01-02T20:00:00.000Z', 'week', 'UTC')).toBe('0049-W52'); |
| 92 | + expect(bucketDate('0050-01-02T20:00:00.000Z', 'week', 'Asia/Shanghai')).toBe('0050-W01'); |
| 93 | + expect(bucketDate('0050-01-02T20:00:00.000Z', 'day', 'Asia/Shanghai')).toBe('0050-01-03'); |
| 94 | + // The control. |
| 95 | + expect(bucketDate('2026-06-14T20:00:00.000Z', 'week', 'Asia/Shanghai')).toBe('2026-W25'); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('[#20867] a draft preview keys 0050-06-15 as the published path does', () => { |
| 100 | + const CTX = { tenantId: 'org_A' } as ExecutionContext; |
| 101 | + const DATASET = DatasetSchema.parse({ |
| 102 | + name: 'expense_ds', label: 'Expense', object: 'expense', include: [], |
| 103 | + dimensions: [{ name: 'spent_on', field: 'spent_on', type: 'date', dateGranularity: 'month' }], |
| 104 | + measures: [{ name: 'expense_count', aggregate: 'count' }], |
| 105 | + }); |
| 106 | + // A `date` value and a `datetime` value on the same calendar day, and the control. |
| 107 | + const ROWS = [ |
| 108 | + { spent_on: '0050-06-15', amount: 10 }, |
| 109 | + { spent_on: '0050-06-15T10:00:00.000Z', amount: 20 }, |
| 110 | + { spent_on: '2026-06-15', amount: 30 }, |
| 111 | + ]; |
| 112 | + |
| 113 | + /** The draft preview: the seed rows, evaluated in memory. */ |
| 114 | + const preview = new AnalyticsService({ draftRowsResolver: vi.fn(async () => ROWS) }); |
| 115 | + |
| 116 | + /** |
| 117 | + * The published path over the same rows: the ObjectQL strategy, the engine's |
| 118 | + * in-memory grouping (`@objectstack/objectql`) as `executeAggregate`, and the |
| 119 | + * dimension labels `queryDataset` resolves after grouping. |
| 120 | + */ |
| 121 | + const published = new AnalyticsService({ |
| 122 | + queryCapabilities: () => ({ nativeSql: false, objectqlAggregate: true, inMemory: false }), |
| 123 | + executeAggregate: async (_object, options) => |
| 124 | + applyInMemoryAggregation( |
| 125 | + ROWS, |
| 126 | + { |
| 127 | + groupBy: options.groupBy, |
| 128 | + aggregations: (options.aggregations ?? []).map((a) => ({ function: a.method, field: a.field, alias: a.alias })), |
| 129 | + }, |
| 130 | + options.timezone, |
| 131 | + ), |
| 132 | + sourceFieldMeta: (_o, f) => (f === 'spent_on' ? { type: 'date' } : undefined), |
| 133 | + labelResolver: { |
| 134 | + getObjectFields: (o) => (o === 'expense' ? { spent_on: { type: 'date' } } : undefined), |
| 135 | + fetchRecordLabels: async () => new Map(), |
| 136 | + }, |
| 137 | + }); |
| 138 | + |
| 139 | + const keysOf = (rows: Record<string, unknown>[]) => |
| 140 | + rows.map((r) => [r.spent_on, r.expense_count]).sort((a, b) => String(a[0]).localeCompare(String(b[0]))); |
| 141 | + |
| 142 | + it.each([ |
| 143 | + ['year', '0050', '2026'], |
| 144 | + ['quarter', '0050-Q2', '2026-Q2'], |
| 145 | + ['month', '0050-06', '2026-06'], |
| 146 | + ['week', '0050-W24', '2026-W25'], |
| 147 | + ['day', '0050-06-15', '2026-06-15'], |
| 148 | + ] as const)('%s: %s, and %s for the control', async (g, key, control) => { |
| 149 | + const selection = { dimensions: ['spent_on'], measures: ['expense_count'], dateGranularity: g }; |
| 150 | + const drafted = await preview.queryDataset(DATASET, selection, CTX, { previewDrafts: true }); |
| 151 | + const live = await published.queryDataset(DATASET, selection, CTX); |
| 152 | + const expected = [[key, 2], [control, 1]]; |
| 153 | + expect(keysOf(live.rows), 'published').toEqual(expected); |
| 154 | + expect(keysOf(drafted.rows), 'preview').toEqual(expected); |
| 155 | + }); |
| 156 | +}); |
0 commit comments