-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathroute.test.ts
More file actions
114 lines (100 loc) · 3.46 KB
/
Copy pathroute.test.ts
File metadata and controls
114 lines (100 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @vitest-environment node
*/
import { createMockRequest } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const {
mockFlags,
mockDbLimit,
mockCheckInternalApiKey,
mockCheckServerSideUsageLimits,
mockCheckOrgMemberUsageLimit,
} = vi.hoisted(() => ({
mockFlags: { isHosted: true },
mockDbLimit: vi.fn(),
mockCheckInternalApiKey: vi.fn(),
mockCheckServerSideUsageLimits: vi.fn(),
mockCheckOrgMemberUsageLimit: vi.fn(),
}))
vi.mock('@sim/db', () => ({
db: {
select: () => ({ from: () => ({ where: () => ({ limit: mockDbLimit }) }) }),
},
}))
vi.mock('@/lib/billing/calculations/usage-monitor', () => ({
checkServerSideUsageLimits: mockCheckServerSideUsageLimits,
checkOrgMemberUsageLimit: mockCheckOrgMemberUsageLimit,
}))
vi.mock('@/lib/copilot/request/http', () => ({
checkInternalApiKey: mockCheckInternalApiKey,
}))
vi.mock('@/lib/copilot/request/otel', () => ({
withIncomingGoSpan: (
_headers: unknown,
_span: unknown,
_attrs: unknown,
fn: (span: { setAttribute: () => void; setAttributes: () => void }) => unknown
) => fn({ setAttribute: vi.fn(), setAttributes: vi.fn() }),
}))
vi.mock('@/lib/core/config/env-flags', () => ({
get isHosted() {
return mockFlags.isHosted
},
}))
import { POST } from '@/app/api/copilot/api-keys/validate/route'
function request(body: Record<string, unknown>) {
return createMockRequest('POST', body, { 'x-api-key': 'internal' })
}
describe('POST /api/copilot/api-keys/validate — per-member enforcement', () => {
beforeEach(() => {
vi.clearAllMocks()
mockFlags.isHosted = true
mockCheckInternalApiKey.mockReturnValue({ success: true })
mockDbLimit.mockResolvedValue([{ id: 'user-1' }])
mockCheckServerSideUsageLimits.mockResolvedValue({
isExceeded: false,
currentUsage: 0,
limit: 100,
})
mockCheckOrgMemberUsageLimit.mockResolvedValue({
isExceeded: false,
currentUsage: 0,
limit: null,
})
})
it('returns 402 when the pooled/personal limit is exceeded (existing behavior)', async () => {
mockCheckServerSideUsageLimits.mockResolvedValue({
isExceeded: true,
currentUsage: 200,
limit: 100,
})
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
expect(res.status).toBe(402)
expect(mockCheckOrgMemberUsageLimit).not.toHaveBeenCalled()
})
it('returns 402 when the per-member org-workspace cap is exceeded', async () => {
mockCheckOrgMemberUsageLimit.mockResolvedValue({
isExceeded: true,
currentUsage: 5,
limit: 4,
})
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
expect(res.status).toBe(402)
expect(mockCheckOrgMemberUsageLimit).toHaveBeenCalledWith('user-1', 'ws-1')
})
it('returns 200 when under both limits', async () => {
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
expect(res.status).toBe(200)
})
it('rejects with 400 when workspaceId is omitted (contract-required, fail closed)', async () => {
const res = await POST(request({ userId: 'user-1' }))
expect(res.status).toBe(400)
expect(mockCheckOrgMemberUsageLimit).not.toHaveBeenCalled()
})
it('skips the per-member check when not hosted', async () => {
mockFlags.isHosted = false
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
expect(res.status).toBe(200)
expect(mockCheckOrgMemberUsageLimit).not.toHaveBeenCalled()
})
})