Skip to content

Adding new set of test #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 65 additions & 44 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,96 @@
// 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(() => {
nock.cleanAll();
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