11import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
2- import { validProjectResponse , validServiceConfig } from "../../mocks.js" ;
3- import { updateRateLimitedAt } from "../api.js" ;
2+ import { validServiceConfig , validTeamResponse } from "../../mocks.js" ;
43import { rateLimit } from "./index.js" ;
54
65const mockRedis = {
76 incr : vi . fn ( ) ,
87 expire : vi . fn ( ) ,
98} ;
109
11- // Mocking the updateRateLimitedAt function
12- vi . mock ( "../../../src/core/api" , ( ) => ( {
13- updateRateLimitedAt : vi . fn ( ) . mockResolvedValue ( { } ) ,
14- } ) ) ;
15-
1610describe ( "rateLimit" , ( ) => {
1711 beforeEach ( ( ) => {
1812 // Clear mock function calls and reset any necessary state.
@@ -27,7 +21,7 @@ describe("rateLimit", () => {
2721
2822 it ( "should not rate limit if service scope is not in rate limits" , async ( ) => {
2923 const result = await rateLimit ( {
30- project : validProjectResponse ,
24+ team : validTeamResponse ,
3125 limitPerSecond : 0 ,
3226 serviceConfig : validServiceConfig ,
3327 redis : mockRedis ,
@@ -44,7 +38,7 @@ describe("rateLimit", () => {
4438 mockRedis . incr . mockResolvedValue ( 50 ) ; // Current count is 50 requests in 10 seconds.
4539
4640 const result = await rateLimit ( {
47- project : validProjectResponse ,
41+ team : validTeamResponse ,
4842 limitPerSecond : 5 ,
4943 serviceConfig : validServiceConfig ,
5044 redis : mockRedis ,
@@ -55,15 +49,15 @@ describe("rateLimit", () => {
5549 requestCount : 50 ,
5650 rateLimit : 50 ,
5751 } ) ;
58- expect ( updateRateLimitedAt ) . not . toHaveBeenCalled ( ) ;
52+
5953 expect ( mockRedis . expire ) . not . toHaveBeenCalled ( ) ;
6054 } ) ;
6155
6256 it ( "should rate limit if exceeded hard limit" , async ( ) => {
6357 mockRedis . incr . mockResolvedValue ( 51 ) ;
6458
6559 const result = await rateLimit ( {
66- project : validProjectResponse ,
60+ team : validTeamResponse ,
6761 limitPerSecond : 5 ,
6862 serviceConfig : validServiceConfig ,
6963 redis : mockRedis ,
@@ -77,15 +71,15 @@ describe("rateLimit", () => {
7771 errorMessage : `You've exceeded your storage rate limit at 5 reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.` ,
7872 errorCode : "RATE_LIMIT_EXCEEDED" ,
7973 } ) ;
80- expect ( updateRateLimitedAt ) . toHaveBeenCalled ( ) ;
74+
8175 expect ( mockRedis . expire ) . not . toHaveBeenCalled ( ) ;
8276 } ) ;
8377
8478 it ( "expires on the first incr request only" , async ( ) => {
8579 mockRedis . incr . mockResolvedValue ( 1 ) ;
8680
8781 const result = await rateLimit ( {
88- project : validProjectResponse ,
82+ team : validTeamResponse ,
8983 limitPerSecond : 5 ,
9084 serviceConfig : validServiceConfig ,
9185 redis : mockRedis ,
@@ -104,7 +98,7 @@ describe("rateLimit", () => {
10498 vi . spyOn ( global . Math , "random" ) . mockReturnValue ( 0.08 ) ;
10599
106100 const result = await rateLimit ( {
107- project : validProjectResponse ,
101+ team : validTeamResponse ,
108102 limitPerSecond : 5 ,
109103 serviceConfig : validServiceConfig ,
110104 redis : mockRedis ,
@@ -127,7 +121,7 @@ describe("rateLimit", () => {
127121 vi . spyOn ( global . Math , "random" ) . mockReturnValue ( 0.15 ) ;
128122
129123 const result = await rateLimit ( {
130- project : validProjectResponse ,
124+ team : validTeamResponse ,
131125 limitPerSecond : 5 ,
132126 serviceConfig : validServiceConfig ,
133127 redis : mockRedis ,
0 commit comments