using System.Security.Cryptography; using System.Text; using Chronos.Core.Archiving; using Chronos.Core.Project; using Chronos.Data.Workspaces; namespace Chronos.Data.Tests.Homes; /// Workspaces and caches laid out as 1.0 left them, for the sorting into homes (owner review 5, #66). internal static class HomeFixtures { public const string Nas = "https://nas.local:8443"; public const string NasByIp = "https://192.168.1.5:8443"; public const string Other = "https://other.example:8443"; /// A working state of written into <root>/<id> with some media, versions and a render. public static Guid Project(string root, BundleHome home, string name = "Main", DateTimeOffset? saved = null, Guid? id = null, int proxies = 3) { var projectId = id ?? Guid.CreateVersion7(); var folder = Path.Combine(root, projectId.ToString("N")); Directory.CreateDirectory(folder); var archive = home.IsArchive ? new BundleArchive(home.ArchiveId ?? Guid.CreateVersion7(), "This PC", @"D:\ChronosArchive") : new BundleArchive(Data.Projects.ServerProjectHome.ArchiveIdOf(home.Server!), "Studio NAS", null); var bundle = new ProjectBundle(archive, new BundleSequence(home.SequenceId, "2026/2026-11-15 Harbour", "Harbour", proxies), new ProjectSettings(), saved ?? DateTimeOffset.UtcNow) { Workspace = new BundleWorkspace(projectId, name, true, home, null, new BundleMedia(BundleMedia.Proxies, 1)), }; bundle.Save(Path.Combine(folder, WorkspaceProject.BundleFile)); var media = Path.Combine(folder, WorkspaceProject.MediaFolder, WorkspaceProject.ProxiesFolder); Directory.CreateDirectory(media); for (var i = 0; i < proxies; i++) File.WriteAllBytes(Path.Combine(media, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(projectId, i, 500 + i)); Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.VersionsFolder)); File.WriteAllText(Path.Combine(folder, WorkspaceProject.VersionsFolder, "1-" + Guid.CreateVersion7().ToString("N") + ".json"), "{}"); Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.ExportsFolder)); File.WriteAllBytes(Path.Combine(folder, WorkspaceProject.ExportsFolder, "render.mp4"), Bytes(projectId, 99, 2048)); return projectId; } public static BundleHome Local() => new(BundleHome.ArchiveKind, Guid.CreateVersion7(), Guid.CreateVersion7()); public static BundleHome Server(string key) => new(BundleHome.ServerKind, Guid.CreateVersion7(), null, key); /// A 1.0 workspace: its manifest (format 1) and the folder. public static string LegacyWorkspace(string parent) { var root = Path.Combine(parent, "workspace"); _ = new WorkspaceStore(root); return root; } /// A trash entry of holding a working state, as a removal leaves it. public static string TrashEntry(string root, Guid projectId, BundleHome home) { var trash = Path.Combine(root, WorkspaceTrash.FolderName); var staging = Path.Combine(trash, "staging"); Directory.CreateDirectory(staging); Project(staging, home, id: projectId, proxies: 1); var entry = Path.Combine(trash, projectId.ToString("N") + "-638000000000000000"); Directory.Move(Path.Combine(staging, projectId.ToString("N")), entry); Directory.Delete(staging); return entry; } /// The 1.0 proxy cache of 's sequence . public static string LegacyProxies(string cache, string key, Guid remoteId, int frames = 2) { var folder = Data.Sync.ProxyCache.LegacyFolder(Path.Combine(cache, "proxies"), key, remoteId); Directory.CreateDirectory(folder); File.WriteAllText(Path.Combine(folder, "remote.json"), "{}"); for (var i = 0; i < frames; i++) File.WriteAllBytes(Path.Combine(folder, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(remoteId, i, 300)); return folder; } /// A live lease on a project folder, as another Chronos working on the same workspace holds it. public static LeaseFile Hold(string projectFolder) => LeaseFile.TryAcquire(projectFolder, ".lock", "workspace@other", TimeSpan.FromMinutes(5)) ?? throw new InvalidOperationException("The lease is held."); /// /// The SHA-256 of every file under by its name relative to the project or entry it belongs to, /// whatever home folder holds it: the same before and after a sorting that only renames (the test-only proof). /// public static SortedDictionary Manifest(string root) { var manifest = new SortedDictionary(StringComparer.Ordinal); if (!Directory.Exists(root)) return manifest; foreach (var file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)) { var relative = Path.GetRelativePath(root, file).Replace('\\', '/'); var name = Path.GetFileName(file); if (name is WorkspaceStore.ManifestFile or "server.json" && !relative.Contains('/', StringComparison.Ordinal)) continue; if (name is WorkspaceStore.ManifestFile or "server.json" && IsHomeLevel(relative)) continue; manifest[Strip(relative)] = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(file))) + "|" + new FileInfo(file).Length; } return manifest; } /// A path without the home folders it lies in (local/, servers/<folder>/). private static string Strip(string relative) { if (relative.StartsWith("local/", StringComparison.Ordinal)) return relative["local/".Length..]; if (relative.StartsWith("servers/", StringComparison.Ordinal)) { var rest = relative["servers/".Length..]; var slash = rest.IndexOf('/', StringComparison.Ordinal); return slash < 0 ? rest : rest[(slash + 1)..]; } return relative; } private static bool IsHomeLevel(string relative) => relative.Split('/') is { Length: 2 } parts && parts[0] == "local" || relative.Split('/') is { Length: 3 } more && more[0] == "servers"; private static byte[] Bytes(Guid seed, int index, int length) { var bytes = new byte[length]; var hash = SHA256.HashData(Encoding.UTF8.GetBytes(seed.ToString("N") + index)); for (var i = 0; i < length; i++) bytes[i] = hash[i % hash.Length]; return bytes; } }