using System.IO; using System.Text.Json; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Threading; using XFEToolBox.Tools.VideoStudio; using VisualTransition = XFEToolBox.Tools.VideoStudio.VisualTransition; namespace VideoStudio.Validation; internal static partial class Program { private static async Task LinkedSelectionTests(string[] paths) { using var vm = NewVm(); await vm.ImportAssetsAsync(paths); await vm.WaitForAssetPreviewsAsync(); TimelineClip Video() => vm.Clips.First(c => c.Kind == MediaKind.Video); TimelineClip Audio() => vm.Clips.First(c => c.Kind == MediaKind.Audio); Check(Video().LinkGroupId.Length > 0 && Audio().LinkGroupId == Video().LinkGroupId, "video insertion links exactly its original audio by default"); vm.SelectClip(Video(), preserveSelected: false); vm.SeekSequence(1.5); vm.MoveClip(Video(), 1, vm.TrackFor(Video())!); Check(vm.Clips.All(c => c.TimelineStart == 1) && vm.SequencePosition == 1.5, "linked move preserves sync and playhead"); vm.UndoCommand.Execute(null); vm.BeginTrim(Video(), ClipEdge.Start); vm.UpdateTrim(.5); vm.EndTrim(true); Check(vm.Clips.All(c => c.StartSeconds == .5 && c.TimelineStart == .5 && c.EndSeconds == 4) && vm.SequencePosition == 1.5, "head trim edits both linked ends without seeking"); vm.UndoCommand.Execute(null); vm.BeginTrim(Video(), ClipEdge.End); vm.UpdateTrim(-1); Check(vm.Clips.All(c => c.EndSeconds == 3), "tail trim immediately updates both linked clips"); vm.EndTrim(false); Check(vm.Clips.All(c => c.EndSeconds == 4) && vm.SequencePosition == 1.5, "cancel restores both edges and original playhead"); vm.SelectClip(Audio(), preserveSelected: false); vm.VolumeText = "130"; vm.EditFadeIn = ".5"; vm.ApplyClipPropertiesCommand.Execute(null); Check(Audio().AudioVolume == 130 && Audio().FadeIn == .5 && Video().AudioVolume == 100 && Video().FadeIn == 0, "linked audio gain and effects remain independent from video properties"); vm.SelectClip(Video(), preserveSelected: false); vm.TrimOutText = "3"; vm.ApplyClipPropertiesCommand.Execute(null); Check(Video().EndSeconds == 3 && Audio().EndSeconds == 3 && Audio().AudioVolume == 130 && vm.SequencePosition == 1.5, "numeric time edits synchronize peer but preserve peer properties and playhead"); vm.UndoCommand.Execute(null); vm.SelectClip(Video(), preserveSelected: false); vm.SplitSelectedAt(2); Check(vm.Clips.Count == 4 && vm.Clips.GroupBy(c => c.LinkGroupId).All(g => g.Count() == 2 && ClipLinking.SameTiming(g.First(), g.Last())), "split creates two separate synchronized link groups"); Check(vm.SelectedClips.Length == 2 && vm.SelectedClips.All(c => c.TimelineStart == 2), "split selects the new right-hand linked pair"); vm.UndoCommand.Execute(null); vm.RedoCommand.Execute(null); Check(vm.Clips.Count == 4 && vm.SelectedClips.Length == 2, "undo redo preserves linked multi-selection"); vm.UndoCommand.Execute(null); vm.SelectClip(Video(), preserveSelected: false); vm.TrackFor(Audio())!.IsLocked = true; vm.MoveClip(Video(), 2, vm.TrackFor(Video())!); vm.SplitSelectedAt(2); vm.RemoveClipCommand.Execute(null); Check(vm.Clips.Count == 2 && Video().TimelineStart == 0 && !vm.BeginTrim(Video(), ClipEdge.Start), "locking either member rejects entire linked move split delete trim"); vm.TrackFor(Audio())!.IsLocked = false; vm.ToggleClipEnabledCommand.Execute(null); Check(vm.Clips.All(c => !c.IsEnabled), "enable toggle applies to linked selection atomically"); vm.UndoCommand.Execute(null); vm.RemoveClipCommand.Execute(null); Check(vm.Clips.Count == 0, "delete removes both linked clips"); vm.UndoCommand.Execute(null); vm.ToggleClipLinkCommand.Execute(null); Check(vm.Clips.All(c => c.LinkGroupId == "") && vm.SelectedClips.Length == 1, "unlink selects only focused clip for independent edits"); string unlinked = Path.Combine(Root, "unlinked-v3.json"); await vm.SaveProjectToAsync(unlinked); using var restored = NewVm(); Check(await restored.LoadProjectAsync(unlinked) && restored.Clips.All(c => c.LinkGroupId == ""), "v3 save/load preserves deliberate unlinking"); var saved = JsonSerializer.Deserialize(await File.ReadAllTextAsync(unlinked), Json)!; string legacy = Path.Combine(Root, "legacy-v2.json"); await File.WriteAllTextAsync(legacy, JsonSerializer.Serialize(saved with { Version = 2 }, Json)); Check(await restored.LoadProjectAsync(legacy) && restored.Clips.All(c => c.LinkGroupId.Length > 0) && restored.HasUnsavedChanges, "legacy v2 uniquely aligned source pair migrates to linked v3"); var duplicates = saved.Clips.Concat(saved.Clips.Select(c => { var copy = c.Copy(); copy.Id = Guid.NewGuid().ToString(); return copy; })).ToArray(); Check(ClipLinking.MigrateLegacy(duplicates) == 0 && duplicates.All(c => c.LinkGroupId == ""), "ambiguous duplicate legacy pairs are never guessed"); var bad = saved.Clips.Select(c => c.Copy()).ToArray(); bad[0].LinkGroupId = "orphan"; await Reject(() => Task.Run(() => MediaProcessingService.ValidateSequence(vm.CaptureSequence() with { Clips = bad })), "orphaned link rejected before save/export"); vm.ToggleClipLinkCommand.Execute(null); vm.RemoveClipCommand.Execute(null); vm.UndoCommand.Execute(null); vm.SelectedTrack = vm.Tracks.Single(t => t.Name == "V2"); vm.InsertAsset(vm.Assets[1], 0, vm.SelectedTrack); var second = vm.SelectedClip!; var first = Video(); vm.SelectClip(first, preserveSelected: false); vm.SelectClip(second, extend: true); vm.SeekSequence(1.5); long revision = vm.Revision; vm.HasUnsavedChanges = false; vm.SelectClip(second, extend: true); Check(vm.SelectedClips.Length == 2 && !vm.IsClipSelected(second), "Ctrl-click removes only clicked group from selection"); vm.SelectClip(second, extend: true); Check(vm.SelectedClips.Length == 3 && vm.Revision == revision && !vm.HasUnsavedChanges && vm.SequencePosition == 1.5, "Ctrl multi-selection does not dirty project invalidate playback or move playhead"); vm.MoveClip(first, 1, vm.TrackFor(first)!); Check(vm.Clips.All(c => c.TimelineStart == 1) && vm.SelectedClips.Length == 3, "multi-selected clips move together with linked peers once"); vm.UndoCommand.Execute(null); first = Video(); vm.MoveClip(first,0,vm.Tracks.Single(t => t.Name == "V2")); Check(vm.TrackFor(first)!.Name == "V1", "batch vertical move rejects missing destination lane atomically"); vm.AddVideoTrackCommand.Execute(null); vm.MoveClip(first,0,vm.Tracks.Single(t => t.Name == "V2")); Check(vm.TrackFor(first)!.Name == "V2" && vm.TrackFor(vm.Clips.Last(c => c.Kind == MediaKind.Video))!.Name == "V3" && vm.TrackFor(Audio())!.Name == "A1", "batch vertical move preserves visual lane spacing and linked audio lane"); vm.UndoCommand.Execute(null); vm.SplitSelectedAt(1.5); Check(vm.Clips.Count == 6 && vm.SelectedClips.Length == 3 && vm.Clips.Count(c => c.TimelineStart == 1.5) == 3, "multi-selection splits each selected group exactly once"); vm.UndoCommand.Execute(null); vm.RemoveClipCommand.Execute(null); Check(vm.Clips.Count == 0, "multi-selection delete handles all selected groups"); vm.UndoCommand.Execute(null); vm.SelectTrack(vm.Tracks.Single(t => t.Name == "V1")); vm.SelectTrack(vm.Tracks.Single(t => t.Name == "V2"), extend: true); Check(vm.SelectedTracks.Length == 2 && vm.SelectedClips.Length == 3 && vm.Tracks.Count(t => t.IsSelected) == 2, "Ctrl track selection highlights lanes and includes their linked clips"); vm.ToggleTrackEnabledCommand.Execute(null); Check(vm.SelectedTracks.All(t => !t.IsEnabled) && vm.Tracks.Where(t => t.Kind == MediaKind.Audio).All(t => t.IsEnabled), "multi-track M toggle applies only selected lanes"); vm.UndoCommand.Execute(null); vm.ToggleTrackLockCommand.Execute(null); Check(vm.SelectedTracks.All(t => t.IsLocked), "multi-track L toggle locks both selected lanes"); vm.UndoCommand.Execute(null); vm.SelectClip(vm.Clips.First(c => c.Kind == MediaKind.Video), preserveSelected: false); vm.SelectClip(vm.Clips.Last(c => c.Kind == MediaKind.Video), extend: true); vm.EffectPage = 2; vm.ChooseTransitionCommand.Execute(VisualTransition.PaperTear); Check(vm.Clips.Where(c => c.Kind == MediaKind.Video).All(c => c.TransitionIn == VisualTransition.PaperTear && c.FadeIn == .5 && c.FadeOut == .5) && vm.Clips.Single(c => c.Kind == MediaKind.Audio).TransitionIn == VisualTransition.Fade, "batch visual presets affect selected videos but not their bound audio"); string effectProject = Path.Combine(Root,"effects-v3.json"); await vm.SaveProjectToAsync(effectProject); Check(await restored.LoadProjectAsync(effectProject) && restored.Clips.Where(c => c.Kind == MediaKind.Video).All(c => c.TransitionIn == VisualTransition.PaperTear && c.FadeOut == .5), "effect names durations and link groups survive project save/load"); vm.EditFadeIn = "99"; vm.ApplyTransitionCommand.Execute(null); Check(vm.Clips.Where(c => c.Kind == MediaKind.Video).All(c => c.FadeIn == .5), "invalid bulk fade lengths leave all clips untouched"); vm.BeginTrim(vm.SelectedClip!, ClipEdge.End); vm.UpdateTrim(-2.5); vm.EndTrim(true); Check(vm.SelectedClip!.FadeIn + vm.SelectedClip.FadeOut <= vm.SelectedClip.DurationSeconds + .000001, "shortening a visual clip safely fits both transition durations"); vm.UndoCommand.Execute(null); var page = new MainPage(); var model = (MainPageViewModel)page.DataContext; model.FfmpegPath = ffmpeg; model.FfprobePath = ffprobe; var window = TestWindow(page); window.Show(); try { await model.ImportAssetsAsync(paths); await model.WaitForAssetPreviewsAsync(); model.SnappingEnabled = false; var timeline = (TimelineSurface)page.FindName("Timeline"); var c = model.Clips.First(c => c.Kind == MediaKind.Video); model.InsertAsset(model.Assets[1], 0, model.Tracks.Single(t => t.Name == "V2")); var b = model.SelectedClip!; model.SeekSequence(2); model.HasUnsavedChanges = false; long before = model.Revision; int seeks = 0; model.SeekRequested += _ => seeks++; await Dispatcher.Yield(DispatcherPriority.ApplicationIdle); Point Body(TimelineClip clip) => new(timeline.XAt(clip.TimelineStart + 1), timeline.YAt(model.TrackFor(clip)!) + 30); timeline.BeginDrag(Body(c)); timeline.FinishDrag(Body(c)); timeline.BeginDrag(Body(b), true); Check(model.SelectedClips.Length == 3 && model.SequencePosition == 2 && seeks == 0 && before == model.Revision && !model.HasUnsavedChanges, "real timeline selection and Ctrl gesture never seek or invalidate media"); timeline.BeginDrag(new(timeline.XAt(7), timeline.YAt(model.TrackFor(c)!) + 30)); Check(model.SequencePosition == 2 && seeks == 0, "blank track click only selects a lane without seeking"); timeline.BeginDrag(new(timeline.XAt(3), 12)); timeline.FinishDrag(new(timeline.XAt(3), 12)); Check(model.SequencePosition == 3 && seeks > 0, "top ruler remains the only mouse gesture that starts seeking"); var tip = (ToolTip)timeline.ToolTip; tip.PlacementTarget = timeline; tip.IsOpen = true; await Dispatcher.Yield(DispatcherPriority.ApplicationIdle); Check(tip.Background is SolidColorBrush bg && bg.Color.R < 60 && tip.Foreground is SolidColorBrush fg && fg.Color.R > 220 && tip.Template is not null, "actual timeline tooltip uses readable explicit dark template"); tip.IsOpen = false; var library = (ItemsControl)page.FindName("TransitionLibrary"); model.SelectClip(c, preserveSelected: false); model.InspectorPage = 1; model.EffectPage = 2; model.ChooseTransitionCommand.Execute(VisualTransition.Fade); Check(model.SelectedClip!.FadeIn > 0 && model.SelectedClip.FadeOut > 0 && model.Clips.First(x => x.Kind == MediaKind.Audio).FadeIn == 0, "combo effect sets entry and exit on selected visual clip only"); await Dispatcher.Yield(DispatcherPriority.ApplicationIdle); Check(Descendants