feat: add remote status report client
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSessionKeyStable(t *testing.T) {
|
||||
k1 := SessionKey("host", "10.0.0.1", "/proj", "Cursor")
|
||||
k2 := SessionKey("host", "10.0.0.1", "/proj", "Cursor")
|
||||
if k1 != k2 || len(k1) != 32 {
|
||||
t.Fatalf("key=%q len=%d", k1, len(k1))
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientPostStatus(t *testing.T) {
|
||||
var got StatusReport
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
if r.URL.Path != "/api/v1/status" {
|
||||
http.Error(w, "path", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Authorization") != "Bearer secret" {
|
||||
http.Error(w, "auth", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
|
||||
http.Error(w, "decode", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"ok":true}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(ClientConfig{URL: srv.URL, Token: "secret", Timeout: time.Second})
|
||||
err := c.Report(context.Background(), StatusReport{Hostname: "h", Agent: "Cursor", CWD: "/p", Status: "waiting"})
|
||||
if err != nil || got.Hostname != "h" {
|
||||
t.Fatalf("err=%v got=%+v", err, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user