Skip to content

Commit c164afc

Browse files
committed
improvement(tests): converge env-flags mocks onto a complete shared mock
1 parent 2b5a92a commit c164afc

68 files changed

Lines changed: 740 additions & 676 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/sim/app/api/auth/[...all]/route.test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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

77
const handlerMocks = vi.hoisted(() => ({
88
betterAuthGET: vi.fn(),
@@ -12,7 +12,6 @@ const handlerMocks = vi.hoisted(() => ({
1212
user: { id: 'anon' },
1313
session: { id: 'anon-session' },
1414
})),
15-
isAuthDisabled: false,
1615
}))
1716

1817
vi.mock('better-auth/next-js', () => ({
@@ -31,22 +30,18 @@ vi.mock('@/lib/auth/anonymous', () => ({
3130
createAnonymousSession: handlerMocks.createAnonymousSession,
3231
}))
3332

34-
vi.mock('@/lib/core/config/env-flags', () => ({
35-
get isAuthDisabled() {
36-
return handlerMocks.isAuthDisabled
37-
},
38-
}))
39-
4033
import { GET, POST } from '@/app/api/auth/[...all]/route'
4134

35+
afterAll(resetEnvFlagsMock)
36+
4237
describe('auth catch-all route (DISABLE_AUTH get-session)', () => {
4338
beforeEach(() => {
4439
vi.clearAllMocks()
45-
handlerMocks.isAuthDisabled = false
40+
setEnvFlags({ isAuthDisabled: false })
4641
})
4742

4843
it('returns anonymous session in better-auth response envelope when auth is disabled', async () => {
49-
handlerMocks.isAuthDisabled = true
44+
setEnvFlags({ isAuthDisabled: true })
5045

5146
const req = createMockRequest(
5247
'GET',
@@ -67,7 +62,7 @@ describe('auth catch-all route (DISABLE_AUTH get-session)', () => {
6762
})
6863

6964
it('delegates to better-auth handler when auth is enabled', async () => {
70-
handlerMocks.isAuthDisabled = false
65+
setEnvFlags({ isAuthDisabled: false })
7166

7267
const { NextResponse } = await import('next/server')
7368
handlerMocks.betterAuthGET.mockResolvedValueOnce(

apps/sim/app/api/billing/switch-plan/route.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
66

77
const {
88
mockCanManageWorkspaceBilling,
@@ -48,10 +48,6 @@ vi.mock('@/lib/billing/workspace-permissions', () => ({
4848
canManageWorkspaceBilling: mockCanManageWorkspaceBilling,
4949
}))
5050

51-
vi.mock('@/lib/core/config/env-flags', () => ({
52-
isBillingEnabled: true,
53-
}))
54-
5551
vi.mock('@/lib/posthog/server', () => ({
5652
captureServerEvent: vi.fn(),
5753
}))
@@ -62,6 +58,12 @@ vi.mock('@/lib/workspaces/host-context', () => ({
6258

6359
import { POST } from '@/app/api/billing/switch-plan/route'
6460

61+
beforeAll(() => {
62+
setEnvFlags({ isBillingEnabled: true })
63+
})
64+
65+
afterAll(resetEnvFlagsMock)
66+
6567
describe('POST /api/billing/switch-plan', () => {
6668
beforeEach(() => {
6769
vi.clearAllMocks()

apps/sim/app/api/billing/update-cost/route.test.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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

77
const {
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

4540
vi.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-
9480
import { billingUpdateCostBodySchema } from '@/lib/api/contracts/subscription'
9581
import { POST } from '@/app/api/billing/update-cost/route'
9682

83+
afterAll(resetEnvFlagsMock)
84+
9785
const ACCOUNT_BILLING_DECISION = {
9886
userId: 'user-1',
9987
billingEntity: { type: 'organization' as const, id: 'account-org' },
@@ -159,8 +147,8 @@ const KEYLESS_UPDATE_COST_BODY = {
159147
describe('POST /api/billing/update-cost — workspaceId attribution', () => {
160148
beforeEach(() => {
161149
vi.clearAllMocks()
162-
billingState.isBillingEnabled = true
163-
billingState.isCopilotBillingProtocolRequired = false
150+
setEnvFlags({ isBillingEnabled: true })
151+
setEnvFlags({ isCopilotBillingProtocolRequired: false })
164152
mockCheckInternalApiKey.mockReturnValue({ success: true })
165153
mockRecordCumulativeUsage.mockResolvedValue({ billed: true, delta: 0.5, total: 0.5 })
166154
mockCheckAndBillOverageThreshold.mockResolvedValue(undefined)
@@ -178,7 +166,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
178166
})
179167

180168
it('returns 401 for a billing-disabled request without valid internal auth', async () => {
181-
billingState.isBillingEnabled = false
169+
setEnvFlags({ isBillingEnabled: false })
182170
mockCheckInternalApiKey.mockReturnValue({ success: false, error: 'Invalid internal API key' })
183171

184172
const res = await POST(
@@ -200,7 +188,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
200188
})
201189

202190
it('returns no-op success for old markerless Go when billing is disabled', async () => {
203-
billingState.isBillingEnabled = false
191+
setEnvFlags({ isBillingEnabled: false })
204192

205193
const res = await POST(
206194
createMockRequest(
@@ -303,7 +291,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
303291
})
304292

305293
it('rejects markerless callbacks only when protocol-required is explicitly enabled', async () => {
306-
billingState.isCopilotBillingProtocolRequired = true
294+
setEnvFlags({ isCopilotBillingProtocolRequired: true })
307295
const res = await POST(
308296
createMockRequest('POST', OLD_GO_HOSTED_UPDATE_COST_BODY, { 'x-api-key': 'internal' })
309297
)
@@ -328,7 +316,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
328316
})
329317

330318
it('rejects explicitly labeled legacy callbacks without admission attribution', async () => {
331-
billingState.isCopilotBillingProtocolRequired = true
319+
setEnvFlags({ isCopilotBillingProtocolRequired: true })
332320
const res = await POST(
333321
createMockRequest('POST', EXPLICIT_LEGACY_HOSTED_UPDATE_COST_BODY, {
334322
'x-api-key': 'internal',
@@ -343,7 +331,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
343331
})
344332

345333
it('bills explicitly labeled legacy callbacks from their admission attribution', async () => {
346-
billingState.isCopilotBillingProtocolRequired = true
334+
setEnvFlags({ isCopilotBillingProtocolRequired: true })
347335
const res = await POST(
348336
createMockRequest('POST', EXPLICIT_LEGACY_HOSTED_UPDATE_COST_BODY, {
349337
'x-api-key': 'internal',

apps/sim/app/api/chat/manage/[id]/route.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import {
1111
encryptionMock,
1212
encryptionMockFns,
1313
resetDbChainMock,
14+
resetEnvFlagsMock,
15+
setEnvFlags,
1416
workflowsApiUtilsMock,
1517
workflowsApiUtilsMockFns,
1618
workflowsOrchestrationMock,
1719
workflowsOrchestrationMockFns,
1820
workflowsPersistenceUtilsMock,
1921
} from '@sim/testing'
2022
import { NextRequest } from 'next/server'
21-
import { beforeEach, describe, expect, it, vi } from 'vitest'
23+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
2224

2325
const { mockCheckChatAccess, mockValidateChatDeployAuth } = vi.hoisted(() => ({
2426
mockCheckChatAccess: vi.fn(),
@@ -37,11 +39,6 @@ const mockNotifySocketDeploymentChanged =
3739
workflowsOrchestrationMockFns.mockNotifySocketDeploymentChanged
3840

3941
vi.mock('@sim/audit', () => auditMock)
40-
vi.mock('@/lib/core/config/env-flags', () => ({
41-
isDev: true,
42-
isHosted: false,
43-
isProd: false,
44-
}))
4542
vi.mock('@sim/db', () => dbChainMock)
4643
vi.mock('@/app/api/workflows/utils', () => workflowsApiUtilsMock)
4744
vi.mock('@/lib/core/security/encryption', () => encryptionMock)
@@ -66,6 +63,12 @@ vi.mock('@/lib/workflows/orchestration', () => workflowsOrchestrationMock)
6663
import { DELETE, GET, PATCH } from '@/app/api/chat/manage/[id]/route'
6764
import { ChatDeployAuthNotAllowedError } from '@/ee/access-control/utils/permission-check'
6865

66+
beforeAll(() => {
67+
setEnvFlags({ isDev: true })
68+
})
69+
70+
afterAll(resetEnvFlagsMock)
71+
6972
describe('Chat Edit API Route', () => {
7073
beforeEach(() => {
7174
vi.clearAllMocks()

apps/sim/app/api/copilot/api-keys/validate/route.test.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/**
22
* @vitest-environment node
33
*/
4-
import { createMockRequest, queueTableRows, resetDbChainMock, schemaMock } from '@sim/testing'
4+
import {
5+
createMockRequest,
6+
queueTableRows,
7+
resetDbChainMock,
8+
resetEnvFlagsMock,
9+
schemaMock,
10+
setEnvFlags,
11+
} from '@sim/testing'
512
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
613

714
const {
@@ -18,7 +25,6 @@ const {
1825
mockSerializeBillingAttributionHeader,
1926
mockGetUserEntityPermissions,
2027
mockGetWorkspaceBillingSettings,
21-
mockFlags,
2228
} = vi.hoisted(() => ({
2329
mockCheckInternalApiKey: vi.fn(),
2430
mockCheckAttributedUsageLimits: vi.fn(),
@@ -33,9 +39,6 @@ const {
3339
mockSerializeBillingAttributionHeader: vi.fn(),
3440
mockGetUserEntityPermissions: vi.fn(),
3541
mockGetWorkspaceBillingSettings: vi.fn(),
36-
mockFlags: {
37-
isCopilotBillingProtocolRequired: false,
38-
},
3942
}))
4043

4144
const ATTRIBUTION = {
@@ -119,12 +122,6 @@ vi.mock('@/lib/copilot/request/otel', () => ({
119122
) => fn({ setAttribute: vi.fn(), setAttributes: vi.fn() }),
120123
}))
121124

122-
vi.mock('@/lib/core/config/env-flags', () => ({
123-
get isCopilotBillingProtocolRequired() {
124-
return mockFlags.isCopilotBillingProtocolRequired
125-
},
126-
}))
127-
128125
vi.mock('@/lib/workspaces/permissions/utils', () => ({
129126
getUserEntityPermissions: mockGetUserEntityPermissions,
130127
}))
@@ -136,6 +133,8 @@ vi.mock('@/lib/workspaces/utils', () => ({
136133
import { validateCopilotApiKeyBodySchema } from '@/lib/api/contracts/copilot'
137134
import { POST } from '@/app/api/copilot/api-keys/validate/route'
138135

136+
afterAll(resetEnvFlagsMock)
137+
139138
function request(body: Record<string, unknown>, headers: Record<string, string> = {}) {
140139
return createMockRequest('POST', body, { 'x-api-key': 'internal', ...headers })
141140
}
@@ -144,7 +143,7 @@ describe('POST /api/copilot/api-keys/validate billing protocols', () => {
144143
beforeEach(() => {
145144
vi.clearAllMocks()
146145
resetDbChainMock()
147-
mockFlags.isCopilotBillingProtocolRequired = false
146+
setEnvFlags({ isCopilotBillingProtocolRequired: false })
148147
mockCheckInternalApiKey.mockReturnValue({ success: true })
149148
queueTableRows(schemaMock.user, [{ id: 'user-1' }])
150149
mockResolveBillingAttribution.mockResolvedValue(ATTRIBUTION)
@@ -258,7 +257,7 @@ describe('POST /api/copilot/api-keys/validate billing protocols', () => {
258257
})
259258

260259
it('rejects markerless admission only when protocol-required is explicitly enabled', async () => {
261-
mockFlags.isCopilotBillingProtocolRequired = true
260+
setEnvFlags({ isCopilotBillingProtocolRequired: true })
262261
const res = await POST(request(OLD_GO_HOSTED_VALIDATE_BODY))
263262

264263
expect(res.status).toBe(400)
@@ -267,7 +266,7 @@ describe('POST /api/copilot/api-keys/validate billing protocols', () => {
267266
})
268267

269268
it('allows explicitly labeled legacy requests when markerless traffic is disabled', async () => {
270-
mockFlags.isCopilotBillingProtocolRequired = true
269+
setEnvFlags({ isCopilotBillingProtocolRequired: true })
271270
const res = await POST(
272271
request(OLD_GO_HOSTED_VALIDATE_BODY, { 'x-sim-billing-protocol': 'legacy-v0' })
273272
)

apps/sim/app/api/function/execute/route.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
createMockRequest,
88
envFlagsMock,
99
hybridAuthMockFns,
10+
resetEnvFlagsMock,
1011
workflowsUtilsMock,
1112
} from '@sim/testing'
1213
import { NextRequest } from 'next/server'
13-
import { beforeEach, describe, expect, it, vi } from 'vitest'
14+
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
1415

1516
const {
1617
mockExecuteInE2B,
@@ -101,14 +102,14 @@ vi.mock('@/lib/uploads', () => ({
101102

102103
vi.mock('@/lib/workflows/utils', () => workflowsUtilsMock)
103104

104-
vi.mock('@/lib/core/config/env-flags', () => envFlagsMock)
105-
106105
import { validateProxyUrl } from '@/lib/core/security/input-validation'
107106
import { clearLargeValueCacheForTests } from '@/lib/execution/payloads/cache'
108107
import { isLargeArrayManifest } from '@/lib/execution/payloads/large-array-manifest-metadata'
109108
import { isLargeValueRef } from '@/lib/execution/payloads/large-value-ref'
110109
import { POST } from '@/app/api/function/execute/route'
111110

111+
afterAll(resetEnvFlagsMock)
112+
112113
describe('Function Execute API Route', () => {
113114
beforeEach(() => {
114115
vi.clearAllMocks()

0 commit comments

Comments
 (0)