@@ -262,7 +262,7 @@ func TestLoadBotConfig(t *testing.T) {
262262 continue
263263 }
264264 switch cmd .Type {
265- case "http" , "exec" , "ai" :
265+ case "http" , "exec" , "ai" , "builtin" :
266266 default :
267267 t .Errorf ("command %q has invalid type %q" , name , cmd .Type )
268268 }
@@ -412,8 +412,8 @@ func TestQueryTopYappers(t *testing.T) {
412412 if ! strings .Contains (result , "alice" ) {
413413 t .Errorf ("expected alice in result, got: %s" , result )
414414 }
415- if ! strings .Contains (result , "5 msgs " ) {
416- t .Errorf ("expected '5 msgs ' for alice, got: %s" , result )
415+ if ! strings .Contains (result , "10 words " ) {
416+ t .Errorf ("expected '10 words ' for alice, got: %s" , result )
417417 }
418418 if ! strings .Contains (result , "bob" ) {
419419 t .Errorf ("expected bob in result, got: %s" , result )
@@ -445,3 +445,87 @@ func TestQueryTopYappers(t *testing.T) {
445445 t .Errorf ("messages from other rooms should be excluded, got: %s" , result )
446446 }
447447}
448+
449+ func TestQueryYapGuess (t * testing.T ) {
450+ db , err := sql .Open ("sqlite3" , ":memory:" )
451+ if err != nil {
452+ t .Fatalf ("open db: %v" , err )
453+ }
454+ defer db .Close ()
455+
456+ _ , err = db .Exec (`CREATE TABLE IF NOT EXISTS messages (
457+ id TEXT PRIMARY KEY,
458+ room_id TEXT,
459+ sender TEXT,
460+ ts_ms INTEGER,
461+ body TEXT,
462+ msgtype TEXT,
463+ raw_json TEXT
464+ )` )
465+ if err != nil {
466+ t .Fatalf ("create table: %v" , err )
467+ }
468+
469+ now := time .Now ().UnixMilli ()
470+ room := "!testroom:example.com"
471+
472+ // alice=10 words (rank 1), bob=6 words (rank 2), carol=1 word (rank 3)
473+ for i := 0 ; i < 5 ; i ++ {
474+ _ , _ = db .Exec (`INSERT INTO messages(id, room_id, sender, ts_ms, body, msgtype) VALUES (?, ?, ?, ?, ?, ?)` ,
475+ fmt .Sprintf ("alice-%d" , i ), room , "@alice:example.com" , now - int64 (i * 1000 ), fmt .Sprintf ("hello %d" , i ), "m.text" )
476+ }
477+ for i := 0 ; i < 3 ; i ++ {
478+ _ , _ = db .Exec (`INSERT INTO messages(id, room_id, sender, ts_ms, body, msgtype) VALUES (?, ?, ?, ?, ?, ?)` ,
479+ fmt .Sprintf ("bob-%d" , i ), room , "@bob:example.com" , now - int64 (i * 1000 ), fmt .Sprintf ("hey %d" , i ), "m.text" )
480+ }
481+ _ , _ = db .Exec (`INSERT INTO messages(id, room_id, sender, ts_ms, body, msgtype) VALUES (?, ?, ?, ?, ?, ?)` ,
482+ "carol-0" , room , "@carol:example.com" , now , "sup" , "m.text" )
483+
484+ ctx := context .Background ()
485+
486+ // Bob guesses rank 1 but is actually rank 2.
487+ ev := & event.Event {
488+ RoomID : id .RoomID (room ),
489+ }
490+ ev .Sender = "@bob:example.com"
491+ result , err := queryTopYappers (ctx , db , nil , ev , "guess 1" , "" , false )
492+ if err != nil {
493+ t .Fatalf ("queryYapGuess: %v" , err )
494+ }
495+ if ! strings .Contains (result , "guessed #1" ) || ! strings .Contains (result , "actually #2" ) {
496+ t .Errorf ("expected bob at #2 with guess #1, got: %s" , result )
497+ }
498+ if ! strings .Contains (result , "1 position(s) higher" ) {
499+ t .Errorf ("expected 'higher than you thought', got: %s" , result )
500+ }
501+
502+ // Alice guesses rank 1 — exactly right.
503+ ev .Sender = "@alice:example.com"
504+ result , err = queryTopYappers (ctx , db , nil , ev , "guess 1" , "" , false )
505+ if err != nil {
506+ t .Fatalf ("queryYapGuess exact: %v" , err )
507+ }
508+ if ! strings .Contains (result , "exactly right" ) {
509+ t .Errorf ("expected exact match for alice guessing #1, got: %s" , result )
510+ }
511+
512+ // Carol guesses rank 1 but is actually rank 3.
513+ ev .Sender = "@carol:example.com"
514+ result , err = queryTopYappers (ctx , db , nil , ev , "guess 1" , "" , false )
515+ if err != nil {
516+ t .Fatalf ("queryYapGuess carol: %v" , err )
517+ }
518+ if ! strings .Contains (result , "guessed #1" ) || ! strings .Contains (result , "actually #3" ) {
519+ t .Errorf ("expected carol at #3, got: %s" , result )
520+ }
521+
522+ // Unknown sender has no messages.
523+ ev .Sender = "@nobody:example.com"
524+ result , err = queryTopYappers (ctx , db , nil , ev , "guess 1" , "" , false )
525+ if err != nil {
526+ t .Fatalf ("queryYapGuess nobody: %v" , err )
527+ }
528+ if ! strings .Contains (result , "no messages" ) {
529+ t .Errorf ("expected no messages for unknown sender, got: %s" , result )
530+ }
531+ }
0 commit comments