-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathFoundryLocalManagerTest.cs
More file actions
715 lines (608 loc) · 29.4 KB
/
FoundryLocalManagerTest.cs
File metadata and controls
715 lines (608 loc) · 29.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.AI.Foundry.Local.Tests;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AI.Foundry.Local;
using RichardSzalay.MockHttp;
using Xunit;
public class FoundryLocalManagerTests : IDisposable
{
private readonly FoundryLocalManager _manager;
private readonly HttpClient _client;
private readonly MockHttpMessageHandler _mockHttp;
public FoundryLocalManagerTests()
{
_mockHttp = new MockHttpMessageHandler();
_client = _mockHttp.ToHttpClient();
_client.BaseAddress = new Uri("http://localhost:5272"); // matches python tests
_manager = new FoundryLocalManager();
typeof(FoundryLocalManager)
.GetField("_serviceUri", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(_manager, _client.BaseAddress);
typeof(FoundryLocalManager)
.GetField("_serviceClient", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(_manager, _client);
}
private static List<ModelInfo> BuildCatalog(bool includeCuda = true)
{
// Mirrors MOCK_CATALOG_DATA ordering and fields (Python tests)
var common = new
{
ProviderType = "AzureFoundry",
Version = "1",
ModelType = "ONNX",
PromptTemplate = (PromptTemplate?)null,
Publisher = "Microsoft",
Task = "chat-completion",
FileSizeMb = 10403L,
ModelSettings = new ModelSettings { Parameters = [] },
SupportsToolCalling = false,
License = "MIT",
LicenseDescription = "License…",
MaxOutputTokens = 1024L,
MinFLVersion = "1.0.0",
};
var list = new List<ModelInfo>
{
// model-1 generic-gpu, generic-cpu:2, generic-cpu:1
new()
{
ModelId = "model-1-generic-gpu:1",
DisplayName = "model-1-generic-gpu",
Uri = "azureml://registries/azureml/models/model-1-generic-gpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.GPU, ExecutionProvider = "WebGpuExecutionProvider" },
Alias = "model-1",
ParentModelUri = "azureml://registries/azureml/models/model-1/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
new()
{
ModelId = "model-1-generic-cpu:2",
DisplayName = "model-1-generic-cpu",
Uri = "azureml://registries/azureml/models/model-1-generic-cpu/versions/2",
Runtime = new Runtime { DeviceType = DeviceType.CPU, ExecutionProvider = "CPUExecutionProvider" },
Alias = "model-1",
ParentModelUri = "azureml://registries/azureml/models/model-1/versions/2",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
new()
{
ModelId = "model-1-generic-cpu:1",
DisplayName = "model-1-generic-cpu",
Uri = "azureml://registries/azureml/models/model-1-generic-cpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.CPU, ExecutionProvider = "CPUExecutionProvider" },
Alias = "model-1",
ParentModelUri = "azureml://registries/azureml/models/model-1/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
// model-2 npu:2, npu:1, generic-cpu:1
new()
{
ModelId = "model-2-npu:2",
DisplayName = "model-2-npu",
Uri = "azureml://registries/azureml/models/model-2-npu/versions/2",
Runtime = new Runtime { DeviceType = DeviceType.NPU, ExecutionProvider = "QNNExecutionProvider" },
Alias = "model-2",
ParentModelUri = "azureml://registries/azureml/models/model-2/versions/2",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
new()
{
ModelId = "model-2-npu:1",
DisplayName = "model-2-npu",
Uri = "azureml://registries/azureml/models/model-2-npu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.NPU, ExecutionProvider = "QNNExecutionProvider" },
Alias = "model-2",
ParentModelUri = "azureml://registries/azureml/models/model-2/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
new()
{
ModelId = "model-2-generic-cpu:1",
DisplayName = "model-2-generic-cpu",
Uri = "azureml://registries/azureml/models/model-2-generic-cpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.CPU, ExecutionProvider = "CPUExecutionProvider" },
Alias = "model-2",
ParentModelUri = "azureml://registries/azureml/models/model-2/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
};
// model-3 cuda-gpu (optional), generic-gpu, generic-cpu
if (includeCuda)
{
list.Add(new ModelInfo
{
ModelId = "model-3-cuda-gpu:1",
DisplayName = "model-3-cuda-gpu",
Uri = "azureml://registries/azureml/models/model-3-cuda-gpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.GPU, ExecutionProvider = "CUDAExecutionProvider" },
Alias = "model-3",
ParentModelUri = "azureml://registries/azureml/models/model-3/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
});
}
list.AddRange(new[]
{
new ModelInfo
{
ModelId = "model-3-generic-gpu:1",
DisplayName = "model-3-generic-gpu",
Uri = "azureml://registries/azureml/models/model-3-generic-gpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.GPU, ExecutionProvider = "WebGpuExecutionProvider" },
Alias = "model-3",
ParentModelUri = "azureml://registries/azureml/models/model-3/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
},
new ModelInfo
{
ModelId = "model-3-generic-cpu:1",
DisplayName = "model-3-generic-cpu",
Uri = "azureml://registries/azureml/models/model-3-generic-cpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.CPU, ExecutionProvider = "CPUExecutionProvider" },
Alias = "model-3",
ParentModelUri = "azureml://registries/azureml/models/model-3/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = common.PromptTemplate, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
}
});
// model-4 generic-gpu (nullable prompt)
list.Add(new ModelInfo
{
ModelId = "model-4-generic-gpu:1",
DisplayName = "model-4-generic-gpu",
Uri = "azureml://registries/azureml/models/model-4-generic-gpu/versions/1",
Runtime = new Runtime { DeviceType = DeviceType.GPU, ExecutionProvider = "WebGpuExecutionProvider" },
Alias = "model-4",
ParentModelUri = "azureml://registries/azureml/models/model-4/versions/1",
ProviderType = common.ProviderType, Version = common.Version, ModelType = common.ModelType,
PromptTemplate = null, Publisher = common.Publisher, Task = common.Task,
FileSizeMb = common.FileSizeMb, ModelSettings = common.ModelSettings,
SupportsToolCalling = common.SupportsToolCalling, License = common.License,
LicenseDescription = common.LicenseDescription, MaxOutputTokens = common.MaxOutputTokens,
MinFLVersion = common.MinFLVersion
});
return list;
}
private void MockCatalog(bool includeCuda = true)
{
var payload = JsonSerializer.Serialize(BuildCatalog(includeCuda), ModelGenerationContext.Default.ListModelInfo);
_mockHttp.When(HttpMethod.Get, "/foundry/list").Respond("application/json", payload);
}
private void MockLocalModels(params string[] ids)
{
var json = JsonSerializer.Serialize(ids ?? Array.Empty<string>());
_mockHttp.When(HttpMethod.Get, "/openai/models").Respond("application/json", json);
}
private void MockLoadedModels(params string[] ids)
{
var json = JsonSerializer.Serialize(ids ?? Array.Empty<string>());
_mockHttp.When(HttpMethod.Get, "/openai/loadedmodels").Respond("application/json", json);
}
[Fact]
public async Task ListCatalogModelsAsync_ReturnsModels_AndSetsCudaOverrideWhenCudaPresent()
{
// GIVEN
MockCatalog(includeCuda: true);
// WHEN
var models = await _manager.ListCatalogModelsAsync();
// THEN
Assert.NotEmpty(models);
// Presence of any CUDAExecutionProvider should mark generic-gpu EpOverride="cuda"
Assert.Contains(models, m => m.Runtime.ExecutionProvider == "CUDAExecutionProvider");
var gg = models.Find(m => m.ModelId == "model-4-generic-gpu:1");
Assert.NotNull(gg);
Assert.Equal("cuda", gg!.EpOverride);
// cache is used on second call
var again = await _manager.ListCatalogModelsAsync();
Assert.Same(models, again);
}
[Fact]
public void RefreshCatalog_ResetsOnlyListCache()
{
// GIVEN
typeof(FoundryLocalManager)
.GetField("_catalogModels", BindingFlags.NonPublic | BindingFlags.Instance)!
.SetValue(_manager, new List<ModelInfo>());
// WHEN
_manager.RefreshCatalog();
// THEN
var models = typeof(FoundryLocalManager)
.GetField("_catalogModels", BindingFlags.NonPublic | BindingFlags.Instance)!
.GetValue(_manager);
Assert.Null(models);
}
[Fact]
public async Task GetModelInfoAsync_IdWithVersion_IdWithoutVersion_AliasAndDeviceFilter()
{
// GIVEN
MockCatalog(includeCuda: true);
// WHEN/THEN
// unknown
Assert.Null(await _manager.GetModelInfoAsync("unknown-model"));
// exact id (with version)
var m1v1 = await _manager.GetModelInfoAsync("model-1-generic-cpu:1");
Assert.Equal("model-1-generic-cpu:1", m1v1!.ModelId);
// id without version -> latest version among same prefix
var m1latest = await _manager.GetModelInfoAsync("model-1-generic-cpu");
Assert.Equal("model-1-generic-cpu:2", m1latest!.ModelId);
// alias selection (depends on service order); our list puts "model-2-npu:2" first for alias model-2
var a2 = await _manager.GetModelInfoAsync("model-2");
Assert.Equal("model-2-npu:2", a2!.ModelId);
// model-3 should prefer CUDA when present
var a3 = await _manager.GetModelInfoAsync("model-3");
Assert.Equal("model-3-cuda-gpu:1", a3!.ModelId);
// device filter
var a1gpu = await _manager.GetModelInfoAsync("model-1", DeviceType.GPU);
Assert.Equal("model-1-generic-gpu:1", a1gpu!.ModelId);
var a1cpu = await _manager.GetModelInfoAsync("model-1", DeviceType.CPU);
Assert.Equal("model-1-generic-cpu:2", a1cpu!.ModelId);
var a1npu = await _manager.GetModelInfoAsync("model-1", DeviceType.NPU);
Assert.Null(a1npu);
}
[Fact]
public async Task ListCachedModelsAsync_ReturnsMatchingInfos()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels("model-2-npu:1", "model-4-generic-gpu:1");
// WHEN
var local = await _manager.ListCachedModelsAsync();
// THEN
Assert.Equal(2, local.Count);
Assert.Equal("model-2-npu:1", local[0].ModelId);
Assert.Equal("model-4-generic-gpu:1", local[1].ModelId);
}
[Fact]
public async Task ListLoadedModelsAsync_ReturnsModelInfoList()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLoadedModels("model-2-npu:1");
// WHEN
var loaded = await _manager.ListLoadedModelsAsync();
// THEN
var m = Assert.Single(loaded);
Assert.Equal("model-2-npu:1", m.ModelId);
}
[Fact]
public async Task ListLoadedModelsAsync_Throws_WhenNullResponse()
{
// GIVEN
_mockHttp.When(HttpMethod.Get, "/openai/loadedmodels")
.Respond("application/json", "null");
// WHEN/THEN
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.ListLoadedModelsAsync());
Assert.Equal("Failed to read loaded models.", ex.Message);
}
[Fact]
public async Task DownloadModelAsync_Success_ParsesTailJson()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels(); // empty cache
var mockJsonResponse = "log... {\"success\": true, \"errorMessage\": null}";
_mockHttp.When("/openai/download").Respond("application/json", mockJsonResponse);
// WHEN
var result = await _manager.DownloadModelAsync("model-3"); // resolves to cuda-gpu:1
// THEN
Assert.NotNull(result);
Assert.Equal("model-3-cuda-gpu:1", result!.ModelId);
}
[Fact]
public async Task DownloadModelAsync_ThrowsWhenServiceReturnsFailure()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels();
var fail = "tail {\"success\": false, \"errorMessage\": \"nope\"}";
_mockHttp.When("/openai/download").Respond("application/json", fail);
// WHEN/THEN
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.DownloadModelAsync("model-1"));
Assert.Contains("Failed to download model: nope", ex.Message);
}
[Fact]
public async Task DownloadModelAsync_SkipsWhenAlreadyCachedUnlessForce()
{
// GIVEN
MockCatalog(includeCuda: true);
// Latest version already in cache → should skip POST unless force==true
MockLocalModels("model-2-npu:2");
// WHEN: already cached and not forced → no POST to /openai/download
var cached = await _manager.DownloadModelAsync("model-2");
Assert.NotNull(cached);
Assert.Equal("model-2-npu:2", cached!.ModelId);
// AND WHEN: force download → we should POST and parse a JSON body that includes errorMessage
_mockHttp.When(HttpMethod.Post, "/openai/download")
.Respond("application/json", "{\"success\": true, \"errorMessage\": null}");
var forced = await _manager.DownloadModelAsync("model-2", device: null, token: null, force: true);
Assert.NotNull(forced);
Assert.Equal("model-2-npu:2", forced!.ModelId);
// No expectations were set, but this is harmless and keeps parity with other tests
_mockHttp.VerifyNoOutstandingExpectation();
}
[Fact]
public async Task DownloadModelWithProgressAsync_Success()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels(); // empty to force a download
var stream = new MemoryStream();
var progressLine = Encoding.UTF8.GetBytes("Total 0.00% Downloading model.onnx.data\n");
stream.Write(progressLine, 0, progressLine.Length);
var doneLine = Encoding.UTF8.GetBytes("[DONE] All Completed!\n");
stream.Write(doneLine, 0, doneLine.Length);
using (var writer = new Utf8JsonWriter(stream))
{
JsonSerializer.Serialize(writer, new { success = true, errorMessage = (string?)null });
}
stream.Position = 0;
_mockHttp.When("/openai/download").Respond("application/json", stream);
// WHEN
var progressList = new List<ModelDownloadProgress>();
await foreach (var p in _manager.DownloadModelWithProgressAsync("model-3"))
{
progressList.Add(p);
}
// THEN
Assert.Equal(2, progressList.Count);
Assert.False(progressList[0].IsCompleted);
Assert.Equal(0, progressList[0].Percentage);
Assert.True(progressList[1].IsCompleted);
Assert.Equal(100, progressList[1].Percentage);
Assert.Equal("model-3-cuda-gpu:1", progressList[1].ModelInfo!.ModelId);
}
[Fact]
public async Task DownloadModelWithProgressAsync_Error()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels();
var stream = new MemoryStream();
var doneLine = Encoding.UTF8.GetBytes("[DONE] All Completed!\n");
stream.Write(doneLine, 0, doneLine.Length);
using (var writer = new Utf8JsonWriter(stream))
{
JsonSerializer.Serialize(writer, new { success = false, errorMessage = "Download error occurred." });
}
stream.Position = 0;
_mockHttp.When("/openai/download").Respond("application/json", stream);
// WHEN
var progressList = new List<ModelDownloadProgress>();
await foreach (var p in _manager.DownloadModelWithProgressAsync("model-3"))
{
progressList.Add(p);
}
// THEN
var last = Assert.Single(progressList);
Assert.True(last.IsCompleted);
Assert.Equal("Download error occurred.", last.ErrorMessage);
}
[Fact]
public async Task DownloadModelWithProgressAsync_CancellationToken_ThrowsOperationCanceledException()
{
// GIVEN - Simulate an in-progress download that will be cancelled mid-stream
MockCatalog(includeCuda: true);
MockLocalModels(); // empty to force a download
var stream = new MemoryStream();
// Simulate partial download progress (NOT complete)
// This mimics a real download that's in progress when user clicks cancel
for (int i = 0; i <= 50; i += 10)
{
var progressLine = Encoding.UTF8.GetBytes($"Total {i}.00% Downloading model.onnx.data\n");
stream.Write(progressLine, 0, progressLine.Length);
}
// DO NOT add [DONE] or completion JSON - this simulates incomplete download
// The stream will end without completion, which is realistic for cancelled operations
stream.Position = 0;
_mockHttp.When("/openai/download").Respond("application/json", stream);
// WHEN - User cancels after receiving some progress updates
using var cts = new System.Threading.CancellationTokenSource();
var progressList = new List<ModelDownloadProgress>();
// THEN
await Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
await foreach (var p in _manager.DownloadModelWithProgressAsync("model-3", ct: cts.Token))
{
progressList.Add(p);
// Cancel after receiving 2 progress updates (simulating user clicking cancel button)
// Using 2 instead of 3 to ensure we cancel before all mock data is consumed
if (progressList.Count >= 2)
{
cts.Cancel();
}
}
});
// Verify cancellation was responsive and happened during download
Assert.InRange(progressList.Count, 2, 4); // Allow for 1-2 items in async pipeline
Assert.All(progressList, p => Assert.False(p.IsCompleted, "Should not reach completion status"));
Assert.All(progressList, p => Assert.True(p.Percentage < 100, $"Progress {p.Percentage}% should be less than 100%"));
}
[Fact]
public async Task DownloadModelWithProgressAsync_AlreadyCancelledToken_ThrowsImmediately()
{
// GIVEN - Token is already cancelled before download starts
MockCatalog(includeCuda: true);
MockLocalModels(); // empty to force a download
var stream = new MemoryStream();
var progressLine = Encoding.UTF8.GetBytes("Total 10.00% Downloading model.onnx.data\n");
stream.Write(progressLine, 0, progressLine.Length);
stream.Position = 0;
_mockHttp.When("/openai/download").Respond("application/json", stream);
using var cts = new System.Threading.CancellationTokenSource();
cts.Cancel(); // Cancel BEFORE starting download
var progressList = new List<ModelDownloadProgress>();
// WHEN/THEN - Should throw immediately without processing any progress
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
await foreach (var p in _manager.DownloadModelWithProgressAsync("model-3", ct: cts.Token))
{
progressList.Add(p);
}
});
// Should not receive any progress updates since token was already cancelled
Assert.Empty(progressList);
}
[Fact]
public async Task LoadModelAsync_Succeeds_AndPassesEpOverrideWhenCudaPresent()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels("model-4-generic-gpu:1"); // in cache
// After ListCatalogModelsAsync runs, EpOverride for generic-gpu will be "cuda"
// First call ensures the override is applied
await _manager.ListCatalogModelsAsync();
_mockHttp
.When(HttpMethod.Get, "http://localhost:5272/openai/load/model-4-generic-gpu:1*")
.Respond("application/json", "{}");
// WHEN
var result = await _manager.LoadModelAsync("model-4");
// THEN
Assert.Equal("model-4-generic-gpu:1", result.ModelId);
}
[Fact]
public async Task LoadModelAsync_ThrowsIfNotInCache()
{
// GIVEN
MockCatalog(includeCuda: true);
MockLocalModels(); // empty
// WHEN/THEN
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.LoadModelAsync("model-3"));
Assert.Contains("not found in local models", ex.Message);
}
[Fact]
public async Task UnloadModelAsync_CallsCorrectUri()
{
// GIVEN
MockCatalog(includeCuda: true);
var model = "model-2-npu:1";
_mockHttp.When("/openai/unload/model-2-npu:1")
.WithQueryString("force=false")
.Respond(HttpStatusCode.OK);
// WHEN
await _manager.UnloadModelAsync(model);
// THEN just no exception
Assert.True(true);
}
[Fact]
public async Task IsModelUpgradeableAsync_ReturnsTrue_WhenCachedOlderThanLatest()
{
MockCatalog(includeCuda: true);
MockLocalModels("model-2-npu:1"); // older
Assert.True(await _manager.IsModelUpgradeableAsync("model-2"));
}
[Fact]
public async Task IsModelUpgradeableAsync_ReturnsFalse_WhenLatestVersionCached()
{
MockCatalog(includeCuda: true);
MockLocalModels("model-2-npu:2"); // latest
Assert.False(await _manager.IsModelUpgradeableAsync("model-2"));
}
[Fact]
public async Task IsModelUpgradeableAsync_ReturnsFalse_WhenModelMissingFromCatalog()
{
// empty catalog for this case
_mockHttp.When(HttpMethod.Get, "/foundry/list").Respond("application/json", "[]");
// cached state doesn’t matter when it’s missing from catalog
MockLocalModels("model-2-npu:1");
Assert.False(await _manager.IsModelUpgradeableAsync("model-2"));
}
[Fact]
public async Task UpgradeModelAsync_HappyPath()
{
MockCatalog(includeCuda: true);
MockLocalModels(); // empty -> forces download
_mockHttp.When(HttpMethod.Post, "/openai/download")
.Respond("application/json", "{\"success\": true, \"errorMessage\": null}");
var upgraded = await _manager.UpgradeModelAsync("model-3");
Assert.Equal("model-3-cuda-gpu:1", upgraded!.ModelId);
}
[Fact]
public async Task UpgradeModelAsync_Throws_WhenDownloadFails()
{
MockCatalog(includeCuda: true);
MockLocalModels(); // empty -> forces download
_mockHttp.When(HttpMethod.Post, "/openai/download")
.Respond("application/json", "{\"success\": false, \"errorMessage\": \"Simulated download failure.\"}");
await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.UpgradeModelAsync("model-3"));
}
[Fact]
public async Task UpgradeModelAsync_ThrowsWhenModelNotFound()
{
// GIVEN
_mockHttp.When(HttpMethod.Get, "/foundry/list").Respond("application/json", "[]");
// WHEN/THEN
var ex = await Assert.ThrowsAsync<ArgumentException>(() => _manager.UpgradeModelAsync("missing-model"));
Assert.Contains("not found", ex.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void Dispose_DisposesHttpClient()
{
_manager.Dispose();
Assert.True(true);
}
[Fact]
public async Task DisposeAsync_DisposesHttpClient()
{
await _manager.DisposeAsync();
Assert.True(true);
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
}