-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFlowContextTests.cs
More file actions
215 lines (179 loc) · 6.09 KB
/
FlowContextTests.cs
File metadata and controls
215 lines (179 loc) · 6.09 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using MaIN.Core.Hub.Contexts;
using MaIN.Domain.Entities;
using MaIN.Domain.Entities.Agents;
using MaIN.Domain.Entities.Agents.AgentSource;
using MaIN.Domain.Entities.Agents.Knowledge;
using MaIN.Domain.Exceptions.Flows;
using MaIN.Domain.Models.Abstract;
using MaIN.Services.Services.Abstract;
using Moq;
namespace MaIN.Core.UnitTests;
public class FlowContextTests
{
private readonly Mock<IAgentFlowService> _mockFlowService;
private readonly Mock<IAgentService> _mockAgentService;
private FlowContext _flowContext;
private readonly string _testModelId = "test-model";
public FlowContextTests()
{
_mockFlowService = new Mock<IAgentFlowService>();
_mockAgentService = new Mock<IAgentService>();
_flowContext = new FlowContext(_mockFlowService.Object, _mockAgentService.Object);
var testModel = new GenericLocalModel(_testModelId);
ModelRegistry.RegisterOrReplace(testModel);
}
[Fact]
public async Task WithId_ShouldSetFlowId()
{
// Arrange
var expectedId = "test-flow-id";
// Act
var result = _flowContext.WithId(expectedId);
// Setup mock to return flow with the set ID
_mockFlowService
.Setup(s => s.GetFlowById(expectedId))
.ReturnsAsync(new AgentFlow { Id = expectedId, Name = It.IsAny<string>() });
var flow = await _flowContext.GetCurrentFlow();
// Assert
Assert.Equal(expectedId, flow.Id);
Assert.Equal(result, _flowContext);
}
[Fact]
public async Task WithName_ShouldSetFlowName()
{
// Arrange
var expectedName = "Test Flow";
// Act
var result = _flowContext.WithName(expectedName);
// Setup mock to return flow with the set name
_mockFlowService
.Setup(s => s.GetFlowById(It.IsAny<string>()))
.ReturnsAsync(new AgentFlow
{
Id = It.IsAny<string>(),
Name = expectedName
});
var flow = await _flowContext.GetCurrentFlow();
// Assert
Assert.Equal(expectedName, flow.Name);
Assert.Equal(result, _flowContext);
}
[Fact]
public async Task CreateAsync_ShouldCallFlowService()
{
// Arrange
var expectedFlow = new AgentFlow { Id = "new-flow-id", Name = It.IsAny<string>() };
_mockFlowService
.Setup(s => s.CreateFlow(It.IsAny<AgentFlow>()))
.ReturnsAsync(expectedFlow);
// Act
var result = await _flowContext.CreateAsync();
// Assert
_mockFlowService.Verify(s => s.CreateFlow(It.IsAny<AgentFlow>()), Times.Once);
Assert.Equal(expectedFlow, result);
}
[Fact]
public async Task ProcessAsync_WithStringMessage_ShouldReturnChatResult()
{
// Arrange
var firstAgent = new Agent
{
Id = "first-agent",
Order = 0,
CurrentBehaviour = It.IsAny<string>(),
Config = new AgentConfig()
};
_flowContext.AddAgent(firstAgent);
var message = "Hello, flow!";
var chat = new Chat { Id = firstAgent.Id, Messages = [], ModelId = _testModelId, Name = "test" };
_mockAgentService
.Setup(s => s.GetChatByAgent(firstAgent.Id))
.ReturnsAsync(chat);
_mockAgentService
.Setup(s => s.Process(It.IsAny<Chat>(), firstAgent.Id, It.IsAny<Knowledge>(), It.IsAny<bool>(), null, null))
.ReturnsAsync(new Chat
{
ModelId = _testModelId,
Name = "test",
Messages =
[
new() { Content = "Response", Role = "Assistant", Type = MessageType.LocalLLM}
]
});
// Act
var result = await _flowContext.ProcessAsync(message);
// Assert
Assert.True(result.Done);
Assert.Equal(_testModelId, result.Model);
Assert.NotNull(result.Message);
}
[Fact]
public async Task Delete_ShouldCallFlowServiceDelete()
{
// Arrange
var flowId = "test-flow-id";
_flowContext.WithId(flowId);
_mockFlowService
.Setup(s => s.DeleteFlow(flowId))
.Returns(Task.CompletedTask);
// Act
await _flowContext.Delete();
// Assert
_mockFlowService.Verify(s => s.DeleteFlow(flowId), Times.Once);
}
[Fact]
public async Task GetAllFlows_ShouldReturnListOfFlows()
{
// Arrange
var expectedFlows = new List<AgentFlow>
{
new() { Id = "flow1", Name = "Flow 1" },
new() { Id = "flow2", Name = "Flow 2" }
};
_mockFlowService
.Setup(s => s.GetAllFlows())
.ReturnsAsync(expectedFlows);
// Act
var result = await _flowContext.GetAllFlows();
// Assert
Assert.Equal(expectedFlows, result);
}
[Fact]
public async Task FromExisting_ShouldCreateFlowContextFromExistingFlow()
{
// Arrange
var existingFlowId = "existing-flow-id";
var existingFlow = new AgentFlow
{
Id = existingFlowId,
Name = "Existing Flow",
Agents =
[
new Agent
{
Id = "agent1",
CurrentBehaviour = It.IsAny<string>(),
Config = new AgentConfig()
}
]
};
_mockFlowService
.Setup(s => s.GetFlowById(existingFlowId))
.ReturnsAsync(existingFlow);
// Act
var result = await _flowContext.FromExisting(existingFlowId);
// Assert
Assert.NotNull(result);
}
[Fact]
public async Task FromExisting_ShouldThrowArgumentExceptionWhenFlowNotFound()
{
// Arrange
var nonExistentFlowId = "non-existent-flow-id";
_mockFlowService
.Setup(s => s.GetFlowById(nonExistentFlowId))
.ReturnsAsync((AgentFlow)null!);
// Act & Assert
await Assert.ThrowsAsync<FlowNotFoundException>(() => _flowContext.FromExisting(nonExistentFlowId));
}
}