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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
import { describe, expect, test } from "bun:test";
import type { ProjectRecord } from "@/services/storage/migrations/transformers/types";
import {
collectReferencedMediaIds,
remapProjectMediaIds,
resolveImportedProjectName,
} from "../remap";
function buildProject(): ProjectRecord {
return {
version: 31,
metadata: { id: "project-1", name: "Demo" },
currentSceneId: "scene-1",
scenes: [
{
id: "scene-1",
tracks: {
main: {
id: "track-main",
elements: [
{ id: "el-video", type: "video", mediaId: "media-video" },
{ id: "el-image", type: "image", mediaId: "media-image" },
],
},
overlay: [
{
id: "track-text",
elements: [{ id: "el-text", type: "text", content: "hello" }],
},
],
audio: [
{
id: "track-audio",
elements: [
{
id: "el-upload",
type: "audio",
sourceType: "upload",
mediaId: "media-audio",
},
{
id: "el-library",
type: "audio",
sourceType: "library",
sourceUrl: "https://example.com/sound.mp3",
},
{
id: "el-shared",
type: "audio",
sourceType: "upload",
mediaId: "media-video",
},
],
},
],
},
},
{
id: "scene-2",
tracks: {
main: {
id: "track-main-2",
elements: [
{ id: "el-video-2", type: "video", mediaId: "media-video-2" },
],
},
overlay: [],
audio: [],
},
},
],
};
}
describe("collectReferencedMediaIds", () => {
test("collects media ids across scenes and track groups, deduplicated", () => {
const ids = collectReferencedMediaIds({ project: buildProject() });
expect(ids.sort()).toEqual([
"media-audio",
"media-image",
"media-video",
"media-video-2",
]);
});
test("tolerates malformed shapes", () => {
expect(collectReferencedMediaIds({ project: null })).toEqual([]);
expect(collectReferencedMediaIds({ project: { scenes: "nope" } })).toEqual(
[],
);
expect(
collectReferencedMediaIds({
project: { scenes: [{ tracks: { main: { elements: "nope" } } }] },
}),
).toEqual([]);
});
});
describe("remapProjectMediaIds", () => {
test("replaces mapped ids only, across every track group", () => {
const project = buildProject();
const idMap = new Map([
["media-video", "new-video"],
["media-image", "new-image"],
["media-audio", "new-audio"],
]);
const remapped = remapProjectMediaIds({ project, idMap });
const collected = collectReferencedMediaIds({ project: remapped });
expect(collected.sort()).toEqual([
"media-video-2",
"new-audio",
"new-image",
"new-video",
]);
// The shared media id is remapped in every referencing element.
const scenes = remapped.scenes;
expect(JSON.stringify(scenes)).not.toContain("media-video\"");
expect(JSON.stringify(scenes)).toContain("new-video");
});
test("does not mutate the input document", () => {
const project = buildProject();
const before = JSON.stringify(project);
remapProjectMediaIds({
project,
idMap: new Map([["media-video", "new-video"]]),
});
expect(JSON.stringify(project)).toBe(before);
});
test("returns the input unchanged when the map is empty", () => {
const project = buildProject();
expect(remapProjectMediaIds({ project, idMap: new Map() })).toBe(project);
});
test("is fully reversible with the inverse map", () => {
const project = buildProject();
const idMap = new Map([
["media-video", "new-video"],
["media-image", "new-image"],
["media-audio", "new-audio"],
["media-video-2", "new-video-2"],
]);
const inverse = new Map([...idMap].map(([from, to]) => [to, from]));
const remapped = remapProjectMediaIds({ project, idMap });
const restored = remapProjectMediaIds({ project: remapped, idMap: inverse });
expect(restored).toEqual(project);
});
});
describe("resolveImportedProjectName", () => {
test("keeps the name when there is no collision", () => {
expect(
resolveImportedProjectName({ name: "Demo", existingNames: ["Other"] }),
).toBe("Demo");
});
test("suffixes the name when it collides", () => {
expect(
resolveImportedProjectName({ name: "Demo", existingNames: ["Demo"] }),
).toBe("Demo (imported)");
});
});
|