|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hdresearch/vers-cli/internal/handlers" |
| 12 | +) |
| 13 | + |
| 14 | +func TestHandleCommitCreate_WithName(t *testing.T) { |
| 15 | + var commitBody map[string]interface{} |
| 16 | + |
| 17 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + w.Header().Set("Content-Type", "application/json") |
| 19 | + |
| 20 | + switch { |
| 21 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v1/vm/vm-123/status": |
| 22 | + w.WriteHeader(http.StatusOK) |
| 23 | + w.Write([]byte(`{"vm_id":"vm-123","owner_id":"owner-1","created_at":"2026-01-01T00:00:00Z","state":"running"}`)) |
| 24 | + |
| 25 | + case r.Method == http.MethodPost && r.URL.Path == "/api/v1/vm/vm-123/commit": |
| 26 | + body, _ := io.ReadAll(r.Body) |
| 27 | + json.Unmarshal(body, &commitBody) |
| 28 | + w.WriteHeader(http.StatusCreated) |
| 29 | + w.Write([]byte(`{"commit_id":"commit-abc"}`)) |
| 30 | + |
| 31 | + default: |
| 32 | + t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) |
| 33 | + w.WriteHeader(http.StatusNotFound) |
| 34 | + } |
| 35 | + })) |
| 36 | + defer server.Close() |
| 37 | + |
| 38 | + a := testApp(server.URL) |
| 39 | + res, err := handlers.HandleCommitCreate(context.Background(), a, handlers.CommitCreateReq{ |
| 40 | + Target: "vm-123", |
| 41 | + Name: "my-commit", |
| 42 | + Description: "my description", |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("unexpected error: %v", err) |
| 46 | + } |
| 47 | + if res.CommitID != "commit-abc" { |
| 48 | + t.Errorf("expected commit ID commit-abc, got %s", res.CommitID) |
| 49 | + } |
| 50 | + if res.VmID != "vm-123" { |
| 51 | + t.Errorf("expected VM ID vm-123, got %s", res.VmID) |
| 52 | + } |
| 53 | + if res.Name != "my-commit" { |
| 54 | + t.Errorf("expected name my-commit, got %s", res.Name) |
| 55 | + } |
| 56 | + if res.Description != "my description" { |
| 57 | + t.Errorf("expected description 'my description', got %s", res.Description) |
| 58 | + } |
| 59 | + |
| 60 | + // Verify name and description were sent in the commit request body |
| 61 | + if commitBody == nil { |
| 62 | + t.Fatal("expected commit request to have a body") |
| 63 | + } |
| 64 | + if commitBody["name"] != "my-commit" { |
| 65 | + t.Errorf("expected body name=my-commit, got %v", commitBody["name"]) |
| 66 | + } |
| 67 | + if commitBody["description"] != "my description" { |
| 68 | + t.Errorf("expected body description='my description', got %v", commitBody["description"]) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestHandleCommitCreate_WithoutName(t *testing.T) { |
| 73 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 | + w.Header().Set("Content-Type", "application/json") |
| 75 | + |
| 76 | + switch { |
| 77 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v1/vm/vm-123/status": |
| 78 | + w.WriteHeader(http.StatusOK) |
| 79 | + w.Write([]byte(`{"vm_id":"vm-123","owner_id":"owner-1","created_at":"2026-01-01T00:00:00Z","state":"running"}`)) |
| 80 | + |
| 81 | + case r.Method == http.MethodPost && r.URL.Path == "/api/v1/vm/vm-123/commit": |
| 82 | + w.WriteHeader(http.StatusCreated) |
| 83 | + w.Write([]byte(`{"commit_id":"commit-abc"}`)) |
| 84 | + |
| 85 | + default: |
| 86 | + t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) |
| 87 | + w.WriteHeader(http.StatusNotFound) |
| 88 | + } |
| 89 | + })) |
| 90 | + defer server.Close() |
| 91 | + |
| 92 | + a := testApp(server.URL) |
| 93 | + res, err := handlers.HandleCommitCreate(context.Background(), a, handlers.CommitCreateReq{ |
| 94 | + Target: "vm-123", |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("unexpected error: %v", err) |
| 98 | + } |
| 99 | + if res.CommitID != "commit-abc" { |
| 100 | + t.Errorf("expected commit ID commit-abc, got %s", res.CommitID) |
| 101 | + } |
| 102 | + if res.Name != "" { |
| 103 | + t.Errorf("expected empty name, got %s", res.Name) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestHandleCommitCreate_NameOnly(t *testing.T) { |
| 108 | + var commitBody map[string]interface{} |
| 109 | + |
| 110 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 111 | + w.Header().Set("Content-Type", "application/json") |
| 112 | + |
| 113 | + switch { |
| 114 | + case r.Method == http.MethodGet && r.URL.Path == "/api/v1/vm/vm-123/status": |
| 115 | + w.WriteHeader(http.StatusOK) |
| 116 | + w.Write([]byte(`{"vm_id":"vm-123","owner_id":"owner-1","created_at":"2026-01-01T00:00:00Z","state":"running"}`)) |
| 117 | + |
| 118 | + case r.Method == http.MethodPost && r.URL.Path == "/api/v1/vm/vm-123/commit": |
| 119 | + body, _ := io.ReadAll(r.Body) |
| 120 | + json.Unmarshal(body, &commitBody) |
| 121 | + w.WriteHeader(http.StatusCreated) |
| 122 | + w.Write([]byte(`{"commit_id":"commit-abc"}`)) |
| 123 | + |
| 124 | + default: |
| 125 | + t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) |
| 126 | + w.WriteHeader(http.StatusNotFound) |
| 127 | + } |
| 128 | + })) |
| 129 | + defer server.Close() |
| 130 | + |
| 131 | + a := testApp(server.URL) |
| 132 | + res, err := handlers.HandleCommitCreate(context.Background(), a, handlers.CommitCreateReq{ |
| 133 | + Target: "vm-123", |
| 134 | + Name: "just-a-name", |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("unexpected error: %v", err) |
| 138 | + } |
| 139 | + if res.Name != "just-a-name" { |
| 140 | + t.Errorf("expected name just-a-name, got %s", res.Name) |
| 141 | + } |
| 142 | + if res.Description != "" { |
| 143 | + t.Errorf("expected empty description, got %s", res.Description) |
| 144 | + } |
| 145 | + |
| 146 | + // Verify name was sent but description was not |
| 147 | + if commitBody["name"] != "just-a-name" { |
| 148 | + t.Errorf("expected name in body, got %v", commitBody["name"]) |
| 149 | + } |
| 150 | + if _, hasDesc := commitBody["description"]; hasDesc { |
| 151 | + t.Error("description should not be in body when not provided") |
| 152 | + } |
| 153 | +} |
0 commit comments