|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29195 — switching a report to `joined` clears EVERY container key |
| 5 | + * the joined arm of `ReportSchema` refuses, `dateRange` and `exportOptions` |
| 6 | + * included, and `ReportSchema` is the judge of "every". |
| 7 | + * |
| 8 | + * The joined arm refuses a container `dateRange` and `exportOptions` whatever |
| 9 | + * their value (objectstack#26548: "a `joined` report takes no `dateRange`" / |
| 10 | + * "a `joined` report offers no export"), and the spec form's "Period & export" |
| 11 | + * section hides both on a joined report. The type picker cleared the six |
| 12 | + * selection / ordering / chart keys (objectui#10746) but not these two, so a |
| 13 | + * report that carried either one and was switched to Joined saved into a |
| 14 | + * refusal at a key no visible control reached. |
| 15 | + * |
| 16 | + * The spec exports no list of the refused keys, so this file does not keep a |
| 17 | + * second one. It builds a report carrying every top-level key a non-joined |
| 18 | + * report can carry (`MAXIMAL`), pins that fixture against the spec's own |
| 19 | + * property list (a key the spec adds later must join the fixture or the |
| 20 | + * named exclusion set), switches it to `joined` through the inspector, and |
| 21 | + * asks `ReportSchema` two things: the keys it refuses on the type-only switch |
| 22 | + * are exactly the keys the patch clears, and the switched report parses. |
| 23 | + * |
| 24 | + * Patches are read with `toStrictEqual`: `toEqual` treats an own key holding |
| 25 | + * `undefined` as absent, so it cannot tell a clear from a missing key. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 29 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 30 | +import userEvent from '@testing-library/user-event'; |
| 31 | +import { ReportSchema } from '@objectstack/spec/ui'; |
| 32 | +import { MetadataProtectionFields, OverrideDeclarationFields } from '@objectstack/spec/kernel'; |
| 33 | +import { ReportDefaultInspector } from './ReportDefaultInspector'; |
| 34 | +import { getReportSchema } from '../report-schema'; |
| 35 | +import type { DatasetCatalogEntry } from '../previews/useDatasetCatalog'; |
| 36 | + |
| 37 | +afterEach(cleanup); |
| 38 | + |
| 39 | +// One fetch double at MODULE scope, never torn down: `useDatasetSemantics` |
| 40 | +// fires a fire-and-forget read that no test body awaits (as in |
| 41 | +// `ReportDefaultInspector.test.tsx`). |
| 42 | +vi.stubGlobal( |
| 43 | + 'fetch', |
| 44 | + vi.fn(async () => new Response('null', { status: 404, headers: { 'content-type': 'application/json' } })), |
| 45 | +); |
| 46 | + |
| 47 | +const catalog: DatasetCatalogEntry[] = [ |
| 48 | + { |
| 49 | + name: 'sales_metrics', |
| 50 | + label: 'Sales metrics', |
| 51 | + dimensions: [ |
| 52 | + { name: 'stage', type: 'text' }, |
| 53 | + { name: 'close_quarter', type: 'date' }, |
| 54 | + ], |
| 55 | + measures: [ |
| 56 | + { name: 'total_amount', aggregate: 'sum' }, |
| 57 | + { name: 'deal_count', aggregate: 'count' }, |
| 58 | + ], |
| 59 | + }, |
| 60 | +]; |
| 61 | + |
| 62 | +const baseProps = { |
| 63 | + type: 'report', |
| 64 | + name: 'pipeline', |
| 65 | + locale: 'en-US' as const, |
| 66 | + onSelectionChange: vi.fn(), |
| 67 | + datasetCatalogOverride: catalog, |
| 68 | +}; |
| 69 | + |
| 70 | +/** One dataset-bound block: what a joined report needs before it saves. */ |
| 71 | +const block = { name: 'won_deals', dataset: 'sales_metrics', rows: ['stage'], values: ['total_amount'] }; |
| 72 | + |
| 73 | +/** |
| 74 | + * A matrix report carrying every top-level key a non-joined report can carry, |
| 75 | + * each with a value the spec accepts there. The `coverage` case below holds it |
| 76 | + * against the spec's property list. |
| 77 | + */ |
| 78 | +const MAXIMAL: Record<string, unknown> = { |
| 79 | + name: 'pipeline', |
| 80 | + label: 'Pipeline', |
| 81 | + description: 'Won deals by stage and quarter', |
| 82 | + type: 'matrix', |
| 83 | + dataset: 'sales_metrics', |
| 84 | + rows: ['stage'], |
| 85 | + columns: ['close_quarter'], |
| 86 | + values: ['total_amount'], |
| 87 | + runtimeFilter: { stage: 'won' }, |
| 88 | + order: [{ by: 'total_amount' }], |
| 89 | + drilldown: false, |
| 90 | + chart: { type: 'bar', xAxis: 'stage', yAxis: 'total_amount' }, |
| 91 | + dateRange: { dimension: 'close_quarter', defaultRange: 'this_quarter', allowCustomRange: false }, |
| 92 | + exportOptions: { formats: ['csv'], includeHeaders: true, fileNamePrefix: 'pipeline' }, |
| 93 | +}; |
| 94 | + |
| 95 | +/** |
| 96 | + * Spec properties `MAXIMAL` deliberately leaves out, each read from the spec |
| 97 | + * where it can be: `blocks` is the joined report's own key (added after the |
| 98 | + * switch below); `protection` is the package author's lock block, which the |
| 99 | + * save door refuses; the ADR-0010 envelope (`MetadataProtectionFields`) is set |
| 100 | + * by the loader; `overrides` is the ADR-0146 override declaration |
| 101 | + * (`OverrideDeclarationFields`). None of them is report-type data. |
| 102 | + */ |
| 103 | +const EXCLUDED = new Set([ |
| 104 | + 'blocks', |
| 105 | + 'protection', |
| 106 | + ...Object.keys(MetadataProtectionFields), |
| 107 | + ...Object.keys(OverrideDeclarationFields), |
| 108 | +]); |
| 109 | + |
| 110 | +type Patch = Record<string, unknown>; |
| 111 | + |
| 112 | +/** Mount on `draft`, pick `optionLabel` in the Report type control, hand back the one patch. */ |
| 113 | +async function switchType(draft: Record<string, unknown>, optionLabel: string): Promise<Patch> { |
| 114 | + const onPatch = vi.fn<(patch: Patch) => void>(); |
| 115 | + render(<ReportDefaultInspector {...baseProps} draft={draft} onPatch={onPatch} readOnly={false} />); |
| 116 | + await userEvent.click(screen.getByRole('combobox', { name: 'Report type' })); |
| 117 | + await userEvent.click(await screen.findByRole('option', { name: optionLabel })); |
| 118 | + expect(onPatch, 'the type picker commits exactly one patch').toHaveBeenCalledTimes(1); |
| 119 | + return onPatch.mock.calls[0][0]; |
| 120 | +} |
| 121 | + |
| 122 | +/** The hosts' shallow merge (`{ ...draft, ...patch }`). */ |
| 123 | +const merge = (draft: Record<string, unknown>, patch: Patch): Record<string, unknown> => ({ ...draft, ...patch }); |
| 124 | + |
| 125 | +/** What reaches the wire: the draft JSON-encoded, as `client.save` sends a body. */ |
| 126 | +const serialised = (doc: Record<string, unknown>): Record<string, unknown> => JSON.parse(JSON.stringify(doc)); |
| 127 | + |
| 128 | +/** Top-level keys `ReportSchema` refuses on `doc`, sorted. */ |
| 129 | +function refusedTopLevelKeys(doc: Record<string, unknown>): string[] { |
| 130 | + const parsed = ReportSchema.safeParse(doc); |
| 131 | + if (parsed.success) return []; |
| 132 | + return [...new Set(parsed.error.issues.map((i) => String(i.path[0])))].sort(); |
| 133 | +} |
| 134 | + |
| 135 | +/** Keys a patch clears (an own key holding `undefined`), sorted. */ |
| 136 | +const clearedKeys = (patch: Patch): string[] => |
| 137 | + Object.keys(patch) |
| 138 | + .filter((k) => patch[k] === undefined) |
| 139 | + .sort(); |
| 140 | + |
| 141 | +describe('ReportDefaultInspector — switching to `joined` clears every key the joined arm refuses (objectstack#29195)', () => { |
| 142 | + it('INSTRUMENT: the maximal fixture is a valid non-joined report', () => { |
| 143 | + const parsed = ReportSchema.safeParse(MAXIMAL); |
| 144 | + expect(parsed.success, JSON.stringify(parsed.success ? null : parsed.error.issues)).toBe(true); |
| 145 | + }); |
| 146 | + |
| 147 | + it('coverage: the fixture carries every spec property except the named exclusions', () => { |
| 148 | + const specKeys = Object.keys(getReportSchema().properties ?? {}).sort(); |
| 149 | + const covered = [...Object.keys(MAXIMAL), ...EXCLUDED].sort(); |
| 150 | + expect( |
| 151 | + covered, |
| 152 | + 'a top-level key the spec added joins MAXIMAL (with a valid non-joined value) or EXCLUDED (with its reason)', |
| 153 | + ).toStrictEqual(specKeys); |
| 154 | + }); |
| 155 | + |
| 156 | + it('SPEC AS JUDGE: the patch clears exactly the keys `ReportSchema` refuses on a joined container, and the switched report parses', async () => { |
| 157 | + const patch = await switchType(MAXIMAL, 'Joined'); |
| 158 | + // What the type alone would commit, with the blocks a joined report needs. |
| 159 | + const typeOnly = serialised({ ...MAXIMAL, type: 'joined', blocks: [block] }); |
| 160 | + const refused = refusedTopLevelKeys(typeOnly); |
| 161 | + expect(refused, 'INSTRUMENT CONTROL: the spec refuses the type-only switch').toContain('dateRange'); |
| 162 | + expect(clearedKeys(patch)).toStrictEqual(refused); |
| 163 | + |
| 164 | + const saved = serialised({ ...merge(MAXIMAL, patch), blocks: [block] }); |
| 165 | + const parsed = ReportSchema.safeParse(saved); |
| 166 | + expect(parsed.success, JSON.stringify(parsed.success ? null : parsed.error.issues)).toBe(true); |
| 167 | + // The keys the joined branch reads travel through. |
| 168 | + expect(saved).toMatchObject({ runtimeFilter: { stage: 'won' }, drilldown: false, description: MAXIMAL.description }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('THE DEFECT: a report with `dateRange` and `exportOptions` switched to `joined` saves without them, and the spec accepts the save', async () => { |
| 172 | + const draft = { |
| 173 | + name: 'pipeline', |
| 174 | + label: 'Pipeline', |
| 175 | + type: 'summary', |
| 176 | + dataset: 'sales_metrics', |
| 177 | + values: ['total_amount'], |
| 178 | + dateRange: { defaultRange: 'this_month' }, |
| 179 | + exportOptions: { formats: ['csv', 'xlsx'] }, |
| 180 | + blocks: [block], |
| 181 | + }; |
| 182 | + const patch = await switchType(draft, 'Joined'); |
| 183 | + expect(patch).toStrictEqual({ |
| 184 | + type: 'joined', |
| 185 | + dataset: undefined, |
| 186 | + values: undefined, |
| 187 | + dateRange: undefined, |
| 188 | + exportOptions: undefined, |
| 189 | + }); |
| 190 | + const saved = serialised(merge(draft, patch)); |
| 191 | + expect(saved).not.toHaveProperty('dateRange'); |
| 192 | + expect(saved).not.toHaveProperty('exportOptions'); |
| 193 | + const parsed = ReportSchema.safeParse(saved); |
| 194 | + expect(parsed.success, JSON.stringify(parsed.success ? null : parsed.error.issues)).toBe(true); |
| 195 | + }); |
| 196 | + |
| 197 | + it('an empty `dateRange` and an export turned off are cleared too: the joined arm refuses any present value', async () => { |
| 198 | + const draft = { name: 'pipeline', label: 'Pipeline', type: 'tabular', dateRange: {}, exportOptions: { formats: [] } }; |
| 199 | + const patch = await switchType(draft, 'Joined'); |
| 200 | + expect(patch).toStrictEqual({ type: 'joined', dateRange: undefined, exportOptions: undefined }); |
| 201 | + }); |
| 202 | + |
| 203 | + it('`joined` → non-joined brings neither key back: the patch is the type alone', async () => { |
| 204 | + const first = await switchType(MAXIMAL, 'Joined'); |
| 205 | + const joined = { ...serialised(merge(MAXIMAL, first)), blocks: [block] }; |
| 206 | + cleanup(); |
| 207 | + const patch = await switchType(joined, 'Summary'); |
| 208 | + expect(patch).toStrictEqual({ type: 'summary' }); |
| 209 | + const committed = merge(joined, patch); |
| 210 | + expect(committed).not.toHaveProperty('dateRange'); |
| 211 | + expect(committed).not.toHaveProperty('exportOptions'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('CONTROL: switching between two non-joined types keeps `dateRange` and `exportOptions`', async () => { |
| 215 | + const patch = await switchType(MAXIMAL, 'Summary'); |
| 216 | + expect(patch).toStrictEqual({ type: 'summary' }); |
| 217 | + expect(serialised(merge(MAXIMAL, patch))).toMatchObject({ |
| 218 | + dateRange: MAXIMAL.dateRange, |
| 219 | + exportOptions: MAXIMAL.exportOptions, |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments