// Copyright (c) 2026 Lark Technologies Pte. Ltd. // SPDX-License-Identifier: MIT package convertlib import "testing" func TestTextConverterConvert(t *testing.T) { ctx := &ConvertContext{ RawContent: `{"text":"hi @_user_1"}`, MentionMap: map[string]string{"@_user_1": "Alice"}, } if got := (textConverter{}).Convert(ctx); got != "hi @Alice" { t.Fatalf("textConverter.Convert() = %q, want %q", got, "hi @Alice") } } func TestTextConverterConvertFallsBackToRawContent(t *testing.T) { ctx := &ConvertContext{RawContent: `{"message":"no text field"}`} if got := (textConverter{}).Convert(ctx); got != ctx.RawContent { t.Fatalf("textConverter.Convert() = %q, want raw content %q", got, ctx.RawContent) } } func TestTextConverterConvertInvalidJSON(t *testing.T) { ctx := &ConvertContext{RawContent: `{invalid`} if got := (textConverter{}).Convert(ctx); got != "[Invalid text JSON]" { t.Fatalf("textConverter.Convert() = %q, want %q", got, "[Invalid text JSON]") } } func TestPostConverterConvert(t *testing.T) { ctx := &ConvertContext{ RawContent: `{"zh_cn":{"title":"Weekly Update","content":[[{"tag":"text","text":"Hello "},{"tag":"at","user_name":"Alice"}],[{"tag":"a","text":"Spec","href":"https://example.com/spec"}]]}}`, MentionMap: map[string]string{}, } want := "Weekly Update\nHello @Alice\n[Spec](https://example.com/spec)" if got := (postConverter{}).Convert(ctx); got != want { t.Fatalf("postConverter.Convert() = %q, want %q", got, want) } } func TestPostConverterConvertFallback(t *testing.T) { tests := []struct { name string raw string want string }{ {name: "invalid json", raw: `{invalid`, want: "[Invalid rich text JSON]"}, {name: "no locale body", raw: `{"unknown":"value"}`, want: "[Rich text message]"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := (postConverter{}).Convert(&ConvertContext{RawContent: tt.raw}); got != tt.want { t.Fatalf("postConverter.Convert() = %q, want %q", got, tt.want) } }) } } func TestUnwrapPostLocale(t *testing.T) { direct := map[string]interface{}{"title": "Direct"} if got := unwrapPostLocale(direct); got["title"] != "Direct" { t.Fatalf("unwrapPostLocale(direct) = %#v, want direct body", got) } localized := map[string]interface{}{ "zh_cn": map[string]interface{}{"title": "Chinese"}, } if got := unwrapPostLocale(localized); got["title"] != "Chinese" { t.Fatalf("unwrapPostLocale(localized) = %#v, want zh_cn body", got) } deterministicFallback := map[string]interface{}{ "z_locale": map[string]interface{}{"title": "Zulu"}, "a_locale": map[string]interface{}{"title": "Alpha"}, } if got := unwrapPostLocale(deterministicFallback); got["title"] != "Alpha" { t.Fatalf("unwrapPostLocale(deterministic fallback) = %#v, want alphabetically first locale body", got) } } func TestRenderPostElem(t *testing.T) { tests := []struct { name string el map[string]interface{} want string }{ {name: "text", el: map[string]interface{}{"tag": "text", "text": "hello"}, want: "hello"}, {name: "link", el: map[string]interface{}{"tag": "a", "text": "doc", "href": "https://example.com"}, want: "[doc](https://example.com)"}, {name: "mention all", el: map[string]interface{}{"tag": "at", "user_id": "@_all"}, want: "@all"}, {name: "mention user", el: map[string]interface{}{"tag": "at", "user_name": "Alice"}, want: "@Alice"}, {name: "image", el: map[string]interface{}{"tag": "img", "image_key": "img_123"}, want: "[Image: img_123]"}, {name: "media", el: map[string]interface{}{"tag": "media", "file_key": "file_123"}, want: "[Media: file_123]"}, {name: "code block", el: map[string]interface{}{"tag": "code_block", "language": "go", "text": "fmt.Println(1)"}, want: "\n```go\nfmt.Println(1)\n```\n"}, {name: "hr", el: map[string]interface{}{"tag": "hr"}, want: "\n---\n"}, {name: "unknown", el: map[string]interface{}{"tag": "unknown", "text": "fallback"}, want: "fallback"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := renderPostElem(tt.el); got != tt.want { t.Fatalf("renderPostElem() = %q, want %q", got, tt.want) } }) } }