|
| 1 | +import { handler } from './guardrails'; |
| 2 | +import testCredsFile from './creds.json'; |
| 3 | +import { HookEventType, PluginContext, PluginParameters } from '../types'; |
| 4 | + |
| 5 | +const testCreds = { |
| 6 | + apiKey: testCredsFile.apiKey, |
| 7 | +}; |
| 8 | + |
| 9 | +describe('WalledAI Guardrail Plugin Handler (integration)', () => { |
| 10 | + const baseParams: PluginParameters = { |
| 11 | + credentials: testCreds, |
| 12 | + text_type: 'prompt', |
| 13 | + generic_safety_check: true, |
| 14 | + greetings_list: ['generalgreetings'], |
| 15 | + pii_list: ["Person's Name", 'Address'], |
| 16 | + compliance_list: [], |
| 17 | + }; |
| 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 | + |
| 29 | + it('returns verdict=true for safe text', async () => { |
| 30 | + const context = makeContext('Hello, how are you'); |
| 31 | + |
| 32 | + const result = await handler(context, baseParams, 'beforeRequestHook'); |
| 33 | + |
| 34 | + expect(result.verdict).toBe(true); |
| 35 | + expect(result.error).toBeNull(); |
| 36 | + expect(result.data).toBeDefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns verdict=false for unsafe text', async () => { |
| 40 | + const context = makeContext('I want to harm someone.'); |
| 41 | + |
| 42 | + const result = await handler(context, baseParams, 'beforeRequestHook'); |
| 43 | + |
| 44 | + expect(result.verdict).toBe(false); |
| 45 | + expect(result.error).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns error if apiKey is missing', async () => { |
| 49 | + const context = makeContext('Hello world'); |
| 50 | + |
| 51 | + const result = await handler( |
| 52 | + context, |
| 53 | + { ...baseParams, credentials: {} }, |
| 54 | + 'beforeRequestHook' |
| 55 | + ); |
| 56 | + |
| 57 | + expect(result.error).toMatch(/apiKey/i); |
| 58 | + expect(result.verdict).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns error if text is empty', async () => { |
| 62 | + const context = makeContext(''); |
| 63 | + |
| 64 | + const result = await handler(context, baseParams, 'beforeRequestHook'); |
| 65 | + |
| 66 | + expect(result.error).toBeDefined(); |
| 67 | + expect(result.verdict).toBe(true); |
| 68 | + expect(result.data).toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('uses default values for missing optional parameters', async () => { |
| 72 | + const context = makeContext('Hello world'); |
| 73 | + |
| 74 | + const minimalParams: PluginParameters = { |
| 75 | + credentials: testCreds, |
| 76 | + }; |
| 77 | + |
| 78 | + const result = await handler(context, minimalParams, 'beforeRequestHook'); |
| 79 | + |
| 80 | + expect(result.verdict).toBe(true); |
| 81 | + expect(result.error).toBeNull(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments