Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Sep 4, 2024
1 parent 2be91f6 commit e32602a
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 57 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@octokit/webhooks": "13.2.8",
"@octokit/webhooks-types": "7.5.1",
"@sinclair/typebox": "0.32.35",
"@ubiquity-dao/ubiquibot-logger": "1.3.0",
"@ubiquity-dao/ubiquibot-logger": "1.3.1",
"dotenv": "16.4.5",
"hono": "4.4.13",
"smee-client": "2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function createPlugin<TConfig = unknown, TEnv = unknown, TSupported

const context: Context<TConfig, TEnv, TSupportedEvents> = {
eventName: payload.eventName,
payload: payload.payload,
payload: payload.eventPayload,
octokit: new customOctokit({ auth: payload.authToken }),
config: config,
env: env,
Expand Down
43 changes: 34 additions & 9 deletions tests/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getConfig } from "../src/github/utils/config";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import { getManifest } from "../src/github/utils/plugins";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";

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

Expand All @@ -23,16 +24,40 @@ afterAll(() => {

describe("Configuration tests", () => {
it("Should properly parse the Action path if a branch and workflow are specified", async () => {
function getContent() {
return {
data: `
plugins:
- uses:
- plugin: ubiquity/user-activity-watcher:compute.yml@fork/pull/1
with:
settings1: 'enabled'`,
};
function getContent(args: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) {
let data: string;
if (args.path === "manifest.json") {
data = `
{
"name": "plugin",
"commands": {
"command": {
"description": "description",
"ubiquity:example": "example"
}
}
}
`;
} else {
data = `
plugins:
- uses:
- plugin: ubiquity/user-activity-watcher:compute.yml@fork/pull/1
with:
settings1: 'enabled'`;
}

if (args.mediaType === undefined || args.mediaType?.format === "base64") {
return {
data: {
content: Buffer.from(data).toString("base64"),
},
};
} else if (args.mediaType?.format === "raw") {
return { data };
}
}

const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
Expand Down
93 changes: 47 additions & 46 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,36 +165,56 @@ describe("Worker tests", () => {
it("Should merge organization and repository configuration", async () => {
const workflowId = "compute.yml";
function getContent(args: RestEndpointMethodTypes["repos"]["getContent"]["parameters"]) {
if (args.repo !== "ubiquibot-config") {
let data: string;
if (args.path === "manifest.json") {
data = `
{
"name": "plugin",
"commands": {
"command": {
"description": "description",
"ubiquity:example": "example"
}
}
}
`;
} else if (args.repo !== "ubiquibot-config") {
data = `
plugins:
- uses:
- plugin: repo-3/plugin-3
with:
setting1: false
- uses:
- plugin: repo-1/plugin-1
with:
setting2: true`;
} else {
data = `
plugins:
- uses:
- plugin: uses-1/plugin-1
with:
settings1: 'enabled'
- uses:
- plugin: repo-1/plugin-1
with:
setting1: false
- uses:
- plugin: repo-2/plugin-2
with:
setting2: true`;
}

if (args.mediaType === undefined || args.mediaType?.format === "base64") {
return {
data: `
plugins:
- uses:
- plugin: repo-3/plugin-3
with:
setting1: false
- uses:
- plugin: repo-1/plugin-1
with:
setting2: true`,
data: {
content: Buffer.from(data).toString("base64"),
},
};
} else if (args.mediaType?.format === "raw") {
return { data };
}
return {
data: `
plugins:
- uses:
- plugin: uses-1/plugin-1
with:
settings1: 'enabled'
- uses:
- plugin: repo-1/plugin-1
with:
setting1: false
- uses:
- plugin: repo-2/plugin-2
with:
setting2: true`,
};
}
const cfg = await getConfig({
key: issueOpened,
Expand All @@ -207,25 +227,6 @@ plugins:
},
} as unknown as GitHubContext<"issues.closed">["payload"],
octokit: {
repos: {
getContent() {
return {
data: {
content: Buffer.from(
JSON.stringify({
name: "plugin",
commands: {
command: {
description: "description",
"ubiquity:example": "example",
},
},
})
).toString("base64"),
},
};
},
},
rest: {
repos: {
getContent,
Expand Down
43 changes: 43 additions & 0 deletions tests/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ describe("SDK worker tests", () => {
expect(res.status).toEqual(400);
});
it("Should handle thrown errors", async () => {
const createComment = jest.fn();
jest.mock("../src/sdk/octokit", () => ({
customOctokit: class MockOctokit {
constructor() {
return {
rest: {
issues: {
createComment,
},
},
};
}
},
}));

const { createPlugin } = await import("../src/sdk/server");
const app = await createPlugin(
async (context: Context<{ shouldFail: boolean }>) => {
if (context.config.shouldFail) {
throw context.logger.error("test error");
}
return {
success: true,
event: context.eventName,
};
},
{ name: "test" },
{ kernelPublicKey: publicKey }
);

const data = {
...issueCommented,
stateId: "stateId",
Expand All @@ -108,6 +138,19 @@ describe("SDK worker tests", () => {
method: "POST",
});
expect(res.status).toEqual(500);
expect(createComment).toHaveBeenCalledWith({
issue_number: 5,
owner: "ubiquibot",
repo: "bot",
body: `\`\`\`diff
! test error
\`\`\`
<!--
{
"caller": "error"
}
-->`,
});
});
it("Should accept correct request", async () => {
const data = {
Expand Down

0 comments on commit e32602a

Please sign in to comment.