|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +import { MIGRATIONS_BY_MAJOR } from '../migrations/registry.js'; |
| 6 | +import { DatasetSchema } from '../ui/dataset.zod.js'; |
| 7 | +import { applyConversions } from './apply.js'; |
| 8 | +import { ALL_CONVERSIONS, CONVERSIONS_BY_MAJOR } from './registry.js'; |
| 9 | +import { applyConversionsToStoredItem } from './stored.js'; |
| 10 | +import type { ConversionNotice, ConversionTodoNotice } from './types.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * [#21220] `dataset-count-measure-empty-field-removed` — the D2 half of the |
| 14 | + * dataset `field` narrowing: the one sub-shape with a working row and a |
| 15 | + * lossless repair. |
| 16 | + * |
| 17 | + * A dataset measure's `field` is a column reference, so `''` is refused at |
| 18 | + * parse. A `count` measure with `field: ''` (the shape Studio's dataset |
| 19 | + * inspector stores when the Field box is left blank) parsed before, the |
| 20 | + * dataset door skipped it, and it compiled to the row count on SQLite's native |
| 21 | + * path. Dropping the key compiles it to `COUNT(*)`, the same row count. |
| 22 | + * |
| 23 | + * The fixture pair in `conversions.test.ts` proves before → after over the |
| 24 | + * whole table. This file pins it on a STORED row, the seam that replays it: |
| 25 | + * the key is dropped and the row then parses; everything outside the |
| 26 | + * sub-shape is the same reference and stays refused where it was. |
| 27 | + */ |
| 28 | + |
| 29 | +const ID = 'dataset-count-measure-empty-field-removed'; |
| 30 | +const D3_ID = 'dataset-member-field-expression-refused'; |
| 31 | + |
| 32 | +function storedDataset(measures: unknown[], dimensions: unknown[] = [{ name: 'stage', field: 'stage' }]) { |
| 33 | + return { name: 'deal_metrics', label: 'Deal Metrics', object: 'deal', dimensions, measures }; |
| 34 | +} |
| 35 | + |
| 36 | +function convertStored(row: Record<string, unknown>) { |
| 37 | + const notices: ConversionNotice[] = []; |
| 38 | + const todos: ConversionTodoNotice[] = []; |
| 39 | + const item = applyConversionsToStoredItem('dataset', row, { |
| 40 | + onNotice: (n) => notices.push(n), |
| 41 | + onTodo: (t) => todos.push(t), |
| 42 | + }); |
| 43 | + return { item, notices: notices.filter((n) => n.conversionId === ID), allNotices: notices, todos }; |
| 44 | +} |
| 45 | + |
| 46 | +function fieldIssuePaths(row: unknown): string[] { |
| 47 | + const r = DatasetSchema.safeParse(row); |
| 48 | + return r.success ? [] : r.error.issues.map((i) => i.path.join('.')).filter((p) => p.endsWith('field')); |
| 49 | +} |
| 50 | + |
| 51 | +describe('[#21220] dataset-count-measure-empty-field-removed (ADR-0087 D2)', () => { |
| 52 | + it('is registered for protocol 18, retired from the authoring load path, and linked from its D3 entry', () => { |
| 53 | + const entry = ALL_CONVERSIONS.find((c) => c.id === ID); |
| 54 | + expect(entry, 'the conversion is registered').toBeDefined(); |
| 55 | + expect(entry!.toMajor).toBe(18); |
| 56 | + expect(entry!.retiredFromLoadPath).toBe(true); |
| 57 | + expect(CONVERSIONS_BY_MAJOR[18]!.map((c) => c.id)).toContain(ID); |
| 58 | + expect(MIGRATIONS_BY_MAJOR[18]!.conversionIds).toContain(ID); |
| 59 | + const d3 = MIGRATIONS_BY_MAJOR[18]!.semantic.find((s) => s.id === D3_ID); |
| 60 | + expect(d3?.conversionIds).toEqual([ID]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('a stored count measure with `field: \'\'` loses the key, and the row then parses', () => { |
| 64 | + const row = storedDataset([ |
| 65 | + { name: 'deal_count', aggregate: 'count', field: '' }, |
| 66 | + { name: 'won_count', aggregate: 'count', field: '', filter: { stage: 'won' } }, |
| 67 | + ]); |
| 68 | + expect(fieldIssuePaths(row), 'refused before the replay').toEqual(['measures.0.field', 'measures.1.field']); |
| 69 | + const { item, notices, todos } = convertStored(row); |
| 70 | + const measures = (item as typeof row).measures as Array<Record<string, unknown>>; |
| 71 | + expect(measures).toEqual([ |
| 72 | + { name: 'deal_count', aggregate: 'count' }, |
| 73 | + { name: 'won_count', aggregate: 'count', filter: { stage: 'won' } }, |
| 74 | + ]); |
| 75 | + expect(notices.map((n) => [n.path, n.from, n.to])).toEqual([ |
| 76 | + ['datasets[0].measures[0].field', 'field', '(removed)'], |
| 77 | + ['datasets[0].measures[1].field', 'field', '(removed)'], |
| 78 | + ]); |
| 79 | + expect(todos).toEqual([]); |
| 80 | + expect(DatasetSchema.safeParse(item).success, 'parses after the replay').toBe(true); |
| 81 | + }); |
| 82 | + |
| 83 | + it('control: outside the sub-shape a row is the same reference — a sum or a dimension over `\'\'` stays refused', () => { |
| 84 | + const rows = [ |
| 85 | + storedDataset([{ name: 'blank_sum', aggregate: 'sum', field: '' }]), |
| 86 | + storedDataset([{ name: 'row_count', aggregate: 'count' }], [{ name: 'blank_axis', field: '' }]), |
| 87 | + storedDataset([{ name: 'star_count', aggregate: 'count', field: '*' }]), |
| 88 | + storedDataset([{ name: 'row_count', aggregate: 'count' }]), |
| 89 | + storedDataset([{ name: 'owner_count', aggregate: 'count', field: 'owner' }]), |
| 90 | + storedDataset([{ name: 'padded_count', aggregate: 'count', field: ' ' }]), |
| 91 | + storedDataset([{ name: 'expr_count', aggregate: 'count', field: 'COUNT(*)' }]), |
| 92 | + ]; |
| 93 | + for (const row of rows) { |
| 94 | + const { item, allNotices, todos } = convertStored(row); |
| 95 | + expect(item, JSON.stringify(row.measures)).toBe(row); |
| 96 | + expect(allNotices).toEqual([]); |
| 97 | + expect(todos).toEqual([]); |
| 98 | + } |
| 99 | + expect(fieldIssuePaths(rows[0])).toEqual(['measures.0.field']); |
| 100 | + expect(fieldIssuePaths(rows[1])).toEqual(['dimensions.0.field']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('is idempotent — the converted result replays to itself with no second notice', () => { |
| 104 | + const once = applyConversions( |
| 105 | + { datasets: [storedDataset([{ name: 'deal_count', aggregate: 'count', field: '' }])] }, |
| 106 | + { includeRetired: true }, |
| 107 | + ); |
| 108 | + const notices: ConversionNotice[] = []; |
| 109 | + const twice = applyConversions(once, { includeRetired: true, onNotice: (n) => notices.push(n) }); |
| 110 | + expect(twice).toBe(once); |
| 111 | + expect(notices).toEqual([]); |
| 112 | + }); |
| 113 | +}); |
0 commit comments