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:
@@ -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"])
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user