package rainyun import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestServerList(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" || r.URL.Path != "/product/rcs/" { t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) } resp := map[string]any{ "code": 0, "data": map[string]any{ "Records": []map[string]any{ { "ID": 12001, "HostName": "web-server", "status": "running", "MainIPv4": "1.2.3.4", "CPU": 2, "memory": 4096, "disk": 50, "OsName": "Ubuntu 22.04", "Zone": "cn-sy1", "ExpDate": 1756656000, }, }, "TotalRecords": 1, }, } json.NewEncoder(w).Encode(resp) })) defer srv.Close() rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)} svc := rain.Server() servers, err := svc.List(context.Background()) if err != nil { t.Fatalf("List() error: %v", err) } if len(servers) != 1 { t.Fatalf("len = %d, want 1", len(servers)) } s := servers[0] if s.Name != "web-server" { t.Errorf("Name = %q", s.Name) } if s.Status != "running" { t.Errorf("Status = %q", s.Status) } if s.CPU != 2 { t.Errorf("CPU = %d", s.CPU) } } func TestServerStart(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" || r.URL.Path != "/product/rcs/12001/start" { t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) } json.NewEncoder(w).Encode(map[string]any{"code": 0}) })) defer srv.Close() rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)} err := rain.Server().Start(context.Background(), "12001") if err != nil { t.Fatalf("Start() error: %v", err) } } func TestServerVNC(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { resp := map[string]any{ "code": 0, "data": map[string]any{"vnc_url": "https://vnc.example.com/abc"}, } json.NewEncoder(w).Encode(resp) })) defer srv.Close() rain := &Rainyun{client: newClient(srv.URL, "sk-test", false)} url, err := rain.Server().GetVNC(context.Background(), "12001") if err != nil { t.Fatalf("GetVNC() error: %v", err) } if url != "https://vnc.example.com/abc" { t.Errorf("VNC url = %q", url) } }