mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers, and the ptr helper went once new(expr) could say it; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
//go:generate mockery
|
|
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/insights/insights/livestream/events"
|
|
"github.com/insights/insights/livestream/handlers"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIndex(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
|
|
if assert.NoError(t, handlers.Index(c)) {
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, "RealTime Script 3000", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestStatsHandler(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodGet, "/stats", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
|
|
// Mock the authorization header
|
|
req.Header.Set("Authorization", "Bearer mock_token")
|
|
|
|
// Create a mock TeamStats
|
|
stats := events.NewStatsKeeper()
|
|
stats.GetStoreForToken("mock_token").Add("user1", events.NoSpaceType{})
|
|
|
|
// Add the teamStats to the context
|
|
c.Set("teamStats", stats)
|
|
|
|
handler := func(c echo.Context) error {
|
|
return c.JSON(http.StatusOK, map[string]any{
|
|
"users_on_product": stats.GetStoreForToken("mock_token").Len(),
|
|
})
|
|
}
|
|
|
|
if assert.NoError(t, handler(c)) {
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.JSONEq(t, `{"users_on_product":1}`, rec.Body.String())
|
|
}
|
|
}
|