-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateServiceTests.cs
More file actions
340 lines (281 loc) · 12.4 KB
/
Copy pathUpdateServiceTests.cs
File metadata and controls
340 lines (281 loc) · 12.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using SwitchifyPc.Core.Updates;
namespace SwitchifyPc.Tests;
public sealed class UpdateServiceTests
{
private static readonly DateTimeOffset FixedNow = DateTimeOffset.Parse("2026-06-29T12:00:00Z");
[Fact]
public async Task CheckFailsInUnpackagedBuildsWithoutCallingBackend()
{
FakeUpdateBackend backend = new();
UpdateService service = CreateService(backend, isPackaged: false);
UpdateState state = await service.CheckForUpdatesAsync();
Assert.Equal(UpdateCheckStatus.CheckFailed, state.Info.Status);
Assert.Equal(UpdateFailureReason.NotPackaged, state.Info.Reason);
Assert.Equal(0, backend.CheckCalls);
}
[Fact]
public async Task CheckReportsAvailableUpdate()
{
FakeUpdateBackend backend = new()
{
CheckOutcome = UpdateCheckOutcome.Available(new AvailableUpdate("0.2.0", "Switchify PC 0.2.0", "Native app."))
};
List<UpdateState> changes = [];
UpdateService service = CreateService(backend, onStateChanged: changes.Add);
UpdateState state = await service.CheckForUpdatesAsync();
Assert.Equal(UpdateCheckStatus.UpdateAvailable, state.Info.Status);
Assert.Equal("0.2.0", state.Info.LatestVersion);
Assert.Equal("Switchify PC 0.2.0", state.Info.ReleaseName);
Assert.Equal("Native app.", state.Info.ReleaseNotes);
Assert.Equal(FixedNow, state.Info.CheckedAt);
Assert.Contains(changes, item => item.Info.Status == UpdateCheckStatus.Checking);
Assert.Contains(changes, item => item.Info.Status == UpdateCheckStatus.UpdateAvailable);
}
[Fact]
public async Task DownloadRequiresAvailableUpdate()
{
FakeUpdateBackend backend = new();
UpdateService service = CreateService(backend);
UpdateState state = await service.DownloadUpdateAsync();
Assert.Equal(UpdateDownloadStatus.DownloadFailed, state.Download.Status);
Assert.Equal(UpdateFailureReason.NotAvailable, state.Download.Reason);
Assert.Equal(0, backend.DownloadCalls);
}
[Fact]
public async Task DownloadCapturesInstallerPathAndProgress()
{
FakeUpdateBackend backend = new()
{
CheckOutcome = UpdateCheckOutcome.Available(new AvailableUpdate("0.2.0", null, null)),
DownloadOutcome = UpdateDownloadOutcome.Downloaded(@"C:\cache\Switchify-PC-Setup-0.2.0-x64.exe")
};
List<UpdateState> changes = [];
UpdateService service = CreateService(backend, onStateChanged: changes.Add);
await service.CheckForUpdatesAsync();
UpdateState state = await service.DownloadUpdateAsync();
Assert.Equal(UpdateDownloadStatus.Downloaded, state.Download.Status);
Assert.Equal(100, state.Download.Percent);
Assert.Contains(changes, item => item.Download.Status == UpdateDownloadStatus.Downloading && item.Download.Percent == 40);
}
[Fact]
public async Task InstallBeforeDownloadFails()
{
FakeInstallerLauncher launcher = new();
UpdateService service = CreateService(new FakeUpdateBackend(), launcher: launcher);
UpdateInstallResult result = await service.InstallDownloadedUpdateAsync();
Assert.False(result.Ok);
Assert.Equal(UpdateInstallFailureReason.NotDownloaded, result.Reason);
Assert.Equal(0, launcher.LaunchCalls);
}
[Fact]
public async Task InstallOpensDownloadedInstallerAndKeepsServiceState()
{
FakeUpdateBackend backend = new()
{
CheckOutcome = UpdateCheckOutcome.Available(new AvailableUpdate("0.2.0", null, null)),
DownloadOutcome = UpdateDownloadOutcome.Downloaded(@"C:\cache\Switchify-PC-Setup-0.2.0-x64.exe")
};
FakeInstallerLauncher launcher = new();
UpdateService service = CreateService(backend, launcher: launcher);
await service.CheckForUpdatesAsync();
await service.DownloadUpdateAsync();
UpdateInstallResult result = await service.InstallDownloadedUpdateAsync();
Assert.True(result.Ok);
Assert.Equal(1, launcher.LaunchCalls);
Assert.Equal(@"C:\cache\Switchify-PC-Setup-0.2.0-x64.exe", launcher.LastInstallerPath);
Assert.Equal(UpdateDownloadStatus.Downloaded, service.GetState().Download.Status);
}
[Fact]
public async Task InstallReturnsLauncherFailure()
{
FakeUpdateBackend backend = new()
{
CheckOutcome = UpdateCheckOutcome.Available(new AvailableUpdate("0.2.0", null, null)),
DownloadOutcome = UpdateDownloadOutcome.Downloaded(@"C:\cache\Switchify-PC-Setup-0.2.0-x64.exe")
};
FakeInstallerLauncher launcher = new()
{
Result = UpdateInstallerLaunchResult.Failure(UpdateInstallFailureReason.InstallerLaunchFailed)
};
UpdateService service = CreateService(backend, launcher: launcher);
await service.CheckForUpdatesAsync();
await service.DownloadUpdateAsync();
UpdateInstallResult result = await service.InstallDownloadedUpdateAsync();
Assert.False(result.Ok);
Assert.Equal(UpdateInstallFailureReason.InstallerLaunchFailed, result.Reason);
}
[Fact]
public async Task AutomaticPollingRunsInitialAndHourlyChecks()
{
FakeUpdateBackend backend = new();
FakeUpdateScheduler scheduler = new();
UpdateService service = CreateService(backend, scheduler: scheduler);
service.StartAutomaticUpdateChecks();
ScheduledCallback initial = Assert.Single(scheduler.OneShot);
ScheduledCallback recurring = Assert.Single(scheduler.Recurring);
await initial.RunAsync();
await recurring.RunAsync();
Assert.Equal(2, backend.CheckCalls);
Assert.Equal(UpdateService.InitialPollDelay, initial.Delay);
Assert.Equal(UpdateService.PollInterval, recurring.Delay);
}
[Fact]
public void AutomaticPollingDoesNotStartDuplicateTimers()
{
FakeUpdateScheduler scheduler = new();
UpdateService service = CreateService(new FakeUpdateBackend(), scheduler: scheduler);
service.StartAutomaticUpdateChecks();
service.StartAutomaticUpdateChecks();
Assert.Single(scheduler.OneShot);
Assert.Single(scheduler.Recurring);
}
[Fact]
public async Task StopAutomaticPollingDisposesScheduledChecks()
{
FakeUpdateBackend backend = new();
FakeUpdateScheduler scheduler = new();
UpdateService service = CreateService(backend, scheduler: scheduler);
service.StartAutomaticUpdateChecks();
service.StopAutomaticUpdateChecks();
await scheduler.OneShot[0].RunAsync();
await scheduler.Recurring[0].RunAsync();
Assert.Equal(0, backend.CheckCalls);
}
[Fact]
public async Task AutomaticPollingSkipsOverlappingChecks()
{
FakeUpdateBackend backend = new() { CompleteChecksManually = true };
FakeUpdateScheduler scheduler = new();
UpdateService service = CreateService(backend, scheduler: scheduler);
service.StartAutomaticUpdateChecks();
Task firstCheck = scheduler.OneShot[0].RunAsync();
await backend.WaitForCheckStartAsync();
await scheduler.Recurring[0].RunAsync();
backend.CompletePendingCheck(UpdateCheckOutcome.UpToDate());
await firstCheck;
Assert.Equal(1, backend.CheckCalls);
}
[Fact]
public void AutomaticPollingDoesNotStartWhenUnsupported()
{
FakeUpdateScheduler scheduler = new();
UpdateService service = CreateService(new FakeUpdateBackend(), scheduler: scheduler, platform: "darwin");
service.StartAutomaticUpdateChecks();
Assert.Empty(scheduler.OneShot);
Assert.Empty(scheduler.Recurring);
}
[Fact]
public async Task AutomaticPollingSkipsWhenUpdateIsDownloaded()
{
FakeUpdateBackend backend = new()
{
CheckOutcome = UpdateCheckOutcome.Available(new AvailableUpdate("0.2.0", null, null)),
DownloadOutcome = UpdateDownloadOutcome.Downloaded(@"C:\cache\Switchify-PC-Setup-0.2.0-x64.exe")
};
FakeUpdateScheduler scheduler = new();
UpdateService service = CreateService(backend, scheduler: scheduler);
await service.CheckForUpdatesAsync();
await service.DownloadUpdateAsync();
service.StartAutomaticUpdateChecks();
await scheduler.Recurring[0].RunAsync();
Assert.Equal(1, backend.CheckCalls);
}
private static UpdateService CreateService(
FakeUpdateBackend backend,
FakeInstallerLauncher? launcher = null,
FakeUpdateScheduler? scheduler = null,
bool isPackaged = true,
string platform = "win32",
Action<UpdateState>? onStateChanged = null)
{
return new UpdateService(new UpdateServiceOptions(
CurrentVersion: "0.1.20",
IsPackaged: isPackaged,
Platform: platform,
Backend: backend,
InstallerLauncher: launcher ?? new FakeInstallerLauncher(),
Scheduler: scheduler,
Now: () => FixedNow,
OnStateChanged: onStateChanged));
}
private sealed class FakeUpdateBackend : IUpdateBackend
{
public int CheckCalls { get; private set; }
public int DownloadCalls { get; private set; }
public UpdateCheckOutcome CheckOutcome { get; init; } = UpdateCheckOutcome.UpToDate();
public UpdateDownloadOutcome DownloadOutcome { get; init; } = UpdateDownloadOutcome.Downloaded(@"C:\cache\setup.exe");
public bool CompleteChecksManually { get; init; }
private TaskCompletionSource<UpdateCheckOutcome>? pendingCheck;
private readonly TaskCompletionSource checkStarted = new(TaskCreationOptions.RunContinuationsAsynchronously);
public Task<UpdateCheckOutcome> CheckForUpdatesAsync(string currentVersion, CancellationToken cancellationToken = default)
{
CheckCalls++;
checkStarted.TrySetResult();
if (CompleteChecksManually)
{
pendingCheck = new TaskCompletionSource<UpdateCheckOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
return pendingCheck.Task;
}
return Task.FromResult(CheckOutcome);
}
public Task WaitForCheckStartAsync()
{
return checkStarted.Task;
}
public void CompletePendingCheck(UpdateCheckOutcome outcome)
{
pendingCheck?.SetResult(outcome);
}
public Task<UpdateDownloadOutcome> DownloadUpdateAsync(
AvailableUpdate update,
IProgress<UpdateDownloadSnapshot> progress,
CancellationToken cancellationToken = default)
{
DownloadCalls++;
progress.Report(new UpdateDownloadSnapshot(40, 100, 40));
return Task.FromResult(DownloadOutcome);
}
}
private sealed class FakeInstallerLauncher : IUpdateInstallerLauncher
{
public int LaunchCalls { get; private set; }
public string? LastInstallerPath { get; private set; }
public UpdateInstallerLaunchResult Result { get; init; } = UpdateInstallerLaunchResult.Success();
public Task<UpdateInstallerLaunchResult> LaunchAsync(string? installerPath, CancellationToken cancellationToken = default)
{
LaunchCalls++;
LastInstallerPath = installerPath;
return Task.FromResult(Result);
}
}
private sealed class FakeUpdateScheduler : IUpdatePollScheduler
{
public List<ScheduledCallback> OneShot { get; } = [];
public List<ScheduledCallback> Recurring { get; } = [];
public IDisposable ScheduleOnce(TimeSpan delay, Func<Task> callback)
{
ScheduledCallback scheduled = new(delay, callback);
OneShot.Add(scheduled);
return scheduled;
}
public IDisposable ScheduleRecurring(TimeSpan interval, Func<Task> callback)
{
ScheduledCallback scheduled = new(interval, callback);
Recurring.Add(scheduled);
return scheduled;
}
}
private sealed class ScheduledCallback(TimeSpan delay, Func<Task> callback) : IDisposable
{
private bool disposed;
public TimeSpan Delay { get; } = delay;
public Task RunAsync()
{
return disposed ? Task.CompletedTask : callback();
}
public void Dispose()
{
disposed = true;
}
}
}