Skip to content

Commit 53488e8

Browse files
saphidgithub-actions[bot]
authored andcommitted
fix(desktop): prevent browser screenshot filename collisions
1 parent 43bd667 commit 53488e8

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,46 @@ 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 capturePage = vi.fn(async () => ({
2366+
toPNG: () => firstPng,
2367+
toJPEG: () => firstPng,
2368+
getSize: () => ({ width: 1280, height: 720 }),
2369+
}));
2370+
capturePage.mockResolvedValueOnce({
2371+
toPNG: () => firstPng,
2372+
toJPEG: () => firstPng,
2373+
getSize: () => ({ width: 1280, height: 720 }),
2374+
});
2375+
capturePage.mockResolvedValueOnce({
2376+
toPNG: () => secondPng,
2377+
toJPEG: () => secondPng,
2378+
getSize: () => ({ width: 1280, height: 720 }),
2379+
});
2380+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
2381+
yield* manager.createTab("tab_1");
2382+
yield* manager.registerWebview("tab_1", 42);
2383+
// it.effect keeps TestClock fixed until explicitly advanced.
2384+
const first = yield* manager.captureScreenshot("tab_1");
2385+
const second = yield* manager.captureScreenshot("tab_1");
2386+
2387+
expect(first.createdAt).toBe(second.createdAt);
2388+
expect(first.id).not.toBe(second.id);
2389+
expect(first.path).not.toBe(second.path);
2390+
expect(first.id).toMatch(/^browser-screenshot-example-com-[a-z0-9]+-[0-9a-f]{8}$/);
2391+
expect(second.id).toMatch(/^browser-screenshot-example-com-[a-z0-9]+-[0-9a-f]{8}$/);
2392+
expect(writeFile.mock.calls).toEqual([
2393+
[first.path, firstPng],
2394+
[second.path, secondPng],
2395+
]);
2396+
}),
2397+
),
2398+
);
2399+
23602400
effectIt.effect("keeps every recorded guest unthrottled until its frame capture stops", () =>
23612401
withManager((manager) =>
23622402
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)