2025-04-14 10:34:11 -07:00
|
|
|
import type { Document } from '@/lib/db/schema';
|
|
|
|
|
import { generateUUID } from '@/lib/utils';
|
2025-04-22 18:55:17 -07:00
|
|
|
import { expect, test } from '../fixtures';
|
2025-05-13 19:01:28 -07:00
|
|
|
import { getMessageByErrorCode } from '@/lib/errors';
|
2025-04-14 10:34:11 -07:00
|
|
|
|
|
|
|
|
const documentsCreatedByAda: Array<Document> = [];
|
|
|
|
|
|
|
|
|
|
test.describe
|
|
|
|
|
.serial('/api/document', () => {
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada cannot retrieve a document without specifying an id', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const response = await adaContext.request.get('/api/document');
|
|
|
|
|
expect(response.status()).toBe(400);
|
|
|
|
|
|
2025-05-13 19:01:28 -07:00
|
|
|
const { code, message } = await response.json();
|
|
|
|
|
expect(code).toEqual('bad_request:api');
|
|
|
|
|
expect(message).toEqual(getMessageByErrorCode(code));
|
2025-04-14 10:34:11 -07:00
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada cannot retrieve a document that does not exist', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const documentId = generateUUID();
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.get(
|
|
|
|
|
`/api/document?id=${documentId}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(404);
|
|
|
|
|
|
2025-05-13 19:01:28 -07:00
|
|
|
const { code, message } = await response.json();
|
|
|
|
|
expect(code).toEqual('not_found:document');
|
|
|
|
|
expect(message).toEqual(getMessageByErrorCode(code));
|
2025-04-14 10:34:11 -07:00
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada can create a document', async ({ adaContext }) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const documentId = generateUUID();
|
|
|
|
|
|
|
|
|
|
const draftDocument = {
|
|
|
|
|
title: "Ada's Document",
|
|
|
|
|
kind: 'text',
|
|
|
|
|
content: 'Created by Ada',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.post(
|
|
|
|
|
`/api/document?id=${documentId}`,
|
|
|
|
|
{
|
|
|
|
|
data: draftDocument,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const [createdDocument] = await response.json();
|
|
|
|
|
expect(createdDocument).toMatchObject(draftDocument);
|
|
|
|
|
|
|
|
|
|
documentsCreatedByAda.push(createdDocument);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada can retrieve a created document', async ({ adaContext }) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [document] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.get(
|
|
|
|
|
`/api/document?id=${document.id}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const retrievedDocuments = await response.json();
|
|
|
|
|
expect(retrievedDocuments).toHaveLength(1);
|
|
|
|
|
|
|
|
|
|
const [retrievedDocument] = retrievedDocuments;
|
|
|
|
|
expect(retrievedDocument).toMatchObject(document);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada can save a new version of the document', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const draftDocument = {
|
|
|
|
|
title: "Ada's Document",
|
|
|
|
|
kind: 'text',
|
|
|
|
|
content: 'Updated by Ada',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.post(
|
|
|
|
|
`/api/document?id=${firstDocument.id}`,
|
|
|
|
|
{
|
|
|
|
|
data: draftDocument,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const [createdDocument] = await response.json();
|
|
|
|
|
expect(createdDocument).toMatchObject(draftDocument);
|
|
|
|
|
|
|
|
|
|
documentsCreatedByAda.push(createdDocument);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada can retrieve all versions of her documents', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument, secondDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.get(
|
|
|
|
|
`/api/document?id=${firstDocument.id}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const retrievedDocuments = await response.json();
|
|
|
|
|
expect(retrievedDocuments).toHaveLength(2);
|
|
|
|
|
|
|
|
|
|
const [firstRetrievedDocument, secondRetrievedDocument] =
|
|
|
|
|
retrievedDocuments;
|
|
|
|
|
expect(firstRetrievedDocument).toMatchObject(firstDocument);
|
|
|
|
|
expect(secondRetrievedDocument).toMatchObject(secondDocument);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada cannot delete a document without specifying an id', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const response = await adaContext.request.delete(`/api/document`);
|
|
|
|
|
expect(response.status()).toBe(400);
|
|
|
|
|
|
2025-05-13 19:01:28 -07:00
|
|
|
const { code, message } = await response.json();
|
|
|
|
|
expect(code).toEqual('bad_request:api');
|
|
|
|
|
expect(message).toEqual(getMessageByErrorCode(code));
|
2025-04-14 10:34:11 -07:00
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada cannot delete a document without specifying a timestamp', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.delete(
|
|
|
|
|
`/api/document?id=${firstDocument.id}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(400);
|
|
|
|
|
|
2025-05-13 19:01:28 -07:00
|
|
|
const { code, message } = await response.json();
|
|
|
|
|
expect(code).toEqual('bad_request:api');
|
|
|
|
|
expect(message).toEqual(getMessageByErrorCode(code));
|
2025-04-14 10:34:11 -07:00
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada can delete a document by specifying id and timestamp', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument, secondDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.delete(
|
|
|
|
|
`/api/document?id=${firstDocument.id}×tamp=${firstDocument.createdAt}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const deletedDocuments = await response.json();
|
|
|
|
|
expect(deletedDocuments).toHaveLength(1);
|
|
|
|
|
|
|
|
|
|
const [deletedDocument] = deletedDocuments;
|
|
|
|
|
expect(deletedDocument).toMatchObject(secondDocument);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test('Ada can retrieve documents without deleted versions', async ({
|
|
|
|
|
adaContext,
|
|
|
|
|
}) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.get(
|
|
|
|
|
`/api/document?id=${firstDocument.id}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const retrievedDocuments = await response.json();
|
|
|
|
|
expect(retrievedDocuments).toHaveLength(1);
|
|
|
|
|
|
|
|
|
|
const [firstRetrievedDocument] = retrievedDocuments;
|
|
|
|
|
expect(firstRetrievedDocument).toMatchObject(firstDocument);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test("Babbage cannot update Ada's document", async ({ babbageContext }) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const draftDocument = {
|
|
|
|
|
title: "Babbage's Document",
|
|
|
|
|
kind: 'text',
|
|
|
|
|
content: 'Created by Babbage',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const response = await babbageContext.request.post(
|
|
|
|
|
`/api/document?id=${firstDocument.id}`,
|
|
|
|
|
{
|
|
|
|
|
data: draftDocument,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(403);
|
|
|
|
|
|
2025-05-13 19:01:28 -07:00
|
|
|
const { code, message } = await response.json();
|
|
|
|
|
expect(code).toEqual('forbidden:document');
|
|
|
|
|
expect(message).toEqual(getMessageByErrorCode(code));
|
2025-04-14 10:34:11 -07:00
|
|
|
});
|
|
|
|
|
|
2025-04-22 18:55:17 -07:00
|
|
|
test("Ada's documents did not get updated", async ({ adaContext }) => {
|
2025-04-14 10:34:11 -07:00
|
|
|
const [firstDocument] = documentsCreatedByAda;
|
|
|
|
|
|
|
|
|
|
const response = await adaContext.request.get(
|
|
|
|
|
`/api/document?id=${firstDocument.id}`,
|
|
|
|
|
);
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
|
|
|
|
|
|
const documentsRetrieved = await response.json();
|
|
|
|
|
expect(documentsRetrieved).toHaveLength(1);
|
|
|
|
|
});
|
|
|
|
|
});
|