Skip to content

Commit bf96299

Browse files
committed
test(core): cover typed side effect payloads
1 parent b2d61d4 commit bf96299

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Text.Json;
2+
using ModularityKit.Mutator.Abstractions.Effects;
3+
using Xunit;
4+
5+
namespace ModularityKit.Mutator.Tests.Effects;
6+
7+
public sealed class SideEffectTypedDataTests
8+
{
9+
[Fact]
10+
public void Create_with_typed_payload_populates_contract_metadata()
11+
{
12+
var effect = SideEffect.Create(
13+
type: "WorkflowStarted",
14+
description: "Workflow started",
15+
data: new WorkflowStartedSideEffectData
16+
{
17+
Initiator = "alice",
18+
StepCount = 2,
19+
WorkflowId = "wf-42"
20+
});
21+
22+
Assert.Equal("workflow.started", effect.DataContractType);
23+
Assert.Equal(1, effect.DataContractVersion);
24+
Assert.True(effect.TryGetData<WorkflowStartedSideEffectData>(out var data));
25+
Assert.Equal("wf-42", data!.WorkflowId);
26+
}
27+
28+
[Fact]
29+
public void Json_roundtrip_rehydrates_registered_typed_payload()
30+
{
31+
SideEffectDataContractRegistry.Register<WorkflowStartedSideEffectData>();
32+
33+
var effect = SideEffect.Create(
34+
type: "WorkflowStarted",
35+
description: "Workflow started",
36+
data: new WorkflowStartedSideEffectData
37+
{
38+
Initiator = "alice",
39+
StepCount = 2,
40+
WorkflowId = "wf-42"
41+
});
42+
43+
var roundtrip = JsonSerializer.Deserialize<SideEffect>(JsonSerializer.Serialize(effect));
44+
45+
Assert.NotNull(roundtrip);
46+
Assert.Equal("workflow.started", roundtrip!.DataContractType);
47+
Assert.True(roundtrip.TryGetData<WorkflowStartedSideEffectData>(out var data));
48+
Assert.Equal("alice", data!.Initiator);
49+
Assert.Equal(2, data.StepCount);
50+
}
51+
52+
[Fact]
53+
public void Json_roundtrip_without_registration_preserves_contract_and_payload_shape()
54+
{
55+
var effect = new SideEffect
56+
{
57+
Type = "WorkflowStarted",
58+
Description = "Workflow started",
59+
Data = new WorkflowStartedSideEffectData
60+
{
61+
Initiator = "alice",
62+
StepCount = 2,
63+
WorkflowId = "wf-42"
64+
},
65+
DataContractType = "workflow.started.unregistered",
66+
DataContractVersion = 1
67+
};
68+
69+
var roundtrip = JsonSerializer.Deserialize<SideEffect>(JsonSerializer.Serialize(effect));
70+
71+
Assert.NotNull(roundtrip);
72+
Assert.Equal("workflow.started.unregistered", roundtrip!.DataContractType);
73+
Assert.Equal(1, roundtrip.DataContractVersion);
74+
75+
var payload = Assert.IsAssignableFrom<IReadOnlyDictionary<string, object?>>(roundtrip.Data);
76+
Assert.Equal("alice", payload["Initiator"]);
77+
Assert.Equal(2L, payload["StepCount"]);
78+
Assert.Equal("wf-42", payload["WorkflowId"]);
79+
}
80+
81+
[SideEffectDataContract("workflow.started", 1)]
82+
private sealed record WorkflowStartedSideEffectData
83+
{
84+
public required string Initiator { get; init; }
85+
86+
public required int StepCount { get; init; }
87+
88+
public required string WorkflowId { get; init; }
89+
}
90+
}

0 commit comments

Comments
 (0)