Skip to content

Commit 322502f

Browse files
committed
added tests and fixed URLs
1 parent 6ce536b commit 322502f

12 files changed

Lines changed: 581 additions & 543 deletions

File tree

ContentLoader/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105

106106
tip.Content = tip.Content.Replace(
107107
$"]({localImagePath}",
108-
$"](/images/{image.ImageId}/original"
108+
$"](/article-images/{image.ImageId}/original.{Path.GetExtension(image.FileName).TrimStart('.')}"
109109
);
110110
}
111111

CopilotThatJawn.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContentLoader", "ContentLoa
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{6DE4B66D-F85E-4DCE-BAE3-85F5AE2531A0}"
1515
EndProject
16+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}"
17+
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web.Tests", "Tests\Web.Tests\Web.Tests.csproj", "{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}"
19+
EndProject
1620
Global
1721
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1822
Debug|Any CPU = Debug|Any CPU
@@ -83,8 +87,23 @@ Global
8387
{6DE4B66D-F85E-4DCE-BAE3-85F5AE2531A0}.Release|x64.Build.0 = Release|Any CPU
8488
{6DE4B66D-F85E-4DCE-BAE3-85F5AE2531A0}.Release|x86.ActiveCfg = Release|Any CPU
8589
{6DE4B66D-F85E-4DCE-BAE3-85F5AE2531A0}.Release|x86.Build.0 = Release|Any CPU
90+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
91+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Debug|Any CPU.Build.0 = Debug|Any CPU
92+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Debug|x64.ActiveCfg = Debug|Any CPU
93+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Debug|x64.Build.0 = Debug|Any CPU
94+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Debug|x86.ActiveCfg = Debug|Any CPU
95+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Debug|x86.Build.0 = Debug|Any CPU
96+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Release|Any CPU.ActiveCfg = Release|Any CPU
97+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Release|Any CPU.Build.0 = Release|Any CPU
98+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Release|x64.ActiveCfg = Release|Any CPU
99+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Release|x64.Build.0 = Release|Any CPU
100+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Release|x86.ActiveCfg = Release|Any CPU
101+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102}.Release|x86.Build.0 = Release|Any CPU
86102
EndGlobalSection
87103
GlobalSection(SolutionProperties) = preSolution
88104
HideSolutionNode = FALSE
89105
EndGlobalSection
106+
GlobalSection(NestedProjects) = preSolution
107+
{0CA4934D-8348-4E35-BA0B-1AD4D3AE0102} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
108+
EndGlobalSection
90109
EndGlobal

Shared/ImageUploadHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ private static async Task UploadImageAsync(BlobContainerClient containerClient,
7777
await using var fileStream = File.OpenRead(filePath);
7878
await blobClient.UploadAsync(fileStream, new BlobUploadOptions
7979
{
80+
HttpHeaders = new BlobHttpHeaders
81+
{
82+
ContentType = imageInfo.ContentType
83+
},
8084
Metadata = new Dictionary<string, string>
8185
{
8286
["ImageId"] = imageInfo.ImageId,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Markdig;
2+
using Markdig.Renderers;
3+
using Markdig.Renderers.Html;
4+
using Markdig.Syntax;
5+
using Markdig.Syntax.Inlines;
6+
using Moq;
7+
using Web.Extensions;
8+
using Web.Services;
9+
using Xunit;
10+
11+
namespace Web.Tests.Extensions;
12+
13+
public class ImageUrlRewriterExtensionTests
14+
{
15+
[Fact]
16+
public void Setup_ReplacesDefaultImageRenderer()
17+
{
18+
// Arrange
19+
20+
var extension = new ImageUrlRewriterExtension();
21+
var pipeline = new MarkdownPipelineBuilder().Build();
22+
var renderer = new HtmlRenderer(new StringWriter());
23+
24+
// Act
25+
extension.Setup(pipeline, renderer);
26+
27+
// Assert
28+
var imageRenderer = renderer.ObjectRenderers.FindExact<Markdig.Renderers.Html.Inlines.LinkInlineRenderer>();
29+
Assert.Null(imageRenderer); // Original renderer should be removed
30+
31+
var customRenderer = renderer.ObjectRenderers.Find(r => r.GetType().Name == "CustomImageRenderer");
32+
Assert.NotNull(customRenderer); // Custom renderer should be added
33+
}
34+
35+
[Fact]
36+
public void Render_AddsResponsiveImageClasses_WhenRenderingImage()
37+
{
38+
// Arrange
39+
var extension = new ImageUrlRewriterExtension();
40+
41+
// Create a markdown pipeline with our extension
42+
var pipeline = new MarkdownPipelineBuilder()
43+
.Use(extension)
44+
.Build();
45+
46+
// Create a markdown document with an image
47+
var markdown = "![Alt text](image.jpg \"Image title\")";
48+
49+
// Act
50+
var result = Markdown.ToHtml(markdown, pipeline);
51+
52+
// Assert
53+
Assert.Contains("class=\"img-fluid\"", result);
54+
Assert.Contains("loading=\"lazy\"", result);
55+
Assert.Contains("title=\"Image title\"", result);
56+
}
57+
}

Tests/Web.Tests/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copilot That Jawn - Unit Tests
2+
3+
This directory contains unit tests for the Copilot That Jawn web application using XUnit framework.
4+
5+
## Running Tests
6+
7+
### From Command Line
8+
9+
You can run the tests using the .NET CLI:
10+
11+
```powershell
12+
# Run all tests
13+
dotnet test Tests/Web.Tests
14+
15+
# Run specific test class
16+
dotnet test Tests/Web.Tests --filter "FullyQualifiedName~Web.Tests.Extensions.ImageUrlRewriterExtensionTests"
17+
18+
# Run with detailed output
19+
dotnet test Tests/Web.Tests -v n
20+
```
21+
22+
### From Visual Studio
23+
24+
1. Open the Test Explorer window (Test > Test Explorer)
25+
2. Click "Run All Tests" or right-click on specific tests to run them
26+
27+
## Test Organization
28+
29+
Tests are organized to mirror the structure of the main project:
30+
31+
- `Extensions/` - Tests for extension methods
32+
- `Services/` - Tests for services
33+
- `Controllers/` - Tests for controllers
34+
- `Pages/` - Tests for Razor Pages
35+
- `Components/` - Tests for components
36+
37+
## Best Practices
38+
39+
- Each test class should focus on testing a single component
40+
- Use meaningful test names that describe what is being tested
41+
- Follow the Arrange-Act-Assert pattern
42+
- Use Moq for mocking dependencies
43+
- Keep tests independent of each other
44+
45+
## Adding New Tests
46+
47+
When adding new tests:
48+
49+
1. Create a test class in the appropriate subdirectory
50+
2. Name the test class after the class being tested, with a "Tests" suffix
51+
3. Group related tests within the same test class
52+
4. Use [Fact] for simple tests and [Theory] with [InlineData] for parameterized tests
53+
54+
## Integration with .NET Aspire
55+
56+
When testing components that use .NET Aspire services, use the Aspire testing tools for integration tests involving multiple services.

Tests/Web.Tests/Web.Tests.csproj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
<IsTestProject>true</IsTestProject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
13+
<PackageReference Include="xunit" Version="2.7.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="6.0.2">
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
21+
</PackageReference>
22+
<PackageReference Include="Moq" Version="4.20.70" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\Web\Web.csproj" />
27+
</ItemGroup>
28+
29+
</Project>

0 commit comments

Comments
 (0)