2026-04-09 15:15:30 +09:00
import { afterEach , beforeEach , describe , expect , test } from "bun:test"
2026-04-10 18:47:17 +09:00
import type { PluginInput } from "@opencode-ai/plugin"
2026-04-09 15:15:30 +09:00
import { createKeywordDetectorHook } from "./index"
import { _resetForTesting , setMainSession } from "../../features/claude-code-session-state"
type StartLoopCall = {
sessionID : string
prompt : string
options : Record < string , unknown >
}
function createMockPluginInput ( toastCalls : string [ ] = [ ] ) {
return {
client : {
tui : {
showToast : async ( opts : { body : { title : string } } ) = > {
toastCalls . push ( opts . body . title )
} ,
} ,
} ,
2026-04-10 18:47:17 +09:00
} as unknown as PluginInput
2026-04-09 15:15:30 +09:00
}
function createMockRalphLoop ( startLoopCalls : StartLoopCall [ ] ) {
return {
startLoop : ( sessionID : string , prompt : string , options? : Record < string , unknown > ) : boolean = > {
startLoopCalls . push ( { sessionID , prompt , options : options ? ? { } } )
return true
} ,
}
}
describe ( "keyword-detector ultrawork edge trigger" , ( ) = > {
beforeEach ( ( ) = > {
_resetForTesting ( )
setMainSession ( "main-session" )
} )
afterEach ( ( ) = > {
_resetForTesting ( )
} )
2026-04-10 11:40:14 +09:00
test ( "#given greeting text before ulw and surrounding whitespace #when chat.message fires #then ultrawork still activates without starting ralph loop" , async ( ) = > {
2026-04-09 15:15:30 +09:00
// given
const toastCalls : string [ ] = [ ]
const startLoopCalls : StartLoopCall [ ] = [ ]
const hook = createKeywordDetectorHook (
createMockPluginInput ( toastCalls ) ,
undefined ,
createMockRalphLoop ( startLoopCalls ) ,
)
const output = {
message : { } as Record < string , unknown > ,
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" )
2026-04-10 11:40:14 +09:00
expect ( startLoopCalls ) . toHaveLength ( 0 )
2026-04-09 15:15:30 +09:00
expect ( output . parts [ 0 ] ? . text ) . toContain ( "ULTRAWORK MODE ENABLED!" )
expect ( output . parts [ 0 ] ? . text ) . toContain ( " hi there ulw " )
} )
2026-04-10 11:40:14 +09:00
test ( "#given greeting before ulw with a trailing task #when chat.message fires #then ultrawork activates and preserves the task without starting ralph loop" , async ( ) = > {
2026-04-10 11:09:01 +09:00
// given
const toastCalls : string [ ] = [ ]
const startLoopCalls : StartLoopCall [ ] = [ ]
const hook = createKeywordDetectorHook (
createMockPluginInput ( toastCalls ) ,
undefined ,
createMockRalphLoop ( startLoopCalls ) ,
)
const output = {
message : { } as Record < string , unknown > ,
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" )
2026-04-10 11:40:14 +09:00
expect ( startLoopCalls ) . toHaveLength ( 0 )
2026-04-10 11:09:01 +09:00
expect ( output . parts [ 0 ] ? . text ) . toContain ( "ULTRAWORK MODE ENABLED!" )
expect ( output . parts [ 0 ] ? . text ) . toContain ( "hey ulw fix the flaky keyword tests" )
} )
2026-04-10 11:40:14 +09:00
test ( "#given ulw mentioned in the middle of a sentence #when chat.message fires #then ultrawork still activates without starting ralph loop" , async ( ) = > {
2026-04-09 15:15:30 +09:00
// given
const toastCalls : string [ ] = [ ]
const startLoopCalls : StartLoopCall [ ] = [ ]
const hook = createKeywordDetectorHook (
createMockPluginInput ( toastCalls ) ,
undefined ,
createMockRalphLoop ( startLoopCalls ) ,
)
const output = {
message : { } as Record < string , unknown > ,
2026-04-10 11:12:56 +09:00
parts : [ { type : "text" , text : "please ulw fix the flaky keyword tests" } ] ,
2026-04-09 15:15:30 +09:00
}
// when
await hook [ "chat.message" ] ( { sessionID : "main-session" , agent : "sisyphus" } , output )
// then
2026-04-10 11:12:56 +09:00
expect ( toastCalls ) . toContain ( "Ultrawork Mode Activated" )
2026-04-10 11:40:14 +09:00
expect ( startLoopCalls ) . toHaveLength ( 0 )
2026-04-10 11:12:56 +09:00
expect ( output . parts [ 0 ] ? . text ) . toContain ( "please ulw fix the flaky keyword tests" )
2026-04-09 15:15:30 +09:00
} )
2026-04-10 11:40:14 +09:00
test ( "#given trailing ultrawork reference without punctuation #when chat.message fires #then ultrawork still activates without starting ralph loop" , async ( ) = > {
2026-04-09 15:15:30 +09:00
// given
const toastCalls : string [ ] = [ ]
const startLoopCalls : StartLoopCall [ ] = [ ]
const hook = createKeywordDetectorHook (
createMockPluginInput ( toastCalls ) ,
undefined ,
createMockRalphLoop ( startLoopCalls ) ,
)
const output = {
message : { } as Record < string , unknown > ,
parts : [ { type : "text" , text : "what is ultrawork" } ] ,
}
// when
await hook [ "chat.message" ] ( { sessionID : "main-session" , agent : "sisyphus" } , output )
// then
2026-04-10 11:15:23 +09:00
expect ( toastCalls ) . toContain ( "Ultrawork Mode Activated" )
2026-04-10 11:40:14 +09:00
expect ( startLoopCalls ) . toHaveLength ( 0 )
2026-04-10 11:15:23 +09:00
expect ( output . parts [ 0 ] ? . text ) . toContain ( "what is ultrawork" )
2026-04-09 15:15:30 +09:00
} )
} )