Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ends With Plugin #607

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
};
Loading