test: tests para soporte de threads de Matrix

Tests unitarios:
- runner_test.go: verifica ruteo correcto de ReplyAction segun
  ThreadID (plain markdown, reply, thread, thread sin fallback, nil reply)
- thread_test.go: extraccion de ThreadID desde m.relates_to raw
  (thread, reply sin thread, plain, m.replace, thread sin event_id)
- thread_relates_test.go: estructura JSON de RelatesTo.SetThread
  cumple la spec de Matrix (rel_type, event_id, is_falling_back, m.in_reply_to)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 12:50:41 +00:00
parent 38d11a0b32
commit e3da95c12b
3 changed files with 333 additions and 0 deletions
+172
View File
@@ -0,0 +1,172 @@
package effects
import (
"context"
"log/slog"
"testing"
"github.com/enmanuel/agents/pkg/decision"
)
// fakeMatrixSender records calls for assertions.
type fakeMatrixSender struct {
calls []senderCall
}
type senderCall struct {
method string
roomID string
threadRootID string
inReplyTo string
markdown string
}
func (f *fakeMatrixSender) SendText(ctx context.Context, roomID, text string) error {
f.calls = append(f.calls, senderCall{method: "SendText", roomID: roomID, markdown: text})
return nil
}
func (f *fakeMatrixSender) SendMarkdown(ctx context.Context, roomID, markdown string) error {
f.calls = append(f.calls, senderCall{method: "SendMarkdown", roomID: roomID, markdown: markdown})
return nil
}
func (f *fakeMatrixSender) SendReplyMarkdown(ctx context.Context, roomID, inReplyTo, markdown string) error {
f.calls = append(f.calls, senderCall{method: "SendReplyMarkdown", roomID: roomID, inReplyTo: inReplyTo, markdown: markdown})
return nil
}
func (f *fakeMatrixSender) SendThreadMarkdown(ctx context.Context, roomID, threadRootID, inReplyTo, markdown string) error {
f.calls = append(f.calls, senderCall{method: "SendThreadMarkdown", roomID: roomID, threadRootID: threadRootID, inReplyTo: inReplyTo, markdown: markdown})
return nil
}
func (f *fakeMatrixSender) SendTyping(ctx context.Context, roomID string, typing bool) error {
return nil
}
func TestExecuteReply_PlainMarkdown(t *testing.T) {
sender := &fakeMatrixSender{}
runner := NewRunner(sender, nil, slog.Default())
actions := []decision.Action{{
Kind: decision.ActionKindReply,
Reply: &decision.ReplyAction{Content: "hello"},
}}
results := runner.Execute(context.Background(), "!room:test", actions)
if len(results) != 1 || results[0].Err != nil {
t.Fatalf("unexpected results: %+v", results)
}
if len(sender.calls) != 1 {
t.Fatalf("expected 1 call, got %d", len(sender.calls))
}
c := sender.calls[0]
if c.method != "SendMarkdown" {
t.Errorf("expected SendMarkdown, got %s", c.method)
}
if c.roomID != "!room:test" {
t.Errorf("expected room !room:test, got %s", c.roomID)
}
}
func TestExecuteReply_WithInReplyTo(t *testing.T) {
sender := &fakeMatrixSender{}
runner := NewRunner(sender, nil, slog.Default())
actions := []decision.Action{{
Kind: decision.ActionKindReply,
Reply: &decision.ReplyAction{Content: "hello", InReplyTo: "$evt1"},
}}
runner.Execute(context.Background(), "!room:test", actions)
if len(sender.calls) != 1 {
t.Fatalf("expected 1 call, got %d", len(sender.calls))
}
c := sender.calls[0]
if c.method != "SendReplyMarkdown" {
t.Errorf("expected SendReplyMarkdown, got %s", c.method)
}
if c.inReplyTo != "$evt1" {
t.Errorf("expected inReplyTo=$evt1, got %s", c.inReplyTo)
}
}
func TestExecuteReply_WithThread(t *testing.T) {
sender := &fakeMatrixSender{}
runner := NewRunner(sender, nil, slog.Default())
actions := []decision.Action{{
Kind: decision.ActionKindReply,
Reply: &decision.ReplyAction{
Content: "thread reply",
ThreadID: "$root",
InReplyTo: "$evt2",
},
}}
runner.Execute(context.Background(), "!room:test", actions)
if len(sender.calls) != 1 {
t.Fatalf("expected 1 call, got %d", len(sender.calls))
}
c := sender.calls[0]
if c.method != "SendThreadMarkdown" {
t.Errorf("expected SendThreadMarkdown, got %s", c.method)
}
if c.threadRootID != "$root" {
t.Errorf("expected threadRootID=$root, got %s", c.threadRootID)
}
if c.inReplyTo != "$evt2" {
t.Errorf("expected inReplyTo=$evt2, got %s", c.inReplyTo)
}
if c.roomID != "!room:test" {
t.Errorf("expected room !room:test, got %s", c.roomID)
}
}
func TestExecuteReply_ThreadWithoutInReplyTo(t *testing.T) {
sender := &fakeMatrixSender{}
runner := NewRunner(sender, nil, slog.Default())
actions := []decision.Action{{
Kind: decision.ActionKindReply,
Reply: &decision.ReplyAction{
Content: "thread reply no fallback",
ThreadID: "$root",
},
}}
runner.Execute(context.Background(), "!room:test", actions)
if len(sender.calls) != 1 {
t.Fatalf("expected 1 call, got %d", len(sender.calls))
}
c := sender.calls[0]
if c.method != "SendThreadMarkdown" {
t.Errorf("expected SendThreadMarkdown, got %s", c.method)
}
// inReplyTo should be empty; SendThreadMarkdown will default to threadRootID
if c.inReplyTo != "" {
t.Errorf("expected empty inReplyTo, got %s", c.inReplyTo)
}
}
func TestExecuteReply_NilReply(t *testing.T) {
sender := &fakeMatrixSender{}
runner := NewRunner(sender, nil, slog.Default())
actions := []decision.Action{{
Kind: decision.ActionKindReply,
Reply: nil,
}}
results := runner.Execute(context.Background(), "!room:test", actions)
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if results[0].Err == nil {
t.Error("expected error for nil reply")
}
}
+66
View File
@@ -0,0 +1,66 @@
package matrix
import (
"encoding/json"
"testing"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// TestThreadRelatesToStructure verifies the JSON structure produced by SetThread
// matches the Matrix spec for m.thread events.
func TestThreadRelatesToStructure(t *testing.T) {
rel := (&event.RelatesTo{}).SetThread(id.EventID("$root"), id.EventID("$last"))
data, err := json.Marshal(rel)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if m["rel_type"] != "m.thread" {
t.Errorf("expected rel_type=m.thread, got %v", m["rel_type"])
}
if m["event_id"] != "$root" {
t.Errorf("expected event_id=$root, got %v", m["event_id"])
}
if m["is_falling_back"] != true {
t.Errorf("expected is_falling_back=true, got %v", m["is_falling_back"])
}
inReplyTo, ok := m["m.in_reply_to"].(map[string]any)
if !ok {
t.Fatal("expected m.in_reply_to to be an object")
}
if inReplyTo["event_id"] != "$last" {
t.Errorf("expected m.in_reply_to.event_id=$last, got %v", inReplyTo["event_id"])
}
}
// TestThreadRelatesToStructure_SameRootAndFallback verifies that when root == fallback,
// the structure is still correct.
func TestThreadRelatesToStructure_SameRootAndFallback(t *testing.T) {
rel := (&event.RelatesTo{}).SetThread(id.EventID("$root"), id.EventID("$root"))
data, err := json.Marshal(rel)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if m["rel_type"] != "m.thread" {
t.Errorf("expected rel_type=m.thread, got %v", m["rel_type"])
}
if m["event_id"] != "$root" {
t.Errorf("expected event_id=$root, got %v", m["event_id"])
}
}
+95
View File
@@ -0,0 +1,95 @@
package matrix
import (
"testing"
)
// extractThreadID is the pure logic extracted from the listener for testability.
// It mirrors the extraction in listener.go's EventMessage handler.
func extractThreadID(rawContent map[string]any) string {
relatesTo, ok := rawContent["m.relates_to"].(map[string]any)
if !ok {
return ""
}
relType, _ := relatesTo["rel_type"].(string)
if relType != "m.thread" {
return ""
}
threadRoot, _ := relatesTo["event_id"].(string)
return threadRoot
}
func TestExtractThreadID_ThreadMessage(t *testing.T) {
raw := map[string]any{
"body": "hello",
"m.relates_to": map[string]any{
"rel_type": "m.thread",
"event_id": "$root123",
"is_falling_back": true,
"m.in_reply_to": map[string]any{
"event_id": "$last456",
},
},
}
got := extractThreadID(raw)
if got != "$root123" {
t.Errorf("expected $root123, got %q", got)
}
}
func TestExtractThreadID_ReplyNotThread(t *testing.T) {
raw := map[string]any{
"body": "hello",
"m.relates_to": map[string]any{
"m.in_reply_to": map[string]any{
"event_id": "$evt789",
},
},
}
got := extractThreadID(raw)
if got != "" {
t.Errorf("expected empty string for non-thread reply, got %q", got)
}
}
func TestExtractThreadID_NoRelatesTo(t *testing.T) {
raw := map[string]any{
"body": "plain message",
}
got := extractThreadID(raw)
if got != "" {
t.Errorf("expected empty string for plain message, got %q", got)
}
}
func TestExtractThreadID_ReplaceRelation(t *testing.T) {
raw := map[string]any{
"body": "edited",
"m.relates_to": map[string]any{
"rel_type": "m.replace",
"event_id": "$original",
},
}
got := extractThreadID(raw)
if got != "" {
t.Errorf("expected empty string for m.replace, got %q", got)
}
}
func TestExtractThreadID_ThreadWithoutEventID(t *testing.T) {
raw := map[string]any{
"m.relates_to": map[string]any{
"rel_type": "m.thread",
// missing event_id
},
}
got := extractThreadID(raw)
if got != "" {
t.Errorf("expected empty string for thread without event_id, got %q", got)
}
}