feat: add tests for /api/chat (#950)
This commit is contained in:
parent
4ca93aaf89
commit
a159b77fcf
9 changed files with 197 additions and 50 deletions
81
tests/routes/chat.test.ts
Normal file
81
tests/routes/chat.test.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import { generateUUID } from '@/lib/utils';
|
||||
import { expect, test } from '../fixtures';
|
||||
import { TEST_PROMPTS } from '../prompts/routes';
|
||||
|
||||
const chatIdsCreatedByAda: Array<string> = [];
|
||||
|
||||
test.describe
|
||||
.serial('/api/chat', () => {
|
||||
test('Ada cannot invoke a chat generation with empty request body', async ({
|
||||
adaContext,
|
||||
}) => {
|
||||
const response = await adaContext.request.post('/api/chat', {
|
||||
data: {},
|
||||
});
|
||||
expect(response.status()).toBe(500);
|
||||
|
||||
const text = await response.text();
|
||||
expect(text).toEqual('An error occurred while processing your request!');
|
||||
});
|
||||
|
||||
test('Ada can invoke chat generation', async ({ adaContext }) => {
|
||||
const chatId = generateUUID();
|
||||
|
||||
const response = await adaContext.request.post('api/chat', {
|
||||
data: {
|
||||
id: chatId,
|
||||
messages: TEST_PROMPTS.SKY.MESSAGES,
|
||||
selectedChatModel: 'chat-model',
|
||||
},
|
||||
});
|
||||
expect(response.status()).toBe(200);
|
||||
|
||||
const text = await response.text();
|
||||
const lines = text.split('\n');
|
||||
|
||||
const [_, ...rest] = lines;
|
||||
expect(rest.filter(Boolean)).toEqual(TEST_PROMPTS.SKY.OUTPUT_STREAM);
|
||||
|
||||
chatIdsCreatedByAda.push(chatId);
|
||||
});
|
||||
|
||||
test("Babbage cannot append message to Ada's chat", async ({
|
||||
babbageContext,
|
||||
}) => {
|
||||
const [chatId] = chatIdsCreatedByAda;
|
||||
|
||||
const response = await babbageContext.request.post('api/chat', {
|
||||
data: {
|
||||
id: chatId,
|
||||
messages: TEST_PROMPTS.GRASS.MESSAGES,
|
||||
selectedChatModel: 'chat-model',
|
||||
},
|
||||
});
|
||||
expect(response.status()).toBe(403);
|
||||
|
||||
const text = await response.text();
|
||||
expect(text).toEqual('Forbidden');
|
||||
});
|
||||
|
||||
test("Babbage cannot delete Ada's chat", async ({ babbageContext }) => {
|
||||
const [chatId] = chatIdsCreatedByAda;
|
||||
|
||||
const response = await babbageContext.request.delete(
|
||||
`api/chat?id=${chatId}`,
|
||||
);
|
||||
expect(response.status()).toBe(403);
|
||||
|
||||
const text = await response.text();
|
||||
expect(text).toEqual('Forbidden');
|
||||
});
|
||||
|
||||
test('Ada can delete her own chat', async ({ adaContext }) => {
|
||||
const [chatId] = chatIdsCreatedByAda;
|
||||
|
||||
const response = await adaContext.request.delete(`api/chat?id=${chatId}`);
|
||||
expect(response.status()).toBe(200);
|
||||
|
||||
const deletedChat = await response.json();
|
||||
expect(deletedChat).toMatchObject({ id: chatId });
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue