Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

test(fake-claude): type the hook events the fake fires; skip a matcher that does not compile, as 2.1.325 does

Absterrg0c7637e882410
diff --git a/server/testing/claude-hook-matcher.test.ts b/server/testing/claude-hook-matcher.test.ts
index fd554a9387..a2d062afaa 100644
--- a/server/testing/claude-hook-matcher.test.ts
+++ b/server/testing/claude-hook-matcher.test.ts
@@ -29,6 +29,10 @@ describe("hookMatches", () => {
expect(hookMatches("PreCompact", "auto", { trigger: "manual" })).toBe(false);
});
+ it("skips a hook whose expression does not compile", () => {
+ expect(hookMatches("PostToolUse", "[", tool("Bash"))).toBe(false);
+ });
+
it("ignores a matcher on an event that has none", () => {
expect(hookMatches("Stop", "Bash", { stop_hook_active: false })).toBe(true);
});
diff --git a/server/testing/claude-hook-matcher.ts b/server/testing/claude-hook-matcher.ts
index 8a973b70e4..653c243afd 100644
--- a/server/testing/claude-hook-matcher.ts
+++ b/server/testing/claude-hook-matcher.ts
@@ -1,12 +1,19 @@
-const MATCHED_FIELD: Record<string, string> = { PostToolUse: "tool_name", PostToolUseFailure: "tool_name", PreToolUse: "tool_name", SessionStart: "source", PreCompact: "trigger" };
+export type FakeHookEvent = "PostToolUse" | "PostToolUseFailure" | "PreCompact" | "SessionStart" | "Stop";
+
+const MATCHED_FIELD: Record<FakeHookEvent, string | null> = { PostToolUse: "tool_name", PostToolUseFailure: "tool_name", PreCompact: "trigger", SessionStart: "source", Stop: null };
/** Whether a settings hook's `matcher` selects an event, as the Claude Code hooks reference describes it: an event with no
* matcher field ignores it; "", "*" or none match all; a matcher of only letters, digits, `_`, `-`, spaces, `,` and `|` is
- * an exact name or a list of them; any other is an unanchored JavaScript regular expression. */
-export function hookMatches(event: string, matcher: string | undefined, payload: Record<string, unknown>): boolean {
+ * an exact name or a list of them; any other is an unanchored JavaScript regular expression, and 2.1.325 skips a hook
+ * whose expression does not compile. */
+export function hookMatches(event: FakeHookEvent, matcher: string | undefined, payload: Record<string, unknown>): boolean {
const field = MATCHED_FIELD[event];
if (!field || !matcher || matcher === "*") return true;
const text = String(payload[field] ?? "");
if (/^[\w\s,|-]+$/.test(matcher)) return matcher.split(/[|,]/).some((name) => name.trim() === text);
- return new RegExp(matcher).test(text);
+ try {
+ return new RegExp(matcher).test(text);
+ } catch {
+ return false;
+ }
}
diff --git a/server/testing/fake-claude-cli.ts b/server/testing/fake-claude-cli.ts
index e238ba86d7..416ae5e03d 100755
--- a/server/testing/fake-claude-cli.ts
+++ b/server/testing/fake-claude-cli.ts
@@ -96,7 +96,7 @@ import { spawn, spawnSync, type ChildProcess } from "node:child_process";
import { appendFileSync, closeSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
-import { hookMatches } from "./claude-hook-matcher.ts";
+import { hookMatches, type FakeHookEvent } from "./claude-hook-matcher.ts";
import { runRoomHandoffAgent } from "./room-handoff-agent.ts";
import { playStreamScript, streamScript } from "./stream-script.ts";
@@ -255,7 +255,7 @@ const settingsHooks: Record<string, Array<{ matcher?: string; hooks?: Array<{ ty
return {};
}
})();
-function runHooks(event: string, payload: Record<string, unknown>): string {
+function runHooks(event: FakeHookEvent, payload: Record<string, unknown>): string {
let stdout = "";
for (const entry of settingsHooks[event] ?? []) {
if (!hookMatches(event, entry.matcher, payload)) continue;