|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + activate: vi.fn(), |
| 9 | + parseRequest: vi.fn(), |
| 10 | + read: vi.fn(), |
| 11 | + session: vi.fn(), |
| 12 | + update: vi.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@/lib/api/server', () => ({ |
| 16 | + getValidationErrorMessage: vi.fn(), |
| 17 | + parseRequest: mocks.parseRequest, |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/api/server/routes', () => ({ |
| 21 | + InternalUnauthenticatedError: class InternalUnauthenticatedError extends Error {}, |
| 22 | + internalSessionAuth: { authenticate: mocks.session }, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/core/utils/with-route-handler', () => ({ |
| 26 | + withRouteHandler: (handler: unknown) => handler, |
| 27 | +})) |
| 28 | + |
| 29 | +vi.mock('@/lib/workflows/application/deployments', () => ({ |
| 30 | + activateWorkflowVersion: { execute: mocks.activate }, |
| 31 | + updateWorkflowVersion: { execute: mocks.update }, |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/lib/workflows/application/read-workflow-version', () => ({ |
| 35 | + readWorkflowVersion: { execute: mocks.read }, |
| 36 | +})) |
| 37 | + |
| 38 | +import { PATCH } from '@/app/api/workflows/[id]/deployments/[version]/route' |
| 39 | + |
| 40 | +describe('workflow deployment version PATCH', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks() |
| 43 | + mocks.session.mockResolvedValue({ kind: 'session', userId: 'user-1', sessionId: 'session-1' }) |
| 44 | + mocks.activate.mockResolvedValue({ |
| 45 | + deployedAt: new Date('2026-01-01T00:00:00Z'), |
| 46 | + warnings: undefined, |
| 47 | + activeDeployment: null, |
| 48 | + latestDeploymentAttempt: null, |
| 49 | + name: 'Release 2', |
| 50 | + description: 'Production', |
| 51 | + }) |
| 52 | + mocks.update.mockResolvedValue({ name: 'Release 2', description: 'Production' }) |
| 53 | + }) |
| 54 | + |
| 55 | + it('sends activation and optional metadata through one application command', async () => { |
| 56 | + mocks.parseRequest.mockResolvedValue({ |
| 57 | + success: true, |
| 58 | + data: { |
| 59 | + params: { id: 'workflow-1', version: 2 }, |
| 60 | + body: { isActive: true, name: 'Release 2', description: 'Production' }, |
| 61 | + }, |
| 62 | + }) |
| 63 | + |
| 64 | + const response = await PATCH( |
| 65 | + createMockRequest( |
| 66 | + 'PATCH', |
| 67 | + undefined, |
| 68 | + {}, |
| 69 | + 'http://localhost/api/workflows/workflow-1/deployments/2' |
| 70 | + ), |
| 71 | + { params: Promise.resolve({ id: 'workflow-1', version: '2' }) } |
| 72 | + ) |
| 73 | + |
| 74 | + expect(response.status).toBe(200) |
| 75 | + expect(await response.json()).toMatchObject({ |
| 76 | + success: true, |
| 77 | + name: 'Release 2', |
| 78 | + description: 'Production', |
| 79 | + }) |
| 80 | + expect(mocks.activate).toHaveBeenCalledWith( |
| 81 | + expect.objectContaining({ |
| 82 | + input: expect.objectContaining({ |
| 83 | + workflowId: 'workflow-1', |
| 84 | + version: 2, |
| 85 | + name: 'Release 2', |
| 86 | + description: 'Production', |
| 87 | + }), |
| 88 | + }) |
| 89 | + ) |
| 90 | + expect(mocks.update).not.toHaveBeenCalled() |
| 91 | + }) |
| 92 | + |
| 93 | + it('keeps metadata-only edits on the existing update-version operation', async () => { |
| 94 | + mocks.parseRequest.mockResolvedValue({ |
| 95 | + success: true, |
| 96 | + data: { |
| 97 | + params: { id: 'workflow-1', version: 2 }, |
| 98 | + body: { isActive: false, name: 'Release 2' }, |
| 99 | + }, |
| 100 | + }) |
| 101 | + |
| 102 | + const response = await PATCH( |
| 103 | + createMockRequest( |
| 104 | + 'PATCH', |
| 105 | + undefined, |
| 106 | + {}, |
| 107 | + 'http://localhost/api/workflows/workflow-1/deployments/2' |
| 108 | + ), |
| 109 | + { params: Promise.resolve({ id: 'workflow-1', version: '2' }) } |
| 110 | + ) |
| 111 | + |
| 112 | + expect(response.status).toBe(200) |
| 113 | + expect(mocks.update).toHaveBeenCalledOnce() |
| 114 | + expect(mocks.activate).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | +}) |
0 commit comments