79 lines
2.8 KiB
Go
79 lines
2.8 KiB
Go
package orbit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWorkflowEventsSDKIdentityPrecisionAndBounds(t *testing.T) {
|
|
const id = "11111111111111111111111111111111"
|
|
const event = "22222222222222222222222222222222"
|
|
var calls atomic.Int32
|
|
var corrupt atomic.Bool
|
|
summary := WorkflowEventSummary{ID: event, WorkflowID: id, AggregateKind: "workflow", AggregateID: id, AggregateRevision: 9007199254740993, Type: "workflow.advanced", StateScope: "WORKFLOW", State: "WAITING", OccurredAt: time.Date(2026, 9, 18, 0, 0, 0, 0, time.UTC)}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls.Add(1)
|
|
if r.Method != "GET" || r.Header.Get("Authorization") != "Bearer token" || r.Header.Get("Idempotency-Key") != "" {
|
|
t.Error("read request invalid")
|
|
}
|
|
item := summary
|
|
if corrupt.Load() {
|
|
item.WorkflowID = event
|
|
}
|
|
var data any
|
|
if strings.HasSuffix(r.URL.Path, "/events") {
|
|
if r.URL.Query().Get("limit") != "1" || r.URL.Query().Get("cursor") != "cursor+/=" {
|
|
t.Error("cursor encoding changed")
|
|
}
|
|
data = WorkflowEventPage{WorkflowID: id, Revision: 9007199254740993, Items: []WorkflowEventSummary{item}}
|
|
} else {
|
|
data = WorkflowEventDetail{WorkflowEventSummary: item, Payload: json.RawMessage(`{"n":9007199254740993,"state":"WAITING"}`)}
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"request_id": event, "data": data})
|
|
}))
|
|
defer server.Close()
|
|
c, err := NewClient(server.URL, Options{Token: "token"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
input := HistoryPageRequest{Limit: 1, Cursor: "cursor+/="}
|
|
page, err := c.ListWorkflowEvents(context.Background(), id, input)
|
|
if err != nil || page.Items[0].AggregateRevision != 9007199254740993 {
|
|
t.Fatal(page, err)
|
|
}
|
|
detail, err := c.GetWorkflowEvent(context.Background(), id, event)
|
|
if err != nil || !strings.Contains(string(detail.Payload), "9007199254740993") {
|
|
t.Fatal(detail, err)
|
|
}
|
|
before := calls.Load()
|
|
for _, bad := range []string{"", "../wrong", strings.Repeat("A", 32)} {
|
|
if _, err := c.ListWorkflowEvents(context.Background(), bad, input); err == nil {
|
|
t.Fatal("invalid parent")
|
|
}
|
|
if _, err := c.GetWorkflowEvent(context.Background(), id, bad); err == nil {
|
|
t.Fatal("invalid event")
|
|
}
|
|
}
|
|
for _, bad := range []HistoryPageRequest{{Limit: -1}, {Limit: 101}, {Cursor: strings.Repeat("x", 1025)}} {
|
|
if _, err := c.ListWorkflowEvents(context.Background(), id, bad); err == nil {
|
|
t.Fatal("invalid page")
|
|
}
|
|
}
|
|
if calls.Load() != before {
|
|
t.Fatal("invalid request reached HTTP")
|
|
}
|
|
corrupt.Store(true)
|
|
if _, err := c.ListWorkflowEvents(context.Background(), id, input); err == nil {
|
|
t.Fatal("foreign item accepted")
|
|
}
|
|
if _, err := c.GetWorkflowEvent(context.Background(), id, event); err == nil {
|
|
t.Fatal("foreign detail accepted")
|
|
}
|
|
}
|