verify: 合并宽屏自适应与推理输出改造(测试服验证用)
This commit is contained in:
@@ -21,6 +21,24 @@ const (
|
||||
cardCreateURL = "https://api.dingtalk.com/v1.0/card/instances/createAndDeliver"
|
||||
cardStreamingURL = "https://api.dingtalk.com/v1.0/card/streaming"
|
||||
cardDebugPrivateKey = "_CARD_DEBUG_TOOL_ENTRY"
|
||||
|
||||
// cardWideScreenJSONKey is DingTalk's reserved system field inside
|
||||
// cardData.cardParamMap. Template variables whose value is a complex type
|
||||
// (object / array of objects) must be passed through this field as a JSON
|
||||
// string; plain string/number/boolean variables go in cardParamMap
|
||||
// directly. Putting a complex value anywhere else (for example as a
|
||||
// sibling of cardParamMap under cardData) leaves the template variable
|
||||
// unassigned and silently falls back to the default layout.
|
||||
cardWideScreenJSONKey = "sys_full_json_obj"
|
||||
|
||||
// cardWideScreenJSON configures the AI card's root "config": PC clients
|
||||
// render the card wide (auto layout) when autoLayout is true. The card
|
||||
// template must declare an object variable "config" with a boolean child
|
||||
// "autoLayout" (card builder > 数据源 > 编辑变量) and the value must be
|
||||
// passed when the card is created - setting it statically inside the
|
||||
// template does not take effect. Mobile clients keep their own adaptive
|
||||
// layout, so this only widens the card on desktop.
|
||||
cardWideScreenJSON = `{"config":{"autoLayout":true}}`
|
||||
)
|
||||
|
||||
// CardRequest is the input for sending a final AI card.
|
||||
@@ -229,6 +247,7 @@ func (s *CardSender) buildCardBody(req CardRequest, outTrackID string) map[strin
|
||||
"feedbackReasonSelected": "",
|
||||
"showFeedbackReasons": "false",
|
||||
"feedbackMessage": "",
|
||||
cardWideScreenJSONKey: cardWideScreenJSON,
|
||||
},
|
||||
},
|
||||
"userIdType": 1,
|
||||
|
||||
@@ -1289,6 +1289,117 @@ func TestOAuthAcrossStartAndUpdates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test: PC wide-screen layout config is delivered through the reserved
|
||||
// cardParamMap system field, never as a sibling of cardParamMap
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestCardWideScreenAutoLayoutParam(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
req CardRequest
|
||||
}{
|
||||
{
|
||||
name: "direct",
|
||||
req: CardRequest{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "user-456",
|
||||
Content: "Hello from AI",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "group",
|
||||
req: CardRequest{
|
||||
ConversationType: core.ConversationGroup,
|
||||
ExternalChatID: "chat-789",
|
||||
ExternalUserID: "user-456",
|
||||
Content: "Hello from AI",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ts := newCardTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := sender.StartCard(context.Background(), tc.req); err != nil {
|
||||
t.Fatalf("StartCard: %v", err)
|
||||
}
|
||||
|
||||
cardData, ok := ts.lastCreateBody["cardData"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("cardData missing or wrong type: %#v", ts.lastCreateBody["cardData"])
|
||||
}
|
||||
// Regression guard: a complex-typed value placed next to
|
||||
// cardParamMap leaves the template variable unassigned and the
|
||||
// card silently renders in the default narrow layout.
|
||||
for _, badKey := range []string{"config", cardWideScreenJSONKey} {
|
||||
if _, present := cardData[badKey]; present {
|
||||
t.Fatalf("%q must not be a sibling of cardParamMap: %#v", badKey, cardData)
|
||||
}
|
||||
}
|
||||
paramMap, ok := cardData["cardParamMap"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("cardParamMap missing or wrong type: %#v", cardData["cardParamMap"])
|
||||
}
|
||||
raw, ok := paramMap[cardWideScreenJSONKey].(string)
|
||||
if !ok {
|
||||
t.Fatalf("%s must be a JSON string, got %#v", cardWideScreenJSONKey, paramMap[cardWideScreenJSONKey])
|
||||
}
|
||||
var decoded struct {
|
||||
Config struct {
|
||||
AutoLayout bool `json:"autoLayout"`
|
||||
} `json:"config"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(raw), &decoded); err != nil {
|
||||
t.Fatalf("%s is not valid JSON (%q): %v", cardWideScreenJSONKey, raw, err)
|
||||
}
|
||||
if !decoded.Config.AutoLayout {
|
||||
t.Fatalf("config.autoLayout=false in %q, want true", raw)
|
||||
}
|
||||
// Plain string variables must keep going through cardParamMap directly.
|
||||
if paramMap["content"] != tc.req.Content {
|
||||
t.Errorf("content=%v, want %v", paramMap["content"], tc.req.Content)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test: streaming updates only carry content (no layout config)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestCardWideScreenNotSentOnStreamingUpdate(t *testing.T) {
|
||||
ts := newCardTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outTrackID, err := sender.StartCard(context.Background(), CardRequest{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "user-456",
|
||||
Content: "placeholder",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("StartCard: %v", err)
|
||||
}
|
||||
if err := sender.UpdateCard(context.Background(), outTrackID, "streamed chunk", false, false); err != nil {
|
||||
t.Fatalf("UpdateCard: %v", err)
|
||||
}
|
||||
if _, present := ts.lastStreamBody[cardWideScreenJSONKey]; present {
|
||||
t.Fatalf("streaming body must not carry %s: %#v", cardWideScreenJSONKey, ts.lastStreamBody)
|
||||
}
|
||||
if ts.lastStreamBody["key"] != "content" {
|
||||
t.Errorf("stream key=%v, want content", ts.lastStreamBody["key"])
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user