Files
larksuite__cli/shortcuts/sheets/chart_examples_test.go
xiongyuanwen-byted be2a96f490 feat(sheets): harden error prescriptions, batch updates, and read workflows
Aggregate the sheets work from feat/lark-sheets-develop:

- Improve validation errors with schema hints, aggregated issues, enum guidance, and prescriptive flag/style-field messages.
- Harden +batch-update input contracts, key normalization, style vocabulary handling, and resource-budget checks.
- Add read offload and truncation handling for cells, csv, and table-get, with typed output-path errors and safer jq/output-path semantics.
- Correct freeze semantics by emitting full-state freeze/unfreeze operations and adding --rows/--cols for +dim-freeze.
- Improve +styles-put and shared --styles parsing for styles, merges, row/column sizing, freeze, and sheet-prefixed range validation.
- Fix dim-insert inherit-style mapping, table-get date/time handling, table-put style anchors, and CSV path-shaped input guards.
- Update lark-sheets skill docs, scripts, tests, and generated flag data.

Tested with:
- go test ./shortcuts/common ./shortcuts/sheets/...
- go test ./shortcuts/... ./internal/...
- python3 -m py_compile skills/lark-sheets/scripts/*.py
2026-08-07 11:21:12 +08:00

110 lines
3.9 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)
}
})
}
}
// 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"])
}
}