2026-10-14 07:02:56 -07:00
package agents
import (
"context"
"strings"
"testing"
"g.fc.run/misc/byre/internal/api"
"g.fc.run/misc/byre/internal/store"
)
func TestValidateAsk ( t * testing . T ) {
m := & Manager {}
l := & live { id : "s1" }
yes := true
cases := [] struct {
name string
args askArgs
want [] string
}{
2026-10-14 10:37:04 -07:00
{ "valid" , askArgs { Context : "c" , Question : "q" , WhyBlocked : "w" , Options : [] string { "a" , "b" }, RecommendedOption : "a" , Blocking : & yes }, nil },
{ "empty" , askArgs {}, [] string { "context is empty" , "question is empty" , "why_blocked is empty" , "at least two" , "recommended_option" , "blocking is required" }},
{ "one option" , askArgs { Context : "c" , Question : "q" , WhyBlocked : "w" , Options : [] string { "a" }, RecommendedOption : "a" , Blocking : & yes }, [] string { "at least two" }},
{ "bad recommendation" , askArgs { Context : "c" , Question : "q" , WhyBlocked : "w" , Options : [] string { "a" , "b" }, RecommendedOption : "c" , Blocking : & yes }, [] string { "recommended_option" }},
{ "bad attachment" , askArgs { Context : "c" , Question : "q" , WhyBlocked : "w" , Options : [] string { "a" , "b" }, RecommendedOption : "a" , Blocking : & yes ,
2026-10-14 07:02:56 -07:00
Attachments : [] api . Attachment {{ Kind : "video" }}}, [] string { `attachments[0].kind "video"` }},
}
for _ , c := range cases {
t . Run ( c . name , func ( t * testing . T ) {
err := m . validateAsk ( context . Background (), l , c . args )
if len ( c . want ) == 0 {
if err != nil {
t . Fatalf ( "unexpected error %v" , err )
}
return
}
if err == nil {
t . Fatalf ( "want rejection mentioning %v" , c . want )
}
if ! strings . Contains ( err . Error (), "nothing was filed" ) {
t . Fatalf ( "rejection doesn't say nothing was filed: %v" , err )
}
for _ , w := range c . want {
if ! strings . Contains ( err . Error (), w ) {
t . Fatalf ( "rejection %q doesn't mention %q" , err , w )
}
}
})
}
}
func TestDeliveryMessage ( t * testing . T ) {
str := func ( s string ) * string { return & s }
2026-10-14 10:37:04 -07:00
question := api . InboxItem { Id : "q1" , ThreadId : "q1" , Kind : api . Question , Payload : map [ string ] any { "question" : "Which color?" }}
failure := api . InboxItem { Id : "e1" , ThreadId : "e1" , Kind : api . SessionError , Payload : map [ string ] any { "error" : "process died" }}
2026-10-14 07:02:56 -07:00
msg := deliveryMessage ([] store . Pending {
2026-10-14 10:37:04 -07:00
{ Item : question , Response : api . InboxResponse { User : "alice" , Type : api . Option , Option : str ( "blue" )}},
{ Item : question , Response : api . InboxResponse { User : "bob" , Type : api . Other , Text : str ( "what is it for?" ),
2026-10-14 07:02:56 -07:00
Attachments : [] api . Attachment {{ Kind : api . AttachmentKindUrl , Url : str ( "https://example.com" )}}}},
2026-10-14 10:37:04 -07:00
{ Item : failure , Response : api . InboxResponse { User : "alice" , Type : api . Other , Text : str ( "stop and write tests" )}},
2026-10-14 07:02:56 -07:00
})
for _ , want := range [] string {
"Your question q1: Which color?" ,
"alice chose: blue" ,
2026-10-14 10:37:04 -07:00
"bob answered (none of your options): what is it for?" ,
2026-10-14 07:02:56 -07:00
`calling ask_human with thread_id "q1"` ,
`"url":"https://example.com"` ,
2026-10-14 10:37:04 -07:00
"Your error e1: process died" ,
"alice replied: stop and write tests" ,
2026-10-14 07:02:56 -07:00
} {
if ! strings . Contains ( msg , want ) {
t . Fatalf ( "message missing %q:\n%s" , want , msg )
}
}
}