|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// F14:SetFieldHint/SetFieldWidget 向 InputSchema 注入 x-ui 呈现 hints。 |
| 10 | +func hintsOf(t *testing.T, schema string) map[string]interface{} { |
| 11 | + t.Helper() |
| 12 | + var out map[string]interface{} |
| 13 | + if err := json.Unmarshal([]byte(schema), &out); err != nil { |
| 14 | + t.Fatalf("schema not valid JSON: %v", err) |
| 15 | + } |
| 16 | + return out |
| 17 | +} |
| 18 | + |
| 19 | +func TestMetadataBuilder_SetFieldHint(t *testing.T) { |
| 20 | + t.Run("merge into existing schema", func(t *testing.T) { |
| 21 | + metadata, err := NewMetadataBuilder(). |
| 22 | + SetID("player.ban"). |
| 23 | + SetInputSchema(`{"type":"object","properties":{"id":{"type":"string","title":"玩家 ID"}}}`). |
| 24 | + SetFieldHint("id", "x-widget", "Select"). |
| 25 | + SetFieldHint("id", "x-options-source", map[string]interface{}{ |
| 26 | + "functionId": "player.list", |
| 27 | + "labelPath": "/items/*/name", |
| 28 | + "valuePath": "/items/*/id", |
| 29 | + }). |
| 30 | + Build() |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("Build: %v", err) |
| 33 | + } |
| 34 | + schema := hintsOf(t, metadata.InputSchema) |
| 35 | + props := schema["properties"].(map[string]interface{}) |
| 36 | + id := props["id"].(map[string]interface{}) |
| 37 | + if id["x-widget"] != "Select" { |
| 38 | + t.Fatalf("x-widget = %v", id["x-widget"]) |
| 39 | + } |
| 40 | + source := id["x-options-source"].(map[string]interface{}) |
| 41 | + if source["functionId"] != "player.list" { |
| 42 | + t.Fatalf("x-options-source = %v", source) |
| 43 | + } |
| 44 | + // 既有 title 保留 |
| 45 | + if id["title"] != "玩家 ID" { |
| 46 | + t.Fatalf("title lost: %v", id["title"]) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("empty schema creates object skeleton", func(t *testing.T) { |
| 51 | + metadata, err := NewMetadataBuilder(). |
| 52 | + SetID("f"). |
| 53 | + SetFieldWidget("level", "Slider"). |
| 54 | + Build() |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("Build: %v", err) |
| 57 | + } |
| 58 | + schema := hintsOf(t, metadata.InputSchema) |
| 59 | + if schema["type"] != "object" { |
| 60 | + t.Fatalf("type = %v", schema["type"]) |
| 61 | + } |
| 62 | + props := schema["properties"].(map[string]interface{}) |
| 63 | + level := props["level"].(map[string]interface{}) |
| 64 | + if level["x-widget"] != "Slider" { |
| 65 | + t.Fatalf("x-widget = %v", level["x-widget"]) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("override previous hint", func(t *testing.T) { |
| 70 | + metadata, err := NewMetadataBuilder(). |
| 71 | + SetID("f"). |
| 72 | + SetFieldWidget("a", "Input"). |
| 73 | + SetFieldWidget("a", "TextArea"). |
| 74 | + Build() |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("Build: %v", err) |
| 77 | + } |
| 78 | + schema := hintsOf(t, metadata.InputSchema) |
| 79 | + a := schema["properties"].(map[string]interface{})["a"].(map[string]interface{}) |
| 80 | + if a["x-widget"] != "TextArea" { |
| 81 | + t.Fatalf("x-widget = %v (expected override)", a["x-widget"]) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("x_ prefix normalized to x-", func(t *testing.T) { |
| 86 | + metadata, err := NewMetadataBuilder(). |
| 87 | + SetID("f"). |
| 88 | + SetFieldHint("a", "x_widget", "Input"). |
| 89 | + Build() |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("Build: %v", err) |
| 92 | + } |
| 93 | + schema := hintsOf(t, metadata.InputSchema) |
| 94 | + a := schema["properties"].(map[string]interface{})["a"].(map[string]interface{}) |
| 95 | + if _, ok := a["x-widget"]; !ok { |
| 96 | + t.Fatalf("expected normalized x-widget key, got %v", a) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("invalid hint rejected", func(t *testing.T) { |
| 101 | + _, err := NewMetadataBuilder(). |
| 102 | + SetID("f"). |
| 103 | + SetFieldHint("a", "widget", "Input"). |
| 104 | + Build() |
| 105 | + if err == nil || !strings.Contains(err.Error(), "x- extension key") { |
| 106 | + t.Fatalf("expected hint validation error, got %v", err) |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("empty field rejected", func(t *testing.T) { |
| 111 | + _, err := NewMetadataBuilder(). |
| 112 | + SetID("f"). |
| 113 | + SetFieldHint("", "x-widget", "Input"). |
| 114 | + Build() |
| 115 | + if err == nil || !strings.Contains(err.Error(), "field key is required") { |
| 116 | + t.Fatalf("expected field validation error, got %v", err) |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("empty widget rejected", func(t *testing.T) { |
| 121 | + _, err := NewMetadataBuilder(). |
| 122 | + SetID("f"). |
| 123 | + SetFieldWidget("a", " "). |
| 124 | + Build() |
| 125 | + if err == nil || !strings.Contains(err.Error(), "widget is required") { |
| 126 | + t.Fatalf("expected widget validation error, got %v", err) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("invalid existing schema rejected", func(t *testing.T) { |
| 131 | + _, err := NewMetadataBuilder(). |
| 132 | + SetID("f"). |
| 133 | + SetInputSchema(`not-json`). |
| 134 | + SetFieldHint("a", "x-widget", "Input"). |
| 135 | + Build() |
| 136 | + if err == nil || !strings.Contains(err.Error(), "not valid JSON") { |
| 137 | + t.Fatalf("expected schema parse error, got %v", err) |
| 138 | + } |
| 139 | + }) |
| 140 | +} |
0 commit comments