73 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
		
		
			
		
	
	
			73 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 
								 | 
							
								import { expect, tap } from '@push.rocks/tapbundle';
							 | 
						||
| 
								 | 
							
								import * as qenv from '@push.rocks/qenv';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const testQenv = new qenv.Qenv('./', './.nogit/');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import * as smartai from '../ts/index.js';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								let anthropicProvider: smartai.AnthropicProvider;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								tap.test('Anthropic Chat: should create and start Anthropic provider', async () => {
							 | 
						||
| 
								 | 
							
								  anthropicProvider = new smartai.AnthropicProvider({
							 | 
						||
| 
								 | 
							
								    anthropicToken: await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN'),
							 | 
						||
| 
								 | 
							
								  });
							 | 
						||
| 
								 | 
							
								  await anthropicProvider.start();
							 | 
						||
| 
								 | 
							
								  expect(anthropicProvider).toBeInstanceOf(smartai.AnthropicProvider);
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								tap.test('Anthropic Chat: should create chat response', async () => {
							 | 
						||
| 
								 | 
							
								  const userMessage = 'What is the capital of France? Answer in one word.';
							 | 
						||
| 
								 | 
							
								  const response = await anthropicProvider.chat({
							 | 
						||
| 
								 | 
							
								    systemMessage: 'You are a helpful assistant. Be concise.',
							 | 
						||
| 
								 | 
							
								    userMessage: userMessage,
							 | 
						||
| 
								 | 
							
								    messageHistory: [],
							 | 
						||
| 
								 | 
							
								  });
							 | 
						||
| 
								 | 
							
								  console.log(`Anthropic Chat - User: ${userMessage}`);
							 | 
						||
| 
								 | 
							
								  console.log(`Anthropic Chat - Response: ${response.message}`);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  expect(response.role).toEqual('assistant');
							 | 
						||
| 
								 | 
							
								  expect(response.message).toBeTruthy();
							 | 
						||
| 
								 | 
							
								  expect(response.message.toLowerCase()).toInclude('paris');
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								tap.test('Anthropic Chat: should handle message history', async () => {
							 | 
						||
| 
								 | 
							
								  const messageHistory: smartai.ChatMessage[] = [
							 | 
						||
| 
								 | 
							
								    { role: 'user', content: 'My name is Claude Test' },
							 | 
						||
| 
								 | 
							
								    { role: 'assistant', content: 'Nice to meet you, Claude Test!' }
							 | 
						||
| 
								 | 
							
								  ];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  const response = await anthropicProvider.chat({
							 | 
						||
| 
								 | 
							
								    systemMessage: 'You are a helpful assistant with good memory.',
							 | 
						||
| 
								 | 
							
								    userMessage: 'What is my name?',
							 | 
						||
| 
								 | 
							
								    messageHistory: messageHistory,
							 | 
						||
| 
								 | 
							
								  });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  console.log(`Anthropic Memory Test - Response: ${response.message}`);
							 | 
						||
| 
								 | 
							
								  expect(response.message.toLowerCase()).toInclude('claude test');
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								tap.test('Anthropic Chat: should handle errors gracefully', async () => {
							 | 
						||
| 
								 | 
							
								  // Test with invalid message (empty)
							 | 
						||
| 
								 | 
							
								  let errorCaught = false;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  try {
							 | 
						||
| 
								 | 
							
								    await anthropicProvider.chat({
							 | 
						||
| 
								 | 
							
								      systemMessage: '',
							 | 
						||
| 
								 | 
							
								      userMessage: '',
							 | 
						||
| 
								 | 
							
								      messageHistory: [],
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								  } catch (error) {
							 | 
						||
| 
								 | 
							
								    errorCaught = true;
							 | 
						||
| 
								 | 
							
								    console.log('Expected error caught:', error.message);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  // Anthropic might handle empty messages, so we don't assert error
							 | 
						||
| 
								 | 
							
								  console.log(`Error handling test - Error caught: ${errorCaught}`);
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								tap.test('Anthropic Chat: should stop the provider', async () => {
							 | 
						||
| 
								 | 
							
								  await anthropicProvider.stop();
							 | 
						||
| 
								 | 
							
								});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export default tap.start();
							 |