Skip to content

Commit e983fc3

Browse files
committed
fix(cli): omit skipped Lottie previews
1 parent 7d9718d commit e983fc3

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

packages/cli/src/capture/mediaCapture.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
1+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
22
import { tmpdir } from "node:os";
33
import { join } from "node:path";
44
import type { Browser, Page } from "puppeteer-core";
@@ -75,6 +75,55 @@ describe("Lottie capture budget", () => {
7575
expect(previewPage.setViewport).not.toHaveBeenCalled();
7676
expect(previewPage.screenshot).not.toHaveBeenCalled();
7777
});
78+
79+
it("omits a preview path when the budget expires after Lottie readiness", async () => {
80+
const dir = tempDir();
81+
const lottieDir = join(dir, "assets", "lottie");
82+
mkdirSync(join(dir, "extracted"), { recursive: true });
83+
mkdirSync(lottieDir, { recursive: true });
84+
writeFileSync(
85+
join(lottieDir, "logo.json"),
86+
JSON.stringify({
87+
nm: "Logo",
88+
w: 100,
89+
h: 100,
90+
fr: 30,
91+
ip: 0,
92+
op: 30,
93+
layers: [],
94+
}),
95+
);
96+
97+
const screenshot = vi.fn(async () => undefined);
98+
const previewPage = {
99+
setViewport: vi.fn(async () => undefined),
100+
setContent: vi.fn(async () => undefined),
101+
evaluate: vi.fn(async () => undefined),
102+
waitForFunction: vi.fn(async () => undefined),
103+
screenshot,
104+
close: vi.fn(async () => undefined),
105+
};
106+
const browser = { newPage: vi.fn(async () => previewPage) } as unknown as Browser;
107+
let budgetChecks = 0;
108+
109+
await renderLottiePreviews(browser, lottieDir, dir, {
110+
remainingMs: () => (++budgetChecks < 4 ? 10_000 : 0),
111+
});
112+
113+
const manifest = JSON.parse(
114+
readFileSync(join(dir, "extracted", "lottie-manifest.json"), "utf-8"),
115+
);
116+
expect(screenshot).not.toHaveBeenCalled();
117+
expect(manifest).toHaveLength(1);
118+
expect(manifest[0]).toMatchObject({
119+
file: "assets/lottie/logo.json",
120+
name: "Logo",
121+
width: 100,
122+
height: 100,
123+
});
124+
expect(manifest[0]).not.toHaveProperty("preview");
125+
expect(existsSync(join(lottieDir, "previews", "logo-preview.png"))).toBe(false);
126+
});
78127
});
79128

80129
describe("video capture live budget", () => {

packages/cli/src/capture/mediaCapture.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export async function saveLottieAnimations(
122122
*
123123
* Opens each Lottie JSON in a headless Chrome page via lottie-web,
124124
* seeks to ~30% through the animation, and takes a transparent screenshot.
125-
* Writes a lottie-manifest.json with metadata + preview paths.
125+
* Writes a lottie-manifest.json with metadata and successfully rendered preview paths.
126126
*/
127127
// fallow-ignore-next-line complexity
128128
export async function renderLottiePreviews(
@@ -133,7 +133,7 @@ export async function renderLottiePreviews(
133133
): Promise<void> {
134134
const manifest: Array<{
135135
file: string;
136-
preview: string;
136+
preview?: string;
137137
name: string;
138138
width: number;
139139
height: number;
@@ -152,6 +152,7 @@ export async function renderLottiePreviews(
152152
const fr = raw.fr || 30;
153153
const dur = ((raw.op || 0) - (raw.ip || 0)) / fr;
154154
const previewName = file.replace(".json", "-preview.png");
155+
let preview: string | undefined;
155156

156157
// Render a mid-frame thumbnail using Puppeteer + lottie-web
157158
// Skip huge Lottie files for preview (CDP has a ~256MB message limit)
@@ -202,6 +203,7 @@ export async function renderLottiePreviews(
202203
type: "png",
203204
omitBackground: true,
204205
});
206+
preview = `assets/lottie/previews/${previewName}`;
205207
}
206208
} catch {
207209
/* preview rendering failed — non-critical */
@@ -211,7 +213,7 @@ export async function renderLottiePreviews(
211213

212214
manifest.push({
213215
file: `assets/lottie/${file}`,
214-
preview: `assets/lottie/previews/${previewName}`,
216+
...(preview ? { preview } : {}),
215217
name: raw.nm || file,
216218
width: raw.w || 0,
217219
height: raw.h || 0,

0 commit comments

Comments
 (0)