Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added test types and test for configuration #55

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install bun
run: npm i -g bun
- uses: oven-sh/setup-bun@v1
- name: Install dependencies
run: bun install
- name: Run typecheck
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@cspell/dict-software-terms": "^3.3.18",
"@cspell/dict-typescript": "^3.1.2",
"@mswjs/data": "0.16.1",
"@types/bun": "1.1.3",
"@types/node": "^20.11.19",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/github/github-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class GitHubEventHandler {

return await crypto.subtle.importKey(
"pkcs8",
binaryDer.buffer,
binaryDer.buffer as ArrayBuffer,
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
Expand Down
15 changes: 15 additions & 0 deletions tests/__mocks__/webhooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/naming-convention */

export class WebhooksMocked {
constructor(_: unknown) {}
verifyAndReceive(_: unknown) {
return Promise.resolve();
}
onAny(_: unknown) {}
on(_: unknown) {}
onError(_: unknown) {}
removeListener(_: unknown, __: unknown) {}
sign(_: unknown) {}
verify(_: unknown, __: unknown) {}
receive(_: unknown) {}
}
79 changes: 79 additions & 0 deletions tests/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { afterAll, afterEach, beforeAll, describe, expect, it, mock } from "bun:test";
import { config } from "dotenv";
import { server } from "./__mocks__/node";
import { WebhooksMocked } from "./__mocks__/webhooks";
import { getConfig } from "../src/github/utils/config";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";

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

mock.module("@octokit/webhooks", () => ({
Webhooks: WebhooksMocked,
}));

const issueOpened = "issues.opened";

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

describe("Configuration tests", () => {
it("Should properly parse the Action path if a branch and workflow are specified", async () => {
const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
id: "",
payload: {
repository: {
owner: { login: "ubiquity" },
name: "conversation-rewards",
},
} as unknown as GitHubContext<"issues.closed">["payload"],
octokit: {
rest: {
repos: {
getContent() {
return {
data: `
plugins:
'issues.labeled':
- uses:
- plugin: ubiquity/user-activity-watcher:compute.yml@pull/1
type: github
with:
settings1: 'enabled'`,
};
},
},
},
},
eventHandler: {} as GitHubEventHandler,
} as unknown as GitHubContext);
expect(cfg.plugins["issues.labeled"]).toEqual([
{
uses: [
{
plugin: {
owner: "ubiquity",
repo: "user-activity-watcher",
workflowId: "compute.yml",
ref: "pull/1",
},
type: "github",
with: {
settings1: "enabled",
},
},
],
skipBotEvents: true,
},
]);
});
});
19 changes: 2 additions & 17 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
/* eslint-disable @typescript-eslint/naming-convention */
// @ts-expect-error package name is correct, TypeScript doesn't recognize it
import { afterAll, afterEach, beforeAll, describe, expect, it, jest, mock, spyOn } from "bun:test";
import { config } from "dotenv";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import { getConfig } from "../src/github/utils/config";
import worker from "../src/worker";
import { server } from "./__mocks__/node";
import { WebhooksMocked } from "./__mocks__/webhooks";

mock.module("@octokit/webhooks", () => ({
void mock.module("@octokit/webhooks", () => ({
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
Webhooks: WebhooksMocked,
}));

const issueOpened = "issues.opened";

class WebhooksMocked {
constructor(_: unknown) {}
verifyAndReceive(_: unknown) {
return Promise.resolve();
}
onAny(_: unknown) {}
on(_: unknown) {}
onError(_: unknown) {}
removeListener(_: unknown, __: unknown) {}
sign(_: unknown) {}
verify(_: unknown, __: unknown) {}
receive(_: unknown) {}
}

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

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": ["@cloudflare/workers-types/2023-07-01"] /* Specify type package names to be included without being referenced in a source file. */,
"types": ["@cloudflare/workers-types/2023-07-01", "bun"] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true /* Enable importing .json files */,
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
Expand Down
Loading