Skip to content

Commit

Permalink
Merge pull request #607 from shivam-pareek/feat/endsWith-plugin
Browse files Browse the repository at this point in the history
Ends With Plugin
  • Loading branch information
VisargD authored Sep 27, 2024
2 parents 8ae3ff6 + 0254b33 commit f3ed2b1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
49 changes: 49 additions & 0 deletions plugins/default/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { handler as sentenceCountHandler } from './sentenceCount';
import { handler as webhookHandler } from './webhook';
import { handler as logHandler } from './log';
import { handler as allUppercaseHandler } from './alluppercase';
import { handler as endsWithHandler } from './endsWith';

import { z } from 'zod';
import { PluginContext, PluginParameters } from '../types';
Expand Down Expand Up @@ -705,3 +706,51 @@ describe('allUppercase handler', () => {
expect(result.verdict).toBe(false);
});
});
describe('endsWith handler', () => {
it('should return true verdict if response ends with provided suffix', async () => {
const eventType = 'afterRequestHook';
const context: PluginContext = {
response: {
text: 'This is a sentence that ends with the expected word i.e. HarryPortkey.',
},
};
const parameters: PluginParameters = {
suffix: 'HarryPortkey',
};
const result = await endsWithHandler(context, parameters, eventType);
expect(result.error).toBe(null);
expect(result.verdict).toBe(true);
});
it('should return false verdict if response not ending with provided suffix', async () => {
const context: PluginContext = {
response: {
text: 'This is a sentence ending with wrong word i.e. MalfoyPortkey.',
},
};
const eventType = 'afterRequestHook';

const parameters: PluginParameters = {
suffix: 'HarryPortkey',
};

const result = await endsWithHandler(context, parameters, eventType);
expect(result.error).toBe(null);
expect(result.verdict).toBe(false);
});

it('should return error for missing suffix in parameters', async () => {
const context: PluginContext = {
response: { text: 'This is a sentence which ends with Portkey.' },
};
const eventType = 'afterRequestHook';

const parameters: PluginParameters = {};

const result = await endsWithHandler(context, parameters, eventType);

expect(result.error).toBeInstanceOf(Error);
expect(result.error?.message).toBe('Missing suffix or text');
expect(result.verdict).toBe(false);
expect(result.data).toBe(null);
});
});
32 changes: 32 additions & 0 deletions plugins/default/endsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
HookEventType,
PluginContext,
PluginHandler,
PluginParameters,
} from '../types';
import { getText } from '../utils';

export const handler: PluginHandler = async (
context: PluginContext,
parameters: PluginParameters,
eventType: HookEventType
) => {
let error = null;
let verdict = false;
let data = null;

try {
const suffix = parameters.suffix;
let text = getText(context, eventType);

if (suffix !== undefined && '' !== suffix && text.length >= 0) {
verdict = text.endsWith(suffix) || text.endsWith(`${suffix}.`);
} else {
error = error || new Error('Missing suffix or text');
}
} catch (e) {
error = e as Error;
}

return { error, verdict, data };
};

0 comments on commit f3ed2b1

Please sign in to comment.