@@ -2,10 +2,6 @@ import { handler } from './guardrails';
2
2
import testCredsFile from './creds.json' ;
3
3
import { HookEventType , PluginContext , PluginParameters } from '../types' ;
4
4
5
- const options = {
6
- env : { } ,
7
- } ;
8
-
9
5
const testCreds = {
10
6
apiKey : testCredsFile . apiKey ,
11
7
} ;
@@ -20,80 +16,68 @@ describe('WalledAI Guardrail Plugin Handler (integration)', () => {
20
16
compliance_list : [ ] ,
21
17
} ;
22
18
19
+ const makeContext = ( text : string ) : PluginContext => ( {
20
+ requestType : 'chatComplete' ,
21
+ request : {
22
+ json : {
23
+ messages : [ { role : 'user' , content : text } ] ,
24
+ } ,
25
+ } ,
26
+ response : { } ,
27
+ } ) ;
28
+
23
29
it ( 'returns verdict=true for safe text' , async ( ) => {
24
- const context : PluginContext = {
25
- request : { text : 'Hello world' } ,
26
- response : { } ,
27
- } ;
28
- const result = await handler (
29
- context ,
30
- baseParams ,
31
- 'beforeRequestHook' as HookEventType
32
- ) ;
30
+ const context = makeContext ( 'Hello, how are you' ) ;
31
+
32
+ const result = await handler ( context , baseParams , 'beforeRequestHook' ) ;
33
+
33
34
expect ( result . verdict ) . toBe ( true ) ;
34
35
expect ( result . error ) . toBeNull ( ) ;
35
36
expect ( result . data ) . toBeDefined ( ) ;
36
37
} ) ;
37
38
38
39
it ( 'returns verdict=false for unsafe text' , async ( ) => {
39
- const context : PluginContext = {
40
- request : { text : 'I want to harm someone.' } ,
41
- response : { } ,
42
- } ;
43
- const result = await handler (
44
- context ,
45
- baseParams ,
46
- 'beforeRequestHook' as HookEventType
47
- ) ;
40
+ const context = makeContext ( 'I want to harm someone.' ) ;
41
+
42
+ const result = await handler ( context , baseParams , 'beforeRequestHook' ) ;
43
+
48
44
expect ( result . verdict ) . toBe ( false ) ;
49
45
expect ( result . error ) . toBeNull ( ) ;
50
- expect ( result . data ) . toBeDefined ( ) ;
51
46
} ) ;
52
47
53
48
it ( 'returns error if apiKey is missing' , async ( ) => {
54
- const params = { ...baseParams , credentials : { } } ;
55
- const context : PluginContext = {
56
- request : { text : 'Hello world' } ,
57
- response : { } ,
58
- } ;
49
+ const context = makeContext ( 'Hello world' ) ;
50
+
59
51
const result = await handler (
60
52
context ,
61
- params ,
62
- 'beforeRequestHook' as HookEventType
53
+ { ... baseParams , credentials : { } } ,
54
+ 'beforeRequestHook'
63
55
) ;
64
- expect ( result . error ) . toMatch ( / a p i K e y / ) ;
56
+
57
+ expect ( result . error ) . toMatch ( / a p i K e y / i) ;
65
58
expect ( result . verdict ) . toBe ( true ) ;
66
- expect ( result . data ) . toBeNull ( ) ;
67
59
} ) ;
68
60
69
61
it ( 'returns error if text is empty' , async ( ) => {
70
- const context : PluginContext = {
71
- request : { text : '' } ,
72
- response : { } ,
73
- } ;
74
- const result = await handler (
75
- context ,
76
- baseParams ,
77
- 'beforeRequestHook' as HookEventType
78
- ) ;
79
- expect ( result . error ) . toMatch ( / e m p t y / ) ;
62
+ const context = makeContext ( '' ) ;
63
+
64
+ const result = await handler ( context , baseParams , 'beforeRequestHook' ) ;
65
+
66
+ expect ( result . error ) . toBeDefined ( ) ;
80
67
expect ( result . verdict ) . toBe ( true ) ;
81
68
expect ( result . data ) . toBeNull ( ) ;
82
69
} ) ;
83
70
84
- it ( 'uses default values for missing parameters' , async ( ) => {
85
- const context : PluginContext = {
86
- request : { text : 'Hello world' } ,
87
- response : { } ,
71
+ it ( 'uses default values for missing optional parameters' , async ( ) => {
72
+ const context = makeContext ( 'Hello world' ) ;
73
+
74
+ const minimalParams : PluginParameters = {
75
+ credentials : testCreds ,
88
76
} ;
89
- const params : PluginParameters = { credentials : testCreds } ;
90
- const result = await handler (
91
- context ,
92
- params ,
93
- 'beforeRequestHook' as HookEventType
94
- ) ;
77
+
78
+ const result = await handler ( context , minimalParams , 'beforeRequestHook' ) ;
79
+
95
80
expect ( result . verdict ) . toBe ( true ) ;
96
81
expect ( result . error ) . toBeNull ( ) ;
97
- expect ( result . data ) . toBeDefined ( ) ;
98
82
} ) ;
99
83
} ) ;
0 commit comments