11/**
22 * @vitest -environment node
33 */
4- import { createMockRequest } from '@sim/testing'
5- import { beforeEach , describe , expect , it , vi } from 'vitest'
4+ import { createMockRequest , resetEnvFlagsMock , setEnvFlags } from '@sim/testing'
5+ import { afterAll , beforeEach , describe , expect , it , vi } from 'vitest'
66
77const {
88 mockCheckInternalApiKey,
@@ -15,7 +15,6 @@ const {
1515 mockToBillingContext,
1616 MockCumulativeUsageContextMismatchError,
1717 MockThresholdSettlementError,
18- billingState,
1918} = vi . hoisted ( ( ) => ( {
2019 mockCheckInternalApiKey : vi . fn ( ) ,
2120 mockRecordCumulativeUsage : vi . fn ( ) ,
@@ -36,10 +35,6 @@ const {
3635 this . code = code
3736 }
3837 } ,
39- billingState : {
40- isBillingEnabled : true ,
41- isCopilotBillingProtocolRequired : false ,
42- } ,
4338} ) )
4439
4540vi . mock ( '@/lib/copilot/request/http' , ( ) => ( {
@@ -82,18 +77,11 @@ vi.mock('@/lib/billing/threshold-billing', () => ({
8277 ThresholdSettlementError : MockThresholdSettlementError ,
8378} ) )
8479
85- vi . mock ( '@/lib/core/config/env-flags' , ( ) => ( {
86- get isBillingEnabled ( ) {
87- return billingState . isBillingEnabled
88- } ,
89- get isCopilotBillingProtocolRequired ( ) {
90- return billingState . isCopilotBillingProtocolRequired
91- } ,
92- } ) )
93-
9480import { billingUpdateCostBodySchema } from '@/lib/api/contracts/subscription'
9581import { POST } from '@/app/api/billing/update-cost/route'
9682
83+ afterAll ( resetEnvFlagsMock )
84+
9785const ACCOUNT_BILLING_DECISION = {
9886 userId : 'user-1' ,
9987 billingEntity : { type : 'organization' as const , id : 'account-org' } ,
@@ -159,8 +147,7 @@ const KEYLESS_UPDATE_COST_BODY = {
159147describe ( 'POST /api/billing/update-cost — workspaceId attribution' , ( ) => {
160148 beforeEach ( ( ) => {
161149 vi . clearAllMocks ( )
162- billingState . isBillingEnabled = true
163- billingState . isCopilotBillingProtocolRequired = false
150+ setEnvFlags ( { isBillingEnabled : true , isCopilotBillingProtocolRequired : false } )
164151 mockCheckInternalApiKey . mockReturnValue ( { success : true } )
165152 mockRecordCumulativeUsage . mockResolvedValue ( { billed : true , delta : 0.5 , total : 0.5 } )
166153 mockCheckAndBillOverageThreshold . mockResolvedValue ( undefined )
@@ -178,7 +165,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
178165 } )
179166
180167 it ( 'returns 401 for a billing-disabled request without valid internal auth' , async ( ) => {
181- billingState . isBillingEnabled = false
168+ setEnvFlags ( { isBillingEnabled : false } )
182169 mockCheckInternalApiKey . mockReturnValue ( { success : false , error : 'Invalid internal API key' } )
183170
184171 const res = await POST (
@@ -200,7 +187,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
200187 } )
201188
202189 it ( 'returns no-op success for old markerless Go when billing is disabled' , async ( ) => {
203- billingState . isBillingEnabled = false
190+ setEnvFlags ( { isBillingEnabled : false } )
204191
205192 const res = await POST (
206193 createMockRequest (
@@ -303,7 +290,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
303290 } )
304291
305292 it ( 'rejects markerless callbacks only when protocol-required is explicitly enabled' , async ( ) => {
306- billingState . isCopilotBillingProtocolRequired = true
293+ setEnvFlags ( { isCopilotBillingProtocolRequired : true } )
307294 const res = await POST (
308295 createMockRequest ( 'POST' , OLD_GO_HOSTED_UPDATE_COST_BODY , { 'x-api-key' : 'internal' } )
309296 )
@@ -328,7 +315,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
328315 } )
329316
330317 it ( 'rejects explicitly labeled legacy callbacks without admission attribution' , async ( ) => {
331- billingState . isCopilotBillingProtocolRequired = true
318+ setEnvFlags ( { isCopilotBillingProtocolRequired : true } )
332319 const res = await POST (
333320 createMockRequest ( 'POST' , EXPLICIT_LEGACY_HOSTED_UPDATE_COST_BODY , {
334321 'x-api-key' : 'internal' ,
@@ -343,7 +330,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
343330 } )
344331
345332 it ( 'bills explicitly labeled legacy callbacks from their admission attribution' , async ( ) => {
346- billingState . isCopilotBillingProtocolRequired = true
333+ setEnvFlags ( { isCopilotBillingProtocolRequired : true } )
347334 const res = await POST (
348335 createMockRequest ( 'POST' , EXPLICIT_LEGACY_HOSTED_UPDATE_COST_BODY , {
349336 'x-api-key' : 'internal' ,
0 commit comments