package dingtalk import ( "context" "strings" "testing" ) func TestNormalizeKeepsCompactTableUnchanged(t *testing.T) { content := "重量如下:\n\n| 产品型号 | 净重 (本体重量) | 毛重 (含包装) |\n| :--- | :--- | :--- |\n| CR3A | 16.5 kg / 17.5 kg | 28.5 kg |\n| CR5A | 25 kg | 37 kg |\n\n结束" got := normalizeWideMarkdownTables(content) if got != content { t.Fatalf("compact table must pass through untouched, got %q", got) } } func TestNormalizeConvertsWideTableToGroupedList(t *testing.T) { longCell := "J1: 180°/s (CR3A/5A/7A), 150°/s (CR10A/12A/16A)
J2: 180°/s (CR3A/5A/7A)" content := "差异如下:\n\n| 对比项 | CR 系列 | CRA 系列 |\n| :--- | :--- | :--- |\n| **关节最大速度** | " + longCell + " | J1: 180°/s (CR3A/5A/7A), 150°/s (CR10A/12A/16A)
J2: 更多参数说明 |\n| 控制柜 | CC162 | CC262 |\n\n结论" got := normalizeWideMarkdownTables(content) if strings.Contains(got, "| 对比项") || strings.Contains(got, "
") { t.Fatalf("wide table must be converted and
removed, got %q", got) } if !strings.HasPrefix(got, "差异如下:\n\n") || !strings.HasSuffix(got, "\n\n结论") { t.Fatalf("surrounding text must be preserved, got %q", got) } // compact row (控制柜) from the same table is now a list entry too if !strings.Contains(got, "**关节最大速度**\n- CR 系列:") || !strings.Contains(got, "- CRA 系列:") { t.Fatalf("row key must become bold heading with header-labelled values, got %q", got) } if !strings.Contains(got, "**控制柜**\n- CR 系列:CC162\n- CRA 系列:CC262") { t.Fatalf("every row must become a group, got %q", got) } if strings.Contains(got, "J1: 180°/s") == false { t.Fatalf("cell content must be preserved, got %q", got) } } func TestNormalizeReplacesBrKeepsTableWhenCellsShort(t *testing.T) { content := "| A | B |\n| --- | --- |\n| 短 | 行一
行二 |" got := normalizeWideMarkdownTables(content) if strings.Contains(got, "
") { t.Fatalf("
must be replaced, got %q", got) } if !strings.Contains(got, "| A | B |") || !strings.Contains(got, "行一;行二") { t.Fatalf("short table must stay a table with ; separator, got %q", got) } } func TestNormalizeSkipsFencedTables(t *testing.T) { long := strings.Repeat("长", 30) content := "```\n| 对比 | " + long + " |\n| --- | --- |\n| a | b |\n```\n正文" got := normalizeWideMarkdownTables(content) if got != content { t.Fatalf("fenced content must never be rewritten, got %q", got) } } func TestNormalizeKeepsWideTableWhenGroupingDisabled(t *testing.T) { longCell := "J1: 180°/s (CR3A/5A/7A), 150°/s (CR10A/12A/16A)
J2: 180°/s (CR3A/5A/7A)" content := "差异如下:\n\n| 对比项 | CR 系列 | CRA 系列 |\n| :--- | :--- | :--- |\n| **关节最大速度** | " + longCell + " | J1: 180°/s (CR3A/5A/7A)
J2: 更多参数说明 |\n| 控制柜 | CC162 | CC262 |\n\n结论" options := defaultTableNormalizeOptions() options.groupWideTables = false got := normalizeMarkdownTables(content, options) if !strings.Contains(got, "| 对比项 | CR 系列 | CRA 系列 |") || !strings.Contains(got, "| **关节最大速度** |") { t.Fatalf("wide table must stay a table when grouping is disabled, got %q", got) } if strings.Contains(got, "**关节最大速度**\n- CR 系列:") { t.Fatalf("grouped list must not be produced when grouping is disabled, got %q", got) } if strings.Contains(got, "
") { t.Fatalf("
must still be replaced inside cells, got %q", got) } if !strings.Contains(got, ";") { t.Fatalf("cell line breaks must become the full-width separator, got %q", got) } if !strings.HasPrefix(got, "差异如下:\n\n") || !strings.HasSuffix(got, "\n\n结论") { t.Fatalf("surrounding text must be preserved, got %q", got) } } // TestRewriteOutboundContentWideTableGroupingToggle proves the handler wires // the switch through to the final frame, which is what the client sees. func TestRewriteOutboundContentWideTableGroupingToggle(t *testing.T) { wide := "| 对比项 | CR 系列 | CRA 系列 |\n| :--- | :--- | :--- |\n| **关节最大速度** | " + strings.Repeat("J1: 180°/s (CR3A/5A/7A) ", 6) + " | CC262 |" cases := []struct { name string disabled bool wantTable bool wantDetail string }{ {name: "grouping enabled rewrites wide table", disabled: false, wantTable: false, wantDetail: "**关节最大速度**"}, {name: "grouping disabled keeps wide table", disabled: true, wantTable: true, wantDetail: "| 对比项 |"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { h, err := NewHandlerWithOptions( TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"}, &fakeQA{}, &fakeReply{}, HandlerOptions{WideTableGroupingDisabled: tc.disabled}, ) if err != nil { t.Fatalf("NewHandlerWithOptions: %v", err) } got := h.rewriteOutboundContent(context.Background(), wide) if strings.Contains(got, "| 对比项 |") != tc.wantTable { t.Fatalf("table present = %v, want %v, got %q", strings.Contains(got, "| 对比项 |"), tc.wantTable, got) } if !strings.Contains(got, tc.wantDetail) { t.Fatalf("missing %q in %q", tc.wantDetail, got) } }) } } func TestNormalizeHandlesEscapedPipesAndEmptyHeaders(t *testing.T) { long := strings.Repeat("x", 40) content := "| 项 | | \n| --- | --- |\n| a\\|b | " + long + " |" got := normalizeWideMarkdownTables(content) if strings.Contains(got, "| a\\|b") { t.Fatalf("escaped pipe cell should be converted out of table form, got %q", got) } if !strings.Contains(got, "a|b") { t.Fatalf("escaped pipe must be restored as literal, got %q", got) } if !strings.Contains(got, "第2项:") { t.Fatalf("missing header should fall back to ordinal label, got %q", got) } }