-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathDifferentialUpgradeIntegrationTests.cs
More file actions
833 lines (691 loc) · 34 KB
/
Copy pathDifferentialUpgradeIntegrationTests.cs
File metadata and controls
833 lines (691 loc) · 34 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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using GeneralUpdate.Core.FileSystem;
using GeneralUpdate.Core.JsonContext;
using GeneralUpdate.Differential;
using GeneralUpdate.Differential.Abstractions;
using GeneralUpdate.Differential.Binary;
using GeneralUpdate.Differential.Matchers;
using GeneralUpdate.Differential.Models;
using GeneralUpdate.Differential.Pipeline;
using Xunit;
using Xunit.Abstractions;
namespace DifferentialTest
{
/// <summary>
/// Comprehensive differential upgrade integration tests.
/// Covers:
/// - Client — Upgrade mesh update: generate patches in client context, apply in upgrade context
/// - All file operations: modified, added, deleted, unchanged, binary
/// - Complex directory structures
/// - Push upgrade simulation via differential pipeline
/// - Various parameter combinations (parallel, serial, cancellation, progress)
/// - Real-world developer usage scenarios
/// </summary>
public class DifferentialUpgradeIntegrationTests : IDisposable
{
private readonly string _testDir;
private readonly ITestOutputHelper _output;
public DifferentialUpgradeIntegrationTests(ITestOutputHelper output)
{
_testDir = Path.Combine(Path.GetTempPath(), $"GU_DiffIntegration_{Guid.NewGuid()}");
_output = output;
Directory.CreateDirectory(_testDir);
}
public void Dispose()
{
try { Directory.Delete(_testDir, true); } catch { /* ignore */ }
}
#region Clean — Dirty Full Cycle (Client — Upgrade Mesh Update)
/// <summary>
/// Scenario: Client generates patches (Clean), Upgrade applies them (Dirty).
/// This is the core differential upgrade flow.
/// </summary>
[Fact]
public async Task CleanAndDirty_FullMeshUpdate_CorrectlyUpdatesApp()
{
// Arrange — emulate client-side source (current version) and target (new version)
var sourceDir = Path.Combine(_testDir, "source_v1.0.0");
var targetDir = Path.Combine(_testDir, "target_v2.0.0");
var patchDir = Path.Combine(_testDir, "patch");
var appDir = Path.Combine(_testDir, "app");
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(targetDir);
Directory.CreateDirectory(patchDir);
Directory.CreateDirectory(appDir);
// Current version files (v1.0.0)
File.WriteAllText(Path.Combine(sourceDir, "config.json"), @"{""version"":""1.0.0"",""theme"":""dark""}");
File.WriteAllText(Path.Combine(sourceDir, "main.dll"), "DLL content v1.0.0");
File.WriteAllText(Path.Combine(sourceDir, "readme.txt"), "README v1.0.0");
// New version files (v2.0.0) — config modified, readme deleted, new file added
File.WriteAllText(Path.Combine(targetDir, "config.json"), @"{""version"":""2.0.0"",""theme"":""light"",""newFeature"":true}");
File.WriteAllText(Path.Combine(targetDir, "main.dll"), "DLL content v2.0.0");
File.WriteAllText(Path.Combine(targetDir, "whatsnew.txt"), "What's New in v2.0.0!");
// Copy source to appDir (simulate the current installed application)
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(appDir, Path.GetFileName(file)));
// Act — Step 1: Client generates patches (Clean)
await DifferentialCore.Clean(sourceDir, targetDir, patchDir);
// Assert — patches generated
// config.json.patch generation depends on differ; verified by Dirty assertions below
Assert.True(File.Exists(Path.Combine(patchDir, "main.dll.patch")));
Assert.True(File.Exists(Path.Combine(patchDir, "whatsnew.txt"))); // new file copied directly
// Verify delete list
var deleteListFile = Path.Combine(patchDir, "generalupdate_delete_files.json");
Assert.True(File.Exists(deleteListFile), "Delete list should be generated for removed files");
var deleteList = JsonSerializer.Deserialize<List<FileNode>>(
File.ReadAllText(deleteListFile),
FileNodesJsonContext.Default.ListFileNode);
Assert.NotNull(deleteList);
Assert.Contains(deleteList, f => f.Name == "readme.txt");
// Act — Step 2: Upgrade applies patches (Dirty)
await DifferentialCore.Dirty(appDir, patchDir);
// Assert - core files should still exist after Dirty
Assert.True(File.Exists(Path.Combine(appDir, "config.json")), "config.json should exist after Dirty");
Assert.True(File.Exists(Path.Combine(appDir, "main.dll")), "main.dll should still exist after Dirty");
Assert.True(File.Exists(Path.Combine(appDir, "whatsnew.txt")), "whatsnew.txt should still exist after Dirty");
Assert.False(File.Exists(Path.Combine(appDir, "readme.txt")), "Deleted file should be removed");
}
#endregion
#region Binary File Differential Scenarios
/// <summary>
/// Tests differential upgrade with binary files (EXE, DLL, image assets).
/// Common scenario for application updates.
/// </summary>
[Fact]
public async Task CleanAndDirty_BinaryFiles_ProducesCorrectResult()
{
// Arrange
var sourceDir = Path.Combine(_testDir, "source_bin");
var targetDir = Path.Combine(_testDir, "target_bin");
var patchDir = Path.Combine(_testDir, "patch_bin");
var appDir = Path.Combine(_testDir, "app_bin");
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(targetDir);
Directory.CreateDirectory(patchDir);
Directory.CreateDirectory(appDir);
// Simulate binary files
var sourceBinary = new byte[4096];
var targetBinary = new byte[4096];
new Random(42).NextBytes(sourceBinary);
Array.Copy(sourceBinary, targetBinary, 4096);
// Modify a region in the middle
for (var i = 1024; i < 2048; i++)
targetBinary[i] = (byte)(sourceBinary[i] ^ 0xFF);
File.WriteAllBytes(Path.Combine(sourceDir, "app.exe"), sourceBinary);
File.WriteAllBytes(Path.Combine(targetDir, "app.exe"), targetBinary);
// Also add an unchanged binary
File.WriteAllBytes(Path.Combine(sourceDir, "icon.png"), new byte[] { 0x89, 0x50, 0x4E, 0x47 });
File.WriteAllBytes(Path.Combine(targetDir, "icon.png"), new byte[] { 0x89, 0x50, 0x4E, 0x47 });
// Copy source to app
File.Copy(Path.Combine(sourceDir, "app.exe"), Path.Combine(appDir, "app.exe"));
File.Copy(Path.Combine(sourceDir, "icon.png"), Path.Combine(appDir, "icon.png"));
// Act — generate and apply patches
await DifferentialCore.Clean(sourceDir, targetDir, patchDir);
await DifferentialCore.Dirty(appDir, patchDir);
// Assert
var resultBinary = File.ReadAllBytes(Path.Combine(appDir, "app.exe"));
Assert.Equal(targetBinary, resultBinary);
// Unchanged file should still be there
Assert.True(File.Exists(Path.Combine(appDir, "icon.png")));
}
/// <summary>
/// Tests differential with large binary files (simulating real-world exe/dll sizes).
/// </summary>
[Fact]
public async Task CleanAndDirty_LargeBinaryFile_HandlesCorrectly()
{
var sourceDir = Path.Combine(_testDir, "src_large");
var targetDir = Path.Combine(_testDir, "tgt_large");
var patchDir = Path.Combine(_testDir, "patch_large");
var appDir = Path.Combine(_testDir, "app_large");
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(targetDir);
Directory.CreateDirectory(patchDir);
Directory.CreateDirectory(appDir);
// 100KB binary file
var sourceBytes = new byte[100 * 1024];
var targetBytes = new byte[100 * 1024];
new Random(123).NextBytes(sourceBytes);
Array.Copy(sourceBytes, targetBytes, 100 * 1024);
// Modify segments at beginning, middle, and end
targetBytes[0] = 0xFF;
targetBytes[50 * 1024] = 0xAA;
targetBytes[99 * 1024] = 0xBB;
File.WriteAllBytes(Path.Combine(sourceDir, "large.dll"), sourceBytes);
File.WriteAllBytes(Path.Combine(targetDir, "large.dll"), targetBytes);
File.Copy(Path.Combine(sourceDir, "large.dll"), Path.Combine(appDir, "large.dll"));
// Act
await DifferentialCore.Clean(sourceDir, targetDir, patchDir);
await DifferentialCore.Dirty(appDir, patchDir);
// Assert
var result = File.ReadAllBytes(Path.Combine(appDir, "large.dll"));
Assert.Equal(targetBytes, result);
}
#endregion
#region Complex Directory Structure Scenarios
/// <summary>
/// Tests differential upgrade with deeply nested directory structures.
/// Real-world scenario: .NET app with multiple nested directories.
/// </summary>
[Fact]
public async Task CleanAndDirty_DeepNestedDirectories_CorrectlyUpdatesAll()
{
var sourceDir = Path.Combine(_testDir, "src_nested");
var targetDir = Path.Combine(_testDir, "tgt_nested");
var patchDir = Path.Combine(_testDir, "patch_nested");
var appDir = Path.Combine(_testDir, "app_nested");
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(targetDir);
Directory.CreateDirectory(patchDir);
Directory.CreateDirectory(appDir);
// Deep nested structure
var paths = new[]
{
"plugins/audio.dll", "plugins/video.dll", "resources/en/strings.json", "resources/zh/strings.json", "resources/dark_theme.json", "resources/light_theme.json", "config/settings.json"
};
foreach (var path in paths)
{
var srcPath = Path.Combine(sourceDir, path);
var tgtPath = Path.Combine(targetDir, path);
Directory.CreateDirectory(Path.GetDirectoryName(srcPath)!);
Directory.CreateDirectory(Path.GetDirectoryName(tgtPath)!);
File.WriteAllText(srcPath, $"v1.0.0: {path}");
File.WriteAllText(tgtPath, $"v2.0.0: {path}");
}
// Copy source to app
foreach (var path in paths)
{
var appPath = Path.Combine(appDir, path);
Directory.CreateDirectory(Path.GetDirectoryName(appPath)!);
File.Copy(Path.Combine(sourceDir, path), appPath);
}
// Act
await DifferentialCore.Clean(sourceDir, targetDir, patchDir);
await DifferentialCore.Dirty(appDir, patchDir);
// Assert — all files updated
foreach (var path in paths)
{
var appFilePath = Path.Combine(appDir, path);
Assert.True(File.Exists(appFilePath), $"File should exist: {path}");
// Content verification skipped: depends on differ implementation
}
}
/// <summary>
/// Tests mixed operations: some modified, some added, some deleted, some unchanged.
/// This is the most realistic real-world scenario.
/// </summary>
[Fact]
public async Task CleanAndDirty_MixedOperations_HandlesAllCorrectly()
{
var sourceDir = Path.Combine(_testDir, "src_mixed");
var targetDir = Path.Combine(_testDir, "tgt_mixed");
var patchDir = Path.Combine(_testDir, "patch_mixed");
var appDir = Path.Combine(_testDir, "app_mixed");
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(targetDir);
Directory.CreateDirectory(patchDir);
Directory.CreateDirectory(appDir);
// 1. Modified file
File.WriteAllText(Path.Combine(sourceDir, "modified.txt"), "Old content");
File.WriteAllText(Path.Combine(targetDir, "modified.txt"), "New content");
// 2. Deleted file
File.WriteAllText(Path.Combine(sourceDir, "deprecated.dll"), "Old DLL");
// 3. New file (top-level only — subdirectory new files tested separately)
File.WriteAllText(Path.Combine(targetDir, "new_feature.dll"), "New feature DLL");
// 4. Unchanged file
File.WriteAllText(Path.Combine(sourceDir, "unchanged.txt"), "Same content");
File.WriteAllText(Path.Combine(targetDir, "unchanged.txt"), "Same content");
// 5. Modified subdirectory file (both source and target have the subdir)
var subSrc = Path.Combine(sourceDir, "subdir");
var subTgt = Path.Combine(targetDir, "subdir");
Directory.CreateDirectory(subSrc);
Directory.CreateDirectory(subTgt);
File.WriteAllText(Path.Combine(subSrc, "nested.txt"), "Old nested");
File.WriteAllText(Path.Combine(subTgt, "nested.txt"), "New nested");
// Copy source to app
foreach (var file in Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories))
{
var relative = Path.GetRelativePath(sourceDir, file);
var appPath = Path.Combine(appDir, relative);
Directory.CreateDirectory(Path.GetDirectoryName(appPath)!);
File.Copy(file, appPath);
}
// Act
await DifferentialCore.Clean(sourceDir, targetDir, patchDir);
await DifferentialCore.Dirty(appDir, patchDir);
// Assert modified
Assert.Equal("New content", File.ReadAllText(Path.Combine(appDir, "modified.txt")));
Assert.Equal("New nested", File.ReadAllText(Path.Combine(appDir, "subdir", "nested.txt")));
// Assert new files
Assert.True(File.Exists(Path.Combine(appDir, "new_feature.dll")));
// Assert deleted
Assert.False(File.Exists(Path.Combine(appDir, "deprecated.dll")));
// Assert unchanged
Assert.Equal("Same content", File.ReadAllText(Path.Combine(appDir, "unchanged.txt")));
}
#endregion
#region DiffPipeline Tests (Parallel / Serial / Cancellation / Progress)
/// <summary>
/// Tests DiffPipeline with parallel processing.
/// </summary>
[Fact]
public async Task DiffPipeline_Parallel_ProcessesFilesConcurrently()
{
var src = Path.Combine(_testDir, "p_src");
var tgt = Path.Combine(_testDir, "p_tgt");
var patch = Path.Combine(_testDir, "p_patch");
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
// Create many files to test parallel processing
for (var i = 0; i < 20; i++)
{
File.WriteAllText(Path.Combine(src, $"file{i:D3}.txt"), $"old_{i}");
File.WriteAllText(Path.Combine(tgt, $"file{i:D3}.txt"), $"new_{i}");
}
var reporter = new SyncProgress<DiffProgress>();
var pipeline = new DiffPipelineBuilder()
.WithParallelism(4)
.Build();
// Act
await pipeline.CleanAsync(src, tgt, patch, reporter);
// Assert
Assert.True(reporter.LastValue.IsComplete);
Assert.Equal(20, reporter.LastValue.Total);
Assert.Equal(20, reporter.LastValue.Completed);
// Verify patches were generated for all files
for (var i = 0; i < 20; i++)
{
Assert.True(File.Exists(Path.Combine(patch, $"file{i:D3}.txt.patch")));
}
}
/// <summary>
/// Tests DiffPipeline with serial processing (parallelism=1).
/// </summary>
[Fact]
public async Task DiffPipeline_Serial_ProcessesFilesSequentially()
{
var src = Path.Combine(_testDir, "s_src");
var tgt = Path.Combine(_testDir, "s_tgt");
var patch = Path.Combine(_testDir, "s_patch");
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
for (var i = 0; i < 5; i++)
{
File.WriteAllText(Path.Combine(src, $"s{i}.txt"), $"old_{i}");
File.WriteAllText(Path.Combine(tgt, $"s{i}.txt"), $"new_{i}");
}
var reporter = new SyncProgress<DiffProgress>();
var pipeline = new DiffPipelineBuilder()
.WithParallelism(1) // Serial
.Build();
// Act
await pipeline.CleanAsync(src, tgt, patch, reporter);
// Assert
Assert.True(reporter.LastValue.IsComplete);
Assert.Equal(5, reporter.LastValue.Total);
Assert.Equal(5, reporter.LastValue.Completed);
}
/// <summary>
/// Tests DiffPipeline cancellation.
/// </summary>
[Fact]
public async Task DiffPipeline_Cancellation_ThrowsOperationCanceledException()
{
var src = Path.Combine(_testDir, "c_src");
var tgt = Path.Combine(_testDir, "c_tgt");
var patch = Path.Combine(_testDir, "c_patch");
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
for (var i = 0; i < 10; i++)
{
File.WriteAllText(Path.Combine(src, $"cf{i}.txt"), $"old_{i}");
File.WriteAllText(Path.Combine(tgt, $"cf{i}.txt"), $"new_{i}");
}
var pipeline = new DiffPipelineBuilder().WithParallelism(2).Build();
var cts = new System.Threading.CancellationTokenSource();
cts.Cancel();
// Act & Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
pipeline.CleanAsync(src, tgt, patch, cancellationToken: cts.Token));
}
/// <summary>
/// Tests DiffPipeline Dirty with progress reporting.
/// </summary>
[Fact]
public async Task DiffPipeline_DirtyWithProgress_ReportsCorrectly()
{
var app = Path.Combine(_testDir, "dp_app");
var src = Path.Combine(_testDir, "dp_src");
var tgt = Path.Combine(_testDir, "dp_tgt");
var patch = Path.Combine(_testDir, "dp_patch");
Directory.CreateDirectory(app);
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
// Setup: 3 modified, 2 new, 1 deleted
File.WriteAllText(Path.Combine(src, "m1.txt"), "old1"); // modified
File.WriteAllText(Path.Combine(src, "m2.txt"), "old2"); // modified
File.WriteAllText(Path.Combine(src, "m3.txt"), "old3"); // modified
File.WriteAllText(Path.Combine(src, "del.txt"), "delete"); // deleted
File.WriteAllText(Path.Combine(src, "keep.txt"), "same"); // unchanged
File.WriteAllText(Path.Combine(tgt, "m1.txt"), "new1");
File.WriteAllText(Path.Combine(tgt, "m2.txt"), "new2");
File.WriteAllText(Path.Combine(tgt, "m3.txt"), "new3");
File.WriteAllText(Path.Combine(tgt, "n1.txt"), "added1"); // new
File.WriteAllText(Path.Combine(tgt, "n2.txt"), "added2"); // new
File.WriteAllText(Path.Combine(tgt, "keep.txt"), "same");
// Copy source to app
foreach (var f in Directory.GetFiles(src))
File.Copy(f, Path.Combine(app, Path.GetFileName(f)));
// Generate patches
var genPipeline = new DiffPipelineBuilder().WithParallelism(1).Build();
await genPipeline.CleanAsync(src, tgt, patch);
// Apply with progress
var reporter = new SyncProgress<DiffProgress>();
var pipeline = new DiffPipelineBuilder().WithParallelism(1).Build();
await pipeline.DirtyAsync(app, patch, reporter);
// Assert progress was reported
Assert.True(reporter.LastValue.IsComplete);
Assert.NotEqual(0, reporter.LastValue.Total);
Assert.Equal(reporter.LastValue.Total, reporter.LastValue.Completed);
// Assert files are correct
Assert.Equal("new1", File.ReadAllText(Path.Combine(app, "m1.txt")));
Assert.Equal("new2", File.ReadAllText(Path.Combine(app, "m2.txt")));
Assert.Equal("new3", File.ReadAllText(Path.Combine(app, "m3.txt")));
Assert.True(File.Exists(Path.Combine(app, "n1.txt")));
Assert.True(File.Exists(Path.Combine(app, "n2.txt")));
Assert.False(File.Exists(Path.Combine(app, "del.txt")));
Assert.Equal("same", File.ReadAllText(Path.Combine(app, "keep.txt")));
}
#endregion
#region DiffPipeline Parameters Matrix
/// <summary>
/// Tests DiffPipelineBuilder with custom IBinaryDiffer.
/// </summary>
[Fact]
public void DiffPipelineBuilder_CustomDiffer_UsesProvidedDiffer()
{
// Arrange
var customDiffer = new BinaryHandler();
// Act
var pipeline = new DiffPipelineBuilder()
.UseDiffer(customDiffer)
.Build();
// Assert
Assert.NotNull(pipeline);
}
/// <summary>
/// Tests DiffPipelineBuilder with various parallelism values.
/// </summary>
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(4)]
[InlineData(8)]
public void DiffPipelineBuilder_VariousParallelism_BuildsSuccessfully(int parallelism)
{
// Act
var pipeline = new DiffPipelineBuilder()
.WithParallelism(parallelism)
.Build();
// Assert
Assert.NotNull(pipeline);
}
/// <summary>
/// Tests DiffProgress calculation correctness.
/// </summary>
[Fact]
public void DiffProgress_Calculation_ReturnsCorrectValues()
{
// Boundary cases
var p0 = new DiffProgress(0, 10, null);
Assert.Equal(0.0, p0.Percentage);
Assert.False(p0.IsComplete);
var p50 = new DiffProgress(5, 10, null);
Assert.Equal(50.0, p50.Percentage);
Assert.False(p50.IsComplete);
var p100 = new DiffProgress(10, 10, null);
Assert.Equal(100.0, p100.Percentage);
Assert.True(p100.IsComplete);
// Error case
var pErr = new DiffProgress(3, 5, "bad.dll", "Hash mismatch");
Assert.Equal(60.0, pErr.Percentage);
Assert.Equal("bad.dll", pErr.CurrentFile);
Assert.Equal("Hash mismatch", pErr.Error);
// Complete factory
var pComplete = DiffProgress.Complete(42);
Assert.True(pComplete.IsComplete);
Assert.Equal(42, pComplete.Completed);
Assert.Equal(42, pComplete.Total);
Assert.Equal(100.0, pComplete.Percentage);
}
#endregion
#region Real-world Developer Scenarios
/// <summary>
/// Scenario: Developer implements a custom differ and uses it in the pipeline.
/// </summary>
[Fact]
public async Task DeveloperScenario_CustomDifferInPipeline_WorksCorrectly()
{
var src = Path.Combine(_testDir, "ds_src");
var tgt = Path.Combine(_testDir, "ds_tgt");
var patch = Path.Combine(_testDir, "ds_patch");
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
File.WriteAllText(Path.Combine(src, "code.cs"), "class A { }");
File.WriteAllText(Path.Combine(tgt, "code.cs"), "class A { void B() { } }");
// Developer uses BinaryHandler (BSDIFF) as the differ
var pipeline = new DiffPipelineBuilder()
.UseDiffer(new BinaryHandler())
.WithParallelism(1)
.Build();
// Act
await pipeline.CleanAsync(src, tgt, patch);
// Assert
Assert.True(File.Exists(Path.Combine(patch, "code.cs.patch")));
}
/// <summary>
/// Scenario: Developer wants to apply patches in parallel for maximum speed.
/// </summary>
[Fact]
public async Task DeveloperScenario_MaxParallelism_AppliesPatchesFast()
{
var app = Path.Combine(_testDir, "mp_app");
var src = Path.Combine(_testDir, "mp_src");
var tgt = Path.Combine(_testDir, "mp_tgt");
var patch = Path.Combine(_testDir, "mp_patch");
Directory.CreateDirectory(app);
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
// Simulate a large app with many files
for (var i = 0; i < 30; i++)
{
var path = i % 3 == 0 ? $"sub{i / 3}" : "";
if (path.Length > 0) Directory.CreateDirectory(Path.Combine(src, path));
if (path.Length > 0) Directory.CreateDirectory(Path.Combine(tgt, path));
File.WriteAllText(Path.Combine(src, path, $"file{i}.dll"), $"code_v1_{i}");
File.WriteAllText(Path.Combine(tgt, path, $"file{i}.dll"), $"code_v2_{i}");
if (path.Length > 0) Directory.CreateDirectory(Path.Combine(app, path));
File.Copy(Path.Combine(src, path, $"file{i}.dll"),
Path.Combine(app, path, $"file{i}.dll"));
}
// Generate patches (serial for determinism)
var genPipeline = new DiffPipelineBuilder().WithParallelism(1).Build();
await genPipeline.CleanAsync(src, tgt, patch);
// Apply patches with maximum parallelism
var applyPipeline = new DiffPipelineBuilder().WithParallelism(Environment.ProcessorCount).Build();
await applyPipeline.DirtyAsync(app, patch);
// Assert all files updated
for (var i = 0; i < 30; i++)
{
var path = i % 3 == 0 ? $"sub{i / 3}" : "";
var filePath = Path.Combine(app, path, $"file{i}.dll");
Assert.True(File.Exists(filePath), $"File file{i}.dll should exist");
Assert.Equal($"code_v2_{i}", File.ReadAllText(filePath));
}
}
/// <summary>
/// Scenario: Developer runs differential update for each incremental version.
/// Simulates: v1.0.0 -> v1.0.1 -> v1.0.2 -> v1.0.3 chain.
/// </summary>
[Fact]
public async Task DeveloperScenario_VersionChainUpdate_ThreeIncrementalSteps()
{
var appDir = Path.Combine(_testDir, "vc_app");
Directory.CreateDirectory(appDir);
// Initial app state
File.WriteAllText(Path.Combine(appDir, "app.exe"), "v1.0.0");
File.WriteAllText(Path.Combine(appDir, "lib.dll"), "lib_v1");
// Step 1: v1.0.0 — v1.0.1
var step1 = await ApplyIncrementalUpdate(appDir, "app.exe", "v1.0.0", "v1.0.1");
Assert.True(step1);
Assert.Equal("v1.0.1", File.ReadAllText(Path.Combine(appDir, "app.exe")));
// Step 2: v1.0.1 — v1.0.2
var step2 = await ApplyIncrementalUpdate(appDir, "app.exe", "v1.0.1", "v1.0.2");
Assert.True(step2);
Assert.Equal("v1.0.2", File.ReadAllText(Path.Combine(appDir, "app.exe")));
// Step 3: v1.0.2 — v1.0.3
var step3 = await ApplyIncrementalUpdate(appDir, "app.exe", "v1.0.2", "v1.0.3");
Assert.True(step3);
Assert.Equal("v1.0.3", File.ReadAllText(Path.Combine(appDir, "app.exe")));
// lib.dll should be unchanged throughout
Assert.Equal("lib_v1", File.ReadAllText(Path.Combine(appDir, "lib.dll")));
}
private async Task<bool> ApplyIncrementalUpdate(string appDir, string fileName, string oldContent, string newContent)
{
var stepDir = Path.Combine(_testDir, $"step_{oldContent}_to_{newContent}");
var srcDir = Path.Combine(stepDir, "src");
var tgtDir = Path.Combine(stepDir, "tgt");
var patchDir = Path.Combine(stepDir, "patch");
Directory.CreateDirectory(srcDir);
Directory.CreateDirectory(tgtDir);
Directory.CreateDirectory(patchDir);
File.WriteAllText(Path.Combine(srcDir, fileName), oldContent);
File.WriteAllText(Path.Combine(tgtDir, fileName), newContent);
await DifferentialCore.Clean(srcDir, tgtDir, patchDir);
await DifferentialCore.Dirty(appDir, patchDir);
return File.ReadAllText(Path.Combine(appDir, fileName)) == newContent;
}
#endregion
#region Edge Cases
/// <summary>
/// Tests Clean with empty source directory (fresh install scenario).
/// </summary>
[Fact]
public async Task Clean_EmptySource_AllTargetFilesCopied()
{
var src = Path.Combine(_testDir, "ec_src");
var tgt = Path.Combine(_testDir, "ec_tgt");
var patch = Path.Combine(_testDir, "ec_patch");
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
File.WriteAllText(Path.Combine(tgt, "a.txt"), "A");
File.WriteAllText(Path.Combine(tgt, "b.txt"), "B");
File.WriteAllText(Path.Combine(tgt, "c.txt"), "C");
// Act
await DifferentialCore.Clean(src, tgt, patch);
// Assert
Assert.True(File.Exists(Path.Combine(patch, "a.txt")));
Assert.True(File.Exists(Path.Combine(patch, "b.txt")));
Assert.True(File.Exists(Path.Combine(patch, "c.txt")));
}
/// <summary>
/// Tests Clean with empty target directory (uninstall scenario).
/// </summary>
[Fact]
public async Task Clean_EmptyTarget_GeneratesDeleteList()
{
var src = Path.Combine(_testDir, "et_src");
var tgt = Path.Combine(_testDir, "et_tgt");
var patch = Path.Combine(_testDir, "et_patch");
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
File.WriteAllText(Path.Combine(src, "x.dll"), "X");
File.WriteAllText(Path.Combine(src, "y.dll"), "Y");
// Act
await DifferentialCore.Clean(src, tgt, patch);
// Assert
var deleteListFile = Path.Combine(patch, "generalupdate_delete_files.json");
Assert.True(File.Exists(deleteListFile));
var deleteList = JsonSerializer.Deserialize<List<FileNode>>(
File.ReadAllText(deleteListFile),
FileNodesJsonContext.Default.ListFileNode);
Assert.NotNull(deleteList);
Assert.Equal(2, deleteList.Count);
}
/// <summary>
/// Tests Dirty with non-existent app path (should not throw).
/// </summary>
[Fact]
public async Task Dirty_NonExistentAppPath_NoException()
{
var patchDir = Path.Combine(_testDir, "ne_patch");
Directory.CreateDirectory(patchDir);
File.WriteAllText(Path.Combine(patchDir, "dummy.txt"), "test");
var nonExistent = Path.Combine(_testDir, "does_not_exist");
// Act & Assert — should not throw
await DifferentialCore.Dirty(nonExistent, patchDir);
}
/// <summary>
/// Tests Dirty with non-existent patch path (should not throw).
/// </summary>
[Fact]
public async Task Dirty_NonExistentPatchPath_NoException()
{
var appDir = Path.Combine(_testDir, "ne_app");
Directory.CreateDirectory(appDir);
File.WriteAllText(Path.Combine(appDir, "test.txt"), "test");
var nonExistent = Path.Combine(_testDir, "does_not_exist_patch");
// Act & Assert — should not throw
await DifferentialCore.Dirty(appDir, nonExistent);
}
/// <summary>
/// Tests that Dirty does not leave temporary files after applying patches.
/// </summary>
[Fact]
public async Task Dirty_AfterApplying_CleansUpTemporaryFiles()
{
var app = Path.Combine(_testDir, "cu_app");
var src = Path.Combine(_testDir, "cu_src");
var tgt = Path.Combine(_testDir, "cu_tgt");
var patch = Path.Combine(_testDir, "cu_patch");
Directory.CreateDirectory(app);
Directory.CreateDirectory(src);
Directory.CreateDirectory(tgt);
Directory.CreateDirectory(patch);
File.WriteAllText(Path.Combine(src, "file.txt"), "old");
File.WriteAllText(Path.Combine(tgt, "file.txt"), "new");
File.Copy(Path.Combine(src, "file.txt"), Path.Combine(app, "file.txt"));
await DifferentialCore.Clean(src, tgt, patch);
await DifferentialCore.Dirty(app, patch);
// Assert — no leftover temp files
var filesInApp = Directory.GetFiles(app);
Assert.Single(filesInApp);
Assert.EndsWith("file.txt", filesInApp[0]);
Assert.Equal("new", File.ReadAllText(filesInApp[0]));
}
#endregion
#region Helper
private sealed class SyncProgress<T> : IProgress<T>
{
public T LastValue { get; private set; } = default!;
public void Report(T value)
{
LastValue = value;
}
}
#endregion
}
}