@@ -36,6 +36,11 @@ vi.mock('@object-ui/auth', async (importOriginal) => ({
3636import { PackageDetailSheet , PackagesPage , type InstalledPackageRow } from './PackagesPage' ;
3737import { t } from './i18n' ;
3838import { initRuntimeConfig , resetRuntimeConfigForTesting } from '../../runtime-config' ;
39+ import { loadStudioCatalogue } from '@object-ui/i18n/namespaces/studio' ;
40+
41+ // The zh catalogue of the designer strings is fetched on first use; loaded here
42+ // so a zh render answers in zh from its first paint.
43+ const ZH = await loadStudioCatalogue ( 'zh' ) ;
3944
4045const label = ( key : string ) => t ( `engine.packages.detail.${ key } ` , 'en' ) ;
4146
@@ -72,15 +77,20 @@ const reply = (status: number, body: unknown) =>
7277
7378let calls : { path : string ; method : string } [ ] ;
7479
75- /** Serve the drafts read, the lifecycle doors and (for the page) the list door. */
76- function stubWire ( packages : unknown [ ] = [ ] ) {
80+ /**
81+ * Serve the drafts read, the lifecycle doors and (for the page) the list door.
82+ * `answer` serves a door's own reply first (a refusal), when it returns one.
83+ */
84+ function stubWire ( packages : unknown [ ] = [ ] , answer ?: ( path : string , method : string ) => Response | undefined ) {
7785 calls = [ ] ;
7886 vi . stubGlobal (
7987 'fetch' ,
8088 vi . fn ( async ( input : unknown , init ?: { method ?: string } ) => {
8189 const path = new URL ( String ( input ) , 'http://console.test' ) . pathname ;
8290 const method = ( init ?. method ?? 'GET' ) . toUpperCase ( ) ;
8391 calls . push ( { path, method } ) ;
92+ const own = answer ?.( path , method ) ;
93+ if ( own ) return own ;
8494 if ( path . startsWith ( '/api/v1/meta/_drafts' ) ) return reply ( 200 , { success : true , data : { drafts : [ ] } } ) ;
8595 if ( path === '/api/v1/packages' && method === 'GET' ) return reply ( 200 , { success : true , data : { packages } } ) ;
8696 if ( path === '/api/v1/cloud-connection/installed' ) return reply ( 404 , { } ) ;
@@ -103,11 +113,16 @@ const settle = () =>
103113 await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
104114 } ) ;
105115
106- async function renderSheet ( pkg : InstalledPackageRow ) {
107- render (
116+ async function renderSheet ( pkg : InstalledPackageRow , language ?: 'zh' ) {
117+ const sheet = (
108118 < MemoryRouter >
109119 < PackageDetailSheet pkg = { pkg } open onOpenChange = { ( ) => { } } onChanged = { ( ) => { } } />
110- </ MemoryRouter > ,
120+ </ MemoryRouter >
121+ ) ;
122+ render (
123+ language
124+ ? < I18nProvider config = { { defaultLanguage : language , detectBrowserLanguage : false } } > { sheet } </ I18nProvider >
125+ : sheet ,
111126 ) ;
112127 await screen . findByTestId ( 'pkg-scope' ) ;
113128 await settle ( ) ;
@@ -220,3 +235,75 @@ describe('Installed Apps view — a marketplace suite\'s module row (objectstack
220235 expect ( screen . queryByRole ( 'button' , { name : label ( 'disable' ) } ) ) . toBeNull ( ) ;
221236 } ) ;
222237} ) ;
238+
239+ describe ( 'PackageDetailSheet — a disable the server refuses for a dependent validation rule (objectstack#31003)' , ( ) => {
240+ const CODE = 'PACKAGE_DISABLE_RESTRICTED_BY_VALIDATION_RULE' ;
241+ // The disable door's own sentence, as it spells it: the rule as
242+ // package · object · rule, then the remedy.
243+ const LISTING = 'com.acme.suite.custom · account · credit_cap (reads credit_limit)' ;
244+ const REMEDY = 'Switch off or change that rule first, or keep the package enabled.' ;
245+ const SENTENCE =
246+ `Package '${ MODULE_ID } ' cannot be disabled: an active validation rule reads a field it adds through ` +
247+ `objectExtensions, which the disable would withhold — ${ LISTING } . ${ REMEDY } ` ;
248+ const refuseDisable = ( status : number , code : string , message : string ) => ( path : string , method : string ) =>
249+ method === 'PATCH' && path . endsWith ( '/disable' )
250+ ? reply ( status , { success : false , error : { code, message } } )
251+ : undefined ;
252+
253+ async function confirmDisable ( ) {
254+ fireEvent . click ( screen . getByRole ( 'button' , { name : label ( 'disable' ) } ) ) ;
255+ const dialog = await screen . findByTestId ( 'pkg-detail-disable-dialog' ) ;
256+ fireEvent . click ( within ( dialog ) . getByTestId ( 'pkg-detail-disable-confirm' ) ) ;
257+ await vi . waitFor ( ( ) => expect ( lifecycleCalls ( ) ) . toEqual ( [ 'disable' ] ) ) ;
258+ await settle ( ) ;
259+ }
260+
261+ it ( 'the confirm dialog says a dependent validation rule can still refuse the disable' , async ( ) => {
262+ stubWire ( ) ;
263+ await renderSheet ( moduleRow ( ) ) ;
264+ fireEvent . click ( screen . getByRole ( 'button' , { name : label ( 'disable' ) } ) ) ;
265+ const dialog = await screen . findByTestId ( 'pkg-detail-disable-dialog' ) ;
266+ expect ( within ( dialog ) . getByTestId ( 'pkg-detail-disable-may-refuse' ) ) . toHaveTextContent ( label ( 'disableMayBeRefused' ) ) ;
267+ expect ( within ( dialog ) . getByText ( label ( 'disableDescription' ) ) ) . toBeInTheDocument ( ) ;
268+ } ) ;
269+
270+ it . each ( [
271+ [ 'en' , undefined , label ( 'disableRestricted' ) ] ,
272+ [ 'zh' , 'zh' as const , ZH [ 'engine.packages.detail.disableRestricted' ] ] ,
273+ ] ) ( '(%s) a 409 draws the heading in the reader\'s language and the server\'s whole sentence' , async ( _name , language , heading ) => {
274+ expect ( heading , 'the catalogue carries the heading' ) . toBeTruthy ( ) ;
275+ stubWire ( [ ] , refuseDisable ( 409 , CODE , SENTENCE ) ) ;
276+ await renderSheet ( moduleRow ( ) , language ) ;
277+ if ( language ) expect ( heading ) . not . toBe ( label ( 'disableRestricted' ) ) ;
278+ await confirmDisable ( ) ;
279+
280+ expect ( await screen . findByTestId ( 'pkg-detail-refusal-heading' ) ) . toHaveTextContent ( heading ! ) ;
281+ const text = screen . getByTestId ( 'pkg-detail-refusal-text' ) ;
282+ // Whole, never cut to its first sentence: the rule listing and the remedy
283+ // are both on screen, with no "More" in front of either.
284+ expect ( text ) . toHaveTextContent ( LISTING ) ;
285+ expect ( text ) . toHaveTextContent ( REMEDY ) ;
286+ expect ( text . textContent ) . toContain ( SENTENCE ) ;
287+ expect ( screen . queryByTestId ( 'refusal-more' ) ) . toBeNull ( ) ;
288+ // Still on offer: the package was not switched off.
289+ expect ( screen . getByRole ( 'button' , { name : language ? ZH [ 'engine.packages.detail.disable' ] : label ( 'disable' ) } ) ) . toBeInTheDocument ( ) ;
290+ } ) ;
291+
292+ it ( 'CONTROL: another refusal code keeps the first-sentence drawing, with no heading' , async ( ) => {
293+ stubWire ( [ ] , refuseDisable ( 403 , 'WRITABLE_PACKAGE_REQUIRED' , 'Package is read-only. Disable it from its owner.' ) ) ;
294+ await renderSheet ( moduleRow ( ) ) ;
295+ await confirmDisable ( ) ;
296+ expect ( await screen . findByTestId ( 'refusal-lead' ) ) . toHaveTextContent ( 'Package is read-only.' ) ;
297+ expect ( screen . getByTestId ( 'refusal-more' ) ) . toBeInTheDocument ( ) ;
298+ expect ( screen . queryByTestId ( 'pkg-detail-refusal-heading' ) ) . toBeNull ( ) ;
299+ } ) ;
300+
301+ it ( 'CONTROL: a disable with no dependent rule lands in one confirm, with no refusal drawn' , async ( ) => {
302+ stubWire ( ) ;
303+ await renderSheet ( moduleRow ( ) ) ;
304+ await confirmDisable ( ) ;
305+ expect ( await screen . findByText ( label ( 'disabled' ) ) ) . toBeInTheDocument ( ) ;
306+ expect ( screen . queryByTestId ( 'pkg-detail-refusal-heading' ) ) . toBeNull ( ) ;
307+ expect ( lifecycleCalls ( ) ) . toEqual ( [ 'disable' ] ) ;
308+ } ) ;
309+ } ) ;
0 commit comments