@@ -15,13 +15,18 @@ vi.mock('drizzle-orm', () => ({
1515 or : vi . fn ( ) ,
1616 sql : vi . fn ( ) ,
1717} ) )
18+ const { mockGetRequestContext } = vi . hoisted ( ( ) => ( {
19+ mockGetRequestContext : vi . fn ( ) ,
20+ } ) )
21+
1822vi . mock ( '@sim/logger' , ( ) => ( {
1923 createLogger : ( ) => ( {
2024 info : vi . fn ( ) ,
2125 warn : vi . fn ( ) ,
2226 error : vi . fn ( ) ,
2327 debug : vi . fn ( ) ,
2428 } ) ,
29+ getRequestContext : mockGetRequestContext ,
2530} ) )
2631vi . mock ( '@sim/utils/id' , ( ) => ( {
2732 generateId : ( ) => 'test-uuid-123' ,
@@ -181,6 +186,43 @@ describe('recordAudit', () => {
181186 )
182187 } )
183188
189+ it ( 'records the surface the request came from' , async ( ) => {
190+ mockGetRequestContext . mockReturnValueOnce ( {
191+ requestId : 'req-1' ,
192+ client : { surface : 'cli' , version : '2.1.16' , source : 'header' } ,
193+ } )
194+
195+ recordAudit ( {
196+ workspaceId : 'ws-1' ,
197+ actorId : 'user-1' ,
198+ actorName : 'Test' ,
199+ actorEmail : 'test@test.com' ,
200+ action : AuditAction . WORKFLOW_CREATED ,
201+ resourceType : AuditResourceType . WORKFLOW ,
202+ } )
203+
204+ await flush ( )
205+
206+ expect ( dbChainMockFns . values ) . toHaveBeenCalledWith ( expect . objectContaining ( { surface : 'cli' } ) )
207+ } )
208+
209+ it ( 'records no surface outside a request' , async ( ) => {
210+ mockGetRequestContext . mockReturnValueOnce ( undefined )
211+
212+ recordAudit ( {
213+ workspaceId : 'ws-1' ,
214+ actorId : 'user-1' ,
215+ actorName : 'Test' ,
216+ actorEmail : 'test@test.com' ,
217+ action : AuditAction . WORKFLOW_CREATED ,
218+ resourceType : AuditResourceType . WORKFLOW ,
219+ } )
220+
221+ await flush ( )
222+
223+ expect ( dbChainMockFns . values . mock . calls . at ( - 1 ) ?. [ 0 ] . surface ) . toBeUndefined ( )
224+ } )
225+
184226 it ( 'records null when x-forwarded-for is absent' , async ( ) => {
185227 const request = new Request ( 'https://example.com' , {
186228 headers : { 'x-real-ip' : '10.0.0.1' } ,
0 commit comments