Tests write serialized queue and watch-memory bytes directly; the app persists through its coalescing writer. Single-instance hand-over no longer returns an unused flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
84 lines
3.3 KiB
C#
84 lines
3.3 KiB
C#
using System.IO;
|
|
using Titano.Testing;
|
|
using Titano.Uploader.Sources;
|
|
using Titano.Uploader.Watching;
|
|
|
|
namespace Titano.App.Tests.Uploader;
|
|
|
|
public sealed class ProcessedFileMemoryTests(TestContext context)
|
|
{
|
|
private static readonly DateTime Written = new(2026, 9, 28, 16, 2, 11, DateTimeKind.Utc);
|
|
|
|
[Test]
|
|
public void RemembersPathSizeAndWriteTime()
|
|
{
|
|
var root = Path.Combine(Path.GetTempPath(), "roof");
|
|
var photo = Path.Combine(root, "100CANON", "IMG_0413.CR2");
|
|
var memory = new ProcessedFileMemory();
|
|
Assert.False(memory.Contains(root, photo, 25_000_000, Written));
|
|
|
|
memory.Add(root, photo, 25_000_000, Written);
|
|
Assert.True(memory.IsDirty);
|
|
Assert.True(memory.Contains(root, photo, 25_000_000, Written));
|
|
Assert.False(memory.Contains(root, photo, 25_000_001, Written), "a resized file is new");
|
|
Assert.False(memory.Contains(root, photo, 25_000_000, Written.AddSeconds(1)), "a rewritten file is new");
|
|
Assert.False(memory.Contains(Path.Combine(Path.GetTempPath(), "garden"), photo, 25_000_000, Written), "folders are separate");
|
|
}
|
|
|
|
[Test]
|
|
public void PruneForgetsFilesNoLongerInTheFolder()
|
|
{
|
|
var root = Path.Combine(Path.GetTempPath(), "roof");
|
|
var kept = Path.Combine(root, "a.jpg");
|
|
var gone = Path.Combine(root, "b.jpg");
|
|
var memory = new ProcessedFileMemory();
|
|
memory.Add(root, kept, 1, Written);
|
|
memory.Add(root, gone, 2, Written);
|
|
|
|
memory.Prune(root, [kept]);
|
|
Assert.True(memory.Contains(root, kept, 1, Written));
|
|
Assert.False(memory.Contains(root, gone, 2, Written));
|
|
}
|
|
|
|
[Test]
|
|
public void RoundTripKeepsFilesAndTheLastSession()
|
|
{
|
|
var dir = context.CreateTempDirectory();
|
|
var root = Path.Combine(dir, "cam");
|
|
var photo = Path.Combine(root, "IMG_0001.JPG");
|
|
var memory = new ProcessedFileMemory();
|
|
memory.Add(root, photo, 4_200_000, Written);
|
|
memory.SetSession(root, new SessionContinuation("1a2b3c4d-20260928-180000", new DateTime(2026, 9, 28, 18, 40, 0)));
|
|
|
|
var path = Path.Combine(dir, "uploader-watch.json");
|
|
File.WriteAllBytes(path, memory.Serialize());
|
|
Assert.False(memory.IsDirty);
|
|
var restored = ProcessedFileMemory.Load(path);
|
|
|
|
Assert.True(restored.Contains(root, photo, 4_200_000, Written));
|
|
Assert.Equal(new SessionContinuation("1a2b3c4d-20260928-180000", new DateTime(2026, 9, 28, 18, 40, 0)), restored.GetSession(root));
|
|
Assert.False(restored.IsDirty);
|
|
}
|
|
|
|
[Test]
|
|
public void RemovedFolderIsForgotten()
|
|
{
|
|
var root = Path.Combine(Path.GetTempPath(), "roof");
|
|
var memory = new ProcessedFileMemory();
|
|
memory.Add(root, Path.Combine(root, "a.jpg"), 1, Written);
|
|
memory.SetSession(root, new SessionContinuation("k", Written));
|
|
memory.RemoveFolder(root);
|
|
Assert.False(memory.Contains(root, Path.Combine(root, "a.jpg"), 1, Written));
|
|
Assert.Null(memory.GetSession(root));
|
|
}
|
|
|
|
[Test]
|
|
public void CorruptFileGivesAnEmptyMemory()
|
|
{
|
|
var path = Path.Combine(context.CreateTempDirectory(), "uploader-watch.json");
|
|
File.WriteAllText(path, "[1,2");
|
|
var memory = ProcessedFileMemory.Load(path);
|
|
Assert.False(memory.Contains("x", "x/a.jpg", 1, Written));
|
|
}
|
|
}
|