4545 * A formula over the field does not refuse the disable: it answers `null`.
4646 */
4747
48- import { afterAll , beforeAll , describe , expect , it } from 'vitest' ;
48+ import { afterAll , beforeAll , describe , expect , it , vi } from 'vitest' ;
4949import { mkdtempSync , rmSync , writeFileSync } from 'node:fs' ;
5050import { tmpdir } from 'node:os' ;
5151import { join } from 'node:path' ;
@@ -58,8 +58,14 @@ import { resolveThrownHttpError } from '@objectstack/types';
5858import { AppPlugin } from '../app-plugin.js' ;
5959import { HttpDispatcher } from '../http-dispatcher.js' ;
6060import { loadDisabledPackageIds , setPackageDisabled } from '../package-state-store.js' ;
61+ import { PACKAGE_STATE_UNINSTALL_CLEANUP } from '../package-state-host-store.js' ;
6162import * as runtime from '../index.js' ;
62- import { disableDependentRulesMessage , readDisableDependentRules } from './packages.js' ;
63+ import {
64+ disableDependentRulesMessage ,
65+ PACKAGE_DEPENDENT_RULES_UNINSTALL_GUARD ,
66+ packageUninstallDependentRulesRefusal ,
67+ readDisableDependentRules ,
68+ } from './packages.js' ;
6369
6470const ENVIRONMENT_ID = 'env_suite_extension_fields' ;
6571const ADMIN = {
@@ -1232,3 +1238,151 @@ describe('[#31067] the verdict the disable door asks is the package entry\'s exp
12321238 expect ( registry . getPackage ( EXTENDER ) ?. enabled ) . toBe ( false ) ;
12331239 } ) ;
12341240} ) ;
1241+
1242+ /**
1243+ * [#31232] The uninstall VERB (`protocol.deletePackage`, in
1244+ * `@objectstack/metadata-protocol`) withdraws the package's `objectExtensions`
1245+ * fields from the served objects for every in-process caller (a host's own
1246+ * uninstall tool, install-local's pair-install undo, the unmanaged-import
1247+ * rollback), not only behind the package door. The runtime registers the
1248+ * package doors' dependent-rule verdict on the verb as a pre-uninstall guard
1249+ * (`ensureDependentRulesUninstallGuard`, from `AppPlugin.start`), so the verb
1250+ * itself refuses, before it reads or moves anything, with `409`
1251+ * `PACKAGE_DISABLE_RESTRICTED_BY_VALIDATION_RULE` and the doors' sentence
1252+ * under an uninstall lead. Measured before this change on this rig: the
1253+ * in-process uninstall answered, the field left the served object, and every
1254+ * write to the object then answered `400 VALIDATION_FAILED`, the rule failing
1255+ * closed on the withdrawn field.
1256+ *
1257+ * Two writable packages extend the app's object with one field each; a third
1258+ * package's active rule reads the first one's field. The verb is the kernel's
1259+ * real protocol, called in process.
1260+ */
1261+ describe ( '[#31232] the in-process uninstall verb refuses while an enabled package\'s validation rule reads a field the package adds' , ( ) => {
1262+ const TIERS = 'com.qa.inproc.tiers' ;
1263+ const NOTES = 'com.qa.inproc.notes' ;
1264+ const READER = 'com.qa.inproc.reader' ;
1265+ const TIER = 'qy_tier' ;
1266+ const NOTE = 'qy_note' ;
1267+ const RULE = 'qy_tier_known' ;
1268+ const CODE = 'PACKAGE_DISABLE_RESTRICTED_BY_VALIDATION_RULE' ;
1269+ const manifestOf = ( id : string ) => ( { id, name : id , version : '1.0.0' , type : 'module' } ) ;
1270+
1271+ let root : string ;
1272+ let booted : Booted | undefined ;
1273+ let accountId : string ;
1274+ let protocol : any ;
1275+
1276+ beforeAll ( async ( ) => {
1277+ root = mkdtempSync ( join ( tmpdir ( ) , 'os-suite-ext-inproc-' ) ) ;
1278+ writeFileSync ( join ( root , '.keep' ) , '' ) ;
1279+ process . env . OS_HOME = mkdtempSync ( join ( home , 'inproc-uninstall-' ) ) ;
1280+ booted = await bootKernel ( root ) ;
1281+ const { kernel, engine } = booted ;
1282+ for ( const id of [ TIERS , NOTES , READER ] ) engine . registry . installPackage ( manifestOf ( id ) as any , 'authored' ) ;
1283+ engine . registry . registerObject ( { name : ACCOUNT , fields : { [ TIER ] : { name : TIER , type : 'text' , label : 'Tier' } } } as any , TIERS , undefined , 'extend' ) ;
1284+ engine . registry . registerObject ( { name : ACCOUNT , fields : { [ NOTE ] : { name : NOTE , type : 'text' , label : 'Note' } } } as any , NOTES , undefined , 'extend' ) ;
1285+ engine . registry . registerObject ( {
1286+ name : ACCOUNT ,
1287+ validations : [ { name : RULE , type : 'script' , severity : 'error' , message : 'Unknown tier' , condition : `record.${ TIER } == 'unknown'` } ] ,
1288+ } as any , READER , undefined , 'extend' ) ;
1289+ await engine . syncSchemas ( ) ;
1290+ const created = await dispatchAs ( kernel , 'POST' , `/data/${ ACCOUNT } ` , { name : 'Acme' , [ TIER ] : 'gold' , [ NOTE ] : 'n' } ) ;
1291+ expect ( created . status , `precondition: the account is written (${ String ( created . message ) } )` ) . toBe ( 201 ) ;
1292+ accountId = idOf ( created ) ;
1293+ protocol = kernel . getService < any > ( 'protocol' ) ;
1294+ } , 120_000 ) ;
1295+ afterAll ( async ( ) => {
1296+ vi . restoreAllMocks ( ) ;
1297+ await shutdown ( booted ) ;
1298+ rmSync ( root , { recursive : true , force : true } ) ;
1299+ } , 60_000 ) ;
1300+
1301+ /** The verb's refusal sentence: the exported builder over the exported verdict, under the uninstall lead. */
1302+ const expectedMessage = ( ) => runtime . formatPackageDisableDependents (
1303+ `Package '${ TIERS } ' cannot be uninstalled` ,
1304+ runtime . readPackageDisableDependents ( booted ! . engine . registry , TIERS ) ,
1305+ ) ;
1306+
1307+ it ( 'the verb answers 409 with the doors\' code and sentence under an uninstall lead, having read and moved nothing' , async ( ) => {
1308+ const { kernel, engine } = booted ! ;
1309+ const disabledBefore = [ ...loadDisabledPackageIds ( ENVIRONMENT_ID ) ] . sort ( ) ;
1310+ const reads = vi . spyOn ( protocol , 'findEnvironmentRows' ) ;
1311+ const deletes = vi . spyOn ( protocol , 'deleteMetaItem' ) ;
1312+ const cleanups = vi . spyOn ( protocol , 'runUninstallCleanups' ) ;
1313+ const withdrawals = vi . spyOn ( engine . registry , 'uninstallPackage' ) ;
1314+
1315+ const refused = await protocol . deletePackage ( { packageId : TIERS , actor : ADMIN . userId } ) . then (
1316+ ( ) => { throw new Error ( 'the in-process uninstall was not refused' ) ; } ,
1317+ ( e : any ) => e ,
1318+ ) ;
1319+
1320+ expect ( { status : refused . status , code : refused . code } ) . toEqual ( { status : 409 , code : CODE } ) ;
1321+ expect ( refused . message ) . toBe ( expectedMessage ( ) ) ;
1322+ expect ( refused . message ) . toBe ( packageUninstallDependentRulesRefusal ( engine . registry , TIERS ) ?. message ) ;
1323+ expect ( String ( refused . message ) ) . toMatch ( new RegExp ( `^Package '${ TIERS . replace ( / \. / g, '\\.' ) } ' cannot be uninstalled: an active validation rule reads a field it adds through objectExtensions` ) ) ;
1324+ expect ( String ( refused . message ) ) . toContain ( `${ READER } · ${ ACCOUNT } · ${ RULE } (reads ${ TIER } )` ) ;
1325+
1326+ // Nothing read, nothing moved: no stored row read or deleted, no
1327+ // registry withdrawal, no cleanup; the package and its field stay
1328+ // served, the state file is as it was, and writes still land.
1329+ expect ( {
1330+ reads : reads . mock . calls . length ,
1331+ deletes : deletes . mock . calls . length ,
1332+ cleanups : cleanups . mock . calls . length ,
1333+ withdrawals : withdrawals . mock . calls . length ,
1334+ } ) . toEqual ( { reads : 0 , deletes : 0 , cleanups : 0 , withdrawals : 0 } ) ;
1335+ expect ( engine . registry . getPackage ( TIERS ) ) . toBeDefined ( ) ;
1336+ expect ( await servedFieldNames ( kernel ) ) . toEqual ( expect . arrayContaining ( [ TIER , NOTE ] ) ) ;
1337+ expect ( [ ...loadDisabledPackageIds ( ENVIRONMENT_ID ) ] . sort ( ) ) . toEqual ( disabledBefore ) ;
1338+ const written = await dispatchAs ( kernel , 'PATCH' , `/data/${ ACCOUNT } /${ accountId } ` , { [ TIER ] : 'silver' } ) ;
1339+ expect ( written . status , String ( written . message ) ) . toBe ( 200 ) ;
1340+ expect ( await readAccount ( kernel , accountId ) ) . toMatchObject ( { [ TIER ] : 'silver' } ) ;
1341+ vi . restoreAllMocks ( ) ;
1342+ } ) ;
1343+
1344+ it ( 'the package door answers the same refusal, word for word, and the package stays' , async ( ) => {
1345+ const { kernel, engine } = booted ! ;
1346+ const refused = await dispatchAs ( kernel , 'DELETE' , `/packages/${ TIERS } ` ) ;
1347+ expect ( { status : refused . status , code : refused . code } ) . toEqual ( { status : 409 , code : CODE } ) ;
1348+ expect ( String ( refused . message ) ) . toBe ( expectedMessage ( ) ) ;
1349+ expect ( refused . details ) . toBeUndefined ( ) ;
1350+ expect ( engine . registry . getPackage ( TIERS ) ) . toBeDefined ( ) ;
1351+ expect ( await servedFieldNames ( kernel ) ) . toContain ( TIER ) ;
1352+ } ) ;
1353+
1354+ it ( 'control: a package no rule depends on uninstalls in process as before, its disable record cleared by the verb' , async ( ) => {
1355+ const { kernel, engine } = booted ! ;
1356+ // Disabled first, so the verb has a disable record to clear. The
1357+ // reader's rule stays active on the same object: it reads another field.
1358+ expect ( ( await dispatchAs ( kernel , 'PATCH' , `/packages/${ NOTES } /disable` ) ) . status ) . toBe ( 200 ) ;
1359+ expect ( loadDisabledPackageIds ( ENVIRONMENT_ID ) . has ( NOTES ) , 'precondition: the disable is recorded' ) . toBe ( true ) ;
1360+
1361+ const uninstalled = await protocol . deletePackage ( { packageId : NOTES , actor : ADMIN . userId } ) ;
1362+
1363+ expect ( uninstalled . cleanups . map ( ( c : { name : string } ) => c . name ) ) . toContain ( PACKAGE_STATE_UNINSTALL_CLEANUP ) ;
1364+ expect ( engine . registry . getPackage ( NOTES ) ) . toBeUndefined ( ) ;
1365+ expect ( loadDisabledPackageIds ( ENVIRONMENT_ID ) . has ( NOTES ) ) . toBe ( false ) ;
1366+ expect ( await servedFieldNames ( kernel ) ) . not . toContain ( NOTE ) ;
1367+ // The guard is still registered: it answered nothing for this package.
1368+ expect ( [ ...( protocol as any ) . uninstallGuards . keys ( ) ] ) . toContain ( PACKAGE_DEPENDENT_RULES_UNINSTALL_GUARD ) ;
1369+ } ) ;
1370+
1371+ it ( 'control: proceeds in process once the reading rule is switched off through the tenant overlay, and writes still land' , async ( ) => {
1372+ const { kernel, engine } = booted ! ;
1373+ engine . registry . registerObject ( {
1374+ name : ACCOUNT , label : 'Account' , fields : { name : { name : 'name' , type : 'text' , label : 'Name' } } ,
1375+ validations : [ { name : RULE , type : 'script' , message : 'Switched off by the tenant' , condition : 'true' , active : false } ] ,
1376+ _provenance : 'org' ,
1377+ } as any , APP , undefined , 'overlay' ) ;
1378+ const served = ( ( engine . registry . getObject ( ACCOUNT ) as any ) ?. validations ?? [ ] ) as Array < { name ?: string ; active ?: unknown } > ;
1379+ expect ( served . find ( ( r ) => r . name === RULE ) ?. active , 'precondition: the reader\'s rule is served, switched off' ) . toBe ( false ) ;
1380+
1381+ await protocol . deletePackage ( { packageId : TIERS , actor : ADMIN . userId } ) ;
1382+
1383+ expect ( engine . registry . getPackage ( TIERS ) ) . toBeUndefined ( ) ;
1384+ expect ( await servedFieldNames ( kernel ) ) . not . toContain ( TIER ) ;
1385+ const written = await dispatchAs ( kernel , 'PATCH' , `/data/${ ACCOUNT } /${ accountId } ` , { name : 'Acme Ltd' } ) ;
1386+ expect ( written . status , String ( written . message ) ) . toBe ( 200 ) ;
1387+ } ) ;
1388+ } ) ;
0 commit comments