lotr-sut/tests/e2e/utils/testApiUtils.ts
Fellowship Scholar f6a5823439 init commit
2026-03-29 20:07:56 +00:00

51 lines
1.9 KiB
TypeScript

import { Page } from '@playwright/test';
/**
* Test-only API utilities for per-test data cleanup.
*
* These helpers call backend endpoints that are only active outside production
* (guarded by FLASK_ENV check in the backend).
*
* Design notes
* ────────────
* • resetShopState — calls POST /api/shop/test-reset. No auth required so it
* can (and should) be called BEFORE login, avoiding any
* risk of session-cookie side-effects.
*
* • NPC chat sessions do NOT need resetting between tests because each test
* runs in a fresh Playwright browser context (new cookies → new scope_id).
* The backend keys conversations by (user_id, scope_id, character), so a new
* scope_id means a clean slate automatically.
*
* Endpoints used
* ──────────────
* POST /api/shop/test-reset — marks all items unsold, restores all user gold
* to 500, clears all inventory entries.
*/
export class TestApiUtils {
/**
* Reset shop DB state: unsell items, restore gold (→ 500), clear inventory.
*
* Must be called BEFORE login (no auth cookie required) so that it cannot
* accidentally modify the Flask session and corrupt subsequent auth checks.
*/
static async resetShopState(page: Page): Promise<void> {
const baseUrl = process.env.SUT_URL || 'http://localhost';
const response = await page.request.post(`${baseUrl}/api/shop/test-reset`);
if (!response.ok()) {
console.warn(
`[TestApiUtils] /api/shop/test-reset returned ${response.status()}` +
'shop state may be dirty for this test',
);
}
}
/**
* Full bargaining test teardown.
* Only resets shop DB state; NPC sessions auto-isolate per browser context.
*/
static async cleanupBargainingState(page: Page): Promise<void> {
await TestApiUtils.resetShopState(page);
}
}