/** * What a finger dragged across an alert row means: a sideways swipe (far enough, clearly more sideways than up or down) acknowledges or snoozes; a scroll never does; a direction with * nothing bound to it does nothing. And the bar under the thumb that the page and its style sheet promise on a phone. * * Run with: npm test --workspace server */ import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { swipeCommits, swipeDirection, swipeOffset, SWIPE_COMMIT, SWIPE_MAX, SWIPE_START } from "../../web/src/utils/swipe.js"; const both = { right: true, left: true }; describe("is it a swipe", () => { test("not until it has gone a little way, and then only if it is clearly sideways", () => { assert.equal(swipeDirection(SWIPE_START - 1, 0), null, "a finger resting"); assert.equal(swipeDirection(30, 0), "right"); assert.equal(swipeDirection(-30, 0), "left"); assert.equal(swipeDirection(40, 30), null, "mostly down: a scroll"); assert.equal(swipeDirection(60, 40), "right", "sideways by half as much again as down"); assert.equal(swipeDirection(60, 41), null, "just short of it"); assert.equal(swipeDirection(-60, -20), "left", "up and left is still sideways enough"); assert.equal(swipeDirection(5, 200), null, "a plain scroll"); }); }); describe("where the row sits while it is held, and what letting go does", () => { test("it follows the finger, but no further than the limit, and not at all where nothing is bound", () => { assert.equal(swipeOffset(40, both), 40); assert.equal(swipeOffset(-40, both), -40); assert.equal(swipeOffset(500, both), SWIPE_MAX); assert.equal(swipeOffset(-500, both), -SWIPE_MAX); assert.equal(swipeOffset(60, { right: false, left: true }), 0, "nothing to the right"); assert.equal(swipeOffset(-60, { right: true, left: false }), 0, "nothing to the left"); assert.equal(swipeOffset(60, { right: true, left: false }), 60); }); test("letting go does it only past the commit distance, in a direction that has something, on a sideways drag", () => { assert.equal(swipeCommits(SWIPE_COMMIT, 0, both), "right"); assert.equal(swipeCommits(-SWIPE_COMMIT, 0, both), "left"); assert.equal(swipeCommits(SWIPE_COMMIT - 1, 0, both), null, "not far enough"); assert.equal(swipeCommits(-200, 10, { right: true, left: false }), null, "nothing bound to the left"); assert.equal(swipeCommits(200, 10, { right: false, left: true }), null, "nothing bound to the right"); assert.equal(swipeCommits(100, 100, both), null, "diagonal: a scroll that wandered"); assert.equal(swipeCommits(0, 0, both), null); }); test("it takes more than a nudge, and less than the whole width of a phone", () => { assert.ok(SWIPE_COMMIT >= 60 && SWIPE_COMMIT < 150); assert.ok(SWIPE_MAX > SWIPE_COMMIT); }); }); describe("the bar under the thumb", () => { const css = readFileSync(new URL("../../web/src/styles.css", import.meta.url), "utf8"); const shell = readFileSync(new URL("../../web/src/layout/AppShell.tsx", import.meta.url), "utf8"); const alerts = readFileSync(new URL("../../web/src/pages/Alerts.tsx", import.meta.url), "utf8"); test("it is not there on a wide screen, and on a phone it is fixed to the bottom, above the page, clear of the home bar", () => { assert.match(css, /\.thumb-bar \{\s*display: none;\s*\}/); assert.match(css, /@media \(max-width: 767\.98px\) \{\s*\.thumb-bar \{\s*position: fixed;\s*inset: auto 0 0 0;\s*z-index: 1025;\s*display: flex;\s*padding-bottom: env\(safe-area-inset-bottom\);/); }); test("its buttons are at least as tall as a finger, and the page and the toasts leave room for it", () => { assert.match(css, /\.thumb-item \{[^}]*min-height: 3\.5rem;/); assert.match(css, /\.page-body \{\s*padding-bottom: calc\(4\.5rem \+ env\(safe-area-inset-bottom\)\);/); assert.match(css, /\.toast-stack \{\s*bottom: calc\(4\.75rem \+ env\(safe-area-inset-bottom\)\);/); }); test("it holds the dashboard, the alerts (with how many), the servers and the menu — and a page the person has hidden is not offered", () => { for (const piece of ['to: "/", label: t("Dashboard")', 'to: "/alerts", label: t("Alerts")', 'to: "/servers", label: t("Servers")', "navHas(nav, item.to)", 'aria-label={t("Quick links")}', "thumb-badge"]) assert.ok(shell.includes(piece), piece); }); test("an alert can be taken or snoozed with one tap or one swipe, each with an Undo, and only while it is live and the person can", () => { for (const piece of ["async function quickAck", "async function quickSnooze", 'label: t("Undo")', "api.alerts.unack(a.id)", "api.alerts.unsnooze(a.id)", "onRight={canSilence && !a.silenced && !a.closed && !a.snoozed && !a.ack?.active", "onLeft={canSilence && !a.silenced && !a.closed && !a.snoozed"]) assert.ok(alerts.includes(piece), piece); }); });