Add activity tracker server, Hyprland agent and Gitea image publishing
The Go API on PostgreSQL ingests desktop, browser, presence, manual and Health Auto Export events and serves activities and daily goal summaries. The Hyprland agent records window focus, idle and lock intervals. The CI workflow publishes adam-french.co.uk/adamf/activity-tracker to the Gitea registry and rolls the web_server stack onto it. The image probes itself through a new `tracker healthcheck` command, because distroless has no wget. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"ccp/internal/migrations"
|
||||
"ccp/internal/store"
|
||||
)
|
||||
|
||||
// Requires a disposable database, e.g. the one from deploy/docker-compose.yml:
|
||||
//
|
||||
// TEST_DATABASE_URL=postgres://tracker:tracker@localhost:5432/tracker go test ./internal/api
|
||||
func setup(t *testing.T) (*httptest.Server, string) {
|
||||
url := os.Getenv("TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("TEST_DATABASE_URL not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if err := migrations.Up(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `TRUNCATE activities, samples, goals, devices RESTART IDENTITY`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
st := store.New(pool)
|
||||
_, key, err := st.CreateDevice(ctx, "test", "hyprland")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := st.CreateGoal(ctx, store.Goal{Kind: "app_focus", Label: "kitty", Op: ">=", TargetS: 3600}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
srv := httptest.NewServer(New(st, time.UTC, slog.New(slog.NewTextHandler(io.Discard, nil))))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv, key
|
||||
}
|
||||
|
||||
func do(t *testing.T, method, url, key, body string) (int, map[string]any) {
|
||||
t.Helper()
|
||||
req, _ := http.NewRequest(method, url, strings.NewReader(body))
|
||||
if key != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+key)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var out map[string]any
|
||||
json.NewDecoder(resp.Body).Decode(&out)
|
||||
return resp.StatusCode, out
|
||||
}
|
||||
|
||||
func TestIngestAndSummary(t *testing.T) {
|
||||
srv, key := setup(t)
|
||||
day := time.Now().UTC().Add(-48 * time.Hour).Format(time.DateOnly)
|
||||
|
||||
// The second event crosses midnight; only its first hour belongs to `day`.
|
||||
batch := `{"events":[
|
||||
{"id":"e1","kind":"app_focus","label":"kitty","started_at":"` + day + `T09:00:00Z","ended_at":"` + day + `T10:30:00Z","attrs":{"title":"nvim"}},
|
||||
{"id":"e2","kind":"app_focus","label":"firefox","started_at":"` + day + `T23:00:00Z","ended_at":"` + day + `T23:59:59Z"}
|
||||
]}`
|
||||
|
||||
if code, _ := do(t, "POST", srv.URL+"/api/v1/desktop/events", "", batch); code != http.StatusUnauthorized {
|
||||
t.Fatalf("no key: got %d", code)
|
||||
}
|
||||
if code, _ := do(t, "POST", srv.URL+"/api/v1/desktop/events", "trk_wrong", batch); code != http.StatusUnauthorized {
|
||||
t.Fatalf("bad key: got %d", code)
|
||||
}
|
||||
|
||||
code, body := do(t, "POST", srv.URL+"/api/v1/desktop/events", key, batch)
|
||||
if code != http.StatusOK || body["inserted"] != 2.0 {
|
||||
t.Fatalf("first upload: %d %v", code, body)
|
||||
}
|
||||
code, body = do(t, "POST", srv.URL+"/api/v1/desktop/events", key, batch)
|
||||
if code != http.StatusOK || body["inserted"] != 0.0 {
|
||||
t.Fatalf("re-upload should be idempotent: %d %v", code, body)
|
||||
}
|
||||
|
||||
code, body = do(t, "POST", srv.URL+"/api/v1/desktop/events", key, `{"events":[{"id":"x","kind":"walk","started_at":"`+day+`T09:00:00Z","ended_at":"`+day+`T10:00:00Z"}]}`)
|
||||
if code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("wrong kind for endpoint: %d %v", code, body)
|
||||
}
|
||||
|
||||
code, body = do(t, "GET", srv.URL+"/api/v1/summary/daily?date="+day, key, "")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("summary: %d %v", code, body)
|
||||
}
|
||||
if got := body["by_kind"].(map[string]any)["app_focus"]; got != float64(5400+3599) {
|
||||
t.Errorf("app_focus total = %v", got)
|
||||
}
|
||||
goal := body["goals"].([]any)[0].(map[string]any)
|
||||
if goal["actual_s"] != 5400.0 || goal["met"] != true {
|
||||
t.Errorf("goal = %v", goal)
|
||||
}
|
||||
|
||||
code, body = do(t, "GET", srv.URL+"/api/v1/activities?from="+day+"T00:00:00Z&to="+day+"T23:59:59Z&kind=app_focus", key, "")
|
||||
if code != http.StatusOK || len(body["activities"].([]any)) != 2 {
|
||||
t.Fatalf("activities: %d %v", code, body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user