11import { describe , it , expect , beforeEach , vi , afterEach } from 'vitest'
2- import { getSecret } from 'astro:env/server'
2+
3+ // Mock environment utilities BEFORE importing the module under test
4+ vi . mock ( '@components/scripts/utils' , ( ) => ( {
5+ isDev : vi . fn ( ( ) => false ) ,
6+ isTest : vi . fn ( ( ) => false ) ,
7+ } ) )
8+
39import { rateLimiters , checkRateLimit , checkContactRateLimit } from '@pages/api/_utils/rateLimit'
410
511// Mock the Upstash Redis and Ratelimit modules
@@ -246,57 +252,40 @@ describe('Rate Limit Utils', () => {
246252 } )
247253
248254 describe ( 'checkContactRateLimit' , ( ) => {
249- let originalEnvMode : string | undefined
250- let originalDev : boolean | undefined
251- let originalCI : string | undefined
252-
253- beforeEach ( ( ) => {
254- originalEnvMode = import . meta. env . MODE
255- originalDev = import . meta. env . DEV
256- originalCI = getSecret ( 'CI' )
255+ let isDev : ReturnType < typeof vi . fn >
256+ let isTest : ReturnType < typeof vi . fn >
257+
258+ beforeEach ( async ( ) => {
259+ // Get the mocked functions
260+ const utils = await import ( '@components/scripts/utils' )
261+ isDev = utils . isDev as ReturnType < typeof vi . fn >
262+ isTest = utils . isTest as ReturnType < typeof vi . fn >
263+
264+ // Reset to production mode by default
265+ isDev . mockReturnValue ( false )
266+ isTest . mockReturnValue ( false )
257267 } )
258268
259269 afterEach ( ( ) => {
260- if ( originalEnvMode !== undefined ) {
261- import . meta. env . MODE = originalEnvMode
262- }
263- if ( originalDev !== undefined ) {
264- import . meta. env . DEV = originalDev
265- }
266- if ( originalCI !== undefined ) {
267- /* eslint-disable-next-line no-process-env */
268- process . env [ 'CI' ] = originalCI
269- }
270- } )
271-
272- it ( 'should return true in test environment' , ( ) => {
273- import . meta. env . MODE = 'test'
274-
275- const result = checkContactRateLimit ( '192.168.1.1' )
276- expect ( result ) . toBe ( true )
270+ vi . clearAllMocks ( )
277271 } )
278272
279- it ( 'should return true in development environment ' , ( ) => {
280- import . meta . env . DEV = true
273+ it ( 'should return true when isTest() returns true ' , ( ) => {
274+ isTest . mockReturnValue ( true )
281275
282276 const result = checkContactRateLimit ( '192.168.1.1' )
283277 expect ( result ) . toBe ( true )
284278 } )
285279
286- it ( 'should return true in CI environment' , ( ) => {
287- /* eslint-disable-next-line no-process-env */
288- process . env [ 'CI' ] = 'true'
280+ it ( 'should return true when isDev() returns true' , ( ) => {
281+ isDev . mockReturnValue ( true )
289282
290283 const result = checkContactRateLimit ( '192.168.1.1' )
291284 expect ( result ) . toBe ( true )
292285 } )
293286
294287 it ( 'should allow requests under the limit in production' , ( ) => {
295- // Set production-like environment
296- import . meta. env . MODE = 'production'
297- import . meta. env . DEV = false
298- /* eslint-disable-next-line no-process-env */
299- process . env [ 'CI' ] = 'false'
288+ // isDev() and isTest() return false (set in beforeEach)
300289
301290 const ip = '192.168.1.2'
302291
@@ -308,11 +297,7 @@ describe('Rate Limit Utils', () => {
308297 } )
309298
310299 it ( 'should block requests over the limit in production' , ( ) => {
311- // Set production-like environment
312- import . meta. env . MODE = 'production'
313- import . meta. env . DEV = false
314- /* eslint-disable-next-line no-process-env */
315- process . env [ 'CI' ] = 'false'
300+ // isDev() and isTest() return false (set in beforeEach)
316301
317302 const ip = '192.168.1.3'
318303
@@ -327,11 +312,7 @@ describe('Rate Limit Utils', () => {
327312 } )
328313
329314 it ( 'should isolate rate limits by IP address' , ( ) => {
330- // Set production-like environment
331- import . meta. env . MODE = 'production'
332- import . meta. env . DEV = false
333- /* eslint-disable-next-line no-process-env */
334- process . env [ 'CI' ] = 'false'
315+ // isDev() and isTest() return false (set in beforeEach)
335316
336317 const ip1 = '192.168.1.4'
337318 const ip2 = '192.168.1.5'
0 commit comments