Skip to content

Commit

Permalink
feat: make createPlugin sync function
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Oct 25, 2024
1 parent 6941ff8 commit ff36f56
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/sdk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const inputSchema = T.Object({
signature: T.String(),
});

export async function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>(
export function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>(
handler: (context: Context<TConfig, TEnv, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>,
manifest: Manifest,
options?: Options
Expand Down
54 changes: 28 additions & 26 deletions tests/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { expect, describe, beforeAll, afterAll, afterEach, it, jest } from "@jes

import * as crypto from "crypto";
import { createPlugin } from "../src/sdk/server";
import { Hono } from "hono";
import { Context } from "../src/sdk/context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import { CloudflareKv } from "../src/github/utils/cloudflare-kv";
Expand All @@ -28,6 +27,10 @@ const issueCommentedEvent = {
eventPayload: issueCommented.eventPayload,
};

const sdkOctokitImportPath = "../src/sdk/octokit";
const githubActionImportPath = "@actions/github";
const githubCoreImportPath = "@actions/core";

const eventHandler = new GitHubEventHandler({
environment: "production",
webhookSecret: "test",
Expand All @@ -36,22 +39,21 @@ const eventHandler = new GitHubEventHandler({
pluginChainState: undefined as unknown as CloudflareKv<PluginChainState>,
});

let app: Hono;
const app = 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 }
);

beforeAll(async () => {
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 }
);
server.listen();
});

Expand Down Expand Up @@ -98,7 +100,7 @@ describe("SDK worker tests", () => {
});
it("Should handle thrown errors", async () => {
const createComment = jest.fn();
jest.mock("../src/sdk/octokit", () => ({
jest.mock(sdkOctokitImportPath, () => ({
customOctokit: class MockOctokit {
constructor() {
return {
Expand All @@ -113,7 +115,7 @@ describe("SDK worker tests", () => {
}));

const { createPlugin } = await import("../src/sdk/server");
const app = await createPlugin(
const app = createPlugin(
async (context: Context<{ shouldFail: boolean }>) => {
if (context.config.shouldFail) {
throw context.logger.error("test error");
Expand Down Expand Up @@ -177,7 +179,7 @@ describe("SDK actions tests", () => {
it("Should accept correct request", async () => {
const inputs = new PluginInput(eventHandler, "stateId", issueCommentedEvent.eventName, issueCommentedEvent.eventPayload, {}, "test_token", "");
const githubInputs = await inputs.getWorkflowInputs();
jest.mock("@actions/github", () => ({
jest.mock(githubActionImportPath, () => ({
context: {
runId: "1",
payload: {
Expand All @@ -188,7 +190,7 @@ describe("SDK actions tests", () => {
}));
const setOutput = jest.fn();
const setFailed = jest.fn();
jest.mock("@actions/core", () => ({
jest.mock(githubCoreImportPath, () => ({
setOutput,
setFailed,
}));
Expand Down Expand Up @@ -248,7 +250,7 @@ describe("SDK actions tests", () => {
}));
const setOutput = jest.fn();
const setFailed = jest.fn();
jest.mock("@actions/core", () => ({
jest.mock(githubCoreImportPath, () => ({
setOutput,
setFailed,
}));
Expand All @@ -271,7 +273,7 @@ describe("SDK actions tests", () => {
const inputs = new PluginInput(eventHandler, "stateId", issueCommentedEvent.eventName, issueCommentedEvent.eventPayload, {}, "test_token", "");
const githubInputs = await inputs.getWorkflowInputs();

jest.mock("@actions/github", () => ({
jest.mock(githubActionImportPath, () => ({
context: {
runId: "1",
payload: {
Expand All @@ -291,18 +293,18 @@ describe("SDK actions tests", () => {
}));
const setOutput = jest.fn();
const setFailed = jest.fn();
jest.mock("@actions/core", () => ({
jest.mock(githubCoreImportPath, () => ({
setOutput,
setFailed,
}));
const createDispatchEvent = jest.fn();
jest.mock("../src/sdk/octokit", () => ({
const createDispatchEventFn = jest.fn();
jest.mock(sdkOctokitImportPath, () => ({
customOctokit: class MockOctokit {
constructor() {
return {
rest: {
repos: {
createDispatchEvent: createDispatchEvent,
createDispatchEvent: createDispatchEventFn,
},
},
};
Expand All @@ -323,7 +325,7 @@ describe("SDK actions tests", () => {
);
expect(setFailed).not.toHaveBeenCalled();
expect(setOutput).toHaveBeenCalledWith("result", { event: issueCommentedEvent.eventName });
expect(createDispatchEvent).toHaveBeenCalledWith({
expect(createDispatchEventFn).toHaveBeenCalledWith({
event_type: "return-data-to-ubiquity-os-kernel",
owner: repo.owner,
repo: repo.repo,
Expand Down

0 comments on commit ff36f56

Please sign in to comment.