|
1 | 1 | import fs from "node:fs/promises"; |
2 | 2 | import { createServer, type Server } from "node:http"; |
3 | 3 | import type { AddressInfo } from "node:net"; |
4 | | -import os from "node:os"; |
5 | 4 | import path from "node:path"; |
6 | 5 | import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { useAutoCleanupTempDirTracker } from "../../test-support.js"; |
7 | 7 | import { playwrightCore } from "./playwright-core.runtime.js"; |
8 | 8 | import { ensurePageState } from "./pw-session-state.js"; |
9 | 9 | import { closePlaywrightBrowserConnection, getPageForTargetId } from "./pw-session.js"; |
10 | | -import { waitForDownloadViaPlaywright } from "./pw-tools-core.downloads.js"; |
| 10 | +import { downloadViaPlaywright, waitForDownloadViaPlaywright } from "./pw-tools-core.downloads.js"; |
11 | 11 | import { getFreePort } from "./test-port.js"; |
12 | 12 |
|
13 | 13 | const runChromiumProof = process.env.OPENCLAW_BROWSER_DOWNLOAD_E2E === "1"; |
| 14 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
14 | 15 |
|
15 | 16 | function listen(server: Server): Promise<number> { |
16 | 17 | return new Promise((resolve, reject) => { |
@@ -47,22 +48,35 @@ describe.runIf(runChromiumProof)("managed Chromium download cancellation", () => |
47 | 48 | }); |
48 | 49 |
|
49 | 50 | it("does not let a cancelled waiter capture and write a later download", async () => { |
50 | | - const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-download-cancel-")); |
| 51 | + const rootDir = tempDirs.make("openclaw-download-cancel-"); |
51 | 52 | cleanup.push(async () => await fs.rm(rootDir, { recursive: true, force: true })); |
52 | 53 |
|
53 | | - const payload = Buffer.from("late-download-owned-by-cancelled-request\n"); |
| 54 | + const abandonedPayload = Buffer.from("abandoned-click-download\n"); |
| 55 | + const successorPayload = Buffer.from("successor-download\n"); |
54 | 56 | const downloadServer = createServer((request, response) => { |
55 | 57 | if (request.url === "/late.txt") { |
56 | 58 | response.writeHead(200, { |
57 | 59 | "content-disposition": 'attachment; filename="duplicate.txt"', |
58 | | - "content-length": String(payload.byteLength), |
| 60 | + "content-length": String(abandonedPayload.byteLength), |
59 | 61 | "content-type": "text/plain", |
60 | 62 | }); |
61 | | - response.end(payload); |
| 63 | + response.end(abandonedPayload); |
| 64 | + return; |
| 65 | + } |
| 66 | + if (request.url === "/successor.txt") { |
| 67 | + response.writeHead(200, { |
| 68 | + "content-disposition": 'attachment; filename="duplicate.txt"', |
| 69 | + "content-length": String(successorPayload.byteLength), |
| 70 | + "content-type": "text/plain", |
| 71 | + }); |
| 72 | + response.end(successorPayload); |
62 | 73 | return; |
63 | 74 | } |
64 | 75 | response.writeHead(200, { "content-type": "text/html; charset=utf-8" }); |
65 | | - response.end('<a id="download" href="/late.txt" download>Download</a>'); |
| 76 | + response.end( |
| 77 | + '<button id="delayed" disabled onclick="location.href=\'/late.txt\'">Delayed Download</button>' + |
| 78 | + '<a id="download" href="/successor.txt" download>Download</a>', |
| 79 | + ); |
66 | 80 | }); |
67 | 81 | const downloadPort = await listen(downloadServer); |
68 | 82 | cleanup.push(async () => await closeServer(downloadServer)); |
@@ -137,11 +151,73 @@ describe.runIf(runChromiumProof)("managed Chromium download cancellation", () => |
137 | 151 | finalOutcome: { kind: "rejected", message: "request aborted" }, |
138 | 152 | written: undefined, |
139 | 153 | successor: { |
140 | | - bytes: payload.toString("utf8"), |
| 154 | + bytes: successorPayload.toString("utf8"), |
141 | 155 | suggestedFilename: "duplicate.txt", |
142 | 156 | }, |
143 | 157 | }); |
144 | 158 |
|
| 159 | + const pageState = ensurePageState(controlledPage); |
| 160 | + pageState.roleRefs = { e1: { role: "button", name: "Delayed Download" } }; |
| 161 | + pageState.roleRefsMode = "role"; |
| 162 | + await page.evaluate(() => { |
| 163 | + setTimeout(() => { |
| 164 | + const delayed = document.querySelector<HTMLButtonElement>("#delayed"); |
| 165 | + if (delayed) { |
| 166 | + delayed.disabled = false; |
| 167 | + } |
| 168 | + }, 500); |
| 169 | + }); |
| 170 | + const sharedPath = path.join(outputRoot, "shared.txt"); |
| 171 | + const clickController = new AbortController(); |
| 172 | + const cancelledClick = downloadViaPlaywright({ |
| 173 | + cdpUrl, |
| 174 | + targetId, |
| 175 | + ref: "e1", |
| 176 | + path: sharedPath, |
| 177 | + rootDir: outputRoot, |
| 178 | + timeoutMs: 5_000, |
| 179 | + signal: clickController.signal, |
| 180 | + }); |
| 181 | + const clickOutcome = cancelledClick.then( |
| 182 | + () => ({ kind: "resolved" as const }), |
| 183 | + (error: unknown) => ({ |
| 184 | + kind: "rejected" as const, |
| 185 | + message: error instanceof Error ? error.message : String(error), |
| 186 | + }), |
| 187 | + ); |
| 188 | + await expect.poll(() => pageState.downloadWaiterDepth).toBe(1); |
| 189 | + clickController.abort(new Error("click request aborted")); |
| 190 | + const clickAfterAbort = await Promise.race([ |
| 191 | + clickOutcome, |
| 192 | + new Promise<{ kind: "pending" }>((resolve) => { |
| 193 | + setTimeout(() => resolve({ kind: "pending" }), 200); |
| 194 | + }), |
| 195 | + ]); |
| 196 | + expect(clickAfterAbort).toEqual({ kind: "rejected", message: "click request aborted" }); |
| 197 | + expect(pageState.downloadWaiterDepth).toBe(0); |
| 198 | + |
| 199 | + const clickSuccessor = waitForDownloadViaPlaywright({ |
| 200 | + cdpUrl, |
| 201 | + targetId, |
| 202 | + path: sharedPath, |
| 203 | + rootDir: outputRoot, |
| 204 | + timeoutMs: 5_000, |
| 205 | + }); |
| 206 | + await expect.poll(() => pageState.downloadWaiterDepth).toBe(1); |
| 207 | + const beforeSuccessorClick = await Promise.race([ |
| 208 | + clickSuccessor.then(() => "resolved" as const), |
| 209 | + new Promise<"pending">((resolve) => { |
| 210 | + setTimeout(() => resolve("pending"), 700); |
| 211 | + }), |
| 212 | + ]); |
| 213 | + expect(beforeSuccessorClick).toBe("pending"); |
| 214 | + await expect(fs.access(sharedPath)).rejects.toMatchObject({ code: "ENOENT" }); |
| 215 | + await page.locator("#download").click(); |
| 216 | + const clickSuccessorResult = await clickSuccessor; |
| 217 | + await expect(fs.readFile(clickSuccessorResult.path, "utf8")).resolves.toBe( |
| 218 | + successorPayload.toString("utf8"), |
| 219 | + ); |
| 220 | + |
145 | 221 | await closePlaywrightBrowserConnection({ cdpUrl }); |
146 | 222 | await context.close(); |
147 | 223 |
|
@@ -179,8 +255,12 @@ describe.runIf(runChromiumProof)("managed Chromium download cancellation", () => |
179 | 255 | expect(firstDuplicate.suggestedFilename).toBe("duplicate.txt"); |
180 | 256 | expect(secondDuplicate.suggestedFilename).toBe("duplicate.txt"); |
181 | 257 | expect(firstDuplicate.path).not.toBe(secondDuplicate.path); |
182 | | - await expect(fs.readFile(firstDuplicate.path, "utf8")).resolves.toBe(payload.toString("utf8")); |
183 | | - await expect(fs.readFile(secondDuplicate.path, "utf8")).resolves.toBe(payload.toString("utf8")); |
| 258 | + await expect(fs.readFile(firstDuplicate.path, "utf8")).resolves.toBe( |
| 259 | + successorPayload.toString("utf8"), |
| 260 | + ); |
| 261 | + await expect(fs.readFile(secondDuplicate.path, "utf8")).resolves.toBe( |
| 262 | + successorPayload.toString("utf8"), |
| 263 | + ); |
184 | 264 | expect((await fs.readdir(outputRoot)).some((name) => name.endsWith(".part"))).toBe(false); |
185 | 265 |
|
186 | 266 | await closePlaywrightBrowserConnection({ cdpUrl: restartedCdpUrl }); |
|
0 commit comments