Repository navigation
feat(ui): open the Script pane from Command Search #3515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,10 @@ | ||||||
| // Browser smoke test for the web app: starts a `trunk build` in headless Chrome, offline, and | ||||||
| // works it from the keyboard as a user would. Add Data from Command Search, a command, Undo and | ||||||
| // Redo, Save (a download), then a fresh page that opens the saved file and saves it again. It | ||||||
| // fails on any page error, console error or panic, and on a saved project that doesn't hold what | ||||||
| // was done. CI compiles the browser build; this runs it, where a clock or blocking call that only | ||||||
| // drives the app as a user would. Add Data from Command Search, Move Down, Undo and Redo, Save, | ||||||
| // then a fresh page that opens the saved file and saves it again. Open Script from Command Search, | ||||||
| // paste and run a script that edits the table, exports it as CSV, then exports the map as PNG. | ||||||
| // Check the downloaded rows and image pixels. | ||||||
| // It fails on any page error, console error or panic, and on files that don't reflect what was done. | ||||||
| // CI compiles the browser build; this runs it, where a clock or blocking call that only | ||||||
| // fails at run time shows (AGENTS.md › Never break wasm). | ||||||
| // | ||||||
| // cd apps/rustgis-web && trunk build --release && cargo xtask web-pwa ../../dist/web | ||||||
|
|
@@ -12,7 +14,7 @@ | |||||
| // | ||||||
| // No packages: Node's standard library, and Chrome driven through its DevTools protocol on a pipe. | ||||||
| // The app's interface is drawn on a canvas, so the test uses only what doesn't move when the | ||||||
| // interface is rearranged: shortcuts, Command Search by name, the browser's file chooser and | ||||||
| // interface is rearranged: shortcuts, Command Search by name, paste, the browser's file chooser and | ||||||
| // downloads. Nothing waits a fixed time; each step waits for the event it causes, and the project | ||||||
| // state is read from saved copies. | ||||||
|
|
||||||
|
|
@@ -242,9 +244,13 @@ const onCanvas = (what) => until(`document.activeElement?.id === "rustgis_canvas | |||||
| // frame or on a focus event it can miss while busy (starting up), so an idle app can drop the | ||||||
| // next key. Moving the pointer over the map makes it draw, and so check, first. | ||||||
| let nudges = 0; | ||||||
| const shortcut = async (key, what) => { | ||||||
| await onCanvas(what); | ||||||
| await send("Input.dispatchMouseEvent", { type: "mouseMoved", x: 800 + (nudges++ % 2), y: 450 }, session); | ||||||
| const shortcut = async (key, what, ready = onCanvas) => { | ||||||
| await ready(what); | ||||||
| const { x, y } = await js(`(() => { | ||||||
| const bounds = document.getElementById("rustgis_canvas").getBoundingClientRect(); | ||||||
| return { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height / 2 }; | ||||||
| })()`); | ||||||
| await send("Input.dispatchMouseEvent", { type: "mouseMoved", x: x + (nudges++ % 2), y }, session); | ||||||
| await frames(); | ||||||
| await press(key, true); | ||||||
| }; | ||||||
|
|
@@ -318,6 +324,13 @@ const finished = new Set(); | |||||
| listeners.add(({ method, params }) => { | ||||||
| if (method === "Browser.downloadProgress" && params.state === "completed") finished.add(params.guid); | ||||||
| }); | ||||||
| const download = async (begun, what) => { | ||||||
| const { guid, suggestedFilename } = await begun; | ||||||
| if (!finished.has(guid)) { | ||||||
| await event("Browser.downloadProgress", `completed download of ${what}`, (p) => p.guid === guid && p.state === "completed"); | ||||||
| } | ||||||
| return { bytes: readFileSync(join(downloads, guid)), suggestedFilename }; | ||||||
| }; | ||||||
| // Saves the project and returns the downloaded file. Ctrl+S asks for a file name the first time | ||||||
| // (Save As, whose name box takes Enter); a project with a name downloads straight away. | ||||||
| let saves = 0; | ||||||
|
|
@@ -335,11 +348,10 @@ const save = async () => { | |||||
| if (crashed || Date.now() > deadline) break; | ||||||
| await frames(); | ||||||
| } | ||||||
| const { guid, suggestedFilename } = await begun; | ||||||
| if (!finished.has(guid)) await event("Browser.downloadProgress", "finished download", (p) => p.guid === guid && p.state === "completed"); | ||||||
| const { bytes, suggestedFilename } = await download(begun, "the saved project"); | ||||||
| const file = join(downloads, `save-${++saves}-${suggestedFilename.replace(/[^\w.-]/g, "_")}`); | ||||||
| writeFileSync(file, readFileSync(join(downloads, guid))); | ||||||
| return { file, project: JSON.parse(readFileSync(file, "utf8")) }; | ||||||
| writeFileSync(file, bytes); | ||||||
| return { file, project: JSON.parse(bytes.toString("utf8")) }; | ||||||
| }; | ||||||
| // Saves until the project passes `ready`: the app reads files and runs work between frames, so a | ||||||
| // save shows when it has landed. Fails after the limit, saying what the last save held. | ||||||
|
|
@@ -355,6 +367,40 @@ const saveUntil = async (what, ready) => { | |||||
| const layerNames = (p) => JSON.stringify((p.maps?.[0]?.layers ?? []).map((l) => l.name)); | ||||||
| const value = (cell) => (cell && typeof cell === "object" ? Object.values(cell)[0] : cell); | ||||||
|
|
||||||
| const checkCsv = (bytes) => { | ||||||
| // The fixture only has plain cells: no commas, quotes or newlines within a cell. | ||||||
| const [header, ...rows] = bytes.toString("utf8").replace(/\r?\n$/, "").split(/\r?\n/); | ||||||
| if (header !== "parcel_id,value_usd") throw new Error(`CSV header: expected parcel_id,value_usd, got ${JSON.stringify(header)}`); | ||||||
| if (rows.length !== 3) throw new Error(`CSV row count: expected 3, got ${rows.length}`); | ||||||
| const edited = rows.map((row) => row.split(",")).find(([id]) => id === "00101"); | ||||||
| if (edited?.[1] !== "100500") throw new Error(`CSV parcel 00101: expected edited value 100500, got ${edited?.[1] ?? "no row"}`); | ||||||
| step(`CSV: ${rows.length} rows, parcel 00101 = ${edited[1]}`); | ||||||
| }; | ||||||
|
|
||||||
| const checkPng = async (bytes, width, height) => { | ||||||
| if (!bytes.subarray(0, 8).equals(Buffer.from("89504e470d0a1a0a", "hex"))) throw new Error("PNG signature is missing"); | ||||||
| // Let Chrome decode the downloaded bytes so this check does not need a PNG library. | ||||||
| const image = await js(`(async () => { | ||||||
| const bytes = Uint8Array.from(atob("${bytes.toString("base64")}"), (c) => c.charCodeAt(0)); | ||||||
| const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/png" })); | ||||||
| const { width, height } = bitmap; | ||||||
| if (width !== ${width} || height !== ${height}) { | ||||||
| bitmap.close(); | ||||||
| return { width, height }; | ||||||
| } | ||||||
| const ctx = new OffscreenCanvas(width, height).getContext("2d"); | ||||||
| ctx.drawImage(bitmap, 0, 0); | ||||||
| bitmap.close(); | ||||||
| const pixels = new Uint32Array(ctx.getImageData(0, 0, width, height).data.buffer); | ||||||
| return { width, height, varied: pixels.some((pixel) => pixel !== pixels[0]) }; | ||||||
| })()`).catch(() => { throw new Error("PNG download could not be decoded"); }); | ||||||
| if (image.width !== width || image.height !== height) { | ||||||
| throw new Error(`PNG dimensions: expected ${width}×${height}, got ${image.width}×${image.height}`); | ||||||
| } | ||||||
| if (!image.varied) throw new Error("PNG is one flat colour"); | ||||||
| step(`PNG: ${width}×${height}, ${bytes.length} bytes, more than one colour`); | ||||||
| }; | ||||||
|
|
||||||
| try { | ||||||
| await send("Browser.setDownloadBehavior", { behavior: "allowAndName", downloadPath: downloads, eventsEnabled: true }); | ||||||
| const { targetId } = await send("Target.createTarget", { url: "about:blank" }); | ||||||
|
|
@@ -409,6 +455,31 @@ try { | |||||
| const data = (p) => JSON.stringify([p.maps.map((m) => m.layers.map((l) => [l.name, l.dataset])), p.tables]); | ||||||
| if (data(again.project) !== data(redone.project)) failures.push("the reopened project's layers or tables differ from the saved ones"); | ||||||
| step(`${saves} saves; layers ${order(again.project)}`); | ||||||
|
|
||||||
| step("Script: edit assessments, then export the table to CSV and the map to PNG"); | ||||||
| const [csv, png] = ["edited-assessments.csv", "parcels-map.png"]; | ||||||
| const [width, height] = [320, 240]; | ||||||
| const script = [ | ||||||
| 'edit.calculateField {"table":"assessments","field":"value_usd","expression":"value_usd + 500"}', | ||||||
| `share.exportTable ${JSON.stringify({ table: "assessments", path: csv })}`, | ||||||
| "map.zoomFull {}", | ||||||
| `share.exportMap ${JSON.stringify({ path: png, width, height })}`, | ||||||
| ].join("\n"); | ||||||
| await search("Script"); | ||||||
| await typing("the Script editor after Command Search"); | ||||||
| // Pass a paste to eframe without reading or changing the host clipboard. | ||||||
| await js(`(() => { | ||||||
| const clipboardData = new DataTransfer(); | ||||||
| clipboardData.setData("text/plain", ${JSON.stringify(script)}); | ||||||
| document.dispatchEvent(new ClipboardEvent("paste", { clipboardData, cancelable: true })); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low-confidence robustness note on the synthetic paste.
Suggested change
Category: quality
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left for the release quality pass. |
||||||
| return true; | ||||||
| })()`); | ||||||
| const begun = (name) => event("Browser.downloadWillBegin", `script download of ${name}`, (p) => p.suggestedFilename === name); | ||||||
| const csvStarted = begun(csv); | ||||||
| const pngStarted = begun(png); | ||||||
| await shortcut("Enter", "the pasted script", typing); | ||||||
| checkCsv((await download(csvStarted, csv)).bytes); | ||||||
| await checkPng((await download(pngStarted, png)).bytes, width, height); | ||||||
| } catch (e) { | ||||||
| failures.push(e.message); | ||||||
| if (shot && !closed) { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
.catchturns every failure of the decode script into "PNG download could not be decoded", including a page error or a Chrome stop that thejshelper reports. That hides the cause in the failure message, which is what this smoke test exists to report. Keep the original error, or add it to the message:Category: quality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left for the release quality pass.