Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
480 changes: 385 additions & 95 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

192 changes: 191 additions & 1 deletion client/src/a11y.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi } from 'vitest';
import { act, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MockedProvider } from '@apollo/client/testing/react';
Expand All @@ -7,6 +7,13 @@ import { GraphQLError } from 'graphql';
import axe from 'axe-core';

import App from './App.js';
import AlbumDetailPage from './pages/AlbumDetailPage.js';
import CratePage from './pages/CratePage.js';
import ReviewForm from './components/ReviewForm.js';
import { ALBUM_DETAIL } from './api/albumDetail.js';
import { MY_CRATE } from './api/crate.js';
import { UPSERT_REVIEW } from './api/reviews.js';
import { MY_SEARCH_HISTORY } from './api/searchHistory.js';
import { searchHistoryMocks } from './test/searchHistoryMocks.js';
import { SEARCH_ALBUMS } from './api/searchAlbums.js';
import type {
Expand Down Expand Up @@ -193,4 +200,187 @@ describe('axe-core on the search page', () => {
act(() => target!.focus());
expect(target).toHaveFocus();
});

it('is clean with the recent-searches combobox open and an option active', async () => {
const historyMock: MockLink.MockedResponse = {
request: { query: MY_SEARCH_HISTORY },
result: {
data: {
mySearchHistory: [
{ query: 'Blue', lastSearchedAt: '2026-11-10T12:00:00Z' },
{ query: 'Abbey Road', lastSearchedAt: '2026-11-09T12:00:00Z' },
],
},
},
maxUsageCount: Number.POSITIVE_INFINITY,
};

const { container } = render(
<MockedProvider mocks={[historyMock, mockSearch('', [album])]}>
<App />
</MockedProvider>,
);
await waitForQueries();

const user = userEvent.setup();
const combobox = screen.getByRole('combobox', { name: 'Search by album or artist' });
await user.click(combobox);
await user.keyboard('{ArrowDown}');

expect(screen.getByRole('listbox', { name: 'Recent searches' })).toBeInTheDocument();
expect(screen.getByRole('option', { name: 'Blue' })).toHaveAttribute(
'aria-selected',
'true',
);
await expectNoViolations(container);
});
});

describe('axe-core across detail, crate, and review form error states', () => {
const albumDetailMock: MockLink.MockedResponse = {
request: {
query: ALBUM_DETAIL,
variables: { id: '1', reviewFirst: 10, reviewAfter: null },
},
result: {
data: {
album: {
id: '1',
title: 'Abbey Road',
releaseYear: 1969,
musicBrainzReleaseGroupId: null,
artists: [{ id: '1', name: 'The Beatles' }],
genres: [{ id: '1', name: 'Rock' }],
tracks: [
{ id: '1', title: 'Come Together', trackNumber: 1, durationSeconds: 259 },
{ id: '2', title: 'Something', trackNumber: 2, durationSeconds: null },
],
averageRating: 4.8,
reviewCount: 1,
isInCrate: false,
myReview: null,
reviews: {
edges: [
{
cursor: 'cursor-1',
node: {
id: '1',
rating: 5,
comment: 'Timeless record.',
createdAt: '2026-10-20T12:00:00.000Z',
user: { displayName: 'Velvet Groove' },
},
},
],
totalCount: 1,
pageInfo: { hasNextPage: false, endCursor: 'cursor-1' },
},
},
},
},
};

it('is clean on AlbumDetailPage with tracklist, reviews, and review form', async () => {
const { container } = render(
<MockedProvider mocks={[albumDetailMock]}>
<AlbumDetailPage id="1" />
</MockedProvider>,
);

await screen.findByRole('heading', { level: 1, name: 'Abbey Road' });
await expectNoViolations(container);
});

it('is clean on CratePage when populated and when empty', async () => {
const populatedMock: MockLink.MockedResponse = {
request: { query: MY_CRATE, variables: { first: 20, after: null } },
result: {
data: {
myCrate: {
edges: [
{
cursor: 'cursor-0',
node: {
__typename: 'CrateItem',
id: '10',
addedAt: '2026-11-01T12:00:00.000Z',
album: { ...album, __typename: 'Album', isInCrate: true },
},
},
],
pageInfo: { hasNextPage: false, endCursor: 'cursor-0' },
totalCount: 1,
},
},
},
};
const emptyMock: MockLink.MockedResponse = {
request: { query: MY_CRATE, variables: { first: 20, after: null } },
result: {
data: {
myCrate: {
edges: [],
pageInfo: { hasNextPage: false, endCursor: null },
totalCount: 0,
},
},
},
};

const populated = render(
<MockedProvider mocks={[populatedMock]}>
<CratePage />
</MockedProvider>,
);
await screen.findByRole('heading', { level: 3, name: 'Abbey Road' });
await expectNoViolations(populated.container);
populated.unmount();

const empty = render(
<MockedProvider mocks={[emptyMock]}>
<CratePage />
</MockedProvider>,
);
await screen.findByRole('heading', { level: 2, name: 'Nothing saved yet' });
await expectNoViolations(empty.container);
});

it('is clean when ReviewForm shows validation and mutation error states', async () => {
const user = userEvent.setup();
const { container } = render(
<MockedProvider
mocks={[
{
request: {
query: UPSERT_REVIEW,
variables: { input: { albumId: '1', rating: 4, comment: 'Nice' } },
},
error: new Error('Network unavailable'),
},
]}
>
<main>
<h1>Abbey Road</h1>
<h2>Ratings &amp; reviews</h2>
<ReviewForm albumId="1" review={null} onChanged={vi.fn(async () => {})} />
</main>
</MockedProvider>,
);

// Client-side validation error (no rating selected).
await user.click(screen.getByRole('button', { name: 'Publish review' }));
expect(screen.getByRole('alert')).toHaveTextContent(
'Choose a rating from 1 to 5 stars.',
);
await expectNoViolations(container);

// Server/network error state.
await user.click(screen.getByRole('radio', { name: '4 stars' }));
await user.type(screen.getByRole('textbox', { name: /Comment/ }), 'Nice');
await user.click(screen.getByRole('button', { name: 'Publish review' }));
expect(await screen.findByRole('alert')).toHaveTextContent(
'Your review could not be saved. Please try again.',
);
await expectNoViolations(container);
});
});
10 changes: 7 additions & 3 deletions client/src/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import '@testing-library/jest-dom/vitest';
import { cleanup } from '@testing-library/react';
import { afterEach, beforeEach } from 'vitest';

// The URL is application state, not a browser detail: useSearchState reads it
// and writes it. Tests render a mounted app, so each one has to start from a
// known query string or the previous test's search leaks into the next.
// The URL and Web Storage are application state, not browser details:
// useSearchState, userIdentity and searchReturnState read and write them.
// Reset all three around each test so state cannot leak across tests.
beforeEach(() => {
window.localStorage.clear();
window.sessionStorage.clear();
window.history.replaceState({}, '', '/');
});

afterEach(() => {
cleanup();
window.localStorage.clear();
window.sessionStorage.clear();
window.history.replaceState({}, '', '/');
});

Expand Down
4 changes: 3 additions & 1 deletion client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

const apiProxyTarget = process.env.VITE_API_PROXY_TARGET || 'http://127.0.0.1:3001';

export default defineConfig({
plugins: [react()],
// The course VM serves the built app from /project2.
Expand All @@ -9,7 +11,7 @@ export default defineConfig({
port: 5173,
proxy: {
// Matches Apache's production /graphql route; the local API uses port 3001.
'/graphql': 'http://127.0.0.1:3001',
'/graphql': apiProxyTarget,
},
},
});
42 changes: 38 additions & 4 deletions e2e/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import os from 'node:os';
import { defineConfig, devices } from '@playwright/test';

/**
* The tests run against a stack that is already up: client, API and a database
* loaded with `pnpm db:seed`. Nothing here starts servers, so the same tests can
* point at a local dev stack or any other deployment through E2E_BASE_URL.
* The tests run against a stack seeded with `pnpm db:seed`.
*
* When `E2E_BASE_URL` is set (for example in CI or against a staging server),
* Playwright connects directly to that URL without starting servers. When
* `E2E_BASE_URL` is unset, `webServer` reuses an already-running local dev
* stack or starts the API and Vite dev server automatically.
*
* The base URL must end with a slash so that `page.goto('./')` and relative
* paths resolve under /project2/.
*/
const baseURL = process.env.E2E_BASE_URL ?? 'http://localhost:5173/project2/';
const customBaseURL = process.env.E2E_BASE_URL;
const hostname = os.hostname();

// Guard against accidentally running local `webServer` (which reuses port 3001)
// on the production VM (`it2810-19`), where live `crate-api` listens on 3001
// backed by the production database.
if (!customBaseURL && (hostname === 'it2810-19' || hostname.startsWith('it2810-19.'))) {
throw new Error(
'Refusing to start or reuse local webServer on it2810-19 without E2E_BASE_URL: ' +
'port 3001 is used by live crate-api against the production database. ' +
'Set E2E_BASE_URL to an isolated test stack instead.',
);
}

const baseURL = customBaseURL ?? 'http://localhost:5173/project2/';

export default defineConfig({
testDir: './tests',
Expand All @@ -22,5 +40,21 @@ export default defineConfig({
baseURL: baseURL.endsWith('/') ? baseURL : `${baseURL}/`,
trace: 'retain-on-failure',
},
webServer: customBaseURL
? undefined
: [
{
command: 'pnpm --filter @t19/server dev',
port: 3001,
cwd: '..',
reuseExistingServer: !process.env.CI,
},
{
command: 'pnpm --filter @t19/client dev',
url: 'http://localhost:5173/project2/',
cwd: '..',
reuseExistingServer: !process.env.CI,
},
],
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});
Loading