feat: add tests for /api/chat (#950)

This commit is contained in:
Jeremy 2025-04-22 18:55:17 -07:00 committed by GitHub
parent 4ca93aaf89
commit a159b77fcf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 197 additions and 50 deletions

View file

@ -1,36 +1,14 @@
import type { Document } from '@/lib/db/schema';
import { generateUUID } from '@/lib/utils';
import {
createAuthenticatedContext,
type UserContext,
} from '@/tests/auth-helper';
import { expect, test } from '@playwright/test';
let adaContext: UserContext;
let babbageContext: UserContext;
import { expect, test } from '../fixtures';
const documentsCreatedByAda: Array<Document> = [];
test.beforeAll(async ({ browser }) => {
adaContext = await createAuthenticatedContext({
browser,
name: 'ada',
});
babbageContext = await createAuthenticatedContext({
browser,
name: 'babbage',
});
});
test.afterAll(async () => {
await adaContext.context.close();
await babbageContext.context.close();
});
test.describe
.serial('/api/document', () => {
test('Ada cannot retrieve a document without specifying an id', async () => {
test('Ada cannot retrieve a document without specifying an id', async ({
adaContext,
}) => {
const response = await adaContext.request.get('/api/document');
expect(response.status()).toBe(400);
@ -38,7 +16,9 @@ test.describe
expect(text).toEqual('Missing id');
});
test('Ada cannot retrieve a document that does not exist', async () => {
test('Ada cannot retrieve a document that does not exist', async ({
adaContext,
}) => {
const documentId = generateUUID();
const response = await adaContext.request.get(
@ -50,7 +30,7 @@ test.describe
expect(text).toEqual('Not found');
});
test('Ada can create a document', async () => {
test('Ada can create a document', async ({ adaContext }) => {
const documentId = generateUUID();
const draftDocument = {
@ -73,7 +53,7 @@ test.describe
documentsCreatedByAda.push(createdDocument);
});
test('Ada can retrieve a created document', async () => {
test('Ada can retrieve a created document', async ({ adaContext }) => {
const [document] = documentsCreatedByAda;
const response = await adaContext.request.get(
@ -88,7 +68,9 @@ test.describe
expect(retrievedDocument).toMatchObject(document);
});
test('Ada can save a new version of the document', async () => {
test('Ada can save a new version of the document', async ({
adaContext,
}) => {
const [firstDocument] = documentsCreatedByAda;
const draftDocument = {
@ -111,7 +93,9 @@ test.describe
documentsCreatedByAda.push(createdDocument);
});
test('Ada can retrieve all versions of her documents', async () => {
test('Ada can retrieve all versions of her documents', async ({
adaContext,
}) => {
const [firstDocument, secondDocument] = documentsCreatedByAda;
const response = await adaContext.request.get(
@ -128,7 +112,9 @@ test.describe
expect(secondRetrievedDocument).toMatchObject(secondDocument);
});
test('Ada cannot delete a document without specifying an id', async () => {
test('Ada cannot delete a document without specifying an id', async ({
adaContext,
}) => {
const response = await adaContext.request.delete(`/api/document`);
expect(response.status()).toBe(400);
@ -136,7 +122,9 @@ test.describe
expect(text).toEqual('Missing id');
});
test('Ada cannot delete a document without specifying a timestamp', async () => {
test('Ada cannot delete a document without specifying a timestamp', async ({
adaContext,
}) => {
const [firstDocument] = documentsCreatedByAda;
const response = await adaContext.request.delete(
@ -148,7 +136,9 @@ test.describe
expect(text).toEqual('Missing timestamp');
});
test('Ada can delete a document by specifying id and timestamp', async () => {
test('Ada can delete a document by specifying id and timestamp', async ({
adaContext,
}) => {
const [firstDocument, secondDocument] = documentsCreatedByAda;
const response = await adaContext.request.delete(
@ -163,7 +153,9 @@ test.describe
expect(deletedDocument).toMatchObject(secondDocument);
});
test('Ada can retrieve documents without deleted versions', async () => {
test('Ada can retrieve documents without deleted versions', async ({
adaContext,
}) => {
const [firstDocument] = documentsCreatedByAda;
const response = await adaContext.request.get(
@ -178,7 +170,7 @@ test.describe
expect(firstRetrievedDocument).toMatchObject(firstDocument);
});
test("Babbage cannot update Ada's document", async () => {
test("Babbage cannot update Ada's document", async ({ babbageContext }) => {
const [firstDocument] = documentsCreatedByAda;
const draftDocument = {
@ -199,7 +191,7 @@ test.describe
expect(text).toEqual('Forbidden');
});
test("Ada's documents did not get updated", async () => {
test("Ada's documents did not get updated", async ({ adaContext }) => {
const [firstDocument] = documentsCreatedByAda;
const response = await adaContext.request.get(