2424 * - Restore deletes the pair's draft and row of that one member, and the
2525 * base's version reads back;
2626 * - a walled caller sees Customize disabled, and nothing is asked or sent.
27+ *
28+ * objectstack#30064 — a read-only base's dataset, in the same editor with the
29+ * type's registered designer (a probe here): the whole dataset written into
30+ * the pair, declared; what uses it listed from the references read before
31+ * anything is published, a failed read said as failed; a designer's blocking
32+ * issue holding the edit unsent; and a kept publish refusal shown at the item.
2733 */
2834
2935import '@testing-library/jest-dom/vitest' ;
@@ -90,6 +96,7 @@ const mockClient = vi.hoisted(() => ({
9096 srv . version += 1 ;
9197 return { success : true , version : `v${ srv . version } ` , state : 'draft' } ;
9298 } ) ,
99+ references : vi . fn ( async ( _type : string , _name : string ) : Promise < unknown [ ] > => [ ] ) ,
93100 reset : vi . fn ( async ( type : string , name : string , opts : Record < string , unknown > = { } ) => {
94101 srv . resets . push ( { type, name, opts : clone ( opts ) } ) ;
95102 const map = opts . state === 'draft' ? srv . drafts : srv . rows ;
@@ -115,6 +122,43 @@ import type { StudioBaseCustomization } from './StudioDesignSurface';
115122import { ensureCustomizationPackage } from './packages-io' ;
116123import { registerMetadataPreview } from '../metadata-admin/preview-registry' ;
117124import { registerDefaultMetadataSchemas } from '../metadata-admin/default-schemas' ;
125+ import {
126+ registerMetadataDefaultInspector ,
127+ type MetadataDefaultInspectorProps ,
128+ } from '../metadata-admin/default-inspector-registry' ;
129+ import type { StudioRefusal } from './metadataError' ;
130+
131+ /**
132+ * The dataset's designer slot: the draft it is handed, its read-only gate, one
133+ * edit (the second measure's aggregate) and one blocking report.
134+ */
135+ function DesignerProbe ( { draft, onPatch, onBlockingIssuesChange, readOnly } : MetadataDefaultInspectorProps ) {
136+ const measures = Array . isArray ( draft . measures ) ? ( draft . measures as Array < Record < string , unknown > > ) : [ ] ;
137+ return (
138+ < div data-testid = "designer-probe" data-readonly = { String ( readOnly ) } >
139+ < p data-testid = "designer-probe-aggregate" > { String ( measures [ 1 ] ?. aggregate ?? '' ) } </ p >
140+ < button
141+ type = "button"
142+ data-testid = "designer-probe-avg"
143+ disabled = { readOnly }
144+ onClick = { ( ) => onPatch ( { measures : measures . map ( ( m , i ) => ( i === 1 ? { ...m , aggregate : 'avg' } : m ) ) } ) }
145+ >
146+ avg
147+ </ button >
148+ < button
149+ type = "button"
150+ data-testid = "designer-probe-blank-dimension"
151+ disabled = { readOnly }
152+ onClick = { ( ) => {
153+ onPatch ( { dimensions : [ { name : 'stage' } ] } ) ;
154+ onBlockingIssuesChange ?.( 1 ) ;
155+ } }
156+ >
157+ blank
158+ </ button >
159+ </ div >
160+ ) ;
161+ }
118162
119163/** The preview slot, printing the live draft it is handed. */
120164function PreviewProbe ( { draft } : { draft : Record < string , unknown > } ) {
@@ -134,6 +178,37 @@ const WELCOME_EN = {
134178 _packageId : BASE ,
135179} ;
136180const WELCOME_ZH = { ...WELCOME_EN , locale : 'zh-CN' , subject : '欢迎使用 HotCRM' , bodyHtml : '<p>你好 {{user.name}}</p>' } ;
181+ const METRICS = {
182+ name : 'opportunity_metrics' ,
183+ label : 'Opportunity Metrics' ,
184+ object : 'crm_opportunity' ,
185+ dimensions : [ { name : 'stage' , label : 'Stage' , field : 'stage' , type : 'string' } ] ,
186+ measures : [
187+ { name : 'opp_count' , label : 'Opportunities' , aggregate : 'count' } ,
188+ { name : 'total_amount' , label : 'Total Amount' , aggregate : 'sum' , field : 'amount' } ,
189+ ] ,
190+ _packageId : BASE ,
191+ } ;
192+ /** What the references read answers for the dataset (`client.references`, mapped from the served `references[]`). */
193+ const USED_BY = [
194+ {
195+ fromType : 'dashboard' ,
196+ fromName : 'pipeline_dashboard' ,
197+ fromLabel : 'Pipeline Dashboard' ,
198+ path : 'widgets[].dataset' ,
199+ kind : 'dashboard dataset' ,
200+ value : 'opportunity_metrics' ,
201+ } ,
202+ {
203+ fromType : 'dashboard' ,
204+ fromName : 'pipeline_dashboard' ,
205+ fromLabel : 'Pipeline Dashboard' ,
206+ path : 'widgets[].compare.dataset' ,
207+ kind : 'dashboard dataset' ,
208+ value : 'opportunity_metrics' ,
209+ } ,
210+ { fromType : 'report' , fromName : 'won_by_stage' , path : 'dataset' , kind : 'report dataset' , value : 'opportunity_metrics' } ,
211+ ] ;
137212const BUNDLE_ZH = {
138213 name : 'hotcrm_zh' ,
139214 label : 'HotCRM zh-CN' ,
@@ -146,6 +221,7 @@ beforeAll(() => {
146221 registerDefaultMetadataSchemas ( ) ;
147222 registerMetadataPreview ( 'email_template' , PreviewProbe as never ) ;
148223 registerMetadataPreview ( 'translation' , PreviewProbe as never ) ;
224+ registerMetadataDefaultInspector ( 'dataset' , DesignerProbe ) ;
149225} ) ;
150226
151227beforeEach ( ( ) => {
@@ -155,6 +231,7 @@ beforeEach(() => {
155231 at ( srv . rows , BASE ) . set ( slot ( 'email_template' , 'welcome' , 'zh-CN' ) , clone ( WELCOME_ZH ) ) ;
156232 at ( srv . rows , BASE ) . set ( slot ( 'translation' , 'hotcrm_zh' ) , clone ( BUNDLE_ZH ) ) ;
157233 at ( srv . rows , WRITABLE ) . set ( slot ( 'email_template' , 'welcome' , 'zh-CN' ) , { ...clone ( WELCOME_ZH ) , _packageId : WRITABLE } ) ;
234+ at ( srv . rows , BASE ) . set ( slot ( 'dataset' , 'opportunity_metrics' ) , clone ( METRICS ) ) ;
158235 srv . pairExists = false ;
159236 srv . ensure . length = 0 ;
160237 srv . saves . length = 0 ;
@@ -196,13 +273,15 @@ function Host({
196273 packageId = BASE ,
197274 walled = false ,
198275 listedPair = null ,
276+ publishRefusal = null ,
199277} : {
200278 type : string ;
201279 name : string ;
202280 member ?: string ;
203281 packageId ?: string ;
204282 walled ?: boolean ;
205283 listedPair ?: string | null ;
284+ publishRefusal ?: StudioRefusal | null ;
206285} ) {
207286 const readOnly = packageId === BASE ;
208287 const [ pairId , setPairId ] = React . useState < string | null > ( listedPair ) ;
@@ -228,6 +307,7 @@ function Host({
228307 readOnly = { readOnly }
229308 customization = { customization }
230309 basePath = { `/studio/${ packageId } /metadata` }
310+ publishRefusal = { publishRefusal }
231311 />
232312 ) ;
233313}
@@ -437,3 +517,104 @@ describe('Customize a read-only base’s translation bundle (objectstack#28569)'
437517 await waitFor ( ( ) => expect ( screen . queryByTestId ( 'studio-bundle-editor-whole-bundle' ) ) . toBeNull ( ) ) ;
438518 } ) ;
439519} ) ;
520+
521+ describe ( 'Customize a read-only base’s dataset (objectstack#30064)' , ( ) => {
522+ beforeEach ( ( ) => {
523+ mockClient . references . mockImplementation ( async ( ) => clone ( USED_BY ) ) ;
524+ } ) ;
525+
526+ it ( 'opens the designer read-only, says the whole dataset is copied, and lists what uses it before anything is published' , async ( ) => {
527+ mount ( { type : 'dataset' , name : 'opportunity_metrics' } ) ;
528+ const probe = await screen . findByTestId ( 'designer-probe' , undefined , { timeout : 8000 } ) ;
529+ expect ( probe ) . toHaveAttribute ( 'data-readonly' , 'true' ) ;
530+ expect ( screen . getByTestId ( 'designer-probe-aggregate' ) ) . toHaveTextContent ( 'sum' ) ;
531+ expect ( screen . getByTestId ( 'studio-bundle-editor-whole-dataset' ) ) . toHaveTextContent (
532+ 'Customizing a dataset copies HotCRM’s whole dataset into your customization' ,
533+ ) ;
534+ const usedBy = await screen . findByTestId ( 'studio-bundle-editor-used-by' ) ;
535+ await waitFor ( ( ) => expect ( usedBy ) . toHaveAttribute ( 'data-state' , 'loaded' ) ) ;
536+ expect ( mockClient . references ) . toHaveBeenCalledWith ( 'dataset' , 'opportunity_metrics' ) ;
537+ // One entry per referring item: the dashboard binding it twice is one dashboard.
538+ const items = within ( usedBy ) . getAllByTestId ( 'studio-bundle-editor-used-by-item' ) ;
539+ expect ( items . map ( ( el ) => [ el . getAttribute ( 'data-type' ) , el . getAttribute ( 'data-name' ) ] ) ) . toEqual ( [
540+ [ 'dashboard' , 'pipeline_dashboard' ] ,
541+ [ 'report' , 'won_by_stage' ] ,
542+ ] ) ;
543+ expect ( items [ 0 ] ) . toHaveTextContent ( 'Pipeline Dashboard' ) ;
544+ expect ( items [ 1 ] ) . toHaveTextContent ( 'won_by_stage' ) ;
545+ expect ( usedBy ) . toHaveTextContent ( 'reaches the 2 items built on this dataset' ) ;
546+ expect ( srv . saves ) . toEqual ( [ ] ) ;
547+ } ) ;
548+
549+ it ( 'Customize and an edit in the designer write the whole dataset into the pair’s draft, declared; the base is untouched' , async ( ) => {
550+ mount ( { type : 'dataset' , name : 'opportunity_metrics' } ) ;
551+ await screen . findByTestId ( 'designer-probe' , undefined , { timeout : 8000 } ) ;
552+ await customize ( ) ;
553+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'designer-probe' ) ) . toHaveAttribute ( 'data-readonly' , 'false' ) ) ;
554+ fireEvent . click ( screen . getByTestId ( 'designer-probe-avg' ) ) ;
555+ await waitFor ( ( ) => expect ( srv . saves ) . toHaveLength ( 1 ) , { timeout : 5000 } ) ;
556+ expect ( srv . ensure ) . toEqual ( [ 201 ] ) ;
557+ const [ save ] = srv . saves ;
558+ expect ( save ) . toMatchObject ( { type : 'dataset' , name : 'opportunity_metrics' } ) ;
559+ expect ( save . opts ) . toMatchObject ( { mode : 'draft' , packageId : PAIR , ifNoneMatch : '*' } ) ;
560+ const { _packageId : _dropped , ...whole } = clone ( METRICS ) ;
561+ expect ( save . body ) . toEqual ( {
562+ ...whole ,
563+ measures : [ whole . measures [ 0 ] , { ...whole . measures [ 1 ] , aggregate : 'avg' } ] ,
564+ overrides : { package : BASE , basedOn : '4.0.0' } ,
565+ } ) ;
566+ expect ( await screen . findByTestId ( 'studio-override-chip' ) ) . toHaveTextContent ( 'Overrides HotCRM' ) ;
567+ await waitFor ( ( ) => expect ( screen . queryByTestId ( 'studio-bundle-editor-whole-dataset' ) ) . toBeNull ( ) ) ;
568+ // What uses it stays in view until the change is published.
569+ expect ( screen . getByTestId ( 'studio-bundle-editor-used-by' ) ) . toBeInTheDocument ( ) ;
570+ expect ( srv . rows . get ( BASE ) ?. get ( slot ( 'dataset' , 'opportunity_metrics' ) ) ) . toEqual ( METRICS ) ;
571+ } ) ;
572+
573+ it ( 'holds an edit the designer reports as blocking: nothing is sent' , async ( ) => {
574+ mount ( { type : 'dataset' , name : 'opportunity_metrics' } ) ;
575+ await screen . findByTestId ( 'designer-probe' , undefined , { timeout : 8000 } ) ;
576+ await customize ( ) ;
577+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'designer-probe' ) ) . toHaveAttribute ( 'data-readonly' , 'false' ) ) ;
578+ fireEvent . click ( screen . getByTestId ( 'designer-probe-blank-dimension' ) ) ;
579+ await sleep ( 2500 ) ;
580+ expect ( srv . ensure ) . toEqual ( [ ] ) ;
581+ expect ( srv . saves ) . toEqual ( [ ] ) ;
582+ } ) ;
583+
584+ it ( 'a references read that failed says so and asks again; it never reads as nothing using the dataset' , async ( ) => {
585+ mockClient . references . mockRejectedValueOnce ( Object . assign ( new Error ( 'Service Unavailable' ) , { status : 503 } ) ) ;
586+ mount ( { type : 'dataset' , name : 'opportunity_metrics' } ) ;
587+ const usedBy = await screen . findByTestId ( 'studio-bundle-editor-used-by' , undefined , { timeout : 8000 } ) ;
588+ await waitFor ( ( ) => expect ( usedBy ) . toHaveAttribute ( 'data-state' , 'error' ) ) ;
589+ expect ( within ( usedBy ) . getByRole ( 'alert' ) ) . toHaveTextContent ( 'could not be read' ) ;
590+ expect ( usedBy ) . not . toHaveTextContent ( 'Nothing uses this dataset' ) ;
591+ fireEvent . click ( within ( usedBy ) . getByTestId ( 'studio-bundle-editor-used-by-retry' ) ) ;
592+ await waitFor ( ( ) => expect ( usedBy ) . toHaveAttribute ( 'data-state' , 'loaded' ) ) ;
593+ expect ( within ( usedBy ) . getAllByTestId ( 'studio-bundle-editor-used-by-item' ) ) . toHaveLength ( 2 ) ;
594+ } ) ;
595+
596+ it ( 'a read that answers no reference says nothing uses the dataset' , async ( ) => {
597+ mockClient . references . mockImplementation ( async ( ) => [ ] ) ;
598+ mount ( { type : 'dataset' , name : 'opportunity_metrics' } ) ;
599+ const usedBy = await screen . findByTestId ( 'studio-bundle-editor-used-by' , undefined , { timeout : 8000 } ) ;
600+ await waitFor ( ( ) => expect ( usedBy ) . toHaveAttribute ( 'data-state' , 'loaded' ) ) ;
601+ expect ( usedBy ) . toHaveTextContent ( 'Nothing uses this dataset yet' ) ;
602+ } ) ;
603+
604+ it ( 'shows the refusal the header’s Publish kept for the dataset at the item, with the server’s text under Details' , async ( ) => {
605+ mount ( {
606+ type : 'dataset' ,
607+ name : 'opportunity_metrics' ,
608+ listedPair : PAIR ,
609+ publishRefusal : {
610+ message : 'Not published. Check Aggregate of the measure “Total Amount”: Details say what to change.' ,
611+ target : null ,
612+ input : 'Aggregate' ,
613+ detail : 'datasets.opportunity_metrics.measures[1].aggregate — aggregate "sum" over field "expected_revenue" is refused' ,
614+ } ,
615+ } ) ;
616+ const strip = await screen . findByTestId ( 'studio-bundle-editor-publish-refusal' , undefined , { timeout : 8000 } ) ;
617+ expect ( within ( strip ) . getByRole ( 'alert' ) ) . toHaveTextContent ( 'Check Aggregate of the measure “Total Amount”' ) ;
618+ expect ( within ( strip ) . getByTestId ( 'studio-refusal-detail' ) ) . toHaveTextContent ( 'expected_revenue' ) ;
619+ } ) ;
620+ } ) ;
0 commit comments