fix(electron): number a download whose name is taken, and place downloads of an early session (ENG-892)

Merged
KD
20h ago
+61-6

What

  • A download whose name is already taken in the attempt's download directory is saved as note (1).txt, note (2).txt, and so on, as a browser numbers it. Before, the second download replaced the first.
  • A download from a session the app made before any window used it (session.fromPartition('persist:later') in the app's ready handler) is saved inside the attempt. Before, it went to the app's own downloads path, which can be the user's Downloads folder.

Why

The third PR of the stack for the review of #2237 (ENG-892), which added @e2e-dev/electron.

  • placeDownloads saved each download as <directory>/<filename>. A second download of the same name overwrote the first, and electron.waitForDownload then pointed two downloads at one file.
  • placeDownloads placed the default session, the sessions of open windows, and sessions created later. A session created before it ran, with no window on it yet, was in none of these, so its downloads were not placed.

How it works

  1. Before the main process is told where to save, placeDownloads lists the files already in the directory (packages/electron/src/app-process.ts:548). A relaunch in the same attempt keeps the files of the launch before it.
  2. In the main process, unique (app-process.ts:554) gives each download a name not in that list and not taken by an earlier download of this launch, compared without case, since two names that differ only in case are one file on macOS and Windows. Files the app writes into the directory itself are not counted.
  3. placeDownloads also places the session of each new webContents (app-process.ts:584), so a session made earlier is placed when a window first uses it.
  4. docs/electron.mdx (Downloads) says both.

A download that starts before the app's first window loads still goes where the app saves it, because placeDownloads runs after the first window. The docs say so.

Issue

Part of ENG-892. Stack: #2419 → #2420 → this PR → #2422 → #2423.

Verified

Ran it locally: yes

  • pnpm --filter @e2e-dev/electron test and test:integration, and the electron testbed, on this layer:
@e2e-dev/electron unit + integration: Tests  148 passed (148)
apps/electron-testbed:                Tests  75 passed (75)
  • The new tests of this PR on the code of the PR below it (the bug) and on this PR (the fix):
TestPR belowthis PR
launch.test.ts: saves inside the attempt a download from a session the app made before any window used itfailedpassed
testbed controls.e2e.ts: saves a download in the attempt's own directory, never the user's (second note (1).txt)failedpassed
  • The integration test also checks that nothing was written to the app's own downloads path.
  • The testbed's download test also covers the numbering across a relaunch and without case: after note.txt and note (1).txt, NOTE.txt is saved as NOTE (2).txt, and after app.restart() the next note.txt is note (3).txt. With the list of the files a launch found forced empty, the test fails at the relaunch step.
  • Behavior and timing against main: see the pnpm bench:ab table in #2423.
  • No changeset: @e2e-dev/electron is not released yet (0.0.0, not on npm).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Repeated downloads with the same filename are saved with numbered names, even when capitalization differs. Files written by the app itself don’t affect numbering.
    • Downloads started before the first app window loads are saved to the app’s normal download destination.
    • The Electron testbed now includes a “Download shouting note” link for NOTE.txt.
  • Documentation
    • Clarified how filename conflicts are handled and where early downloads are saved.