From 6a4f6e1ccbc5403c47a1840ca8bed243f1c16d18 Mon Sep 17 00:00:00 2001 From: Simon Taggart Date: Sun, 10 Dec 2023 19:15:35 -0800 Subject: [PATCH] test: add tests for the api routes (#3666) --- .../on_deployment_status_cypress.yml | 1 + .github/workflows/on_pull_request_cypress.yml | 1 + cypress.config.e2e.ts | 1 + cypress.config.ts | 1 + cypress/integration/api/ai.spec.ts | 11 +++++++ .../api/discussions-search.spec.ts | 8 +++++ cypress/integration/api/docs-search.spec.ts | 8 +++++ cypress/integration/api/og-image.spec.ts | 9 ++++++ .../api/paste-assistant-message.spec.ts | 31 +++++++++++++++++++ .../api/paste-assistant-thread.spec.ts | 29 +++++++++++++++++ 10 files changed, 100 insertions(+) create mode 100644 cypress/integration/api/ai.spec.ts create mode 100644 cypress/integration/api/discussions-search.spec.ts create mode 100644 cypress/integration/api/docs-search.spec.ts create mode 100644 cypress/integration/api/og-image.spec.ts create mode 100644 cypress/integration/api/paste-assistant-message.spec.ts create mode 100644 cypress/integration/api/paste-assistant-thread.spec.ts diff --git a/.github/workflows/on_deployment_status_cypress.yml b/.github/workflows/on_deployment_status_cypress.yml index d4e0f4ca75..b8a7807100 100644 --- a/.github/workflows/on_deployment_status_cypress.yml +++ b/.github/workflows/on_deployment_status_cypress.yml @@ -31,6 +31,7 @@ jobs: CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PERCY_TOKEN: ${{ secrets.PERCY_TOKEN }} + OPENAI_API_SECRET: ${{ secrets.OPENAI_API_SECRET }} USE_CYPRESS_VRT: false steps: diff --git a/.github/workflows/on_pull_request_cypress.yml b/.github/workflows/on_pull_request_cypress.yml index c51e3a0afc..c6699b79f0 100644 --- a/.github/workflows/on_pull_request_cypress.yml +++ b/.github/workflows/on_pull_request_cypress.yml @@ -35,6 +35,7 @@ jobs: AIRTABLE_APIKEY: ${{ secrets.AIRTABLE_APIKEY }} AIRTABLE_BASEID: ${{ secrets.AIRTABLE_BASEID }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + OPENAI_API_SECRET: ${{ secrets.OPENAI_API_SECRET }} CYPRESS_BASE_URL: http://localhost:3000 SUPABASE_KEY: ${{ secrets.SUPABASE_STAGING_KEY }} SUPABASE_URL: ${{ secrets.SUPABASE_STAGING_URL }} diff --git a/cypress.config.e2e.ts b/cypress.config.e2e.ts index 3387b87d2c..b94685eb37 100644 --- a/cypress.config.e2e.ts +++ b/cypress.config.e2e.ts @@ -8,6 +8,7 @@ export default defineConfig({ env: { USE_CYPRESS_VRT: process.env.USE_CYPRESS_VRT, CYPRESS_BASE_URL: process.env.CYPRESS_BASE_URL, + OPENAI_API_SECRET: process.env.OPENAI_API_SECRET, }, viewportWidth: 1440, viewportHeight: 1440, diff --git a/cypress.config.ts b/cypress.config.ts index 4e90104264..bc6caa6a35 100644 --- a/cypress.config.ts +++ b/cypress.config.ts @@ -8,6 +8,7 @@ export default defineConfig({ env: { USE_CYPRESS_VRT: process.env.USE_CYPRESS_VRT, CYPRESS_BASE_URL: process.env.CYPRESS_BASE_URL, + OPENAI_API_SECRET: process.env.OPENAI_API_SECRET, }, viewportWidth: 1440, viewportHeight: 1440, diff --git a/cypress/integration/api/ai.spec.ts b/cypress/integration/api/ai.spec.ts new file mode 100644 index 0000000000..19ad1998cc --- /dev/null +++ b/cypress/integration/api/ai.spec.ts @@ -0,0 +1,11 @@ +context("POST /api/ai", () => { + it("gets an ai response for a given prompty", () => { + cy.request("POST", "/api/ai", { + prompt: "How do I create a primary button", + secret: Cypress.env("OPENAI_API_SECRET"), + }).then((response) => { + expect(response.status).to.eq(200); + expect(response.body).length.to.be.greaterThan(1); + }); + }); +}); diff --git a/cypress/integration/api/discussions-search.spec.ts b/cypress/integration/api/discussions-search.spec.ts new file mode 100644 index 0000000000..177b0a60b3 --- /dev/null +++ b/cypress/integration/api/discussions-search.spec.ts @@ -0,0 +1,8 @@ +context("GET /api/discussions-search", () => { + it("gets a list of discussions", () => { + cy.request("POST", "/api/discussions-search", { prompt: "creating a button" }).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.data).length.to.be.greaterThan(1); + }); + }); +}); diff --git a/cypress/integration/api/docs-search.spec.ts b/cypress/integration/api/docs-search.spec.ts new file mode 100644 index 0000000000..02338fbd5f --- /dev/null +++ b/cypress/integration/api/docs-search.spec.ts @@ -0,0 +1,8 @@ +context("GET /api/docs-search", () => { + it("gets a list of docs", () => { + cy.request("POST", "/api/docs-search", { prompt: "creating a button" }).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.data).length.to.be.greaterThan(1); + }); + }); +}); diff --git a/cypress/integration/api/og-image.spec.ts b/cypress/integration/api/og-image.spec.ts new file mode 100644 index 0000000000..a0cb14537e --- /dev/null +++ b/cypress/integration/api/og-image.spec.ts @@ -0,0 +1,9 @@ +context("GET /api/og-image", () => { + it("gets an image", () => { + cy.request("GET", "/api/og-image?componentRequested=primitives/box").then((response) => { + expect(response.status).to.eq(200); + expect(response.headers["content-type"]).to.eq("image/png"); + console.log(response); + }); + }); +}); diff --git a/cypress/integration/api/paste-assistant-message.spec.ts b/cypress/integration/api/paste-assistant-message.spec.ts new file mode 100644 index 0000000000..e36e224ee1 --- /dev/null +++ b/cypress/integration/api/paste-assistant-message.spec.ts @@ -0,0 +1,31 @@ +context("POST /api/paste-assistant-message", () => { + let threadId: string; + + before(() => { + // create a thread for the message + cy.request("POST", "/api/paste-assistant-thread", {}).then((response) => { + threadId = response.body.id; + }); + }); + + after(() => { + // delete the thread + cy.request("DELETE", "/api/paste-assistant-thread", { id: threadId }); + }); + + it("creates an message on an ai thread", () => { + // create a message on the thread + cy.request("POST", "/api/paste-assistant-message", { threadId, message: "create a button" }).then((response) => { + expect(response.status).to.eq(200); + }); + }); + + it("gets messages on an ai thread", () => { + // get messages on the thread + cy.request("GET", `/api/paste-assistant-messages/${threadId}`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.data).length.to.be.greaterThan(0); + expect(response.body.data[0].content[0].text.value).to.eq("create a button"); + }); + }); +}); diff --git a/cypress/integration/api/paste-assistant-thread.spec.ts b/cypress/integration/api/paste-assistant-thread.spec.ts new file mode 100644 index 0000000000..9f288b7b38 --- /dev/null +++ b/cypress/integration/api/paste-assistant-thread.spec.ts @@ -0,0 +1,29 @@ +context("POST /api/paste-assistant-thread", () => { + let threadId: string; + it("creates an ai thread", () => { + cy.request("POST", "/api/paste-assistant-thread", {}).then((response) => { + expect(response.status).to.eq(200); + threadId = response.body.id; + }); + }); + it("updates an ai thread", () => { + cy.request("PUT", "/api/paste-assistant-thread", { id: threadId, metadata: { testKey: "testData" } }).then( + (response) => { + expect(response.status).to.eq(200); + expect(response.body.metadata.testKey).to.eq("testData"); + }, + ); + }); + it("gets an ai thread", () => { + cy.request("GET", `/api/paste-assistant-thread/${threadId}`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.metadata.testKey).to.eq("testData"); + }); + }); + it("deletes an ai thread", () => { + cy.request("DELETE", "/api/paste-assistant-thread", { id: threadId }).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.deleted).to.eq(true); + }); + }); +});