Skip to content

Commit fa56909

Browse files
saphidgithub-actions[bot]
authored andcommitted
fix(desktop): prevent browser screenshot filename collisions
1 parent f870c41 commit fa56909

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,40 @@ describe("PreviewManager", () => {
23572357
),
23582358
);
23592359

2360+
effectIt.effect("preserves both screenshots of the same site in the same millisecond", () =>
2361+
withManager((manager) =>
2362+
Effect.gen(function* () {
2363+
const firstPng = Buffer.from("first-preview-png");
2364+
const secondPng = Buffer.from("second-preview-png");
2365+
const pngImage = (png: Buffer) => ({
2366+
toPNG: () => png,
2367+
toJPEG: () => png,
2368+
getSize: () => ({ width: 1280, height: 720 }),
2369+
});
2370+
const capturePage = vi
2371+
.fn<() => Promise<ReturnType<typeof pngImage>>>()
2372+
.mockResolvedValueOnce(pngImage(firstPng))
2373+
.mockResolvedValueOnce(pngImage(secondPng));
2374+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
2375+
yield* manager.createTab("tab_1");
2376+
yield* manager.registerWebview("tab_1", 42);
2377+
// it.effect keeps TestClock fixed until explicitly advanced.
2378+
const first = yield* manager.captureScreenshot("tab_1");
2379+
const second = yield* manager.captureScreenshot("tab_1");
2380+
2381+
expect(first.createdAt).toBe(second.createdAt);
2382+
expect(first.id).not.toBe(second.id);
2383+
expect(first.path).not.toBe(second.path);
2384+
expect(first.id).toMatch(/^browser-screenshot-example-com-[a-z0-9]+-[0-9a-f]{8}$/);
2385+
expect(second.id).toMatch(/^browser-screenshot-example-com-[a-z0-9]+-[0-9a-f]{8}$/);
2386+
expect(writeFile.mock.calls).toEqual([
2387+
[first.path, firstPng],
2388+
[second.path, secondPng],
2389+
]);
2390+
}),
2391+
),
2392+
);
2393+
23602394
effectIt.effect("keeps every recorded guest unthrottled until its frame capture stops", () =>
23612395
withManager((manager) =>
23622396
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
29212921
wc,
29222922
),
29232923
]);
2924-
const id = `browser-screenshot-${artifactSiteSlug(wc.getURL())}-${millis.toString(36)}`;
2924+
const id = `browser-screenshot-${artifactSiteSlug(wc.getURL())}-${millis.toString(36)}-${NodeCrypto.randomUUID().slice(0, 8)}`;
29252925
const artifactPath = path.join(resolvedArtifactDirectory, `${id}.png`);
29262926
const data = image.toPNG();
29272927
yield* fileSystem.makeDirectory(resolvedArtifactDirectory, { recursive: true }).pipe(

0 commit comments

Comments
 (0)