@@ -107,7 +107,11 @@ const thrownDatabaseError: StoreDelete = async () => {
107107} ;
108108
109109/** One process over `world`. A second call over the same world is a restart. */
110- function boot ( world : World , storeDelete : StoreDelete = landed , opts : { uninstallRefusal ?: Error } = { } ) {
110+ function boot (
111+ world : World ,
112+ storeDelete : StoreDelete = landed ,
113+ opts : { uninstallRefusal ?: Error ; audit ?: unknown ; environmentId ?: string } = { } ,
114+ ) {
111115 const rows = new Map < string , { manifest : Record < string , unknown > ; status : string ; enabled : boolean } > ( ) ;
112116 const registry = {
113117 installPackage ( manifest : Record < string , unknown > , source : string ) {
@@ -150,6 +154,8 @@ function boot(world: World, storeDelete: StoreDelete = landed, opts: { uninstall
150154 } ,
151155 } ;
152156 const services = new Map < string , unknown > ( [
157+ // [#31186] The `audit` slot, when the case composes one.
158+ ...( opts . audit !== undefined ? [ [ 'audit' , opts . audit ] as [ string , unknown ] ] : [ ] ) ,
153159 [ 'package' , {
154160 publish : async ( ) => ( { success : true } ) ,
155161 delete : async ( id : string ) => {
@@ -158,7 +164,7 @@ function boot(world: World, storeDelete: StoreDelete = landed, opts: { uninstall
158164 } ,
159165 } ] ,
160166 ] ) ;
161- const impl = new ObjectStackProtocolImplementation ( engine as never , ( ) => services as never ) as any ;
167+ const impl = new ObjectStackProtocolImplementation ( engine as never , ( ) => services as never , opts . environmentId ) as any ;
162168 vi . spyOn ( impl , 'deleteMetaItem' ) . mockImplementation ( async ( req : any ) => {
163169 world . steps . push ( `sys_metadata.delete ${ req . type } /${ req . name } /${ req . state } ` ) ;
164170 world . sysMetadata = world . sysMetadata . filter (
@@ -323,3 +329,137 @@ describe('#21276 the restart — a fresh process over the same store holds the p
323329 expect ( snapshot ( world ) ) . toEqual ( { sysPackages : [ ] , sysMetadata : [ ] , grants : [ ] } ) ;
324330 } ) ;
325331} ) ;
332+
333+ /**
334+ * [#31186] The uninstall verb hands the `audit` slot ONE `package_uninstall`
335+ * event per package it uninstalled, after the package's stored row is gone and
336+ * its cleanups ran — and none for an uninstall it refused. plugin-audit owns
337+ * the row that event becomes (`package-lifecycle-audit.test.ts` there); this
338+ * pins the verb's half: when it calls, how often, and with what.
339+ */
340+ describe ( '#31186 deletePackage — the package\'s own ledger event on the `audit` slot' , ( ) => {
341+ /** An `audit` slot that records each event and the step it ran at. */
342+ function recordingAudit ( world : World ) {
343+ const events : unknown [ ] = [ ] ;
344+ return {
345+ events,
346+ slot : {
347+ async recordPackageLifecycleEvent ( event : unknown ) {
348+ world . steps . push ( 'audit.package_uninstall' ) ;
349+ events . push ( event ) ;
350+ } ,
351+ } ,
352+ } ;
353+ }
354+
355+ it ( 'an uninstall hands the slot one event naming the package, its version, the environment and the actor — last' , async ( ) => {
356+ const world = makeWorld ( ) ;
357+ const audit = recordingAudit ( world ) ;
358+ const { impl } = boot ( world , landed , { audit : audit . slot , environmentId : 'env_31186' } ) ;
359+
360+ const res = await impl . deletePackage ( { packageId : PKG , actor : 'u_admin' , keepData : true } ) ;
361+
362+ expect ( res ) . toMatchObject ( { success : true , deletedCount : 2 , failedCount : 0 } ) ;
363+ expect ( audit . events ) . toEqual ( [ {
364+ action : 'package_uninstall' ,
365+ packageId : PKG ,
366+ version : '1.0.0' ,
367+ environmentId : 'env_31186' ,
368+ actor : 'u_admin' ,
369+ keepData : true ,
370+ itemsRemoved : 2 ,
371+ itemsFailed : 0 ,
372+ } ] ) ;
373+ // After the store delete, the item deletes, the withdrawal and the cleanups.
374+ expect ( world . steps [ world . steps . length - 1 ] ) . toBe ( 'audit.package_uninstall' ) ;
375+ expect ( world . steps . indexOf ( 'sys_packages.delete' ) ) . toBeLessThan ( world . steps . indexOf ( 'audit.package_uninstall' ) ) ;
376+ } ) ;
377+
378+ it ( 'a caller with no actor and a host bound to no environment: the event says neither, and still names the package' , async ( ) => {
379+ const world = makeWorld ( ) ;
380+ const audit = recordingAudit ( world ) ;
381+ const { impl } = boot ( world , landed , { audit : audit . slot } ) ;
382+
383+ await impl . deletePackage ( { packageId : PKG } ) ;
384+
385+ expect ( audit . events ) . toEqual ( [ {
386+ action : 'package_uninstall' , packageId : PKG , version : '1.0.0' , itemsRemoved : 2 , itemsFailed : 0 ,
387+ } ] ) ;
388+ } ) ;
389+
390+ it ( 'a partial uninstall still records the package, with the item it left behind' , async ( ) => {
391+ const world = makeWorld ( ) ;
392+ const audit = recordingAudit ( world ) ;
393+ const { impl } = boot ( world , landed , { audit : audit . slot } ) ;
394+ vi . spyOn ( impl , 'deleteMetaItem' ) . mockImplementation ( async ( req : any ) => {
395+ if ( req . type === 'view' ) throw Object . assign ( new Error ( 'locked' ) , { code : 'METADATA_LOCKED' , status : 423 } ) ;
396+ world . sysMetadata = world . sysMetadata . filter ( ( r ) => ! ( r . type === req . type && r . name === req . name ) ) ;
397+ return { success : true } ;
398+ } ) ;
399+
400+ const res = await impl . deletePackage ( { packageId : PKG , actor : 'u_admin' } ) ;
401+
402+ expect ( res ) . toMatchObject ( { success : false , deletedCount : 1 , failedCount : 1 } ) ;
403+ expect ( audit . events ) . toEqual ( [ expect . objectContaining ( { packageId : PKG , itemsRemoved : 1 , itemsFailed : 1 } ) ] ) ;
404+ } ) ;
405+
406+ it . each ( REFUSALS ) ( 'CONTROL: a %s store refusal hands the slot nothing' , async ( _label , _code , refusal ) => {
407+ const world = makeWorld ( ) ;
408+ const audit = recordingAudit ( world ) ;
409+ const { impl } = boot ( world , refusal , { audit : audit . slot } ) ;
410+
411+ await rejectionOf ( impl . deletePackage ( { packageId : PKG , actor : 'u_admin' } ) ) ;
412+
413+ expect ( audit . events ) . toEqual ( [ ] ) ;
414+ } ) ;
415+
416+ it ( 'CONTROL: the registry\'s uninstall refusal and a retired request key hand the slot nothing' , async ( ) => {
417+ const world = makeWorld ( ) ;
418+ const audit = recordingAudit ( world ) ;
419+ const refused = boot ( world , landed , { audit : audit . slot , uninstallRefusal : new Error ( 'extended by com.example.addon' ) } ) ;
420+ await rejectionOf ( refused . impl . deletePackage ( { packageId : PKG } ) ) ;
421+ const retired = boot ( world , landed , { audit : audit . slot } ) ;
422+ await rejectionOf ( retired . impl . deletePackage ( { packageId : PKG , organizationId : 'org_a' } as any ) ) ;
423+
424+ expect ( audit . events ) . toEqual ( [ ] ) ;
425+ } ) ;
426+
427+ it ( 'CONTROL: an id that is not installed and binds no metadata is not a package — no event' , async ( ) => {
428+ const world = makeWorld ( ) ;
429+ const audit = recordingAudit ( world ) ;
430+ const { impl } = boot ( world , landed , { audit : audit . slot } ) ;
431+
432+ const res = await impl . deletePackage ( { packageId : 'com.example.absent' } ) ;
433+
434+ expect ( res ) . toMatchObject ( { deletedCount : 0 , failedCount : 0 } ) ;
435+ expect ( audit . events ) . toEqual ( [ ] ) ;
436+ } ) ;
437+
438+ it ( 'CONTROL: with no `audit` slot the uninstall completes exactly as before' , async ( ) => {
439+ const world = makeWorld ( ) ;
440+ const { impl, registry } = boot ( world ) ;
441+
442+ const res = await impl . deletePackage ( { packageId : PKG , actor : 'u_admin' } ) ;
443+
444+ expect ( res ) . toMatchObject ( { success : true , deletedCount : 2 , failedCount : 0 } ) ;
445+ expect ( registry . getPackage ( PKG ) ) . toBeUndefined ( ) ;
446+ } ) ;
447+
448+ it ( 'a slot that throws does not change the answer: the uninstall completes and the lost row is logged at error' , async ( ) => {
449+ const world = makeWorld ( ) ;
450+ const errorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
451+ try {
452+ const { impl } = boot ( world , landed , {
453+ audit : { recordPackageLifecycleEvent : async ( ) => { throw new Error ( 'ledger offline' ) ; } } ,
454+ } ) ;
455+
456+ const res = await impl . deletePackage ( { packageId : PKG } ) ;
457+
458+ expect ( res ) . toMatchObject ( { success : true , deletedCount : 2 } ) ;
459+ expect ( errorSpy ) . toHaveBeenCalledTimes ( 1 ) ;
460+ expect ( String ( errorSpy . mock . calls [ 0 ] ?. [ 0 ] ) ) . toContain ( PKG ) ;
461+ } finally {
462+ errorSpy . mockRestore ( ) ;
463+ }
464+ } ) ;
465+ } ) ;
0 commit comments