import { afterEach, beforeEach, describe, expect, test } from "bun:test" import { createKeywordDetectorHook } from "./index" import { _resetForTesting, setMainSession } from "../../features/claude-code-session-state" type StartLoopCall = { sessionID: string prompt: string options: Record } function createMockPluginInput(toastCalls: string[] = []) { return { client: { tui: { showToast: async (opts: { body: { title: string } }) => { toastCalls.push(opts.body.title) }, }, }, } as any } function createMockRalphLoop(startLoopCalls: StartLoopCall[]) { return { startLoop: (sessionID: string, prompt: string, options?: Record): boolean => { startLoopCalls.push({ sessionID, prompt, options: options ?? {} }) return true }, } } describe("keyword-detector ultrawork edge trigger", () => { beforeEach(() => { _resetForTesting() setMainSession("main-session") }) afterEach(() => { _resetForTesting() }) test("#given greeting text before ulw and surrounding whitespace #when chat.message fires #then ultrawork still activates", async () => { // given const toastCalls: string[] = [] const startLoopCalls: StartLoopCall[] = [] const hook = createKeywordDetectorHook( createMockPluginInput(toastCalls), undefined, createMockRalphLoop(startLoopCalls), ) const output = { message: {} as Record, parts: [{ type: "text", text: " hi there ulw " }], } // when await hook["chat.message"]({ sessionID: "main-session", agent: "sisyphus" }, output) // then expect(toastCalls).toContain("Ultrawork Mode Activated") expect(startLoopCalls).toHaveLength(1) expect(startLoopCalls[0]).toEqual({ sessionID: "main-session", prompt: "Complete the task as instructed", options: { ultrawork: true, maxIterations: undefined, completionPromise: undefined, strategy: undefined, }, }) expect(output.parts[0]?.text).toContain("ULTRAWORK MODE ENABLED!") expect(output.parts[0]?.text).toContain(" hi there ulw ") }) test("#given greeting before ulw with a trailing task #when chat.message fires #then ultrawork activates and preserves the task", async () => { // given const toastCalls: string[] = [] const startLoopCalls: StartLoopCall[] = [] const hook = createKeywordDetectorHook( createMockPluginInput(toastCalls), undefined, createMockRalphLoop(startLoopCalls), ) const output = { message: {} as Record, parts: [{ type: "text", text: "hey ulw fix the flaky keyword tests" }], } // when await hook["chat.message"]({ sessionID: "main-session", agent: "sisyphus" }, output) // then expect(toastCalls).toContain("Ultrawork Mode Activated") expect(startLoopCalls).toHaveLength(1) expect(startLoopCalls[0]).toEqual({ sessionID: "main-session", prompt: "fix the flaky keyword tests", options: { ultrawork: true, maxIterations: undefined, completionPromise: undefined, strategy: undefined, }, }) expect(output.parts[0]?.text).toContain("ULTRAWORK MODE ENABLED!") expect(output.parts[0]?.text).toContain("hey ulw fix the flaky keyword tests") }) test("#given ulw mentioned in the middle of a sentence #when chat.message fires #then ultrawork still activates", async () => { // given const toastCalls: string[] = [] const startLoopCalls: StartLoopCall[] = [] const hook = createKeywordDetectorHook( createMockPluginInput(toastCalls), undefined, createMockRalphLoop(startLoopCalls), ) const output = { message: {} as Record, parts: [{ type: "text", text: "please ulw fix the flaky keyword tests" }], } // when await hook["chat.message"]({ sessionID: "main-session", agent: "sisyphus" }, output) // then expect(toastCalls).toContain("Ultrawork Mode Activated") expect(startLoopCalls).toHaveLength(1) expect(startLoopCalls[0]?.prompt).toBe("please fix the flaky keyword tests") expect(output.parts[0]?.text).toContain("please ulw fix the flaky keyword tests") }) test("#given trailing ultrawork reference without punctuation #when chat.message fires #then ultrawork still activates", async () => { // given const toastCalls: string[] = [] const startLoopCalls: StartLoopCall[] = [] const hook = createKeywordDetectorHook( createMockPluginInput(toastCalls), undefined, createMockRalphLoop(startLoopCalls), ) const output = { message: {} as Record, parts: [{ type: "text", text: "what is ultrawork" }], } // when await hook["chat.message"]({ sessionID: "main-session", agent: "sisyphus" }, output) // then expect(toastCalls).toContain("Ultrawork Mode Activated") expect(startLoopCalls).toHaveLength(1) expect(startLoopCalls[0]?.prompt).toBe("what is") expect(output.parts[0]?.text).toContain("what is ultrawork") }) })