diff --git a/README.md b/README.md index 40843fd..df14aea 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ The two `AlbumDetailPage` rows are the chunk added by the album detail route (#1 The frontend supports searching by album title or artist, committed on Search or Enter rather than while typing, with loading and error feedback, and a no-results message. It currently shows the first 20 matches ordered by relevance. -**Planned:** Frontend filters, sorting, pagination controls and album details; reviews, ratings and wantlist functionality; accessibility and sustainability work (M3); and Playwright end-to-end tests (M4). The database tables for reviews and crate items exist, but their GraphQL functionality is still planned. `X-User-Id` is also defined but not yet handled at runtime. +**Planned:** Frontend filters, sorting and pagination controls; album details; reviews, ratings and wantlist functionality; and Playwright end-to-end tests (M4). The database tables for reviews and crate items exist, but their GraphQL functionality is still planned. `X-User-Id` is also defined but not yet handled at runtime. Accessibility and sustainability are M3 work and are covered under [Sustainability measures](#sustainability-measures) and the accessibility pass in the issue tracker. ### Backend search behavior diff --git a/client/package.json b/client/package.json index 11820db..dbdb1a7 100644 --- a/client/package.json +++ b/client/package.json @@ -22,9 +22,11 @@ "@testing-library/dom": "^10.4.1", "@testing-library/jest-dom": "^7.0.1", "@testing-library/react": "^16.3.3", + "@testing-library/user-event": "^14.6.8", "@types/react": "^19.2.18", "@types/react-dom": "^19.2.4", "@vitejs/plugin-react": "^6.1.0", + "axe-core": "^4.14.0", "jsdom": "^29.1.1", "typescript": "~5.9.3", "vite": "^8.2.2", diff --git a/client/src/a11y.test.tsx b/client/src/a11y.test.tsx new file mode 100644 index 0000000..ff1211f --- /dev/null +++ b/client/src/a11y.test.tsx @@ -0,0 +1,165 @@ +import { describe, it, expect } from 'vitest'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { MockedProvider } from '@apollo/client/testing/react'; +import type { MockedResponse } from '@apollo/client/testing'; +import { GraphQLError } from 'graphql'; +import axe from 'axe-core'; + +import App from './App.js'; +import { SEARCH_ALBUMS } from './api/searchAlbums.js'; +import type { + AlbumSummary, + SearchAlbumsData, + SearchAlbumsVariables, +} from './api/searchAlbums.js'; + +const album: AlbumSummary = { + id: '1', + title: 'Abbey Road', + artists: [{ id: '1', name: 'The Beatles' }], + releaseYear: 1969, + genres: [{ id: '1', name: 'Rock' }], + averageRating: 4.8, + reviewCount: 1240, +}; + +function mockSearch( + query: string, + albums: AlbumSummary[] = [album], +): MockedResponse { + return { + request: { + query: SEARCH_ALBUMS, + variables: { input: { query, sort: 'RELEVANCE' }, first: 20, after: null }, + }, + result: { + data: { + searchAlbums: { + edges: albums.map((node) => ({ cursor: node.id, node })), + totalCount: albums.length, + pageInfo: { hasNextPage: false, endCursor: null }, + }, + }, + }, + delay: 10, + }; +} + +function renderApp( + mocks: MockedResponse[] = [], +) { + return render( + + + , + ); +} + +function submitSearch(query: string) { + fireEvent.change(screen.getByRole('searchbox'), { target: { value: query } }); + fireEvent.submit(screen.getByRole('search')); +} + +/** + * Runs axe over the rendered tree and fails with a readable list if anything + * is wrong. + * + * `color-contrast` is off because jsdom does no layout or painting, so axe + * has nothing to measure a rendered contrast ratio against and would report + * every pair as incomplete rather than passing. Contrast is verified by hand + * in a browser, which is where the numbers in the README come from. Every + * other rule axe can evaluate runs here. + */ +async function expectNoViolations(container: HTMLElement) { + const results = await axe.run(container, { + rules: { 'color-contrast': { enabled: false } }, + }); + + // Guard against a false pass: if axe examined nothing it would report a + // clean tree whatever the markup looks like. + expect(results.passes.length).toBeGreaterThan(0); + + const summary = results.violations.map( + (violation) => + `${violation.id}: ${violation.help} (${violation.nodes.length} node(s)) -> ${violation.nodes[0]?.html}`, + ); + + expect(summary).toEqual([]); +} + +describe('axe-core on the search page', () => { + it('is clean before anything is searched', async () => { + const { container } = renderApp(); + await expectNoViolations(container); + }); + + it('is clean with results rendered', async () => { + const { container } = renderApp([mockSearch('Abbey')]); + submitSearch('Abbey'); + + await screen.findByRole('region', { name: 'Search results' }); + await waitFor(() => + expect(screen.getByText(/Showing 1 of 1 albums/)).toBeInTheDocument(), + ); + + await expectNoViolations(container); + }); + + it('is clean with no results', async () => { + const { container } = renderApp([mockSearch('zzzz', [])]); + submitSearch('zzzz'); + + await screen.findByRole('heading', { name: 'No albums found' }); + await expectNoViolations(container); + }); + + it('is clean when the search fails', async () => { + const failing: MockedResponse = { + request: { + query: SEARCH_ALBUMS, + variables: { + input: { query: 'boom', sort: 'RELEVANCE' }, + first: 20, + after: null, + }, + }, + result: { errors: [new GraphQLError('Something went wrong')] }, + }; + const { container } = renderApp([failing]); + submitSearch('boom'); + + await screen.findByText('Could not load albums. Please try again.'); + await expectNoViolations(container); + }); + + it('is the first thing Tab reaches on the search page', async () => { + renderApp(); + const user = userEvent.setup(); + + // One Tab from a fresh page has to land on the skip link. Checking that it + // is first in the DOM is not the same thing: a link can be first in the + // markup and still unreachable if something ahead of it is not tabbable. + await user.tab(); + expect(screen.getByRole('link', { name: 'Skip to content' })).toHaveFocus(); + }); + + it('points the skip link at a target that exists and can take focus', async () => { + renderApp([mockSearch('Abbey')]); + submitSearch('Abbey'); + await screen.findByRole('region', { name: 'Search results' }); + + const skipLink = screen.getByRole('link', { name: 'Skip to content' }); + + // An href pointing at nothing skips nothing. + const href = skipLink.getAttribute('href'); + expect(href).toMatch(/^#/); + const target = document.querySelector(href!); + expect(target).not.toBeNull(); + + // The target has to be programmatically focusable too, or the browser + // scrolls to it and leaves focus stranded back in the navigation. + target!.focus(); + expect(target).toHaveFocus(); + }); +}); diff --git a/docs/AI-USAGE.md b/docs/AI-USAGE.md index d5cbdd8..b0b3350 100644 --- a/docs/AI-USAGE.md +++ b/docs/AI-USAGE.md @@ -59,6 +59,11 @@ the part that matters most. | 2026-10-19 | erikhfj | opencode | Reviewed #53 against the code rather than as prose. Found the README contradicting its own Implemented section and a smoke test that dropped an expectation while keeping the field in the query. Verified Rachel's other claims, including that `AVERAGE_RATING_DESC` throws, which corrected my own "four sort modes" in the README. | | 2026-10-19 | erikhfj | opencode | Search state in the URL for #19, with `useSearchState` owning `q`, `sort`, `genres` and `decades`. My first version committed on a 300 ms debounce, which rewrote the URL mid-word and reset the input back through the `query !== previousQuery` branch; it read as the page reloading on every keystroke. Changed so the query reaches the URL only on Search. | +| 2026-10-20 | erikhfj | opencode | Processed two co-students' reviews of the project. Both found the same real bug: search matches whole words, so `radio` finds nothing for Radiohead. Their diagnosis was right, but the fix proposed, `to_tsquery` with `:*`, throws on quotes and dashes, which is the whole reason the code uses `websearch_to_tsquery`. Filed #55 to build the prefix query in TypeScript instead. Their rating suggestion exposed something worse: #12 asked for `AVG(review.rating)` as the keyset sort key, an aggregate that moves whenever anyone writes a review. Revised #12 to a trigger-maintained `album.average_rating`, warned #15 not to write the same column, and corrected #18 where a bullet still described a debounce #19 had already removed. | +| 2026-10-22 | erikhfj | opencode | Reviewed #57 and #58 by running the full gate suite on the branches rather than reading them as prose. Six findings on each. One of mine was wrong: I wrote that `App.tsx` was the only file in the repo carrying a UTF-8 BOM. It is one of four, and three predate that PR entirely. Posted a correction rather than leaving the error standing. Recorded here because a wrong review is as much a failure of control as a wrong patch. | +| 2026-10-22 | erikhfj | opencode | #18, dark theme under `prefers-color-scheme`. Lifted the eight hard-coded colours into custom properties first so the light palette stays byte-identical. Did not take the generated palette as-is: `--border-strong` at `#556055` measured 2.5:1 against the surface, under the 3:1 a form-control border needs, so it became `#6b766b` at 3.4:1. Computed every ratio by hand rather than trusting the values that came back. | +| 2026-10-22 | erikhfj | opencode | #17, axe-core over all four states of the search page. A green suite proves nothing on its own, so the harness asserts axe found passing rules before asserting it found no violations, and I checked it against a deliberately broken tree first to confirm it reports failures. `color-contrast` is disabled because jsdom paints nothing; that limitation is written into the PR rather than quietly omitted. | + ## Notes - The prototype was never merged. `archive/scaffold-prototype` is a reference to diff --git a/docs/architecture.md b/docs/architecture.md index ffe9aca..91b744d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -5,7 +5,7 @@ in M2 and M3. The API contract is `schema.graphql`, the data model is in `docs/data-model.md`, and the planned interface is described in `docs/ui-specification.md`. -At M2, search, filtering, sorting, pagination and DataLoaders are implemented in the backend. User identity, reviews, crate functionality and URL-based search state are planned for later milestones. +At M2, search, filtering, sorting, pagination and DataLoaders are implemented in the backend. URL-based search state landed in M3. User identity, reviews and crate functionality are planned for later milestones. ## 1. PostgreSQL + GraphQL Yoga diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c3001c2..6be07d1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -69,6 +69,9 @@ importers: '@testing-library/react': specifier: ^16.3.3 version: 16.3.3(@testing-library/dom@10.4.2)(@types/react-dom@19.3.0(@types/react@19.3.0))(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + '@testing-library/user-event': + specifier: ^14.6.8 + version: 14.6.8(@testing-library/dom@10.4.2) '@types/react': specifier: ^19.2.18 version: 19.3.0 @@ -78,6 +81,9 @@ importers: '@vitejs/plugin-react': specifier: ^6.1.0 version: 6.1.1(vite@8.3.1(@types/node@24.19.2)(esbuild@0.28.2)(tsx@4.23.15)) + axe-core: + specifier: ^4.14.0 + version: 4.14.0 jsdom: specifier: ^29.1.1 version: 29.1.1 @@ -1009,6 +1015,12 @@ packages: '@types/react-dom': optional: true + '@testing-library/user-event@14.6.8': + resolution: {integrity: sha512-vgSSqnANMnkcYR/vUNDbwS7aM3OErRLNUG8vCVVlj4oc6cKffUrccX7UAS5AmJQQNZTSibi9z5GvvRZ0aYgUCQ==} + engines: {node: '>=12', npm: '>=6'} + peerDependencies: + '@testing-library/dom': '>=7.21.4' + '@types/aria-query@5.0.4': resolution: {integrity: sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw==} @@ -1264,6 +1276,10 @@ packages: resolution: {integrity: sha512-UzGt8zg7Ny8djbYMhxl2zuEevVa7r2gJjYY5Lwr1xM7+XU2nd6CkIWFTVcCIbAP63vSz71NaVyyuSk9lHKcy0A==} engines: {node: '>=4'} + axe-core@4.14.0: + resolution: {integrity: sha512-9WTZxEjsZ7b13TH8JPmbV2z8CHbl80/2hm3XPEG4JgNdQLK81IBRXmSxHfMAOkSqQeRxT/0dwNDz2GOm3zzpcQ==} + engines: {node: '>=4'} + axobject-query@4.1.0: resolution: {integrity: sha512-qIj0G9wZbMGNLjLmg1PT6v2mE9AH2zlnADJD/2tC6E00hgmhUOfEB6greHPAfLRSufHqROIUTkw6E+M3lH0PTQ==} engines: {node: '>= 0.4'} @@ -3331,6 +3347,10 @@ snapshots: '@types/react': 19.3.0 '@types/react-dom': 19.3.0(@types/react@19.3.0) + '@testing-library/user-event@14.6.8(@testing-library/dom@10.4.2)': + dependencies: + '@testing-library/dom': 10.4.2 + '@types/aria-query@5.0.4': {} '@types/chai@5.2.3': @@ -3638,6 +3658,8 @@ snapshots: axe-core@4.13.0: {} + axe-core@4.14.0: {} + axobject-query@4.1.0: {} balanced-match@1.0.2: {}