Files
pipu-music/services/api/internal/system/handler_test.go
T
mrleo1nidandClaude Opus 5.5 ebf296542c
ci / contract (сгенерированный код совпадает с openapi.yaml) (push) Successful in 3m55s
ci / api (push) Successful in 51s
ci / mobile (Metro-бандл Android) (push) Canceled after 0s
release / images (services/api/Dockerfile, storage-agent, storage-agent) (push) Canceled after 0s
release / images (services/recommender/Dockerfile, recommender, ) (push) Canceled after 0s
ci / recommender (push) Successful in 1m11s
ci / ts (packages + web) (push) Canceled after 3s
release / verify (push) Successful in 1m16s
release / images (apps/web/Dockerfile, web, ) (push) Failing after 2m40s
release / android + release (push) Skipped
release / images (services/api/Dockerfile, api, api) (push) Canceled after 1s
feat: M0 monorepo skeleton with services, clients, deploy and CI
- api/openapi.yaml: contract with /healthz, /readyz, /api/v1/server-info
- services/api (Go): api with oapi-codegen strict server, goose migrations,
  readiness checks for Postgres/Redis; storage-agent stub with internal token
- services/recommender (Python, uv): FastAPI health/readiness/internal status,
  `pipu-rec serve` and `pipu-rec analyze` entrypoints
- packages: generated api-client, core (server URL + server check),
  i18n (ru/en + dictionary check), design-tokens (CSS vars + TS from tokens.json)
- apps/web: Vite + React shell showing server status, language switch
- apps/mobile: Expo (SDK 53) Android app with server connect screen
- deploy: Dockerfiles, compose (prod + dev), nginx template, install/update scripts
- .gitea/workflows: ci.yml and tag-based release.yml (images, signed APK, release)
- scripts/version.mjs: version and Android versionCode from git tag
- tests/fixtures/music/generate.sh: synthetic test library via ffmpeg
- docs: versionCode formula, OpenAPI 3.0.3, dev instructions in README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 23:55:11 +03:00

82 lines
2.5 KiB
Go

package system_test
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"pipu/api/internal/server"
"pipu/api/internal/system"
)
func newServer(checks map[string]system.Checker) http.Handler {
h := system.NewHandler(system.Info{
Version: "1.2.3",
SupportedLocales: []string{"ru", "en"},
MinAndroidVersionCode: 10203,
}, checks)
return server.NewRouter(server.Deps{System: h})
}
func get(t *testing.T, h http.Handler, path string) (*httptest.ResponseRecorder, map[string]any) {
t.Helper()
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
var body map[string]any
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body), rec.Body.String())
return rec, body
}
func TestServerInfo(t *testing.T) {
rec, body := get(t, newServer(nil), "/api/v1/server-info")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "pipu-music", body["name"])
assert.Equal(t, "1.2.3", body["version"])
assert.Equal(t, "v1", body["api_version"])
assert.Equal(t, []any{"ru", "en"}, body["supported_locales"])
assert.EqualValues(t, 10203, body["min_android_version_code"])
}
func TestHealthz(t *testing.T) {
rec, body := get(t, newServer(nil), "/healthz")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "ok", body["status"])
}
func TestReadyz(t *testing.T) {
ok := system.CheckerFunc(func(context.Context) error { return nil })
down := system.CheckerFunc(func(context.Context) error { return errors.New("connection refused") })
t.Run("all dependencies up", func(t *testing.T) {
rec, body := get(t, newServer(map[string]system.Checker{"postgres": ok, "redis": ok}), "/readyz")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "ok", body["status"])
assert.Equal(t, map[string]any{"postgres": "ok", "redis": "ok"}, body["checks"])
})
t.Run("one dependency down", func(t *testing.T) {
rec, body := get(t, newServer(map[string]system.Checker{"postgres": ok, "redis": down}), "/readyz")
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
assert.Equal(t, "degraded", body["status"])
assert.Equal(t, map[string]any{"postgres": "ok", "redis": "connection refused"}, body["checks"])
})
}
func TestUnknownRouteIsProblemJSON(t *testing.T) {
rec, body := get(t, newServer(nil), "/api/v1/nope")
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(t, "application/problem+json", rec.Header().Get("Content-Type"))
assert.Equal(t, "common.not_found", body["code"])
}