fix: report AI table binding validation failures

This commit is contained in:
姚章浩
2026-08-14 11:11:07 +08:00
parent b4b68d8b21
commit a3defdd1c0
3 changed files with 108 additions and 3 deletions
+57
View File
@@ -2,6 +2,7 @@ package settings
import (
"context"
"errors"
"strings"
"testing"
"time"
@@ -13,6 +14,7 @@ type memoryRepo struct {
bindings map[string]ResourceBinding
candidate BootstrapCandidate
sessions map[string]SettingSession
saveErr error
}
func newMemoryRepo() *memoryRepo {
@@ -26,6 +28,9 @@ func (r *memoryRepo) SetPreference(_ context.Context, tenant, channel, staff, lo
return nil
}
func (r *memoryRepo) SaveBinding(_ context.Context, b ResourceBinding) error {
if r.saveErr != nil {
return r.saveErr
}
r.bindings[b.Kind] = b
return nil
}
@@ -184,6 +189,58 @@ func TestControllerFAQBindingPromptExplainsPermissionsSchemaActivationAndLinkCar
}
}
}
func TestControllerReportsFAQPermissionFailureAndKeepsWaitingSession(t *testing.T) {
r := newMemoryRepo()
r.admins["t/a"] = true
c := NewController(r)
if _, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting faq", ConversationType: "direct"}); err != nil {
t.Fatal(err)
}
r.saveErr = errors.New("schema_http_403")
out, err := c.Handle(context.Background(), SettingInput{
TenantKey: "t", StaffID: "a", UnionID: "union", ConversationType: "direct",
Text: "https://alidocs.dingtalk.com/i/nodes/base?sheetId=sheet&viewId=view",
})
if err != nil {
t.Fatalf("validation failure must become an actionable reply: %v", err)
}
for _, want := range []string{"table_permission_denied", "未激活", "机器人应用", "管理员", "重新发送链接卡片", "取消"} {
if !strings.Contains(out, want) {
t.Fatalf("reply=%q must contain %q", out, want)
}
}
if _, ok := r.sessions["t/a"]; !ok {
t.Fatal("failed validation must keep the setting session for retry")
}
if _, ok := r.bindings["faq_table"]; ok {
t.Fatal("failed validation must not save a binding")
}
}
func TestTableBindingFailureReplyUsesStableCodes(t *testing.T) {
tests := []struct {
err error
code string
}{
{errors.New("schema_http_403"), "table_permission_denied"},
{errors.New("schema_token_failed"), "table_permission_denied"},
{errors.New("missing_field"), "table_schema_missing_field"},
{errors.New("duplicate_field"), "table_schema_duplicate_field"},
{errors.New("field_type_mismatch"), "table_schema_field_type_mismatch"},
{errors.New("schema_network_failed"), "table_schema_network_failed"},
{errors.New("something private happened"), "table_validation_failed"},
}
for _, tt := range tests {
out := tableBindingFailureReply("faq", tt.err)
if !strings.Contains(out, tt.code) {
t.Fatalf("error=%q reply=%q want code=%q", tt.err, out, tt.code)
}
if tt.code == "table_validation_failed" && strings.Contains(out, tt.err.Error()) {
t.Fatalf("reply leaked raw unknown error: %q", out)
}
}
}
func TestControllerDetectsBootstrapCandidate(t *testing.T) {
r := newMemoryRepo()
c := NewController(r)