Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- **Semantic HTML.** The page language is set (`<html lang="en">`), the main navigation is a `<nav>` with a label, and related form controls are grouped. For example, the page count filter uses a `<fieldset>` with a `<legend>`. This gives screen readers a structure to navigate by.

- **`<div>` and `<span>` only where no semantic element fits.** When an element describes the content, we use it: every page has a `<main>` and its own headings, lists of books and reviews are `<ul>` lists, each review is an `<article>` with a `<time>` for its date, and actions are buttons or links. A `<div>` or `<span>` tells a screen reader nothing about what it contains, so we only use them where that is fine. A `<div>` groups elements for layout, and a `<span>` gives a part of a text its own styling, like the colored words in the heading on the home page, or holds text that is only meant for screen readers, like "Rated" and "out of 5" next to the stars.

- **Accessible components.** Our UI components are built on Base UI (through shadcn/ui), which handles keyboard navigation, focus and ARIA attributes for components like dialogs, comboboxes, selects and sliders. Buttons and inputs show a visible focus ring when they are reached with the keyboard.

- **Forms.** Every field has a label connected to its input, invalid fields are marked with `aria-invalid`, and help text (such as the minimum password length) is connected with `aria-describedby`. Error messages are shown with `role="alert"`, so screen readers read them out as they appear. We also set `autoComplete` on the fields, so browsers and password managers can fill them in, and the error messages tell the user what went wrong, for example that the email is already in use.
Expand Down
28 changes: 25 additions & 3 deletions docs/choices-and-reasoning.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,30 @@ Search results are fetched 12 at a time with `limit` and `offset`, together with

We chose offset pagination based on how we expect the app to be used. Most users look at the first few pages of results, and change their search or filters if they don't find what they are looking for. The cost of `offset` only grows with how far out you go: to show page 600, the database has to step past all the rows on the pages before it. Since browsing that deep is very rare, we think page numbers are worth more to our users than what we would gain from keyset (cursor) pagination, which keeps every page equally fast but only lets you move to the next or previous page. The pagination in the interface shows the first and last page and the pages around the current one, so an arbitrary page can only be opened from the URL. With page numbers, the URL stays short and readable, and we can show which page you are on out of how many.

As the dataset grows, keeping the first pages fast depends on database indexes on the columns we sort and filter by (planned in #77), and the total count could be capped or estimated instead of counting every match.
Keeping the first pages fast depends on database indexes. We have added indexes on the columns we sort by (number of ratings, rating, title, pages and publish year), on the language and book format filters, and trigram indexes for the search on title and author (see `backend/src/db/schema/books.ts`). If the dataset grows much larger, the total count could be capped or estimated instead of counting every match.

## Books without ratings are stored as NULL

The dataset uses a rating of 0.0 for books nobody has rated. We store these as `NULL` instead, so they aren't treated as the worst-rated books, and they are placed last when sorting by rating.

## Sorting

The search results can be sorted by relevance (the default), rating, title, publish date and number of pages.

Relevance shows the books with the most ratings first, so well-known books come before books that few people have rated. When a search has no exact matches and we show books with similar titles or authors instead, the closest match comes first.

### Sorting by highest rating adds a minimum number of ratings

When the user chooses "Rating: high to low", we also add the filter "10,000+ ratings", unless the user has already chosen a minimum number of ratings. A toast tells the user that the filter was added, and that it can be changed in the filters.

Without a minimum, this sort order is not very useful. More than 800 books in our dataset have a rating of 5.0, and the one with the most ratings has only 36, so about the first 70 pages would be books that almost nobody has rated. A single 5-star review from one of our users on a book without ratings would also place it among them. With the minimum, about 12,600 of our roughly 52,000 books are left, and it is the same limit as we use for the top-rated books on the home page.

We discussed whether a filter that is chosen for the user is more annoying than useful (#117). We kept it because it is easy to notice and easy to remove: the toast explains it, the filter button shows how many filters are active, and the minimum can be set back to "Any". We also never replace a minimum that the user has chosen. The filter stays when the user switches to another sort order, since filters and sorting are independent everywhere else.

### No sorting by author

We do not offer sorting by author (#100). The author field in the dataset is free text and can hold several names, like "Author One, Author Two". We have no guarantee that the authors of a book are always listed in the same order, or whether a name is written as "First Last" or "Last, First", so sorting on this field would often give an order that looks random. Authors can be found with the search field instead, which searches both titles and authors.

## Selecting several genres shows books in any of them

When several genres are selected, we show books that belong to at least one of them (OR), not all of them (AND). This is because it widens the search instead of quickly ending up with no results.
Expand All @@ -84,11 +102,15 @@ These cover almost all of the browser engines that are in use, i.e. Chromium, Ge
We have skipped Microsoft Edge and (branded) Google Chrome because we already test Chromium, which is the browser engine these use.
Mobile Chrome and Mobile Safari are used to test the app on mobile screens.

We run the tests with one worker, so only one test runs at a time (`workers: 1` in `frontend/playwright.config.ts`).
With several workers, the tests run in several browsers at the same time against the same local backend, and we got timeouts and other failures that were not caused by bugs in the app.
How many workers a machine can handle differs, so one worker gives the same results for everyone on the team. The price is that a full run takes longer.

We do not use CI for E2E tests. Our VM only has 1 CPU core available and the tests take a very long time. They also slow down our app, which is running on the same server.
Instead, we use CI for other tasks, such as linting and formatting.

## Changes/not implemented yet from our specification

- **Filters:** We filter on genres, language, book format and number of pages. The specification also mentioned author, publisher, place and person, which we have not implemented yet.
- **Filters:** We filter on genres, language, book format, number of pages and a minimum number of ratings. The specification also mentioned author, publisher, place and person. Authors are covered by the search field, which searches both titles and authors, and we have not implemented filters for publisher, place and person.
- **Login:** Instead of Google login, we support email/password and GitHub and Discord login. Google was rejected since it required https connection, which we do not provide.
- **Reviews:** Reviews with a star rating are planned for the next delivery.
- **Reviews:** Implemented in delivery 4 as specified, with a star rating from 1 to 5 and an optional text. Reviews are written and shown on the book's page, and the user's own reviews are also listed on their profile. A book's rating combines the rating from the dataset with the reviews from our users.
6 changes: 3 additions & 3 deletions docs/sustainability.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## What we do well

- **Only fetch what is needed.** Search only returns 12 books for each page (`SEARCH_LIMIT`), and filtering, sorting and counting happen in the database, not in the client. This is because we only want to fetch what is necessary, avoid having the client fetch the whole dataset, and keep the heavy operations out of the client as well.
- **Only fetch what is needed.** Search only returns 12 books for each page (`BOOK_PAGE_LIMIT`), and filtering, sorting and counting happen in the database, not in the client. This is because we only want to fetch what is necessary, avoid having the client fetch the whole dataset, and keep the heavy operations out of the client as well.
- **Fewer requests when searching.** When searching, we have a 500 ms debounce (`SEARCH_DEBOUNCE_MS`), which waits until the user has stopped typing for 500 ms before actually sending the request.
- **Small responses from GraphQL.** The book cards only fetch what is actually being displayed: `id`, `title`, `author`, `publishDate`, `coverImgUrl` and `rating`, and not the description and other heavy fields.
- **No unnecessary queries.** Queries are skipped when their result is not needed (with Apollo's `skip` option and `skipToken`), for example the favorites of a user who is not logged in.
Expand All @@ -16,9 +16,9 @@ This section is meant as an overview of potential improvements with regard to su
| Limitation | Possible improvement | Delivery 3 | Delivery 4 |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Cover images are loaded when the page renders, also those outside the screen, and have no fixed width or height. | Add `loading="lazy"` and set `width`/`height` on the `<img>` in `BookCover.tsx`, so off-screen images load only when needed and the layout doesn't shift. | Not done | Done. Covers use `loading="lazy"`, except on the book page where the cover is visible right away. The cover box already has a fixed aspect ratio, so no `width`/`height` is needed to avoid layout shift. |
| We use the cover URLs from the dataset as they are, which may be larger images than the size we display them at. | Measure the image sizes, and use a smaller image variant if the image host offers one. | Not done | |
| We use the cover URLs from the dataset as they are, which may be larger images than the size we display them at. | Measure the image sizes, and use a smaller image variant if the image host offers one. | Not done | Not done. We still use the cover URLs from the dataset as they are. |
| The app loads two font families: Inter (self-hosted with `@fontsource`) and Open Sans (from Google Fonts). | Use one font family and host it ourselves, which removes an external request and one set of font files. | Not done | Done. Open Sans was not used anywhere, so we removed it. |
| Apollo caches the lists of genres, languages and formats in each browser, but the backend computes them again with a `GROUP BY` over the whole `books` table every time a browser without them asks. | Cache the lists in the backend, so they are computed once and shared between all users. They only change when the dataset changes. | Not done | |
| Apollo caches the lists of genres, languages and formats in each browser, but the backend computes them again with a `GROUP BY` over the whole `books` table every time a browser without them asks. | Cache the lists in the backend, so they are computed once and shared between all users. They only change when the dataset changes. | Not done | Not done. The backend still computes the lists on every request. |
| Added after delivery 3: Apache sent our own JS, CSS and font files without cache headers, so browsers asked for them again on every visit. | Let browsers keep the files in `assets/` for a year, since their names change whenever their content changes. See the [deployment documentation](/docs/deployment.md#apache-config). | - | Done |

## Lighthouse
Expand Down
49 changes: 35 additions & 14 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ Test files live next to the code they test (`*.test.ts` / `*.test.tsx`).
Shared helpers and fixtures live in `app/test/`:

- `setup.ts` adds the [jest-dom](https://github.com/testing-library/jest-dom)
matchers (e.g. `toBeInTheDocument`) and clears the DOM and `sessionStorage`
after every test.
- `fixtures/` holds a sample book.
matchers (e.g. `toBeInTheDocument`), replaces browser APIs that jsdom does
not have (`IntersectionObserver` and `matchMedia`), and clears the DOM and
`sessionStorage` after every test.
- `renderRoutes.tsx` renders routes inside an in-memory router and a mocked
Apollo Client.
- `mocks.ts` builds the mocked GraphQL responses that several tests share.
- `session.ts` holds what `authClient.useSession()` returns for a logged-in
and a logged-out user.
- `fixtures/` holds sample books.

### What is tested

Expand All @@ -89,16 +95,24 @@ markup (like `Header` and `Footer`) is not tested: such tests break on every
intended change without catching real bugs.

- **Component tests** for `BookCard`, `BookPagination`, `FavoriteButton`,
`FilterBooksSection` and `SearchBar`: rendering based on props, user
interaction (clicks, typing), keeping search and filters in sync with the
URL, and paging.
`BookFilters`, `SearchBar`, `SortSelect` and `BookReviews`: rendering based
on props, user interaction (clicks, typing), keeping search, filters and
sorting in sync with the URL, paging, and saving favorites and reviews.
- **Page tests** for the home page, the books page and the book page
(`app/routes/`): the whole page is rendered with mocked API responses, for
example searching, the suggestions when a search has no results, and
browsing from one book to the next.
- **Hook tests** for `useBookSearchParams` and `useCountUp`.
- **Snapshot test** for `BookCard`. The rendered HTML is stored in
`__snapshots__/` next to the test and compared on every run. If a markup
change is intended, update the snapshot with `pnpm test -u`.

### Mocking

The page tests (home and selectedBook) were removed when the frontend moved from Open Library to our GraphQL API. Several component tests have not been updated after recent changes and currently fail, for example `FavoriteButton`, which now uses Apollo instead of `localStorage`. We will fix them and rewrite the page tests with Apollo's MockedProvider in #33.
The tests never send real requests. We mock two things:

- **The GraphQL API.** Components that use the API are rendered inside Apollo's `MockedProvider` (through `renderRoutes`), with a list of mocked responses. A request only gets a response if both its query and its variables match a mock, so the tests also check that the components ask the API for the right data.
- **The session.** The tests mock `~/api/auth` with `vi.mock`, so `authClient.useSession()` returns the logged-in or logged-out user from `app/test/session.ts`, and no requests are sent to the auth server.

### Playwright (end-to-end tests)

Expand All @@ -117,18 +131,25 @@ cd frontend
pnpm e2e
```

The tests will take a while to complete, because we test against multiple browsers.
The tests will take a while to complete, because we test against multiple browsers and run one test at a time (see [project choices and reasoning](../docs/choices-and-reasoning.md#playwright-e2e-testing)).

The E2E tests cover signing up, logging in and out, searching, filtering, sorting and paging, favorites, profile settings, the home page, dark mode and using the app with only the keyboard. They also run automatic accessibility checks with [axe](https://github.com/dequelabs/axe-core) on every page, in both light and dark mode.

### Browsers and devices

The application is checked manually in the browsers and screen sizes below.
The grid should show 4/3/2/1 columns as the viewport gets narrower, and the
details view should stack vertically below 900px.
The current version is checked in Chromium, Firefox and WebKit, and on the
screen sizes of two phones (Pixel 5 and iPhone 12), by the Playwright tests
above.

Before we had these tests, the application was checked manually in the
browsers and screen sizes below: the grid should show 4/3/2/1 columns as the
viewport gets narrower, and the details view should stack vertically on
narrow screens.

> [!NOTE]
> These checks were done in September, before the move to our GraphQL API and
> before login, the profile page and the filters were added. They will be
> repeated for the current version.
> These manual checks were done in September, before the move to our GraphQL
> API and before login, the profile page and the filters were added. The table
> has not been updated since.
| Browser / device | Viewport | Result | Date |
| --------------------------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------- | ---------- |
Expand Down