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

test: adding parsing tests. changed files parsing.test.ts #996

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
94 changes: 94 additions & 0 deletions packages/core/src/tests/parsing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { describe, it, expect } from 'vitest';
import {
parseShouldRespondFromText,
parseBooleanFromText,
parseJsonArrayFromText,
parseJSONObjectFromText,
} from '../parsing';

describe('Parsing Module', () => {
describe('parseShouldRespondFromText', () => {
it('should parse exact matches', () => {
expect(parseShouldRespondFromText('[RESPOND]')).toBe('RESPOND');
expect(parseShouldRespondFromText('[IGNORE]')).toBe('IGNORE');
expect(parseShouldRespondFromText('[STOP]')).toBe('STOP');
});

it('should handle case insensitive input', () => {
expect(parseShouldRespondFromText('[respond]')).toBe('RESPOND');
expect(parseShouldRespondFromText('[ignore]')).toBe('IGNORE');
expect(parseShouldRespondFromText('[stop]')).toBe('STOP');
});

it('should handle text containing keywords', () => {
expect(parseShouldRespondFromText('I think we should RESPOND here')).toBe('RESPOND');
expect(parseShouldRespondFromText('Better to IGNORE this one')).toBe('IGNORE');
expect(parseShouldRespondFromText('We need to STOP now')).toBe('STOP');
});

it('should return null for invalid input', () => {
expect(parseShouldRespondFromText('')).toBe(null);
expect(parseShouldRespondFromText('invalid')).toBe(null);
expect(parseShouldRespondFromText('[INVALID]')).toBe(null);
});
});

describe('parseBooleanFromText', () => {
it('should parse exact YES/NO matches', () => {
expect(parseBooleanFromText('YES')).toBe(true);
expect(parseBooleanFromText('NO')).toBe(false);
});

it('should handle case insensitive input', () => {
expect(parseBooleanFromText('yes')).toBe(true);
expect(parseBooleanFromText('no')).toBe(false);
});

it('should return null for invalid input', () => {
expect(parseBooleanFromText('')).toBe(null);
expect(parseBooleanFromText('maybe')).toBe(null);
expect(parseBooleanFromText('YES NO')).toBe(null);
});
});

describe('parseJsonArrayFromText', () => {
it('should parse JSON array from code block', () => {
const input = '```json\n["item1", "item2", "item3"]\n```';
expect(parseJsonArrayFromText(input)).toEqual(['item1', 'item2', 'item3']);
});

it('should handle empty arrays', () => {
expect(parseJsonArrayFromText('```json\n[]\n```')).toEqual([]);
expect(parseJsonArrayFromText('[]')).toEqual(null);
});

it('should return null for invalid JSON', () => {
expect(parseJsonArrayFromText('invalid')).toBe(null);
expect(parseJsonArrayFromText('[invalid]')).toBe(null);
expect(parseJsonArrayFromText('```json\n[invalid]\n```')).toBe(null);
});
});

describe('parseJSONObjectFromText', () => {
it('should parse JSON object from code block', () => {
const input = '```json\n{"key": "value", "number": 42}\n```';
expect(parseJSONObjectFromText(input)).toEqual({ key: 'value', number: 42 });
});

it('should parse JSON object without code block', () => {
const input = '{"key": "value", "number": 42}';
expect(parseJSONObjectFromText(input)).toEqual({ key: 'value', number: 42 });
});

it('should handle empty objects', () => {
expect(parseJSONObjectFromText('```json\n{}\n```')).toEqual({});
expect(parseJSONObjectFromText('{}')).toEqual({});
});

it('should return null for invalid JSON', () => {
expect(parseJSONObjectFromText('invalid')).toBe(null);
expect(parseJSONObjectFromText('{invalid}')).toBe(null);
expect(parseJSONObjectFromText('```json\n{invalid}\n```')).toBe(null);
});
});
});
Loading