|
| 1 | +package poolhttp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/jackc/pgx/v5" |
| 12 | + "go.uber.org/zap" |
| 13 | +) |
| 14 | + |
| 15 | +// mockRow implements pgx.Row for testing. |
| 16 | +type mockRow struct { |
| 17 | + nodeID string |
| 18 | + tier string |
| 19 | + status string |
| 20 | + err error |
| 21 | +} |
| 22 | + |
| 23 | +func (m *mockRow) Scan(dest ...any) error { |
| 24 | + if m.err != nil { |
| 25 | + return m.err |
| 26 | + } |
| 27 | + *dest[0].(*string) = m.nodeID |
| 28 | + *dest[1].(*string) = m.tier |
| 29 | + *dest[2].(*string) = m.status |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +// mockDB implements dbQuerier for testing. |
| 34 | +type mockDB struct { |
| 35 | + nodeID string |
| 36 | + tier string |
| 37 | + status string |
| 38 | + err error |
| 39 | +} |
| 40 | + |
| 41 | +func (m *mockDB) QueryRow(_ context.Context, _ string, _ ...any) pgx.Row { |
| 42 | + return &mockRow{ |
| 43 | + nodeID: m.nodeID, |
| 44 | + tier: m.tier, |
| 45 | + status: m.status, |
| 46 | + err: m.err, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func serveWithMux(handler http.HandlerFunc, req *http.Request) *httptest.ResponseRecorder { |
| 51 | + rr := httptest.NewRecorder() |
| 52 | + mux := http.NewServeMux() |
| 53 | + mux.HandleFunc("GET /internal/pool/nodes/{pubkey}/status", handler) |
| 54 | + mux.ServeHTTP(rr, req) |
| 55 | + return rr |
| 56 | +} |
| 57 | + |
| 58 | +func TestHandleGetNodeStatus_Active(t *testing.T) { |
| 59 | + db := &mockDB{nodeID: "uuid-1", tier: "t1", status: "active"} |
| 60 | + handler := handleGetNodeStatus(db, zap.NewNop()) |
| 61 | + |
| 62 | + req := httptest.NewRequest(http.MethodGet, "/internal/pool/nodes/somepubkey/status", nil) |
| 63 | + rr := serveWithMux(handler, req) |
| 64 | + |
| 65 | + if rr.Code != http.StatusOK { |
| 66 | + t.Fatalf("expected 200, got %d", rr.Code) |
| 67 | + } |
| 68 | + body := rr.Body.String() |
| 69 | + if !strings.Contains(body, `"status":"active"`) { |
| 70 | + t.Errorf("body missing status:active, got: %s", body) |
| 71 | + } |
| 72 | + if !strings.Contains(body, `"coordinator_verified":true`) { |
| 73 | + t.Errorf("body missing coordinator_verified:true, got: %s", body) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestHandleGetNodeStatus_Pending(t *testing.T) { |
| 78 | + db := &mockDB{nodeID: "uuid-2", tier: "t1", status: "pending"} |
| 79 | + handler := handleGetNodeStatus(db, zap.NewNop()) |
| 80 | + |
| 81 | + req := httptest.NewRequest(http.MethodGet, "/internal/pool/nodes/somepubkey/status", nil) |
| 82 | + rr := serveWithMux(handler, req) |
| 83 | + |
| 84 | + if rr.Code != http.StatusOK { |
| 85 | + t.Fatalf("expected 200, got %d", rr.Code) |
| 86 | + } |
| 87 | + if !strings.Contains(rr.Body.String(), `"status":"pending"`) { |
| 88 | + t.Errorf("body missing status:pending, got: %s", rr.Body.String()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestHandleGetNodeStatus_NotFound(t *testing.T) { |
| 93 | + db := &mockDB{err: pgx.ErrNoRows} |
| 94 | + handler := handleGetNodeStatus(db, zap.NewNop()) |
| 95 | + |
| 96 | + req := httptest.NewRequest(http.MethodGet, "/internal/pool/nodes/somepubkey/status", nil) |
| 97 | + rr := serveWithMux(handler, req) |
| 98 | + |
| 99 | + if rr.Code != http.StatusNotFound { |
| 100 | + t.Fatalf("expected 404, got %d", rr.Code) |
| 101 | + } |
| 102 | + if !strings.Contains(rr.Body.String(), `"error":"node not found"`) { |
| 103 | + t.Errorf("body missing error message, got: %s", rr.Body.String()) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestHandleGetNodeStatus_DBError(t *testing.T) { |
| 108 | + db := &mockDB{err: errors.New("connection reset by peer")} |
| 109 | + handler := handleGetNodeStatus(db, zap.NewNop()) |
| 110 | + |
| 111 | + req := httptest.NewRequest(http.MethodGet, "/internal/pool/nodes/somepubkey/status", nil) |
| 112 | + rr := serveWithMux(handler, req) |
| 113 | + |
| 114 | + if rr.Code != http.StatusInternalServerError { |
| 115 | + t.Fatalf("expected 500, got %d", rr.Code) |
| 116 | + } |
| 117 | + if !strings.Contains(rr.Body.String(), `"error":"internal error"`) { |
| 118 | + t.Errorf("body missing internal error message, got: %s", rr.Body.String()) |
| 119 | + } |
| 120 | +} |
0 commit comments