|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +vi.mock('@sim/db', () => dbChainMock) |
| 8 | + |
| 9 | +const { mockAuthorizeWorkflow, mockGetSession, mockIsFeatureEnabled, mockLoadSuites } = vi.hoisted( |
| 10 | + () => ({ |
| 11 | + mockAuthorizeWorkflow: vi.fn(), |
| 12 | + mockGetSession: vi.fn(), |
| 13 | + mockIsFeatureEnabled: vi.fn(), |
| 14 | + mockLoadSuites: vi.fn(), |
| 15 | + }) |
| 16 | +) |
| 17 | + |
| 18 | +vi.mock('@/lib/auth', () => ({ getSession: mockGetSession })) |
| 19 | +vi.mock('@sim/platform-authz/workflow', () => ({ |
| 20 | + authorizeWorkflowByWorkspacePermission: mockAuthorizeWorkflow, |
| 21 | +})) |
| 22 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 23 | + isFeatureEnabled: mockIsFeatureEnabled, |
| 24 | +})) |
| 25 | +vi.mock('@/lib/workflows/evals/loader', () => ({ |
| 26 | + loadWorkflowEvalSuites: mockLoadSuites, |
| 27 | +})) |
| 28 | + |
| 29 | +import { GET } from '@/app/api/workflows/[id]/evals/route' |
| 30 | + |
| 31 | +function callRoute(id = 'workflow-1') { |
| 32 | + return GET(createMockRequest('GET'), { params: Promise.resolve({ id }) }) |
| 33 | +} |
| 34 | + |
| 35 | +describe('GET /api/workflows/[id]/evals', () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks() |
| 38 | + resetDbChainMock() |
| 39 | + mockGetSession.mockResolvedValue({ user: { id: 'user-1' } }) |
| 40 | + mockAuthorizeWorkflow.mockResolvedValue({ |
| 41 | + allowed: true, |
| 42 | + status: 200, |
| 43 | + workflow: { id: 'workflow-1', workspaceId: 'workspace-1' }, |
| 44 | + }) |
| 45 | + dbChainMockFns.limit.mockResolvedValue([{ organizationId: 'organization-1' }]) |
| 46 | + mockIsFeatureEnabled.mockResolvedValue(true) |
| 47 | + mockLoadSuites.mockResolvedValue([]) |
| 48 | + }) |
| 49 | + |
| 50 | + it('returns 401 before parsing or listing without a session', async () => { |
| 51 | + mockGetSession.mockResolvedValue(null) |
| 52 | + |
| 53 | + const response = await callRoute() |
| 54 | + |
| 55 | + expect(response.status).toBe(401) |
| 56 | + expect(mockAuthorizeWorkflow).not.toHaveBeenCalled() |
| 57 | + expect(mockLoadSuites).not.toHaveBeenCalled() |
| 58 | + }) |
| 59 | + |
| 60 | + it('enforces workflow read authorization', async () => { |
| 61 | + mockAuthorizeWorkflow.mockResolvedValue({ |
| 62 | + allowed: false, |
| 63 | + status: 403, |
| 64 | + message: 'Access denied', |
| 65 | + workflow: { id: 'workflow-1', workspaceId: 'workspace-1' }, |
| 66 | + }) |
| 67 | + |
| 68 | + const response = await callRoute() |
| 69 | + |
| 70 | + expect(response.status).toBe(403) |
| 71 | + expect(mockAuthorizeWorkflow).toHaveBeenCalledWith({ |
| 72 | + workflowId: 'workflow-1', |
| 73 | + userId: 'user-1', |
| 74 | + action: 'read', |
| 75 | + }) |
| 76 | + expect(mockIsFeatureEnabled).not.toHaveBeenCalled() |
| 77 | + expect(mockLoadSuites).not.toHaveBeenCalled() |
| 78 | + }) |
| 79 | + |
| 80 | + it('returns the disabled availability response without listing suites', async () => { |
| 81 | + mockIsFeatureEnabled.mockResolvedValue(false) |
| 82 | + |
| 83 | + const response = await callRoute() |
| 84 | + |
| 85 | + expect(response.status).toBe(200) |
| 86 | + expect(await response.json()).toEqual({ enabled: false, suites: [] }) |
| 87 | + expect(mockIsFeatureEnabled).toHaveBeenCalledWith('workflow-evals', { |
| 88 | + userId: 'user-1', |
| 89 | + orgId: 'organization-1', |
| 90 | + }) |
| 91 | + expect(mockLoadSuites).not.toHaveBeenCalled() |
| 92 | + }) |
| 93 | + |
| 94 | + it('returns enabled suites from the authorized workflow workspace', async () => { |
| 95 | + mockLoadSuites.mockResolvedValue([ |
| 96 | + { |
| 97 | + id: 'suite-1', |
| 98 | + name: 'Regression', |
| 99 | + definitionRevision: 1, |
| 100 | + archivedAt: null, |
| 101 | + tests: [ |
| 102 | + { |
| 103 | + id: 'test-1', |
| 104 | + name: 'Answers routine questions', |
| 105 | + evaluatorType: 'code', |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 'test-2', |
| 109 | + name: 'Escalates refunds', |
| 110 | + evaluatorType: 'code', |
| 111 | + }, |
| 112 | + ], |
| 113 | + testCount: 2, |
| 114 | + latestRun: { |
| 115 | + id: 'run-1', |
| 116 | + scope: 'suite', |
| 117 | + selectedTestId: null, |
| 118 | + suiteDefinitionRevision: 1, |
| 119 | + status: 'completed', |
| 120 | + revision: 8, |
| 121 | + completedCount: 2, |
| 122 | + passedCount: 1, |
| 123 | + warningCount: 0, |
| 124 | + failedCount: 1, |
| 125 | + errorCount: 0, |
| 126 | + totalCount: 2, |
| 127 | + createdAt: new Date('2026-07-15T12:00:00.000Z'), |
| 128 | + updatedAt: new Date('2026-07-15T12:01:00.000Z'), |
| 129 | + startedAt: new Date('2026-07-15T12:00:00.000Z'), |
| 130 | + completedAt: new Date('2026-07-15T12:01:00.000Z'), |
| 131 | + error: null, |
| 132 | + tests: [ |
| 133 | + { |
| 134 | + id: 'test-1', |
| 135 | + name: 'Answers routine questions', |
| 136 | + evaluatorType: 'code', |
| 137 | + }, |
| 138 | + { |
| 139 | + id: 'test-2', |
| 140 | + name: 'Escalates refunds', |
| 141 | + evaluatorType: 'code', |
| 142 | + }, |
| 143 | + ], |
| 144 | + testRuns: [ |
| 145 | + { |
| 146 | + id: 'test-run-1', |
| 147 | + testId: 'test-1', |
| 148 | + ordinal: 0, |
| 149 | + name: 'Answers routine questions', |
| 150 | + evaluatorType: 'code', |
| 151 | + phase: 'completed', |
| 152 | + outcome: 'pass', |
| 153 | + score: 10, |
| 154 | + subjectExecutionId: 'execution-1', |
| 155 | + judgeExecutionId: null, |
| 156 | + error: null, |
| 157 | + criteria: [], |
| 158 | + }, |
| 159 | + { |
| 160 | + id: 'test-run-2', |
| 161 | + testId: 'test-2', |
| 162 | + ordinal: 1, |
| 163 | + name: 'Escalates refunds', |
| 164 | + evaluatorType: 'code', |
| 165 | + phase: 'completed', |
| 166 | + outcome: 'fail', |
| 167 | + score: 0, |
| 168 | + subjectExecutionId: 'execution-2', |
| 169 | + judgeExecutionId: null, |
| 170 | + error: null, |
| 171 | + criteria: [], |
| 172 | + }, |
| 173 | + ], |
| 174 | + }, |
| 175 | + latestSuiteRun: null, |
| 176 | + }, |
| 177 | + ]) |
| 178 | + |
| 179 | + const response = await callRoute() |
| 180 | + const body = await response.json() |
| 181 | + |
| 182 | + expect(response.status).toBe(200) |
| 183 | + expect(body.enabled).toBe(true) |
| 184 | + expect(body.suites[0].tests).toEqual([ |
| 185 | + { |
| 186 | + id: 'test-1', |
| 187 | + name: 'Answers routine questions', |
| 188 | + evaluatorType: 'code', |
| 189 | + }, |
| 190 | + { |
| 191 | + id: 'test-2', |
| 192 | + name: 'Escalates refunds', |
| 193 | + evaluatorType: 'code', |
| 194 | + }, |
| 195 | + ]) |
| 196 | + expect(body.suites[0].latestRun.tests).toEqual(body.suites[0].tests) |
| 197 | + expect(body.suites[0].latestRun.testRuns).toEqual([ |
| 198 | + { |
| 199 | + id: 'test-run-1', |
| 200 | + testId: 'test-1', |
| 201 | + ordinal: 0, |
| 202 | + name: 'Answers routine questions', |
| 203 | + evaluatorType: 'code', |
| 204 | + phase: 'completed', |
| 205 | + outcome: 'pass', |
| 206 | + score: 10, |
| 207 | + reason: null, |
| 208 | + errorBlockIds: [], |
| 209 | + subjectExecutionId: 'execution-1', |
| 210 | + judgeExecutionId: null, |
| 211 | + error: null, |
| 212 | + criteria: [], |
| 213 | + }, |
| 214 | + { |
| 215 | + id: 'test-run-2', |
| 216 | + testId: 'test-2', |
| 217 | + ordinal: 1, |
| 218 | + name: 'Escalates refunds', |
| 219 | + evaluatorType: 'code', |
| 220 | + phase: 'completed', |
| 221 | + outcome: 'fail', |
| 222 | + score: 0, |
| 223 | + reason: null, |
| 224 | + errorBlockIds: [], |
| 225 | + subjectExecutionId: 'execution-2', |
| 226 | + judgeExecutionId: null, |
| 227 | + error: null, |
| 228 | + criteria: [], |
| 229 | + }, |
| 230 | + ]) |
| 231 | + expect(mockLoadSuites).toHaveBeenCalledWith('workflow-1', 'workspace-1') |
| 232 | + }) |
| 233 | +}) |
0 commit comments