|
| 1 | +using System.Text.Json; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using SIL.Harmony.Changes; |
| 4 | +using SIL.Harmony.Sample; |
| 5 | +using SIL.Harmony.Sample.Changes; |
| 6 | + |
| 7 | +namespace SIL.Harmony.Tests; |
| 8 | + |
| 9 | +public class ChangeConverterTests |
| 10 | +{ |
| 11 | + private static JsonSerializerOptions SampleOptions() => |
| 12 | + new ServiceCollection() |
| 13 | + .AddCrdtDataSample(":memory:") |
| 14 | + .BuildServiceProvider() |
| 15 | + .GetRequiredService<JsonSerializerOptions>(); |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void Happy_path_deserializes_to_concrete_change() |
| 19 | + { |
| 20 | + var options = SampleOptions(); |
| 21 | + var entityId = Guid.Parse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); |
| 22 | + IChange change = new SetWordTextChange(entityId, "hello"); |
| 23 | + |
| 24 | + var json = JsonSerializer.Serialize(change, options); |
| 25 | + var roundTripped = JsonSerializer.Deserialize<IChange>(json, options); |
| 26 | + |
| 27 | + roundTripped.Should().BeOfType<SetWordTextChange>() |
| 28 | + .Which.Text.Should().Be("hello"); |
| 29 | + roundTripped!.EntityId.Should().Be(entityId); |
| 30 | + json.Should().StartWith("{\"$type\":\"SetWordTextChange\""); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void Unknown_type_deserializes_to_OpaqueChange() |
| 35 | + { |
| 36 | + var options = SampleOptions(); |
| 37 | + var json = """ |
| 38 | + {"$type":"SetWordPriorityChange","EntityId":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","Priority":7} |
| 39 | + """; |
| 40 | + |
| 41 | + var change = JsonSerializer.Deserialize<IChange>(json, options); |
| 42 | + |
| 43 | + var opaque = change.Should().BeOfType<OpaqueChange>().Subject; |
| 44 | + opaque.TypeName.Should().Be("SetWordPriorityChange"); |
| 45 | + opaque.RawJson.GetProperty("Priority").GetInt32().Should().Be(7); |
| 46 | + opaque.SupportsNewEntity().Should().BeFalse(); |
| 47 | + opaque.SupportsApplyChange().Should().BeFalse(); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void OpaqueChange_round_trips_original_discriminator() |
| 52 | + { |
| 53 | + var options = SampleOptions(); |
| 54 | + var json = """ |
| 55 | + {"$type":"SetWordPriorityChange","EntityId":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","Priority":7} |
| 56 | + """; |
| 57 | + |
| 58 | + var change = JsonSerializer.Deserialize<IChange>(json, options)!; |
| 59 | + var rewritten = JsonSerializer.Serialize(change, options); |
| 60 | + |
| 61 | + rewritten.Should().Contain("\"$type\":\"SetWordPriorityChange\""); |
| 62 | + rewritten.Should().Contain("\"Priority\":7"); |
| 63 | + rewritten.Should().NotContain("OpaqueChange"); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Mixed_commit_round_trips_known_and_opaque_changes() |
| 68 | + { |
| 69 | + var options = SampleOptions(); |
| 70 | + var entityId = Guid.Parse("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); |
| 71 | + var commit = new Commit |
| 72 | + { |
| 73 | + ClientId = Guid.NewGuid(), |
| 74 | + HybridDateTime = new HybridDateTime(DateTimeOffset.UtcNow, 0), |
| 75 | + }; |
| 76 | + commit.ChangeEntities.Add(new ChangeEntity<IChange> |
| 77 | + { |
| 78 | + Index = 0, |
| 79 | + CommitId = commit.Id, |
| 80 | + EntityId = entityId, |
| 81 | + Change = new SetWordTextChange(entityId, "hello") |
| 82 | + }); |
| 83 | + |
| 84 | + var json = JsonSerializer.Serialize(commit, options); |
| 85 | + // Inject an unknown change as if from a newer client. |
| 86 | + json = json.Replace( |
| 87 | + "\"ChangeEntities\":[", |
| 88 | + """ |
| 89 | + "ChangeEntities":[{"Index":1,"CommitId":"00000000-0000-0000-0000-000000000000","EntityId":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","Change":{"$type":"SetWordPriorityChange","Priority":3}}, |
| 90 | + """); |
| 91 | + |
| 92 | + var roundTripped = JsonSerializer.Deserialize<Commit>(json, options)!; |
| 93 | + roundTripped.ChangeEntities.Should().HaveCount(2); |
| 94 | + roundTripped.ChangeEntities.Select(c => c.Change.GetType()) |
| 95 | + .Should().BeEquivalentTo([typeof(OpaqueChange), typeof(SetWordTextChange)]); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void Requires_type_as_first_property() |
| 100 | + { |
| 101 | + var options = SampleOptions(); |
| 102 | + var json = """ |
| 103 | + {"Text":"hello","EntityId":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","$type":"SetWordTextChange"} |
| 104 | + """; |
| 105 | + |
| 106 | + var act = () => JsonSerializer.Deserialize<IChange>(json, options); |
| 107 | + act.Should().Throw<JsonException>().WithMessage("*first property*"); |
| 108 | + } |
| 109 | +} |
0 commit comments