Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions apps/web/__tests__/unit/custom-thumbnail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { describe, expect, it } from "vitest";
import {
CUSTOM_THUMBNAIL_MAX_DIMENSION,
clampThumbnailFrameTime,
fitThumbnailSize,
isJpegData,
validateThumbnailSourceFile,
} from "@/lib/custom-thumbnail";
import {
buildShareVideoMetadata,
getShareVideoUrls,
} from "@/lib/share-video-metadata";
import {
customThumbnailKey,
getCustomThumbnailKey,
getCustomThumbnailMetadata,
getCustomThumbnailVersion,
isCustomThumbnailFileName,
} from "../../../../packages/web-backend/src/Storage/custom-thumbnail";

const video = { id: "vid123", ownerId: "owner456" };

describe("custom thumbnail metadata", () => {
it("resolves a key under the video's own prefix", () => {
const metadata = {
customThumbnail: {
file: "custom-lz9k2a1b2c3.jpg",
source: "frame",
time: 12.5,
updatedAt: "2026-11-16T10:00:00.000Z",
},
};
expect(getCustomThumbnailKey({ ...video, metadata })).toBe(
"owner456/vid123/thumbnail/custom-lz9k2a1b2c3.jpg",
);
expect(getCustomThumbnailMetadata(metadata)).toEqual(
metadata.customThumbnail,
);
expect(getCustomThumbnailVersion(metadata)).toBe("lz9k2a1b2c3");
});

it("ignores old videos and anything that is not a plain file name", () => {
for (const metadata of [
null,
undefined,
{},
{ customThumbnail: null },
{ customThumbnail: { file: "../other/screenshot.jpg" } },
{ customThumbnail: { file: "custom-abc.png" } },
{ customThumbnail: { file: "custom-ABC.jpg" } },
{ customThumbnail: { file: "custom-a/b.jpg" } },
{ customThumbnail: { file: "custom-.jpg" } },
{ customThumbnail: { file: 42 } },
]) {
expect(getCustomThumbnailKey({ ...video, metadata })).toBeUndefined();
expect(getCustomThumbnailVersion(metadata)).toBeNull();
}
});

it("defaults the source and drops a bad time", () => {
expect(
getCustomThumbnailMetadata({
customThumbnail: { file: "custom-a1.jpg", source: "x", time: "1" },
}),
).toEqual({ file: "custom-a1.jpg", source: "upload", updatedAt: "" });
});

it("follows the video when it is duplicated under a new id", () => {
const metadata = { customThumbnail: { file: "custom-a1.jpg" } };
expect(
getCustomThumbnailKey({ id: "copy789", ownerId: "owner456", metadata }),
).toBe(
customThumbnailKey({
id: "copy789",
ownerId: "owner456",
file: "custom-a1.jpg",
}),
);
});

it("validates file names", () => {
expect(isCustomThumbnailFileName("custom-0a.jpg")).toBe(true);
expect(isCustomThumbnailFileName(`custom-${"a".repeat(33)}.jpg`)).toBe(
false,
);
});
});

describe("thumbnail input checks", () => {
it("accepts PNG, JPEG and WebP sources up to 20 MB", () => {
expect(
validateThumbnailSourceFile({ type: "image/png", size: 1024 }),
).toBeNull();
expect(
validateThumbnailSourceFile({ type: "IMAGE/JPEG", size: 1024 }),
).toBeNull();
expect(
validateThumbnailSourceFile({ type: "image/webp", size: 1024 }),
).toBeNull();
expect(
validateThumbnailSourceFile({ type: "image/svg+xml", size: 10 }),
).not.toBeNull();
expect(
validateThumbnailSourceFile({ type: "image/gif", size: 10 }),
).not.toBeNull();
expect(
validateThumbnailSourceFile({ type: "image/png", size: 0 }),
).not.toBeNull();
expect(
validateThumbnailSourceFile({
type: "image/png",
size: 20 * 1024 * 1024 + 1,
}),
).not.toBeNull();
});

it("recognises complete JPEG data only", () => {
expect(
isJpegData(new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0xff, 0xd9])),
).toBe(true);
expect(
isJpegData(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a])),
).toBe(false);
expect(
isJpegData(new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x00])),
).toBe(false);
expect(isJpegData(new Uint8Array([0xff, 0xd8]))).toBe(false);
});

it("fits inside the bounds without upscaling", () => {
expect(fitThumbnailSize(3840, 2160)).toEqual({ width: 1920, height: 1080 });
expect(fitThumbnailSize(1080, 2400)).toEqual({
width: 864,
height: CUSTOM_THUMBNAIL_MAX_DIMENSION,
});
expect(fitThumbnailSize(1280, 720)).toEqual({ width: 1280, height: 720 });
expect(fitThumbnailSize(0, 720)).toBeNull();
});

it("clamps frame times inside the video", () => {
expect(clampThumbnailFrameTime(12.3456, 60)).toBe(12.346);
expect(clampThumbnailFrameTime(100, 60)).toBeCloseTo(59.95, 5);
expect(clampThumbnailFrameTime(5, null)).toBe(5);
expect(clampThumbnailFrameTime(0, 0)).toBe(0);
for (const time of [-1, Number.NaN, Number.POSITIVE_INFINITY, "3", null]) {
expect(clampThumbnailFrameTime(time, 60)).toBeNull();
}
});
});

describe("link previews with a custom thumbnail", () => {
const base = {
videoId: "vid123",
name: "Demo",
sourceType: "desktopMP4" as const,
webUrl: "https://cap.so",
width: 1920,
height: 1080,
};

it("versions the preview image and drops the animated preview", () => {
const metadata = buildShareVideoMetadata({
...base,
thumbnailVersion: "lz9k2a",
});
expect(metadata.openGraph?.images).toEqual([
{
url: "https://cap.so/api/video/og?videoId=vid123&v=lz9k2a",
width: 1200,
height: 630,
type: "image/png",
},
]);
expect(metadata.twitter?.images).toEqual([
"https://cap.so/api/video/og?videoId=vid123&v=lz9k2a",
]);
});

it("leaves previews unchanged for videos without one", () => {
expect(buildShareVideoMetadata(base)).toEqual(
buildShareVideoMetadata({ ...base, thumbnailVersion: null }),
);
expect(getShareVideoUrls(base).ogImageUrl).toBe(
"https://cap.so/api/video/og?videoId=vid123",
);
const images = buildShareVideoMetadata(base).openGraph?.images;
expect(Array.isArray(images) ? images.length : 0).toBe(2);
});
});
173 changes: 173 additions & 0 deletions apps/web/__tests__/unit/embed-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { describe, expect, it } from "vitest";
import { buildEmbedCode } from "@/lib/embed-code";
import {
buildEmbedQuery,
DEFAULT_EMBED_OPTIONS,
parseEmbedOptions,
} from "@/lib/embed-options";

const legacyBuildEmbedCode = ({
webUrl,
videoId,
startTime,
}: {
webUrl: string;
videoId: string;
startTime?: number | null;
}): string => {
const start =
typeof startTime === "number" && Number.isFinite(startTime) && startTime > 0
? `?t=${Math.floor(startTime)}`
: "";

return `
<div style="position: relative; padding-bottom: 56.25%; height: 0;">
<iframe
src="${webUrl}/embed/${videoId}${start}"
frameborder="0"
webkitallowfullscreen
mozallowfullscreen
allowfullscreen
style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"
></iframe>
</div>
`
.trim()
.replace(/[\n\t]+/g, " ")
.replace(/>\s+</g, "><")
.replace(/"\s+>/g, '">');
};

describe("parseEmbedOptions", () => {
it("defaults to how embeds have always looked", () => {
expect(parseEmbedOptions({})).toEqual(DEFAULT_EMBED_OPTIONS);
expect(parseEmbedOptions(null)).toEqual(DEFAULT_EMBED_OPTIONS);
expect(parseEmbedOptions(new URLSearchParams(""))).toEqual(
DEFAULT_EMBED_OPTIONS,
);
});

it("only hides things on an explicit false", () => {
expect(
parseEmbedOptions({ title: "false", owner: "0", branding: "false" }),
).toMatchObject({
showTitle: false,
showOwner: false,
showBranding: false,
});
expect(
parseEmbedOptions({ title: "no", owner: "", branding: "true" }),
).toMatchObject({ showTitle: true, showOwner: true, showBranding: true });
});

it("only autoplays and mutes on an explicit true", () => {
expect(parseEmbedOptions({ autoplay: "true", muted: "1" })).toMatchObject({
autoplay: true,
muted: true,
});
expect(
parseEmbedOptions({ autoplay: "yes", muted: "false" }),
).toMatchObject({ autoplay: false, muted: false });
});

it("keeps the existing start time rules", () => {
expect(parseEmbedOptions({ t: "83" }).startTime).toBe(83);
expect(parseEmbedOptions({ t: ["12", "40"] }).startTime).toBe(12);
for (const t of ["0", "-5", "abc", ""]) {
expect(parseEmbedOptions({ t }).startTime).toBeNull();
}
});

it("takes the first of repeated params", () => {
expect(parseEmbedOptions({ title: ["false", "true"] }).showTitle).toBe(
false,
);
expect(
parseEmbedOptions(new URLSearchParams("title=true&title=false"))
.showTitle,
).toBe(true);
});
});

describe("buildEmbedQuery", () => {
it("is empty for defaults", () => {
expect(buildEmbedQuery({})).toBe("");
expect(buildEmbedQuery(DEFAULT_EMBED_OPTIONS)).toBe("");
});

it("writes only changed options, in a fixed order", () => {
expect(
buildEmbedQuery({
showBranding: false,
showTitle: false,
autoplay: true,
muted: true,
showOwner: false,
startTime: 61.9,
}),
).toBe(
"?t=61&autoplay=true&muted=true&title=false&owner=false&branding=false",
);
});

it("round trips through the parser", () => {
const options = {
...DEFAULT_EMBED_OPTIONS,
showOwner: false,
autoplay: true,
muted: true,
startTime: 30,
};
expect(
parseEmbedOptions(new URLSearchParams(buildEmbedQuery(options))),
).toEqual(options);
});
});

describe("buildEmbedCode", () => {
it("is byte-identical to the previous snippet with default options", () => {
const cases = [
{ webUrl: "https://cap.so", videoId: "abc123" },
{ webUrl: "https://cap.so", videoId: "abc123", startTime: 83 },
{ webUrl: "https://cap.so", videoId: "abc123", startTime: 83.7 },
{ webUrl: "https://cap.so", videoId: "abc123", startTime: null },
{ webUrl: "https://cap.example.com", videoId: "xyz", startTime: 0 },
];
for (const input of cases) {
expect(buildEmbedCode(input)).toBe(legacyBuildEmbedCode(input));
expect(buildEmbedCode({ ...input, options: {} })).toBe(
legacyBuildEmbedCode(input),
);
}
expect(
buildEmbedCode({ webUrl: "https://cap.so", videoId: "abc123" }),
).toBe(
'<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://cap.so/embed/abc123" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>',
);
});

it("adds the options and delegates autoplay to the iframe", () => {
const code = buildEmbedCode({
webUrl: "https://cap.so",
videoId: "abc123",
startTime: 10,
options: { autoplay: true, muted: true, showTitle: false },
});
expect(code).toContain(
'src="https://cap.so/embed/abc123?t=10&autoplay=true&muted=true&title=false"',
);
expect(code).toContain(
'frameborder="0" allow="autoplay; fullscreen; picture-in-picture" webkitallowfullscreen',
);
});

it("does not add the allow attribute without autoplay", () => {
const code = buildEmbedCode({
webUrl: "https://cap.so",
videoId: "abc123",
options: { showBranding: false },
});
expect(code).not.toContain("allow=");
expect(code).toContain('src="https://cap.so/embed/abc123?branding=false"');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ vi.mock(
"@/app/(org)/dashboard/settings/organization/components/ShareableLinkIcon",
() => ({ ShareableLinkIcon: () => null }),
);
vi.mock(
"@/app/(org)/dashboard/settings/organization/components/ShareableLinkBrandColor",
() => ({ ShareableLinkBrandColor: () => null }),
);
vi.mock(
"@/app/(org)/dashboard/settings/organization/components/AccessEmailDomain",
() => ({ default: () => null }),
Expand Down
Loading
Loading