diff --git a/test/index.test.ts b/test/index.test.ts index d37b7df..98063ec 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,62 +1,92 @@ -// TODO - write tests for your application. -// You can import your modules -// import index from '../src/index' - +// Import necessary modules and mock utilities +import { Probot, ProbotOctokit } from "probot"; import nock from "nock"; -// Requiring our app implementation import myProbotApp from "../src"; -import { Probot, ProbotOctokit } from "probot"; -// Requiring our fixtures import payload from "./fixtures/issues.opened.json"; -const issueCreatedBody = { body: "Thanks for opening this issue!" }; + +// Mock utilities and fixtures const fs = require("fs"); const path = require("path"); +const privateKey = fs.readFileSync(path.join(__dirname, "fixtures/mock-cert.pem"), "utf-8"); -const privateKey = fs.readFileSync( - path.join(__dirname, "fixtures/mock-cert.pem"), - "utf-8" -); +// Mock responses and external services +const mockOpenAIResponse = { + choices: [ + { + message: { + content: JSON.stringify({ + labels: ["duplicate"], + content: "This issue is a duplicate.", + }), + }, + }, + ], +}; +const mockEmbeddingResponse = { + data: [ + { + embedding: [/* 1536-dimensional vector */], + }, + ], +}; +const mockSupabaseResponse = [ + { + id: 1, + content: "Mock issue content", + metadata: { + issue_number: 1, + issue_id: 1, + repo_id: 123, + }, + similarity: 0.9, // High similarity score indicating a duplicate + }, +]; -describe("My Probot app", () => { - let probot: any; +describe("Repo Assistant AI", () => { + let probot; beforeEach(() => { nock.disableNetConnect(); probot = new Probot({ appId: 123, - privateKey, - // disable request throttling and retries for testing + privateKey: privateKey, Octokit: ProbotOctokit.defaults({ retry: { enabled: false }, throttle: { enabled: false }, }), }); - // Load our app into probot probot.load(myProbotApp); }); - test("creates a comment when an issue is opened", async () => { - const mock = nock("https://api.github.com") - // Test that we correctly return a test token + test("saves an issue in the database and checks for duplicates", async () => { + // Mock GitHub API for authentication and comment creation + const mockGitHub = nock("https://api.github.com") .post("/app/installations/2/access_tokens") - .reply(200, { - token: "test", - permissions: { - issues: "write", - }, - }) - - // Test that a comment is posted - .post("/repos/hiimbex/testing-things/issues/1/comments", (body: any) => { - expect(body).toMatchObject(issueCreatedBody); - return true; - }) + .reply(200, { token: "test" }) + .post("/repos/owner/repo/issues/1/comments") + .reply(200) + .post("/repos/owner/repo/issues/1/labels") .reply(200); - // Receive a webhook event - await probot.receive({ name: "issues", payload }); + // Mock OpenAI API for embeddings and chat completions + const mockOpenAI = nock("https://api.openai.com") + .post("/v1/embeddings") + .reply(200, mockEmbeddingResponse) + .post("/v1/chat/completions") + .reply(200, mockOpenAIResponse); - expect(mock.pendingMocks()).toStrictEqual([]); + // Mock Supabase API for the RPC call + const mockSupabase = nock("https://your-supabase-url") + .post("/rest/v1/rpc/match_documents") + .reply(200, mockSupabaseResponse); + + // Simulate receiving a webhook event + await probot.receive({ name: "issues", payload: payload }); + + // Assertions to ensure the mocks have been called + expect(mockGitHub.isDone()).toBeTruthy(); + expect(mockOpenAI.isDone()).toBeTruthy(); + expect(mockSupabase.isDone()).toBeTruthy(); }); afterEach(() => { @@ -64,12 +94,3 @@ describe("My Probot app", () => { nock.enableNetConnect(); }); }); - -// For more information about testing with Jest see: -// https://facebook.github.io/jest/ - -// For more information about using TypeScript in your tests, Jest recommends: -// https://github.com/kulshekhar/ts-jest - -// For more information about testing with Nock see: -// https://github.com/nock/nock