From dbddf9da9ce1547f4ac6f819f14cbdc620ac8ed2 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Fri, 30 Oct 2026 20:36:07 -0700 Subject: [PATCH 1/2] Keep the Seeded State When a Renamed File Has a Sidecar A rename seeds the new SidecarFile with the in-memory state plus FileReNamed, and the sidecar load replaced it. OR the seeded flags back in after the load settles the state. Co-Authored-By: Claude Sonnet 5.5 --- PlexCleaner/SidecarFile.cs | 4 + PlexCleanerTests/SidecarRenameStateTests.cs | 107 ++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 PlexCleanerTests/SidecarRenameStateTests.cs diff --git a/PlexCleaner/SidecarFile.cs b/PlexCleaner/SidecarFile.cs index 2f38e32e..9ef15b4f 100644 --- a/PlexCleaner/SidecarFile.cs +++ b/PlexCleaner/SidecarFile.cs @@ -223,11 +223,15 @@ public bool Open(bool modified = false) if (_sidecarFileInfo.Exists) { // Sidecar file exists, read and verify it matches media file + // Keep the state seeded by the caller, the sidecar load replaces it + StatesType seeded = State; if (!Read(out bool current)) { // Failed to read, create it return Create(); } + State |= seeded; + // Media file changed, force an update if (!current) { diff --git a/PlexCleanerTests/SidecarRenameStateTests.cs b/PlexCleanerTests/SidecarRenameStateTests.cs new file mode 100644 index 00000000..3c008681 --- /dev/null +++ b/PlexCleanerTests/SidecarRenameStateTests.cs @@ -0,0 +1,107 @@ +using System.Text.Json.Nodes; +using AwesomeAssertions; +using PlexCleaner; +using ptr727.Utilities; +using Xunit; + +namespace PlexCleanerTests; + +[Collection("Sequential")] +public sealed class SidecarRenameStateTests : IDisposable +{ + private const SidecarFile.StatesType Seeded = + SidecarFile.StatesType.ReMuxed | SidecarFile.StatesType.FileReNamed; + + private readonly string _directory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + private readonly ToolsOptions1 _toolsOptions = Program.Config.ToolsOptions; + private readonly MediaToolInfo _ffProbeInfo = Tools.FfProbe.Info; + private readonly MediaToolInfo _mkvMergeInfo = Tools.MkvMerge.Info; + private readonly MediaToolInfo _mediaInfoInfo = Tools.MediaInfo.Info; + + public SidecarRenameStateTests() + { + _ = Directory.CreateDirectory(_directory); + Program.Config.ToolsOptions = new ToolsOptions1 + { + UseSystem = false, + RootRelative = false, + RootPath = _directory, + }; + } + + public void Dispose() + { + Program.Config.ToolsOptions = _toolsOptions; + Tools.FfProbe.Info = _ffProbeInfo; + Tools.MkvMerge.Info = _mkvMergeInfo; + Tools.MediaInfo.Info = _mediaInfoInfo; + Directory.Delete(_directory, true); + } + + private static JsonNode ReadState(string sidecarPath, out int state) + { + JsonNode node = JsonNode.Parse(File.ReadAllText(sidecarPath))!; + state = node["State"]!.GetValue(); + return node; + } + + [Fact] + public void Open_SeededStateOverMatchingSidecar_KeepsBothStates() + { + // name.MKV and name.mkv share name.PlexCleaner and the hash matches + string mediaFile = FakeTools.CopyUnverifiedSample(_directory); + string sidecarPath = Path.Combine(_directory, "name.PlexCleaner"); + JsonNode stored = JsonNode.Parse(File.ReadAllText(sidecarPath))!; + stored["State"] = (int)SidecarFile.StatesType.Verified; + File.WriteAllText(sidecarPath, stored.ToJsonString()); + string renamed = Path.Combine(_directory, "name.MKV"); + File.Move(mediaFile, renamed); + + SidecarFile sidecarFile = new(renamed) { State = Seeded }; + _ = sidecarFile.Open().Should().BeTrue(); + + SidecarFile.StatesType expected = Seeded | SidecarFile.StatesType.Verified; + _ = sidecarFile.State.Should().Be(expected); + _ = ReadState(sidecarPath, out int written); + _ = written.Should().Be((int)expected); + } + + [Fact] + public void Open_SeededStateOverStaleSidecar_KeepsSeededAndMarksModified() + { + // name.avi remuxed to name.mkv meets a stale name.PlexCleaner + string mediaFile = FakeTools.CopyUnverifiedSample(_directory); + string sidecarPath = Path.Combine(_directory, "name.PlexCleaner"); + JsonNode stored = JsonNode.Parse(File.ReadAllText(sidecarPath))!; + stored["State"] = (int)SidecarFile.StatesType.Verified; + File.WriteAllText(sidecarPath, stored.ToJsonString()); + File.AppendAllText(mediaFile, "changed"); + + // Tools return the stored sample data so the media update can run + foreach ( + (MediaTool tool, string key) in new (MediaTool, string)[] + { + (Tools.MediaInfo, "MediaInfoData"), + (Tools.MkvMerge, "MkvMergeInfoData"), + (Tools.FfProbe, "FfProbeInfoData"), + } + ) + { + string dataFile = Path.Combine(_directory, key + ".json"); + File.WriteAllText( + dataFile, + StringCompression.Decompress(stored[key]!.GetValue()) + ); + FakeTools.WriteScript(tool.GetToolPath(), $"cat \"{dataFile}\"\n"); + } + + SidecarFile sidecarFile = new(mediaFile) { State = Seeded }; + _ = sidecarFile.Open(true).Should().BeTrue(); + + // The stale Verified flag is cleared, the seeded flags are kept + SidecarFile.StatesType expected = Seeded | SidecarFile.StatesType.FileModified; + _ = sidecarFile.State.Should().Be(expected); + _ = ReadState(sidecarPath, out int written); + _ = written.Should().Be((int)expected); + } +} From 34b38981e6a78233ce7f6d00b4725e63cace2027 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Fri, 30 Oct 2026 20:42:58 -0700 Subject: [PATCH 2/2] Make the Rename State Tests Portable and Lint Clean Co-Authored-By: Claude Sonnet 5.5 --- PlexCleanerTests/SidecarRenameStateTests.cs | 23 +++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/PlexCleanerTests/SidecarRenameStateTests.cs b/PlexCleanerTests/SidecarRenameStateTests.cs index 3c008681..62c00fbc 100644 --- a/PlexCleanerTests/SidecarRenameStateTests.cs +++ b/PlexCleanerTests/SidecarRenameStateTests.cs @@ -38,38 +38,36 @@ public void Dispose() Directory.Delete(_directory, true); } - private static JsonNode ReadState(string sidecarPath, out int state) - { - JsonNode node = JsonNode.Parse(File.ReadAllText(sidecarPath))!; - state = node["State"]!.GetValue(); - return node; - } + private static int ReadState(string sidecarPath) => + JsonNode.Parse(File.ReadAllText(sidecarPath))!["State"]!.GetValue(); [Fact] public void Open_SeededStateOverMatchingSidecar_KeepsBothStates() { - // name.MKV and name.mkv share name.PlexCleaner and the hash matches + // The sidecar name ignores the extension case, so the hash matches string mediaFile = FakeTools.CopyUnverifiedSample(_directory); string sidecarPath = Path.Combine(_directory, "name.PlexCleaner"); JsonNode stored = JsonNode.Parse(File.ReadAllText(sidecarPath))!; stored["State"] = (int)SidecarFile.StatesType.Verified; File.WriteAllText(sidecarPath, stored.ToJsonString()); string renamed = Path.Combine(_directory, "name.MKV"); - File.Move(mediaFile, renamed); + string tempName = Path.ChangeExtension(mediaFile, ".tmp7"); + File.Move(mediaFile, tempName); + File.Move(tempName, renamed); SidecarFile sidecarFile = new(renamed) { State = Seeded }; _ = sidecarFile.Open().Should().BeTrue(); SidecarFile.StatesType expected = Seeded | SidecarFile.StatesType.Verified; _ = sidecarFile.State.Should().Be(expected); - _ = ReadState(sidecarPath, out int written); - _ = written.Should().Be((int)expected); + _ = ReadState(sidecarPath).Should().Be((int)expected); } [Fact] public void Open_SeededStateOverStaleSidecar_KeepsSeededAndMarksModified() { - // name.avi remuxed to name.mkv meets a stale name.PlexCleaner + Assert.SkipWhen(OperatingSystem.IsWindows(), "The fake tools are shell scripts"); + // A remuxed file meets the stale sidecar of the file it replaced string mediaFile = FakeTools.CopyUnverifiedSample(_directory); string sidecarPath = Path.Combine(_directory, "name.PlexCleaner"); JsonNode stored = JsonNode.Parse(File.ReadAllText(sidecarPath))!; @@ -101,7 +99,6 @@ public void Open_SeededStateOverStaleSidecar_KeepsSeededAndMarksModified() // The stale Verified flag is cleared, the seeded flags are kept SidecarFile.StatesType expected = Seeded | SidecarFile.StatesType.FileModified; _ = sidecarFile.State.Should().Be(expected); - _ = ReadState(sidecarPath, out int written); - _ = written.Should().Be((int)expected); + _ = ReadState(sidecarPath).Should().Be((int)expected); } }