refactor
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/flow"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/order"
|
||||
messages "git.k6n.net/mats/go-cart-actor/proto/order"
|
||||
)
|
||||
|
||||
func TestProjectFulfillmentIntegrationHookIsRegistered(t *testing.T) {
|
||||
s := testServer(t)
|
||||
if !strings.Contains(strings.Join(s.freg.Capabilities().Hooks, ","), "project_fulfillment_integration") {
|
||||
t.Fatalf("hooks = %v, want project_fulfillment_integration", s.freg.Capabilities().Hooks)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectFulfillmentIntegrationLogs(t *testing.T) {
|
||||
s := testServer(t)
|
||||
var logs bytes.Buffer
|
||||
logger := slog.New(slog.NewJSONHandler(&logs, nil))
|
||||
|
||||
provider := order.NewPassthroughProvider("legacy", "ref", 25000)
|
||||
freg := buildFlowRegistry(s.applier, provider, nil, nil, nil, nil, logger)
|
||||
engine := flow.NewEngine(freg, logger)
|
||||
|
||||
st := flow.NewState(801, logger)
|
||||
po := orderTestPlaceMsg()
|
||||
po.CustomerEmail = "dropship@example.com"
|
||||
st.Vars[order.PlaceOrderVar] = po
|
||||
|
||||
def, err := order.EmbeddedFlow("place-and-pay")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := engine.Run(context.Background(), def, st); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ship := &flow.Definition{Name: "ship", Steps: []flow.Step{{
|
||||
Name: "fulfill",
|
||||
Action: "create_fulfillment",
|
||||
Params: json.RawMessage(`{"carrier":"dropshipper","trackingNumber":"DS-123","lines":[{"reference":"l1","quantity":1}]}`),
|
||||
Hooks: flow.Hooks{After: []flow.HookRef{{
|
||||
Type: "project_fulfillment_integration",
|
||||
Params: json.RawMessage(`{"integration":"dropship-test","target":"erp-sandbox","dropship":true}`),
|
||||
}}},
|
||||
}}}
|
||||
if _, err := engine.Run(context.Background(), ship, flow.NewState(801, logger)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got := logs.String()
|
||||
for _, want := range []string{
|
||||
`"msg":"project fulfillment integration"`,
|
||||
`"integration":"dropship-test"`,
|
||||
`"target":"erp-sandbox"`,
|
||||
`"dropship":true`,
|
||||
`"trackingNumber":"DS-123"`,
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Fatalf("log output missing %s: %s", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func orderTestPlaceMsg() *messages.PlaceOrder {
|
||||
return &messages.PlaceOrder{
|
||||
OrderReference: "ref-1",
|
||||
CartId: "cart-1",
|
||||
Currency: "SEK",
|
||||
Country: "SE",
|
||||
CustomerName: "Dropship Customer",
|
||||
TotalAmount: 25000,
|
||||
TotalTax: 5000,
|
||||
PlacedAtMs: 1718877600000,
|
||||
Lines: []*messages.OrderLine{
|
||||
{Reference: "l1", Sku: "ABC", Name: "Widget", Quantity: 2, UnitPrice: 10000, TaxRate: 25, TotalAmount: 20000},
|
||||
{Reference: "l2", Sku: "DEF", Name: "Gizmo", Quantity: 1, UnitPrice: 5000, TaxRate: 25, TotalAmount: 5000},
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user