Files
Chronos/tests/Titano.App.Tests/Jobs/JobListModelTests.cs
T
Claude ffbea7cb74 feat(app): library, jobs, settings, server, welcome and about screens
Local-mode composition root (SQLite index, one JobEngine fed by a single
handler registration list), keyset-paged library with thumbnail cache and
hover scrub, live job list with throttled updates and the shell job bar,
settings with live template validation and encoder self-test, server
connection with trust-on-first-use and DPAPI token, first-run welcome,
About with update check, and --capture screenshots with synthetic data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:26:58 +00:00

188 lines
8.3 KiB
C#

using System.Globalization;
using Titano.App.Jobs;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Jobs;
using Titano.Testing;
namespace Titano.App.Tests.Jobs;
public sealed class JobListModelTests
{
private static readonly DateTimeOffset T0 = new(2026, 9, 28, 10, 0, 0, TimeSpan.Zero);
[Test]
public void OrderIsRunningPausedQueueThenNewestHistory()
{
var model = new JobListModel();
var oldDone = Job(JobState.Done, created: -60, started: -60, finished: -50);
var newDone = Job(JobState.Done, created: -40, started: -40, finished: -10);
var failed = Job(JobState.Failed, created: -30, started: -30, finished: -20);
var queuedLow = Job(JobState.Queued, created: -50, priority: JobPriority.Background);
var queuedOld = Job(JobState.Queued, created: -45);
var queuedNew = Job(JobState.Queued, created: -5);
var queuedUrgent = Job(JobState.Queued, created: -1, priority: JobPriority.User);
var paused = Job(JobState.Paused, created: -35, started: -35);
var runningLate = Job(JobState.Running, created: -8, started: -3);
var runningEarly = Job(JobState.Running, created: -9, started: -7);
foreach (var j in new[] { oldDone, newDone, failed, queuedLow, queuedOld, queuedNew, queuedUrgent, paused, runningLate, runningEarly }) model.Apply(j);
var order = model.Ordered().Select(e => e.Id).ToList();
Assert.SequenceEqual(
new[] { runningEarly, runningLate, paused, queuedUrgent, queuedOld, queuedNew, queuedLow, newDone, failed, oldDone }.Select(j => j.Id),
order);
Assert.Equal(1, model.QueuePosition(queuedUrgent.Id));
Assert.Equal(2, model.QueuePosition(queuedOld.Id));
Assert.Equal(4, model.QueuePosition(queuedLow.Id));
Assert.Equal(0, model.QueuePosition(runningEarly.Id));
Assert.Equal(2, model.RunningCount);
Assert.Equal(4, model.QueuedCount);
}
[Test]
public void FiltersCountAndHideClearedHistory()
{
var model = new JobListModel();
model.Apply(Job(JobState.Running, started: -1));
model.Apply(Job(JobState.Paused, started: -2));
model.Apply(Job(JobState.Queued));
model.Apply(Job(JobState.Done, started: -30, finished: -20));
model.Apply(Job(JobState.Done, started: -5, finished: -2));
model.Apply(Job(JobState.Failed, started: -6, finished: -4));
model.Apply(Job(JobState.Cancelled, started: -7, finished: -6));
Assert.Equal(7, model.CountOf(JobFilter.All));
Assert.Equal(2, model.CountOf(JobFilter.Running));
Assert.Equal(1, model.CountOf(JobFilter.Queued));
Assert.Equal(2, model.CountOf(JobFilter.Done));
Assert.Equal(2, model.CountOf(JobFilter.Failed));
var cleared = T0.AddMinutes(-10);
Assert.Equal(1, model.CountOf(JobFilter.Done, cleared));
Assert.Equal(6, model.Ordered(JobFilter.All, null, cleared).Count);
Assert.Equal(3, model.Ordered(JobFilter.All, null, T0).Count); // only the active jobs remain
}
[Test]
public void SearchMatchesTargetAndKind()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var model = new JobListModel();
model.Apply(Job(JobState.Queued, kind: JobKind.Render, label: "Harbour sunset → 4K"));
model.Apply(Job(JobState.Queued, kind: JobKind.Verify, label: "Archive · 2024"));
Assert.Equal(1, model.Ordered(JobFilter.All, "harbour").Count);
Assert.Equal(1, model.Ordered(JobFilter.All, "verify").Count);
Assert.Equal(2, model.Ordered(JobFilter.All, " ").Count);
Assert.Empty(model.Ordered(JobFilter.All, "comet"));
}
[Test]
public void ProgressMergesIntoRunningJobsOnly()
{
var model = new JobListModel();
var running = Job(JobState.Running, started: -1, done: 10, total: 100);
var queued = Job(JobState.Queued);
model.Apply(running);
model.Apply(queued);
Assert.True(model.Apply(Progress(running.Id, 62, 100, 11.3, TimeSpan.FromMinutes(2))));
Assert.False(model.Apply(Progress(queued.Id, 5, 100)));
Assert.False(model.Apply(Progress(Guid.CreateVersion7(), 5, 100)));
var entry = model[running.Id]!;
Assert.Equal(62, entry.Done);
Assert.Approximately(0.62, entry.Fraction!.Value, 1e-9);
Assert.Equal(11.3, entry.ItemsPerSecond);
Assert.Same(entry, model.Current);
Assert.Equal(0, model[queued.Id]!.Done);
// Finishing drops the live progress and falls back to the stored snapshot.
model.Apply(running with { State = JobState.Done, ProgressDone = 100, Finished = T0 });
Assert.Null(model[running.Id]!.Live);
Assert.Equal(100, model[running.Id]!.Done);
Assert.Null(model.Current);
}
[Test]
public void ALoadedPageNeverOverwritesANewerEvent()
{
var model = new JobListModel();
var queued = Job(JobState.Queued);
var readAt = model.Stamp; // page requested
model.Apply(queued with { State = JobState.Running, Started = T0 }); // event arrives first
Assert.False(model.ApplyLoaded(queued, readAt)); // the stale page row is ignored
Assert.Equal(JobState.Running, model[queued.Id]!.State);
var other = Job(JobState.Done, started: -3, finished: -1);
Assert.True(model.ApplyLoaded(other, model.Stamp));
Assert.True(model.Apply(other with { State = JobState.Queued, Finished = null }));
Assert.Equal(JobState.Queued, model[other.Id]!.State);
}
[Test]
public void CurrentPrefersTheJobWithLiveProgress()
{
var model = new JobListModel();
var a = Job(JobState.Running, started: -10);
var b = Job(JobState.Running, started: -1);
model.Apply(a);
model.Apply(b);
Assert.Equal(a.Id, model.Current!.Id);
model.Apply(Progress(b.Id, 1, 2));
Assert.Equal(b.Id, model.Current!.Id);
}
[Test]
public void ClaimOrderMatchesTheRepository()
{
var older = Job(JobState.Queued, created: -10);
var newer = Job(JobState.Queued, created: -5);
var urgent = Job(JobState.Queued, created: -1, priority: JobPriority.User);
Assert.True(JobListModel.ClaimsBefore(older, newer));
Assert.True(JobListModel.ClaimsBefore(urgent, older));
Assert.False(JobListModel.ClaimsBefore(newer, older));
}
[Test]
public void TextsDescribeTheJob()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var running = new JobEntry(Job(JobState.Running, kind: JobKind.Render, label: "Harbour sunset", started: -3))
{
Live = Progress(Guid.Empty, 2232, 3600, 11.3, TimeSpan.FromMinutes(2)),
};
Assert.Equal("Render · Harbour sunset · 2,232 / 3,600 · 11.3 fps · 2 min left", JobText.Bar(running));
Assert.Equal("2 min", JobText.Left(running, 0));
Assert.Equal("118/s", JobText.Speed(JobKind.Import, 118));
Assert.Equal(string.Empty, JobText.Speed(JobKind.Render, null));
Assert.Equal("this PC", JobText.Where("STUDIO#2", "studio"));
Assert.Equal("render-box", JobText.Where("render-box", "studio"));
var queued = new JobEntry(Job(JobState.Queued));
Assert.Equal("next", JobText.Left(queued, 1));
Assert.Equal("after 2", JobText.Left(queued, 3));
var done = new JobEntry(Job(JobState.Done, started: -10, finished: -7));
Assert.Equal("done · 3:00", JobText.State(done));
}
private static Job Job(JobState state, int created = 0, int? started = null, int? finished = null, JobPriority priority = JobPriority.Normal,
JobKind kind = JobKind.Proxies, string? label = null, long done = 0, long total = 0) => new()
{
Id = Guid.CreateVersion7(),
Kind = kind,
State = state,
Priority = priority,
TargetLabel = label,
ProgressDone = done,
ProgressTotal = total,
Created = T0.AddMinutes(created),
Started = started is { } s ? T0.AddMinutes(s) : null,
Finished = finished is { } f ? T0.AddMinutes(f) : null,
};
private static JobProgressInfo Progress(Guid id, long done, long total, double? rate = null, TimeSpan? eta = null) =>
new(id, JobKind.Render, null, done, total, rate, eta, null);
}