Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test: hold the desktop segment manifest to the server's parser with shared fixtures

Richie McIlroyf595514c92f0
diff --git a/apps/desktop-gpui/src/upload.rs b/apps/desktop-gpui/src/upload.rs
index d952cb86b..53878a197 100644
--- a/apps/desktop-gpui/src/upload.rs
+++ b/apps/desktop-gpui/src/upload.rs
@@ -2746,6 +2746,58 @@ mod tests {
}
}
+ /// The finished segment manifest this app uploads, checked against the
+ /// files the web server's own parser is tested with
+ /// (`apps/web/__tests__/unit/desktop-segments-manifest-contract.test.ts`),
+ /// so neither side can change the format without the other noticing. Run
+ /// with `CAP_UPDATE_CONTRACT_FIXTURES=1` to rewrite the files after an
+ /// intended change.
+ #[test]
+ fn finished_segment_manifests_match_the_server_contract_fixtures() {
+ let fixtures = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
+ .join("../web/__tests__/fixtures/desktop-segments");
+ for (name, audio) in [("with-audio", 2), ("video-only", 0)] {
+ let mut manifest = SegmentUploadManifest::default();
+ manifest.record(&segment_event(0, 0.0, true, SegmentMediaType::Video));
+ for (index, duration) in [(1, 2.0), (2, 2.0), (3, 1.25)] {
+ manifest.record(&segment_event(
+ index,
+ duration,
+ false,
+ SegmentMediaType::Video,
+ ));
+ }
+ if audio > 0 {
+ manifest.record(&segment_event(0, 0.0, true, SegmentMediaType::Audio));
+ for index in 1..=audio {
+ manifest.record(&segment_event(index, 3.0, false, SegmentMediaType::Audio));
+ }
+ }
+ manifest.is_complete = true;
+ let uploaded = serde_json::to_value(&manifest).unwrap();
+ let path = fixtures.join(format!("finished-manifest-{name}.json"));
+ if std::env::var_os("CAP_UPDATE_CONTRACT_FIXTURES").is_some() {
+ std::fs::create_dir_all(&fixtures).unwrap();
+ let mut json = Vec::new();
+ let formatter = serde_json::ser::PrettyFormatter::with_indent(b"\t");
+ let mut serializer = serde_json::Serializer::with_formatter(&mut json, formatter);
+ serde::Serialize::serialize(&uploaded, &mut serializer).unwrap();
+ json.push(b'\n');
+ std::fs::write(&path, json).unwrap();
+ }
+ let fixture: serde_json::Value = serde_json::from_slice(
+ &std::fs::read(&path).unwrap_or_else(|error| panic!("{}: {error}", path.display())),
+ )
+ .unwrap();
+ assert_eq!(
+ fixture,
+ uploaded,
+ "{} no longer matches what the app uploads",
+ path.display()
+ );
+ }
+ }
+
#[test]
fn segment_manifest_requires_init_and_orders_video_and_audio() {
let mut manifest = SegmentUploadManifest::default();
diff --git a/apps/web/__tests__/fixtures/desktop-segments/finished-manifest-video-only.json b/apps/web/__tests__/fixtures/desktop-segments/finished-manifest-video-only.json
new file mode 100644
index 000000000..703a23d03
--- /dev/null
+++ b/apps/web/__tests__/fixtures/desktop-segments/finished-manifest-video-only.json
@@ -0,0 +1,21 @@
+{
+ "version": 2,
+ "video_init_uploaded": true,
+ "audio_init_uploaded": false,
+ "video_segments": [
+ {
+ "index": 1,
+ "duration": 2.0
+ },
+ {
+ "index": 2,
+ "duration": 2.0
+ },
+ {
+ "index": 3,
+ "duration": 1.25
+ }
+ ],
+ "audio_segments": [],
+ "is_complete": true
+}
diff --git a/apps/web/__tests__/fixtures/desktop-segments/finished-manifest-with-audio.json b/apps/web/__tests__/fixtures/desktop-segments/finished-manifest-with-audio.json
new file mode 100644
index 000000000..8f95c70a4
--- /dev/null
+++ b/apps/web/__tests__/fixtures/desktop-segments/finished-manifest-with-audio.json
@@ -0,0 +1,30 @@
+{
+ "version": 2,
+ "video_init_uploaded": true,
+ "audio_init_uploaded": true,
+ "video_segments": [
+ {
+ "index": 1,
+ "duration": 2.0
+ },
+ {
+ "index": 2,
+ "duration": 2.0
+ },
+ {
+ "index": 3,
+ "duration": 1.25
+ }
+ ],
+ "audio_segments": [
+ {
+ "index": 1,
+ "duration": 3.0
+ },
+ {
+ "index": 2,
+ "duration": 3.0
+ }
+ ],
+ "is_complete": true
+}
diff --git a/apps/web/__tests__/unit/desktop-segments-manifest-contract.test.ts b/apps/web/__tests__/unit/desktop-segments-manifest-contract.test.ts
new file mode 100644
index 000000000..9817ca7d3
--- /dev/null
+++ b/apps/web/__tests__/unit/desktop-segments-manifest-contract.test.ts
@@ -0,0 +1,38 @@
+import { Video } from "@cap/web-domain";
+import { Schema } from "effect";
+import { describe, expect, it } from "vitest";
+import { readCompletedRecordingManifest } from "@/lib/desktop-recording-verification";
+import videoOnly from "../fixtures/desktop-segments/finished-manifest-video-only.json";
+import withAudio from "../fixtures/desktop-segments/finished-manifest-with-audio.json";
+
+// The desktop app's own test writes these files from the manifest it uploads
+// (`finished_segment_manifests_match_the_server_contract_fixtures` in
+// apps/desktop-gpui/src/upload.rs), so a format change on either side fails
+// one of the two tests.
+describe("desktop segment manifest contract", () => {
+ it.each([
+ ["with audio", withAudio, true, [1, 2]],
+ ["video only", videoOnly, false, []],
+ ])(
+ "finalization accepts the finished manifest the app uploads (%s)",
+ (_, fixture, hasAudio, audioSegments) => {
+ const completed = readCompletedRecordingManifest(JSON.stringify(fixture));
+ expect(completed.hasAudio).toBe(hasAudio);
+ expect(completed.videoSegments).toEqual([1, 2, 3]);
+ expect(completed.audioSegments).toEqual(audioSegments);
+ },
+ );
+
+ it.each([
+ ["with audio", withAudio],
+ ["video only", videoOnly],
+ ])(
+ "recovery reads the finished manifest the app uploads (%s)",
+ (_, fixture) => {
+ const manifest = Schema.decodeUnknownSync(Video.SegmentManifest)(fixture);
+ expect(manifest.is_complete).toBe(true);
+ expect(manifest.video_init_uploaded).toBe(true);
+ expect(manifest.video_segments).toHaveLength(3);
+ },
+ );
+});