1use std::collections::{HashMap, HashSet};
4
5use alpha2_model::Project;
6
7#[must_use]
11pub fn stale_sources(previous: &Project, next: &Project) -> HashSet<String> {
12 let kept = next
13 .sources
14 .iter()
15 .map(|source| (&source.id, source))
16 .collect::<HashMap<_, _>>();
17 previous
18 .sources
19 .iter()
20 .filter(|old| {
21 !kept.get(&old.id).is_some_and(|new| {
22 new.locator == old.locator
23 && new.kind == old.kind
24 && new.provider == old.provider
25 && new.fingerprint == old.fingerprint
26 })
27 })
28 .map(|old| old.id.as_str().to_owned())
29 .collect()
30}
31
32#[derive(Debug, Eq, PartialEq)]
34pub enum StalePresentations {
35 All,
37 Sources(HashSet<String>),
39}
40
41#[must_use]
43pub fn stale_presentations(previous: &Project, next: &Project) -> StalePresentations {
44 let (before, after) = (&previous.settings, &next.settings);
45 if (before.width, before.height, before.color) != (after.width, after.height, after.color) {
46 return StalePresentations::All;
47 }
48 let declared = next
49 .sources
50 .iter()
51 .map(|source| (&source.id, source.color_metadata))
52 .collect::<HashMap<_, _>>();
53 StalePresentations::Sources(
54 previous
55 .sources
56 .iter()
57 .filter(|old| {
58 declared
59 .get(&old.id)
60 .is_some_and(|color| *color != old.color_metadata)
61 })
62 .map(|old| old.id.as_str().to_owned())
63 .collect(),
64 )
65}
66
67#[cfg(test)]
68mod tests {
69 use super::{StalePresentations, stale_presentations, stale_sources};
70 use alpha2_model::Project;
71 use serde_json::json;
72 use std::collections::HashSet;
73
74 fn project(sources: &[(&str, &str)], clip_start: i64) -> Project {
75 let sources = sources
76 .iter()
77 .map(|(id, locator)| json!({"id": id, "locator": locator, "kind": "audio"}))
78 .collect::<Vec<_>>();
79 let clips = [json!({
80 "id": "clip",
81 "source_id": "a",
82 "timeline_range": {"start": clip_start, "end": clip_start + 1_000},
83 "source_range": {"start": 0, "end": 1_000}
84 })];
85 Project::from_json(
86 &json!({
87 "schema_version": 1,
88 "id": "p",
89 "name": "p",
90 "settings": {
91 "width": 16, "height": 16,
92 "frame_rate": {"num": 30, "den": 1},
93 "sample_rate": 48_000
94 },
95 "sources": sources,
96 "timeline": {"tracks": [
97 {"id": "t", "name": "t", "kind": "audio", "clips": clips}
98 ]}
99 })
100 .to_string(),
101 )
102 .expect("valid project")
103 }
104
105 #[test]
106 fn stale_sources_are_found_in_time_linear_in_the_sources() {
107 let with_sources = |locator: &str, count: usize| {
108 let mut project = project(&[("a", "file://a")], 0);
109 project.sources.extend((0..count).map(|index| {
110 alpha2_model::MediaSource::new(
111 alpha2_media::SourceId::new(format!("s{index}")).expect("id"),
112 format!("{locator}/{index}"),
113 alpha2_model::MediaKind::Audio,
114 )
115 }));
116 project
117 };
118 let count = alpha2_model::MAX_CLIPS;
119 let (before, after) = (
120 with_sources("file://old", count),
121 with_sources("file://new", count),
122 );
123 assert_eq!(stale_sources(&before, &after).len(), count);
124 alpha2_scaling_support::assert_linear_shared(
125 "stale sources",
126 count / alpha2_scaling_support::GROWTH,
127 |count| {
128 (
129 with_sources("file://old", count),
130 with_sources("file://new", count),
131 )
132 },
133 |(before, after)| stale_sources(before, after),
134 );
135 }
136
137 #[test]
138 fn an_edit_that_keeps_every_source_invalidates_nothing() {
139 let before = project(&[("a", "file://a"), ("b", "file://b")], 0);
140 let moved = project(&[("a", "file://a"), ("b", "file://b")], 5_000);
141 assert!(stale_sources(&before, &moved).is_empty());
142 }
143
144 #[test]
145 fn a_removed_source_is_invalidated() {
146 let before = project(&[("a", "file://a"), ("b", "file://b")], 0);
147 let after = project(&[("a", "file://a")], 0);
148 assert_eq!(stale_sources(&before, &after), ["b".to_owned()].into());
149 }
150
151 #[test]
152 fn presentations_follow_the_size_the_color_pipeline_and_each_declared_color() {
153 let before = project(&[("a", "file://a"), ("b", "file://b")], 0);
154 let mut after = project(&[("a", "file://a"), ("b", "file://b")], 5_000);
155 assert_eq!(
156 stale_presentations(&before, &after),
157 StalePresentations::Sources(HashSet::new())
158 );
159 after.settings.color.hdr_peak_nits = 4_000;
160 assert_eq!(
161 stale_presentations(&before, &after),
162 StalePresentations::All
163 );
164 let mut after = project(&[("a", "file://a"), ("b", "file://b")], 0);
165 after.settings.color.hdr = alpha2_model::HdrSources::Reject;
166 assert_eq!(
167 stale_presentations(&before, &after),
168 StalePresentations::All
169 );
170 let mut after = project(&[("a", "file://a"), ("b", "file://b")], 0);
171 after.sources[1].color_metadata = Some(alpha2_media::ColorMetadata::EXPORT);
172 assert_eq!(
173 stale_presentations(&before, &after),
174 StalePresentations::Sources(["b".to_owned()].into())
175 );
176 }
177
178 #[test]
179 fn a_changed_kind_provider_or_fingerprint_invalidates_that_source() {
180 let lottie = |animation: Option<&str>| {
181 serde_json::from_value::<alpha2_model::ProviderSource>(
182 json!({"format": "lottie", "animation": animation}),
183 )
184 .expect("provider")
185 };
186 let mut before = project(&[("a", "file://a"), ("b", "file://b")], 0);
187 before.sources[1].kind = alpha2_model::MediaKind::Video;
188 before.sources[1].provider = Some(lottie(Some("intro")));
189 let unchanged = before.clone();
190 assert!(stale_sources(&before, &unchanged).is_empty());
191
192 let mut animation = before.clone();
193 animation.sources[1].provider = Some(lottie(Some("outro")));
194 assert_eq!(stale_sources(&before, &animation), ["b".to_owned()].into());
195
196 let mut fingerprint = before.clone();
197 fingerprint.sources[0].fingerprint = Some(alpha2_model::MediaFingerprint::default());
198 assert_eq!(
199 stale_sources(&before, &fingerprint),
200 ["a".to_owned()].into()
201 );
202
203 let mut kind = before.clone();
204 kind.sources[0].kind = alpha2_model::MediaKind::Video;
205 assert_eq!(stale_sources(&before, &kind), ["a".to_owned()].into());
206 }
207
208 #[test]
209 fn a_new_locator_invalidates_only_that_source() {
210 let before = project(&[("a", "file://a"), ("b", "file://b")], 0);
211 let after = project(&[("a", "file://a"), ("b", "file://b-v2")], 0);
212 assert_eq!(stale_sources(&before, &after), ["b".to_owned()].into());
213 }
214}