Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions apps/sim/app/api/auth/[...all]/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @vitest-environment node
*/
import { createMockRequest } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest, resetEnvFlagsMock, setEnvFlags } from '@sim/testing'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'

const handlerMocks = vi.hoisted(() => ({
betterAuthGET: vi.fn(),
Expand All @@ -12,7 +12,6 @@ const handlerMocks = vi.hoisted(() => ({
user: { id: 'anon' },
session: { id: 'anon-session' },
})),
isAuthDisabled: false,
}))

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

vi.mock('@/lib/core/config/env-flags', () => ({
get isAuthDisabled() {
return handlerMocks.isAuthDisabled
},
}))

import { GET, POST } from '@/app/api/auth/[...all]/route'

afterAll(resetEnvFlagsMock)

describe('auth catch-all route (DISABLE_AUTH get-session)', () => {
beforeEach(() => {
vi.clearAllMocks()
handlerMocks.isAuthDisabled = false
setEnvFlags({ isAuthDisabled: false })
})

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

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

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

const { NextResponse } = await import('next/server')
handlerMocks.betterAuthGET.mockResolvedValueOnce(
Expand Down
14 changes: 8 additions & 6 deletions apps/sim/app/api/billing/switch-plan/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @vitest-environment node
*/
import { createMockRequest } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest, resetEnvFlagsMock, setEnvFlags } from '@sim/testing'
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

const {
mockCanManageWorkspaceBilling,
Expand Down Expand Up @@ -48,10 +48,6 @@ vi.mock('@/lib/billing/workspace-permissions', () => ({
canManageWorkspaceBilling: mockCanManageWorkspaceBilling,
}))

vi.mock('@/lib/core/config/env-flags', () => ({
isBillingEnabled: true,
}))

vi.mock('@/lib/posthog/server', () => ({
captureServerEvent: vi.fn(),
}))
Expand All @@ -62,6 +58,12 @@ vi.mock('@/lib/workspaces/host-context', () => ({

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

beforeAll(() => {
setEnvFlags({ isBillingEnabled: true })
})

afterAll(resetEnvFlagsMock)

describe('POST /api/billing/switch-plan', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
33 changes: 10 additions & 23 deletions apps/sim/app/api/billing/update-cost/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @vitest-environment node
*/
import { createMockRequest } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest, resetEnvFlagsMock, setEnvFlags } from '@sim/testing'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'

const {
mockCheckInternalApiKey,
Expand All @@ -15,7 +15,6 @@ const {
mockToBillingContext,
MockCumulativeUsageContextMismatchError,
MockThresholdSettlementError,
billingState,
} = vi.hoisted(() => ({
mockCheckInternalApiKey: vi.fn(),
mockRecordCumulativeUsage: vi.fn(),
Expand All @@ -36,10 +35,6 @@ const {
this.code = code
}
},
billingState: {
isBillingEnabled: true,
isCopilotBillingProtocolRequired: false,
},
}))

vi.mock('@/lib/copilot/request/http', () => ({
Expand Down Expand Up @@ -82,18 +77,11 @@ vi.mock('@/lib/billing/threshold-billing', () => ({
ThresholdSettlementError: MockThresholdSettlementError,
}))

vi.mock('@/lib/core/config/env-flags', () => ({
get isBillingEnabled() {
return billingState.isBillingEnabled
},
get isCopilotBillingProtocolRequired() {
return billingState.isCopilotBillingProtocolRequired
},
}))

import { billingUpdateCostBodySchema } from '@/lib/api/contracts/subscription'
import { POST } from '@/app/api/billing/update-cost/route'

afterAll(resetEnvFlagsMock)

const ACCOUNT_BILLING_DECISION = {
userId: 'user-1',
billingEntity: { type: 'organization' as const, id: 'account-org' },
Expand Down Expand Up @@ -159,8 +147,7 @@ const KEYLESS_UPDATE_COST_BODY = {
describe('POST /api/billing/update-cost — workspaceId attribution', () => {
beforeEach(() => {
vi.clearAllMocks()
billingState.isBillingEnabled = true
billingState.isCopilotBillingProtocolRequired = false
setEnvFlags({ isBillingEnabled: true, isCopilotBillingProtocolRequired: false })
mockCheckInternalApiKey.mockReturnValue({ success: true })
mockRecordCumulativeUsage.mockResolvedValue({ billed: true, delta: 0.5, total: 0.5 })
mockCheckAndBillOverageThreshold.mockResolvedValue(undefined)
Expand All @@ -178,7 +165,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
})

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

const res = await POST(
Expand All @@ -200,7 +187,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
})

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

const res = await POST(
createMockRequest(
Expand Down Expand Up @@ -303,7 +290,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => {
})

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

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

it('bills explicitly labeled legacy callbacks from their admission attribution', async () => {
billingState.isCopilotBillingProtocolRequired = true
setEnvFlags({ isCopilotBillingProtocolRequired: true })
const res = await POST(
createMockRequest('POST', EXPLICIT_LEGACY_HOSTED_UPDATE_COST_BODY, {
'x-api-key': 'internal',
Expand Down
15 changes: 9 additions & 6 deletions apps/sim/app/api/chat/manage/[id]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import {
encryptionMock,
encryptionMockFns,
resetDbChainMock,
resetEnvFlagsMock,
setEnvFlags,
workflowsApiUtilsMock,
workflowsApiUtilsMockFns,
workflowsOrchestrationMock,
workflowsOrchestrationMockFns,
workflowsPersistenceUtilsMock,
} from '@sim/testing'
import { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

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

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

beforeAll(() => {
setEnvFlags({ isDev: true })
})

afterAll(resetEnvFlagsMock)

describe('Chat Edit API Route', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
27 changes: 13 additions & 14 deletions apps/sim/app/api/copilot/api-keys/validate/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/**
* @vitest-environment node
*/
import { createMockRequest, queueTableRows, resetDbChainMock, schemaMock } from '@sim/testing'
import {
createMockRequest,
queueTableRows,
resetDbChainMock,
resetEnvFlagsMock,
schemaMock,
setEnvFlags,
} from '@sim/testing'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'

const {
Expand All @@ -18,7 +25,6 @@ const {
mockSerializeBillingAttributionHeader,
mockGetUserEntityPermissions,
mockGetWorkspaceBillingSettings,
mockFlags,
} = vi.hoisted(() => ({
mockCheckInternalApiKey: vi.fn(),
mockCheckAttributedUsageLimits: vi.fn(),
Expand All @@ -33,9 +39,6 @@ const {
mockSerializeBillingAttributionHeader: vi.fn(),
mockGetUserEntityPermissions: vi.fn(),
mockGetWorkspaceBillingSettings: vi.fn(),
mockFlags: {
isCopilotBillingProtocolRequired: false,
},
}))

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

vi.mock('@/lib/core/config/env-flags', () => ({
get isCopilotBillingProtocolRequired() {
return mockFlags.isCopilotBillingProtocolRequired
},
}))

vi.mock('@/lib/workspaces/permissions/utils', () => ({
getUserEntityPermissions: mockGetUserEntityPermissions,
}))
Expand All @@ -136,6 +133,8 @@ vi.mock('@/lib/workspaces/utils', () => ({
import { validateCopilotApiKeyBodySchema } from '@/lib/api/contracts/copilot'
import { POST } from '@/app/api/copilot/api-keys/validate/route'

afterAll(resetEnvFlagsMock)

function request(body: Record<string, unknown>, headers: Record<string, string> = {}) {
return createMockRequest('POST', body, { 'x-api-key': 'internal', ...headers })
}
Expand All @@ -144,7 +143,7 @@ describe('POST /api/copilot/api-keys/validate billing protocols', () => {
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
mockFlags.isCopilotBillingProtocolRequired = false
setEnvFlags({ isCopilotBillingProtocolRequired: false })
mockCheckInternalApiKey.mockReturnValue({ success: true })
queueTableRows(schemaMock.user, [{ id: 'user-1' }])
mockResolveBillingAttribution.mockResolvedValue(ATTRIBUTION)
Expand Down Expand Up @@ -258,7 +257,7 @@ describe('POST /api/copilot/api-keys/validate billing protocols', () => {
})

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

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

it('allows explicitly labeled legacy requests when markerless traffic is disabled', async () => {
mockFlags.isCopilotBillingProtocolRequired = true
setEnvFlags({ isCopilotBillingProtocolRequired: true })
const res = await POST(
request(OLD_GO_HOSTED_VALIDATE_BODY, { 'x-sim-billing-protocol': 'legacy-v0' })
)
Expand Down
7 changes: 4 additions & 3 deletions apps/sim/app/api/function/execute/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
createMockRequest,
envFlagsMock,
hybridAuthMockFns,
resetEnvFlagsMock,
workflowsUtilsMock,
} from '@sim/testing'
import { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'

const {
mockExecuteInE2B,
Expand Down Expand Up @@ -101,14 +102,14 @@ vi.mock('@/lib/uploads', () => ({

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

vi.mock('@/lib/core/config/env-flags', () => envFlagsMock)

import { validateProxyUrl } from '@/lib/core/security/input-validation'
import { clearLargeValueCacheForTests } from '@/lib/execution/payloads/cache'
import { isLargeArrayManifest } from '@/lib/execution/payloads/large-array-manifest-metadata'
import { isLargeValueRef } from '@/lib/execution/payloads/large-value-ref'
import { POST } from '@/app/api/function/execute/route'

afterAll(resetEnvFlagsMock)

describe('Function Execute API Route', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
2 changes: 0 additions & 2 deletions apps/sim/app/api/knowledge/search/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vites
import { env } from '@/lib/core/config/env'
import * as documentsUtilsModule from '@/lib/knowledge/documents/utils'

vi.mock('drizzle-orm')

/**
* Spy on the real documents/utils namespace instead of vi.mock: the shared
* `@/lib/knowledge/embeddings` module may be cached bound to the real module,
Expand Down
Loading
Loading