|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#35008 — the report quick edit edits the report as its author |
| 5 | + * wrote it, never the report as it is served. |
| 6 | + * |
| 7 | + * ## The defect this pins |
| 8 | + * |
| 9 | + * `ReportView` seeded its editor with `reportData`, the report from the served |
| 10 | + * `/meta/report` list. In a zh-CN session that read is translated (`label`, |
| 11 | + * `description`, each block's `label`), and the save door stores a report body |
| 12 | + * verbatim, so a quick edit of any one field stored the translations as the |
| 13 | + * source text: measured live, an en reader was served `任务概览(联合)` after |
| 14 | + * the publish. A pending draft took the same route a second way: the draft |
| 15 | + * bar's resume handed the inspector the SERVED draft, translated likewise. |
| 16 | + * |
| 17 | + * ## What runs |
| 18 | + * |
| 19 | + * The real `ReportView`, the real `ReportConfigPanel`, the real |
| 20 | + * `RuntimeDraftBar` and the real persistence seam, over a metadata client |
| 21 | + * double: its served reads (the list, `get(…, { state: 'draft' })`) answer the |
| 22 | + * translated report, its layered read the stored source. The panel's spec |
| 23 | + * inspector is reduced to one edit of `description` that also shows the label |
| 24 | + * and block labels it was handed. |
| 25 | + * |
| 26 | + * Direction, written before the run: with the buffer seeded from `reportData` |
| 27 | + * (the unmodified tree) the first two cases go RED on the stored labels, and |
| 28 | + * the description control stays GREEN; with the panel's draft-bar resume |
| 29 | + * restored, the pending-draft case goes RED. |
| 30 | + */ |
| 31 | + |
| 32 | +import * as React from 'react'; |
| 33 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 34 | +import { render, screen, waitFor, fireEvent, act, cleanup } from '@testing-library/react'; |
| 35 | +import { toast } from 'sonner'; |
| 36 | + |
| 37 | +vi.mock('sonner', () => ({ |
| 38 | + toast: Object.assign(vi.fn(), { |
| 39 | + success: vi.fn(), error: vi.fn(), info: vi.fn(), |
| 40 | + warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(), |
| 41 | + }), |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock('@object-ui/plugin-report', async (importOriginal) => ({ |
| 45 | + ...(await importOriginal<Record<string, unknown>>()), |
| 46 | + ReportRenderer: () => null, |
| 47 | +})); |
| 48 | +vi.mock('@object-ui/plugin-dashboard', async (importOriginal) => ({ |
| 49 | + ...(await importOriginal<Record<string, unknown>>()), |
| 50 | + DrillDownDrawer: () => null, |
| 51 | +})); |
| 52 | +// The panel's spec inspector, reduced to one edit of an unrelated field. |
| 53 | +vi.mock('./metadata-admin/inspectors/ReportDefaultInspector', () => ({ |
| 54 | + ReportDefaultInspector: ({ onPatch, draft }: any) => ( |
| 55 | + <button |
| 56 | + type="button" |
| 57 | + data-testid="stub-report-edit" |
| 58 | + data-label={String(draft?.label)} |
| 59 | + data-blocks={JSON.stringify((draft?.blocks ?? []).map((b: any) => b?.label))} |
| 60 | + onClick={() => onPatch({ description: 'Edited description' })} |
| 61 | + > |
| 62 | + edit |
| 63 | + </button> |
| 64 | + ), |
| 65 | +})); |
| 66 | + |
| 67 | +const meta = vi.hoisted(() => ({ value: null as any })); |
| 68 | +vi.mock('../providers/MetadataProvider', () => ({ useMetadata: () => meta.value })); |
| 69 | + |
| 70 | +vi.mock('react-router-dom', () => ({ |
| 71 | + useParams: () => ({ reportName: 'task_overview' }), |
| 72 | + useNavigate: () => vi.fn(), |
| 73 | + useLocation: () => ({ pathname: '/apps/com.qa.tr/report/task_overview', search: '' }), |
| 74 | +})); |
| 75 | + |
| 76 | +vi.mock('./useOpenRecordList', () => ({ |
| 77 | + useRecordListDrillNavigation: () => ({ openRecordList: vi.fn(), canOpenRecordList: () => true }), |
| 78 | +})); |
| 79 | +vi.mock('./MetadataInspector', () => ({ |
| 80 | + MetadataPanel: () => null, |
| 81 | + useMetadataInspector: () => ({ showDebug: false }), |
| 82 | +})); |
| 83 | +const client = vi.hoisted(() => ({ value: null as any })); |
| 84 | +vi.mock('./metadata-admin/useMetadata', () => ({ useMetadataClient: () => client.value })); |
| 85 | +vi.mock('../providers/AdapterProvider', () => ({ useAdapter: () => ({}) })); |
| 86 | +vi.mock('../providers/ExpressionProvider', () => ({ useExpressionContext: () => ({ app: undefined }) })); |
| 87 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 88 | + ...(await importOriginal<Record<string, unknown>>()), |
| 89 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 90 | +})); |
| 91 | + |
| 92 | +// `ReportView` reaches a dynamic `import('@objectstack/lint')` (through the panel's |
| 93 | +// inspector): pay its load at collection, never inside a case's clocked window |
| 94 | +// (`check:test-source-alias`). |
| 95 | +import '@objectstack/lint'; |
| 96 | +import { ReportView } from './ReportView'; |
| 97 | + |
| 98 | +const PKG = 'com.qa.tr'; |
| 99 | + |
| 100 | +/** The report as its author stored it (en source). */ |
| 101 | +const SOURCE = { |
| 102 | + name: 'task_overview', |
| 103 | + label: 'Task Overview (Joined)', |
| 104 | + description: 'Multiple task sub-reports stacked into one joined view.', |
| 105 | + type: 'joined', |
| 106 | + blocks: [ |
| 107 | + { name: 'open_block', label: 'Open Tasks', type: 'summary', dataset: 'task_metrics', rows: ['status'], values: ['est_hours'] }, |
| 108 | + { name: 'done_block', label: 'Completed Tasks', type: 'summary', dataset: 'task_metrics', rows: ['status'], values: ['task_count'] }, |
| 109 | + ], |
| 110 | +}; |
| 111 | + |
| 112 | +/** The same report as the zh-CN read serves it: translated, with the read's decorations. */ |
| 113 | +function served(doc: typeof SOURCE) { |
| 114 | + return { |
| 115 | + ...doc, |
| 116 | + label: '任务概览(联合)', |
| 117 | + description: '将多个任务子报表堆叠在一个联合视图中。', |
| 118 | + blocks: [ |
| 119 | + { ...doc.blocks[0], label: '未完成任务' }, |
| 120 | + { ...doc.blocks[1], label: '已完成任务' }, |
| 121 | + ], |
| 122 | + _packageId: PKG, |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +const refresh = vi.fn(async () => {}); |
| 127 | + |
| 128 | +function mountReport() { |
| 129 | + meta.value = { |
| 130 | + apps: [], |
| 131 | + objects: [], |
| 132 | + dashboards: [], |
| 133 | + reports: [served(SOURCE)], |
| 134 | + pages: [], |
| 135 | + loading: false, |
| 136 | + error: null, |
| 137 | + refresh, |
| 138 | + invalidate: () => {}, |
| 139 | + ensureType: async () => [], |
| 140 | + getItem: vi.fn(async () => null), |
| 141 | + getItemsByType: () => [], |
| 142 | + getTypeStatus: () => 'ready', |
| 143 | + }; |
| 144 | + render(<ReportView dataSource={{ find: vi.fn(async () => ({ data: [] })) } as any} />); |
| 145 | +} |
| 146 | + |
| 147 | +/** A client whose served reads translate and whose layered read is the stored row. */ |
| 148 | +function makeClient({ draft }: { draft: typeof SOURCE | null }) { |
| 149 | + return { |
| 150 | + // The served draft read (`RuntimeDraftBar`): translated, like every served read. |
| 151 | + get: vi.fn(async () => (draft ? served(draft) : null)), |
| 152 | + layered: vi.fn(async () => ({ code: null, overlay: null, overlayScope: null, effective: SOURCE, draft })), |
| 153 | + save: vi.fn(async () => ({})), |
| 154 | + publish: vi.fn(async () => ({})), |
| 155 | + reset: vi.fn(async () => ({})), |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +async function openEditAndSave() { |
| 160 | + fireEvent.click(await screen.findByTestId('report-edit-button')); |
| 161 | + await screen.findByTestId('report-config-panel'); |
| 162 | + // Let the draft bar's read settle, so a resume (if any) has landed first. |
| 163 | + await waitFor(() => expect(client.value.get).toHaveBeenCalled()); |
| 164 | + await act(async () => {}); |
| 165 | + fireEvent.click(screen.getByTestId('stub-report-edit')); |
| 166 | + await act(async () => { |
| 167 | + fireEvent.click(screen.getByTestId('report-config-save')); |
| 168 | + }); |
| 169 | + await waitFor(() => expect(client.value.save).toHaveBeenCalledTimes(1)); |
| 170 | + return client.value.save.mock.calls[0]; |
| 171 | +} |
| 172 | + |
| 173 | +beforeEach(() => { |
| 174 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 175 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 176 | +}); |
| 177 | + |
| 178 | +afterEach(() => { |
| 179 | + cleanup(); |
| 180 | + vi.restoreAllMocks(); |
| 181 | + vi.clearAllMocks(); |
| 182 | +}); |
| 183 | + |
| 184 | +describe('the report quick edit edits the authored report (objectstack#35008, ReportView)', () => { |
| 185 | + it('a zh-CN quick edit then save stores the source label and block labels, and the edited field changes', async () => { |
| 186 | + client.value = makeClient({ draft: null }); |
| 187 | + mountReport(); |
| 188 | + const [type, name, body, opts] = await openEditAndSave(); |
| 189 | + |
| 190 | + expect([type, name, opts]).toEqual(['report', 'task_overview', { mode: 'draft' }]); |
| 191 | + // Read at the package the served report came from. |
| 192 | + expect(client.value.layered).toHaveBeenCalledWith('report', 'task_overview', { packageId: PKG }); |
| 193 | + expect(body.label).toBe(SOURCE.label); |
| 194 | + expect(body.blocks.map((b: any) => b.label)).toEqual(['Open Tasks', 'Completed Tasks']); |
| 195 | + // Control: the edit itself is what the save carries. |
| 196 | + expect(body.description).toBe('Edited description'); |
| 197 | + // The read's decoration is not the report's. |
| 198 | + expect(body).not.toHaveProperty('_packageId'); |
| 199 | + expect(toast.error).not.toHaveBeenCalled(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('a pending draft is edited as stored: the draft bar does not resume the served (translated) draft over it', async () => { |
| 203 | + const pending = { ...SOURCE, description: 'Draft description' }; |
| 204 | + client.value = makeClient({ draft: pending }); |
| 205 | + mountReport(); |
| 206 | + const [, , body] = await openEditAndSave(); |
| 207 | + |
| 208 | + expect(body.label).toBe(SOURCE.label); |
| 209 | + expect(body.blocks.map((b: any) => b.label)).toEqual(['Open Tasks', 'Completed Tasks']); |
| 210 | + expect(body.description).toBe('Edited description'); |
| 211 | + // The draft bar still reads the draft, for its indicator and Publish. |
| 212 | + expect(client.value.get).toHaveBeenCalledWith('report', 'task_overview', { state: 'draft' }); |
| 213 | + }); |
| 214 | + |
| 215 | + it('with nothing authored to read the editor does not open, and nothing is saved from the served copy', async () => { |
| 216 | + client.value = { |
| 217 | + ...makeClient({ draft: null }), |
| 218 | + layered: vi.fn(async () => { |
| 219 | + throw Object.assign(new Error('Layered read refused'), { status: 403 }); |
| 220 | + }), |
| 221 | + }; |
| 222 | + mountReport(); |
| 223 | + fireEvent.click(await screen.findByTestId('report-edit-button')); |
| 224 | + |
| 225 | + await waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1)); |
| 226 | + expect(String(vi.mocked(toast.error).mock.calls[0][0])).toContain('Layered read refused'); |
| 227 | + expect(screen.queryByTestId('report-config-panel')).toBeNull(); |
| 228 | + expect(client.value.save).not.toHaveBeenCalled(); |
| 229 | + }); |
| 230 | +}); |
0 commit comments