test(cli): cover browse screenshot default-path reservation and cleanup#2301
test(cli): cover browse screenshot default-path reservation and cleanup#2301yawbtng wants to merge 1 commit into
Conversation
The default screenshot-path behavior added in browserbase#2246 (write a file by default, collision counter instead of overwrite, clean up the reserved placeholder on failure) shipped without tests. Adds unit coverage for the three helpers in screenshot.ts: - getDefaultPathFromFlags: --path and --base64 opt out of a default; bare invocation reserves a .png (and .jpeg with --type jpeg) - reserveDefaultScreenshotPath: advances the -N collision counter instead of overwriting an existing file - removeIfEmpty: deletes the zero-byte placeholder from a failed capture, keeps a real screenshot, and no-ops on a missing path Exports the three helpers so they can be unit-tested directly, matching the existing pattern of importing pure driver functions from src/ in tests (see driver-commands.test.ts).
|
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
There was a problem hiding this comment.
No issues found across 2 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant User as CLI User
participant Cmd as Screenshot Command
participant Flags as getDefaultPathFromFlags
participant Reserve as reserveDefaultScreenshotPath
participant FS as File System
participant Clean as removeIfEmpty
User->>Cmd: stagehand browse screenshot
Cmd->>Flags: call with flags object
alt No --path, no --base64 (default path needed)
Flags->>Flags: pick extension (.png / .jpeg) from --type
Flags->>Reserve: get unique default path
Reserve->>FS: O_EXCL open(timestamped-name)
alt File exists (collision)
Reserve->>Reserve: increment counter, try "-N" variant
Reserve->>FS: O_EXCL open(next name)
end
FS-->>Reserve: placeholder file created (0 bytes)
Reserve-->>Flags: reserved path string
Flags-->>Cmd: default path
else --path or --base64 set
Flags-->>Cmd: undefined (no default needed)
end
Cmd->>FS: driver captures screenshot (writes file)
alt Capture succeeds
FS-->>Cmd: file has non-zero bytes → keep
else Capture fails
Cmd->>Clean: call removeIfEmpty(path)
Clean->>FS: statSync(path)
alt File exists and size === 0
Clean->>FS: unlinkSync(path)
else File has bytes or missing
Clean->>Clean: no‑op
end
FS-->>Clean: placeholder removed (if empty)
Clean-->>Cmd: done
end
why
browse screenshot's default-path behavior (added in #2246 — write a file by default, advance a collision counter instead of overwriting, clean up the reserved placeholder if capture fails) shipped without any tests.driver-commands.test.tsonly asserts the command name is registered.what changed
Adds
tests/screenshot-default-path.test.tscovering the three helpers insrc/commands/screenshot.ts:getDefaultPathFromFlags—--pathand--base64both opt out of a reserved default; a bare invocation reserves a.png(and.jpegwith--type jpeg).reserveDefaultScreenshotPath— advances the-Ncollision counter instead of overwriting an existing file (fake-timer-pinned timestamp so the collision is deterministic).removeIfEmpty— deletes the zero-byte placeholder from a failed capture, keeps a file that already has bytes, and no-ops when the path is already gone.Test-only, plus a one-line
exporton each of the three helpers so they can be unit-tested directly. This matches the existing convention of importing pure driver functions fromsrc/in tests (e.g.driver-commands.test.tsimportsresolveSelector,formatSnapshotTree,runtimeHandlers). Happy to relocate the helpers intosrc/lib/instead if you'd prefer them out of the command file.testing
vitest run tests/screenshot-default-path.test.ts→ 8 passing. Adjacent suites (driver-commands,cli-surface) still green;tsc -p tsconfig.jsonclean.Summary by cubic
Add tests for the
browse screenshotdefault-path behavior: reserve a file by default, increment-Non collisions, and clean up empty placeholders on failure. Exported helpers to allow direct unit testing.getDefaultPathFromFlags,reserveDefaultScreenshotPath, andremoveIfEmptyfromsrc/commands/screenshot.tsfor testability.Written for commit 3042188. Summary will update on new commits.