Files
ghbot/test/webhook-parity.test.ts
lezi-fun 9e47ab339e feat: sync ghbot-Nodeloc v1.2.0 capabilities into upstream
Merges all 33 commits from lza6/ghbot-Nodeloc (v1.0.0 through v1.2.0)
while keeping both sides' features in every conflicted file:

- EventRouter dispatch extracted from runReview.ts (7.1), with the
  runtime environment block preserved on the review-start comment
- Unified command ack messages (6.3) combined with the collapsed
  runtime environment block and bot signature on /recheck replies
- Status banner and collapsible unposted findings in review bodies (6.1)
- Centralized runtime directories behind GHBOT_RUNTIME_DIR (D-13);
  webhook knowledge cache now lives under tempRootDirectory()
- Production hardening for merge races, timeouts, webhook ops;
  atomic temp-file rename for R2 cache restores kept from upstream
- Webhook parity retained: triage events gated by WEBHOOK_TRIAGE_ENABLED,
  read-only command guidance, repository knowledge injection,
  runtime environment block on webhook chat replies
- Sanitization/secret redaction centralization, error-aware retry,
  duplicate merge prevention, in-process webhook retries, /metrics
- Infra: eslint/prettier gates with CI verify workflow, docs archives

typecheck, eslint, prettier format:check, and 164 tests all pass.
2026-08-24 11:47:28 +08:00

172 lines
4.9 KiB
TypeScript

import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import {
isUninitializedRepositoryKnowledge,
loadRepositoryKnowledge
} from "../src/repository/knowledge.js";
import {
buildWebhookChatPrompt,
loadWebhookRepositoryKnowledge,
matchWebhookReadonlyCommand,
parseWebhookTriageEvent,
type WebhookContext,
type WebhookMention
} from "../src/webhook/processor.js";
function triagePayload(extra: Record<string, unknown> = {}) {
return {
action: "opened",
installation: { id: 42 },
repository: { id: 7, name: "public", owner: { login: "forumlify" } },
...extra
};
}
test("triage events are parsed for issues and pull requests on open/edit/reopen", () => {
const issue = parseWebhookTriageEvent("issues", triagePayload({ issue: { number: 5 } }));
assert.deepEqual(issue, {
action: "opened",
installationId: 42,
owner: "forumlify",
repo: "public",
kind: "issue",
number: 5
});
const pull = parseWebhookTriageEvent(
"pull_request",
triagePayload({
action: "reopened",
pull_request: { number: 9 }
})
);
assert.equal(pull?.kind, "pull_request");
assert.equal(pull?.number, 9);
});
test("triage parsing rejects other events, pushes, PR issues, and missing installation", () => {
assert.equal(
parseWebhookTriageEvent(
"issues",
triagePayload({ action: "synchronize", issue: { number: 5 } })
),
null
);
assert.equal(
parseWebhookTriageEvent(
"pull_request",
triagePayload({ action: "closed", pull_request: { number: 9 } })
),
null
);
assert.equal(
parseWebhookTriageEvent(
"issues",
triagePayload({ issue: { number: 5, pull_request: { url: "x" } } })
),
null
);
assert.equal(
parseWebhookTriageEvent(
"issues",
triagePayload({ installation: undefined, issue: { number: 5 } })
),
null
);
assert.equal(parseWebhookTriageEvent("issue_comment", triagePayload()), null);
});
test("readonly command matching keeps Action-mode exact semantics", () => {
assert.equal(matchWebhookReadonlyCommand("/recheck"), "/recheck");
assert.equal(matchWebhookReadonlyCommand(" /conflict \n"), "/conflict");
assert.equal(matchWebhookReadonlyCommand("/recheck please"), null);
assert.equal(matchWebhookReadonlyCommand("@bot /recheck"), null);
assert.equal(matchWebhookReadonlyCommand(""), null);
});
const mention: WebhookMention = {
eventName: "issue_comment",
action: "created",
deliveryId: "d-1",
installationId: 42,
owner: "forumlify",
repo: "public",
issueNumber: 3,
targetKind: "pull_request",
sourceCommentId: 11,
commentBody: "@bot what does this PR change?",
commenterLogin: "octocat",
replyMode: "conversation"
};
const baseContext: WebhookContext = {
repository: {
fullName: "forumlify/public",
description: null,
defaultBranch: "main",
readme: ""
},
item: {
number: 3,
title: "t",
body: "",
state: "open",
url: "",
kind: "pull_request",
author: null
},
discussion: []
};
test("webhook chat prompt embeds host-curated knowledge when available", () => {
const withKnowledge = buildWebhookChatPrompt(mention, {
...baseContext,
knowledge: "# ghbot repository knowledge\nTests run with npm test."
});
assert.match(withKnowledge, /Verified repository knowledge curated by the host process/);
assert.match(withKnowledge, /npm test\./);
assert.match(
withKnowledge,
/current repository evidence always takes precedence over cached knowledge/
);
const withoutKnowledge = buildWebhookChatPrompt(mention, baseContext);
assert.doesNotMatch(withoutKnowledge, /Verified repository knowledge/);
});
test("uninitialized knowledge scaffold is detected and never injected", async () => {
const runtimeDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-knowledge-"));
try {
const scaffold = await loadRepositoryKnowledge(runtimeDirectory);
assert.equal(isUninitializedRepositoryKnowledge(scaffold), true);
assert.equal(
isUninitializedRepositoryKnowledge("# ghbot repository knowledge\n\nTests: npm test.\n"),
false
);
} finally {
await fs.rm(runtimeDirectory, { recursive: true, force: true });
}
});
test("webhook knowledge loader degrades to undefined without R2 or repository id", async () => {
assert.equal(
await loadWebhookRepositoryKnowledge({ owner: "forumlify", repo: "public" }),
undefined
);
assert.equal(
await loadWebhookRepositoryKnowledge({ repositoryId: 7, owner: "forumlify", repo: "public" }),
undefined
);
});
test("webhook chat prompt stays read-only in both variants", () => {
for (const context of [baseContext, { ...baseContext, knowledge: "k" }]) {
const prompt = buildWebhookChatPrompt(mention, context);
assert.match(prompt, /no repository tools/);
assert.match(prompt, /Do not claim that you ran commands/);
}
});