Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions shortcuts/base/base_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,83 @@ func TestBaseFieldExecuteUpdate(t *testing.T) {
}
}

func TestBaseFieldExecuteUpdateButtonUsesLowLevelTriggerPayload(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
stub := &httpmock.Stub{
Method: "PUT",
URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/fields/fld_x",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"id": "fld_x",
"name": "同步到 CRM",
"type": buttonFieldLowLevelType,
"fieldUIType": buttonFieldUIType,
"property": map[string]interface{}{
"button": map[string]interface{}{"title": "同步到 CRM", "color": 0},
"trigger": map[string]interface{}{
"type": buttonTriggerAutomation,
"config": map[string]interface{}{"id": "wkf_x"},
},
},
},
},
}
reg.Register(stub)

jsonBody := `{"name":"同步到 CRM","type":"button","button":{"title":"同步到 CRM","color":0},"trigger":{"type":"automation","workflow_id":"wkf_x"}}`
if err := runShortcut(t, BaseFieldUpdate, []string{"+field-update", "--base-token", "app_x", "--table-id", "tbl_x", "--field-id", "fld_x", "--json", jsonBody, "--yes"}, factory, stdout); err != nil {
t.Fatalf("err=%v", err)
}

body := decodeCapturedJSONBody(t, stub)
if body["name"] != "同步到 CRM" {
t.Fatalf("request body=%#v", body)
}
if body["type"] != float64(buttonFieldLowLevelType) {
t.Fatalf("request body.type=%#v, want %d", body["type"], buttonFieldLowLevelType)
}
if body["fieldUIType"] != buttonFieldUIType {
t.Fatalf("request body.fieldUIType=%#v, want %q", body["fieldUIType"], buttonFieldUIType)
}
if _, ok := body["button"]; ok {
t.Fatalf("request body must not keep top-level button: %#v", body)
}
if _, ok := body["trigger"]; ok {
t.Fatalf("request body must not keep top-level trigger: %#v", body)
}

property, ok := body["property"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property=%#v", body["property"])
}
button, ok := property["button"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property.button=%#v", property["button"])
}
trigger, ok := property["trigger"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property.trigger=%#v", property["trigger"])
}
config, ok := trigger["config"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property.trigger.config=%#v", trigger["config"])
}
if common.GetString(button, "title") != "同步到 CRM" || button["color"] != float64(0) {
t.Fatalf("request body.property.button=%#v", button)
}
if trigger["type"] != float64(buttonTriggerAutomation) || common.GetString(config, "id") != "wkf_x" {
t.Fatalf("request body.property.trigger=%#v", trigger)
}

got := stdout.String()
for _, want := range []string{`"updated": true`, `"field_get_recommended": true`, `"next_step": "field_get"`, `"verification_hint"`} {
if !strings.Contains(got, want) {
t.Fatalf("stdout missing %q:\n%s", want, got)
}
}
}

func TestFieldUpdateResultAlwaysRecommendsReadback(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -888,6 +965,32 @@ func TestFieldUpdateResultAlwaysRecommendsReadback(t *testing.T) {
submitted: map[string]interface{}{"name": "Amount"},
hintContains: []string{"unknown or uncommon field type", "+field-get"},
},
{
name: "button field still recommends readback",
field: map[string]interface{}{
"type": buttonFieldLowLevelType,
"fieldUIType": buttonFieldUIType,
"property": map[string]interface{}{
"button": map[string]interface{}{"title": "同步到 CRM", "color": 0},
"trigger": map[string]interface{}{
"type": buttonTriggerAutomation,
"config": map[string]interface{}{"id": "wkf_x"},
},
},
},
submitted: map[string]interface{}{
"type": buttonFieldLowLevelType,
"fieldUIType": buttonFieldUIType,
"property": map[string]interface{}{
"button": map[string]interface{}{"title": "同步到 CRM", "color": 0},
"trigger": map[string]interface{}{
"type": buttonTriggerAutomation,
"config": map[string]interface{}{"id": "wkf_x"},
},
},
},
hintContains: []string{"generated field update", "+field-get"},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -1332,6 +1435,83 @@ func TestBaseFieldExecuteCRUD(t *testing.T) {
}
})

t.Run("create button field normalizes to low-level payload", func(t *testing.T) {
factory, stdout, reg := newExecuteFactory(t)
createStub := &httpmock.Stub{
Method: "POST",
URL: "/open-apis/base/v3/bases/app_x/tables/tbl_x/fields",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"id": "fld_btn",
"name": "同步到 CRM",
"type": buttonFieldLowLevelType,
"fieldUIType": buttonFieldUIType,
"property": map[string]interface{}{
"button": map[string]interface{}{"title": "同步到 CRM", "color": 0},
"trigger": map[string]interface{}{
"type": buttonTriggerAutomation,
"config": map[string]interface{}{"id": "wkf_x"},
},
},
},
},
}
reg.Register(createStub)

jsonBody := `{"name":"同步到 CRM","type":"button","button":{"title":"同步到 CRM","color":0},"trigger":{"type":"automation","workflow_id":"wkf_x"}}`
if err := runShortcut(t, BaseFieldCreate, []string{"+field-create", "--base-token", "app_x", "--table-id", "tbl_x", "--json", jsonBody}, factory, stdout); err != nil {
t.Fatalf("err=%v", err)
}

body := decodeCapturedJSONBody(t, createStub)
if body["name"] != "同步到 CRM" {
t.Fatalf("request body=%#v", body)
}
if body["type"] != float64(buttonFieldLowLevelType) {
t.Fatalf("request body.type=%#v, want %d", body["type"], buttonFieldLowLevelType)
}
if body["fieldUIType"] != buttonFieldUIType {
t.Fatalf("request body.fieldUIType=%#v, want %q", body["fieldUIType"], buttonFieldUIType)
}
if _, ok := body["button"]; ok {
t.Fatalf("request body must not keep top-level button: %#v", body)
}
if _, ok := body["trigger"]; ok {
t.Fatalf("request body must not keep top-level trigger: %#v", body)
}

property, ok := body["property"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property=%#v", body["property"])
}
button, ok := property["button"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property.button=%#v", property["button"])
}
trigger, ok := property["trigger"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property.trigger=%#v", property["trigger"])
}
config, ok := trigger["config"].(map[string]interface{})
if !ok {
t.Fatalf("request body.property.trigger.config=%#v", trigger["config"])
}
if common.GetString(button, "title") != "同步到 CRM" || button["color"] != float64(0) {
t.Fatalf("request body.property.button=%#v", button)
}
if trigger["type"] != float64(buttonTriggerAutomation) || common.GetString(config, "id") != "wkf_x" {
t.Fatalf("request body.property.trigger=%#v", trigger)
}

got := stdout.String()
for _, want := range []string{`"created": true`, `"fld_btn"`, `"field_get_recommended": true`, `"next_step": "field_get"`, `"verification_hint"`} {
if !strings.Contains(got, want) {
t.Fatalf("stdout missing %q:\n%s", want, got)
}
}
})

t.Run("create array sequentially", func(t *testing.T) {
oldDelay := fieldCreateBatchDelay
fieldCreateBatchDelay = 0
Expand Down
1 change: 1 addition & 0 deletions shortcuts/base/field_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var BaseFieldCreate = common.Shortcut{
Tips: []string{
`Example text: lark-cli base +field-create --base-token <base_token> --table-id <table_id> --json '{"name":"Status","type":"text"}'`,
`Example select: lark-cli base +field-create --base-token <base_token> --table-id <table_id> --json '{"name":"Status","type":"select","multiple":false,"options":[{"name":"Todo"},{"name":"Done"}]}'`,
`Example button: create the workflow first, then bind it with lark-cli base +field-create --base-token <base_token> --table-id <table_id> --json '{"name":"Sync to CRM","type":"button","button":{"title":"Sync to CRM","color":0},"trigger":{"type":"automation","workflow_id":"wkf_xxx"}}'`,
"Agent hint: use the lark-base skill's field-create guide for usage and limits.",
},
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
Expand Down
Loading
Loading