Skip to content

Commit

Permalink
Override ATUri/ATIdentifier Equals for proper checking (#134)
Browse files Browse the repository at this point in the history
* Bump bindings

* Override Equals
  • Loading branch information
drasticactions authored Dec 24, 2024
1 parent 31c5a54 commit 9f2db36
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 35 deletions.
48 changes: 24 additions & 24 deletions src/FishyFlip.Auth.Tests/AuthorizedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task GetFeedGeneratorAsyncTest(string feedGenerator)
result.Switch(
success =>
{
Assert.AreEqual(atUri.ToString(), success!.View!.Uri!.ToString());
Assert.AreEqual(atUri, success!.View!.Uri!);
},
failed =>
{
Expand All @@ -114,7 +114,7 @@ public async Task GetProfileAsyncTest(string handle1)
result.Switch(
success =>
{
Assert.AreEqual(success!.Handle!.ToString(), handle1);
Assert.AreEqual(success!.Handle!, ATHandle.Create(handle1));
},
failed =>
{
Expand All @@ -136,8 +136,8 @@ public async Task GetProfilesAsyncWithHandlesTest(string handle1, string handle2
result.Switch(
success =>
{
Assert.AreEqual(handle1, success!.Profiles![0]!.Handle!.ToString());
Assert.AreEqual(handle2, success!.Profiles![1]!.Handle!.ToString());
Assert.AreEqual(ATHandle.Create(handle1), success!.Profiles![0]!.Handle!);
Assert.AreEqual(ATHandle.Create(handle2), success!.Profiles![1]!.Handle!);
},
failed =>
{
Expand All @@ -161,8 +161,8 @@ public async Task GetProfilesAsyncWithDidTest(string did1, string did2)
result.Switch(
success =>
{
Assert.AreEqual(test1did!.ToString(), success!.Profiles![0]!.Did!.ToString());
Assert.AreEqual(test2did!.ToString(), success!.Profiles![1]!.Did!.ToString());
Assert.AreEqual(test1did!, success!.Profiles![0]!.Did!);
Assert.AreEqual(test2did!, success!.Profiles![1]!.Did!);
},
failed =>
{
Expand All @@ -186,8 +186,8 @@ public async Task GetPostsAsyncTest(string quotePost, string quotePost2)
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), success!.Posts![0].Uri!.ToString());
Assert.AreEqual(postUri2.ToString(), success!.Posts[1].Uri!.ToString());
Assert.AreEqual(postUri, success!.Posts![0].Uri!);
Assert.AreEqual(postUri2, success!.Posts[1].Uri!);
},
failed =>
{
Expand All @@ -209,7 +209,7 @@ public async Task GetPostThreadAsyncTest(string postThread)
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), ((ThreadViewPost)success!.Thread!)!.Post!.Uri!.ToString());
Assert.AreEqual(postUri, ((ThreadViewPost)success!.Thread!)!.Post!.Uri!);
},
failed =>
{
Expand All @@ -232,9 +232,9 @@ public async Task GetQuotePostThreadAsyncTest(string quotePost, string quotePost
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), ((ThreadViewPost)success!.Thread!).Post!.Uri!.ToString());
Assert.AreEqual(postUri, ((ThreadViewPost)success!.Thread!).Post!.Uri!);
var embedRecord = (ViewRecordDef)((ThreadViewPost)success!.Thread!).Post!.Embed!;
Assert.AreEqual(quotePost2, ((ViewRecord)embedRecord.Record!).Uri!.ToString());
Assert.AreEqual(ATUri.Create(quotePost2), ((ViewRecord)embedRecord.Record!).Uri!);
},
failed =>
{
Expand All @@ -256,7 +256,7 @@ public async Task GetExternalPostThreadAsyncTest(string externalPost)
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), ((ThreadViewPost)success!.Thread!).Post!.Uri!.ToString());
Assert.AreEqual(postUri, ((ThreadViewPost)success!.Thread!).Post!.Uri!);
},
failed =>
{
Expand All @@ -278,7 +278,7 @@ public async Task GetImagesPostThreadAsyncTest(string imagesPost)
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), ((ThreadViewPost)success!.Thread!).Post!.Uri!.ToString());
Assert.AreEqual(postUri, ((ThreadViewPost)success!.Thread!).Post!.Uri!);
},
failed =>
{
Expand All @@ -300,7 +300,7 @@ public async Task GetRecordWithMediaPostThreadAsyncTest(string mediaPost)
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), ((ThreadViewPost)success!.Thread!).Post!.Uri!.ToString());
Assert.AreEqual(postUri, ((ThreadViewPost)success!.Thread!).Post!.Uri!);
},
failed =>
{
Expand All @@ -322,7 +322,7 @@ public async Task GetRepliesPostThreadAsyncTest(string postThread)
postThreadResult.Switch(
success =>
{
Assert.AreEqual(postUri.ToString(), ((ThreadViewPost)success!.Thread!).Post!.Uri!.ToString());
Assert.AreEqual(postUri, ((ThreadViewPost)success!.Thread!).Post!.Uri!);
Assert.IsTrue(((ThreadViewPost)success!.Thread!).Replies!.Count() > 0);
},
failed =>
Expand Down Expand Up @@ -451,8 +451,8 @@ public async Task CreatePostWithTagAsyncTest()
[DataRow("did:plc:nrfz3bngz57p7g7yg6pbkyqr")]
public async Task CreateAndRemoveListTest(string followDid)
{
var randomName = Guid.NewGuid().ToString();
var list = new FishyFlip.Lexicon.App.Bsky.Graph.List(ListPurpose.Curatelist, randomName, "Test List", createdAt: DateTime.UtcNow);
var randomName = Guid.NewGuid();
var list = new FishyFlip.Lexicon.App.Bsky.Graph.List(ListPurpose.Curatelist, randomName.ToString(), "Test List", createdAt: DateTime.UtcNow);
var createList = (await AuthorizedTests.proto!.Graph.CreateListAsync(list)).HandleResult();
Assert.IsTrue(createList!.Cid is not null);
Assert.IsTrue(createList!.Uri is not null);
Expand Down Expand Up @@ -486,8 +486,8 @@ public async Task CreateAndRemoveListTest(string followDid)
[TestMethod]
public async Task CreateAndRemovePostTest()
{
var randomName = Guid.NewGuid().ToString();
var create = (await AuthorizedTests.proto!.Feed.CreatePostAsync(randomName)).HandleResult();
var randomName = Guid.NewGuid();
var create = (await AuthorizedTests.proto!.Feed.CreatePostAsync(randomName.ToString())).HandleResult();
Assert.IsTrue(create!.Cid is not null);
Assert.IsTrue(create!.Uri is not null);
var repo = AuthorizedTests.proto!.Session!.Did;
Expand All @@ -502,8 +502,8 @@ public async Task CreateAndRemovePostTest()
[TestMethod]
public async Task CreateAndRemoveRepostTest()
{
var randomName = Guid.NewGuid().ToString();
var create = (await AuthorizedTests.proto!.Feed.CreatePostAsync(randomName)).HandleResult();
var randomName = Guid.NewGuid();
var create = (await AuthorizedTests.proto!.Feed.CreatePostAsync(randomName.ToString())).HandleResult();
Assert.IsTrue(create!.Cid is not null);
Assert.IsTrue(create!.Uri is not null);

Expand All @@ -526,8 +526,8 @@ public async Task CreateAndRemoveRepostTest()
[TestMethod]
public async Task CreateAndRemoveLikeTest()
{
var randomName = Guid.NewGuid().ToString();
var create = (await AuthorizedTests.proto!.Feed.CreatePostAsync(randomName)).HandleResult();
var randomName = Guid.NewGuid();
var create = (await AuthorizedTests.proto!.Feed.CreatePostAsync(randomName.ToString())).HandleResult();
Assert.IsTrue(create!.Cid is not null);
Assert.IsTrue(create!.Uri is not null);

Expand Down Expand Up @@ -590,7 +590,7 @@ public async Task DescribeRepoTest()
Assert.IsTrue(describe is not null);
Assert.IsTrue(describe.HandleIsCorrect);
Assert.IsTrue(describe.Did is not null);
Assert.IsTrue(describe.Did!.ToString() == repo.ToString());
Assert.AreEqual(describe.Did!, repo);
}

/// <summary>
Expand Down
31 changes: 28 additions & 3 deletions src/FishyFlip.Tests/AnonymousTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task TestPostAsync(string atUri, string embedType)
success =>
{
var value = (Post)success!.Value!;
Assert.AreEqual(postUri.ToString(), success!.Uri!.ToString());
Assert.AreEqual(postUri, success!.Uri!);
Assert.IsNotNull(success.Value);
Assert.AreEqual(success.Value.Type, Post.RecordType);

Expand Down Expand Up @@ -135,7 +135,7 @@ public async Task DescribeRepoTest(string did)
Assert.IsTrue(describe is not null);
Assert.IsTrue(describe.HandleIsCorrect);
Assert.IsTrue(describe.Did is not null);
Assert.AreEqual(describe.Did!.ToString(), repo!.ToString());
Assert.AreEqual(describe.Did!, repo!);
}

/// <summary>
Expand All @@ -154,7 +154,7 @@ public void MarkdownPostTest()
Assert.IsTrue(post.Facets[0].Index!.ByteStart == 15);
Assert.IsTrue(post.Facets[0].Index!.ByteEnd == 24);
Assert.IsTrue(post.Facets[1].Features![0]!.Type == FishyFlip.Lexicon.App.Bsky.Richtext.Mention.RecordType);
Assert.IsTrue(((Mention)post.Facets[1].Features![0]!).Did!.ToString() == "did:plc:yhgc5rlqhoezrx6fbawajxlh");
Assert.IsTrue(Equals(((Mention)post.Facets[1].Features![0]!).Did!, ATDid.Create("did:plc:yhgc5rlqhoezrx6fbawajxlh")!));
Assert.IsTrue(post.Facets[1].Index!.ByteStart == 38);
Assert.IsTrue(post.Facets[1].Index!.ByteEnd == 57);
Assert.IsTrue(post.Facets[2].Features![0]!.Type == FishyFlip.Lexicon.App.Bsky.Richtext.Tag.RecordType);
Expand Down Expand Up @@ -241,4 +241,29 @@ public async Task LabelDetails()
var labelerViewDetailed = (LabelerViewDetailed)result.Views![0];
Assert.IsTrue(labelerViewDetailed.Policies?.LabelValues?.Any());
}

/// <summary>
/// Validate ATUri.
/// </summary>
[TestMethod]
public void ATUriValidation()
{
var atUri = ATUri.Create("at://did:plc:okblbaji7rz243bluudjlgxt/app.bsky.feed.post/3kv25q4gqbk2y");
var atUri2 = ATUri.Create("at://did:plc:okblbaji7rz243bluudjlgxt/app.bsky.feed.post/3knxcq7bwwo2j");
var test = atUri.Equals(atUri);
Assert.AreEqual(atUri, atUri);
Assert.AreNotEqual(atUri, atUri2);

var atDid = ATDid.Create("did:plc:okblbaji7rz243bluudjlgxt");
var atDid2 = ATDid.Create("did:web:drasticactions.dev");

Assert.AreEqual(atDid, atDid);
Assert.AreNotEqual(atDid, atDid2);

var atHandle = ATHandle.Create("drasticactions.dev");
var atHandle2 = ATHandle.Create("drasticactions.jp");

Assert.AreEqual(atHandle, atHandle);
Assert.AreNotEqual(atHandle, atHandle2);
}
}
17 changes: 17 additions & 0 deletions src/FishyFlip/Models/ATIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,21 @@ public static bool TryCreate(string uri, out ATIdentifier? atIdentifier)
return false;
}
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
return obj switch
{
ATDid atDid => this.ToString() == atDid.ToString(),
ATHandle atHandle => this.ToString() == atHandle.ToString(),
_ => obj?.ToString() == this.ToString(),
};
}

/// <inheritdoc/>
public override int GetHashCode()
{
return this.ToString()?.GetHashCode() ?? 0;
}
}
26 changes: 18 additions & 8 deletions src/FishyFlip/Models/ATUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ public static bool IsValid(string uri)
return match.Success;
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
if (obj is ATUri atUri)
{
return this.ToString() == atUri.ToString();
}

return false;
}

/// <inheritdoc/>
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}

/// <summary>
/// Returns a string representation of the AT URI.
/// </summary>
Expand All @@ -156,14 +173,7 @@ public override string ToString()
buffer.Append("at://");
buffer.Append(this.host);

if (!this.Pathname.StartsWith("/"))
{
buffer.Append($"/{this.Pathname}");
}
else
{
buffer.Append(this.Pathname);
}
buffer.Append(!this.Pathname.StartsWith("/") ? $"/{this.Pathname}" : this.Pathname);

if (!string.IsNullOrEmpty(this.Hash) && !this.Hash.StartsWith("#"))
{
Expand Down

0 comments on commit 9f2db36

Please sign in to comment.