Files
zhengzhijiej-tech 4120a6b47c feat(sheets): add chart sizing and quality checks (#2504)
* fix(sheets): preserve chart axes and clarify label defaults

* docs(sheets): refine chart display defaults

* feat(sheets): support per-series chart data labels

* Revert "feat(sheets): support per-series chart data labels"

This reverts commit fc7a9cc745cf4963447b0682ce2df023994bf70d.

* refactor(sheets): use sparse scatter markers for point labels

* fix(sheets): validate chart data sources in layout check

* feat(sheets): support per-series chart data labels

* Revert "feat(sheets): support per-series chart data labels"

This reverts commit fc7a9cc745cf4963447b0682ce2df023994bf70d.

* fix(sheets): expose raw cell values for chart checks

* feat(sheets): support aggregate categories flag

* feat(sheets): add chart quality and size helpers

* fix(sheets): refine chart data label handling

* fix(sheets): refine chart sizing recommendations

* fix(sheets): deduplicate aggregated category labels

* fix(sheets): sync refined chart sizing guidance

* docs(sheets): sync dense data label guidance

* fix(sheets): align chart size label default

* fix(sheets): remove dense data label advice

* docs(sheets): clarify chart labels and category axis

* fix(sheets): detect overwide charts in quality checks

* fix(sheets): detect unbound secondary axes

* fix(sheets): sync chart quality guidance

* fix(sheets): bound chart source sampling and align schemas

* fix(sheets): validate chart source types without cells-get extensions

* fix(sheets): address chart quality review feedback

* fix(sheets): harden chart source and axis sizing checks

* fix(sheets): address chart quality review findings

* fix(sheets): keep bar Top-N threshold above 24 categories

* fix(sheets): align chart examples with size minimums

* fix(sheets): detect positionless continuous X axes
2026-09-08 14:45:22 +08:00

164 lines
5.6 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package sheets
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
// TestChartPrintExample pins the --print-example contract: a known type
// prints its template and skips execution entirely; an unknown type lists
// the available ones.
func TestChartPrintExample(t *testing.T) {
t.Parallel()
t.Run("prints template without locator flags", func(t *testing.T) {
t.Parallel()
sc := shortcutFromRegistry(t, "+chart-create")
parent, _, _, _ := newTestRig(t, sc)
var buf bytes.Buffer
parent.SetOut(&buf) // --print-example writes via cobra's OutOrStdout
parent.SetArgs([]string{sc.Command, "--print-example", "pie"})
if err := parent.Execute(); err != nil {
t.Fatalf("print-example should run standalone, got: %v", err)
}
if !strings.Contains(buf.String(), `"sectors"`) {
t.Errorf("pie template should carry sectors, got %q", buf.String())
}
})
t.Run("unknown type lists available", func(t *testing.T) {
t.Parallel()
sc := shortcutFromRegistry(t, "+chart-create")
_, _, err := runShortcutCapturingErr(t, sc, []string{"--print-example", "donut"})
ve := requireValidation(t, err, `no example for chart type "donut"`)
if !strings.Contains(ve.Message, "pie") {
t.Errorf("message should list available types, got %q", ve.Message)
}
if ve.Param != "--print-example" {
t.Errorf("Param = %q, want %q", ve.Param, "--print-example")
}
})
}
// TestChartExampleTemplates_ValidateAgainstSchema drift-guards every
// template against the embedded chart-create properties schema — a template
// the CLI itself would reject is worse than none.
func TestChartExampleTemplates_ValidateAgainstSchema(t *testing.T) {
t.Parallel()
for typ, tmpl := range chartExampleTemplates {
t.Run(typ, func(t *testing.T) {
t.Parallel()
var v interface{}
if err := json.Unmarshal([]byte(tmpl), &v); err != nil {
t.Fatalf("template is not valid JSON: %v", err)
}
fv := newMapFlagViewForCommand("+chart-create", map[string]interface{}{"properties": v})
if err := validateValueAgainstSchema(fv, "properties", v); err != nil {
t.Errorf("template rejected by embedded schema: %v", err)
}
})
}
}
// TestChartExampleTemplates_MeetQualityMinimumSizes keeps the ready-to-edit
// templates aligned with the minimums enforced by lark_chart_quality_check.py.
func TestChartExampleTemplates_MeetQualityMinimumSizes(t *testing.T) {
t.Parallel()
type size struct {
Width float64 `json:"width"`
Height float64 `json:"height"`
}
overrides := map[string]size{
"bar": {Width: 720, Height: 420},
"combo": {Width: 720, Height: 420},
"pie": {Width: 720, Height: 440},
}
for typ, tmpl := range chartExampleTemplates {
t.Run(typ, func(t *testing.T) {
t.Parallel()
var properties struct {
Size size `json:"size"`
}
if err := json.Unmarshal([]byte(tmpl), &properties); err != nil {
t.Fatalf("template is not valid JSON: %v", err)
}
minimum := size{Width: 640, Height: 400}
if override, ok := overrides[typ]; ok {
minimum = override
}
if properties.Size.Width < minimum.Width || properties.Size.Height < minimum.Height {
t.Errorf("template size = %.0fx%.0f, minimum = %.0fx%.0f",
properties.Size.Width, properties.Size.Height, minimum.Width, minimum.Height)
}
})
}
}
func TestChartExampleTemplates_SpecialChartContracts(t *testing.T) {
t.Parallel()
tests := map[string][]string{
"bubble": {`"role": "x"`, `"role": "y"`, `"role": "group"`, `"role": "size"`},
"waterfall": {`"firstValueAsTotal"`, `"lastValueAsSubtotal"`, `"connectorLine"`},
"pareto": {`"aggregateType": "sum"`, `"index": 1`, `"index": 2`, `"percentage": true`},
}
for typ, markers := range tests {
tmpl, ok := chartExampleTemplates[typ]
if !ok {
t.Fatalf("missing %s template", typ)
}
for _, marker := range markers {
if !strings.Contains(tmpl, marker) {
t.Errorf("%s template missing %s", typ, marker)
}
}
}
}
// TestNormalizeChartHexColors_Arrays pins color normalization inside arrays:
// the chart schema uses colorTheme / colorScale / highlight_colors, whose
// values are LISTS of bare hex strings. Recursing without the key context
// dropped the "#" prefix and the server rejected a payload its own schema
// allows.
func TestNormalizeChartHexColors_Arrays(t *testing.T) {
t.Parallel()
in := map[string]interface{}{
"colorTheme": []interface{}{"4472C4", "ED7D31"},
"highlight_colors": []interface{}{"FF0000"},
"colorScale": []interface{}{map[string]interface{}{"color": "70AD47"}},
"backgroundColor": "4472C4",
"colorMode": "auto",
"title": []interface{}{"4472C4"},
}
raw, err := json.Marshal(normalizeChartHexColors(in))
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got map[string]interface{}
if err := json.Unmarshal(raw, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
theme := got["colorTheme"].([]interface{})
if theme[0] != "#4472C4" || theme[1] != "#ED7D31" {
t.Errorf("colorTheme = %v, want both prefixed", theme)
}
if got["highlight_colors"].([]interface{})[0] != "#FF0000" {
t.Errorf("highlight_colors = %v", got["highlight_colors"])
}
if got["colorScale"].([]interface{})[0].(map[string]interface{})["color"] != "#70AD47" {
t.Errorf("colorScale = %v", got["colorScale"])
}
// Non-hex values under a color-ish key, and hex-looking values under a
// non-color key, must both be left alone.
if got["colorMode"] != "auto" {
t.Errorf("colorMode = %v, want untouched", got["colorMode"])
}
if got["title"].([]interface{})[0] != "4472C4" {
t.Errorf("title = %v, want untouched (not a color key)", got["title"])
}
}