diff --git a/web/api/meta.ts b/web/api/meta.ts index 06679cb..95a57b1 100644 --- a/web/api/meta.ts +++ b/web/api/meta.ts @@ -13,7 +13,7 @@ function readIndex(): Promise { return index; } -/** /@:handle and /d/:did, with person-specific link-preview meta. */ +/** /:handle and /d/:did, with person-specific link-preview meta. */ export const GET = createMetaHandler({ readIndex, baseUrl: process.env.PUBLIC_BASE_URL, diff --git a/web/e2e/customise.spec.ts b/web/e2e/customise.spec.ts index 2b7372b..fc2ca7b 100644 --- a/web/e2e/customise.spec.ts +++ b/web/e2e/customise.spec.ts @@ -389,28 +389,70 @@ test.describe('lanyard customiser', () => { expect(lanyardSaves(writes)[0]?.hidden).toEqual([]); }); - test('switches to View as public and back', async ({ page }) => { + test('moves between Customise and View from the top bar (#150)', async ({ + page, + }) => { await openCustomiser(page); const sidebar = page.getByRole('complementary', { name: 'Style' }); await expect(sidebar).toBeVisible(); - await page.getByRole('button', { name: 'View as public' }).click(); + const nav = page + .getByRole('banner') + .getByRole('navigation', { name: 'Your Lanyards' }); + await expect(nav.getByRole('link', { name: 'Customise' })).toHaveAttribute( + 'aria-current', + 'page' + ); + await nav.getByRole('link', { name: 'View' }).click(); + await expect(page).toHaveURL(/\/alice\.test$/); await expect(sidebar).toBeHidden(); - await expect( - page.getByText('You’re seeing your lanyard as the public sees it.') - ).toBeVisible(); await expect(page.getByRole('switch', { name: 'Show Links' })).toBeHidden(); - await expect( - page.getByRole('button', { name: 'View as public' }) - ).toHaveAttribute('aria-pressed', 'true'); - await page.getByRole('button', { name: 'Edit' }).click(); + await expect(nav.getByRole('link', { name: 'View' })).toHaveAttribute( + 'aria-current', + 'page' + ); + await nav.getByRole('link', { name: 'Customise' }).click(); + await expect(page).toHaveURL(/\/alice\.test\/edit$/); await expect(sidebar).toBeVisible(); }); + test('gives phones a bottom bar, Show QR first among equals (#150)', async ({ + page, + }) => { + await page.setViewportSize({ width: 320, height: 640 }); + await openCustomiser(page); + const bar = page.getByRole('navigation', { name: 'Your Lanyards' }); + // Only the bottom bar's: the top bar's links are hidden on phones. + await expect(bar).toHaveCount(1); + const box = await bar.boundingBox(); + expect((box?.y ?? 0) + (box?.height ?? 0)).toBeCloseTo(640, 0); + const widths: Record = {}; + for (const name of ['Customise', 'View', 'Show QR', 'Your account']) { + const link = bar.getByRole('link', { name }); + await expect(link).toBeVisible(); + widths[name] = (await link.boundingBox())?.width ?? 0; + } + expect(widths['Show QR']).toBeGreaterThan( + Math.max( + widths.Customise ?? 0, + widths.View ?? 0, + widths['Your account'] ?? 0 + ) + ); + expect( + await page.evaluate(() => document.documentElement.scrollWidth) + ).toBeLessThanOrEqual(320); + await bar.getByRole('link', { name: 'Show QR' }).click(); + await expect(page).toHaveURL(/\/alice\.test\/share$/); + }); + test('asks before leaving unsaved changes: a link, Back and reload', async ({ page, }) => { await page.goto('/settings'); - await page.getByRole('link', { name: 'My lanyard' }).click(); + await page + .getByRole('banner') + .getByRole('link', { name: 'Customise' }) + .click(); await expect( page.getByRole('button', { name: 'Reorder Links' }) ).toBeEnabled(); @@ -424,25 +466,23 @@ test.describe('lanyard customiser', () => { await page.getByRole('link', { name: 'Lanyards' }).click(); await page.goBack(); await expect.poll(() => dialogs).toEqual(['confirm', 'confirm']); - await expect(page).toHaveURL(/\/lanyard$/); + await expect(page).toHaveURL(/\/alice\.test\/edit$/); // Dismissing the prompt cancels the reload, so don't wait for a load. await page.evaluate(() => window.location.reload()); await expect.poll(() => dialogs).toContain('beforeunload'); await expect(page.locator('html')).toHaveAttribute('data-accent', 'mint'); }); - test('never hides keyboard focus under the phone toolbar', async ({ - page, - }) => { + test('never hides keyboard focus under the bottom bar', async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); await openCustomiser(page); - const toolbar = page.getByRole('toolbar', { name: 'Customiser' }); + const toolbar = page.getByRole('navigation', { name: 'Your Lanyards' }); const hidden: string[] = []; for (let step = 0; step < 120; step += 1) { await page.keyboard.press('Tab'); const covered = await page.evaluate(() => { const focused = document.activeElement as HTMLElement | null; - const bar = document.querySelector('[role="toolbar"]'); + const bar = document.querySelector('[data-bottom-toolbar]'); if (!focused || !bar || bar.contains(focused)) return null; // Tabbing past the last control returns to the page itself. if (focused === document.body) return null; @@ -531,8 +571,10 @@ test.describe('lanyard customiser', () => { await expect( page.getByRole('complementary', { name: 'Style' }) ).toBeHidden(); - const toolbar = page.getByRole('toolbar', { name: 'Customiser' }); - const style = toolbar.getByRole('button', { name: 'Style' }); + // On phones, Style is in the top bar (#150). + const style = page + .getByRole('banner') + .getByRole('button', { name: 'Style' }); await style.click(); const sheet = page.getByRole('dialog', { name: 'Style' }); await expect(sheet).toBeVisible(); diff --git a/web/e2e/feedback.spec.ts b/web/e2e/feedback.spec.ts index ded8881..a7ae218 100644 --- a/web/e2e/feedback.spec.ts +++ b/web/e2e/feedback.spec.ts @@ -132,7 +132,7 @@ test.describe('feedback (#134)', () => { // On phones, support is at the foot of the Style sheet. await page - .getByRole('toolbar', { name: 'Customiser' }) + .getByRole('banner') .getByRole('button', { name: 'Style' }) .click(); const sheet = page.getByRole('dialog', { name: 'Style' }); @@ -162,7 +162,7 @@ test.describe('feedback (#134)', () => { await expect(form.getByRole('img', { name: 'Screenshot 1' })).toBeVisible(); await form.getByLabel('Describe screenshot 1').fill('The QR sheet'); await form.getByLabel('Include technical details').check(); - await expect(form.locator('pre')).toContainText('Page: /lanyard'); + await expect(form.locator('pre')).toContainText('Page: /alice.test/edit'); // Nothing in the dialog spills sideways at 320 px. const overflow = await page.evaluate( @@ -200,7 +200,7 @@ test.describe('feedback (#134)', () => { expect(post?.record.body).toContain( 'On my phone the bottom of the code is hidden.' ); - expect(post?.record.body).toContain('Page: /lanyard'); + expect(post?.record.body).toContain('Page: /alice.test/edit'); }); test('captures the page without the dialog over it', async ({ page }) => { diff --git a/web/e2e/fixtures.ts b/web/e2e/fixtures.ts index 30ed44e..11829ba 100644 --- a/web/e2e/fixtures.ts +++ b/web/e2e/fixtures.ts @@ -3,7 +3,7 @@ import { test as base, expect } from '@playwright/test'; export const HEADING = 'Welcome to Lanyards'; /** The homepage's live sample lanyard (#145), framed at `md` and up. */ -export const SAMPLE_PATH = '/@renderg.host'; +export const SAMPLE_PATH = '/renderg.host'; const APP_ORIGIN = 'http://127.0.0.1:4173'; diff --git a/web/e2e/landing.spec.ts b/web/e2e/landing.spec.ts index d783cbd..f6358b3 100644 --- a/web/e2e/landing.spec.ts +++ b/web/e2e/landing.spec.ts @@ -92,3 +92,89 @@ test.describe('the homepage (#145)', () => { await expect(handle).toBeInViewport(); }); }); + +test.describe('the homepage’s hero (#151)', () => { + /** The element around the heading that sways: its inline transform. */ + const swayOf = (page: import('@playwright/test').Page) => + page.evaluate(() => { + let element = document.querySelector('main h1'); + while (element && !element.classList.contains('origin-top')) { + element = element.parentElement; + } + return element?.style.transform ?? null; + }); + + /** Sweeps the mouse right across the badge, in small steps. */ + async function sweep(page: import('@playwright/test').Page) { + const box = await page + .getByRole('heading', { name: HEADING }) + .boundingBox(); + const y = (box?.y ?? 0) + (box?.height ?? 0) / 2; + await page.mouse.move((box?.x ?? 0) + 10, y); + await page.mouse.move((box?.x ?? 0) + (box?.width ?? 0) - 10, y, { + steps: 12, + }); + } + + for (const colorScheme of ['light', 'dark'] as const) { + test(`sets the header straight on the blue (${colorScheme})`, async ({ + page, + }) => { + await page.emulateMedia({ colorScheme }); + await page.setViewportSize({ width: 1440, height: 900 }); + await page.goto('/'); + await expect(page.getByRole('heading', { name: HEADING })).toBeVisible(); + const banner = page.getByRole('banner'); + await expect(banner.getByRole('link', { name: 'Sign in' })).toBeVisible(); + // No bar: nothing painted behind it, and the hero starts under it. + expect( + await banner.evaluate( + (header) => getComputedStyle(header).backgroundColor + ) + ).toBe('rgba(0, 0, 0, 0)'); + expect((await banner.boundingBox())?.y).toBe(0); + const ground = await page.evaluate(() => { + const hero = document.querySelector('main section'); + const stage = hero?.firstElementChild; + if (!stage) return null; + return { + top: stage.getBoundingClientRect().top, + colour: getComputedStyle(stage).backgroundColor, + }; + }); + expect(ground).toEqual({ top: 0, colour: 'rgb(64, 32, 223)' }); + const results = await new AxeBuilder({ page }) + .include('header') + .withTags(WCAG) + .analyze(); + expect(results.violations).toEqual([]); + }); + } + + test('sways the badge with the mouse, then lets it settle', async ({ + page, + }) => { + await page.setViewportSize({ width: 1440, height: 900 }); + await page.goto('/'); + await expect(page.getByRole('heading', { name: HEADING })).toBeVisible(); + expect(await swayOf(page)).toBe(''); + await sweep(page); + await expect + .poll(() => swayOf(page), { timeout: 2000 }) + .toMatch(/^rotate\(-?[\d.]+deg\)$/); + // Never more than a couple of degrees. + const angle = Number(/-?[\d.]+/.exec((await swayOf(page)) ?? '')?.[0]); + expect(Math.abs(angle)).toBeLessThan(2); + await expect.poll(() => swayOf(page), { timeout: 8000 }).toBe(''); + }); + + test('keeps the badge still with reduced motion', async ({ page }) => { + await page.emulateMedia({ reducedMotion: 'reduce' }); + await page.setViewportSize({ width: 1440, height: 900 }); + await page.goto('/'); + await expect(page.getByRole('heading', { name: HEADING })).toBeVisible(); + await sweep(page); + await page.waitForTimeout(300); + expect(await swayOf(page)).toBe(''); + }); +}); diff --git a/web/e2e/lanyard.spec.ts b/web/e2e/lanyard.spec.ts index 55944e8..e0bd258 100644 --- a/web/e2e/lanyard.spec.ts +++ b/web/e2e/lanyard.spec.ts @@ -13,7 +13,7 @@ const badge = (page: import('@playwright/test').Page) => test.describe('public lanyard, signed out', () => { test('shows the badge, links and Bluesky follow link', async ({ page }) => { await mockPerson(page); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect(badge(page)).toBeVisible(); const links = page.getByRole('region', { name: 'Links' }); await expect( @@ -41,7 +41,7 @@ test.describe('public lanyard, signed out', () => { }, }); await page.emulateMedia({ colorScheme }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect(page.getByRole('region', { name: 'Talks' })).toBeVisible(); await expect( page.getByRole('region', { name: 'Open to' }) @@ -57,7 +57,7 @@ test.describe('public lanyard, signed out', () => { }) => { await mockPerson(page); await page.setViewportSize({ width: 320, height: 640 }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect(badge(page)).toBeVisible(); const width = await page.evaluate( () => document.documentElement.scrollWidth @@ -83,7 +83,7 @@ test.describe('public lanyard, signed out', () => { }, }); await page.setViewportSize({ width: 320, height: 640 }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect(page.getByRole('region', { name: 'Skills' })).toBeVisible(); await expect(page.getByText(unbroken)).toBeVisible(); const width = await page.evaluate( @@ -98,7 +98,7 @@ test.describe('public lanyard, signed out', () => { const longName = `Ada${'x'.repeat(57)}`; await mockPerson(page, { sifa: { displayName: longName } }); await page.setViewportSize({ width: 320, height: 640 }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect( page.getByRole('heading', { level: 1, name: `Dr ${longName}` }) ).toBeVisible(); @@ -122,7 +122,7 @@ test.describe('public lanyard, signed out', () => { }) => { await mockPerson(page, { config: { accent } }); await page.emulateMedia({ colorScheme }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect(badge(page)).toBeVisible(); await expect(page.locator('dl', { hasText: 'Now' })).toBeVisible(); const result = await new AxeBuilder({ page }) @@ -138,7 +138,7 @@ test.describe('public lanyard, signed out', () => { page, }) => { await mockPerson(page); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); const roles = page.getByRole('region', { name: 'Roles' }); const rows = roles.getByRole('listitem'); await expect(rows).toHaveCount(3); @@ -168,7 +168,7 @@ test.describe('public lanyard, signed out', () => { }, }); await page.setViewportSize({ width: 320, height: 640 }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); const roles = page.getByRole('region', { name: 'Roles' }); const title = roles.getByText('Senior Research Software Engineer'); await expect(title).toBeVisible(); @@ -186,7 +186,7 @@ test.describe('public lanyard, signed out', () => { hidden: [`at://${ADA.did}/id.sifa.profile.position/3mpos2`], }, }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); const roles = page.getByRole('region', { name: 'Roles' }); await expect(roles.getByRole('listitem')).toHaveCount(2); await expect(roles).not.toContainText('Visiting Fellow'); @@ -196,10 +196,19 @@ test.describe('public lanyard, signed out', () => { await expect(page.getByRole('link', { name: /more role/ })).toHaveCount(0); }); + test('sends the beta’s /@handle links to /handle (#150)', async ({ + page, + }) => { + await mockPerson(page); + await page.goto('/@alice.test'); + await expect(page).toHaveURL('/alice.test'); + await expect(badge(page)).toBeVisible(); + }); + test('redirects an encoded @ to the canonical URL', async ({ page }) => { await mockPerson(page); await page.goto('/%40alice.test'); - await expect(page).toHaveURL('/@alice.test'); + await expect(page).toHaveURL('/alice.test'); await expect(badge(page)).toBeVisible(); }); @@ -209,7 +218,7 @@ test.describe('public lanyard, signed out', () => { 'https://public.api.bsky.app/xrpc/app.bsky.actor.searchActorsTypeahead**', (route) => route.fulfill({ json: { actors: [] } }) ); - await page.goto('/@nobody.test'); + await page.goto('/nobody.test'); await expect( page.getByRole('heading', { name: "We couldn't find @nobody.test" }) ).toBeVisible(); @@ -220,7 +229,7 @@ test.describe('public lanyard, signed out', () => { test('respects an opt-out from logged-out viewing', async ({ page }) => { await mockPerson(page, { labels: ['!no-unauthenticated'] }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect( page.getByText( 'This person has chosen to show their lanyard only to people who are signed in.' @@ -233,7 +242,7 @@ test.describe('public lanyard, signed out', () => { page, }) => { await mockPerson(page, { sifaDown: true }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); // The title comes from Sifa: without it, the name stands alone. await expect( page.getByRole('heading', { level: 1, name: 'Ada Example' }) @@ -286,7 +295,7 @@ test.describe('public lanyard, signed in', () => { }); await signIn(page, { did: BEA.did, scope: GRANULAR }); await page.setViewportSize({ width: 320, height: 640 }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect( page.getByRole('list', { name: 'What you share' }) ).toContainText(unbroken); @@ -302,7 +311,7 @@ test.describe('public lanyard, signed in', () => { await mockPerson(page); const writes = await mockViewer(page); await signIn(page, { did: BEA.did, scope: 'atproto' }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await page.getByRole('button', { name: 'Follow' }).click(); await expect( page.getByRole('heading', { name: 'Lanyards needs your permission' }) @@ -345,7 +354,7 @@ test.describe('pills at 320 px', () => { ], }, }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect(page.getByRole('region', { name: 'Skills' })).toBeVisible(); expect( await page.evaluate(() => document.documentElement.scrollWidth) @@ -358,7 +367,7 @@ test.describe('cards', () => { page, }) => { await mockPerson(page); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); const talks = page.getByRole('region', { name: 'Talks' }); await expect(talks).toBeVisible(); // Every card holds at most one link. @@ -388,7 +397,7 @@ test.describe('cards', () => { page, }) => { await mockPerson(page); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); const talks = page.getByRole('region', { name: 'Talks' }); const card = talks.locator('[data-card]:has(a)').first(); await expect(card).toBeVisible(); diff --git a/web/e2e/perf.spec.ts b/web/e2e/perf.spec.ts index 26050a6..8e63634 100644 --- a/web/e2e/perf.spec.ts +++ b/web/e2e/perf.spec.ts @@ -24,7 +24,7 @@ async function coldLoad(browser: Browser, baseURL: string): Promise { uploadThroughput: (750 * 1024) / 8, }); - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect( page.getByRole('heading', { level: 1, name: 'Dr Ada Example' }) ).toBeVisible({ timeout: 15_000 }); diff --git a/web/e2e/qr.spec.ts b/web/e2e/qr.spec.ts index 1753f4c..821fb99 100644 --- a/web/e2e/qr.spec.ts +++ b/web/e2e/qr.spec.ts @@ -28,7 +28,7 @@ test.describe('QR sheet', () => { await expect( page.getByRole('heading', { name: 'Customise your lanyard' }) ).toBeVisible(); - await page.getByRole('button', { name: 'Show QR' }).click(); + await page.getByRole('link', { name: 'Show QR' }).click(); await expect( page.getByRole('dialog', { name: 'Dr Ada Example' }) ).toBeVisible(); @@ -41,13 +41,15 @@ test.describe('QR sheet', () => { }) => { await context.grantPermissions(['clipboard-read', 'clipboard-write']); await page.goto('/lanyard'); - const show = page.getByRole('button', { name: 'Show QR' }); + const show = page.getByRole('link', { name: 'Show QR' }); await expect(show).toBeVisible(); expect( await page.evaluate(() => document.documentElement.scrollWidth) ).toBeLessThanOrEqual(320); await show.click(); + // Its own page now, //share (#150). + await expect(page).toHaveURL('/alice.test/share'); const sheet = page.getByRole('dialog', { name: 'Dr Ada Example' }); await expect(sheet).toBeVisible(); await expect(sheet.getByText('Scan to see my lanyard')).toBeVisible(); @@ -68,16 +70,17 @@ test.describe('QR sheet', () => { const copied = await page.evaluate(() => navigator.clipboard.readText()); expect(copied).toMatch(new RegExp(`/d/${ADA.did}$`)); + // Back where it was opened. await page.keyboard.press('Escape'); await expect(sheet).toBeHidden(); - await expect(show).toBeFocused(); + await expect(page).toHaveURL('/alice.test/edit'); }); test('keeps the whole code at least 240 px wide on a 320×640 screen (#129)', async ({ page, }) => { await page.goto('/lanyard'); - await page.getByRole('button', { name: 'Show QR' }).click(); + await page.getByRole('link', { name: 'Show QR' }).click(); const sheet = page.getByRole('dialog', { name: 'Dr Ada Example' }); const qr = sheet.getByRole('img', { name: /^QR code for / }); await expect(qr).toBeVisible(); @@ -103,7 +106,7 @@ test.describe('QR sheet', () => { const viewport = { width: 844, height: 390 }; await page.setViewportSize(viewport); await page.goto('/lanyard'); - await page.getByRole('button', { name: 'Show QR' }).click(); + await page.getByRole('link', { name: 'Show QR' }).click(); const sheet = page.getByRole('dialog', { name: 'Dr Ada Example' }); const qr = sheet.getByRole('img', { name: /^QR code for / }); await expect(qr).toBeVisible(); @@ -128,7 +131,7 @@ test.describe('QR sheet', () => { await mockPerson(page, { config: { accent: 'lime' } }); await page.setViewportSize({ width: 844, height: 390 }); await page.goto('/lanyard'); - await page.getByRole('button', { name: 'Show QR' }).click(); + await page.getByRole('link', { name: 'Show QR' }).click(); const sheet = page.getByRole('dialog', { name: 'Dr Ada Example' }); await expect(sheet).toHaveAttribute('data-accent', 'lime'); for (const name of ['Copy link', 'Close']) { @@ -171,7 +174,7 @@ test.describe('QR sheet', () => { const viewport = { width: 430, height: 390 }; await page.setViewportSize(viewport); await page.goto('/lanyard'); - await page.getByRole('button', { name: 'Show QR' }).click(); + await page.getByRole('link', { name: 'Show QR' }).click(); const sheet = page.getByRole('dialog', { name: 'Dr Ada Example' }); await expect(sheet).toBeVisible(); // Nothing spills or scrolls, in either direction. @@ -206,7 +209,7 @@ test.describe('QR sheet', () => { test('stays black on white in dark mode', async ({ page }) => { await page.emulateMedia({ colorScheme: 'dark' }); await page.goto('/lanyard'); - await page.getByRole('button', { name: 'Show QR' }).click(); + await page.getByRole('link', { name: 'Show QR' }).click(); const qr = page.getByRole('img', { name: /^QR code for / }); await expect(qr.locator('rect')).toHaveAttribute('fill', '#ffffff'); await expect(qr.locator('path')).toHaveAttribute('fill', '#000000'); @@ -223,7 +226,10 @@ test.describe('the Show QR badge, opened on someone else’s lanyard (#129)', () await page.route( 'https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile**', (route) => - new URL(route.request().url()).searchParams.get('actor') === BEA.did + // By DID or by handle, as the AppView answers. + [BEA.did, 'bea.test'].includes( + new URL(route.request().url()).searchParams.get('actor') ?? '' + ) ? route.fulfill({ json: { did: BEA.did, @@ -287,7 +293,7 @@ test.describe('the Show QR badge, opened on someone else’s lanyard (#129)', () test('wears the owner’s accent, and leaves the page’s alone', async ({ page, }) => { - await page.goto('/@alice.test'); + await page.goto('/alice.test'); await expect( page.getByRole('heading', { level: 1, name: 'Dr Ada Example' }) ).toBeVisible(); @@ -295,7 +301,7 @@ test.describe('the Show QR badge, opened on someone else’s lanyard (#129)', () page.evaluate(() => document.documentElement.dataset.accent); expect(await pageAccent()).toBe('mint'); - const show = page.getByRole('banner').getByRole('button', { + const show = page.getByRole('banner').getByRole('link', { name: 'Show QR', }); await show.click(); @@ -304,12 +310,13 @@ test.describe('the Show QR badge, opened on someone else’s lanyard (#129)', () await expect(sheet).toHaveAttribute('data-accent', 'pink'); const card = sheet.locator('.bg-accent').first(); await expect(card).toHaveCSS('background-color', 'rgb(247, 135, 154)'); - expect(await pageAccent()).toBe('mint'); const result = await new AxeBuilder({ page }).withTags(WCAG).analyze(); expect(result.violations).toEqual([]); + // Back on Ada's lanyard, in Ada's accent. await sheet.getByRole('button', { name: 'Close' }).click(); await expect(sheet).toBeHidden(); - expect(await pageAccent()).toBe('mint'); + await expect(page).toHaveURL('/alice.test'); + await expect.poll(pageAccent).toBe('mint'); }); }); diff --git a/web/scripts/bundleSize.ts b/web/scripts/bundleSize.ts index 0b20e67..250d532 100644 --- a/web/scripts/bundleSize.ts +++ b/web/scripts/bundleSize.ts @@ -8,7 +8,7 @@ export const INITIAL_JS_BUDGET_BYTES = 200 * 1024; * own (spec §2: initial JS for the public lanyard route ≤ 200 KB gzipped). */ export const PUBLIC_LANYARD_ROUTES = [ - 'routes/@{$handle}.tsx', + 'routes/$handle.tsx', 'routes/d.$did.tsx', ] as const; diff --git a/web/scripts/styleRules.test.ts b/web/scripts/styleRules.test.ts index d3ed046..68a560c 100644 --- a/web/scripts/styleRules.test.ts +++ b/web/scripts/styleRules.test.ts @@ -122,6 +122,8 @@ const CHROME = [ const LANYARD_ONLY = [ 'src/components/RichText/', 'src/features/qr/components/QrSheet/', + // Show QR as a page (#150): the badge's ground while it loads. + 'src/features/qr/components/SharePage/', 'src/features/qr/components/LanyardStraps/', ]; diff --git a/web/scripts/vercelConfig.test.ts b/web/scripts/vercelConfig.test.ts index 0409ece..9ea4d85 100644 --- a/web/scripts/vercelConfig.test.ts +++ b/web/scripts/vercelConfig.test.ts @@ -15,6 +15,7 @@ import { afterEach, describe, expect, it } from 'vitest'; type Config = { ignoreCommand: string; functions?: Record; + redirects?: { source: string; destination: string; permanent: boolean }[]; rewrites: { source: string; destination: string }[]; headers: { source: string; headers: { key: string; value: string }[] }[]; }; @@ -24,9 +25,21 @@ const config = JSON.parse( ) as Config; describe('vercel.json', () => { + // The beta's /@handle links, link previews included (#150). + it('sends /@handle on to /handle for good', () => { + expect(config.redirects).toEqual([ + { source: '/@:handle', destination: '/:handle', permanent: true }, + ]); + }); + + // A lanyard is one path segment with a dot in it: a handle. Files such + // as /sw.js are served before any rewrite. it('sends lanyard and image paths to the preview functions before the app', () => { expect(config.rewrites).toEqual([ - { source: '/@:handle', destination: '/api/meta?handle=:handle' }, + { + source: '/:handle([^/]+\\.[^/]+)', + destination: '/api/meta?handle=:handle', + }, { source: '/d/:did', destination: '/api/meta?did=:did' }, { source: '/og/p/:did.png', destination: '/api/og?did=:did' }, { source: '/og/default.png', destination: '/api/og' }, diff --git a/web/server/metaHandler.test.ts b/web/server/metaHandler.test.ts index 8616053..16d2473 100644 --- a/web/server/metaHandler.test.ts +++ b/web/server/metaHandler.test.ts @@ -28,7 +28,7 @@ const handler = (fetchImpl: typeof fetch = profileFetch) => describe('meta handler', () => { it('serves the app with person-specific meta, cached for ten minutes', async () => { const response = await handler()( - new Request('https://next.lanyards.app/@alice.test?handle=alice.test') + new Request('https://next.lanyards.app/alice.test?handle=alice.test') ); const html = await response.text(); expect(response.status).toBe(200); @@ -56,6 +56,16 @@ describe('meta handler', () => { ); }); + it('reads a bare handle from the path, the lanyard’s address (#150)', async () => { + const fetchImpl = vi.fn(profileFetch); + await handler(fetchImpl as unknown as typeof fetch)( + new Request('https://next.lanyards.app/alice.test') + ); + expect(String(vi.mocked(fetchImpl).mock.calls[0]?.[0])).toContain( + 'actor=alice.test' + ); + }); + it('serves default meta, briefly cached, when the person is unknown', async () => { const missing = (async () => Response.json( @@ -63,7 +73,7 @@ describe('meta handler', () => { { status: 400 } )) as typeof fetch; const response = await handler(missing)( - new Request('https://next.lanyards.app/@nobody.test?handle=nobody.test') + new Request('https://next.lanyards.app/nobody.test?handle=nobody.test') ); expect(response.status).toBe(200); expect(response.headers.get('cache-control')).toBe('public, s-maxage=60'); diff --git a/web/server/metaHandler.ts b/web/server/metaHandler.ts index 8bdc147..94bde96 100644 --- a/web/server/metaHandler.ts +++ b/web/server/metaHandler.ts @@ -16,7 +16,7 @@ type Options = { /** The handle or DID asked for, from the rewrite's query or the path. */ function subject(url: URL): { handle?: string; did?: string } { const handle = - url.searchParams.get('handle') ?? /^\/@([^/]+)$/.exec(url.pathname)?.[1]; + url.searchParams.get('handle') ?? /^\/@?([^/]+)$/.exec(url.pathname)?.[1]; const did = url.searchParams.get('did') ?? /^\/d\/([^/]+)$/.exec(url.pathname)?.[1]; const clean = (value?: string) => { @@ -36,7 +36,7 @@ function subject(url: URL): { handle?: string; did?: string } { } /** - * Serves the app's index.html for /@handle and /d/did with that person's + * Serves the app's index.html for /handle and /d/did with that person's * title, description and badge image, so link previews show who it is. * Anything unknown or failing gets the default meta, briefly cached. */ diff --git a/web/server/profileMeta.test.ts b/web/server/profileMeta.test.ts index c1446e4..74601d5 100644 --- a/web/server/profileMeta.test.ts +++ b/web/server/profileMeta.test.ts @@ -55,7 +55,7 @@ describe('fetchProfileMeta', () => { expect(meta).toEqual({ title: 'Ada Example (@alice.test) · Lanyards', description: 'Research Engineer', - canonical: 'https://lanyards.app/@alice.test', + canonical: 'https://lanyards.app/alice.test', image: `https://lanyards.app/og/p/${DID}.png`, imageAlt: "Ada Example's lanyard", type: 'profile', @@ -75,7 +75,7 @@ describe('fetchProfileMeta', () => { it('points the DID page at the handle URL', async () => { const meta = await fetchProfileMeta({ did: DID }, BASE, stubFetch({})); - expect(meta?.canonical).toBe('https://lanyards.app/@alice.test'); + expect(meta?.canonical).toBe('https://lanyards.app/alice.test'); }); it('gives nothing away for someone who opted out of logged-out viewing', async () => { diff --git a/web/server/profileMeta.ts b/web/server/profileMeta.ts index 18bd22a..8f5ebce 100644 --- a/web/server/profileMeta.ts +++ b/web/server/profileMeta.ts @@ -106,7 +106,7 @@ export async function fetchProfileMeta( title: `${person.displayName} (@${person.handle}) · Lanyards`, description: person.headline ?? `${person.displayName}'s conference lanyard`, - canonical: `${base}/@${person.handle}`, + canonical: `${base}/${person.handle}`, image: `${base}/og/p/${person.did}.png`, imageAlt: `${person.displayName}'s lanyard`, type: 'profile', diff --git a/web/src/app/customise.test.tsx b/web/src/app/customise.test.tsx index 9cc4211..79bb663 100644 --- a/web/src/app/customise.test.tsx +++ b/web/src/app/customise.test.tsx @@ -8,7 +8,7 @@ import { useSession } from '@/features/auth/session/useSession'; import { expectNoAxeViolations } from '@/test/axe'; import { server } from '@/test/msw'; import { mockListRecords } from '@/test/handlers'; -import { ADA } from '@/test/people'; +import { ADA, BEA } from '@/test/people'; import { servePerson, signedInSession, @@ -118,6 +118,9 @@ async function jobCheckbox() { const saveChanges = () => userEvent.click(screen.getByRole('button', { name: 'Save' })); +/** The owner's customiser (#150). */ +const EDIT = `/${ADA.handle}/edit`; + beforeEach(() => { vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); document.documentElement.dataset.accent = 'blue'; @@ -125,17 +128,31 @@ beforeEach(() => { // Lazily loaded routes and Sifa's retry-then-fallback path take a few // seconds under a full parallel run. -describe('/lanyard', { timeout: 20_000 }, () => { - it('sends signed-out visitors to sign in first', async () => { +describe('//edit', { timeout: 20_000 }, () => { + it('sends signed-out visitors to sign in, then back here', async () => { vi.mocked(useSession).mockReturnValue(signedOutSession()); + const { router } = renderRoute(EDIT); + await waitForRoute(router, `/?returnTo=${encodeURIComponent(EDIT)}`, 5000); + }); + + it('forwards /lanyard to your own //edit (#150)', async () => { + servePerson(); const { router } = renderRoute('/lanyard'); - await waitForRoute(router, '/?returnTo=%2Flanyard', 5000); + await waitForRoute(router, EDIT, 5000); + expect(await preview()).toBeInTheDocument(); + }); + + it('sends anyone else to that person’s lanyard (#150)', async () => { + vi.mocked(useSession).mockReturnValue(signedInSession(BEA.did)); + servePerson(); + const { router } = renderRoute(EDIT); + await waitForRoute(router, `/${ADA.handle}`, 5000); }); it('previews the owner’s lanyard as the owner sees it', async () => { servePerson(); sifaDown(); - renderRoute('/lanyard'); + renderRoute(EDIT); const region = await preview(); // The title comes from Sifa: without it, the name stands alone. expect( @@ -155,7 +172,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('the lanyard sits on its own ground, with no box round it', async () => { servePerson(); - const { container } = renderRoute('/lanyard'); + const { container } = renderRoute(EDIT); const region = await preview(); await within(region).findByRole( 'heading', @@ -171,57 +188,57 @@ describe('/lanyard', { timeout: 20_000 }, () => { expect(ground).toContainElement( screen.getByRole('heading', { level: 1, name: 'Customise your lanyard' }) ); - await userEvent.click( - screen.getByRole('button', { name: 'View as public' }) - ); - await within(await preview()).findByRole( - 'heading', - { level: 2, name: 'Dr Ada Example' }, - { timeout: 5000 } - ); - expect(grounds()).toHaveLength(1); }); it('shows only its own header, with the customiser’s actions', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); await preview(); const banners = screen.getAllByRole('banner'); expect(banners).toHaveLength(1); - expect(banners[0]).toHaveTextContent('Your lanyard'); + const banner = banners[0] as HTMLElement; + // Customise is where you are (#150). expect( - within(banners[0] as HTMLElement).getByRole('button', { name: 'Save' }) + within( + within(banner).getByRole('navigation', { name: 'Your Lanyards' }) + ).getByRole('link', { name: 'Customise' }) + ).toHaveAttribute('aria-current', 'page'); + expect( + within(banner).getByRole('button', { name: 'Save' }) ).toBeInTheDocument(); expect(screen.getAllByRole('main')).toHaveLength(1); }); - it('keeps support at the foot of the sidebar, even viewed as public (#134)', async () => { + it('View takes you to your lanyard as visitors see it (#150)', async () => { + servePerson(); + const { router } = renderRoute(EDIT); + await preview(); + await userEvent.click( + within(screen.getByRole('banner')).getByRole('link', { name: 'View' }) + ); + await waitFor(() => + expect(router.state.location.pathname).toBe(`/${ADA.handle}`) + ); + }); + + it('keeps support at the foot of the sidebar (#134)', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); await preview(); expect(screen.queryByRole('contentinfo')).toBeNull(); const support = screen.getByRole('region', { name: 'Beta support' }); expect( within(support).getByRole('button', { name: 'Give feedback' }) ).toBeInTheDocument(); - await userEvent.click( - screen.getByRole('button', { name: 'View as public' }) - ); - expect( - screen.getByRole('region', { name: 'Beta support' }) - ).toBeInTheDocument(); }); it('keeps support at the foot of the Style sheet on phones (#134)', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); await preview(); - const toolbar = screen.getByRole('toolbar', { name: 'Customiser' }); - expect( - within(toolbar).queryByRole('button', { name: 'Support' }) - ).toBeNull(); + // On phones, Style is in the top bar (#150). await userEvent.click( - within(toolbar).getByRole('button', { name: 'Style' }) + within(screen.getByRole('banner')).getByRole('button', { name: 'Style' }) ); const sheet = await screen.findByRole('dialog', { name: 'Style' }); const support = within(sheet).getByRole('region', { @@ -241,7 +258,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('lays the lanyard out beside a pinned style sidebar', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); const sidebar = await screen.findByRole( 'complementary', { name: 'Style' }, @@ -258,38 +275,10 @@ describe('/lanyard', { timeout: 20_000 }, () => { expect(screen.queryByRole('region', { name: 'More settings' })).toBeNull(); }); - it('View as public hides the sidebar and edit controls, and Edit brings them back', async () => { - servePerson(); - renderRoute('/lanyard'); - const region = await preview(); - await within(region).findByRole( - 'button', - { name: 'Hide Building on ATProto at Example Conf 2024' }, - { timeout: 8000 } - ); - await userEvent.click( - screen.getByRole('button', { name: 'View as public' }) - ); - expect(screen.queryByRole('complementary', { name: 'Style' })).toBeNull(); - expect(screen.queryByRole('switch', { name: 'Show Links' })).toBeNull(); - expect( - screen.getByText('You’re seeing your lanyard as the public sees it.') - ).toBeInTheDocument(); - expect( - within(await preview()).queryByRole('button', { - name: /^(Hide|Show) /, - }) - ).toBeNull(); - await userEvent.click(screen.getByRole('button', { name: 'Edit' })); - expect( - screen.getByRole('complementary', { name: 'Style' }) - ).toBeInTheDocument(); - }); - it('asks before leaving with unsaved changes', async () => { const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false); servePerson(); - const { router } = renderRoute('/lanyard'); + const { router } = renderRoute(EDIT); const mint = await accent('Mint'); await waitFor(() => expect(mint).toBeEnabled(), { timeout: 5000 }); await userEvent.click(mint); @@ -297,13 +286,13 @@ describe('/lanyard', { timeout: 20_000 }, () => { expect(confirm).toHaveBeenCalledWith( 'You have unsaved changes. Leave without saving?' ); - expect(router.state.location.pathname).toBe('/lanyard'); + expect(router.state.location.pathname).toBe(EDIT); confirm.mockRestore(); }); it('gives the page one level-one heading, its purpose', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); const region = await preview(); await within(region).findByRole( 'heading', @@ -315,25 +304,10 @@ describe('/lanyard', { timeout: 20_000 }, () => { ).toEqual(['Customise your lanyard']); }); - it('switches the preview to what visitors see', async () => { - servePerson(); - sifaDown(); - renderRoute('/lanyard'); - const region = await preview(); - await within(region).findAllByText("Couldn't load Sifa", undefined, { - timeout: 8000, - }); - const toggle = screen.getByRole('button', { name: 'View as public' }); - expect(toggle).toHaveAttribute('aria-pressed', 'false'); - await userEvent.click(toggle); - expect(toggle).toHaveAttribute('aria-pressed', 'true'); - expect(within(region).queryAllByText("Couldn't load Sifa")).toHaveLength(0); - }); - it('applies and saves a new accent, announcing progress', async () => { servePerson(); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); const mint = await accent('Mint'); await waitFor(() => expect(mint).toBeEnabled(), { timeout: 5000 }); await userEvent.click(mint); @@ -356,7 +330,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('resets unsaved changes to the last save, writing nothing', async () => { servePerson(); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); const mint = await accent('Mint'); await waitFor(() => expect(mint).toBeEnabled(), { timeout: 5000 }); const save = screen.getByRole('button', { name: 'Save' }); @@ -379,7 +353,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('saves the current-role switch', async () => { servePerson(); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); const job = await jobCheckbox(); await waitFor(() => expect(job).toBeEnabled(), { timeout: 5000 }); expect(job).toBeChecked(); @@ -395,7 +369,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('asks for permission instead of letting an identity-only session edit', async () => { vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did, 'atproto')); servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); expect( await screen.findByRole( 'heading', @@ -418,7 +392,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { : undefined ) ); - renderRoute('/lanyard'); + renderRoute(EDIT); await preview(); expect( await screen.findByText( @@ -433,7 +407,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('saves a new block order', async () => { servePerson(); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); const links = await screen.findByRole( 'listitem', { name: 'Links' }, @@ -455,7 +429,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('hides a record in place, then shows it again', async () => { servePerson(); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); const region = await preview(); const hide = await within(region).findByRole( 'button', @@ -487,7 +461,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { }, }); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); const region = await preview(); await userEvent.click( await within(region).findByRole( @@ -508,7 +482,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('links can only be hidden', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); const links = await linksBlock(); expect( await within(links).findByRole( @@ -527,7 +501,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('has no Title control: the title comes from Sifa', async () => { servePerson(); - renderRoute('/lanyard'); + renderRoute(EDIT); await enabled('Hide GitHub'); expect(screen.queryByRole('combobox', { name: 'Title' })).toBeNull(); }); @@ -546,7 +520,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { ) ); servePerson({ config: { blocks: [{ type: 'media', visible: true }] } }); - renderRoute('/lanyard'); + renderRoute(EDIT); expect(await enabled('Add a podcast or video')).toBeEnabled(); expect( screen.queryByRole('heading', { name: 'Lanyards needs your permission' }) @@ -575,7 +549,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { () => new Promise(() => {}) ) ); - renderRoute('/lanyard'); + renderRoute(EDIT); expect( await screen.findByText('Updating your podcasts and videos…', undefined, { timeout: 8000, @@ -596,7 +570,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { ], }, }); - renderRoute('/lanyard'); + renderRoute(EDIT); const region = await preview(); const publications = await within(region).findByRole( 'listitem', @@ -615,7 +589,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('adds a podcast in a dialog; it’s saved only on Save', async () => { servePerson({ config: { blocks: [{ type: 'media', visible: true }] } }); const batches = captureWrites(); - renderRoute('/lanyard'); + renderRoute(EDIT); await userEvent.click(await enabled('Add a podcast or video')); const dialog = screen.getByRole('dialog', { name: 'Add a podcast or video', @@ -660,7 +634,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { servePerson({ config: { blocks: [{ type: 'media', visible: true }] } }); const batches = captureWrites(); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); await userEvent.click(await enabled('Add a podcast or video')); const dialog = screen.getByRole('dialog', { name: 'Add a podcast or video', @@ -700,7 +674,7 @@ describe('/lanyard', { timeout: 20_000 }, () => { config: { blocks: [{ type: 'links', visible: true, limit: 0 }] }, }); const saves = captureSaves(); - renderRoute('/lanyard'); + renderRoute(EDIT); expect( await screen.findByText( "We couldn't read your lanyard settings", @@ -723,13 +697,18 @@ describe('/lanyard', { timeout: 20_000 }, () => { it('has no accessibility violations', async () => { servePerson(); - const { container } = renderRoute('/lanyard'); + const { container } = renderRoute(EDIT); const region = await preview(); await within(region).findByRole( 'heading', { level: 2, name: 'Dr Ada Example' }, { timeout: 5000 } ); - await expectNoAxeViolations(container); + // The bottom bar and the header's links are never shown together, + // but jsdom applies no CSS. MobileNav has its own axe test, and e2e + // runs axe in a real browser. + await expectNoAxeViolations(container, { + exclude: ['[data-bottom-toolbar]'], + }); }); }); diff --git a/web/src/app/landing.test.tsx b/web/src/app/landing.test.tsx index 5678011..fd77dba 100644 --- a/web/src/app/landing.test.tsx +++ b/web/src/app/landing.test.tsx @@ -4,6 +4,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; import { useSession } from '@/features/auth/session/useSession'; import { TYPEAHEAD_URL } from '@/test/fixtures'; +import { ADA } from '@/test/people'; +import { servePerson, signedInSession } from '@/test/person'; import { server } from '@/test/msw'; import { renderRoute, waitForRoute } from '@/test/router'; @@ -79,10 +81,11 @@ describe('landing page', () => { expect(document.title).toBe('Sign in · Lanyards'); }); - it('sends a signed-in visitor to their lanyard', async () => { - vi.mocked(useSession).mockReturnValue(signedIn); + it('sends a signed-in visitor to their own customiser (#150)', async () => { + vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); + servePerson(); const { router } = renderRoute('/'); - await waitForRoute(router, '/lanyard', LAZY_ROUTE_TIMEOUT); + await waitForRoute(router, `/${ADA.handle}/edit`, LAZY_ROUTE_TIMEOUT); }); it('honours a safe return path for signed-in visitors', async () => { @@ -92,8 +95,9 @@ describe('landing page', () => { }); it('ignores an unsafe return path', async () => { - vi.mocked(useSession).mockReturnValue(signedIn); + vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); + servePerson(); const { router } = renderRoute('/?returnTo=%2F%2Fevil.com'); - await waitForRoute(router, '/lanyard', LAZY_ROUTE_TIMEOUT); + await waitForRoute(router, `/${ADA.handle}/edit`, LAZY_ROUTE_TIMEOUT); }); }); diff --git a/web/src/app/lanyard.test.tsx b/web/src/app/lanyard.test.tsx index c4267d7..f003768 100644 --- a/web/src/app/lanyard.test.tsx +++ b/web/src/app/lanyard.test.tsx @@ -37,7 +37,7 @@ beforeEach(() => { describe('public lanyard routes', () => { it('shows the badge for a handle', async () => { servePerson(); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect(await hero()).toBeInTheDocument(); expect(screen.getByText('she/her · @alice.test')).toBeInTheDocument(); expect( @@ -54,7 +54,7 @@ describe('public lanyard routes', () => { it("keeps the lanyard's own background on its public page", async () => { servePerson(); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); const name = await hero(); // The app chrome is neutral (#118); the lanyard keeps its brand ground. expect(name.closest('.bg-background')).not.toBeNull(); @@ -67,8 +67,12 @@ describe('public lanyard routes', () => { }); it.each([ - ['/@Alice.Test', '/@alice.test'], - ['/%40alice.test', '/@alice.test'], + ['/Alice.Test', '/alice.test'], + // The beta's links, and some apps' encoding of them (#150). + ['/@alice.test', '/alice.test'], + ['/@Alice.Test', '/alice.test'], + ['/%40alice.test', '/alice.test'], + [`/${ADA.did}`, `/d/${ADA.did}`], [`/@${ADA.did}`, `/d/${ADA.did}`], ])('redirects %s to %s', async (from, to) => { servePerson(); @@ -77,7 +81,7 @@ describe('public lanyard routes', () => { expect(await hero()).toBeInTheDocument(); }); - it.each(['/%40alice%', '/@alice%'])( + it.each(['/%40alice%', '/@alice%', '/alice%', '/not-a-handle'])( 'shows the not-found page, not an error, for a malformed escape in %s', async (path) => { renderRoute(path); @@ -100,7 +104,7 @@ describe('public lanyard routes', () => { describe('lanyard page states', () => { it('offers search when a handle does not exist', async () => { servePerson(); - const { container } = renderRoute('/@nobody.test'); + const { container } = renderRoute('/nobody.test'); expect( await screen.findByRole('heading', { name: "We couldn't find @nobody.test", @@ -122,7 +126,7 @@ describe('lanyard page states', () => { } ) ); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); const loading = await screen.findByText('Loading lanyard…', undefined, { timeout: 5000, }); @@ -137,7 +141,7 @@ describe('lanyard page states', () => { return HttpResponse.json({}); }) ); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect( await screen.findByText('Loading lanyard…', undefined, { timeout: 5000 }) ).toBeInTheDocument(); @@ -154,7 +158,7 @@ describe('lanyard page states', () => { it("keeps the lanyard's own ground and palette for an unknown handle", async () => { servePerson(); - renderRoute('/@nobody.test'); + renderRoute('/nobody.test'); const heading = await screen.findByRole('heading', { name: "We couldn't find @nobody.test", }); @@ -168,7 +172,7 @@ describe('lanyard page states', () => { servePerson({ profileError: { error: 'AccountDeactivated', status: 400 }, }); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect( await screen.findByRole('heading', { name: "This account isn't available", @@ -189,7 +193,7 @@ describe('lanyard page states', () => { ], }, }); - const { container } = renderRoute('/@alice.test'); + const { container } = renderRoute('/alice.test'); expect( await screen.findByText( 'This person has chosen to show their lanyard only to people who are signed in.' @@ -197,7 +201,7 @@ describe('lanyard page states', () => { ).toBeInTheDocument(); expect( within(screen.getByRole('main')).getByRole('link', { name: 'Sign in' }) - ).toHaveAttribute('href', '/?returnTo=%2F%40alice.test'); + ).toHaveAttribute('href', '/?returnTo=%2Falice.test'); expect(screen.queryByText('Dr Ada Example')).not.toBeInTheDocument(); // The name must not leak through the tab title either. await waitFor(() => @@ -222,7 +226,7 @@ describe('lanyard page states', () => { }); serveViewerReads(); vi.mocked(useSession).mockReturnValue(signedInSession(BEA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect(await hero()).toBeInTheDocument(); }); @@ -232,7 +236,7 @@ describe('lanyard page states', () => { relationship: { blockedBy: `at://${ADA.did}/app.bsky.graph.block/3m` }, }); vi.mocked(useSession).mockReturnValue(signedInSession(BEA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect( await screen.findByText("This lanyard isn't available to you.") ).toBeInTheDocument(); @@ -241,7 +245,7 @@ describe('lanyard page states', () => { it('passes axe on a loaded lanyard', async () => { servePerson(); - const { container } = renderRoute('/@alice.test'); + const { container } = renderRoute('/alice.test'); await hero(); await expectNoAxeViolations(container); }); @@ -252,7 +256,7 @@ describe('shared context', () => { servePerson(); serveViewerReads(); vi.mocked(useSession).mockReturnValue(signedInSession(BEA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect( await screen.findByText('You follow each other') ).toBeInTheDocument(); @@ -265,7 +269,7 @@ describe('shared context', () => { it('is absent for signed-out visitors and on your own lanyard', async () => { servePerson(); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); await hero(); expect(screen.queryByText('You follow each other')).not.toBeInTheDocument(); }); @@ -274,7 +278,7 @@ describe('shared context', () => { describe('accent', () => { it('applies the owner’s accent while their lanyard is open', async () => { servePerson({ config: { accent: 'mint' } }); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); await hero(); await waitFor(() => expect(document.documentElement.dataset.accent).toBe('mint') @@ -285,7 +289,7 @@ describe('accent', () => { describe('lanyard blocks', () => { it('shows the default blocks: links and talks', async () => { servePerson(); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); const links = await screen.findByRole('region', { name: 'Links' }); expect( within(links).getByRole('link', { name: 'GitHub (opens in a new tab)' }) @@ -307,7 +311,7 @@ describe('lanyard blocks', () => { ], }, }); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); await screen.findByRole('region', { name: 'Skills' }); const headings = screen .getAllByRole('heading', { level: 2 }) @@ -331,7 +335,7 @@ describe('lanyard blocks', () => { ) ); vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); const talks = await screen.findByRole( 'region', { name: 'Talks' }, @@ -351,7 +355,7 @@ describe('lanyard blocks', () => { describe('visitor actions on the page', () => { it('offers signed-out visitors Bluesky, with sign-in and sign-up in the header', async () => { servePerson(); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect( await screen.findByRole('link', { name: 'Follow on Bluesky (opens in a new tab)', @@ -362,7 +366,7 @@ describe('visitor actions on the page', () => { within(header).getByRole('link', { name: 'Sign in' }) ).toHaveAttribute( 'href', - `/?returnTo=${encodeURIComponent('/@alice.test')}` + `/?returnTo=${encodeURIComponent('/alice.test')}` ); expect( within(header).getByRole('button', { name: 'Make your lanyard' }) @@ -395,13 +399,13 @@ describe('visitor actions on the page', () => { it('gives owners Edit lanyard and Show QR on the badge', async () => { servePerson(); vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); const badge = await screen.findByRole('region', { name: /Ada/ }); expect( within(badge).getByRole('link', { name: 'Edit lanyard' }) - ).toHaveAttribute('href', '/lanyard'); + ).toHaveAttribute('href', '/alice.test/edit'); expect( - within(badge).getByRole('button', { name: 'Show QR' }) + within(badge).getByRole('link', { name: 'Show QR' }) ).toBeInTheDocument(); }); }); @@ -436,7 +440,7 @@ describe('blocks between viewer and owner', () => { ) ); vi.mocked(useSession).mockReturnValue(signedInSession(BEA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); await new Promise((resolve) => setTimeout(resolve, 400)); expect(screen.queryByText('Dr Ada Example')).not.toBeInTheDocument(); release(); @@ -460,7 +464,7 @@ describe('owner settings that cannot be read', () => { ) ); vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); - renderRoute('/@alice.test'); + renderRoute('/alice.test'); expect( await screen.findByText( "Couldn't load your lanyard settings", diff --git a/web/src/app/share.test.tsx b/web/src/app/share.test.tsx new file mode 100644 index 0000000..c309bb1 --- /dev/null +++ b/web/src/app/share.test.tsx @@ -0,0 +1,113 @@ +import { screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { notify } from '@/components/Toaster/notify'; +import { useSession } from '@/features/auth/session/useSession'; +import { qrUrl } from '@/features/qr/qrUrl'; +import { captureError, track } from '@/lib/analytics/analytics'; +import { expectNoAxeViolations } from '@/test/axe'; +import { ADA } from '@/test/people'; +import { servePerson, signedInSession, signedOutSession } from '@/test/person'; +import { renderRoute, waitForRoute } from '@/test/router'; + +vi.mock('@/features/auth/session/useSession', () => ({ useSession: vi.fn() })); +vi.mock('@/lib/analytics/analytics', () => ({ + captureError: vi.fn(), + track: vi.fn(), +})); +vi.mock('@/components/Toaster/notify', () => ({ + notify: { success: vi.fn(), error: vi.fn(), info: vi.fn() }, +})); + +const SHARE = `/${ADA.handle}/share`; + +const release = vi.fn(async () => undefined); +const request = vi.fn(async () => ({ + release, + addEventListener: vi.fn(), + removeEventListener: vi.fn(), +})); + +beforeEach(() => { + vi.mocked(useSession).mockReturnValue(signedInSession(ADA.did)); + servePerson(); + Object.defineProperty(navigator, 'wakeLock', { + value: { request }, + configurable: true, + }); +}); + +afterEach(() => { + Reflect.deleteProperty(navigator, 'wakeLock'); +}); + +const badge = () => + screen.findByRole('dialog', { name: 'Dr Ada Example' }, { timeout: 5000 }); + +describe('//share (#150)', { timeout: 20_000 }, () => { + it('shows the QR badge full screen, with no app header', async () => { + renderRoute(SHARE); + expect(await badge()).toHaveTextContent('Scan to see my lanyard'); + expect( + screen.getByRole('img', { name: `QR code for ${qrUrl(ADA.did)}` }) + ).toBeInTheDocument(); + expect(screen.queryByRole('banner')).toBeNull(); + expect(track).toHaveBeenCalledWith('qr_opened'); + }); + + it('works for anyone’s lanyard, signed in or not', async () => { + vi.mocked(useSession).mockReturnValue(signedOutSession()); + renderRoute(SHARE); + expect(await badge()).toBeInTheDocument(); + }); + + it('copies the link, saying so when it can’t', async () => { + const user = userEvent.setup(); + const writeText = vi.spyOn(navigator.clipboard, 'writeText'); + renderRoute(SHARE); + await badge(); + await user.click(screen.getByRole('button', { name: 'Copy link' })); + expect(writeText).toHaveBeenCalledWith(qrUrl(ADA.did)); + await waitFor(() => + expect(notify.success).toHaveBeenCalledWith('Link copied') + ); + + writeText.mockRejectedValueOnce(new Error('Denied')); + await user.click(screen.getByRole('button', { name: 'Copy link' })); + await waitFor(() => + expect(notify.error).toHaveBeenCalledWith( + "Couldn't copy the link. Please try again." + ) + ); + expect(captureError).toHaveBeenCalledWith(new Error('Denied'), { + source: 'copy-link', + }); + }); + + it('goes back to where you were on Close', async () => { + const { router } = renderRoute([`/${ADA.handle}/edit`, SHARE]); + await badge(); + await userEvent.click(screen.getByRole('button', { name: 'Close' })); + await waitForRoute(router, `/${ADA.handle}/edit`, 5000); + }); + + it('goes to the lanyard on Escape when opened directly', async () => { + const { router } = renderRoute(SHARE); + await badge(); + await userEvent.keyboard('{Escape}'); + await waitForRoute(router, `/${ADA.handle}`, 5000); + }); + + it('keeps the screen awake while it’s open', async () => { + renderRoute(SHARE); + await badge(); + await waitFor(() => expect(request).toHaveBeenCalledWith('screen')); + }); + + it('has no axe violations', async () => { + const { baseElement } = renderRoute(SHARE); + await badge(); + await expectNoAxeViolations(baseElement as HTMLElement); + }); +}); diff --git a/web/src/app/staticData.ts b/web/src/app/staticData.ts index a67f8ce..ace4905 100644 --- a/web/src/app/staticData.ts +++ b/web/src/app/staticData.ts @@ -11,6 +11,11 @@ declare module '@tanstack/react-router' { * lanyard (lanyard pages, #129). */ signedOutActions?: boolean; + /** + * The route fills the screen itself (Show QR, #150): the root layout + * renders no header, no
and no navigation around it. + */ + fullScreen?: boolean; } } diff --git a/web/src/components/AccountLink/AccountLink.styles.ts b/web/src/components/AccountLink/AccountLink.styles.ts index 19464b5..1431433 100644 --- a/web/src/components/AccountLink/AccountLink.styles.ts +++ b/web/src/components/AccountLink/AccountLink.styles.ts @@ -1,4 +1,8 @@ export const link = 'inline-flex size-11 shrink-0 items-center justify-center rounded-full'; +/** With its label: a 3 px button-like target around avatar and word. */ +export const labelled = + 'inline-flex min-h-11 shrink-0 items-center gap-2 rounded-sm px-2 text-sm font-medium leading-5 text-ui-text hover:bg-ui-surface-muted'; + export const avatar = 'size-8 rounded-full bg-ui-surface-muted'; diff --git a/web/src/components/AccountLink/AccountLink.tsx b/web/src/components/AccountLink/AccountLink.tsx index a1f1556..7a663a0 100644 --- a/web/src/components/AccountLink/AccountLink.tsx +++ b/web/src/components/AccountLink/AccountLink.tsx @@ -9,7 +9,7 @@ import * as styles from './AccountLink.styles'; import type { AccountLinkProps } from './AccountLink.types'; /** The signed-in person's avatar, linking to their account settings. */ -export function AccountLink({ className }: AccountLinkProps) { +export function AccountLink({ labelled = false, className }: AccountLinkProps) { const { t } = useLingui(); const session = useSession(); const did = session.status === 'signedIn' ? session.session.did : undefined; @@ -19,13 +19,14 @@ export function AccountLink({ className }: AccountLinkProps) { {profile.data?.avatar ? ( ) : (