Files
GCCWechatBot/tests/githubEvents.test.ts

158 lines
4.9 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { formatGitHubEvent, getOrganizationLogin } from "../src/githubEvents.js";
test("formats push events with commit summaries", () => {
const message = formatGitHubEvent(
"push",
{
ref: "refs/heads/main",
repository: {
full_name: "acme/app",
owner: { login: "acme" }
},
pusher: { name: "alice" },
commits: [
{
id: "1234567890",
message: "Add feature\n\nLong body",
author: { name: "Alice" },
added: ["src/new.ts"],
modified: ["src/app.ts", "README.md"],
removed: ["old.txt"],
url: "https://github.com/acme/app/commit/1234567"
}
],
compare: "https://github.com/acme/app/compare/a...b"
},
3500
);
assert.ok(message);
assert.match(message, /^━━━━━━━━━━━━━━\n📦 GitHub-GCC Push/);
assert.match(message, /📁 acme\/app/);
assert.match(message, /🌿 main/);
assert.match(message, /👤 alice/);
assert.match(message, /• 1 commit/);
assert.match(message, /• 4 files changed/);
assert.match(message, /• latest: 1234567 Add feature/);
assert.doesNotMatch(message, /https:\/\/github\.com\/acme\/app\/commit\/1234567/);
assert.match(message, /🔗 https:\/\/github\.com\/acme\/app\/compare\/a\.\.\.b/);
assert.match(message, /━━━━━━━━━━━━━━$/);
});
test("formats merged pull requests as merged", () => {
const message = formatGitHubEvent(
"pull_request",
{
action: "closed",
repository: { full_name: "acme/app" },
sender: { login: "bob" },
pull_request: {
merged: true,
number: 12,
title: "Improve deployment",
base: { ref: "main" },
head: { ref: "deploy" },
html_url: "https://github.com/acme/app/pull/12"
}
},
3500
);
assert.ok(message);
assert.match(message, /📦 GitHub-GCC Pull Request/);
assert.match(message, /🔀 #12 Improve deployment/);
assert.match(message, /• merged/);
assert.match(message, /• deploy -> main/);
});
test("ignores ping and non-completed workflow_run events", () => {
assert.equal(formatGitHubEvent("ping", {}, 3500), null);
assert.equal(formatGitHubEvent("workflow_run", { action: "requested" }, 3500), null);
});
test("extracts organization login from organization or repository owner", () => {
assert.equal(getOrganizationLogin({ organization: { login: "Acme" } }), "acme");
assert.equal(getOrganizationLogin({ repository: { owner: { login: "Other" } } }), "other");
});
test("formats Gitea-style push payloads with compare_url and pusher login", () => {
const message = formatGitHubEvent(
"push",
{
ref: "refs/heads/main",
repository: { full_name: "gcc/app", owner: { login: "gcc" } },
pusher: { login: "alice", username: "alice" },
sender: { login: "alice" },
commits: [],
total_commits: 0,
compare_url: "https://192.168.87.52:18473/gcc/app/compare/a...b"
},
3500
);
assert.ok(message);
assert.match(message, /👤 alice/);
assert.match(message, /🔗 https:\/\/192\.168\.87\.52:18473\/gcc\/app\/compare\/a\.\.\.b/);
});
test("formats Gitea pull_request_comment events like issue comments", () => {
const message = formatGitHubEvent(
"pull_request_comment",
{
action: "created",
repository: { full_name: "gcc/app" },
issue: { number: 3, title: "Add login" },
comment: { body: "LGTM", html_url: "https://192.168.87.52:18473/gcc/app/pulls/3#issuecomment-1" },
sender: { login: "bob" },
is_pull: true
},
3500
);
assert.ok(message);
assert.match(message, /📦 GitHub-GCC Issue Comment/);
assert.match(message, /🎫 #3 Add login/);
assert.match(message, /• LGTM/);
});
test("extracts organization login from Gitea username field", () => {
assert.equal(getOrganizationLogin({ organization: { username: "GCC" } }), "gcc");
assert.equal(getOrganizationLogin({ repository: { owner: { username: "GCC" } } }), "gcc");
});
test("formats Gitea payloads that only have sender.username", () => {
const message = formatGitHubEvent(
"issues",
{
action: "opened",
repository: { full_name: "gcc/app" },
issue: { number: 8, title: "Broken login", html_url: "https://192.168.87.52:18473/gcc/app/issues/8" },
sender: { username: "carol" }
},
3500
);
assert.ok(message);
assert.match(message, /👤 carol/);
});
test("truncates long messages", () => {
const message = formatGitHubEvent(
"issue_comment",
{
repository: { full_name: "acme/app" },
action: "created",
issue: { number: 1, title: "Bug" },
comment: { body: "x".repeat(200), html_url: "https://example.test" },
sender: { login: "alice" }
},
120
);
assert.ok(message);
assert.ok(message.length <= 120);
assert.match(message, /\.\.\. truncated$/);
});