feat: support quoted message headers

This commit is contained in:
richjohnrambo
2026-09-23 17:46:51 +08:00
parent aa9f562e1e
commit 3e92021254
5 changed files with 235 additions and 1 deletions
+24
View File
@@ -23,6 +23,8 @@ func TestValidateMessageHeadersProductContract(t *testing.T) {
{"x-im-schema": 1, "type": "audio", "x-im-type": "voice"},
{"x-im-schema": 1, "x-im-type": "system_notice"},
{"x-im-schema": 1, "x-im-type": "system_action"},
{"x-im-schema": 1, "x-im-type": "text", "x-im-reply": map[string]any{"seq": float64(128), "type": "text", "text": "hello", "from": "usr1"}},
{"x-im-schema": 1, "x-im-type": "text", "x-im-reply": `{"seq":128,"type":"text","text":"hello","from":"usr1"}`},
{"type": "system"},
}
for _, head := range valid {
@@ -36,6 +38,10 @@ func TestValidateMessageHeadersProductContract(t *testing.T) {
{"x-im-schema": true, "x-im-type": "text"},
{"x-im-schema": float64(1), "x-im-type": "unknown"},
{"type": "text", "x-im-type": "image"},
{"x-im-schema": 1, "x-im-type": "text", "x-im-reply": map[string]any{"seq": 0, "type": "text", "text": "hello"}},
{"x-im-schema": 1, "x-im-type": "text", "x-im-reply": map[string]any{"seq": 1.5, "type": "text", "text": "hello"}},
{"x-im-schema": 1, "x-im-type": "text", "x-im-reply": map[string]any{"seq": 1, "type": "text"}},
{"x-im-schema": 1, "x-im-type": "text", "x-im-reply": "not-json"},
}
for _, head := range invalid {
if err := validateMessageHeaders(head); err != types.ErrMalformed {
@@ -145,3 +151,21 @@ func TestActionTokenCachePreservesInternalFields(t *testing.T) {
t.Fatalf("cache round trip lost fields: got %#v", got)
}
}
func TestValidateMessageHeadersNormalizesReplyJSONString(t *testing.T) {
head := types.KVMap{
"x-im-schema": 1,
"x-im-type": "text",
"x-im-reply": `{"seq":128,"type":"text","text":"hello","from":"usr1"}`,
}
if err := validateMessageHeaders(head); err != nil {
t.Fatalf("validateMessageHeaders() = %v, want nil", err)
}
reply, ok := head["x-im-reply"].(map[string]any)
if !ok {
t.Fatalf("x-im-reply was not normalized to object: %#v", head["x-im-reply"])
}
if reply["seq"].(float64) != 128 || reply["type"] != "text" || reply["text"] != "hello" || reply["from"] != "usr1" {
t.Fatalf("unexpected normalized reply: %#v", reply)
}
}