From e32602a1f357d093a2a7471796fbe54ae49176e5 Mon Sep 17 00:00:00 2001 From: whilefoo Date: Wed, 4 Sep 2024 13:18:09 +0200 Subject: [PATCH] fix: tests --- bun.lockb | Bin 434290 -> 434290 bytes package.json | 2 +- src/sdk/server.ts | 2 +- tests/configuration.test.ts | 43 +++++++++++++---- tests/main.test.ts | 93 ++++++++++++++++++------------------ tests/sdk.test.ts | 43 +++++++++++++++++ 6 files changed, 126 insertions(+), 57 deletions(-) diff --git a/bun.lockb b/bun.lockb index 4d6ba3fa512f6764746aaa28ed263a9e2fc8f667..0193fd3154c251785c36242dc151ddc9d49ee798 100755 GIT binary patch delta 160 zcmV;R0AK&|z#8(v8jvm^08{^{0Z3H0v@jfwpv8zFy{cUoM$7Es8pN~J6v|bH1pdrO&@)A-NwFeBdu}+Fh zlh{l!g9uEw2uuN8RX~aP9qsUTIK = { eventName: payload.eventName, - payload: payload.payload, + payload: payload.eventPayload, octokit: new customOctokit({ auth: payload.authToken }), config: config, env: env, diff --git a/tests/configuration.test.ts b/tests/configuration.test.ts index 582872b..f238fc2 100644 --- a/tests/configuration.test.ts +++ b/tests/configuration.test.ts @@ -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" }); @@ -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, diff --git a/tests/main.test.ts b/tests/main.test.ts index fa5ad59..85ab398 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -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, @@ -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, diff --git a/tests/sdk.test.ts b/tests/sdk.test.ts index 20fe0aa..8cc7df5 100644 --- a/tests/sdk.test.ts +++ b/tests/sdk.test.ts @@ -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", @@ -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 +\`\`\` +`, + }); }); it("Should accept correct request", async () => { const data = {