more changes

This commit is contained in:
matst80
2025-10-10 09:34:40 +00:00
parent b97eb8f285
commit e7c67fbb9b
25 changed files with 1888 additions and 3689 deletions

View File

@@ -1,9 +1,7 @@
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"
"time"
@@ -14,7 +12,7 @@ import (
// TestCartActorMutationAndState validates end-to-end gRPC mutation + state retrieval
// against a locally started gRPC server (single-node scenario).
// This test uses AddItemType directly to avoid hitting external product
// This test uses the oneof MutationEnvelope directly to avoid hitting external product
// fetching logic (FetchItem) which would require network I/O.
func TestCartActorMutationAndState(t *testing.T) {
// Setup local grain pool + synced pool (no discovery, single host)
@@ -62,35 +60,30 @@ func TestCartActorMutationAndState(t *testing.T) {
Country: "se",
}
// Marshal underlying mutation payload using the existing handler code path
handler, ok := Handlers[AddItemType]
if !ok {
t.Fatalf("Handler for AddItemType missing")
}
payloadData, err := getSerializedPayload(handler, AddItemType, addItem)
if err != nil {
t.Fatalf("serialize add item: %v", err)
// Build oneof envelope directly (no legacy handler/enum)
envelope := &messages.MutationEnvelope{
CartId: cartID,
ClientTimestamp: time.Now().Unix(),
Mutation: &messages.MutationEnvelope_AddItem{
AddItem: addItem,
},
}
// Issue Mutate RPC
mutResp, err := cartClient.Mutate(context.Background(), &messages.MutationRequest{
CartId: cartID,
Type: messages.MutationType(AddItemType),
Payload: payloadData,
ClientTimestamp: time.Now().Unix(),
})
mutResp, err := cartClient.Mutate(context.Background(), envelope)
if err != nil {
t.Fatalf("Mutate RPC error: %v", err)
}
if mutResp.StatusCode != 200 {
t.Fatalf("Mutate returned non-200 status: %d payload=%s", mutResp.StatusCode, string(mutResp.Payload))
t.Fatalf("Mutate returned non-200 status: %d, error: %s", mutResp.StatusCode, mutResp.GetError())
}
// Decode cart state JSON and validate
state := &CartGrain{}
if err := json.Unmarshal(mutResp.Payload, state); err != nil {
t.Fatalf("Unmarshal mutate cart state: %v\nPayload: %s", err, string(mutResp.Payload))
// Validate the response state
state := mutResp.GetState()
if state == nil {
t.Fatalf("Mutate response state is nil")
}
if len(state.Items) != 1 {
t.Fatalf("Expected 1 item after mutation, got %d", len(state.Items))
}
@@ -106,13 +99,14 @@ func TestCartActorMutationAndState(t *testing.T) {
t.Fatalf("GetState RPC error: %v", err)
}
if getResp.StatusCode != 200 {
t.Fatalf("GetState returned non-200 status: %d payload=%s", getResp.StatusCode, string(getResp.Payload))
t.Fatalf("GetState returned non-200 status: %d, error: %s", getResp.StatusCode, getResp.GetError())
}
state2 := &CartGrain{}
if err := json.Unmarshal(getResp.Payload, state2); err != nil {
t.Fatalf("Unmarshal get state: %v", err)
state2 := getResp.GetState()
if state2 == nil {
t.Fatalf("GetState response state is nil")
}
if len(state2.Items) != 1 {
t.Fatalf("Expected 1 item in GetState, got %d", len(state2.Items))
}
@@ -121,15 +115,4 @@ func TestCartActorMutationAndState(t *testing.T) {
}
}
// getSerializedPayload serializes a mutation proto using the registered handler.
func getSerializedPayload(handler MessageHandler, msgType uint16, content interface{}) ([]byte, error) {
msg := &Message{
Type: msgType,
Content: content,
}
var buf bytes.Buffer
if err := handler.Write(msg, &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Legacy serialization helper removed (oneof envelope used directly)