Skip to content

Commit

Permalink
chore: add extra test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
b4s36t4 committed Nov 27, 2024
1 parent 34fa244 commit 3881aaa
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
6 changes: 4 additions & 2 deletions plugins/mistral/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ export const mistralGuardrailHandler: PluginHandler = async (
const guardrailFunction = fn as GuardrailFunction;

const text = getText(context, eventType);
const messages = context.request?.json?.messages;
const messages =
eventType === 'beforeRequestHook'
? context.request?.json?.messages
: context.response?.json?.messages;

// should contain text or should contain messages array
if (
(!text && !Array.isArray(messages)) ||
(Array.isArray(messages) && messages.length === 0)
) {
console.log(!text, messages);
return {
error: 'Mistral: Invalid Request body',
verdict: false,
Expand Down
76 changes: 75 additions & 1 deletion plugins/mistral/mistra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getParameters() {
};
}

describe('validateProject handler', () => {
describe('mistral guardrail handler', () => {
it('should fail if the apiKey is invalid', async () => {
const eventType = 'beforeRequestHook';
const context = {
Expand Down Expand Up @@ -106,4 +106,78 @@ describe('validateProject handler', () => {
expect(result.error).toBeNull();
expect(result.data).toBeNull();
});

it('should give error on invalid request body', async () => {
const eventType = 'beforeRequestHook';
const context = {
request: {},
};
const parameters = JSON.parse(JSON.stringify(getParameters()));

const result = await mistralGuardrailHandler(
context as unknown as PluginContext,
parameters,
eventType,
{ env: {} },
'pii'
);

expect(result).toBeDefined();
expect(result.verdict).toBe(false);
expect(result.error).toBe('Mistral: Invalid Request body');
expect(result.data).toBeNull();
});

it('should work for afterRequestHook', async () => {
const eventType = 'afterRequestHook';
const context = {
response: { text: 'this text is safe text' },
};
const parameters = JSON.parse(JSON.stringify(getParameters()));

const result = await mistralGuardrailHandler(
context as unknown as PluginContext,
parameters,
eventType,
{ env: {} },
'pii'
);

expect(result).toBeDefined();
expect(result.verdict).toBe(false);
expect(result.error).toBeNull();
expect(result.data).toBeNull();
});

it('should work for afterRequestHook with chatCompletion messages', async () => {
const eventType = 'afterRequestHook';
const context = {
requestType: 'chatComplete',
response: {
json: {
messages: [
{
role: 'user',
content:
'Say Hi. My name is Jhon Doe and my email is user@example.com',
},
],
},
},
};
const parameters = JSON.parse(JSON.stringify(getParameters()));

const result = await mistralGuardrailHandler(
context as unknown as PluginContext,
parameters,
eventType,
{ env: {} },
'pii'
);

expect(result).toBeDefined();
expect(result.verdict).toBe(false);
expect(result.error).toBeNull();
expect(result.data).toBeNull();
});
});

0 comments on commit 3881aaa

Please sign in to comment.