-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGiven_TestingSample.cs
More file actions
231 lines (197 loc) · 8.39 KB
/
Given_TestingSample.cs
File metadata and controls
231 lines (197 loc) · 8.39 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
namespace Samples.Testing;
[TestClass]
[DoNotParallelize]
public sealed class Given_TestingSample
{
[TestMethod]
[Description("Shows command execution with text output assertions.")]
public async Task When_RunningSimpleCommand_Then_OutputIsAvailable()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync();
var execution = await session.RunCommandAsync("ping --no-logo");
execution.ExitCode.Should().Be(0);
execution.OutputText.Should().Contain("pong");
}
[TestMethod]
[Description("Shows typed result assertions from one command.")]
public async Task When_CommandReturnsObject_Then_TypedAssertionsArePossible()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync();
var execution = await session.RunCommandAsync("widget show 1 --json --no-logo");
execution.GetResult<SampleReplApp.Widget>().Name.Should().Be("Alpha");
execution.OutputText.Should().Contain("Alpha");
}
[TestMethod]
[Description("Shows semantic interaction assertions with captured events.")]
public async Task When_CommandEmitsStatus_Then_InteractionEventsCanBeAsserted()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync();
var execution = await session.RunCommandAsync("import --no-logo");
execution.InteractionEvents
.OfType<ReplStatusEvent>()
.Should()
.ContainSingle(evt => string.Equals(evt.Text, "Import started", StringComparison.Ordinal));
execution.TimelineEvents.OfType<ResultProducedEvent>().Should().ContainSingle();
}
[TestMethod]
[Description("Shows multi-session simulation and snapshot assertions.")]
public async Task When_MultipleSessionsAreOpen_Then_SessionSnapshotsExposeMetadata()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var ws = await host.OpenSessionAsync(new SessionDescriptor
{
TransportName = "websocket",
RemotePeer = "::1:60288",
TerminalIdentity = "xterm-256color",
WindowSize = (132, 43),
});
await using var telnet = await host.OpenSessionAsync(new SessionDescriptor
{
TransportName = "telnet",
RemotePeer = "::1:45123",
TerminalIdentity = "XTERM-256COLOR",
WindowSize = (120, 40),
});
_ = await ws.RunCommandAsync("ping --no-logo");
_ = await telnet.RunCommandAsync("ping --no-logo");
var snapshots = await host.QuerySessionsAsync();
snapshots.Should().HaveCount(2);
var wsSnapshot = snapshots.Single(snapshot =>
string.Equals(snapshot.SessionId, ws.SessionId, StringComparison.Ordinal));
wsSnapshot.Transport.Should().Be("websocket");
wsSnapshot.Screen.Should().Be((132, 43));
}
[TestMethod]
[Description("Shows per-command timeout behavior for long-running handlers.")]
public async Task When_CommandIsTooSlow_Then_TimeoutIsRaised()
{
await using var host = ReplTestHost.Create(
() =>
{
var app = ReplApp.Create().UseDefaultInteractive();
app.Map("slow", async (CancellationToken ct) =>
{
await Task.Delay(TimeSpan.FromSeconds(1), ct).ConfigureAwait(false);
return "done";
});
return app;
},
options => options.CommandTimeout = TimeSpan.FromMilliseconds(50));
await using var session = await host.OpenSessionAsync();
Func<Task> action = () => session.RunCommandAsync("slow --no-logo").AsTask();
var assertion = await action.Should().ThrowAsync<TimeoutException>();
assertion.Which.Message.Should().Contain("timeout");
}
[TestMethod]
[Description("Shows prefilled text prompt answers via the per-command answers API.")]
public async Task When_CommandPromptsForText_Then_PrefillAnswerIsUsed()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync();
var execution = await session.RunCommandAsync(
"greet --no-logo",
new Dictionary<string, string> { ["name"] = "Alice" });
execution.ExitCode.Should().Be(0);
execution.OutputText.Should().Contain("Hello, Alice!");
}
[TestMethod]
[Description("Shows prefilled confirmation prompt answers.")]
public async Task When_CommandPromptsForConfirmation_Then_PrefillAnswerIsUsed()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync();
var execution = await session.RunCommandAsync(
"deploy --no-logo",
new Dictionary<string, string> { ["proceed"] = "yes" });
execution.ExitCode.Should().Be(0);
execution.GetResult<bool>().Should().BeTrue();
}
[TestMethod]
[Description("Shows prefilled choice prompt answers.")]
public async Task When_CommandPromptsForChoice_Then_PrefillAnswerIsUsed()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync();
var execution = await session.RunCommandAsync(
"region --no-logo",
new Dictionary<string, string> { ["region"] = "eu-west" });
execution.ExitCode.Should().Be(0);
execution.GetResult<string>().Should().Be("eu-west");
}
[TestMethod]
[Description("Shows session-level default answers applied to all commands.")]
public async Task When_SessionHasDefaultAnswers_Then_AllCommandsUseThem()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync(new SessionDescriptor
{
Answers = new Dictionary<string, string> { ["name"] = "Bob" },
});
var execution = await session.RunCommandAsync("greet --no-logo");
execution.ExitCode.Should().Be(0);
execution.OutputText.Should().Contain("Hello, Bob!");
}
[TestMethod]
[Description("Shows per-command answers overriding session-level defaults.")]
public async Task When_PerCommandAnswerOverridesSession_Then_CommandWins()
{
await using var host = ReplTestHost.Create(() => SampleReplApp.Create());
await using var session = await host.OpenSessionAsync(new SessionDescriptor
{
Answers = new Dictionary<string, string> { ["name"] = "Bob" },
});
var execution = await session.RunCommandAsync(
"greet --no-logo",
new Dictionary<string, string> { ["name"] = "Alice" });
execution.ExitCode.Should().Be(0);
execution.OutputText.Should().Contain("Hello, Alice!");
}
[TestMethod]
[Description("Shows a realistic multi-step scenario with two sessions, shared state, typed assertions, and metadata checks.")]
public async Task When_RunningComplexScenario_Then_EndToEndBehaviorRemainsReadable()
{
var shared = new SampleReplApp.SharedState();
await using var host = ReplTestHost.Create(() => SampleReplApp.Create(shared));
await using var admin = await host.OpenSessionAsync(new SessionDescriptor
{
TransportName = "signalr",
RemotePeer = "::1:41957",
TerminalIdentity = "xterm-256color",
WindowSize = (140, 45),
});
await using var operatorSession = await host.OpenSessionAsync(new SessionDescriptor
{
TransportName = "websocket",
RemotePeer = "::1:60288",
TerminalIdentity = "xterm-256color",
WindowSize = (120, 40),
});
var setMaintenance = await admin.RunCommandAsync("settings set maintenance on --no-logo");
var showMaintenance = await operatorSession.RunCommandAsync("settings show maintenance --no-logo");
var increment1 = await admin.RunCommandAsync("counter inc --no-logo");
var increment2 = await operatorSession.RunCommandAsync("counter inc --no-logo");
var counterReadback = await admin.RunCommandAsync("counter get --no-logo");
var widget = await operatorSession.RunCommandAsync("widget show 2 --json --no-logo");
var import = await admin.RunCommandAsync("import --no-logo");
setMaintenance.ExitCode.Should().Be(0);
showMaintenance.OutputText.Should().Contain("on");
increment1.GetResult<int>().Should().Be(1);
increment2.GetResult<int>().Should().Be(2);
counterReadback.GetResult<int>().Should().Be(2);
widget.GetResult<SampleReplApp.Widget>().Name.Should().Be("Beta");
widget.OutputText.Should().Contain("Beta");
import.InteractionEvents
.OfType<ReplStatusEvent>()
.Should()
.ContainSingle(evt => string.Equals(evt.Text, "Import started", StringComparison.Ordinal));
var snapshots = await host.QuerySessionsAsync();
snapshots.Should().HaveCount(2);
var adminSnapshot = snapshots.Single(snapshot =>
string.Equals(snapshot.SessionId, admin.SessionId, StringComparison.Ordinal));
adminSnapshot.Transport.Should().Be("signalr");
adminSnapshot.Screen.Should().Be((140, 45));
}
}