first commit

This commit is contained in:
npc0-hue
2026-07-11 14:56:10 +08:00
commit 7e05d0a4e7
660 changed files with 78119 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"browser.local/run/protocol"
)
func TestPlatformClientIngestLogBatchPostsJSONAndDecodesResponse(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/api/v1/run/logs/batches" {
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
}
if contentType := r.Header.Get("Content-Type"); contentType != "application/json" {
t.Fatalf("expected JSON content type, got %q", contentType)
}
var request protocol.LogBatchIngestRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
t.Fatalf("decode log ingest request: %v", err)
}
if request.LogStreamID != "log-1" || request.FirstSeq != 1 || request.LastSeq != 1 || len(request.Entries) != 1 {
t.Fatalf("unexpected log ingest payload: %+v", request)
}
writeTestJSON(t, w, protocol.LogBatchIngestResponse{Accepted: true, LogStreamID: "log-1", AcceptedFrom: 1, AcceptedTo: 1, LatestSeq: 1, ServerTime: fixedClientTestTime()})
}))
defer server.Close()
client, err := NewPlatformClient(server.URL)
if err != nil {
t.Fatalf("new client: %v", err)
}
response, err := client.IngestLogBatch(context.Background(), validClientLogBatch())
if err != nil {
t.Fatalf("ingest log batch: %v", err)
}
if !response.Accepted || response.LatestSeq != 1 {
t.Fatalf("unexpected log ingest response: %+v", response)
}
}
func TestPlatformClientIngestLogBatchReturnsErrorForPlatformFailure(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"code":"validation_failed"}`))
}))
defer server.Close()
client, err := NewPlatformClient(server.URL)
if err != nil {
t.Fatalf("new client: %v", err)
}
if _, err := client.IngestLogBatch(context.Background(), validClientLogBatch()); err == nil {
t.Fatal("expected platform error")
}
}
func validClientLogBatch() protocol.LogBatchIngestRequest {
return protocol.LogBatchIngestRequest{
RunEndpointID: "run-local",
SessionToken: "session-token",
LogStreamID: "log-1",
ServerInstanceID: "server-1",
StreamKey: "stdout",
Source: "process",
FirstSeq: 1,
LastSeq: 1,
Compression: "none",
Checksum: "sha256:test",
Entries: []protocol.LogEntry{{
Seq: 1,
Timestamp: time.Date(2026, 7, 3, 12, 0, 1, 0, time.UTC),
Level: "info",
Line: "line",
}},
}
}