Skip to content

Commit

Permalink
chore: search api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Aug 12, 2024
1 parent f88a819 commit 60b2c59
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/handlers/collect-linked-pulls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IssuesSearch } from "../types/github-types";
export type IssueParams = ReturnType<typeof parseIssueUrl>;

function additionalBooleanFilters(issueNumber: number) {
return `linked:${issueNumber} in:body "closes #${issueNumber}" OR "closes #${issueNumber}" OR "fixes #${issueNumber}" OR "fix #${issueNumber}" OR "resolves #${issueNumber}"`;
return `linked:${issueNumber} in:body "closes #${issueNumber}" OR "Closes #${issueNumber}" OR "fixes #${issueNumber}" OR "Fixes #${issueNumber}" OR "fix #${issueNumber}" OR "resolves #${issueNumber}" OR "Resolves #${issueNumber}"`;
}

export async function collectLinkedPullRequests(context: Context, issue: IssueParams) {
Expand Down
3 changes: 3 additions & 0 deletions tests/__mocks__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const db = factory({
url: String,
user: nullable(Object),
milestone: nullable(Object),
pull_request: {
html_url: String,
},
assignee: nullable({
avatar_url: String,
email: nullable(String),
Expand Down
21 changes: 21 additions & 0 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,25 @@ export const handlers = [
db.issue.update({ where: { owner: { login: { equals: owner as string } }, repo: { equals: repo as string }, id: { equals: Number(id) } }, data: { assignees: [] } });
return HttpResponse.json({ message: "Assignees removed" });
}),
http.get("https://api.github.com/search/issues", ({ request }) => {
const query = new URL(request.url);
const params = query.searchParams;
const q = params.get("q");

if (!q) {
return HttpResponse.json({ message: "No query" });
}

const issueNumber = q.match(/#(\d+)/)?.[1];
if (!issueNumber) {
return HttpResponse.json({ message: "No issue number" });
}

const issue = db.issue.findFirst({ where: { number: { equals: Number(issueNumber) } } });
if (!issue) {
return HttpResponse.json({ message: "Issue not found" });
}

return HttpResponse.json(issue);
})
];
3 changes: 3 additions & 0 deletions tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function createIssue(
assignees: { login: string, id: number }[],
owner?: string,
created_at?: string,
body?: string,
repo?: string,
) {
db.issue.create({
Expand All @@ -43,5 +44,7 @@ export function createIssue(
owner: { login: owner || STRINGS.UBIQUITY },
number: id,
repo: repo || "test-repo",
body: body || "test",
pull_request: { html_url: `https://github.com/ubiquity/test-repo/pulls/${id}` },
});
}
22 changes: 6 additions & 16 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,8 @@ describe("User start/stop", () => {
const issue = db.issue.findFirst({ where: { id: { equals: 1 } } });
const result = await collectLinkedPullRequests(context, { issue_number: issue?.number as number, repo: issue?.repo as string, owner: issue?.owner.login as string });
expect(result).toHaveLength(1);
expect(result[0].source.issue.number).toEqual(1);
expect(result[0].source.issue.body).toMatch(/Resolves #1/g);
});

it("Should handle collecting linked PR with URL format", async () => {
db.issue.update({ where: { id: { equals: 2 } }, data: { repo: "user-activity-watcher", owner: { login: STRINGS.UBIQUIBOT } } });
const context = createContext(2, 1);
const issue = db.issue.findFirst({ where: { id: { equals: 2 } } });
const result = await collectLinkedPullRequests(context, { issue_number: issue?.number as number, repo: issue?.repo as string, owner: issue?.owner.login as string });
expect(result).toHaveLength(1);
expect(result[0].source.issue.number).toEqual(2);
expect(result[0].source.issue.body).toMatch(/Closes https:\/\/github.com\/ubiquibot\/user-activity-watcher\/issues\/2/);
expect(result[0].number).toEqual(1);
expect(result[0].body).toMatch(/Resolves #1/gi);
});
});

Expand All @@ -186,13 +176,13 @@ async function setupTests() {
createRepo(STRINGS.FILLER_REPO_NAME, 4);
createRepo(STRINGS.UBIQUIBOT, 5, STRINGS.UBIQUIBOT);

createIssue(1, []);
createIssue(1, [], STRINGS.UBIQUITY, daysPriorToNow(1), "resolves #1");
// nothing to do
createIssue(2, [{ login: STRINGS.USER, id: 2 }], STRINGS.UBIQUITY, daysPriorToNow(1));
createIssue(2, [{ login: STRINGS.USER, id: 2 }], STRINGS.UBIQUITY, daysPriorToNow(1), "resolves #1");
// warning
createIssue(3, [{ login: STRINGS.USER, id: 2 }], STRINGS.UBIQUITY, daysPriorToNow(4));
createIssue(3, [{ login: STRINGS.USER, id: 2 }], STRINGS.UBIQUITY, daysPriorToNow(4), "fixes #2");
// disqualification
createIssue(4, [{ login: STRINGS.USER, id: 2 }], STRINGS.UBIQUITY, daysPriorToNow(12));
createIssue(4, [{ login: STRINGS.USER, id: 2 }], STRINGS.UBIQUITY, daysPriorToNow(12), "closes #3");

createComment(1, 1, STRINGS.UBIQUITY);
createComment(2, 2, STRINGS.UBIQUITY);
Expand Down

0 comments on commit 60b2c59

Please sign in to comment.