-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRustMapsGenerationDriverTests.cs
More file actions
178 lines (150 loc) · 7.47 KB
/
Copy pathRustMapsGenerationDriverTests.cs
File metadata and controls
178 lines (150 loc) · 7.47 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
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using RustMapsApi.Results;
using RustMapsApi.V4;
using RustMapsApi.V4.Models;
using RustMapsApi.V4.Requests;
using RustPlusBot.Features.Map.RustMaps;
namespace RustPlusBot.Features.Map.Tests.RustMaps;
public sealed class RustMapsGenerationDriverTests
{
private static readonly RustMapsMapKey Key = new(4000, 12345);
private static readonly Guid Server = Guid.NewGuid();
private static RustMapsError Error(RustMapsErrorKind kind) => new(kind, null, null, null);
private static (RustMapsGenerationDriver Driver, IRustMapsClient Client, RustMapsMapCoordinator Coord) Build()
{
var client = Substitute.For<IRustMapsClient>();
var coord = new RustMapsMapCoordinator();
var driver = new RustMapsGenerationDriver(client, coord, NullLogger<RustMapsGenerationDriver>.Instance);
return (driver, client, coord);
}
[Fact]
public async Task Existing_map_goes_straight_to_ready_without_generating()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
client.GetMapBySeedAndSizeAsync(4000, 12345, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Success(
new MapInfo
{
ImageUrl = "https://img/x.png", Url = "https://rustmaps/x"
}, 200));
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal(RustMapsGenerationState.Ready, coord.Snapshot(Key).State);
Assert.Equal("https://img/x.png", coord.Snapshot(Key).Ready!.ImageUrl);
await client.DidNotReceiveWithAnyArgs().CreateMapAsync(default!, default);
}
[Fact]
public async Task Ready_prefers_the_icon_render_url_over_the_plain_terrain_one()
{
// ImageIconUrl (map_icons.png) carries the monument markers; ImageUrl (map_raw_normalized.png)
// is plain terrain. The driver must store the iconned render's URL.
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
client.GetMapBySeedAndSizeAsync(4000, 12345, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Success(
new MapInfo
{
ImageUrl = "https://img/plain.png",
ImageIconUrl = "https://img/icons.png",
Url = "https://rustmaps/x"
}, 200));
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal("https://img/icons.png", coord.Snapshot(Key).Ready!.ImageUrl);
}
[Fact]
public async Task NotFound_with_budget_generates_exactly_once_across_ticks()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
client.GetMapBySeedAndSizeAsync(4000, 12345, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Failure(Error(RustMapsErrorKind.NotFound), 404));
client.GetLimitsAsync(Arg.Any<string?>(), Arg.Any<CancellationToken>())
.Returns(Result<MapGenLimits>.Success(
new MapGenLimits
{
Concurrent = new(0, 5), Monthly = new(3, 100)
}, 200));
client.CreateMapAsync(Arg.Any<MapGenerationRequest>(), Arg.Any<CancellationToken>())
.Returns(Result<MapGenerationStatus>.Success(new MapGenerationStatus
{
MapId = "map-1"
}, 200));
await driver.AdvanceAsync(Key, CancellationToken.None); // Idle → Generating
await driver.AdvanceAsync(Key, CancellationToken.None); // Generating → poll (still not ready)
Assert.Equal(RustMapsGenerationState.Generating, coord.Snapshot(Key).State);
await client.Received(1).CreateMapAsync(
Arg.Is<MapGenerationRequest>(r => r!.Size == 4000 && r.Seed == 12345 && !r.Staging),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Transient_get_error_that_is_not_NotFound_never_spends_a_credit()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
client.GetMapBySeedAndSizeAsync(4000, 12345, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Failure(Error(RustMapsErrorKind.Transport), 503));
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal(RustMapsGenerationState.Idle, coord.Snapshot(Key).State);
await client.DidNotReceiveWithAnyArgs().GetLimitsAsync(default, default);
await client.DidNotReceiveWithAnyArgs().CreateMapAsync(default!, default);
}
[Fact]
public async Task Limit_reached_never_generates()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
client.GetMapBySeedAndSizeAsync(4000, 12345, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Failure(Error(RustMapsErrorKind.NotFound), 404));
client.GetLimitsAsync(Arg.Any<string?>(), Arg.Any<CancellationToken>())
.Returns(Result<MapGenLimits>.Success(
new MapGenLimits
{
Concurrent = new(5, 5), Monthly = new(3, 100)
}, 200)); // concurrent exhausted
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal(RustMapsGenerationState.LimitReached, coord.Snapshot(Key).State);
await client.DidNotReceiveWithAnyArgs().CreateMapAsync(default!, default);
}
[Fact]
public async Task Limits_call_failing_fails_closed_no_generate()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
client.GetMapBySeedAndSizeAsync(4000, 12345, false, Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Failure(Error(RustMapsErrorKind.NotFound), 404));
client.GetLimitsAsync(Arg.Any<string?>(), Arg.Any<CancellationToken>())
.Returns(Result<MapGenLimits>.Failure(Error(RustMapsErrorKind.Transport), 503));
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal(RustMapsGenerationState.Failed, coord.Snapshot(Key).State);
await client.DidNotReceiveWithAnyArgs().CreateMapAsync(default!, default);
}
[Fact]
public async Task Poll_reaches_ready_and_stores_the_image_url()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
coord.TrySetGenerating(Key, "map-1");
client.GetMapByIdAsync("map-1", Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Success(
new MapInfo
{
ImageUrl = "https://img/x.png", Url = "https://rustmaps/x"
}, 200));
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal(RustMapsGenerationState.Ready, coord.Snapshot(Key).State);
Assert.Equal("https://img/x.png", coord.Snapshot(Key).Ready!.ImageUrl);
}
[Fact]
public async Task Poll_still_generating_stays_generating_without_spending()
{
var (driver, client, coord) = Build();
coord.Register(Key, 1UL, Server);
coord.TrySetGenerating(Key, "map-1");
client.GetMapByIdAsync("map-1", Arg.Any<CancellationToken>())
.Returns(Result<MapInfo>.Failure(Error(RustMapsErrorKind.Queued), 409));
await driver.AdvanceAsync(Key, CancellationToken.None);
Assert.Equal(RustMapsGenerationState.Generating, coord.Snapshot(Key).State);
await client.DidNotReceiveWithAnyArgs().CreateMapAsync(default!, default);
}
}