Files
skillshare/internal/server/handler_log_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

85 lines
2.0 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHandleListLog_Empty(t *testing.T) {
s, _ := newTestServer(t)
// Clear any pre-existing logs first
clearReq := httptest.NewRequest(http.MethodDelete, "/api/log", nil)
clearRR := httptest.NewRecorder()
s.handler.ServeHTTP(clearRR, clearReq)
req := httptest.NewRequest(http.MethodGet, "/api/log", 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 struct {
Entries []any `json:"entries"`
Total int `json:"total"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.Entries) != 0 {
t.Errorf("expected 0 entries, got %d", len(resp.Entries))
}
if resp.Total != 0 {
t.Errorf("expected total 0, got %d", resp.Total)
}
}
func TestHandleListLog_WithEntries(t *testing.T) {
s, _ := newTestServer(t)
// Generate a log entry by adding a target
body := `{"name":"test","path":"/tmp/test"}`
addReq := httptest.NewRequest(http.MethodPost, "/api/targets", strings.NewReader(body))
addRR := httptest.NewRecorder()
s.handler.ServeHTTP(addRR, addReq)
// Now list logs
req := httptest.NewRequest(http.MethodGet, "/api/log", 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 struct {
Entries []any `json:"entries"`
Total int `json:"total"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if resp.Total == 0 {
t.Error("expected at least 1 log entry after target add")
}
}
func TestHandleClearLog(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodDelete, "/api/log", 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["success"] != true {
t.Error("expected success true")
}
}