import type { Page, Route } from "@playwright/test"; import { expect, test } from "../support/browser/strict-browser-test.ts"; const CREATE_URL = "http://localhost:8080/api/reference-resources"; type CreateCall = Readonly<{ name: unknown; idempotencyKey: string | null }>; async function openForm(page: Page) { await page.route(`${CREATE_URL}?*`, (route) => route.fulfill({ json: [] })); await page.goto("/examples/reference-resources/new"); await page.getByRole("button", { name: "로그인 시작" }).click(); await expect( page.getByRole("heading", { name: "Reference resource 만들기" }), ).toBeVisible(); } /** Answers each POST with the next responder and records what was sent. */ async function routeCreate( page: Page, responders: readonly ((route: Route) => Promise)[], ): Promise { const calls: CreateCall[] = []; await page.route(CREATE_URL, async (route) => { const request = route.request(); if (request.method() !== "POST") return route.fallback(); calls.push({ name: (request.postDataJSON() as { name?: unknown }).name, idempotencyKey: await request.headerValue("idempotency-key"), }); const respond = responders[calls.length - 1]; if (!respond) throw new Error(`unexpected create call ${calls.length}`); await respond(route); }); return calls; } /** * 1103/08-02. Saves from the keyboard and checks where focus lands: in the * failure status above the form, one Tab away from its first action, not on * the body after the page disabled the save button. */ async function saveFromKeyboard(page: Page, expectedFirstAction: string) { await page .getByRole("textbox", { name: /새 항목 이름/ }) .fill("Saved in the browser"); await page.getByRole("button", { name: "저장" }).focus(); await page.keyboard.press("Enter"); await expect(page.locator(".page-template__feedback")).toBeFocused(); await page.keyboard.press("Tab"); await expect( page.getByRole("button", { name: expectedFirstAction }), ).toBeFocused(); } const created = (route: Route) => route.fulfill({ status: 201, json: { id: "browser-created", name: "Saved in the browser", createdAt: "2026-07-26T00:00:00.000Z", }, }); test("saves a new reference resource through the production command path", async ({ page, }) => { await openForm(page); const calls = await routeCreate(page, [created]); await page .getByRole("textbox", { name: /새 항목 이름/ }) .fill("Saved in the browser"); await page.getByRole("button", { name: "저장" }).click(); await expect(page.getByRole("status")).toHaveText("저장했습니다."); expect(calls).toHaveLength(1); expect(calls[0]?.name).toBe("Saved in the browser"); expect(calls[0]?.idempotencyKey).toBeTruthy(); }); test.describe("after a rate-limited save", () => { // Chromium and WebKit log a 4xx fetch response to the console, and the // client cancels the 429 body unread, which Chromium reports as aborted. test.use({ allowedBrowserFailure: /^console:error:.*status of 429|^requestfailed:POST:\/api\/reference-resources:net::ERR_ABORTED$/u, }); test("keeps the input and saves on retry", async ({ page }) => { await openForm(page); const calls = await routeCreate(page, [ (route) => route.fulfill({ status: 429, headers: { "retry-after": "1" }, json: { title: "Too many requests" }, }), created, ]); const name = page.getByRole("textbox", { name: /새 항목 이름/ }); await name.fill("Saved in the browser"); await page.getByRole("button", { name: "저장" }).click(); await expect( page.getByText("요청이 많습니다. 잠시 후 다시 시도해 주세요."), ).toBeVisible(); await expect(name).toHaveValue("Saved in the browser"); await page.getByRole("button", { name: "다시 시도" }).click(); await expect(page.getByRole("status")).toHaveText("저장했습니다."); expect(calls.map((call) => call.name)).toEqual([ "Saved in the browser", "Saved in the browser", ]); }); test("keeps keyboard focus in the failure status", async ({ page }) => { await openForm(page); await routeCreate(page, [ (route) => route.fulfill({ status: 429, headers: { "retry-after": "1" }, json: { title: "Too many requests" }, }), ]); await saveFromKeyboard(page, "다시 시도"); }); }); test.describe("after a save whose result is unknown", () => { // The browsers log the 500 response to the console. test.use({ allowedBrowserFailure: /^console:error:.*status of 500/u, }); // 0929/08-04. The case a user most wants to report shows the id support // can find in the server logs. test("shows the request id the server echoed", async ({ page }) => { await openForm(page); await routeCreate(page, [ (route) => route.fulfill({ status: 500, headers: { "x-correlation-id": "corr-save-500", "access-control-expose-headers": "x-correlation-id", }, json: { title: "Internal error" }, }), ]); await page .getByRole("textbox", { name: /새 항목 이름/ }) .fill("Saved in the browser"); await page.getByRole("button", { name: "저장" }).click(); await expect(page.getByText("변경 결과를 확인할 수 없습니다.")).toBeVisible(); await expect(page.getByText("지원 참조: corr-save-500")).toBeVisible(); }); test("keeps keyboard focus in the unknown-result status", async ({ page }) => { await openForm(page); await routeCreate(page, [ (route) => route.fulfill({ status: 500, json: { title: "Internal error" } }), ]); await saveFromKeyboard(page, "변경됨으로 확인"); }); });