Skip to content

Commit 06bc67f

Browse files
authored
feat(desktop): reusable screenshot workflow for agents (#826)
Signed-off-by: Will Pfleger <[email protected]>
1 parent 9f0c22a commit 06bc67f

4 files changed

Lines changed: 212 additions & 30 deletions

File tree

‎AGENTS.md‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,28 +224,50 @@ See [TESTING.md](TESTING.md) for the full multi-agent E2E guide.
224224

225225
### Desktop Screenshots (Playwright)
226226

227-
The desktop app is a Tauri app that cannot render in a plain browser without the
228-
E2E mock bridge. A standalone screenshot helper at
229-
`desktop/tests/helpers/screenshot.mjs` wraps the same mock bridge setup the E2E
230-
tests use (`addInitScript` + `window.__SPROUT_E2E__`) into a CLI tool.
227+
The desktop app requires the E2E mock bridge to render — it cannot run in a plain
228+
browser. Use `just desktop-screenshot` to capture screenshots (builds frontend,
229+
starts preview server, runs Playwright automatically):
231230

232231
```bash
233-
just desktop-build # build the frontend first
234-
cd desktop
235-
node tests/helpers/screenshot.mjs --name home
236-
node tests/helpers/screenshot.mjs --name channel --route /channels/general
237-
node tests/helpers/screenshot.mjs --name search --click open-search
238-
node tests/helpers/screenshot.mjs --name settings --click open-settings
232+
just desktop-screenshot --name home
233+
just desktop-screenshot --name channel --route /channels/general
234+
just desktop-screenshot --name search --click open-search
235+
just desktop-screenshot --name settings --click open-settings
239236
```
240237

241238
Options: `--name` (filename), `--route` (client route), `--click` (data-testid
242239
or CSS selector), `--wait` (ms, default 2000), `--viewport` (WxH, default
243-
1280x720), `--outdir` (default `test-results/screenshots`). Screenshots are
244-
saved as PNGs and the path is printed to stdout.
240+
1280x720), `--outdir` (default `test-results/screenshots`),
241+
`--messages` (JSON file path). Output is a PNG path on stdout.
245242

246-
The Playwright MCP browser (`@playwright/mcp`) is also configured but cannot
247-
drive the desktop app directly because it evaluates JS after page load — too
248-
late for the mock bridge. Use the MCP browser for non-Tauri pages only.
243+
Use `--messages` to inject content into a channel before capture. The JSON file
244+
is an array of `{ channelName, content, pubkey?, kind? }` objects — all must
245+
target the same channel (`[a-z0-9-]+`). When provided, `--route` is ignored.
246+
247+
```bash
248+
just desktop-screenshot --name code-blocks --messages /tmp/msgs.json
249+
```
250+
251+
```json
252+
[
253+
{ "channelName": "general", "content": "```typescript\nconst x = 42;\n```" },
254+
{ "channelName": "general", "content": "plain text message" }
255+
]
256+
```
257+
258+
Available mock channels: `general`, `random`, `design`, `sales`, `engineering`,
259+
`agents`, `watercooler`, `announcements`, `alice-tyler`, `bob-tyler`.
260+
261+
`scripts/post-screenshots.sh` hosts PNGs on a per-developer orphan branch
262+
(`agent-screenshots/<github-username>`) and posts a PR comment:
263+
264+
```bash
265+
./scripts/post-screenshots.sh 803 test-results/screenshots
266+
./scripts/post-screenshots.sh 803 test-results/screenshots body.md # custom body prepended
267+
```
268+
269+
Re-runs for the same PR overwrite previous images. Cleanup:
270+
`git push origin --delete agent-screenshots/<username>`.
249271

250272
---
251273

‎desktop/tests/helpers/screenshot.mjs‎

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
// --wait <ms> Milliseconds to wait before capture (default: 2000)
1717
// --viewport <WxH> Viewport dimensions (default: 1280x720)
1818
// --outdir <path> Output directory (default: test-results/screenshots)
19+
// --messages <path> JSON file with messages to inject before capture
1920

2021
import { parseArgs } from "node:util";
21-
import { existsSync, mkdirSync } from "node:fs";
22+
import { existsSync, mkdirSync, readFileSync } from "node:fs";
2223
import { resolve, join } from "node:path";
2324
import { chromium } from "@playwright/test";
2425

@@ -30,6 +31,7 @@ const { values: args } = parseArgs({
3031
wait: { type: "string", default: "2000" },
3132
viewport: { type: "string", default: "1280x720" },
3233
outdir: { type: "string", default: "test-results/screenshots" },
34+
messages: { type: "string" },
3335
},
3436
strict: true,
3537
});
@@ -110,20 +112,101 @@ await page.addInitScript(() => {
110112
window.__SPROUT_E2E_APP_BADGE_COUNT__ = 0;
111113
});
112114

113-
const url = args.route === "/" ? BASE_URL : `${BASE_URL}/#${args.route}`;
114-
await page.goto(url);
115-
await page.waitForTimeout(waitMs);
115+
try {
116+
if (args.messages) {
117+
if (args.route !== "/") {
118+
console.warn("warning: --route is ignored when --messages is provided");
119+
}
116120

117-
if (args.click) {
118-
const selector = args.click.startsWith("[")
119-
? args.click
120-
: `[data-testid="${args.click}"]`;
121-
await page.click(selector);
122-
await page.waitForTimeout(500);
123-
}
121+
let messages;
122+
try {
123+
messages = JSON.parse(readFileSync(resolve(args.messages), "utf8"));
124+
} catch (err) {
125+
console.error(`Failed to read messages file: ${err.message}`);
126+
process.exitCode = 1;
127+
throw err;
128+
}
129+
130+
if (
131+
!Array.isArray(messages) ||
132+
messages.length === 0 ||
133+
messages.some(
134+
(m) =>
135+
typeof m.channelName !== "string" || typeof m.content !== "string",
136+
)
137+
) {
138+
const msg =
139+
"messages file must be a non-empty array of { channelName: string, content: string, pubkey?: string, kind?: number }";
140+
console.error(msg);
141+
process.exitCode = 1;
142+
throw new Error(msg);
143+
}
144+
145+
const channels = new Set(messages.map((m) => m.channelName));
146+
if (channels.size > 1) {
147+
const msg =
148+
"All messages must target the same channelName for a single screenshot";
149+
console.error(msg);
150+
process.exitCode = 1;
151+
throw new Error(msg);
152+
}
153+
154+
const channelName = messages[0].channelName;
155+
156+
if (!/^[a-z0-9-]+$/.test(channelName)) {
157+
const msg = `Invalid channel name: ${channelName}`;
158+
console.error(msg);
159+
process.exitCode = 1;
160+
throw new Error(msg);
161+
}
162+
163+
await page.goto(BASE_URL);
164+
await page.waitForSelector(`[data-testid="channel-${channelName}"]`, {
165+
timeout: 10000,
166+
});
167+
await page.click(`[data-testid="channel-${channelName}"]`);
124168

125-
const filepath = join(outdir, `${args.name}.png`);
126-
await page.screenshot({ path: filepath });
127-
console.log(filepath);
169+
await page.waitForFunction(
170+
(name) =>
171+
window.__SPROUT_E2E_HAS_MOCK_LIVE_SUBSCRIPTION__?.({
172+
channelName: name,
173+
}) ?? false,
174+
channelName,
175+
{ timeout: 10000 },
176+
);
128177

129-
await browser.close();
178+
for (const msg of messages) {
179+
await page.evaluate(
180+
(m) => {
181+
window.__SPROUT_E2E_EMIT_MOCK_MESSAGE__?.(m);
182+
},
183+
{
184+
channelName: msg.channelName,
185+
content: msg.content,
186+
pubkey: msg.pubkey ?? DEFAULT_MOCK_PUBKEY,
187+
kind: msg.kind,
188+
},
189+
);
190+
}
191+
192+
await page.waitForTimeout(waitMs);
193+
} else {
194+
const url = args.route === "/" ? BASE_URL : `${BASE_URL}/#${args.route}`;
195+
await page.goto(url);
196+
await page.waitForTimeout(waitMs);
197+
}
198+
199+
if (args.click) {
200+
const selector = args.click.startsWith("[")
201+
? args.click
202+
: `[data-testid="${args.click}"]`;
203+
await page.click(selector);
204+
await page.waitForTimeout(500);
205+
}
206+
207+
const filepath = join(outdir, `${args.name}.png`);
208+
await page.screenshot({ path: filepath });
209+
console.log(filepath);
210+
} finally {
211+
await browser.close();
212+
}

‎justfile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ desktop-e2e-smoke:
198198
desktop-e2e-integration: _ensure-migrations
199199
cd {{desktop_dir}} && pnpm test:e2e:integration
200200

201+
# Take desktop screenshots using the mock bridge
202+
desktop-screenshot *ARGS:
203+
#!/usr/bin/env bash
204+
set -euo pipefail
205+
just desktop-build
206+
cd {{desktop_dir}}
207+
if ! curl -sf http://127.0.0.1:4173/ >/dev/null 2>&1; then
208+
python3 -m http.server 4173 -d dist >/dev/null 2>&1 &
209+
trap "kill $! 2>/dev/null || true" EXIT
210+
for i in $(seq 1 20); do curl -sf http://127.0.0.1:4173/ >/dev/null && break; sleep 0.5; done
211+
fi
212+
node tests/helpers/screenshot.mjs {{ARGS}}
213+
201214
# Mesh-compute e2e: the CI-safe layers (relay mesh signaling invariants + Playwright UI)
202215
mesh-e2e:
203216
cargo test -p sprout-relay mesh_signaling

‎scripts/post-screenshots.sh‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [[ $# -lt 2 ]]; then
5+
echo "Usage: $0 <pr-number> <png-dir> [comment-body-file]" >&2
6+
exit 1
7+
fi
8+
9+
PR="$1"
10+
PNG_DIR="$2"
11+
BODY_FILE="${3:-}"
12+
13+
if ! [[ "$PR" =~ ^[0-9]+$ ]]; then
14+
echo "error: PR number must be a positive integer" >&2
15+
exit 1
16+
fi
17+
18+
GH_USER=$(gh api user --jq .login)
19+
BRANCH="agent-screenshots/${GH_USER}"
20+
REPO="block/sprout"
21+
RAW_BASE="https://raw.githubusercontent.com/${REPO}/refs/heads/${BRANCH}"
22+
23+
mapfile -t PNGS < <(find "$PNG_DIR" -maxdepth 1 -name "*.png" -type f | sort)
24+
if [[ ${#PNGS[@]} -eq 0 ]]; then
25+
echo "error: no PNGs found in $PNG_DIR" >&2
26+
exit 1
27+
fi
28+
29+
EXISTING_ENTRIES=""
30+
if git fetch origin "refs/heads/${BRANCH}:refs/remotes/origin/${BRANCH}" 2>/dev/null; then
31+
EXISTING_ENTRIES=$(git ls-tree "origin/${BRANCH}" | grep -v $'\t'"\"\\{0,1\\}pr-${PR}--" || true)
32+
fi
33+
34+
NEW_ENTRIES=""
35+
IMAGE_URLS=()
36+
for PNG in "${PNGS[@]}"; do
37+
FILENAME=$(basename "$PNG")
38+
BLOB=$(git hash-object -w "$PNG")
39+
TREE_PATH="pr-${PR}--${FILENAME}"
40+
NEW_ENTRIES+="$(printf '100644 blob %s\t%s' "$BLOB" "$TREE_PATH")"$'\n'
41+
IMAGE_URLS+=("${RAW_BASE}/${TREE_PATH}")
42+
done
43+
44+
COMBINED=$(printf '%s\n' "$EXISTING_ENTRIES" "$NEW_ENTRIES" | grep -v '^$')
45+
TREE=$(echo "$COMBINED" | git mktree)
46+
47+
COMMIT=$(git commit-tree "$TREE" -m "screenshots: PR #${PR}")
48+
git push --force-with-lease origin "${COMMIT}:refs/heads/${BRANCH}"
49+
50+
IMAGES_SECTION=""
51+
for URL in "${IMAGE_URLS[@]}"; do
52+
FILENAME=$(basename "$URL")
53+
NAME="${FILENAME%.png}"
54+
IMAGES_SECTION+="![${NAME}](${URL})"$'\n\n'
55+
done
56+
57+
if [[ -n "$BODY_FILE" ]]; then
58+
COMMENT_BODY="$(cat "$BODY_FILE")"$'\n\n'"${IMAGES_SECTION}"
59+
else
60+
COMMENT_BODY="## Screenshots"$'\n\n'"${IMAGES_SECTION}"
61+
fi
62+
63+
gh pr comment "$PR" --repo "$REPO" --body "$COMMENT_BODY"
64+
echo "Posted ${#PNGS[@]} screenshot(s) to PR #${PR}"

0 commit comments

Comments
 (0)