|
| 1 | +// Drags apps in the sidebar and moves them from their menu, against the |
| 2 | +// production preview over demo data, in Chrome through playwright-core. |
| 3 | +import assert from "node:assert/strict"; |
| 4 | +import { execFileSync, spawn } from "node:child_process"; |
| 5 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { dirname, join } from "node:path"; |
| 8 | +import { createInterface } from "node:readline"; |
| 9 | +import { fileURLToPath } from "node:url"; |
| 10 | +import { chromium } from "playwright-core"; |
| 11 | + |
| 12 | +const root = join(dirname(fileURLToPath(import.meta.url)), "../.."); |
| 13 | +const temp = mkdtempSync(join(tmpdir(), "gotify-sidebar-order-test-")); |
| 14 | +const binary = join(temp, process.platform === "win32" ? "preview.exe" : "preview"); |
| 15 | +execFileSync("go", ["build", "-o", binary, "./cmd/preview"], { cwd: root }); |
| 16 | +const preview = spawn(binary, ["-addr", "127.0.0.1:0"], { cwd: root, stdio: ["ignore", "pipe", "inherit"], windowsHide: true }); |
| 17 | +const exited = new Promise<void>((resolve) => preview.once("exit", () => resolve())); |
| 18 | +let browser; |
| 19 | +try { |
| 20 | + const url = await new Promise<string>((resolve, reject) => { |
| 21 | + createInterface({ input: preview.stdout! }).once("line", resolve); |
| 22 | + preview.once("exit", () => reject(new Error("preview exited before listening"))); |
| 23 | + }); |
| 24 | + browser = await chromium.launch({ |
| 25 | + executablePath: process.env.CHROME ?? (process.platform === "win32" ? "C:/Program Files/Google/Chrome/Application/chrome.exe" : "/usr/bin/google-chrome"), |
| 26 | + }); |
| 27 | + const page = await browser.newPage({ viewport: { width: 1400, height: 800 } }); |
| 28 | + const errors: string[] = []; |
| 29 | + page.on("pageerror", (error) => errors.push(error.message)); |
| 30 | + await page.goto(url); |
| 31 | + await page.locator(".sidenav-app").first().waitFor(); |
| 32 | + |
| 33 | + // An app's name, from the label of its menu. |
| 34 | + const names = () => |
| 35 | + [...document.querySelectorAll(".sidenav-app")].map((el) => el.querySelector("[aria-label$=' options']")?.getAttribute("aria-label")?.replace(/ options$/, "")); |
| 36 | + const order = () => page.evaluate(names); |
| 37 | + const row = (name: string) => page.locator(".sidenav-app").filter({ hasText: name }); |
| 38 | + const settled = async (want: string[]) => { |
| 39 | + await page.waitForFunction(`JSON.stringify((${names})()) === ${JSON.stringify(JSON.stringify(want))}`); |
| 40 | + assert.deepEqual( |
| 41 | + await page.evaluate(async () => { |
| 42 | + const s = await (window as any).mygo.call("Desktop.State"); |
| 43 | + return s.servers.flatMap((sv: any) => sv.apps.map((a: any) => a.name)); |
| 44 | + }), |
| 45 | + want, |
| 46 | + "the backend's order", |
| 47 | + ); |
| 48 | + }; |
| 49 | + assert.deepEqual(await order(), ["Backup", "Monitoring", "Home Assistant", "CI", "Deploys"]); |
| 50 | + |
| 51 | + // Drag Home Assistant above Backup. |
| 52 | + const from = (await row("Home Assistant").boundingBox())!; |
| 53 | + const to = (await row("Backup").boundingBox())!; |
| 54 | + await page.mouse.move(from.x + 40, from.y + from.height / 2); |
| 55 | + await page.mouse.down(); |
| 56 | + await page.mouse.move(from.x + 40, from.y + from.height / 2 - 10, { steps: 3 }); |
| 57 | + await page.mouse.move(from.x + 40, to.y + 4, { steps: 10 }); |
| 58 | + await page.mouse.up(); |
| 59 | + await settled(["Home Assistant", "Backup", "Monitoring", "CI", "Deploys"]); |
| 60 | + assert.ok(!page.url().includes("/a/3"), "the drag must not open the app"); |
| 61 | + |
| 62 | + // A click still opens it. |
| 63 | + await row("Monitoring").getByText("Monitoring", { exact: true }).click(); |
| 64 | + await page.waitForURL(/#\/s\/1\/a\/2$/); |
| 65 | + |
| 66 | + // Move down from the app's menu. |
| 67 | + await row("CI").getByRole("button", { name: "CI options" }).click(); |
| 68 | + await page.getByRole("menuitem", { name: "Move down" }).click(); |
| 69 | + await settled(["Home Assistant", "Backup", "Monitoring", "Deploys", "CI"]); |
| 70 | + await row("CI").getByRole("button", { name: "CI options" }).click(); |
| 71 | + assert.equal(await page.getByRole("menuitem", { name: "Move down" }).getAttribute("aria-disabled"), "true", "the last app can't move down"); |
| 72 | + await page.keyboard.press("Escape"); |
| 73 | + |
| 74 | + // Drag does not cross servers: Deploys stays in Work Gotify. |
| 75 | + const deploys = (await row("Deploys").boundingBox())!; |
| 76 | + const top = (await row("Home Assistant").boundingBox())!; |
| 77 | + await page.mouse.move(deploys.x + 40, deploys.y + deploys.height / 2); |
| 78 | + await page.mouse.down(); |
| 79 | + await page.mouse.move(deploys.x + 40, deploys.y - 10, { steps: 3 }); |
| 80 | + await page.mouse.move(deploys.x + 40, top.y + 4, { steps: 10 }); |
| 81 | + await page.mouse.up(); |
| 82 | + await settled(["Home Assistant", "Backup", "Monitoring", "Deploys", "CI"]); |
| 83 | + |
| 84 | + // A finger drags after holding still; a swipe leaves the order alone. |
| 85 | + const touch = await (await browser.newContext({ viewport: { width: 1400, height: 800 }, hasTouch: true })).newPage(); |
| 86 | + touch.on("pageerror", (error) => errors.push(error.message)); |
| 87 | + await touch.goto(url); |
| 88 | + await touch.locator(".sidenav-app").first().waitFor(); |
| 89 | + const cdp = await touch.context().newCDPSession(touch); |
| 90 | + const touchRow = (name: string) => touch.locator(".sidenav-app").filter({ hasText: name }); |
| 91 | + const drag = async (name: string, to: string, hold: number) => { |
| 92 | + const box = (await touchRow(name).boundingBox())!; |
| 93 | + const x = box.x + 40; |
| 94 | + const y = box.y + box.height / 2; |
| 95 | + await cdp.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [{ x, y }] }); |
| 96 | + await touch.waitForTimeout(hold); |
| 97 | + const end = (await touchRow(to).boundingBox())!.y + 4; |
| 98 | + for (let i = 1; i <= 10; i++) { |
| 99 | + await cdp.send("Input.dispatchTouchEvent", { type: "touchMove", touchPoints: [{ x, y: y + ((end - y) * i) / 10 }] }); |
| 100 | + await touch.waitForTimeout(16); |
| 101 | + } |
| 102 | + await cdp.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] }); |
| 103 | + }; |
| 104 | + const touchOrder = () => touch.evaluate(names); |
| 105 | + const before = await touchOrder(); |
| 106 | + await drag("Backup", "CI", 0); |
| 107 | + await touch.waitForTimeout(300); |
| 108 | + assert.deepEqual(await touchOrder(), before, "a swipe must not reorder"); |
| 109 | + await drag("Monitoring", "Backup", 400); |
| 110 | + await touch.waitForFunction(`JSON.stringify((${names})()) === ${JSON.stringify(JSON.stringify(["Home Assistant", "Monitoring", "Backup", "Deploys", "CI"]))}`); |
| 111 | + |
| 112 | + assert.deepEqual(errors, []); |
| 113 | + console.log("sidebar order: ok"); |
| 114 | +} finally { |
| 115 | + await browser?.close(); |
| 116 | + preview.kill(); |
| 117 | + await exited; |
| 118 | + rmSync(temp, { recursive: true, force: true }); |
| 119 | +} |
0 commit comments