package handlers import ( "net/http" "net/http/httptest" "os" "strings" "testing" "time" "go-crm/internal/db" "go-crm/internal/whatsapp" "golang.org/x/crypto/bcrypt" ) func TestListClientsWhatsAppStatusIndicators(t *testing.T) { tmpfile, err := os.CreateTemp("", "test_*.db") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) tmpfile.Close() testDB, err := db.Init(tmpfile.Name()) if err != nil { t.Fatalf("failed to init db: %v", err) } defer testDB.Close() fakeWA := whatsapp.NewFakeConnector() fakeWA.MarkConnected(1) DB = testDB WAConnector = fakeWA hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost) _, err = testDB.Exec( "INSERT INTO accounts (email, name, password, created_at) VALUES (?, ?, ?, ?)", "test@example.com", "Test Account", string(hashedPassword), time.Now().Unix(), ) if err != nil { t.Fatalf("failed to create account: %v", err) } var accountID int64 err = testDB.QueryRow("SELECT account_id FROM accounts WHERE email = ?", "test@example.com").Scan(&accountID) if err != nil { t.Fatalf("failed to get account id: %v", err) } _, err = testDB.Exec( "INSERT INTO clients (account_id, name, phone, whatsapp_number, whatsapp_connected, created_at) VALUES (?, ?, ?, ?, ?, ?)", accountID, "Connected Client", "+5521987654321", "+5521987654321", 1, time.Now().Unix(), ) if err != nil { t.Fatalf("failed to create connected client: %v", err) } _, err = testDB.Exec( "INSERT INTO clients (account_id, name, phone, whatsapp_number, whatsapp_connected, created_at) VALUES (?, ?, ?, ?, ?, ?)", accountID, "Disconnected Client", "+5521987654322", "+5521987654322", 1, time.Now().Unix(), ) if err != nil { t.Fatalf("failed to create disconnected client: %v", err) } _, err = testDB.Exec( "INSERT INTO clients (account_id, name, phone, created_at) VALUES (?, ?, ?, ?)", accountID, "No WhatsApp Client", "+5521987654323", time.Now().Unix(), ) if err != nil { t.Fatalf("failed to create no-wa client: %v", err) } sessionID := "test-session-status" expires := time.Now().Add(time.Hour).Unix() _, err = testDB.Exec( "INSERT INTO sessions (session_id, account_id, expires) VALUES (?, ?, ?)", sessionID, accountID, expires, ) if err != nil { t.Fatalf("failed to create session: %v", err) } req := httptest.NewRequest(http.MethodGet, "/clients", nil) req.AddCookie(&http.Cookie{Name: "session", Value: sessionID}) w := httptest.NewRecorder() ListClients(w, req) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } body := w.Body.String() if !strings.Contains(body, "Connected Client") { t.Error("expected to find connected client") } if !strings.Contains(body, "Disconnected Client") { t.Error("expected to find disconnected client") } if !strings.Contains(body, "No WhatsApp Client") { t.Error("expected to find no-wa client") } if !strings.Contains(body, "Connect") { t.Error("expected Connect link for clients without active connection") } greenDot := `color:#28a745` if !strings.Contains(body, greenDot) { t.Error("expected green dot for connected client") } }