Repository navigation
Expand file tree
/
Copy pathexportProject.test.ts
More file actions
134 lines (122 loc) · 5.25 KB
/
Copy pathexportProject.test.ts
File metadata and controls
134 lines (122 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { describe, expect, it, vi } from "vitest";
import {
normalizeProjectEditor,
PROJECT_VERSION,
type ProjectEditorState,
validateProjectData,
} from "@/components/video-editor/projectPersistence";
import { migrateProjectDataToAxcutDocument } from "@/lib/ai-edition/document/migrate";
import { isAxcutDocumentFile } from "@/lib/ai-edition/schema";
import { getEditorSettings } from "@/lib/ai-edition/store/editorSettings";
import { legacyProjectEditor, loadDocumentProject, type VideoProbe } from "./exportProject";
const SCREEN = "/recordings/screen.mp4";
const v2Project = (editor: Record<string, unknown> = {}) => ({
version: PROJECT_VERSION,
media: { screenVideoPath: SCREEN },
editor,
});
/**
* A project file as the editor writes it: the AxcutDocument itself, JSON round-tripped, its
* primary asset carrying the dimensions and duration the editor probed on import, and `settings`
* written straight into `legacyEditor`, where the editor keeps them.
*/
function savedDocument(settings: Record<string, unknown> = {}, schemaVersion?: number) {
const migrated = migrateProjectDataToAxcutDocument({
...v2Project(),
editor: normalizeProjectEditor({}),
});
const doc = { ...migrated, legacyEditor: { ...migrated.legacyEditor, ...settings } };
const assets = doc.assets.map((asset) => ({
...asset,
durationSec: 12,
video: { codec: "h264", fps: 60, width: 1920, height: 1080 },
}));
const raw = JSON.parse(JSON.stringify({ ...doc, assets })) as Record<string, unknown>;
if (schemaVersion !== undefined) raw.schemaVersion = schemaVersion;
return raw;
}
const noProbe: VideoProbe = () =>
Promise.reject(new Error("probed a video the document describes"));
describe("the project file shapes the CLI export reads", () => {
it("tells a saved document from a legacy v2 project", () => {
expect(isAxcutDocumentFile(savedDocument())).toBe(true);
expect(isAxcutDocumentFile(v2Project())).toBe(false);
// The v2 validator is what rejected every document before: it must stay out of their way.
expect(validateProjectData(savedDocument())).toBe(false);
expect(validateProjectData(v2Project())).toBe(true);
expect(isAxcutDocumentFile(null)).toBe(false);
expect(isAxcutDocumentFile({ schemaVersion: 8 })).toBe(false);
});
});
describe("legacyProjectEditor", () => {
it("keeps the render settings the normalizer does not know", () => {
const editor = {
cursorModel3d: true,
cursorClickSound: true,
cursorShow: false,
webcamBackgroundMode: "blur",
webcamBlurIntensity: 0.8,
audioGainDb: 6,
};
const document = migrateProjectDataToAxcutDocument({
...v2Project(),
editor: legacyProjectEditor(editor as Partial<ProjectEditorState>),
});
const settings = getEditorSettings(document);
expect(settings.cursor.model3d).toBe(true);
expect(settings.cursor.clickSound).toBe(true);
expect(settings.cursorShow).toBe(false);
expect(settings.webcamBackgroundMode).toBe("blur");
expect(settings.audioGainDb).toBe(6);
expect(document.legacyEditor).toMatchObject(editor);
});
it("still normalizes the keys it knows", () => {
expect(legacyProjectEditor({ aspectRatio: "nope" as never }).aspectRatio).toBe(
normalizeProjectEditor({}).aspectRatio,
);
});
});
describe("loadDocumentProject", () => {
it("reads the media, length and size off the document without probing", async () => {
const project = await loadDocumentProject(savedDocument(), noProbe);
expect(project.screenVideoPath).toBe(SCREEN);
expect(project.durationMs).toBe(12_000);
expect(project.sourceDims).toEqual({ width: 1920, height: 1080 });
expect(project.document.assets[0]?.originalPath).toBe(SCREEN);
});
it("keeps what the editor saved, the frame included", async () => {
const project = await loadDocumentProject(
savedDocument({ frame: "phone", padding: 30, exportFormat: "gif", exportQuality: "source" }),
noProbe,
);
expect(project.document.legacyEditor).toMatchObject({ frame: "phone", padding: 30 });
expect(project.editor).toMatchObject({ exportFormat: "gif", exportQuality: "source" });
});
it("upgrades an older schemaVersion the way the editor does on open", async () => {
const raw = savedDocument({}, 7);
delete (raw.legacyEditor as Record<string, unknown>).aspectRatio;
const project = await loadDocumentProject(raw, noProbe);
expect(project.document.schemaVersion).toBe(9);
expect(project.document.legacyEditor?.aspectRatio).toBe("16:9");
});
it("probes the recording only when the document lacks its size or length", async () => {
const raw = savedDocument();
const assets = raw.assets as Record<string, unknown>[];
raw.assets = assets.map(({ video: _video, ...asset }) => asset);
const probe = vi
.fn<VideoProbe>()
.mockResolvedValue({ width: 1280, height: 720, durationMs: 4000 });
const project = await loadDocumentProject(raw, probe);
expect(probe).toHaveBeenCalledWith(SCREEN);
expect(project.sourceDims).toEqual({ width: 1280, height: 720 });
expect(project.durationMs).toBe(4000);
});
it("refuses a document with no media, and one that does not validate", async () => {
await expect(loadDocumentProject({ ...savedDocument(), assets: [] }, noProbe)).rejects.toThrow(
"does not reference any recorded media",
);
await expect(
loadDocumentProject({ ...savedDocument(), project: null }, noProbe),
).rejects.toThrow();
});
});