Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/apps-download-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Apps download checks

on:
pull_request:
branches: [master]
paths:
- "Optimizer/AppDownload*.cs"
- "Optimizer/Models/AppInfo.cs"
- "Optimizer/Forms/MainForm*.cs"
- "Optimizer/Optimizer.csproj"
- "Optimizer/packages.config"
- "Optimizer.sln"
- "Optimizer.Tests/**"
- ".github/workflows/apps-download-tests.yml"
push:
branches: [master]
paths:
- "Optimizer/AppDownload*.cs"
- "Optimizer/Models/AppInfo.cs"
- "Optimizer/Forms/MainForm*.cs"
- "Optimizer/Optimizer.csproj"
- "Optimizer/packages.config"
- "Optimizer.sln"
- "Optimizer.Tests/**"
- ".github/workflows/apps-download-tests.yml"

permissions:
contents: read

jobs:
test:
runs-on: windows-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-dotnet@v5
with:
dotnet-version: "8.0.x"
- uses: microsoft/setup-msbuild@v3
- uses: NuGet/setup-nuget@v4
- name: Restore application packages
run: nuget restore Optimizer.sln -NonInteractive
- name: Build application
run: msbuild Optimizer.sln /t:Rebuild /m /p:Configuration=Release '/p:Platform=Any CPU' /verbosity:minimal
- name: Run isolated download tests
run: dotnet test Optimizer.Tests/Optimizer.Tests.csproj --configuration Release
39 changes: 39 additions & 0 deletions Optimizer.Tests/AppDownloadProgressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Xunit;

namespace Optimizer.Tests {
public sealed class AppDownloadProgressTests {
[Theory]
[InlineData(0L, -1L)]
[InlineData(1L, -1L)]
[InlineData(33554432L, -1L)]
[InlineData(long.MaxValue, -1L)]
[InlineData(0L, 0L)]
[InlineData(1L, 0L)]
[InlineData(1L, long.MinValue)]
public void MissingOrInvalidLengthIsIndeterminate(long received, long total)
{
Assert.Null(AppDownloadProgress.Percentage(received, total));
}

[Theory]
[InlineData(0L, 100L, 0)]
[InlineData(-1L, 100L, 0)]
[InlineData(long.MinValue, 100L, 0)]
[InlineData(1L, 100L, 1)]
[InlineData(1L, 3L, 33)]
[InlineData(50L, 100L, 50)]
[InlineData(99L, 100L, 99)]
[InlineData(100L, 100L, 100)]
[InlineData(101L, 100L, 100)]
[InlineData(long.MaxValue, 1L, 100)]
[InlineData(long.MaxValue, long.MaxValue, 100)]
[InlineData(long.MaxValue - 1, long.MaxValue, 99)]
[InlineData(10737418240L, 21474836480L, 50)]
public void KnownLengthProducesABoundedPercentage(long received, long total, int expected)
{
int? percentage = AppDownloadProgress.Percentage(received, total);
Assert.Equal(expected, percentage);
Assert.InRange(percentage.Value, 0, 100);
}
}
}
178 changes: 178 additions & 0 deletions Optimizer.Tests/AppDownloadStateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;

namespace Optimizer.Tests {
public sealed class AppDownloadStateTests {
static AppInfo App(string title)
{
return new AppInfo { Title = title, Tag = "c" + title, Link = "https://example.invalid/x86", Link64 = "https://example.invalid/x64" };
}

static AppDownloadState.Batch Begin(AppDownloadState state)
{
return state.TryBeginDownload(new[] { App("One") }, Path.GetTempPath(), true, false);
}

[Fact]
public void IdleStateIsNotBusy()
{
AppDownloadState state = new AppDownloadState();
Assert.False(state.IsBusy);
Assert.False(state.IsLoadingFeed);
Assert.False(state.IsDownloading);
}

[Fact]
public void FeedLoadBlocksAnotherRefreshAndDownloads()
{
AppDownloadState state = new AppDownloadState();
Assert.True(state.TryBeginFeedLoad());
Assert.True(state.IsLoadingFeed);
Assert.True(state.IsBusy);
Assert.False(state.TryBeginFeedLoad());
Assert.Null(Begin(state));
Assert.False(state.IsDownloading);
}

[Fact]
public void FeedCompletionAllowsDownloadsAgain()
{
AppDownloadState state = new AppDownloadState();
Assert.True(state.TryBeginFeedLoad());
state.EndFeedLoad();
Assert.False(state.IsLoadingFeed);
Assert.NotNull(Begin(state));
}

[Fact]
public void DownloadBlocksRefreshAndReentry()
{
AppDownloadState state = new AppDownloadState();
Assert.NotNull(Begin(state));
Assert.True(state.IsDownloading);
Assert.True(state.IsBusy);
Assert.False(state.TryBeginFeedLoad());
Assert.Null(Begin(state));
}

[Fact]
public void DownloadCompletionRestoresIdleState()
{
AppDownloadState state = new AppDownloadState();
AppDownloadState.Batch batch = Begin(state);
state.EndDownload(batch);
Assert.False(state.IsBusy);
Assert.True(state.TryBeginFeedLoad());
}

[Fact]
public void EmptySelectionDoesNotStartABatch()
{
AppDownloadState state = new AppDownloadState();
Assert.Null(state.TryBeginDownload(new AppInfo[0], Path.GetTempPath(), true, false));
Assert.False(state.IsBusy);
Assert.True(state.TryBeginFeedLoad());
}

[Fact]
public void InvalidInputsDoNotLeaveTheDownloaderBusy()
{
AppDownloadState state = new AppDownloadState();
Assert.Throws<ArgumentNullException>(() => state.TryBeginDownload(null, Path.GetTempPath(), true, false));
Assert.Throws<ArgumentException>(() => state.TryBeginDownload(new[] { App("One") }, string.Empty, true, false));
Assert.False(state.IsBusy);
Assert.NotNull(Begin(state));
}

[Fact]
public void BatchCapturesSelectionAndAppMetadata()
{
AppInfo app = App("One");
List<AppInfo> selected = new List<AppInfo> { app };
AppDownloadState state = new AppDownloadState();
AppDownloadState.Batch batch = state.TryBeginDownload(selected, Path.GetTempPath(), true, false);

selected.Clear();
app.Title = "Changed";
app.Tag = "Changed";
app.Link = "Changed";
app.Link64 = "Changed";

AppInfo captured = Assert.Single(batch.Apps);
Assert.Equal("One", captured.Title);
Assert.Equal("cOne", captured.Tag);
Assert.Equal("https://example.invalid/x86", captured.Link);
Assert.Equal("https://example.invalid/x64", captured.Link64);
Assert.NotSame(app, captured);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void BatchCapturesArchitectureInstallationAndCanonicalFolder(bool prefer64, bool autoInstall)
{
string folder = Path.Combine(Path.GetTempPath(), "Optimizer fixture", "..", "Downloads");
AppDownloadState.Batch batch = new AppDownloadState().TryBeginDownload(new[] { App("One") }, folder, prefer64, autoInstall);
Assert.Equal(Path.GetFullPath(folder), batch.Folder);
Assert.Equal(prefer64, batch.Prefer64);
Assert.Equal(autoInstall, batch.AutoInstall);
}

[Fact]
public void BatchSelectionCannotBeExtendedAfterStarting()
{
AppDownloadState.Batch batch = Begin(new AppDownloadState());
IList<AppInfo> apps = Assert.IsAssignableFrom<IList<AppInfo>>(batch.Apps);
Assert.Throws<NotSupportedException>(() => apps.Add(App("Two")));
Assert.Single(batch.Apps);
}

[Fact]
public void OldCompletionCannotUnlockANewerBatch()
{
AppDownloadState state = new AppDownloadState();
AppDownloadState.Batch first = Begin(state);
state.EndDownload(first);
AppDownloadState.Batch second = Begin(state);
state.EndDownload(first);
state.EndDownload(null);

Assert.True(state.IsDownloading);
Assert.False(state.TryBeginFeedLoad());
state.EndDownload(second);
Assert.False(state.IsBusy);
}

[Fact]
public void EndingAFeedLoadCannotUnlockAnActiveDownload()
{
AppDownloadState state = new AppDownloadState();
AppDownloadState.Batch batch = Begin(state);
state.EndFeedLoad();
Assert.True(state.IsDownloading);
Assert.False(state.TryBeginFeedLoad());
Assert.Null(Begin(state));
state.EndDownload(batch);
Assert.False(state.IsBusy);
}

[Fact]
public void FailedSnapshotEnumerationDoesNotStartAPartialBatch()
{
AppDownloadState state = new AppDownloadState();
Assert.Throws<IOException>(() => state.TryBeginDownload(InterruptedSelection(), Path.GetTempPath(), true, false));
Assert.False(state.IsBusy);
Assert.NotNull(Begin(state));
}

static IEnumerable<AppInfo> InterruptedSelection()
{
yield return App("One");
throw new IOException("Fixture selection failure");
}
}
}
22 changes: 22 additions & 0 deletions Optimizer.Tests/Optimizer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<LangVersion>7.3</LangVersion>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Windows.Forms" />
<Compile Include="..\Optimizer\AppDownloadProgress.cs" Link="Production\AppDownloadProgress.cs" />
<Compile Include="..\Optimizer\AppDownloadState.cs" Link="Production\AppDownloadState.cs" />
<Compile Include="..\Optimizer\Models\AppInfo.cs" Link="Production\AppInfo.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions Optimizer.Tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# App download regression tests

Run on Windows with the .NET Framework 4.8 Developer Pack and .NET SDK 8 or newer:

```powershell
dotnet test .\Optimizer.Tests\Optimizer.Tests.csproj --configuration Release
```

The tests compile the production download-state and progress helpers directly. They do not launch Optimizer, run installers, apply optimizations, or access system configuration. Feed URLs in the state tests use the reserved `.invalid` domain and are never requested.

Coverage includes unknown or zero content lengths, large byte counts, percentage bounds, exclusive feed/download operations, stable batch inputs, and state recovery after invalid inputs. Loopback-only HTTP tests verify that a real chunked download reports unknown-length progress on a Windows Forms synchronization context, including cancellation and timeout cleanup. Downloaded bytes are inert test data in a temporary folder, never executable content.

These are helper and HTTP-contract tests, not an end-to-end test of MainForm or the installer workflow.
Loading