Files
redefined-designs/frontend/tests/e2e/admin-reserved-items.spec.ts
T
bermudalambandClaude Opus 5 ecc2219fa5
SonarQube Analysis / sonarqube (pull_request) Failing after 38m41s
Tests / lint (pull_request) Successful in 8m37s
Tests / backend-unit (pull_request) Successful in 1m22s
Tests / frontend-e2e (pull_request) Failing after 30m44s
feat: stage new items as pending until an admin publishes them (#90)
An item used to be live on the storefront the instant it was created. Now it starts pending, and a customer sees it only once it is published.

The migration changes the column default and nothing else. Backfilling would un-publish the entire live catalogue, which is the one thing it must not do.

Hiding a pending item took four separate changes, not one, and that is the part worth knowing. The storefront's item routes had no status filter at all — sold items are listed and rendered with a Sold badge deliberately — so pending could not be expressed as one more optional filter. GET /api/items now carries an exclusion the caller cannot opt out of; GET /api/items/:id carries the same, because hiding an item from the list while still serving it by id would leave it reachable to anyone who kept a link; and GET /api/filters excludes pending from both aggregates it computes. That last one is the least obvious: a pending item would have inflated its tags' counts, so a customer would read "Rare (1)", filter by it, and be told nothing matches — and its price would have stretched the slider to a range no visible item occupies.

The tag count is computed over the joined items rather than filtered with a WHERE. A WHERE would have dropped the row for a tag whose only item is pending, and the tag would have vanished from the drawer instead of showing zero. There is a test for exactly that, because the first version of this query had that bug.

parseItemFilters is shared by the storefront and admin routes, so 'pending' parses on both. The public route refuses it explicitly rather than answering with an empty list, which would read as "no items match" instead of "you may not ask that". The storefront's URL reader is deliberately left not accepting it either, with a comment saying so, since a request guaranteed to fail is not worth constructing.

Publishing is the existing mark-available: same transition, same UPDATE, so the admin UI labels that button "Publish" when the item is pending rather than adding a second endpoint that does the same thing. Unpublish is new and is not symmetrical — it is refused for a reserved item, which someone is holding in their cart right now, and for a sold one, which is a record of something that happened rather than a draft. Both refusals name their reason, and the buttons are hidden in those states so the refusal is not how you find out.

Changing a column default has reach, and it surfaced eight test fixtures that silently depended on it. Each is now explicit about the status it wants rather than inheriting one — better practice regardless, and immune to the next default change. Two tests also used 'pending' as their example of an *unknown* status; both would have quietly become tautologies, so they now use one that is genuinely unknown.

Verified: 98 unit, 160 integration and 94 end-to-end passing, the last on a freshly created container. One earlier run showed a single failure in favorites.spec.ts; it passes in isolation and on a clean container, and is the cross-spec interference already recorded against the suite rather than anything from this change.

Refs #90
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 12:40:08 -05:00

97 lines
4.3 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.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.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);
});
});