|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/posit-dev/launcher-go-sdk/api" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRequestFromJSON_ConfigReload(t *testing.T) { |
| 11 | + input := `{"messageType": 202, "requestId": 42, "requestUsername": "admin", "username": "testuser"}` |
| 12 | + |
| 13 | + req, err := RequestFromJSON([]byte(input)) |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("RequestFromJSON() error = %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + cr, ok := req.(*ConfigReloadRequest) |
| 19 | + if !ok { |
| 20 | + t.Fatalf("expected *ConfigReloadRequest, got %T", req) |
| 21 | + } |
| 22 | + |
| 23 | + if cr.ID() != 42 { |
| 24 | + t.Errorf("ID() = %d, want 42", cr.ID()) |
| 25 | + } |
| 26 | + if cr.Username != "testuser" { |
| 27 | + t.Errorf("Username = %q, want %q", cr.Username, "testuser") |
| 28 | + } |
| 29 | + if cr.ReqUsername != "admin" { |
| 30 | + t.Errorf("ReqUsername = %q, want %q", cr.ReqUsername, "admin") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestNewConfigReloadResponse_Success(t *testing.T) { |
| 35 | + resp := NewConfigReloadResponse(42, 7, api.ReloadErrorNone, "") |
| 36 | + |
| 37 | + data, err := json.Marshal(resp) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("json.Marshal() error = %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + var got map[string]interface{} |
| 43 | + if err := json.Unmarshal(data, &got); err != nil { |
| 44 | + t.Fatalf("json.Unmarshal() error = %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + if mt := int(got["messageType"].(float64)); mt != 202 { |
| 48 | + t.Errorf("messageType = %d, want 202", mt) |
| 49 | + } |
| 50 | + if rid := uint64(got["requestId"].(float64)); rid != 42 { |
| 51 | + t.Errorf("requestId = %d, want 42", rid) |
| 52 | + } |
| 53 | + if resID := uint64(got["responseId"].(float64)); resID != 7 { |
| 54 | + t.Errorf("responseId = %d, want 7", resID) |
| 55 | + } |
| 56 | + if et := int(got["errorType"].(float64)); et != 0 { |
| 57 | + t.Errorf("errorType = %d, want 0", et) |
| 58 | + } |
| 59 | + if em := got["errorMessage"].(string); em != "" { |
| 60 | + t.Errorf("errorMessage = %q, want empty", em) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestNewConfigReloadResponse_ErrorTypes(t *testing.T) { |
| 65 | + tests := []struct { |
| 66 | + name string |
| 67 | + errorType api.ConfigReloadErrorType |
| 68 | + wantCode int |
| 69 | + }{ |
| 70 | + {"Unknown", api.ReloadErrorUnknown, -1}, |
| 71 | + {"Load", api.ReloadErrorLoad, 1}, |
| 72 | + {"Validate", api.ReloadErrorValidate, 2}, |
| 73 | + {"Save", api.ReloadErrorSave, 3}, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + resp := NewConfigReloadResponse(10, 3, tt.errorType, "error msg") |
| 79 | + |
| 80 | + data, err := json.Marshal(resp) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("json.Marshal() error = %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + var got map[string]interface{} |
| 86 | + if err := json.Unmarshal(data, &got); err != nil { |
| 87 | + t.Fatalf("json.Unmarshal() error = %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + if et := int(got["errorType"].(float64)); et != tt.wantCode { |
| 91 | + t.Errorf("errorType = %d, want %d", et, tt.wantCode) |
| 92 | + } |
| 93 | + if em := got["errorMessage"].(string); em != "error msg" { |
| 94 | + t.Errorf("errorMessage = %q, want %q", em, "error msg") |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments