11import { vi } from 'vitest'
22import { NextRequest } from 'next/server'
33
4- /**
5- * Mock sample workflow state for testing
6- */
74export const sampleWorkflowState = {
85 blocks : {
96 'starter-id' : {
@@ -65,51 +62,108 @@ export const sampleWorkflowState = {
6562 isDeployed : false ,
6663}
6764
68- /**
69- * Mock database with test data
70- */
71- export function mockDb ( ) {
72- return {
73- select : vi . fn ( ) . mockImplementation ( ( ) => ( {
74- from : vi . fn ( ) . mockImplementation ( ( ) => ( {
75- where : vi . fn ( ) . mockImplementation ( ( ) => ( {
76- limit : vi . fn ( ) . mockImplementation ( ( ) => [
77- {
78- id : 'workflow-id' ,
79- userId : 'user-id' ,
80- state : sampleWorkflowState ,
81- } ,
82- ] ) ,
83- } ) ) ,
65+ export const mockDb = {
66+ select : vi . fn ( ) . mockImplementation ( ( ) => ( {
67+ from : vi . fn ( ) . mockImplementation ( ( ) => ( {
68+ where : vi . fn ( ) . mockImplementation ( ( ) => ( {
69+ limit : vi . fn ( ) . mockImplementation ( ( ) => [
70+ {
71+ id : 'workflow-id' ,
72+ userId : 'user-id' ,
73+ state : sampleWorkflowState ,
74+ } ,
75+ ] ) ,
8476 } ) ) ,
8577 } ) ) ,
86- update : vi . fn ( ) . mockImplementation ( ( ) => ( {
87- set : vi . fn ( ) . mockImplementation ( ( ) => ( {
88- where : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
89- } ) ) ,
78+ } ) ) ,
79+ update : vi . fn ( ) . mockImplementation ( ( ) => ( {
80+ set : vi . fn ( ) . mockImplementation ( ( ) => ( {
81+ where : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
9082 } ) ) ,
91- }
83+ } ) ) ,
84+ eq : vi . fn ( ) . mockImplementation ( ( field , value ) => ( { field, value, type : 'eq' } ) ) ,
85+ and : vi . fn ( ) . mockImplementation ( ( ...conditions ) => ( {
86+ conditions,
87+ type : 'and' ,
88+ } ) ) ,
89+ }
90+
91+ export const mockLogger = {
92+ info : vi . fn ( ) ,
93+ warn : vi . fn ( ) ,
94+ error : vi . fn ( ) ,
95+ debug : vi . fn ( ) ,
96+ }
97+
98+ export const mockUser = {
99+ id : 'user-123' ,
100+ email : 'test@example.com' ,
101+ }
102+
103+ export const mockSubscription = {
104+ id : 'sub-123' ,
105+ plan : 'enterprise' ,
106+ status : 'active' ,
107+ seats : 5 ,
108+ referenceId : 'user-123' ,
109+ metadata : {
110+ perSeatAllowance : 100 ,
111+ totalAllowance : 500 ,
112+ updatedAt : '2023-01-01T00:00:00.000Z' ,
113+ } ,
114+ }
115+
116+ export const mockOrganization = {
117+ id : 'org-456' ,
118+ name : 'Test Organization' ,
119+ slug : 'test-org' ,
120+ }
121+
122+ export const mockAdminMember = {
123+ id : 'member-123' ,
124+ userId : 'user-123' ,
125+ organizationId : 'org-456' ,
126+ role : 'admin' ,
127+ }
128+
129+ export const mockRegularMember = {
130+ id : 'member-456' ,
131+ userId : 'user-123' ,
132+ organizationId : 'org-456' ,
133+ role : 'member' ,
134+ }
135+
136+ export const mockTeamSubscription = {
137+ id : 'sub-456' ,
138+ plan : 'team' ,
139+ status : 'active' ,
140+ seats : 5 ,
141+ referenceId : 'org-123' ,
142+ }
143+
144+ export const mockPersonalSubscription = {
145+ id : 'sub-789' ,
146+ plan : 'enterprise' ,
147+ status : 'active' ,
148+ seats : 5 ,
149+ referenceId : 'user-123' ,
150+ metadata : {
151+ perSeatAllowance : 100 ,
152+ totalAllowance : 500 ,
153+ updatedAt : '2023-01-01T00:00:00.000Z' ,
154+ } ,
92155}
93156
94- /**
95- * Mock environment variables for testing
96- */
97157export const mockEnvironmentVars = {
98158 OPENAI_API_KEY : 'encrypted:openai-api-key' ,
99159 SERPER_API_KEY : 'encrypted:serper-api-key' ,
100160}
101161
102- /**
103- * Mock decrypted environment variables for testing
104- */
105162export const mockDecryptedEnvVars = {
106163 OPENAI_API_KEY : 'sk-test123' ,
107164 SERPER_API_KEY : 'serper-test123' ,
108165}
109166
110- /**
111- * Create mock Next.js request for testing
112- */
113167export function createMockRequest (
114168 method : string = 'GET' ,
115169 body ?: any ,
@@ -125,11 +179,7 @@ export function createMockRequest(
125179 } )
126180}
127181
128- /**
129- * Mock the executeWorkflow function dependencies
130- */
131182export function mockExecutionDependencies ( ) {
132- // Mock decryptSecret function
133183 vi . mock ( '@/lib/utils' , async ( ) => {
134184 const actual = await vi . importActual ( '@/lib/utils' )
135185 return {
@@ -150,26 +200,22 @@ export function mockExecutionDependencies() {
150200 }
151201 } )
152202
153- // Mock execution logger functions
154203 vi . mock ( '@/lib/logs/execution-logger' , ( ) => ( {
155204 persistExecutionLogs : vi . fn ( ) . mockResolvedValue ( undefined ) ,
156205 persistExecutionError : vi . fn ( ) . mockResolvedValue ( undefined ) ,
157206 } ) )
158207
159- // Mock trace spans builder
160208 vi . mock ( '@/lib/logs/trace-spans' , ( ) => ( {
161209 buildTraceSpans : vi . fn ( ) . mockReturnValue ( {
162210 traceSpans : [ ] ,
163211 totalDuration : 100 ,
164212 } ) ,
165213 } ) )
166214
167- // Mock workflow utils
168215 vi . mock ( '@/lib/workflows/utils' , ( ) => ( {
169216 updateWorkflowRunCounts : vi . fn ( ) . mockResolvedValue ( undefined ) ,
170217 } ) )
171218
172- // Mock serializer
173219 vi . mock ( '@/serializer' , ( ) => ( {
174220 Serializer : vi . fn ( ) . mockImplementation ( ( ) => ( {
175221 serializeWorkflow : vi . fn ( ) . mockReturnValue ( {
@@ -205,7 +251,6 @@ export function mockExecutionDependencies() {
205251 } ) ) ,
206252 } ) )
207253
208- // Mock executor
209254 vi . mock ( '@/executor' , ( ) => ( {
210255 Executor : vi . fn ( ) . mockImplementation ( ( ) => ( {
211256 execute : vi . fn ( ) . mockResolvedValue ( {
@@ -226,15 +271,11 @@ export function mockExecutionDependencies() {
226271 } ) ) ,
227272 } ) )
228273
229- // Mock database
230274 vi . mock ( '@/db' , ( ) => ( {
231- db : mockDb ( ) ,
275+ db : mockDb ,
232276 } ) )
233277}
234278
235- /**
236- * Mock the workflow access validation middleware
237- */
238279export function mockWorkflowAccessValidation ( shouldSucceed = true ) {
239280 if ( shouldSucceed ) {
240281 vi . mock ( '@/app/api/workflows/middleware' , ( ) => ( {
@@ -258,11 +299,7 @@ export function mockWorkflowAccessValidation(shouldSucceed = true) {
258299 }
259300}
260301
261- /**
262- * Get mocked dependencies for validation
263- */
264302export async function getMockedDependencies ( ) {
265- // Using dynamic imports to avoid module resolution issues
266303 const utilsModule = await import ( '@/lib/utils' )
267304 const logsModule = await import ( '@/lib/logs/execution-logger' )
268305 const traceSpansModule = await import ( '@/lib/logs/trace-spans' )
0 commit comments