Skip to content

Commit

Permalink
fix: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sshivaditya committed Oct 1, 2024
1 parent 12a4725 commit 31773f4
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 88 deletions.
20 changes: 20 additions & 0 deletions tests/__mocks__/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ export function createMockAdapters(context: Context) {
return commentMap.get(commentNodeId);
}),
} as unknown as Comment,

issue: {
findSimilarIssues: jest.fn(async (issueContent: string) => {
if (issueContent && issueContent.length > 0) {
return [
{
node: {
title: "Test Title",
url: "https://www.github.com",
},
similarity: "0.95",
},
];
}
return [];
}),
},
fetchComments: jest.fn(async (issueId: string) => {
return Array.from(commentMap.values()).filter((comment) => comment.issue_id === issueId);
}),
},
voyage: {
embedding: {
Expand Down
4 changes: 3 additions & 1 deletion tests/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const db = factory({
deployments_url: String,
},
issue: {
id: primaryKey(Number),
node_id: primaryKey(String),
number: Number,
title: String,
body: String,
Expand All @@ -78,6 +78,7 @@ export const db = factory({
comments: Number,
labels: Array,
state: String,
closed: Boolean,
locked: Boolean,
assignee: nullable({
login: String,
Expand Down Expand Up @@ -136,6 +137,7 @@ export const db = factory({
login: String,
id: Number,
},
issue_id: String,
author_association: String,
html_url: String,
issue_url: String,
Expand Down
17 changes: 17 additions & 0 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { http, HttpResponse } from "msw";
import { db } from "./db";

/**
* Intercepts the routes and returns a custom payload
*/
Expand Down Expand Up @@ -48,6 +49,21 @@ export const handlers = [
item.body = body;
return HttpResponse.json(item);
}),
//Update issue
http.patch("https://api.github.com/repos/:owner/:repo/issues/:issue_number", async ({ params: { issue_number: issueNumber }, request }) => {
const { body } = await getValue(request.body);
const item = db.issue.findFirst({ where: { number: { equals: Number(issueNumber) } } });
if (!item) {
return new HttpResponse(null, { status: 404 });
}
item.body = body;
return HttpResponse.json(item);
}),

//Fetch comments for the issue
http.get("https://api.github.com/repos/:owner/:repo/issues/:issue_number/comments", ({ params: { issue_id: issueId } }) =>
HttpResponse.json(db.issueComments.findMany({ where: { issue_id: { equals: String(issueId) } } }))
),
];

async function getValue(body: ReadableStream<Uint8Array> | null) {
Expand All @@ -63,4 +79,5 @@ async function getValue(body: ReadableStream<Uint8Array> | null) {
}
}
}
return {};
}
82 changes: 80 additions & 2 deletions tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { db } from "./db";
import { STRINGS } from "./strings";
import usersGet from "./users-get.json";
import threshold95_1 from "../__sample__/match_threshold_95_1.json";
import threshold95_2 from "../__sample__/match_threshold_95_2.json";
import warning75_1 from "../__sample__/warning_threshold_75_1.json";
import warning75_2 from "../__sample__/warning_threshold_75_2.json";
import taskComplete from "../__sample__/task_complete.json";

interface SampleIssue {
title: string;
issue_body: string;
}

/**
* Helper function to setup tests.
Expand Down Expand Up @@ -34,7 +44,7 @@ export async function setupTests() {

// Insert issues
db.issue.create({
id: 1,
node_id: "1", //Node ID
number: 1,
title: "First Issue",
body: "This is the body of the first issue.",
Expand Down Expand Up @@ -65,7 +75,7 @@ export async function setupTests() {
});

db.issue.create({
id: 2,
node_id: "2", //Node ID
number: 2,
title: "Second Issue",
body: "This is the body of the second issue.",
Expand Down Expand Up @@ -96,6 +106,61 @@ export async function setupTests() {
});
}

export function createIssue(
issueBody: string,
issueNodeId: string,
issueTitle: string,
issueNumber: number,
issueUser: {
login: string;
id: number;
},
issueState: string,
issueCloseReason: string | null,
repo: string,
owner: string
) {
const existingIssue = db.issue.findFirst({
where: {
node_id: {
equals: issueNodeId,
},
},
});
if (existingIssue) {
db.issue.update({
where: {
node_id: {
equals: issueNodeId,
},
},
data: {
body: issueBody,
title: issueTitle,
user: issueUser,
updated_at: new Date().toISOString(),
owner: owner,
repo: repo,
},
});
} else {
db.issue.create({
node_id: issueNodeId,
body: issueBody,
title: issueTitle,
user: issueUser,
number: issueNumber,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
author_association: "OWNER",
state: issueState,
state_reason: issueCloseReason,
owner: owner,
repo: repo,
});
}
}

export function createComment(comment: string, commentId: number, nodeId: string) {
const existingComment = db.issueComments.findFirst({
where: {
Expand Down Expand Up @@ -133,3 +198,16 @@ export function createComment(comment: string, commentId: number, nodeId: string
});
}
}

export function fetchSimilarIssues(type?: string): SampleIssue[] {
switch (type) {
case "warning_threshold_75":
return [warning75_1, warning75_2];
case "match_threshold_95":
return [threshold95_1, threshold95_2];
case "task_complete":
return [taskComplete];
default:
return [threshold95_1, threshold95_2];
}
}
2 changes: 2 additions & 0 deletions tests/__mocks__/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export const STRINGS = {
CONFIGURABLE_RESPONSE: "Hello, Code Reviewers!",
INVALID_COMMAND: "/Goodbye",
COMMENT_DOES_NOT_EXIST: "Comment does not exist",
SIMILAR_ISSUE: "Similar issue",
SIMILAR_ISSUE_URL: "Related issue URL",
};
4 changes: 4 additions & 0 deletions tests/__sample__/match_threshold_95_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Bug: NullPointerException in Python API Handler",
"issue_body": "Title: Bug: NullPointerException in Python API Handler\nDescription: A NullPointerException occurs in the get_user function of the Python API handler when a null value is passed for user_id. Implement a null check to handle this scenario.\nSteps to Reproduce: Pass a null user_id to get_user. Observe the exception in the API logs.\nExpected Behavior: Null values should be handled without causing an exception.\nCurrent Behavior: The function throws a NullPointerException.\nAttachments: Stack trace log."
}
4 changes: 4 additions & 0 deletions tests/__sample__/match_threshold_95_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Bug: NullPointerException in Python API Handler",
"issue_body": "Description: The get_user function in the Python API handler throws a NullPointerException when the user_id is not provided. This needs a null check to prevent the exception and handle invalid user IDs.\n\nSteps to reproduce: Call the get_user function with a null user_id. Check the exception in the API logs.\n\nExpected behavior: NullPointerException should be avoided with proper null handling.\n\nCurrent behavior: Exception NullPointerException is thrown.\n\nAttachments: Exception trace log."
}
4 changes: 4 additions & 0 deletions tests/__sample__/task_complete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Review Cache Strategies for Java Service Layer to Handle High Load",
"issue_body": "Description: The Java service layer's performance deteriorates under high load, partly due to the absence of effective caching. Evaluate and implement advanced caching strategies to ensure the service layer performs optimally during peak usage.\nSteps to Reproduce:\nSimulate high load on the Java service layer.\nAnalyze performance metrics and identify bottlenecks.\nExpected Behavior: Service layer maintains stability and performance under high load with improved caching strategies.\nCurrent Behavior: Performance degradation and potential instability under high load.\nAttachments: N/A"
}
5 changes: 5 additions & 0 deletions tests/__sample__/warning_threshold_75_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Feature Request: Add Caching to Java Service Layer",
"issue_body": "Description: The Java service layer does not currently utilize caching, resulting in performance issues with frequent requests. Implement caching to improve performance for repeated queries.\nSteps to Reproduce: Make frequent requests to the Java service layer. Monitor performance and response times.\nExpected Behavior: Improved performance with caching enabled.\nCurrent Behavior: Slower response times due to lack of caching.\nAttachments: N/A"
}

4 changes: 4 additions & 0 deletions tests/__sample__/warning_threshold_75_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Review Cache Strategies for Java Service Layer to Handle High Load",
"issue_body": "Description: The Java service layer's performance deteriorates under high load, partly due to the absence of effective caching. Evaluate and implement advanced caching strategies to ensure the service layer performs optimally during peak usage.\nSteps to Reproduce:\nSimulate high load on the Java service layer.\nAnalyze performance metrics and identify bottlenecks.\nExpected Behavior: Service layer maintains stability and performance under high load with improved caching strategies.\nCurrent Behavior: Performance degradation and potential instability under high load.\nAttachments: N/A"
}
Loading

0 comments on commit 31773f4

Please sign in to comment.