Adds push, list, get, delete, and login subcommands backed by an HTTP API client, plus an MCP server for tool-based access to the document queue. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
240 lines
6.5 KiB
Go
240 lines
6.5 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func testServer(t *testing.T) (*httptest.Server, *Client) {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("POST /v1/auth/login", func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
_ = json.NewDecoder(r.Body).Decode(&req)
|
|
if req.Username == "admin" && req.Password == "pass" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"token": "tok123"})
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
})
|
|
|
|
mux.HandleFunc("GET /v1/documents", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"documents": []Document{
|
|
{ID: 1, Slug: "test-doc", Title: "Test", Body: "# Test\nHello", PushedBy: "admin", PushedAt: "2026-01-01T00:00:00Z", Read: false},
|
|
},
|
|
})
|
|
})
|
|
|
|
mux.HandleFunc("GET /v1/documents/{slug}", func(w http.ResponseWriter, r *http.Request) {
|
|
slug := r.PathValue("slug")
|
|
if slug == "missing" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: "Test", Body: "# Test\nHello"})
|
|
})
|
|
|
|
mux.HandleFunc("PUT /v1/documents/{slug}", func(w http.ResponseWriter, r *http.Request) {
|
|
slug := r.PathValue("slug")
|
|
var req struct {
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
}
|
|
_ = json.NewDecoder(r.Body).Decode(&req)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: req.Title, Body: req.Body, PushedBy: "admin"})
|
|
})
|
|
|
|
mux.HandleFunc("DELETE /v1/documents/{slug}", func(w http.ResponseWriter, r *http.Request) {
|
|
slug := r.PathValue("slug")
|
|
if slug == "missing" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})
|
|
|
|
mux.HandleFunc("POST /v1/documents/{slug}/read", func(w http.ResponseWriter, r *http.Request) {
|
|
slug := r.PathValue("slug")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: "Test", Read: true})
|
|
})
|
|
|
|
mux.HandleFunc("POST /v1/documents/{slug}/unread", func(w http.ResponseWriter, r *http.Request) {
|
|
slug := r.PathValue("slug")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(Document{ID: 1, Slug: slug, Title: "Test", Read: false})
|
|
})
|
|
|
|
ts := httptest.NewServer(mux)
|
|
t.Cleanup(ts.Close)
|
|
c := New(ts.URL, "tok123", WithHTTPClient(ts.Client()))
|
|
return ts, c
|
|
}
|
|
|
|
func TestLogin(t *testing.T) {
|
|
_, c := testServer(t)
|
|
c.token = "" // login doesn't need a pre-existing token
|
|
|
|
token, err := c.Login(context.Background(), "admin", "pass", "")
|
|
if err != nil {
|
|
t.Fatalf("Login: %v", err)
|
|
}
|
|
if token != "tok123" {
|
|
t.Fatalf("got token %q, want %q", token, "tok123")
|
|
}
|
|
}
|
|
|
|
func TestLoginBadCredentials(t *testing.T) {
|
|
_, c := testServer(t)
|
|
c.token = ""
|
|
|
|
_, err := c.Login(context.Background(), "admin", "wrong", "")
|
|
if err != ErrUnauthorized {
|
|
t.Fatalf("got %v, want ErrUnauthorized", err)
|
|
}
|
|
}
|
|
|
|
func TestListDocuments(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
docs, err := c.ListDocuments(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("ListDocuments: %v", err)
|
|
}
|
|
if len(docs) != 1 {
|
|
t.Fatalf("got %d docs, want 1", len(docs))
|
|
}
|
|
if docs[0].Slug != "test-doc" {
|
|
t.Fatalf("got slug %q, want %q", docs[0].Slug, "test-doc")
|
|
}
|
|
}
|
|
|
|
func TestGetDocument(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
doc, err := c.GetDocument(context.Background(), "test-doc")
|
|
if err != nil {
|
|
t.Fatalf("GetDocument: %v", err)
|
|
}
|
|
if doc.Slug != "test-doc" {
|
|
t.Fatalf("got slug %q, want %q", doc.Slug, "test-doc")
|
|
}
|
|
}
|
|
|
|
func TestGetDocumentNotFound(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
_, err := c.GetDocument(context.Background(), "missing")
|
|
if err != ErrNotFound {
|
|
t.Fatalf("got %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestPutDocument(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
doc, err := c.PutDocument(context.Background(), "new-doc", "New Doc", "# New\nContent")
|
|
if err != nil {
|
|
t.Fatalf("PutDocument: %v", err)
|
|
}
|
|
if doc.Slug != "new-doc" {
|
|
t.Fatalf("got slug %q, want %q", doc.Slug, "new-doc")
|
|
}
|
|
if doc.Title != "New Doc" {
|
|
t.Fatalf("got title %q, want %q", doc.Title, "New Doc")
|
|
}
|
|
}
|
|
|
|
func TestDeleteDocument(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
if err := c.DeleteDocument(context.Background(), "test-doc"); err != nil {
|
|
t.Fatalf("DeleteDocument: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteDocumentNotFound(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
err := c.DeleteDocument(context.Background(), "missing")
|
|
if err != ErrNotFound {
|
|
t.Fatalf("got %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestMarkRead(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
doc, err := c.MarkRead(context.Background(), "test-doc")
|
|
if err != nil {
|
|
t.Fatalf("MarkRead: %v", err)
|
|
}
|
|
if !doc.Read {
|
|
t.Fatal("expected doc to be marked read")
|
|
}
|
|
}
|
|
|
|
func TestMarkUnread(t *testing.T) {
|
|
_, c := testServer(t)
|
|
|
|
doc, err := c.MarkUnread(context.Background(), "test-doc")
|
|
if err != nil {
|
|
t.Fatalf("MarkUnread: %v", err)
|
|
}
|
|
if doc.Read {
|
|
t.Fatal("expected doc to be marked unread")
|
|
}
|
|
}
|
|
|
|
func TestExtractTitle(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
markdown string
|
|
fallback string
|
|
want string
|
|
}{
|
|
{"h1 found", "# My Title\nSome content", "default", "My Title"},
|
|
{"no h1", "Some content without heading", "default", "default"},
|
|
{"h2 not matched", "## Subtitle\nContent", "default", "default"},
|
|
{"h1 with spaces", "# Spaced Title \nContent", "default", "Spaced Title"},
|
|
{"multiple h1s", "# First\n# Second", "default", "First"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ExtractTitle(tt.markdown, tt.fallback)
|
|
if got != tt.want {
|
|
t.Errorf("ExtractTitle() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBearerTokenSent(t *testing.T) {
|
|
var gotAuth string
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotAuth = r.Header.Get("Authorization")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"documents": []Document{}})
|
|
}))
|
|
t.Cleanup(ts.Close)
|
|
|
|
c := New(ts.URL, "my-secret-token", WithHTTPClient(ts.Client()))
|
|
_, _ = c.ListDocuments(context.Background())
|
|
|
|
if gotAuth != "Bearer my-secret-token" {
|
|
t.Fatalf("got Authorization %q, want %q", gotAuth, "Bearer my-secret-token")
|
|
}
|
|
}
|