package table import ( "strings" "testing" pdf "ragflow/internal/deepdoc/parser/pdf/type" ) func TestRowsToHTML(t *testing.T) { // rowsToHTML takes [][]pdf.TSRCell instead of [][]string (tableToHTML removed). toCells := func(rows [][]string) [][]pdf.TSRCell { out := make([][]pdf.TSRCell, len(rows)) for ri, row := range rows { out[ri] = make([]pdf.TSRCell, len(row)) for ci, s := range row { out[ri][ci] = pdf.TSRCell{Text: s} } } return out } t.Run("simple 2x2 table", func(t *testing.T) { rows := toCells([][]string{ {"姓名", "年龄"}, {"张三", "25"}, }) html := RowsToHTML(rows, "", nil, nil, nil) expected := "
姓名年龄
张三25
" if html != expected { t.Errorf("got %q\nwant %q", html, expected) } }) t.Run("empty table", func(t *testing.T) { html := RowsToHTML(nil, "", nil, nil, nil) if html != "
" { t.Errorf("expected '
', got %q", html) } }) t.Run("single cell", func(t *testing.T) { rows := toCells([][]string{{"X"}}) html := RowsToHTML(rows, "", nil, nil, nil) expected := "
X
" if html != expected { t.Errorf("got %q\nwant %q", html, expected) } }) t.Run("matches Python format for 公司差旅费", func(t *testing.T) { rows := toCells([][]string{ {"标职务", "飞机", "火车", "轮船", "其他交通工具(不含的士)"}, {"公司级领导人员", "经济舱位", "火车软席", "二等舱位", "按实报销"}, {"其他工作人员", "经济舱位", "火车硬席", "三等舱位", "按实报销"}, }) html := RowsToHTML(rows, "", nil, nil, nil) if !strings.HasPrefix(html, "") || !strings.HasSuffix(html, "
") { t.Errorf("not valid HTML: %s", html) } if !strings.Contains(html, "标职务") { t.Errorf("missing cell '标职务': %s", html) } if strings.Count(html, "") != 3 { t.Errorf("expected 3 rows, got %d", strings.Count(html, "")) } }) } func TestRowsToHTML_HeaderRows(t *testing.T) { cells := []pdf.TSRCell{ {X0: 0, Y0: 0, X1: 100, Y1: 30, Text: "Name", Label: "table column header"}, {X0: 101, Y0: 0, X1: 200, Y1: 30, Text: "Age", Label: "table column header"}, {X0: 0, Y0: 35, X1: 100, Y1: 65, Text: "John", Label: "table row"}, {X0: 101, Y0: 35, X1: 200, Y1: 65, Text: "30", Label: "table row"}, } // constructTable should produce for header row. item := &pdf.TableItem{} html := ConstructTable(cells, nil, "", item) // Header row should use , data row . if !strings.Contains(html, "") { t.Errorf("expected for header row. HTML: %s", html) } if strings.Count(html, " cells, got %d. HTML: %s", strings.Count(html, " cells (data row), got %d", strings.Count(html, "