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

Improve repository error handling, and unit tests #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 app/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { suggestQuestions, generateSpeech, fetchMetadata, getGroupConfig } from './actions';

describe('suggestQuestions', () => {
it('should generate questions based on message history', async () => {
const history = [{ role: 'user', content: 'What is the weather like today?' }];
const result = await suggestQuestions(history);
expect(result).toHaveProperty('questions');
expect(result.questions.length).toBe(3);
});
});

describe('generateSpeech', () => {
it('should generate speech audio from text', async () => {
const text = 'Hello, how are you?';
const result = await generateSpeech(text);
expect(result).toHaveProperty('audio');
expect(result.audio).toContain('data:audio/mp3;base64,');
});
});

describe('fetchMetadata', () => {
it('should fetch metadata from a URL', async () => {
const url = 'https://example.com';
const result = await fetchMetadata(url);
expect(result).toHaveProperty('title');
expect(result).toHaveProperty('description');
});

it('should return null if an error occurs', async () => {
const url = 'https://invalid-url.com';
const result = await fetchMetadata(url);
expect(result).toBeNull();
});
});

describe('getGroupConfig', () => {
it('should return tools and systemPrompt for a given groupId', async () => {
const groupId = 'web';
const result = await getGroupConfig(groupId);
expect(result).toHaveProperty('tools');
expect(result).toHaveProperty('systemPrompt');
});

it('should return default group config if no groupId is provided', async () => {
const result = await getGroupConfig();
expect(result).toHaveProperty('tools');
expect(result).toHaveProperty('systemPrompt');
});
});
8 changes: 5 additions & 3 deletions app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// app/actions.ts
'use server';

import { serverEnv } from '@/env/server';
Expand Down Expand Up @@ -84,9 +83,12 @@ export async function generateSpeech(text: string, voice: 'alloy' | 'echo' | 'fa
};
}

export async function fetchMetadata(url: string) {
export async function fetchMetadata(url: string): Promise<{ title: string; description: string } | null> {
try {
const response = await fetch(url, { next: { revalidate: 3600 } }); // Cache for 1 hour
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const html = await response.text();

const titleMatch = html.match(/<title>(.*?)<\/title>/i);
Expand Down Expand Up @@ -271,4 +273,4 @@ export async function getGroupConfig(groupId: SearchGroupId = 'web') {
tools,
systemPrompt
};
}
}
152 changes: 152 additions & 0 deletions app/api/chat/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {
executeStockChartTool,
executeCurrencyConverterTool,
executeWebSearchTool,
executeXSearchTool,
executeTMDBSearchTool,
executeTrendingMoviesTool,
executeTrendingTVTool,
executeAcademicSearchTool,
executeYouTubeSearchTool,
executeRetrieveTool,
executeGetWeatherDataTool,
executeCodeInterpreterTool,
executeFindPlaceTool,
executeTextSearchTool,
executeNearbySearchTool,
executeTrackFlightTool,
} from './route';

describe('executeStockChartTool', () => {
it('should execute stock chart tool and return message and chart', async () => {
const params = { code: 'some code', title: 'some title', icon: 'stock' };
const result = await executeStockChartTool(params);
expect(result).toHaveProperty('message');
expect(result).toHaveProperty('chart');
});
});

describe('executeCurrencyConverterTool', () => {
it('should execute currency converter tool and return rate', async () => {
const params = { from: 'USD', to: 'EUR' };
const result = await executeCurrencyConverterTool(params);
expect(result).toHaveProperty('rate');
});
});

describe('executeWebSearchTool', () => {
it('should execute web search tool and return searches', async () => {
const params = {
queries: ['query1', 'query2'],
maxResults: [10, 10],
topics: ['general', 'news'],
searchDepth: ['basic', 'advanced'],
};
const result = await executeWebSearchTool(params);
expect(result).toHaveProperty('searches');
});
});

describe('executeXSearchTool', () => {
it('should execute X search tool and return results', async () => {
const params = { query: 'some query' };
const result = await executeXSearchTool(params);
expect(result).toBeInstanceOf(Array);
});
});

describe('executeTMDBSearchTool', () => {
it('should execute TMDB search tool and return result', async () => {
const params = { query: 'some query' };
const result = await executeTMDBSearchTool(params);
expect(result).toHaveProperty('result');
});
});

describe('executeTrendingMoviesTool', () => {
it('should execute trending movies tool and return results', async () => {
const result = await executeTrendingMoviesTool();
expect(result).toHaveProperty('results');
});
});

describe('executeTrendingTVTool', () => {
it('should execute trending TV tool and return results', async () => {
const result = await executeTrendingTVTool();
expect(result).toHaveProperty('results');
});
});

describe('executeAcademicSearchTool', () => {
it('should execute academic search tool and return results', async () => {
const params = { query: 'some query' };
const result = await executeAcademicSearchTool(params);
expect(result).toHaveProperty('results');
});
});

describe('executeYouTubeSearchTool', () => {
it('should execute YouTube search tool and return results', async () => {
const params = { query: 'some query', no_of_results: 5 };
const result = await executeYouTubeSearchTool(params);
expect(result).toHaveProperty('results');
});
});

describe('executeRetrieveTool', () => {
it('should execute retrieve tool and return results', async () => {
const params = { url: 'https://example.com' };
const result = await executeRetrieveTool(params);
expect(result).toHaveProperty('results');
});
});

describe('executeGetWeatherDataTool', () => {
it('should execute get weather data tool and return data', async () => {
const params = { lat: 40.7128, lon: -74.006 };
const result = await executeGetWeatherDataTool(params);
expect(result).toHaveProperty('list');
});
});

describe('executeCodeInterpreterTool', () => {
it('should execute code interpreter tool and return message and chart', async () => {
const params = { code: 'some code', title: 'some title', icon: 'default' };
const result = await executeCodeInterpreterTool(params);
expect(result).toHaveProperty('message');
expect(result).toHaveProperty('chart');
});
});

describe('executeFindPlaceTool', () => {
it('should execute find place tool and return features', async () => {
const params = { query: 'some query', coordinates: [40.7128, -74.006] };
const result = await executeFindPlaceTool(params);
expect(result).toHaveProperty('features');
});
});

describe('executeTextSearchTool', () => {
it('should execute text search tool and return results', async () => {
const params = { query: 'some query', location: '40.7128,-74.006', radius: 5000 };
const result = await executeTextSearchTool(params);
expect(result).toHaveProperty('results');
});
});

describe('executeNearbySearchTool', () => {
it('should execute nearby search tool and return results and center', async () => {
const params = { location: 'New York', latitude: 40.7128, longitude: -74.006, type: 'restaurant', radius: 5000 };
const result = await executeNearbySearchTool(params);
expect(result).toHaveProperty('results');
expect(result).toHaveProperty('center');
});
});

describe('executeTrackFlightTool', () => {
it('should execute track flight tool and return data', async () => {
const params = { flight_number: 'AA100' };
const result = await executeTrackFlightTool(params);
expect(result).toHaveProperty('data');
});
});
Loading