-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatDtoSerializationFuzzTests.cs
More file actions
194 lines (166 loc) · 7.77 KB
/
ChatDtoSerializationFuzzTests.cs
File metadata and controls
194 lines (166 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Text.Json;
using FluentAssertions;
using FsCheck;
using FsCheck.Fluent;
using FsCheck.Xunit;
using Taskdeck.Application.DTOs;
using Taskdeck.Domain.Entities;
using Xunit;
namespace Taskdeck.Application.Tests.Fuzz;
/// <summary>
/// Property-based JSON serialization round-trip tests for Chat DTOs.
/// Key property: serialize then deserialize produces identical object.
/// Exercises adversarial string content in titles, messages, and metadata.
/// </summary>
public class ChatDtoSerializationFuzzTests
{
private const int MaxTests = 200;
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = false
};
private static Gen<ChatMessageRole> RoleGen() =>
Gen.Elements(ChatMessageRole.User, ChatMessageRole.Assistant, ChatMessageRole.System);
private static Gen<ChatSessionStatus> StatusGen() =>
Gen.Elements(ChatSessionStatus.Active, ChatSessionStatus.Archived);
// ─────────────────────── ChatSessionDto round-trip ───────────────────────
[Property(MaxTest = MaxTests)]
public Property ChatSessionDto_RoundTrip_PreservesAllFields()
{
return Prop.ForAll(
Arb.From(FuzzTestGenerators.AdversarialStringGen()),
Arb.From(StatusGen()),
(title, status) =>
{
var dto = new ChatSessionDto(
Guid.NewGuid(),
Guid.NewGuid(),
Guid.NewGuid(),
title,
status,
DateTimeOffset.UtcNow,
DateTimeOffset.UtcNow,
new List<ChatMessageDto>());
var json = JsonSerializer.Serialize(dto, JsonOptions);
var deserialized = JsonSerializer.Deserialize<ChatSessionDto>(json, JsonOptions);
deserialized.Should().NotBeNull();
deserialized!.Title.Should().Be(title);
deserialized.Status.Should().Be(status);
deserialized.Id.Should().Be(dto.Id);
deserialized.UserId.Should().Be(dto.UserId);
deserialized.BoardId.Should().Be(dto.BoardId);
});
}
// ─────────────────────── ChatMessageDto round-trip ───────────────────────
[Property(MaxTest = MaxTests)]
public Property ChatMessageDto_RoundTrip_PreservesAllFields()
{
return Prop.ForAll(
Arb.From(FuzzTestGenerators.AdversarialStringGen()),
Arb.From(RoleGen()),
Arb.From(FuzzTestGenerators.NullableStringGen()),
(content, role, degradedReason) =>
{
var dto = new ChatMessageDto(
Guid.NewGuid(),
Guid.NewGuid(),
role,
content,
"text",
null,
42,
DateTimeOffset.UtcNow,
degradedReason);
var json = JsonSerializer.Serialize(dto, JsonOptions);
var deserialized = JsonSerializer.Deserialize<ChatMessageDto>(json, JsonOptions);
deserialized.Should().NotBeNull();
deserialized!.Content.Should().Be(content);
deserialized.Role.Should().Be(role);
deserialized.DegradedReason.Should().Be(degradedReason);
deserialized.MessageType.Should().Be("text");
deserialized.TokenUsage.Should().Be(42);
});
}
// ─────────────────────── CreateChatSessionDto round-trip ───────────────────────
[Property(MaxTest = MaxTests)]
public Property CreateChatSessionDto_RoundTrip_Identity()
{
return Prop.ForAll(
Arb.From(FuzzTestGenerators.AdversarialStringGen()),
title =>
{
var dto = new CreateChatSessionDto(title, Guid.NewGuid());
var json = JsonSerializer.Serialize(dto, JsonOptions);
var deserialized = JsonSerializer.Deserialize<CreateChatSessionDto>(json, JsonOptions);
deserialized.Should().NotBeNull();
deserialized!.Title.Should().Be(title);
deserialized.BoardId.Should().Be(dto.BoardId);
});
}
// ─────────────────────── SendChatMessageDto round-trip ───────────────────────
[Property(MaxTest = MaxTests)]
public Property SendChatMessageDto_RoundTrip_Identity()
{
return Prop.ForAll(
Arb.From(FuzzTestGenerators.AdversarialStringGen()),
ArbMap.Default.ArbFor<bool>(),
(content, requestProposal) =>
{
var dto = new SendChatMessageDto(content, requestProposal);
var json = JsonSerializer.Serialize(dto, JsonOptions);
var deserialized = JsonSerializer.Deserialize<SendChatMessageDto>(json, JsonOptions);
deserialized.Should().NotBeNull();
deserialized!.Content.Should().Be(content);
deserialized.RequestProposal.Should().Be(requestProposal);
});
}
// ─────────────────────── ChatSessionDto with messages list ───────────────────────
[Property(MaxTest = MaxTests)]
public Property ChatSessionDto_WithMessages_RoundTrip()
{
return Prop.ForAll(
Arb.From(FuzzTestGenerators.AdversarialStringGen()),
Arb.From(FuzzTestGenerators.AdversarialStringGen()),
(title, msgContent) =>
{
var messages = new List<ChatMessageDto>
{
new(Guid.NewGuid(), Guid.NewGuid(), ChatMessageRole.User,
msgContent, "text", null, null, DateTimeOffset.UtcNow),
new(Guid.NewGuid(), Guid.NewGuid(), ChatMessageRole.Assistant,
title, "text", Guid.NewGuid(), 100, DateTimeOffset.UtcNow)
};
var dto = new ChatSessionDto(
Guid.NewGuid(), Guid.NewGuid(), null, title,
ChatSessionStatus.Active, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow,
messages);
var json = JsonSerializer.Serialize(dto, JsonOptions);
var deserialized = JsonSerializer.Deserialize<ChatSessionDto>(json, JsonOptions);
deserialized.Should().NotBeNull();
deserialized!.RecentMessages.Should().HaveCount(2);
deserialized.RecentMessages[0].Content.Should().Be(msgContent);
deserialized.RecentMessages[1].Content.Should().Be(title);
});
}
// ─────────────────────── Malformed JSON deserialization ───────────────────────
[Theory]
[InlineData("{}")]
[InlineData("{\"title\": null}")]
[InlineData("{\"extra_field\": \"value\"}")]
[InlineData("{\"title\": 12345}")]
[InlineData("null")]
public void CreateChatSessionDto_MalformedJson_HandledGracefully(string json)
{
try
{
var result = JsonSerializer.Deserialize<CreateChatSessionDto>(json, JsonOptions);
// If it deserializes, that's fine — API layer validates
}
catch (JsonException)
{
// Expected for truly malformed JSON
}
}
}