1
- import { describe , expect , it , beforeEach , afterEach } from 'bun:test'
2
- import { DefaultContext } from './defaultContext'
3
- import { defaultOptions } from '../constants/defaultOptions'
1
+ import { afterEach , beforeEach , describe , expect , it } from 'bun:test'
4
2
import type { Options } from '../@types/Options'
3
+ import { defaultOptions } from '../constants/defaultOptions'
4
+ import { DefaultContext } from './defaultContext'
5
5
6
6
describe ( 'DefaultContext' , ( ) => {
7
7
let context : DefaultContext
8
-
8
+
9
9
beforeEach ( ( ) => {
10
10
context = new DefaultContext ( )
11
11
context . init ( {
12
12
...defaultOptions ,
13
13
} )
14
14
} )
15
-
15
+
16
16
afterEach ( async ( ) => {
17
17
await context . kill ( )
18
18
} )
19
-
19
+
20
20
it ( 'should initialize with default maxSize' , ( ) => {
21
21
const ctx = new DefaultContext ( )
22
22
expect ( ctx ) . toBeInstanceOf ( DefaultContext )
23
23
} )
24
-
24
+
25
25
it ( 'should initialize with custom maxSize' , ( ) => {
26
26
const ctx = new DefaultContext ( 1000 )
27
27
expect ( ctx ) . toBeInstanceOf ( DefaultContext )
28
28
} )
29
-
29
+
30
30
it ( 'should increment counter for new key' , async ( ) => {
31
31
const key = 'test-key-1'
32
32
const result = await context . increment ( key )
33
-
33
+
34
34
expect ( result . count ) . toBe ( 1 )
35
35
expect ( result . nextReset ) . toBeInstanceOf ( Date )
36
36
expect ( result . nextReset . getTime ( ) ) . toBeGreaterThan ( Date . now ( ) )
37
37
} )
38
-
38
+
39
39
it ( 'should increment counter for existing key' , async ( ) => {
40
40
const key = 'test-key-2'
41
-
41
+
42
42
await context . increment ( key )
43
43
const result = await context . increment ( key )
44
-
44
+
45
45
expect ( result . count ) . toBe ( 2 )
46
46
} )
47
-
47
+
48
48
it ( 'should decrement counter' , async ( ) => {
49
49
const key = 'test-key-3'
50
-
50
+
51
51
await context . increment ( key )
52
52
await context . increment ( key )
53
53
await context . decrement ( key )
54
-
54
+
55
55
const result = await context . increment ( key )
56
56
expect ( result . count ) . toBe ( 2 ) // It should be 2 after decrement and new increment
57
57
} )
58
-
58
+
59
59
it ( 'should reset counter for specific key' , async ( ) => {
60
60
const key1 = 'test-key-4'
61
61
const key2 = 'test-key-5'
62
-
62
+
63
63
await context . increment ( key1 )
64
64
await context . increment ( key2 )
65
65
await context . reset ( key1 )
66
-
66
+
67
67
const result1 = await context . increment ( key1 )
68
68
expect ( result1 . count ) . toBe ( 1 ) // Should be reset
69
-
69
+
70
70
const result2 = await context . increment ( key2 )
71
71
expect ( result2 . count ) . toBe ( 2 ) // Should still be incremented
72
72
} )
73
-
73
+
74
74
it ( 'should reset all counters' , async ( ) => {
75
75
const key1 = 'test-key-6'
76
76
const key2 = 'test-key-7'
77
-
77
+
78
78
await context . increment ( key1 )
79
79
await context . increment ( key2 )
80
80
await context . reset ( )
81
-
81
+
82
82
const result1 = await context . increment ( key1 )
83
83
expect ( result1 . count ) . toBe ( 1 )
84
-
84
+
85
85
const result2 = await context . increment ( key2 )
86
86
expect ( result2 . count ) . toBe ( 1 )
87
87
} )
88
-
88
+
89
89
it ( 'should handle expired keys correctly' , async ( ) => {
90
90
// Create context with a very short duration
91
91
const shortContext = new DefaultContext ( )
92
92
shortContext . init ( {
93
93
...defaultOptions ,
94
- duration : 10
94
+ duration : 10 ,
95
95
} as Omit < Options , 'context' > )
96
-
96
+
97
97
const key = 'test-key-8'
98
-
98
+
99
99
await shortContext . increment ( key )
100
-
100
+
101
101
// Wait for the timeout to expire
102
102
await new Promise < void > ( resolve => setTimeout ( resolve , 15 ) )
103
-
103
+
104
104
// After expiration, the count should reset
105
105
const result = await shortContext . increment ( key )
106
106
expect ( result . count ) . toBe ( 1 )
107
-
107
+
108
108
await shortContext . kill ( )
109
109
} )
110
- } )
110
+ } )
0 commit comments