Skip to content

Commit cd22b0f

Browse files
authored
Merge pull request #170 from fboucher/fix/summaryeditor-crash
fix(summaryeditor): prevent IndexOutOfRangeException on missing tags
2 parents f7a6f80 + 427dce3 commit cd22b0f

4 files changed

Lines changed: 370 additions & 218 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.3.0</Version>
3+
<Version>1.3.1</Version>
44
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Text.Json;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using FluentAssertions;
9+
using NoteBookmark.Domain;
10+
using NoteBookmark.SharedUI;
11+
using Xunit;
12+
13+
namespace NoteBookmark.BlazorApp.Tests.Tests;
14+
15+
public class PostNoteClientTests
16+
{
17+
private class TestHttpMessageHandler : HttpMessageHandler
18+
{
19+
public Func<HttpRequestMessage, HttpResponseMessage> Handler { get; set; } = null!;
20+
21+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
22+
{
23+
return Task.FromResult(Handler(request));
24+
}
25+
}
26+
27+
[Fact]
28+
public async Task CreateReadingNotes_WithNullTags_ShouldNotThrowAndShouldFallbackToMiscellaneous()
29+
{
30+
// Arrange
31+
var handler = new TestHttpMessageHandler();
32+
using var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost/") };
33+
var postNoteClient = new PostNoteClient(client);
34+
35+
var notesList = new List<ReadingNote>
36+
{
37+
new ReadingNote { Title = "Note 1", Tags = null, Category = null }
38+
};
39+
40+
handler.Handler = (req) =>
41+
{
42+
if (req.RequestUri!.PathAndQuery.Contains("GetNextReadingNotesCounter"))
43+
{
44+
return new HttpResponseMessage(HttpStatusCode.OK)
45+
{
46+
Content = new StringContent("708")
47+
};
48+
}
49+
if (req.RequestUri!.PathAndQuery.Contains("GetNotesForSummary/708"))
50+
{
51+
return new HttpResponseMessage(HttpStatusCode.OK)
52+
{
53+
Content = new StringContent(JsonSerializer.Serialize(notesList), System.Text.Encoding.UTF8, "application/json")
54+
};
55+
}
56+
return new HttpResponseMessage(HttpStatusCode.NotFound);
57+
};
58+
59+
// Act
60+
var result = await postNoteClient.CreateReadingNotes();
61+
62+
// Assert
63+
result.Should().NotBeNull();
64+
result.Notes.Should().ContainKey("Miscellaneous");
65+
result.Notes["Miscellaneous"].Should().HaveCount(1);
66+
result.Notes["Miscellaneous"][0].Title.Should().Be("Note 1");
67+
}
68+
69+
[Fact]
70+
public async Task CreateReadingNotes_WithEmptyTags_ShouldNotThrowAndShouldFallbackToMiscellaneous()
71+
{
72+
// Arrange
73+
var handler = new TestHttpMessageHandler();
74+
using var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost/") };
75+
var postNoteClient = new PostNoteClient(client);
76+
77+
var notesList = new List<ReadingNote>
78+
{
79+
new ReadingNote { Title = "Note 2", Tags = "", Category = null }
80+
};
81+
82+
handler.Handler = (req) =>
83+
{
84+
if (req.RequestUri!.PathAndQuery.Contains("GetNextReadingNotesCounter"))
85+
{
86+
return new HttpResponseMessage(HttpStatusCode.OK)
87+
{
88+
Content = new StringContent("708")
89+
};
90+
}
91+
if (req.RequestUri!.PathAndQuery.Contains("GetNotesForSummary/708"))
92+
{
93+
return new HttpResponseMessage(HttpStatusCode.OK)
94+
{
95+
Content = new StringContent(JsonSerializer.Serialize(notesList), System.Text.Encoding.UTF8, "application/json")
96+
};
97+
}
98+
return new HttpResponseMessage(HttpStatusCode.NotFound);
99+
};
100+
101+
// Act
102+
var result = await postNoteClient.CreateReadingNotes();
103+
104+
// Assert
105+
result.Should().NotBeNull();
106+
result.Notes.Should().ContainKey("Miscellaneous");
107+
result.Notes["Miscellaneous"].Should().HaveCount(1);
108+
result.Notes["Miscellaneous"][0].Title.Should().Be("Note 2");
109+
}
110+
111+
[Fact]
112+
public async Task CreateReadingNotes_WithValidTags_ShouldGroupByCategory()
113+
{
114+
// Arrange
115+
var handler = new TestHttpMessageHandler();
116+
using var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost/") };
117+
var postNoteClient = new PostNoteClient(client);
118+
119+
var notesList = new List<ReadingNote>
120+
{
121+
new ReadingNote { Title = "Note 3", Tags = "cloud,dev", Category = null }
122+
};
123+
124+
handler.Handler = (req) =>
125+
{
126+
if (req.RequestUri!.PathAndQuery.Contains("GetNextReadingNotesCounter"))
127+
{
128+
return new HttpResponseMessage(HttpStatusCode.OK)
129+
{
130+
Content = new StringContent("708")
131+
};
132+
}
133+
if (req.RequestUri!.PathAndQuery.Contains("GetNotesForSummary/708"))
134+
{
135+
return new HttpResponseMessage(HttpStatusCode.OK)
136+
{
137+
Content = new StringContent(JsonSerializer.Serialize(notesList), System.Text.Encoding.UTF8, "application/json")
138+
};
139+
}
140+
return new HttpResponseMessage(HttpStatusCode.NotFound);
141+
};
142+
143+
// Act
144+
var result = await postNoteClient.CreateReadingNotes();
145+
146+
// Assert
147+
result.Should().NotBeNull();
148+
result.Notes.Should().ContainKey("Cloud");
149+
result.Notes["Cloud"].Should().HaveCount(1);
150+
result.Notes["Cloud"][0].Title.Should().Be("Note 3");
151+
}
152+
}

src/NoteBookmark.MauiApp/NoteBookmark.MauiApp.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
<ApplicationId>c5m.notebookmark.mauiapp</ApplicationId>
4545

4646
<!-- Versions -->
47-
<ApplicationDisplayVersion>1.3.0</ApplicationDisplayVersion>
48-
<ApplicationVersion>3</ApplicationVersion>
49-
<Version>1.3.0</Version>
47+
<ApplicationDisplayVersion>1.3.1</ApplicationDisplayVersion>
48+
<ApplicationVersion>4</ApplicationVersion>
49+
<Version>1.3.1</Version>
5050

5151
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
5252
<WindowsPackageType>None</WindowsPackageType>

0 commit comments

Comments
 (0)