@@ -76,6 +76,13 @@ const triggerHook = async (
7676 }
7777} ;
7878
79+ /** Retrieve the audit API or fail fast with a descriptive message */
80+ function getAuditAPI ( kernel : any ) : AuditLogPlugin {
81+ const api = getAuditLogAPI ( kernel ) ;
82+ if ( ! api ) throw new Error ( 'AuditLogPlugin not registered on kernel' ) ;
83+ return api ;
84+ }
85+
7986// ── Integration tests ──────────────────────────────────────────────────────────
8087
8188describe ( 'Integration: Auth → Permissions → Data → Audit' , ( ) => {
@@ -114,7 +121,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
114121 userAgent : 'test-agent' ,
115122 } ) ;
116123
117- const audit = getAuditLogAPI ( kernel ) ! ;
124+ const audit = getAuditAPI ( kernel ) ;
118125 const events = await audit . queryEvents ( { userId : 'user-1' } ) ;
119126
120127 expect ( events . length ) . toBe ( 1 ) ;
@@ -129,7 +136,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
129136 ipAddress : '192.168.1.100' ,
130137 } ) ;
131138
132- const audit = getAuditLogAPI ( kernel ) ! ;
139+ const audit = getAuditAPI ( kernel ) ;
133140 const events = await audit . queryEvents ( { } ) ;
134141 expect ( events [ 0 ] . success ) . toBe ( false ) ;
135142 } ) ;
@@ -155,7 +162,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
155162 record : { id : 'acc-1' , name : 'Acme Corp' } ,
156163 } ) ;
157164
158- const audit = getAuditLogAPI ( kernel ) ! ;
165+ const audit = getAuditAPI ( kernel ) ;
159166 const events = await audit . queryEvents ( { objectName : 'accounts' } ) ;
160167 expect ( events . length ) . toBe ( 1 ) ;
161168 expect ( events [ 0 ] . eventType ) . toBe ( 'data.create' ) ;
@@ -183,7 +190,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
183190 } ,
184191 } ) ;
185192
186- const audit = getAuditLogAPI ( kernel ) ! ;
193+ const audit = getAuditAPI ( kernel ) ;
187194 const trail = await audit . getAuditTrail ( 'accounts' , 'acc-1' ) ;
188195 expect ( trail . length ) . toBe ( 1 ) ;
189196 expect ( trail [ 0 ] . eventType ) . toBe ( 'data.update' ) ;
@@ -206,7 +213,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
206213 userId : 'user-1' ,
207214 } ) ;
208215
209- const audit = getAuditLogAPI ( kernel ) ! ;
216+ const audit = getAuditAPI ( kernel ) ;
210217 const events = await audit . queryEvents ( {
211218 objectName : 'accounts' ,
212219 eventType : 'data.delete' ,
@@ -217,7 +224,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
217224 // ── Complete lifecycle: login → create → update → delete → audit ───────────
218225
219226 it ( 'should trace a full CRUD lifecycle in audit' , async ( ) => {
220- const audit = getAuditLogAPI ( kernel ) ! ;
227+ const audit = getAuditAPI ( kernel ) ;
221228
222229 // 1. Login
223230 await triggerHook ( hooks , 'auth.login' , {
@@ -292,7 +299,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
292299 // ── Permission denied → security event ─────────────────────────────────────
293300
294301 it ( 'should record security.access_denied when permission is denied' , async ( ) => {
295- const audit = getAuditLogAPI ( kernel ) ! ;
302+ const audit = getAuditAPI ( kernel ) ;
296303
297304 await triggerHook ( hooks , 'security.access_denied' , {
298305 userId : 'user-2' ,
@@ -310,7 +317,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
310317 // ── Authorization events → audit ───────────────────────────────────────────
311318
312319 it ( 'should record role assignment in audit trail' , async ( ) => {
313- const audit = getAuditLogAPI ( kernel ) ! ;
320+ const audit = getAuditAPI ( kernel ) ;
314321
315322 await triggerHook ( hooks , 'authz.role_assigned' , {
316323 userId : 'admin-1' ,
@@ -361,7 +368,7 @@ describe('Integration: Auth → Permissions → Data → Audit', () => {
361368 } ,
362369 } ) ;
363370
364- const auditApi = getAuditLogAPI ( mock2 . kernel ) ! ;
371+ const auditApi = getAuditAPI ( mock2 . kernel ) ;
365372 const trail = await auditApi . getAuditTrail ( 'users' , 'user-2' ) ;
366373 expect ( trail . length ) . toBe ( 1 ) ;
367374 const fields = trail [ 0 ] . changes ! . map ( ( c : any ) => c . field ) ;
0 commit comments