|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27461 — on the Studio data page, a new Rollup field is held |
| 5 | + * until it is set up, not autosaved unconfigured. |
| 6 | + * |
| 7 | + * The card's step 4: "Add field", Type = Rollup, and the draft PUT went out at |
| 8 | + * once with `{ type: 'summary' }` and no `summaryOperations`, which the server |
| 9 | + * saved (a roll-up that reads 0 everywhere). Now the pillar asks the spec's |
| 10 | + * judges before sending (`objectHeldEdit`, pinned predicate by predicate in |
| 11 | + * `metadataError.summaryHeld-27461.test.ts`): the edit stays dirty and unsent, |
| 12 | + * the held line names the field and the input it still needs, with "Show me" |
| 13 | + * once the field is closed, and the save goes as soon as the roll-up is one |
| 14 | + * the spec accepts. |
| 15 | + * |
| 16 | + * The harness is `DataPillar.heldIncomplete-11786.test.tsx`'s: a real |
| 17 | + * `MetadataClient` over a transport double. |
| 18 | + */ |
| 19 | + |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import * as React from 'react'; |
| 22 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 23 | +import { render, screen, fireEvent, cleanup, act, waitFor, within } from '@testing-library/react'; |
| 24 | +import { MemoryRouter } from 'react-router-dom'; |
| 25 | +import { MetadataClient } from '@object-ui/data-objectstack'; |
| 26 | + |
| 27 | +const objectDef = { |
| 28 | + name: 'showcase_task', |
| 29 | + label: 'Task', |
| 30 | + fields: [{ name: 'title', label: 'Title', type: 'text' }], |
| 31 | +}; |
| 32 | + |
| 33 | +/** Every PUT the transport received, parsed. */ |
| 34 | +const puts: Array<Record<string, unknown>> = []; |
| 35 | + |
| 36 | +const fetchImpl = vi.fn(async (_url: string, init?: RequestInit) => { |
| 37 | + if (init?.method === 'PUT') { |
| 38 | + puts.push(JSON.parse(String(init.body)) as Record<string, unknown>); |
| 39 | + return new Response(JSON.stringify({ success: true }), { |
| 40 | + status: 200, |
| 41 | + headers: { 'content-type': 'application/json' }, |
| 42 | + }); |
| 43 | + } |
| 44 | + return new Response('null', { status: 404, headers: { 'content-type': 'application/json' } }); |
| 45 | +}); |
| 46 | + |
| 47 | +const client = new MetadataClient({ baseUrl: 'http://test.local', fetch: fetchImpl as unknown as typeof fetch }); |
| 48 | +Object.assign(client, { |
| 49 | + list: vi.fn(async () => [{ name: 'showcase_task', label: 'Task' }]), |
| 50 | + listDrafts: vi.fn(async () => []), |
| 51 | + layered: vi.fn(async () => ({ effective: objectDef, code: objectDef })), |
| 52 | + getDraft: vi.fn(async () => null), |
| 53 | +}); |
| 54 | + |
| 55 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 56 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 57 | + return { ...mod, useMetadataClient: () => client, useMetadataTypes: () => ({ entries: [] }) }; |
| 58 | +}); |
| 59 | + |
| 60 | +vi.mock('./packages-io', async (importOriginal) => { |
| 61 | + const mod = await importOriginal<typeof import('./packages-io')>(); |
| 62 | + return { ...mod, fetchPackages: vi.fn(async () => []) }; |
| 63 | +}); |
| 64 | + |
| 65 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 66 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 67 | + return { ...mod, useAdapter: () => dataSource }; |
| 68 | +}); |
| 69 | + |
| 70 | +import { DataPillar } from './StudioDesignSurface'; |
| 71 | +import { addFieldFromFormToolbar } from './__tests__/dataPillarAddField'; |
| 72 | +import { createEmptyDataSource } from './__tests__/emptyDataSource'; |
| 73 | +import { registerBuiltinInspectors } from '../metadata-admin/inspectors'; |
| 74 | +import { t, tFormat } from '../metadata-admin/i18n'; |
| 75 | + |
| 76 | +const dataSource = createEmptyDataSource(); |
| 77 | +registerBuiltinInspectors(); |
| 78 | + |
| 79 | +afterEach(() => { |
| 80 | + cleanup(); |
| 81 | + puts.length = 0; |
| 82 | + fetchImpl.mockClear(); |
| 83 | +}); |
| 84 | + |
| 85 | +/** Past the autosave's 1.5 s debounce. */ |
| 86 | +async function outlastDebounce(): Promise<void> { |
| 87 | + await act(async () => { |
| 88 | + await new Promise((r) => setTimeout(r, 2300)); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +/** The inspector control under a visible label. */ |
| 93 | +function controlUnder(label: string): HTMLElement { |
| 94 | + const lab = screen.getByText(label, { selector: 'label' }); |
| 95 | + return lab.parentElement!.querySelector('input, [role="combobox"]') as HTMLElement; |
| 96 | +} |
| 97 | + |
| 98 | +/** The fields of the last PUT body, by name. */ |
| 99 | +function lastSentField(name: string): Record<string, unknown> | undefined { |
| 100 | + const fields = puts[puts.length - 1]?.fields; |
| 101 | + return Array.isArray(fields) |
| 102 | + ? (fields as Array<Record<string, unknown>>).find((f) => f.name === name) |
| 103 | + : (fields as Record<string, Record<string, unknown>> | undefined)?.[name]; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Mount and add the card's field the card's way: Form tab, Add field, Label |
| 108 | + * "Child Count", Type Rollup, Create. Returns its API name and label. |
| 109 | + */ |
| 110 | +async function addRollup(): Promise<{ name: string; label: string }> { |
| 111 | + render( |
| 112 | + <MemoryRouter initialEntries={['/studio/com.example.showcase/data']}> |
| 113 | + <DataPillar packageId="com.example.showcase" /> |
| 114 | + </MemoryRouter>, |
| 115 | + ); |
| 116 | + fireEvent.click(await screen.findByRole('button', { name: 'Form' })); |
| 117 | + await addFieldFromFormToolbar(undefined, { label: 'Child Count', type: /^Rollup$/ }); |
| 118 | + await screen.findByText('Type', { selector: 'label' }, { timeout: 4000 }); |
| 119 | + const name = (controlUnder('API name') as HTMLInputElement).value; |
| 120 | + const label = (controlUnder('Label') as HTMLInputElement).value; |
| 121 | + expect(name, 'the new field has no API name: the harness is dead').not.toBe(''); |
| 122 | + expect(label).toBe('Child Count'); |
| 123 | + return { name, label }; |
| 124 | +} |
| 125 | + |
| 126 | +const heldLine = (clause: string) => tFormat('engine.studio.held.line', 'en', { clause }); |
| 127 | + |
| 128 | +describe('Studio data page — a Rollup is held until it is set up (objectstack#27461)', () => { |
| 129 | + it('sends no save for the bare roll-up; names each missing input in turn; a count lets the save go', async () => { |
| 130 | + const { name, label } = await addRollup(); |
| 131 | + await outlastDebounce(); |
| 132 | + |
| 133 | + expect(puts, 'an unconfigured roll-up is not sent').toEqual([]); |
| 134 | + expect(screen.queryByTestId('studio-refusal')).toBeNull(); |
| 135 | + const message = () => within(screen.getByTestId('studio-held')).getByTestId('studio-held-message'); |
| 136 | + const needs = (inputKey: string) => |
| 137 | + heldLine(tFormat('engine.studio.held.fieldNeedsInput', 'en', { input: t(inputKey, 'en'), field: label })); |
| 138 | + expect(message()).toHaveTextContent(needs('designer.field.summary.object')); |
| 139 | + |
| 140 | + const child = controlUnder(t('designer.field.summary.object', 'en')) as HTMLInputElement; |
| 141 | + fireEvent.change(child, { target: { value: 'showcase_task' } }); |
| 142 | + fireEvent.blur(child); |
| 143 | + await outlastDebounce(); |
| 144 | + expect(puts, 'a child object alone is not a roll-up yet').toEqual([]); |
| 145 | + expect(message()).toHaveTextContent(needs('designer.field.summary.function')); |
| 146 | + |
| 147 | + fireEvent.keyDown(controlUnder(t('designer.field.summary.function', 'en')), { key: 'ArrowDown' }); |
| 148 | + fireEvent.click( |
| 149 | + within(await screen.findByRole('listbox')).getByRole('option', { name: t('designer.field.summary.fn.count', 'en') }), |
| 150 | + ); |
| 151 | + await waitFor(() => expect(puts.length).toBe(1), { timeout: 4000 }); |
| 152 | + expect(lastSentField(name)).toEqual( |
| 153 | + expect.objectContaining({ type: 'summary', summaryOperations: { object: 'showcase_task', function: 'count' } }), |
| 154 | + ); |
| 155 | + await waitFor(() => expect(screen.queryByTestId('studio-held')).toBeNull()); |
| 156 | + expect(screen.queryByTestId('studio-refusal')).toBeNull(); |
| 157 | + }); |
| 158 | + |
| 159 | + it('"Show me" on the line reopens the held roll-up once its inspector is closed', async () => { |
| 160 | + // The first case proves the roll-up is never sent; this one needs only the line. |
| 161 | + const { name } = await addRollup(); |
| 162 | + |
| 163 | + const railHeader = screen.getByText(t('engine.studio.data.fieldProps', 'en')).closest('header') as HTMLElement; |
| 164 | + fireEvent.click(within(railHeader).getByRole('button', { name: t('engine.studio.close', 'en') })); |
| 165 | + expect(screen.queryByText('API name', { selector: 'label' })).toBeNull(); |
| 166 | + |
| 167 | + fireEvent.click(within(screen.getByTestId('studio-held')).getByRole('button', { name: t('engine.studio.refusal.show', 'en') })); |
| 168 | + expect(await screen.findByText('API name', { selector: 'label' })).toBeInTheDocument(); |
| 169 | + expect((controlUnder('API name') as HTMLInputElement).value).toBe(name); |
| 170 | + }); |
| 171 | +}); |
0 commit comments