(new): rich message support
Golang lint / lint (pull_request) Successful in 1m20s
Golang lint / lint (push) Successful in 4m8s

(fix): runtime reliability
(tests): regression coverage
(doc): v1.1 release notes
This commit is contained in:
2026-08-12 16:34:44 +03:00
parent 48ddf66540
commit f03a081ed6
83 changed files with 6122 additions and 1925 deletions
+56
View File
@@ -10,9 +10,65 @@ import (
"testing"
"git.scuroneko.dev/scuroneko/laniakea/tgapi"
"git.scuroneko.dev/scuroneko/laniakea/tgrich"
"git.scuroneko.dev/scuroneko/sneklog/v2"
)
func TestRichAnswerBuildsInputBlocks(t *testing.T) {
var gotBody map[string]any
client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(body, &gotBody); err != nil {
t.Fatal(err)
}
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"ok":true,"result":{"message_id":9,"date":1}}`)),
}, nil
})}
api := tgapi.NewAPI(tgapi.NewAPIOpts("token").SetAPIURL("https://example.test").SetHTTPClient(client))
defer func() { _ = api.Close() }()
ctx := &MessageContext{
API: api,
Msg: &tgapi.Message{Chat: &tgapi.Chat{ID: 42, Type: tgapi.ChatTypePrivate}},
Logger: sneklog.NewLogger(),
}
answer := ctx.RichAnswer(tgrich.P(tgrich.Bold(tgrich.Text("ready"))))
if answer == nil {
t.Fatal("RichAnswer() returned nil")
}
rich, ok := gotBody["rich_message"].(map[string]any)
if !ok || rich["html"] != "<p><b>ready</b></p>" {
t.Fatalf("rich_message = %#v", gotBody["rich_message"])
}
if _, exists := rich["skip_entity_detection"]; exists {
t.Fatalf("rich_message unexpectedly disables entity detection: %#v", rich)
}
}
func TestRichAnswerRejectsInvalidBlocksWithoutRequest(t *testing.T) {
client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
t.Fatal("unexpected HTTP request")
return nil, nil
})}
api := tgapi.NewAPI(tgapi.NewAPIOpts("token").SetAPIURL("https://example.test").SetHTTPClient(client))
defer func() { _ = api.Close() }()
ctx := &MessageContext{
API: api,
Msg: &tgapi.Message{Chat: &tgapi.Chat{ID: 42, Type: tgapi.ChatTypePrivate}},
Logger: sneklog.NewLogger(),
}
if answer := ctx.RichAnswer(tgrich.H(tgrich.Text("invalid"), 0)); answer != nil {
t.Fatal("RichAnswer() returned an answer for an invalid heading")
}
}
func TestAnswerPhotoIncludesDirectMessagesTopicID(t *testing.T) {
var gotBody map[string]any