|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.Json; |
| 3 | +using Bogus; |
| 4 | +using KnowledgeBaseServer.Dtos; |
| 5 | +using KnowledgeBaseServer.Tests.Data; |
| 6 | +using KnowledgeBaseServer.Tools; |
| 7 | +using Shouldly; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace KnowledgeBaseServer.Tests.Tools; |
| 11 | + |
| 12 | +public class ConnectMemoriesToolTests : DatabaseTest |
| 13 | +{ |
| 14 | + private readonly Faker _faker = new(); |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void ShouldReturnError_WhenIdsAreNotValid() |
| 18 | + { |
| 19 | + // arrange |
| 20 | + |
| 21 | + // act |
| 22 | + var result = ConnectMemoriesTool.Handle(ConnectionString, _faker.Random.Guid(), [_faker.Random.Guid()]); |
| 23 | + |
| 24 | + // assert |
| 25 | + using var connection = ConnectionString.CreateConnection(); |
| 26 | + result.ShouldBe("Invalid memory ids provided."); |
| 27 | + connection.GetMemoryLinks().ShouldBeEmpty(); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void ShouldReturnNotModifyDatabase_WhenLinkExists() |
| 32 | + { |
| 33 | + // arrange |
| 34 | + var memories = JsonSerializer.Deserialize<CreatedMemoryDto[]>( |
| 35 | + CreateMemoriesTool.Handle( |
| 36 | + ConnectionString, |
| 37 | + JsonSerializerOptions.Default, |
| 38 | + _faker.Lorem.Word(), |
| 39 | + _faker.Lorem.Words(2) |
| 40 | + ) |
| 41 | + ); |
| 42 | + |
| 43 | + Debug.Assert(memories is { Length: 2 }); |
| 44 | + var parentMemoryId = memories[0].Id; |
| 45 | + var childMemoryId = memories[1].Id; |
| 46 | + |
| 47 | + using (var seedConnection = ConnectionString.CreateConnection()) |
| 48 | + { |
| 49 | + seedConnection.SeedMemoryLink( |
| 50 | + new MemoryLink |
| 51 | + { |
| 52 | + Created = _faker.Date.PastOffset(), |
| 53 | + FromMemoryId = childMemoryId, |
| 54 | + ToMemoryId = parentMemoryId, |
| 55 | + } |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + // act |
| 60 | + var result = ConnectMemoriesTool.Handle(ConnectionString, parentMemoryId, [childMemoryId]); |
| 61 | + |
| 62 | + // assert |
| 63 | + using var connection = ConnectionString.CreateConnection(); |
| 64 | + result.ShouldContain("already linked"); |
| 65 | + connection.GetMemoryLinks().ShouldHaveSingleItem(); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void ShouldCreateLink() |
| 70 | + { |
| 71 | + // arrange |
| 72 | + var memories = JsonSerializer.Deserialize<CreatedMemoryDto[]>( |
| 73 | + CreateMemoriesTool.Handle( |
| 74 | + ConnectionString, |
| 75 | + JsonSerializerOptions.Default, |
| 76 | + _faker.Lorem.Word(), |
| 77 | + _faker.Lorem.Words() |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + Debug.Assert(memories is { Length: 3 }); |
| 82 | + var parentMemoryId = memories[0].Id; |
| 83 | + var child1MemoryId = memories[1].Id; |
| 84 | + var child2MemoryId = memories[2].Id; |
| 85 | + |
| 86 | + // act |
| 87 | + var result = ConnectMemoriesTool.Handle(ConnectionString, parentMemoryId, [child1MemoryId, child2MemoryId]); |
| 88 | + |
| 89 | + using var connection = ConnectionString.CreateConnection(); |
| 90 | + var actualMemoryLinks = connection.GetMemoryLinks(); |
| 91 | + |
| 92 | + // assert |
| 93 | + result.ShouldBe("Memories linked successfully."); |
| 94 | + actualMemoryLinks.ShouldSatisfyAllConditions( |
| 95 | + () => actualMemoryLinks.ShouldContain(ml => ml.ToMemoryId == parentMemoryId, expectedCount: 2), |
| 96 | + () => actualMemoryLinks.ShouldContain(ml => ml.FromMemoryId == child1MemoryId, expectedCount: 1), |
| 97 | + () => actualMemoryLinks.ShouldContain(ml => ml.FromMemoryId == child2MemoryId, expectedCount: 1) |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments