192 lines
5.1 KiB
Go
192 lines
5.1 KiB
Go
package httpapi_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
httpapi "watch-party-backend/internal/http"
|
|
"watch-party-backend/internal/repo"
|
|
"watch-party-backend/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ---- fake service implementing service.EpisodeService ----
|
|
|
|
type fakeSvc struct {
|
|
cur repo.Episode
|
|
getErr error
|
|
setCur repo.Episode
|
|
setErr error
|
|
moveRes repo.MoveResult
|
|
moveErr error
|
|
lastSetID int64
|
|
lastTime string
|
|
lastMove []int64
|
|
}
|
|
|
|
func (f *fakeSvc) GetCurrent(ctx context.Context) (repo.Episode, error) {
|
|
return f.cur, f.getErr
|
|
}
|
|
func (f *fakeSvc) SetCurrent(ctx context.Context, id int64, start string) (repo.Episode, error) {
|
|
f.lastSetID, f.lastTime = id, start
|
|
return f.setCur, f.setErr
|
|
}
|
|
func (f *fakeSvc) MoveToArchive(ctx context.Context, ids []int64) (repo.MoveResult, error) {
|
|
f.lastMove = append([]int64(nil), ids...)
|
|
return f.moveRes, f.moveErr
|
|
}
|
|
|
|
// ---- helpers ----
|
|
|
|
func newRouterWithSvc(svc service.EpisodeService) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
return httpapi.NewRouter(svc)
|
|
}
|
|
|
|
func doJSON(t *testing.T, r *gin.Engine, method, path string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
if body != nil {
|
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
|
t.Fatalf("encode body: %v", err)
|
|
}
|
|
}
|
|
req := httptest.NewRequest(method, path, &buf)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
return w
|
|
}
|
|
|
|
// ---- tests ----
|
|
|
|
func TestHealthz_OK(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestGetCurrent_V1_OK(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
svc := &fakeSvc{
|
|
cur: repo.Episode{
|
|
EpNum: 7,
|
|
EpTitle: "Lucky",
|
|
SeasonName: "S2",
|
|
StartTime: "18:30:00",
|
|
PlaybackLength: "00:24:00",
|
|
DateCreated: now,
|
|
},
|
|
}
|
|
r := newRouterWithSvc(svc)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/current", nil)
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("got %d", w.Code)
|
|
}
|
|
var got repo.Episode
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if got.EpNum != 7 || got.EpTitle != "Lucky" {
|
|
t.Fatalf("unexpected %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestPostCurrent_BadPayload(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
// missing fields
|
|
w := doJSON(t, r, http.MethodPost, "/v1/current", map[string]any{"id": 1})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestPostCurrent_BadTime(t *testing.T) {
|
|
svc := &fakeSvc{setErr: service.ErrInvalidTime}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/v1/current", map[string]any{
|
|
"id": 42, "start_time": "7:0:0",
|
|
})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPostCurrent_OK(t *testing.T) {
|
|
want := repo.Episode{EpNum: 42, EpTitle: "Answer", StartTime: "21:07:05"}
|
|
svc := &fakeSvc{setCur: want}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/v1/current", map[string]any{
|
|
"id": 42, "start_time": "21:07:05",
|
|
})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
var got repo.Episode
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if got.EpNum != want.EpNum || got.StartTime != want.StartTime {
|
|
t.Fatalf("unexpected %+v", got)
|
|
}
|
|
if svc.lastSetID != 42 || svc.lastTime != "21:07:05" {
|
|
t.Fatalf("service not called as expected: id=%d time=%s", svc.lastSetID, svc.lastTime)
|
|
}
|
|
}
|
|
|
|
func TestPostArchive_Empty(t *testing.T) {
|
|
svc := &fakeSvc{moveErr: service.ErrEmptyIDs}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/v1/archive", map[string]any{})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPostArchive_SingleAndMultiple_OK(t *testing.T) {
|
|
svc := &fakeSvc{
|
|
moveRes: repo.MoveResult{
|
|
MovedIDs: []int64{1, 2},
|
|
DeletedIDs: []int64{1, 2},
|
|
SkippedIDs: []int64{999},
|
|
},
|
|
}
|
|
r := newRouterWithSvc(svc)
|
|
|
|
// single
|
|
w1 := doJSON(t, r, http.MethodPost, "/v1/archive", map[string]any{"id": 1})
|
|
if w1.Code != http.StatusOK {
|
|
t.Fatalf("got %d: %s", w1.Code, w1.Body.String())
|
|
}
|
|
// multiple
|
|
w2 := doJSON(t, r, http.MethodPost, "/v1/archive", map[string]any{"ids": []int64{1, 2, 1}})
|
|
if w2.Code != http.StatusOK {
|
|
t.Fatalf("got %d: %s", w2.Code, w2.Body.String())
|
|
}
|
|
// check the lastMove captured by fake
|
|
if len(svc.lastMove) != 3 || svc.lastMove[0] != 1 || svc.lastMove[1] != 2 {
|
|
t.Fatalf("unexpected service ids: %+v", svc.lastMove)
|
|
}
|
|
// basic JSON fields present
|
|
var body map[string]any
|
|
if err := json.Unmarshal(w2.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if body["inserted"] == nil || body["deleted"] == nil {
|
|
t.Fatalf("missing counters in response: %+v", body)
|
|
}
|
|
}
|