Files
skillshare/internal/server/handler_overview_test.go
T
Willie a8fb965b8b test: add unit tests for sync, install, and server packages
Boost unit test coverage across three core packages:
- internal/sync: 11.6% → 54.6% (discovery, status, checksum, manifest, pull, merge/prune)
- internal/install: 40.7% → 60.0% (meta, ignore, copy, updatable, local install, options, gitignore, source)
- internal/server: 15.1% → 40.0% (health, overview, skills, targets, config, log, trash, sync, collect, check, backup, diff, audit, hub)
2026-02-23 04:18:01 +08:00

77 lines
2.0 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandleOverview_Empty(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/overview", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp map[string]any
json.Unmarshal(rr.Body.Bytes(), &resp)
if resp["skillCount"].(float64) != 0 {
t.Errorf("expected 0 skills, got %v", resp["skillCount"])
}
if resp["targetCount"].(float64) != 0 {
t.Errorf("expected 0 targets, got %v", resp["targetCount"])
}
if resp["isProjectMode"].(bool) {
t.Error("expected isProjectMode false")
}
}
func TestHandleOverview_WithSkills(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "alpha")
addSkill(t, src, "beta")
req := httptest.NewRequest(http.MethodGet, "/api/overview", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp map[string]any
json.Unmarshal(rr.Body.Bytes(), &resp)
if resp["skillCount"].(float64) != 2 {
t.Errorf("expected 2 skills, got %v", resp["skillCount"])
}
}
func TestHandleOverview_ProjectMode(t *testing.T) {
tmp := t.TempDir()
s, _ := newTestServer(t)
s.projectRoot = tmp // simulate project mode
req := httptest.NewRequest(http.MethodGet, "/api/overview", nil)
rr := httptest.NewRecorder()
// Use mux directly to bypass config auto-reload middleware
// (project config file doesn't exist in test)
s.mux.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp map[string]any
json.Unmarshal(rr.Body.Bytes(), &resp)
if !resp["isProjectMode"].(bool) {
t.Error("expected isProjectMode true")
}
if resp["projectRoot"] != tmp {
t.Errorf("expected projectRoot %q, got %v", tmp, resp["projectRoot"])
}
}