Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
test(fake-claude): a matcher on an event without one is ignored, as in the CLI
Absterrg042abda43bc6c
diff --git a/server/testing/claude-hook-matcher.test.ts b/server/testing/claude-hook-matcher.test.tsindex 7952227534..fd554a9387 100644--- a/server/testing/claude-hook-matcher.test.ts+++ b/server/testing/claude-hook-matcher.test.ts@@ -2,23 +2,34 @@ import { describe, expect, it } from "vitest";import { hookMatches } from "./claude-hook-matcher.ts";+const tool = (name: string) => ({ tool_name: name });+describe("hookMatches", () => {it("matches every event with no matcher, an empty one or a star", () => {- for (const matcher of [undefined, "", "*"]) expect(hookMatches(matcher, "Read")).toBe(true);+ for (const matcher of [undefined, "", "*"]) expect(hookMatches("PostToolUse", matcher, tool("Read"))).toBe(true);});it("compares plain names exactly, alone or in a list", () => {- expect(hookMatches("Bash", "Bash")).toBe(true);- expect(hookMatches("Bash", "BashOutput")).toBe(false);- expect(hookMatches("compact", "startup")).toBe(false);- expect(hookMatches("Edit, Write", "Write")).toBe(true);- expect(hookMatches("Edit|Write", "Edit")).toBe(true);- expect(hookMatches("Edit, Write", "NotebookEdit")).toBe(false);+ expect(hookMatches("PostToolUseFailure", "Bash", tool("Bash"))).toBe(true);+ expect(hookMatches("PostToolUseFailure", "Bash", tool("BashOutput"))).toBe(false);+ expect(hookMatches("PostToolUse", "Edit, Write", tool("Write"))).toBe(true);+ expect(hookMatches("PostToolUse", "Edit|Write", tool("Edit"))).toBe(true);+ expect(hookMatches("PostToolUse", "Edit, Write", tool("NotebookEdit"))).toBe(false);});it("tests any other matcher as an unanchored regular expression", () => {- expect(hookMatches("Edit.*", "NotebookEdit")).toBe(true);- expect(hookMatches("^Notebook", "NotebookEdit")).toBe(true);- expect(hookMatches("^Edit$", "NotebookEdit")).toBe(false);+ expect(hookMatches("PostToolUse", "Edit.*", tool("NotebookEdit"))).toBe(true);+ expect(hookMatches("PostToolUse", "^Notebook", tool("NotebookEdit"))).toBe(true);+ expect(hookMatches("PostToolUse", "^Edit$", tool("NotebookEdit"))).toBe(false);+ });++ it("reads a session's source and a compaction's trigger", () => {+ expect(hookMatches("SessionStart", "compact", { source: "compact" })).toBe(true);+ expect(hookMatches("SessionStart", "compact", { source: "startup" })).toBe(false);+ expect(hookMatches("PreCompact", "auto", { trigger: "manual" })).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.tsindex d07e75dba2..8a973b70e4 100644--- a/server/testing/claude-hook-matcher.ts+++ b/server/testing/claude-hook-matcher.ts@@ -1,9 +1,12 @@-/** Whether a settings hook's `matcher` selects an event, as the Claude Code hooks reference describes 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(matcher: string | undefined, value: unknown): boolean {- if (!matcher || matcher === "*") return true;- const text = String(value ?? "");+const MATCHED_FIELD: Record<string, string> = { PostToolUse: "tool_name", PostToolUseFailure: "tool_name", PreToolUse: "tool_name", SessionStart: "source", PreCompact: "trigger" };++/** 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 {+ 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);}diff --git a/server/testing/fake-claude-cli.ts b/server/testing/fake-claude-cli.tsindex d1667ef456..e238ba86d7 100755--- a/server/testing/fake-claude-cli.ts+++ b/server/testing/fake-claude-cli.ts@@ -255,12 +255,10 @@ const settingsHooks: Record<string, Array<{ matcher?: string; hooks?: Array<{ tyreturn {};}})();-// Like Claude Code: a matcher tests the tool name, a session's source or a compaction's trigger.-const HOOK_MATCHED_FIELD: Record<string, string> = { PostToolUse: "tool_name", PostToolUseFailure: "tool_name", SessionStart: "source", PreCompact: "trigger" };function runHooks(event: string, payload: Record<string, unknown>): string {let stdout = "";for (const entry of settingsHooks[event] ?? []) {- if (!hookMatches(entry.matcher, payload[HOOK_MATCHED_FIELD[event] ?? ""])) continue;+ if (!hookMatches(event, entry.matcher, payload)) continue;for (const hook of entry.hooks ?? []) {if (hook.type !== "command" || !hook.command) continue;// Like Claude Code: a hook with `args` starts `command` itself; otherwise `command` is a shell line.