1
1
import 'dotenv/config'
2
- import { describe , expect , it , vi } from 'vitest'
2
+ import { describe , expect , it , beforeAll } from 'vitest'
3
3
4
4
import { LockContext , protect , csTable , csColumn } from '../src'
5
5
@@ -17,10 +17,16 @@ type User = {
17
17
number ?: number
18
18
}
19
19
20
+ let protectClient : Awaited < ReturnType < typeof protect > >
21
+
22
+ beforeAll ( async ( ) => {
23
+ protectClient = await protect ( {
24
+ schemas : [ users ] ,
25
+ } )
26
+ } )
27
+
20
28
describe ( 'encryption and decryption' , ( ) => {
21
29
it ( 'should encrypt and decrypt a payload' , async ( ) => {
22
- const protectClient = await protect ( { schemas : [ users ] } )
23
-
24
30
const email = 'hello@example.com'
25
31
26
32
const ciphertext = await protectClient . encrypt ( email , {
@@ -35,16 +41,18 @@ describe('encryption and decryption', () => {
35
41
// Verify encrypted field
36
42
expect ( ciphertext . data ) . toHaveProperty ( 'c' )
37
43
38
- const plaintext = await protectClient . decrypt ( ciphertext . data )
44
+ const plaintext = await protectClient . decrypt ( ciphertext . data ) . audit ( {
45
+ metadata : {
46
+ user : 'cj@cjb.io' ,
47
+ } ,
48
+ } )
39
49
40
50
expect ( plaintext ) . toEqual ( {
41
51
data : email ,
42
52
} )
43
53
} , 30000 )
44
54
45
55
it ( 'should return null if plaintext is null' , async ( ) => {
46
- const protectClient = await protect ( { schemas : [ users ] } )
47
-
48
56
const ciphertext = await protectClient . encrypt ( null , {
49
57
column : users . email ,
50
58
table : users ,
@@ -65,8 +73,6 @@ describe('encryption and decryption', () => {
65
73
} , 30000 )
66
74
67
75
it ( 'should encrypt and decrypt a model' , async ( ) => {
68
- const protectClient = await protect ( { schemas : [ users ] } )
69
-
70
76
// Create a model with decrypted values
71
77
const decryptedModel = {
72
78
id : '1' ,
@@ -117,8 +123,6 @@ describe('encryption and decryption', () => {
117
123
} , 30000 )
118
124
119
125
it ( 'should handle null values in a model' , async ( ) => {
120
- const protectClient = await protect ( { schemas : [ users ] } )
121
-
122
126
// Create a model with null values
123
127
const decryptedModel = {
124
128
id : '1' ,
@@ -169,8 +173,6 @@ describe('encryption and decryption', () => {
169
173
} , 30000 )
170
174
171
175
it ( 'should handle undefined values in a model' , async ( ) => {
172
- const protectClient = await protect ( { schemas : [ users ] } )
173
-
174
176
// Create a model with undefined values
175
177
const decryptedModel = {
176
178
id : '1' ,
@@ -223,8 +225,6 @@ describe('encryption and decryption', () => {
223
225
224
226
describe ( 'bulk encryption' , ( ) => {
225
227
it ( 'should bulk encrypt and decrypt models' , async ( ) => {
226
- const protectClient = await protect ( { schemas : [ users ] } )
227
-
228
228
// Create models with decrypted values
229
229
const decryptedModels = [
230
230
{
@@ -301,8 +301,6 @@ describe('bulk encryption', () => {
301
301
} , 30000 )
302
302
303
303
it ( 'should return empty array if models is empty' , async ( ) => {
304
- const protectClient = await protect ( { schemas : [ users ] } )
305
-
306
304
// Encrypt empty array of models
307
305
const encryptedModels = await protectClient . bulkEncryptModels < User > (
308
306
[ ] ,
@@ -317,8 +315,6 @@ describe('bulk encryption', () => {
317
315
} , 30000 )
318
316
319
317
it ( 'should return empty array if decrypting empty array of models' , async ( ) => {
320
- const protectClient = await protect ( { schemas : [ users ] } )
321
-
322
318
// Decrypt empty array of models
323
319
const decryptedResult = await protectClient . bulkDecryptModels < User > ( [ ] )
324
320
@@ -332,7 +328,6 @@ describe('bulk encryption', () => {
332
328
333
329
describe ( 'bulk encryption edge cases' , ( ) => {
334
330
it ( 'should handle mixed null and non-null values in bulk operations' , async ( ) => {
335
- const protectClient = await protect ( { schemas : [ users ] } )
336
331
const decryptedModels = [
337
332
{
338
333
id : '1' ,
@@ -405,7 +400,6 @@ describe('bulk encryption edge cases', () => {
405
400
} , 30000 )
406
401
407
402
it ( 'should handle mixed undefined and non-undefined values in bulk operations' , async ( ) => {
408
- const protectClient = await protect ( { schemas : [ users ] } )
409
403
const decryptedModels = [
410
404
{
411
405
id : '1' ,
@@ -478,7 +472,6 @@ describe('bulk encryption edge cases', () => {
478
472
} , 30000 )
479
473
480
474
it ( 'should handle empty models in bulk operations' , async ( ) => {
481
- const protectClient = await protect ( { schemas : [ users ] } )
482
475
const decryptedModels = [
483
476
{
484
477
id : '1' ,
@@ -548,7 +541,6 @@ describe('bulk encryption edge cases', () => {
548
541
549
542
describe ( 'error handling' , ( ) => {
550
543
it ( 'should handle invalid encrypted payloads' , async ( ) => {
551
- const protectClient = await protect ( { schemas : [ users ] } )
552
544
const validModel = {
553
545
id : '1' ,
554
546
email : 'test@example.com' ,
@@ -582,7 +574,6 @@ describe('error handling', () => {
582
574
} , 30000 )
583
575
584
576
it ( 'should handle missing required fields' , async ( ) => {
585
- const protectClient = await protect ( { schemas : [ users ] } )
586
577
const model = {
587
578
id : '1' ,
588
579
email : null ,
@@ -603,7 +594,6 @@ describe('error handling', () => {
603
594
604
595
describe ( 'type safety' , ( ) => {
605
596
it ( 'should maintain type safety with complex nested objects' , async ( ) => {
606
- const protectClient = await protect ( { schemas : [ users ] } )
607
597
const model = {
608
598
id : '1' ,
609
599
email : 'test@example.com' ,
@@ -641,7 +631,6 @@ describe('type safety', () => {
641
631
642
632
describe ( 'performance' , ( ) => {
643
633
it ( 'should handle large numbers of models efficiently' , async ( ) => {
644
- const protectClient = await protect ( { schemas : [ users ] } )
645
634
const largeModels = Array ( 10 )
646
635
. fill ( null )
647
636
. map ( ( _ , i ) => ( {
0 commit comments