Files
Enterprise-Knowledge-Base/internal/im/mattermost/adapter_test.go
T
ochan.kwon 2d5782992e [NELO-1102] feat(im): add thread-based session mode for IM channels
Add session_mode ("user" | "thread") to IM channel config. In thread
mode, each message thread gets its own session, enabling multi-user
collaboration within the same thread context.

Backend:
- Add SessionMode type constants and validation
- Add ThreadID field to IncomingMessage and ChannelSession
- Split resolveSession into resolveUserSession/resolveThreadSession
- Update makeUserKey to include threadID only in thread mode
- Extract ThreadID in Slack, Mattermost, Feishu, Telegram adapters
- Add message_thread_id to Telegram SendReply/StartStream for Forums
- DB migration 000028: session_mode column + thread_id column + index

Frontend:
- Add session_mode radio group to IMChannelPanel dialog
- Auto-reset session_mode when switching to non-thread platform
- Show thread badge on channel card for thread-mode channels
- Add i18n keys for en-US, ko-KR, zh-CN, ru-RU
2026-03-30 10:43:14 +08:00

154 lines
3.6 KiB
Go

package mattermost
import (
"encoding/json"
"testing"
)
func TestParseOutgoingBody_ThreadRoot(t *testing.T) {
tests := []struct {
name string
payload outgoingPayload
postReplyToMain bool
wantThreadRoot string
}{
{
name: "threaded reply has RootID",
payload: outgoingPayload{
PostID: "post-123",
RootID: "root-456",
},
postReplyToMain: false,
wantThreadRoot: "root-456",
},
{
name: "top-level message uses PostID as thread root",
payload: outgoingPayload{
PostID: "post-789",
RootID: "",
},
postReplyToMain: false,
wantThreadRoot: "post-789",
},
{
name: "postReplyToMain clears thread root",
payload: outgoingPayload{
PostID: "post-123",
RootID: "root-456",
},
postReplyToMain: true,
wantThreadRoot: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var threadRoot string
if tt.postReplyToMain {
threadRoot = ""
} else {
threadRoot = tt.payload.RootID
if threadRoot == "" {
threadRoot = tt.payload.PostID
}
}
if threadRoot != tt.wantThreadRoot {
t.Errorf("threadRoot = %q, want %q", threadRoot, tt.wantThreadRoot)
}
})
}
}
func TestParseOutgoingBody_JSON(t *testing.T) {
body := `{"token":"tok","user_id":"u1","user_name":"alice","channel_id":"ch1","post_id":"p1","text":"hello","root_id":"r1"}`
payload, err := parseOutgoingBody("application/json", []byte(body))
if err != nil {
t.Fatalf("parseOutgoingBody error: %v", err)
}
if payload.RootID != "r1" {
t.Errorf("RootID = %q, want %q", payload.RootID, "r1")
}
if payload.PostID != "p1" {
t.Errorf("PostID = %q, want %q", payload.PostID, "p1")
}
}
func TestParseOutgoingBody_FormEncoded(t *testing.T) {
body := "token=tok&user_id=u1&channel_id=ch1&post_id=p2&text=hello&root_id=r2"
payload, err := parseOutgoingBody("application/x-www-form-urlencoded", []byte(body))
if err != nil {
t.Fatalf("parseOutgoingBody error: %v", err)
}
if payload.RootID != "r2" {
t.Errorf("RootID = %q, want %q", payload.RootID, "r2")
}
if payload.PostID != "p2" {
t.Errorf("PostID = %q, want %q", payload.PostID, "p2")
}
}
func TestThreadIDInMessage(t *testing.T) {
// Simulate the adapter logic: build the message and verify ThreadID
payload := outgoingPayload{
Token: "tok",
UserID: "user-1",
UserName: "alice",
ChannelID: "ch-1",
PostID: "post-100",
Text: "hello",
RootID: "root-200",
}
threadRoot := payload.RootID
if threadRoot == "" {
threadRoot = payload.PostID
}
// Verify the Extra map and ThreadID would be consistent
extra := map[string]string{
extraKeyThreadRoot: threadRoot,
extraKeyChannelID: payload.ChannelID,
}
if extra[extraKeyThreadRoot] != "root-200" {
t.Errorf("Extra thread_root_id = %q, want %q", extra[extraKeyThreadRoot], "root-200")
}
// ThreadID should equal threadRoot
threadID := threadRoot
if threadID != "root-200" {
t.Errorf("ThreadID = %q, want %q", threadID, "root-200")
}
}
func TestParseFileIDs(t *testing.T) {
tests := []struct {
name string
raw json.RawMessage
want []string
}{
{"array", json.RawMessage(`["f1","f2"]`), []string{"f1", "f2"}},
{"string", json.RawMessage(`"f3"`), []string{"f3"}},
{"empty", nil, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseFileIDs(tt.raw)
if len(got) != len(tt.want) {
t.Fatalf("parseFileIDs() len = %d, want %d", len(got), len(tt.want))
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("parseFileIDs()[%d] = %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}