Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(web): update tests for dynamic OG images

Richie McIlroyef6f8ca55ae2
diff --git a/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts b/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts
index 7c15b83d6..088cf28c4 100644
--- a/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts
+++ b/apps/web/__tests__/unit/async-video-code-reviews-page.test.ts
@@ -34,8 +34,9 @@ describe("AsyncVideoCodeReviewsPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets async video code reviews keyword", () => {
diff --git a/apps/web/__tests__/unit/avi-to-mp4-page.test.ts b/apps/web/__tests__/unit/avi-to-mp4-page.test.ts
index b47dc75b0..36b53ede5 100644
--- a/apps/web/__tests__/unit/avi-to-mp4-page.test.ts
+++ b/apps/web/__tests__/unit/avi-to-mp4-page.test.ts
@@ -15,8 +15,9 @@ describe("AVI to MP4 page metadata", () => {
);
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("og.png");
});
it("contains OG url field", () => {
diff --git a/apps/web/__tests__/unit/best-screen-recorder-page.test.ts b/apps/web/__tests__/unit/best-screen-recorder-page.test.ts
index b5b71f2dd..269371ca1 100644
--- a/apps/web/__tests__/unit/best-screen-recorder-page.test.ts
+++ b/apps/web/__tests__/unit/best-screen-recorder-page.test.ts
@@ -32,8 +32,9 @@ describe("BestScreenRecorderPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets best-screen-recorder keyword", () => {
diff --git a/apps/web/__tests__/unit/canonical-urls.test.ts b/apps/web/__tests__/unit/canonical-urls.test.ts
index d760a9e4c..ddd55e981 100644
--- a/apps/web/__tests__/unit/canonical-urls.test.ts
+++ b/apps/web/__tests__/unit/canonical-urls.test.ts
@@ -119,7 +119,14 @@ describe("Canonical URLs", () => {
for (const { file, canonical } of expectedCanonicals) {
it(`${file} contains canonical "${canonical}"`, () => {
const content = readPage(file);
- expect(content).toContain(`canonical: "${canonical}"`);
+ // Pages either declare the absolute canonical directly or pass the
+ // relative path to buildMarketingMetadata (resolved against
+ // metadataBase at runtime — same output).
+ const relativePath = canonical.replace("https://cap.so", "");
+ const hasCanonical =
+ content.includes(`canonical: "${canonical}"`) ||
+ content.includes(`path: "${relativePath}"`);
+ expect(hasCanonical).toBe(true);
});
}
diff --git a/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts b/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts
index 17388c072..0f2815f66 100644
--- a/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts
+++ b/apps/web/__tests__/unit/developer-documentation-videos-page.test.ts
@@ -40,8 +40,9 @@ describe("DeveloperDocumentationVideosPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets developer documentation videos keyword", () => {
diff --git a/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts b/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts
index 0296b994b..a75617280 100644
--- a/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts
+++ b/apps/web/__tests__/unit/hipaa-compliant-screen-recording-page.test.ts
@@ -40,8 +40,9 @@ describe("HipaaCompliantScreenRecordingPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets hipaa-compliant-screen-recording keyword", () => {
diff --git a/apps/web/__tests__/unit/mac-screen-recording-with-audio-page.test.ts b/apps/web/__tests__/unit/mac-screen-recording-with-audio-page.test.ts
index 83ade7f90..139471df7 100644
--- a/apps/web/__tests__/unit/mac-screen-recording-with-audio-page.test.ts
+++ b/apps/web/__tests__/unit/mac-screen-recording-with-audio-page.test.ts
@@ -40,8 +40,9 @@ describe("MacScreenRecordingWithAudioPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets mac-screen-recording-with-audio keyword", () => {
diff --git a/apps/web/__tests__/unit/mov-to-mp4-page.test.ts b/apps/web/__tests__/unit/mov-to-mp4-page.test.ts
index 5b03243ab..5e2f9d5ef 100644
--- a/apps/web/__tests__/unit/mov-to-mp4-page.test.ts
+++ b/apps/web/__tests__/unit/mov-to-mp4-page.test.ts
@@ -15,8 +15,9 @@ describe("MOV to MP4 page metadata", () => {
);
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("og.png");
});
it("contains OG url field", () => {
diff --git a/apps/web/__tests__/unit/mp4-to-gif-page.test.ts b/apps/web/__tests__/unit/mp4-to-gif-page.test.ts
index 1d87c3db1..af8c29903 100644
--- a/apps/web/__tests__/unit/mp4-to-gif-page.test.ts
+++ b/apps/web/__tests__/unit/mp4-to-gif-page.test.ts
@@ -15,8 +15,9 @@ describe("MP4 to GIF page metadata", () => {
);
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("og.png");
});
it("contains OG url field", () => {
diff --git a/apps/web/__tests__/unit/obs-alternative-page.test.ts b/apps/web/__tests__/unit/obs-alternative-page.test.ts
index 9dfac6f30..a646e5b1c 100644
--- a/apps/web/__tests__/unit/obs-alternative-page.test.ts
+++ b/apps/web/__tests__/unit/obs-alternative-page.test.ts
@@ -30,8 +30,9 @@ describe("ObsAlternativePage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets obs-alternative keyword", () => {
diff --git a/apps/web/__tests__/unit/og-images.test.ts b/apps/web/__tests__/unit/og-images.test.ts
new file mode 100644
index 000000000..898095f7c
--- /dev/null
+++ b/apps/web/__tests__/unit/og-images.test.ts
@@ -0,0 +1,142 @@
+import { NextRequest } from "next/server";
+import { describe, expect, it } from "vitest";
+import { GET as getMarketingOg } from "../../app/api/og/route";
+import { signOgParams, verifyOgSignature } from "../../lib/og/signature";
+import { ogImageUrl } from "../../lib/og/url";
+import { formatDuration, renderVideoOg } from "../../lib/og/video-og";
+import {
+ richVideoLinkHtml,
+ videoPreviewImageUrl,
+} from "../../lib/video-share-clipboard";
+
+const pngSize = (buf: Buffer) => {
+ // PNG: 8-byte signature, IHDR length+type, then width/height (big endian).
+ expect(buf.subarray(0, 8)).toEqual(
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
+ );
+ return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20) };
+};
+
+const renderMarketing = async (query: string) => {
+ const res = await getMarketingOg(
+ new NextRequest(`http://localhost:3000/api/og${query}`),
+ );
+ expect(res.status).toBe(200);
+ return Buffer.from(await res.arrayBuffer());
+};
+
+describe("/api/og", () => {
+ it("renders the default 1200x630 PNG with immutable caching", async () => {
+ const res = await getMarketingOg(
+ new NextRequest("http://localhost:3000/api/og"),
+ );
+ expect(res.status).toBe(200);
+ expect(res.headers.get("content-type")).toContain("image/png");
+ expect(res.headers.get("cache-control")).toContain("immutable");
+ expect(res.headers.get("x-robots-tag")).toBe("noindex");
+ const { width, height } = pngSize(Buffer.from(await res.arrayBuffer()));
+ expect(width).toBe(1200);
+ expect(height).toBe(630);
+ });
+
+ it("renders signed titles", async () => {
+ const url = ogImageUrl({
+ title: "Simple, transparent pricing",
+ tag: "Pricing",
+ description: "Free for individuals. Powerful for teams.",
+ });
+ expect(url).toMatch(/^\/api\/og\?/);
+ const signedPng = await renderMarketing(url.replace("/api/og", ""));
+ const defaultPng = await renderMarketing("");
+ expect(pngSize(signedPng)).toEqual({ width: 1200, height: 630 });
+ // The signed title actually renders (differs from the default image).
+ expect(signedPng.equals(defaultPng)).toBe(false);
+ });
+
+ it("serves the default image for unsigned or forged params", async () => {
+ const defaultPng = await renderMarketing("");
+ const unsigned = await renderMarketing(
+ "?title=Free%20Bitcoin%20at%20evil.example",
+ );
+ const forged = await renderMarketing(
+ "?title=Free%20Bitcoin%20at%20evil.example&s=0000000000000000000000000000000",
+ );
+ // Signature for one title doesn't authorize different text.
+ const replayed = await renderMarketing(
+ `?title=${encodeURIComponent("Totally different words")}&s=${signOgParams(
+ { title: "Simple, transparent pricing" },
+ )}`,
+ );
+ expect(unsigned.equals(defaultPng)).toBe(true);
+ expect(forged.equals(defaultPng)).toBe(true);
+ expect(replayed.equals(defaultPng)).toBe(true);
+ });
+
+ it("verifies signatures over all params", () => {
+ const params = { title: "A", tag: "B", description: "C" };
+ expect(verifyOgSignature(params, signOgParams(params))).toBe(true);
+ expect(
+ verifyOgSignature({ ...params, tag: "X" }, signOgParams(params)),
+ ).toBe(false);
+ expect(verifyOgSignature(params, null)).toBe(false);
+ });
+});
+
+describe("video og", () => {
+ it("renders the video layout with caching headers", async () => {
+ const res = await renderVideoOg({
+ kind: "video",
+ video: {
+ title: "Cap walkthrough",
+ ownerName: "Richie",
+ duration: 204,
+ },
+ });
+ expect(res.status).toBe(200);
+ expect(res.headers.get("cache-control")).toContain(
+ "stale-while-revalidate",
+ );
+ const { width, height } = pngSize(Buffer.from(await res.arrayBuffer()));
+ expect(width).toBe(1200);
+ expect(height).toBe(630);
+ });
+
+ it.each(["locked", "password", "not-found"] as const)(
+ "renders the %s variant",
+ async (kind) => {
+ const res = await renderVideoOg({ kind });
+ expect(res.status).toBe(200);
+ pngSize(Buffer.from(await res.arrayBuffer()));
+ },
+ );
+
+ it("formats durations", () => {
+ expect(formatDuration(0)).toBe("0:00");
+ expect(formatDuration(64.4)).toBe("1:04");
+ expect(formatDuration(204)).toBe("3:24");
+ expect(formatDuration(3671)).toBe("1:01:11");
+ });
+});
+
+describe("rich share link", () => {
+ it("builds the preview image url", () => {
+ expect(videoPreviewImageUrl("https://cap.so", "abc123")).toBe(
+ "https://cap.so/api/video/preview?videoId=abc123&fallback=og",
+ );
+ });
+
+ it("escapes html in the clipboard markup", () => {
+ const html = richVideoLinkHtml({
+ url: 'https://cap.so/s/abc?"><script>',
+ title: '<img onerror=alert(1)> & "quotes"',
+ previewImageUrl: "https://cap.so/api/video/preview?videoId=abc",
+ });
+ expect(html).not.toContain("<script>");
+ expect(html).not.toContain("<img onerror");
+ expect(html).toContain("&lt;img onerror");
+ expect(html).toContain("&amp;");
+ // The link and image are still present.
+ expect(html).toContain('<a href="https://cap.so/s/abc?&quot;&gt;');
+ expect(html).toContain("<img src=");
+ });
+});
diff --git a/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts b/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts
index 00fab1333..c66095536 100644
--- a/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts
+++ b/apps/web/__tests__/unit/open-source-screen-recorder-page.test.ts
@@ -34,8 +34,9 @@ describe("OpenSourceScreenRecorderPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets open-source-screen-recorder keyword", () => {
diff --git a/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts b/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts
index 451334bb6..455eb33fd 100644
--- a/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts
+++ b/apps/web/__tests__/unit/self-hosted-screen-recording-page.test.ts
@@ -34,8 +34,9 @@ describe("SelfHostedScreenRecordingPage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets self-hosted screen recording keyword", () => {
diff --git a/apps/web/__tests__/unit/video-recording-software-page.test.ts b/apps/web/__tests__/unit/video-recording-software-page.test.ts
index b5a7d9fa4..4741fca8b 100644
--- a/apps/web/__tests__/unit/video-recording-software-page.test.ts
+++ b/apps/web/__tests__/unit/video-recording-software-page.test.ts
@@ -34,8 +34,9 @@ describe("VideoRecordingSoftwarePage metadata", () => {
expect(pageSource).toContain('locale: "en_US"');
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("https://cap.so/og.png");
});
it("title targets video-recording-software keyword", () => {
diff --git a/apps/web/__tests__/unit/video-speed-controller-page.test.ts b/apps/web/__tests__/unit/video-speed-controller-page.test.ts
index 68ddafb0e..c62d56f38 100644
--- a/apps/web/__tests__/unit/video-speed-controller-page.test.ts
+++ b/apps/web/__tests__/unit/video-speed-controller-page.test.ts
@@ -15,8 +15,9 @@ describe("Video Speed Controller page metadata", () => {
);
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("og.png");
});
it("contains OG url field", () => {
diff --git a/apps/web/__tests__/unit/webm-to-mp4-page.test.ts b/apps/web/__tests__/unit/webm-to-mp4-page.test.ts
index 184aa5ed1..9c1f95e69 100644
--- a/apps/web/__tests__/unit/webm-to-mp4-page.test.ts
+++ b/apps/web/__tests__/unit/webm-to-mp4-page.test.ts
@@ -15,8 +15,9 @@ describe("WebM to MP4 page metadata", () => {
);
});
- it("contains full OG image URL", () => {
- expect(pageSource).toContain('"https://cap.so/og.png"');
+ it("uses the dynamic OG image", () => {
+ expect(pageSource).toContain("ogImageUrl(");
+ expect(pageSource).not.toContain("og.png");
});
it("contains OG url field", () => {