Skip to content

Commit bf20737

Browse files
committed
refactor to use createEnvMock helper instead of local mocks
1 parent 8cbe082 commit bf20737

File tree

9 files changed

+34
-63
lines changed

9 files changed

+34
-63
lines changed

apps/sim/app/api/knowledge/search/route.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* @vitest-environment node
77
*/
8+
import { createEnvMock } from '@sim/testing'
89
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
910
import {
1011
createMockRequest,
@@ -26,13 +27,7 @@ vi.mock('drizzle-orm', () => ({
2627

2728
mockKnowledgeSchemas()
2829

29-
vi.mock('@/lib/core/config/env', () => ({
30-
env: {
31-
OPENAI_API_KEY: 'test-api-key',
32-
},
33-
isTruthy: (value: string | boolean | number | undefined) =>
34-
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value),
35-
}))
30+
vi.mock('@/lib/core/config/env', () => createEnvMock({ OPENAI_API_KEY: 'test-api-key' }))
3631

3732
vi.mock('@/lib/core/utils/request', () => ({
3833
generateRequestId: vi.fn(() => 'test-request-id'),

apps/sim/app/api/knowledge/search/utils.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* @vitest-environment node
66
*/
7+
import { createEnvMock } from '@sim/testing'
78
import { beforeEach, describe, expect, it, vi } from 'vitest'
89

910
vi.mock('drizzle-orm')
@@ -30,12 +31,7 @@ vi.stubGlobal(
3031
})
3132
)
3233

33-
vi.mock('@/lib/core/config/env', () => ({
34-
env: {},
35-
getEnv: (key: string) => process.env[key],
36-
isTruthy: (value: string | boolean | number | undefined) =>
37-
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value),
38-
}))
34+
vi.mock('@/lib/core/config/env', () => createEnvMock())
3935

4036
import {
4137
generateSearchEmbedding,

apps/sim/app/api/knowledge/utils.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* This file contains unit tests for the knowledge base utility functions,
77
* including access checks, document processing, and embedding generation.
88
*/
9+
import { createEnvMock } from '@sim/testing'
910
import { beforeEach, describe, expect, it, vi } from 'vitest'
1011

1112
vi.mock('drizzle-orm', () => ({
@@ -15,12 +16,7 @@ vi.mock('drizzle-orm', () => ({
1516
sql: (strings: TemplateStringsArray, ...expr: any[]) => ({ strings, expr }),
1617
}))
1718

18-
vi.mock('@/lib/core/config/env', () => ({
19-
env: { OPENAI_API_KEY: 'test-key' },
20-
getEnv: (key: string) => process.env[key],
21-
isTruthy: (value: string | boolean | number | undefined) =>
22-
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value),
23-
}))
19+
vi.mock('@/lib/core/config/env', () => createEnvMock({ OPENAI_API_KEY: 'test-key' }))
2420

2521
vi.mock('@/lib/knowledge/documents/utils', () => ({
2622
retryWithExponentialBackoff: (fn: any) => fn(),

apps/sim/lib/core/security/csp.test.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { createEnvMock } from '@sim/testing'
12
import { afterEach, describe, expect, it, vi } from 'vitest'
23

3-
vi.mock('@/lib/core/config/env', () => ({
4-
env: {
4+
vi.mock('@/lib/core/config/env', () =>
5+
createEnvMock({
56
NEXT_PUBLIC_APP_URL: 'https://example.com',
67
NEXT_PUBLIC_SOCKET_URL: 'https://socket.example.com',
78
OLLAMA_URL: 'http://localhost:11434',
@@ -13,20 +14,8 @@ vi.mock('@/lib/core/config/env', () => ({
1314
NEXT_PUBLIC_BRAND_FAVICON_URL: 'https://brand.example.com/favicon.ico',
1415
NEXT_PUBLIC_PRIVACY_URL: 'https://legal.example.com/privacy',
1516
NEXT_PUBLIC_TERMS_URL: 'https://legal.example.com/terms',
16-
},
17-
getEnv: vi.fn((key: string) => {
18-
const envMap: Record<string, string> = {
19-
NEXT_PUBLIC_APP_URL: 'https://example.com',
20-
NEXT_PUBLIC_SOCKET_URL: 'https://socket.example.com',
21-
OLLAMA_URL: 'http://localhost:11434',
22-
NEXT_PUBLIC_BRAND_LOGO_URL: 'https://brand.example.com/logo.png',
23-
NEXT_PUBLIC_BRAND_FAVICON_URL: 'https://brand.example.com/favicon.ico',
24-
NEXT_PUBLIC_PRIVACY_URL: 'https://legal.example.com/privacy',
25-
NEXT_PUBLIC_TERMS_URL: 'https://legal.example.com/terms',
26-
}
27-
return envMap[key] || ''
28-
}),
29-
}))
17+
})
18+
)
3019

3120
vi.mock('@/lib/core/config/feature-flags', () => ({
3221
isDev: false,

apps/sim/lib/core/security/encryption.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import { createEnvMock } from '@sim/testing'
12
import { afterEach, describe, expect, it, vi } from 'vitest'
23

3-
const mockEnv = vi.hoisted(() => ({
4-
ENCRYPTION_KEY: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
5-
}))
6-
7-
vi.mock('@/lib/core/config/env', () => ({
8-
env: mockEnv,
9-
}))
4+
vi.mock('@/lib/core/config/env', () =>
5+
createEnvMock({
6+
ENCRYPTION_KEY: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
7+
})
8+
)
109

1110
vi.mock('@sim/logger', () => ({
1211
createLogger: () => ({

apps/sim/lib/messaging/email/mailer.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createEnvMock } from '@sim/testing'
12
import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest'
23

34
/**
@@ -44,15 +45,15 @@ vi.mock('@/lib/messaging/email/unsubscribe', () => ({
4445
}))
4546

4647
// Mock env with valid API keys so the clients get initialized
47-
vi.mock('@/lib/core/config/env', () => ({
48-
env: {
48+
vi.mock('@/lib/core/config/env', () =>
49+
createEnvMock({
4950
RESEND_API_KEY: 'test-api-key',
5051
AZURE_ACS_CONNECTION_STRING: 'test-azure-connection-string',
5152
AZURE_COMMUNICATION_EMAIL_DOMAIN: 'test.azurecomm.net',
5253
NEXT_PUBLIC_APP_URL: 'https://test.sim.ai',
5354
FROM_EMAIL_ADDRESS: 'Sim <[email protected]>',
54-
},
55-
}))
55+
})
56+
)
5657

5758
// Mock URL utilities
5859
vi.mock('@/lib/core/utils/urls', () => ({

apps/sim/lib/messaging/email/unsubscribe.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createEnvMock } from '@sim/testing'
12
import { beforeEach, describe, expect, it, vi } from 'vitest'
23
import type { EmailType } from '@/lib/messaging/email/mailer'
34

@@ -25,14 +26,7 @@ vi.mock('drizzle-orm', () => ({
2526
eq: vi.fn((a, b) => ({ type: 'eq', left: a, right: b })),
2627
}))
2728

28-
vi.mock('@/lib/core/config/env', () => ({
29-
env: {
30-
BETTER_AUTH_SECRET: 'test-secret-key',
31-
},
32-
isTruthy: (value: string | boolean | number | undefined) =>
33-
typeof value === 'string' ? value === 'true' || value === '1' : Boolean(value),
34-
getEnv: (variable: string) => process.env[variable],
35-
}))
29+
vi.mock('@/lib/core/config/env', () => createEnvMock({ BETTER_AUTH_SECRET: 'test-secret-key' }))
3630

3731
vi.mock('@sim/logger', () => ({
3832
createLogger: () => ({

apps/sim/lib/messaging/email/utils.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createEnvMock } from '@sim/testing'
12
import { describe, expect, it, vi } from 'vitest'
23

34
/**
@@ -8,12 +9,12 @@ import { describe, expect, it, vi } from 'vitest'
89
*/
910

1011
// Set up mocks at module level - these will be used for all tests in this file
11-
vi.mock('@/lib/core/config/env', () => ({
12-
env: {
12+
vi.mock('@/lib/core/config/env', () =>
13+
createEnvMock({
1314
FROM_EMAIL_ADDRESS: 'Sim <[email protected]>',
1415
EMAIL_DOMAIN: 'example.com',
15-
},
16-
}))
16+
})
17+
)
1718

1819
vi.mock('@/lib/core/utils/urls', () => ({
1920
getEmailDomain: vi.fn().mockReturnValue('fallback.com'),

apps/sim/lib/oauth/oauth.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { createMockFetch, loggerMock } from '@sim/testing'
1+
import { createEnvMock, createMockFetch, loggerMock } from '@sim/testing'
22
import { describe, expect, it, vi } from 'vitest'
33

4-
vi.mock('@/lib/core/config/env', () => ({
5-
env: {
4+
vi.mock('@/lib/core/config/env', () =>
5+
createEnvMock({
66
GOOGLE_CLIENT_ID: 'google_client_id',
77
GOOGLE_CLIENT_SECRET: 'google_client_secret',
88
GITHUB_CLIENT_ID: 'github_client_id',
@@ -49,8 +49,8 @@ vi.mock('@/lib/core/config/env', () => ({
4949
WORDPRESS_CLIENT_SECRET: 'wordpress_client_secret',
5050
SPOTIFY_CLIENT_ID: 'spotify_client_id',
5151
SPOTIFY_CLIENT_SECRET: 'spotify_client_secret',
52-
},
53-
}))
52+
})
53+
)
5454

5555
vi.mock('@sim/logger', () => loggerMock)
5656

0 commit comments

Comments
 (0)