Go-Programm lc-agent: sendet neu kopierten Text als Clip ins Projekt "Zwischenablage" und übernimmt eigene Clips anderer Geräte live in die Zwischenablage. Von Passwort-Managern markierte Inhalte werden nie gesendet; Größengrenze, Ausschlussmuster, nur die letzten 50 eigenen Clips. Wiederholt beim Start, bis der Server erreichbar ist; beendet sich bei widerrufenem Token mit Exit-Code 3. Build für alle Plattformen per build.sh im Container, Autostart-Vorlagen für alle drei Systeme. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
func newServer(t *testing.T, handler http.HandlerFunc) *Client {
|
|
t.Helper()
|
|
srv := httptest.NewServer(handler)
|
|
t.Cleanup(srv.Close)
|
|
client, err := New(srv.URL+"/", "lc_token", "test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return client
|
|
}
|
|
|
|
func TestRequestsCarryTokenAndJSON(t *testing.T) {
|
|
var seen *http.Request
|
|
var body map[string]any
|
|
client := newServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
seen = r
|
|
data, _ := io.ReadAll(r.Body)
|
|
json.Unmarshal(data, &body)
|
|
w.WriteHeader(http.StatusCreated)
|
|
io.WriteString(w, `{"id": 7, "user_id": 1, "kind": "text", "content": "hi", "project_id": 3}`)
|
|
})
|
|
project := int64(3)
|
|
|
|
clip, err := client.CreateClip(context.Background(), "hi", &project)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if clip.ID != 7 || *clip.ProjectID != 3 {
|
|
t.Fatalf("unexpected clip %+v", clip)
|
|
}
|
|
if seen.URL.Path != "/api/clips" || seen.Method != http.MethodPost {
|
|
t.Fatalf("unexpected request %s %s", seen.Method, seen.URL.Path)
|
|
}
|
|
if seen.Header.Get("Authorization") != "Bearer lc_token" {
|
|
t.Fatal("token missing")
|
|
}
|
|
if !strings.HasPrefix(seen.Header.Get("User-Agent"), "lc-agent/test") {
|
|
t.Fatalf("user agent %q", seen.Header.Get("User-Agent"))
|
|
}
|
|
if body["content"] != "hi" || body["kind"] != "text" || body["project_id"] != float64(3) {
|
|
t.Fatalf("unexpected body %v", body)
|
|
}
|
|
}
|
|
|
|
func TestErrors(t *testing.T) {
|
|
status := http.StatusUnauthorized
|
|
client := newServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(status)
|
|
io.WriteString(w, `{"detail": "Projekt nicht gefunden."}`)
|
|
})
|
|
if _, err := client.Me(context.Background()); !errors.Is(err, ErrUnauthorized) {
|
|
t.Fatalf("want ErrUnauthorized, got %v", err)
|
|
}
|
|
status = http.StatusNotFound
|
|
if err := client.DeleteClip(context.Background(), 5); err != nil {
|
|
t.Fatalf("deleting a missing clip should succeed: %v", err)
|
|
}
|
|
if _, err := client.CreateProject(context.Background(), "x"); err == nil ||
|
|
!strings.Contains(err.Error(), "Projekt nicht gefunden.") {
|
|
t.Fatalf("detail not reported: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEventsDeliversMessagesAndDetectsRevokedToken(t *testing.T) {
|
|
client := newServer(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/ws" || r.Header.Get("Authorization") != "Bearer lc_token" {
|
|
http.Error(w, "bad", http.StatusBadRequest)
|
|
return
|
|
}
|
|
conn, err := websocket.Accept(w, r, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
ctx := r.Context()
|
|
conn.Write(ctx, websocket.MessageText, []byte(`{"type":"hello"}`))
|
|
conn.Write(ctx, websocket.MessageText, []byte(`{"type":"clip.created","data":{"id":1}}`))
|
|
conn.Close(4401, "revoked")
|
|
})
|
|
|
|
var types []string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
err := client.Events(ctx, func(e Event) { types = append(types, e.Type) })
|
|
|
|
if !errors.Is(err, ErrUnauthorized) {
|
|
t.Fatalf("want ErrUnauthorized, got %v", err)
|
|
}
|
|
if strings.Join(types, ",") != "hello,clip.created" {
|
|
t.Fatalf("events %v", types)
|
|
}
|
|
}
|