chatbot-template/tests/routes/chat.test.ts

83 lines
2.4 KiB
TypeScript

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: JSON.stringify({}),
});
expect(response.status()).toBe(400);
const text = await response.text();
expect(text).toEqual('Invalid request body');
});
test('Ada can invoke chat generation', async ({ adaContext }) => {
const chatId = generateUUID();
const response = await adaContext.request.post('/api/chat', {
data: {
id: chatId,
message: TEST_PROMPTS.SKY.MESSAGE,
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,
message: TEST_PROMPTS.GRASS.MESSAGE,
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 });
});
});