feat(uploader): watch folders with stable-file detection and processed-file memory
Polls each folder at its interval with FileSystemWatcher hints on local disks (network shares are polled only); a photo is queued once its size and write time are unchanged in two consecutive checks. Files the server confirmed are remembered by path, size and write time in uploader-watch.json together with each folder's last session. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
5 files changed
+559
No files matched your search
@@ -0,0 +1,83 @@
|
||||
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");
|
||||
memory.Save(path);
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user