Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ jobs:

**3. Push.** The job fails when the text doesn't show, and the run's artifacts hold a screenshot of the screen the phone saw. On a pull request the run leaves one comment, updated on every push:

> ✅ com.example.app launch tested on a Mobilebox cloud phone · Android 14 · 23 s · $0.01<br>
> ✅ com.example.app launch tested on a Mobilebox cloud phone · Android 14 · 6 s · $0.01<br>
> *(the last screen)*<br>
> Tested on Mobilebox
> **Steps** 1. Fresh cloud phone ready in 0.4 s · 2. Installed `com.example.app` 1.0 · 3. Opened it; "Welcome" on screen after 3.5 s · 4. Screenshot, then the phone is deleted<br>
> Tested on Mobilebox · Run your own phone

A real one: [the proof comment on this repo's self-test PR](https://github.com/mobilebox-dev/android-smoke-action/pull/2#issuecomment-6808386702).
Live ones: every pull request in [mobilebox-dev/proof-demo-calculator](https://github.com/mobilebox-dev/proof-demo-calculator/pulls?q=is%3Apr), a fork of Fossify Calculator that runs this action on each PR, and [this repo's self-test PR](https://github.com/mobilebox-dev/android-smoke-action/pull/2#issuecomment-6808386702).

The screenshot is an image file at an unguessable address on mobilebox.dev, not indexed and not shown anywhere else. It and the comment's line are deleted from Mobilebox after 30 days. Turn the comment off with `comment: false`: then nothing is posted or stored.

Expand Down Expand Up @@ -103,7 +104,7 @@ Use `continue-on-error: true` and `steps.<id>.outputs.result` to report instead
2. Uploads your APK, installs it with its permissions granted, and opens it.
3. Waits for `wait-for` on screen, or checks that the app is still in the foreground.
4. Saves a screenshot and uploads it as an artifact, on a pass and on a fail.
5. On a pull request (unless `comment: false`), posts or updates the proof comment.
5. On a pull request (unless `comment: false`), posts or updates the proof comment: the verdict, time and cost, the last screen and these steps with their times.
6. Deletes the phone: on a pass, on a fail, and when you cancel the job.

A lost create is retried with the same idempotency key, so a flaky network never makes a second phone.
Expand Down
20 changes: 16 additions & 4 deletions smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ for (const signal of ['SIGINT', 'SIGTERM']) {

const started = Date.now()
let passed = false
// The proof comment's steps (#184): what the run did, with its times, as the agent's log would say it.
const steps = []
const since = (t) => `${((Date.now() - t) / 1000).toFixed(1)} s`
try {
// Retrying with the same Idempotency-Key returns the same phone, so a lost response cannot leak a second one.
const idempotencyKey = crypto.randomUUID()
Expand All @@ -86,9 +89,12 @@ try {
}
if (phone.status !== 'ready') throw new Error(`Phone not ready: ${JSON.stringify(phone)}`)
ready = true
steps.push(`Fresh cloud phone ready in ${since(started)}`)

app = await call(`/v1/phones/${id}/apps?grant_permissions=true`, 'POST', await readFile(apk))
console.log(`Installed ${app.package} ${app.version_name ?? ''} on ${id}`)
steps.push(`Installed \`${app.package}\`${app.version_name ? ` ${app.version_name}` : ''}, permissions granted`)
const opened = Date.now()
let result = await call(`/v1/phones/${id}/actions`, 'POST', { actions: [
{ type: 'open_app', package: app.package },
{ type: 'wait', ms: 2000 },
Expand All @@ -102,6 +108,9 @@ try {
} while (!result.results[0].ok && result.results[0].error?.code === 'wait_timeout' && Date.now() < until)
}
const bad = result.results.find(item => !item.ok)
steps.push(waitFor
? (bad?.error?.code === 'wait_timeout' ? `Opened it; "${waitFor}" did not appear within ${timeout} s` : `Opened it; "${waitFor}" on screen after ${since(opened)}`)
: `Opened it; ${result.screen.app === app.package ? 'still in the foreground' : `not in the foreground (shows ${result.screen.app})`} after ${since(opened)}`)
if (bad?.error?.code === 'wait_timeout') throw new Error(`"${waitFor}" did not appear within ${timeout} s. Screen:\n${result.screen.text}`)
if (bad) throw new Error(`${JSON.stringify(bad.error)}\nScreen:\n${result.screen.text}`)
if (!waitFor && result.screen.app !== app.package) {
Expand Down Expand Up @@ -134,7 +143,7 @@ try {
}
}

// The run's proof as one PR comment per artifact name, edited on later pushes: one line, the last screen, our link.
// The run's proof as one PR comment per artifact name, edited on later pushes: one line, the last screen, the steps, our link.
// Only on a pull request with comment: true; nothing is published otherwise. If the token cannot comment (fork PRs, no
// pull-requests: write), the just-published proof is deleted again, so nothing is kept that nobody sees.
async function proofComment() {
Expand All @@ -148,9 +157,12 @@ async function proofComment() {
headers: { Authorization: `Bearer ${token}`, Accept: 'application/vnd.github+json', 'User-Agent': userAgent, 'X-GitHub-Api-Version': '2022-11-28' },
})
const proof = await call(`/v1/phones/${id}/proofs`, 'POST', { title: `${app.package} launch`, status: passed ? 'passed' : 'failed' })
const list = await github(`/repos/${repo}/issues/${number}/comments?per_page=100`)
const existing = list.ok ? (await list.json()).find(c => c.body?.startsWith(marker)) : undefined
const body = { body: `${marker}\n${proof.markdown}` }
const found = await github(`/repos/${repo}/issues/${number}/comments?per_page=100`)
const existing = found.ok ? (await found.json()).find(c => c.body?.startsWith(marker)) : undefined
// The steps go above our link, the last line of the API's markdown.
const lines = proof.markdown.split('\n'), link = lines.pop()
const list = [...steps, 'Screenshot of the last screen, then the phone is deleted'].map((s, i) => `${i + 1}. ${s}`)
const body = { body: [marker, ...lines, '**Steps**', ...list, '', link].join('\n') }
const sent = await (existing ? github(`/repos/${repo}/issues/comments/${existing.id}`, 'PATCH', body) : github(`/repos/${repo}/issues/${number}/comments`, 'POST', body))
if (sent.ok) {
output('comment-url', (await sent.json()).html_url ?? '')
Expand Down
Loading