Files
redefined-designs/frontend/tests/e2e/admin-reserved-items.spec.ts
T
bermudalambandClaude Opus 5 b287c07747
Tests / lint (pull_request) Successful in 1m38s
Tests / backend-unit (pull_request) Successful in 1m44s
Tests / frontend-e2e (pull_request) Failing after 9m50s
SonarQube Analysis / sonarqube (pull_request) Failing after 11m46s
feat: capture first and last name so emails can greet informally (#106)
Registration collected one optional Name, so every greeting had only a whole name to use: "Hi Thom Lamb," rather than "Hi Thom,". Both parts are now captured, and the cart reminder greets by first name.

Both are required of anyone new, refused individually rather than as a single "name is required" so a form that filled one and not the other is told which.

The columns are nullable even so, and that is deliberate. Marking them NOT NULL would mean backfilling legacy rows with empty strings, which asserts that every customer has a name — untrue of anyone who registered while the field was optional. The table records what is actually the case; the rule that new registrations must supply both lives in the route, where a missing field can produce a message naming it.

The backfill splits on the first space, and it is lossy in a way no version of this avoids. "Thom Lamb" becomes Thom and Lamb; "Mary Jane Smith" gets a last name of "Jane Smith"; names that are not two parts fare worse. It was chosen over leaving the columns empty because nothing currently lets a customer correct their own name — PUT /api/customers/me exists but no frontend calls it — so empty would have meant permanently unpersonalised for every existing customer. The migration says so, so nobody later reads backfilled values as data the customer supplied in that shape.

Verified against a seeded database rather than reasoned about, because this is the part that cannot be covered by the suite: migrations run in globalSetup before any test, and the old column is gone afterwards. Six representative rows through the real migration gave Thom/Lamb, Mary/"Jane Smith", Cher/null, "  Padded  Name  " trimmed to Padded/Name, and null and whitespace-only names left as null on both. The down migration rejoins the parts and returns all six to their original strings.

The old column is dropped rather than kept alongside, so there is one source of truth instead of two that drift.

The admin keeps receiving a single composed display name. It only ever shows one — the list cell and the drawer title — and never edits one, so giving it both parts plus the joining logic would be work for no reader.

Churn was the bulk of this: 14 backend registrations and 10 end-to-end registration forms. A first attempt at the backend fixtures also added names to login and password-reset payloads, which would still have passed since the server ignores unknown fields, but a login test implying login takes a name is a small lie; that was reverted and redone against register calls only.

Verified: 172 unit, 183 integration and 95 end-to-end passing, lint unchanged at 4 backend and 27 frontend warnings.

Not covered: the cart reminder itself, which runs from a cron and had no test before this either. The greeting change is a one-line substitution in that query.

Refs #106
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 18:23:13 -05:00

101 lines
4.6 KiB
TypeScript

import { test, expect } from './fixtures';
const suffix = () => `r${Date.now().toString(36)}${Math.random().toString(36).slice(2, 7)}`;
// Reserves an item by registering a customer and adding it to their cart, which
// is the only way an item legitimately reaches 'reserved'.
async function reserveItem(page: import('@playwright/test').Page, itemName: string) {
const email = `reserve-${suffix()}@example.com`;
const created = await page.request.post('/api/admin/items', {
multipart: { name: itemName, description: '', price: '75', category_id: '', tags: '[]' }
});
expect(created.ok()).toBeTruthy();
const itemId = (await created.json()).id as number;
// New items are pending, and a pending item cannot be reserved.
expect((await page.request.post(`/api/admin/items/${itemId}/mark-available`)).ok()).toBeTruthy();
await page.goto('/register');
await page.getByRole('textbox', { name: 'Email' }).fill(email);
await page.getByRole('textbox', { name: 'First name' }).fill('Test');
await page.getByRole('textbox', { name: 'Last name' }).fill('Customer');
await page.getByLabel('Password').fill('supersecret123');
await page.getByRole('button', { name: 'Create account' }).click();
await expect(page.getByRole('button', { name: 'My Account' })).toBeVisible({ timeout: 20000 });
const added = await page.request.post(`/api/cart/items/${itemId}`);
expect(added.status()).toBe(201);
return { email, itemId };
}
test.describe('Admin reserved items', () => {
test('shows a reserved count that opens the held items', async ({ page }) => {
const itemName = `Held ${suffix()}`;
const { email } = await reserveItem(page, itemName);
await page.goto('/admin');
await page.getByRole('tab', { name: 'Customers' }).click();
const row = page.getByRole('row').filter({ hasText: email });
await expect(row).toBeVisible();
await row.getByRole('button', { name: /item/ }).click();
const dialog = page.getByRole('dialog', { name: /Items reserved by/ });
await expect(dialog.getByText(itemName)).toBeVisible();
});
test('releasing an item returns it to the storefront as available', async ({ page }) => {
const itemName = `Freed ${suffix()}`;
const { email, itemId } = await reserveItem(page, itemName);
await page.goto('/admin');
await page.getByRole('tab', { name: 'Customers' }).click();
const row = page.getByRole('row').filter({ hasText: email });
await row.getByRole('button', { name: /item/ }).click();
const dialog = page.getByRole('dialog', { name: /Items reserved by/ });
await dialog.getByRole('button', { name: 'Release' }).click();
await expect(page.getByText(`Released "${itemName}"`)).toBeVisible();
// The point of releasing is that the item becomes purchasable again.
const item = await (await page.request.get(`/api/items/${itemId}`)).json();
expect(item.status).toBe('available');
});
test('the count drops once the item is released', async ({ page }) => {
const itemName = `Recount ${suffix()}`;
const { email } = await reserveItem(page, itemName);
await page.goto('/admin');
await page.getByRole('tab', { name: 'Customers' }).click();
const row = page.getByRole('row').filter({ hasText: email });
await row.getByRole('button', { name: /item/ }).click();
const dialog = page.getByRole('dialog', { name: /Items reserved by/ });
await dialog.getByRole('button', { name: 'Release' }).click();
await expect(dialog.getByText("This customer isn't holding any items")).toBeVisible();
// The row behind the dialog must agree with the dialog it opened.
await dialog.getByRole('button', { name: 'Close' }).click();
await expect(row.getByRole('button', { name: /item/ })).toHaveCount(0);
});
test('a customer holding nothing shows no link to click', async ({ page }) => {
const email = `idle-${suffix()}@example.com`;
await page.goto('/register');
await page.getByRole('textbox', { name: 'Email' }).fill(email);
await page.getByRole('textbox', { name: 'First name' }).fill('Test');
await page.getByRole('textbox', { name: 'Last name' }).fill('Customer');
await page.getByLabel('Password').fill('supersecret123');
await page.getByRole('button', { name: 'Create account' }).click();
await expect(page.getByRole('button', { name: 'My Account' })).toBeVisible({ timeout: 20000 });
await page.goto('/admin');
await page.getByRole('tab', { name: 'Customers' }).click();
const row = page.getByRole('row').filter({ hasText: email });
await expect(row).toBeVisible();
await expect(row.getByRole('button', { name: /item/ })).toHaveCount(0);
});
});