-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathsetup.ts
More file actions
282 lines (250 loc) · 6.23 KB
/
Copy pathsetup.ts
File metadata and controls
282 lines (250 loc) · 6.23 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Test setup utilities for common patterns.
*
* Provides helper functions for setting up and tearing down test fixtures
* in a consistent way across the codebase.
*
* @example
* ```typescript
* import { createTestSetup, TestSetupResult } from '@codebuff/common/testing/setup'
*
* describe('my test', () => {
* const setup = createTestSetup()
*
* beforeEach(() => setup.beforeEach())
* afterEach(() => setup.afterEach())
* })
* ```
*/
import { setupAnalyticsMocks } from './mocks/analytics'
import { setupCryptoMocks } from './mocks/crypto'
import { setupDbSpies } from './mocks/database'
import { createMockLogger } from './mocks/logger'
import { resetToolCallIdCounter } from './mocks/stream'
import type {
AnalyticsSpies,
TrackEventFn,
FlushAnalyticsFn,
} from './mocks/analytics'
import type { CryptoMockSpies } from './mocks/crypto'
import type { DbSpies } from './mocks/database'
import type { MockLogger } from './mocks/logger'
/**
* Options for creating a test setup.
*/
export interface CreateTestSetupOptions {
/**
* Whether to set up analytics mocks.
* @default true
*/
analytics?: boolean
/**
* Whether to set up crypto mocks.
* @default true
*/
crypto?: boolean
/**
* Whether to set up database mocks.
* Requires passing the db module.
* @default false
*/
database?: boolean
/**
* The database module to mock (required if database is true).
* Must have insert and update methods that are functions.
*/
dbModule?: {
insert: (...args: unknown[]) => unknown
update: (...args: unknown[]) => unknown
}
/**
* The analytics module to mock (required if analytics is true).
*/
analyticsModule?: {
trackEvent: TrackEventFn
flushAnalytics: FlushAnalyticsFn
}
/**
* Prefix for crypto mock UUIDs.
* @default 'test'
*/
cryptoPrefix?: string
}
/**
* Result of creating a test setup.
*/
export interface TestSetupResult {
/** The mock logger instance */
logger: MockLogger
/** Analytics spies (if enabled) */
analyticsSpy?: AnalyticsSpies
/** Crypto spies (if enabled) */
cryptoSpy?: CryptoMockSpies
/** Database spies (if enabled) */
dbSpy?: DbSpies
/** Call this in beforeEach */
beforeEach: () => void
/** Call this in afterEach */
afterEach: () => void
/** Restore all mocks */
restore: () => void
}
/**
* Creates a test setup with common mocks pre-configured.
*
* @param options - Configuration options
* @returns A test setup result with mocks and lifecycle methods
*
* @example
* ```typescript
* import * as analytics from '@codebuff/common/analytics'
* const db = createMockDbOperations()
*
* describe('my test', () => {
* const setup = createTestSetup({
* analytics: true,
* analyticsModule: analytics,
* database: true,
* dbModule: db,
* })
*
* beforeEach(() => setup.beforeEach())
* afterEach(() => setup.afterEach())
*
* it('does something', () => {
* expect(setup.analyticsSpy.trackEvent).toHaveBeenCalled()
* })
* })
* ```
*/
export function createTestSetup(
options: CreateTestSetupOptions = {},
): TestSetupResult {
const {
analytics = true,
crypto = true,
database = false,
dbModule,
analyticsModule,
cryptoPrefix = 'test',
} = options
const logger = createMockLogger()
let analyticsSpy: AnalyticsSpies | undefined
let cryptoSpy: CryptoMockSpies | undefined
let dbSpy: DbSpies | undefined
const beforeEach = (): void => {
// Reset tool call ID counter for deterministic tests
resetToolCallIdCounter()
// Set up analytics mocks
if (analytics && analyticsModule) {
analyticsSpy = setupAnalyticsMocks(analyticsModule)
}
// Set up crypto mocks
if (crypto) {
cryptoSpy = setupCryptoMocks({ prefix: cryptoPrefix, sequential: true })
}
// Set up database mocks
if (database && dbModule) {
dbSpy = setupDbSpies(dbModule)
}
}
const afterEach = (): void => {
// Restore all mocks
analyticsSpy?.restore()
cryptoSpy?.restore()
dbSpy?.restore()
// Reset the spies
analyticsSpy = undefined
cryptoSpy = undefined
dbSpy = undefined
}
const restore = afterEach
return {
logger,
get analyticsSpy() {
return analyticsSpy
},
get cryptoSpy() {
return cryptoSpy
},
get dbSpy() {
return dbSpy
},
beforeEach,
afterEach,
restore,
}
}
/**
* A simple sleep function for async tests.
*
* @param ms - Milliseconds to sleep
* @returns A promise that resolves after the specified time
*
* @example
* ```typescript
* await sleep(100) // Wait 100ms
* ```
*/
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
/**
* Waits for a condition to be true, polling at the specified interval.
*
* @param condition - Function that returns true when the condition is met
* @param timeout - Maximum time to wait in ms
* @param interval - Polling interval in ms
* @returns A promise that resolves when the condition is met
* @throws Error if the timeout is reached
*
* @example
* ```typescript
* await waitFor(() => document.querySelector('.loaded') !== null)
* ```
*/
export async function waitFor(
condition: () => boolean | Promise<boolean>,
timeout: number = 5000,
interval: number = 50,
): Promise<void> {
const start = Date.now()
while (Date.now() - start < timeout) {
const result = await condition()
if (result) {
return
}
await sleep(interval)
}
throw new Error(`waitFor timed out after ${timeout}ms`)
}
/**
* Wraps a function to capture its call arguments.
* Useful for verifying function calls in tests.
*
* @param fn - The function to wrap
* @returns An object with the wrapped function and captured calls
*
* @example
* ```typescript
* const { fn, calls } = captureCallArgs((a: number, b: string) => a + b.length)
*
* fn(1, 'hello')
* fn(2, 'world')
*
* expect(calls).toEqual([
* [1, 'hello'],
* [2, 'world'],
* ])
* ```
*/
export function captureCallArgs<T extends unknown[], R>(
fn: (...args: T) => R,
): { fn: (...args: T) => R; calls: T[] } {
const calls: T[] = []
const wrappedFn = (...args: T): R => {
calls.push(args)
return fn(...args)
}
return { fn: wrappedFn, calls }
}