66 resetDbChainMock ,
77} from '@sim/testing'
88import { isPlainRecord } from '@sim/utils/object'
9- import { afterAll , beforeEach , describe , expect , onTestFinished , test , vi } from 'vitest'
10- import { readThresholdEmailUsageBefore } from '@/lib/billing/core/reporting-usage-cache'
11- import { maybeSendUsageThresholdEmail } from '@/lib/billing/core/usage'
9+ import { afterAll , beforeEach , describe , expect , test , vi } from 'vitest'
1210import { recordUsage } from '@/lib/billing/core/usage-log'
1311import { ExecutionLogger } from '@/lib/logs/execution/logger'
1412import { SECRET_PROJECTION_VERSION } from '@/lib/logs/execution/trace-store'
@@ -68,7 +66,6 @@ vi.mock('@/lib/billing/core/billing-attribution', () => ({
6866 billingPeriod : {
6967 start : new Date ( attribution . billingPeriod . start ) ,
7068 end : new Date ( attribution . billingPeriod . end ) ,
71- ...( attribution . billingPeriod . source ? { source : attribution . billingPeriod . source } : { } ) ,
7269 } ,
7370 } ) ) ,
7471} ) )
@@ -92,10 +89,6 @@ vi.mock('@/lib/billing/core/usage', () => ({
9289 maybeSendUsageThresholdEmail : vi . fn ( ( ) => Promise . resolve ( ) ) ,
9390} ) )
9491
95- vi . mock ( '@/lib/billing/core/reporting-usage-cache' , ( ) => ( {
96- readThresholdEmailUsageBefore : vi . fn ( ( ) => Promise . resolve ( 250 ) ) ,
97- } ) )
98-
9992vi . mock ( '@/lib/billing/core/usage-log' , ( ) => ( {
10093 recordUsage : vi . fn ( ( ) => Promise . resolve ( ) ) ,
10194 stableEventKey : vi . fn ( ( parts : Record < string , unknown > ) => JSON . stringify ( parts ) ) ,
@@ -393,99 +386,6 @@ describe('ExecutionLogger', () => {
393386 expect ( written ?. resolvedSecretTraceProvenance ) . toEqual ( RUN_PROVENANCE )
394387 } )
395388
396- test ( 'reads the threshold-email baseline through the soft reporting-window reader' , async ( ) => {
397- const startedAt = new Date ( '2026-08-11T00:00:00.000Z' )
398- queueTableRows ( workflowExecutionLogs , [
399- {
400- id : 'log-1' ,
401- workflowId : 'workflow-1' ,
402- workspaceId : 'workspace-1' ,
403- executionId : 'execution-1' ,
404- stateSnapshotId : 'snapshot-1' ,
405- level : 'info' ,
406- status : 'running' ,
407- trigger : 'api' ,
408- startedAt,
409- endedAt : null ,
410- totalDurationMs : null ,
411- executionData : { } ,
412- createdAt : startedAt ,
413- } ,
414- ] )
415- dbChainMockFns . returning . mockResolvedValue ( [
416- { id : 'log-1' , executionData : { } , startedAt, createdAt : startedAt } ,
417- ] )
418- onTestFinished ( resetDbChainMock )
419- const internals = logger as unknown as {
420- applyPiiRedaction : ( workspaceId : string , payload : unknown ) => unknown
421- recordExecutionUsage : ( ) => Promise < number >
422- }
423- vi . spyOn ( internals , 'applyPiiRedaction' ) . mockImplementation (
424- async ( _workspaceId : string , payload : unknown ) => payload
425- )
426- vi . spyOn ( internals , 'recordExecutionUsage' ) . mockResolvedValue ( 10 )
427-
428- await logger . completeWorkflowExecution ( {
429- executionId : 'execution-1' ,
430- endedAt : '2026-08-11T00:00:02.000Z' ,
431- totalDurationMs : 2000 ,
432- costSummary : {
433- totalCost : 0 ,
434- totalInputCost : 0 ,
435- totalOutputCost : 0 ,
436- totalTokens : 0 ,
437- totalPromptTokens : 0 ,
438- totalCompletionTokens : 0 ,
439- baseExecutionCharge : 0 ,
440- models : { } ,
441- } ,
442- finalOutput : { } ,
443- traceSpans : [ ] ,
444- billingAttribution : {
445- actorUserId : 'actor-1' ,
446- workspaceId : 'workspace-1' ,
447- organizationId : 'org-enterprise' ,
448- billedAccountUserId : 'owner-1' ,
449- billingEntity : { type : 'organization' , id : 'org-enterprise' } ,
450- billingPeriod : {
451- start : '2026-01-01T00:00:00.000Z' ,
452- end : '2027-01-01T00:00:00.000Z' ,
453- source : 'reporting' ,
454- } ,
455- payerSubscription : {
456- id : 'sub-1' ,
457- plan : 'enterprise' ,
458- status : 'active' ,
459- seats : 10 ,
460- periodStart : '2026-01-01T00:00:00.000Z' ,
461- periodEnd : '2027-01-01T00:00:00.000Z' ,
462- } ,
463- } as unknown as NonNullable <
464- Parameters < ExecutionLogger [ 'completeWorkflowExecution' ] > [ 0 ] [ 'billingAttribution' ]
465- > ,
466- } )
467-
468- expect ( readThresholdEmailUsageBefore ) . toHaveBeenCalledWith (
469- { type : 'organization' , id : 'org-enterprise' } ,
470- {
471- start : new Date ( '2026-01-01T00:00:00.000Z' ) ,
472- end : new Date ( '2027-01-01T00:00:00.000Z' ) ,
473- source : 'reporting' ,
474- } ,
475- 1000
476- )
477- expect ( maybeSendUsageThresholdEmail ) . toHaveBeenCalledWith (
478- expect . objectContaining ( {
479- scope : 'organization' ,
480- organizationId : 'org-enterprise' ,
481- percentBefore : 25 ,
482- percentAfter : 26 ,
483- currentUsageAfter : 260 ,
484- limit : 1000 ,
485- } )
486- )
487- } )
488-
489389 test ( 'omits the provenance key when the run carried none' , async ( ) => {
490390 const written = await completeAndReadWrite ( { } )
491391
0 commit comments