|
| 1 | +package model_entities |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/langgenius/dify-plugin-daemon/pkg/validators" |
| 8 | +) |
| 9 | + |
| 10 | +func TestModelPollingResultValidatesStatus(t *testing.T) { |
| 11 | + nextCheckAfterSeconds := 10 |
| 12 | + result := ModelPollingResult{ |
| 13 | + Status: PollingStatus("running"), |
| 14 | + PluginState: map[string]any{"task_id": "task-1"}, |
| 15 | + NextCheckAfterSeconds: &nextCheckAfterSeconds, |
| 16 | + } |
| 17 | + |
| 18 | + if err := validators.GlobalEntitiesValidator.Struct(result); err != nil { |
| 19 | + t.Fatalf("validate polling result: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + data, err := json.Marshal(result) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("marshal polling result: %v", err) |
| 25 | + } |
| 26 | + if !json.Valid(data) { |
| 27 | + t.Fatalf("invalid json: %s", data) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestModelPollingResultRejectsUnknownStatus(t *testing.T) { |
| 32 | + result := ModelPollingResult{ |
| 33 | + Status: PollingStatus("pending"), |
| 34 | + } |
| 35 | + |
| 36 | + if err := validators.GlobalEntitiesValidator.Struct(result); err == nil { |
| 37 | + t.Fatal("expected unknown polling status validation error") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestModelPollingResultRequiresRunningState(t *testing.T) { |
| 42 | + result := ModelPollingResult{ |
| 43 | + Status: PollingStatus("running"), |
| 44 | + } |
| 45 | + |
| 46 | + if err := validators.GlobalEntitiesValidator.Struct(result); err == nil { |
| 47 | + t.Fatal("expected missing plugin_state validation error") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestModelPollingResultRequiresSucceededResult(t *testing.T) { |
| 52 | + result := ModelPollingResult{ |
| 53 | + Status: PollingStatus("succeeded"), |
| 54 | + Result: json.RawMessage("null"), |
| 55 | + } |
| 56 | + |
| 57 | + if err := validators.GlobalEntitiesValidator.Struct(result); err == nil { |
| 58 | + t.Fatal("expected missing result validation error") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestModelPollingResultRequiresFailedError(t *testing.T) { |
| 63 | + result := ModelPollingResult{ |
| 64 | + Status: PollingStatus("failed"), |
| 65 | + Error: " ", |
| 66 | + } |
| 67 | + |
| 68 | + if err := validators.GlobalEntitiesValidator.Struct(result); err == nil { |
| 69 | + t.Fatal("expected missing error validation error") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestModelPollingResultAcceptsTerminalPayloads(t *testing.T) { |
| 74 | + succeeded := ModelPollingResult{ |
| 75 | + Status: PollingStatus("succeeded"), |
| 76 | + Result: json.RawMessage(`{"text":"done"}`), |
| 77 | + } |
| 78 | + if err := validators.GlobalEntitiesValidator.Struct(succeeded); err != nil { |
| 79 | + t.Fatalf("validate succeeded result: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + failed := ModelPollingResult{ |
| 83 | + Status: PollingStatus("failed"), |
| 84 | + Error: "provider failed", |
| 85 | + } |
| 86 | + if err := validators.GlobalEntitiesValidator.Struct(failed); err != nil { |
| 87 | + t.Fatalf("validate failed result: %v", err) |
| 88 | + } |
| 89 | +} |
0 commit comments