Skip to content

Commit

Permalink
test: add push event handling tests and mock reset cleanup
Browse files Browse the repository at this point in the history
Add new tests for push event handling and ensure mock reset in other tests.
  • Loading branch information
gentlementlegen committed Oct 19, 2024
1 parent bbd30c3 commit 75ce51a
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 114 deletions.
1 change: 1 addition & 0 deletions tests/dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ describe("handleEvent", () => {
expect(res).toBeTruthy();
// 2 calls means the execution didn't break
expect(dispatchWorker).toHaveBeenCalledTimes(2);
dispatchWorker.mockReset();
});
});
148 changes: 34 additions & 114 deletions tests/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { http, HttpResponse } from "msw";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import issueCommentCreated from "../src/github/handlers/issue-comment-created";
import handlePushEvent from "../src/github/handlers/push-event";
import { CONFIG_FULL_PATH } from "../src/github/utils/config";
import { server } from "./__mocks__/node";
import "./__mocks__/webhooks";
Expand All @@ -25,6 +24,8 @@ beforeAll(() => {
});
afterEach(() => {
server.resetHandlers();
jest.clearAllMocks();
jest.resetAllMocks();
});
afterAll(() => {
server.close();
Expand Down Expand Up @@ -61,17 +62,10 @@ describe("Event related tests", () => {
},
};
const spy = jest.spyOn(issues, "createComment");
await issueCommentCreated({
id: "",
key: "issue_comment.created",
octokit: {
rest: {
issues,
repos: {
getContent(params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) {
if (params?.path === CONFIG_FULL_PATH) {
return {
data: `
const getContent = jest.fn((params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) => {
if (params?.path === CONFIG_FULL_PATH) {
return {
data: `
plugins:
- name: "Run on comment created"
uses:
Expand All @@ -82,27 +76,35 @@ describe("Event related tests", () => {
- id: plugin-B
plugin: ubiquity-os/plugin-b
`,
};
} else if (params?.path === "manifest.json") {
return {
data: {
content: btoa(
JSON.stringify({
name: "plugin",
commands: {
action: {
description: "action",
"ubiquity:example": "/action",
},
},
})
),
};
} else if (params?.path === "manifest.json") {
return {
data: {
content: btoa(
JSON.stringify({
name: "plugin",
commands: {
action: {
description: "action",
"ubiquity:example": "/action",
},
};
} else {
throw new Error("Not found");
}
},
},
})
),
},
};
} else {
throw new Error("Not found");
}
});
await issueCommentCreated({
id: "",
key: "issue_comment.created",
octokit: {
rest: {
issues,
repos: {
getContent: getContent,
},
},
},
Expand Down Expand Up @@ -132,86 +134,4 @@ describe("Event related tests", () => {
],
]);
});
it("should handle push event correctly", async () => {
const issues = {
createComment(params?: RestEndpointMethodTypes["issues"]["createComment"]["parameters"]) {
return params;
},
};
const createCommitComment = jest.fn();
const context = {
id: "",
key: "issue_comment.created",
octokit: {
rest: {
issues,
repos: {
listCommentsForCommit: jest.fn(() => ({ data: [] })),
createCommitComment: createCommitComment,
getContent(params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) {
if (params?.path === CONFIG_FULL_PATH) {
return {
data: `
plugins:
- name: "Run on comment created"
uses:
- id: plugin-A
plugin: https://plugin-a.internal
with:
arg: "true"
- name: "Some Action plugin"
uses:
- id: plugin-B
plugin: ubiquity-os/plugin-b
`,
};
} else if (params?.path === "manifest.json") {
return {
data: {
content: btoa(
JSON.stringify({
name: "plugin",
commands: {
action: {
description: "action",
"ubiquity:example": "/action",
},
},
configuration: {
default: {},
type: "object",
properties: {
arg: {
type: "number",
},
},
required: ["arg"],
},
})
),
},
};
} else {
throw new Error("Not found");
}
},
},
},
},
eventHandler: eventHandler,
payload: {
repository: {
owner: { login: "ubiquity" },
name,
},
issue: { number: 1 },
comment: {
body: "/help",
},
commits: [{ modified: [CONFIG_FULL_PATH] }],
} as unknown as GitHubContext<"issue_comment.created">["payload"],
} as unknown as GitHubContext;
await expect(handlePushEvent(context)).resolves.not.toThrow();
expect(createCommitComment).toBeCalledTimes(1);
});
});
140 changes: 140 additions & 0 deletions tests/push.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, jest } from "@jest/globals";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
import { config } from "dotenv";
import { http, HttpResponse } from "msw";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import handlePushEvent from "../src/github/handlers/push-event";
import { CONFIG_FULL_PATH } from "../src/github/utils/config";
import { server } from "./__mocks__/node";
import "./__mocks__/webhooks";

jest.mock("@octokit/plugin-paginate-rest", () => ({}));
jest.mock("@octokit/plugin-rest-endpoint-methods", () => ({}));
jest.mock("@octokit/plugin-retry", () => ({}));
jest.mock("@octokit/plugin-throttling", () => ({}));
jest.mock("@octokit/auth-app", () => ({}));

config({ path: ".dev.vars" });

const name = "ubiquity-os-kernel";

beforeAll(() => {
server.listen();
});
afterEach(() => {
server.resetHandlers();
jest.clearAllMocks();
jest.resetAllMocks();
});
afterAll(() => {
server.close();
});

const eventHandler = {
environment: "production",
} as GitHubEventHandler;

describe("Push related tests", () => {
beforeEach(() => {
server.use(
http.get("https://plugin-a.internal/manifest.json", () =>
HttpResponse.json({
name: "plugin",
commands: {
foo: {
description: "foo command",
"ubiquity:example": "/foo bar",
},
bar: {
description: "bar command",
"ubiquity:example": "/bar foo",
},
},
})
)
);
});
it("should handle push event correctly", async () => {
const issues = {
createComment(params?: RestEndpointMethodTypes["issues"]["createComment"]["parameters"]) {
return params;
},
};
const createCommitComment = jest.fn();
const context = {
id: "",
key: "issue_comment.created",
octokit: {
rest: {
issues,
repos: {
listCommentsForCommit: jest.fn(() => ({ data: [] })),
createCommitComment: createCommitComment,
getContent(params?: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) {
if (params?.path === CONFIG_FULL_PATH) {
return {
data: `
plugins:
- name: "Run on comment created"
uses:
- id: plugin-A
plugin: https://plugin-a.internal
with:
arg: "true"
- name: "Some Action plugin"
uses:
- id: plugin-B
plugin: ubiquity-os/plugin-b
`,
};
} else if (params?.path === "manifest.json") {
return {
data: {
content: btoa(
JSON.stringify({
name: "plugin",
commands: {
action: {
description: "action",
"ubiquity:example": "/action",
},
},
configuration: {
default: {},
type: "object",
properties: {
arg: {
type: "number",
},
},
required: ["arg"],
},
})
),
},
};
} else {
throw new Error("Not found");
}
},
},
},
},
eventHandler: eventHandler,
payload: {
repository: {
owner: { login: "ubiquity" },
name,
},
issue: { number: 1 },
comment: {
body: "/help",
},
commits: [{ modified: [CONFIG_FULL_PATH] }],
} as unknown as GitHubContext<"issue_comment.created">["payload"],
} as unknown as GitHubContext;
await expect(handlePushEvent(context)).resolves.not.toThrow();
expect(createCommitComment).toBeCalledTimes(1);
});
});

0 comments on commit 75ce51a

Please sign in to comment.