diff --git a/.github/workflows/bun-testing.yml b/.github/workflows/bun-testing.yml index 0b472d3..be055d0 100644 --- a/.github/workflows/bun-testing.yml +++ b/.github/workflows/bun-testing.yml @@ -6,12 +6,16 @@ on: env: NODE_ENV: "test" + WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }} + APP_ID: ${{ secrets.APP_ID }} + PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} jobs: testing: permissions: write-all runs-on: ubuntu-latest steps: + - uses: oven-sh/setup-bun@v1 - uses: actions/setup-node@v4 with: node-version: '20.10.0' diff --git a/bun.lockb b/bun.lockb old mode 100755 new mode 100644 index c0d11df..eaaf7f1 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5f6cbd6..ed44f40 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "worker": "wrangler dev --env dev --port 8787", "proxy": "tsx src/proxy.ts", "knip": "knip --config .github/knip.ts", - "knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts" + "knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts", + "test": "bun test" }, "keywords": [ "typescript", diff --git a/src/github/utils/config.ts b/src/github/utils/config.ts index f1d0584..6de223c 100644 --- a/src/github/utils/config.ts +++ b/src/github/utils/config.ts @@ -1,14 +1,14 @@ import { Value } from "@sinclair/typebox/value"; -import { GitHubContext } from "../github-context"; +import { generateConfiguration } from "@ubiquibot/configuration"; import YAML from "yaml"; +import { GitHubContext } from "../github-context"; import { expressionRegex } from "../types/plugin"; import { configSchema, PluginConfiguration } from "../types/plugin-configuration"; import { eventNames } from "../types/webhook-events"; -import { BotConfig, generateConfiguration } from "@ubiquibot/configuration"; const UBIQUIBOT_CONFIG_FULL_PATH = ".github/.ubiquibot-config.yml"; -export async function getConfig(context: GitHubContext): Promise { +export async function getConfig(context: GitHubContext): Promise { const payload = context.payload; const defaultConfiguration = generateConfiguration(); if (!("repository" in payload) || !payload.repository) { @@ -29,13 +29,13 @@ export async function getConfig(context: GitHubContext): Promise ({ Webhooks: WebhooksMocked, })); +const issueOpened = "issues.opened"; + class WebhooksMocked { constructor(_: unknown) {} verifyAndReceive(_: unknown) { @@ -21,6 +23,9 @@ class WebhooksMocked { } 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"; @@ -53,7 +58,7 @@ describe("Worker tests", () => { it("Should start a worker", async () => { const req = new Request("http://localhost:8080", { headers: { - "x-github-event": "issues.opened", + "x-github-event": issueOpened, "x-github-delivery": "1", "x-hub-signature-256": "123456", }, @@ -66,4 +71,73 @@ describe("Worker tests", () => { }); expect(res.status).toEqual(200); }); + + describe("Configuration tests", () => { + it("Should generate a default configuration when no repo is defined", async () => { + const cfg = await getConfig({ + key: issueOpened, + name: issueOpened, + id: "", + payload: { + repository: "", + }, + octokit: {}, + eventHandler: {} as GitHubEventHandler, + } as unknown as GitHubContext); + expect(cfg).toBeTruthy(); + }); + it("Should generate a default configuration when the target repo does not contain one", async () => { + const cfg = await getConfig({ + key: issueOpened, + name: issueOpened, + id: "", + payload: { + repository: { + owner: { login: "ubiquity" }, + name: "ubiquibot-kernel", + }, + } as unknown as GitHubContext<"issues.closed">["payload"], + octokit: { + rest: { + repos: { + getContent() { + return { data: null }; + }, + }, + }, + }, + eventHandler: {} as GitHubEventHandler, + } as unknown as GitHubContext); + expect(cfg).toBeTruthy(); + }); + it("Should merge the configuration when found", 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: ` +incentives: + enabled: false`, + }; + }, + }, + }, + }, + eventHandler: {} as GitHubEventHandler, + } as unknown as GitHubContext); + expect(cfg).toBeTruthy(); + expect(cfg.incentives.enabled).toBeFalse(); + }); + }); });