diff --git a/docs/operations/api-contract-and-server-state-recovery.md b/docs/operations/api-contract-and-server-state-recovery.md index 4179a062..74e0ed3e 100644 --- a/docs/operations/api-contract-and-server-state-recovery.md +++ b/docs/operations/api-contract-and-server-state-recovery.md @@ -260,10 +260,12 @@ diagnostics에만 남고 `api.request.failed`에는 세지 않는다(03-07). 그 `IDEMPOTENT_REQUEST_MISMATCH`, 기능이 선언한 code) 보내지 못하면(`NOT_STARTED`) 그 시도만 적용되지 않은 것이다. 결과 불명과 key는 그대로다. 폼은 "잠시 뒤 같은 요청으로 다시 보내 주세요"와 결과 불명 화면(확인, 다시 보내기)을 보이고 저장을 받지 않는다. - 401은 다시 로그인 안내가 먼저다. 예외는 같은 요청이 매번 받는 거절이다. 입력 검증 - (`VALIDATION_REJECTED`, 400 `VALIDATION_FAILED`)과 400 `IDEMPOTENCY_KEY_INVALID`는 첫 - 요청도 거절됐다는 뜻이라 결과 불명을 풀고, 폼은 필드 오류를 보이고 저장을 다시 - 받는다(1106/03-01, `src/contracts/errors.ts` `isRefusedOnEveryAttempt`). + 401은 다시 로그인 안내가 먼저다. 예외는 같은 요청이 매번 받는 거절이다. 짝 백엔드 표의 + 400 `VALIDATION_FAILED`(필드 목록이 있든 없든)와 400 `IDEMPOTENCY_KEY_INVALID`만 첫 + 요청도 거절됐다는 뜻으로 읽어 결과 불명을 풀고, 폼은 필드 오류를 보이고 저장을 다시 + 받는다. 기능의 `mapProblem`이 정한 kind(`VALIDATION_REJECTED`)는 보지 않는다. 기능 + code는 상태 규칙일 수 있다(1106/03-01, `src/contracts/errors.ts` + `isRefusedOnEveryAttempt`). 키가 없는 command(`NEVER`)는 서버가 두 번째 요청을 가려내지 못하므로 재전송 버튼을 그리지 않는다(`canResendUnknownEffect`, 0929/03-03). 같은 canonical input의 재저장도 보내지 않는다. 훅의 `submit`이 `NOT_STARTED` `DUPLICATE_IN_FLIGHT`(code diff --git a/src/contracts/errors.ts b/src/contracts/errors.ts index 9290d49f..f6b98b62 100644 --- a/src/contracts/errors.ts +++ b/src/contracts/errors.ts @@ -260,17 +260,18 @@ export const REFUSALS_OF_EVERY_ATTEMPT: Readonly> = }); /** - * 1106/03-01 (D10 supplement). A `NOT_APPLIED` refusal the same request gets - * on every attempt: input validation (`VALIDATION_REJECTED`) or a row above. - * A resend of an unknown request that meets one shows the first attempt was - * refused too, so the request is settled. Not one: a refusal before the - * idempotency check or one that changes over time (429, 401, 403, 503 - * RATE_LIMIT_*), a code a feature declared refused before its write (it may - * be a rule on changing state), any other code, and `NOT_STARTED`. + * 1106/03-01 (D10 supplement and its second fix). A `NOT_APPLIED` refusal + * the same request gets on every attempt: a row above, code and status + * together. A resend of an unknown request that meets one shows the first + * attempt was refused too, so the request is settled. Not one: a refusal + * before the idempotency check or one that changes over time (429, 401, + * 403, 503 RATE_LIMIT_*), a code a feature declared refused before its + * write (it may be a rule on changing state), any other code, and + * `NOT_STARTED`. The kind is not read: a feature's `mapProblem` sets it, and + * it maps its own codes, or every 422, to `VALIDATION_REJECTED`. */ export function isRefusedOnEveryAttempt(failure: AppFailure): boolean { if (failure.effect !== "NOT_APPLIED") return false; - if (failure.kind === "VALIDATION_REJECTED") return true; return ( Object.hasOwn(REFUSALS_OF_EVERY_ATTEMPT, failure.code) && REFUSALS_OF_EVERY_ATTEMPT[failure.code] === failure.httpStatus diff --git a/src/presentation/adapters/query/application-query.ts b/src/presentation/adapters/query/application-query.ts index 03bdca14..5837e348 100644 --- a/src/presentation/adapters/query/application-query.ts +++ b/src/presentation/adapters/query/application-query.ts @@ -636,10 +636,10 @@ export function useApplicationMutation( // that this attempt did not apply; the first may have. The request // stays unknown with its intent, so the next save or resend carries // the same key, never a new one (backend http-contract.md §2). A - // refusal every attempt of the request gets (input validation, a key - // the server's rule rejects) shows the first was refused too, so the - // request is settled and the form can take a corrected save - // (isRefusedOnEveryAttempt, D10 supplement). + // refusal every attempt of the request gets (the paired backend's 400 + // VALIDATION_FAILED and IDEMPOTENCY_KEY_INVALID) shows the first was + // refused too, so the request is settled and the form can take a + // corrected save (isRefusedOnEveryAttempt, D10 supplement). const resendLeavesUnknown = (failure: AppFailure): boolean => replayed !== undefined && (!scope || scope.isCurrent()) && diff --git a/tests/component/application-mutation-admission.test.tsx b/tests/component/application-mutation-admission.test.tsx index a07c961b..944c8a9a 100644 --- a/tests/component/application-mutation-admission.test.tsx +++ b/tests/component/application-mutation-admission.test.tsx @@ -759,6 +759,26 @@ describe("application mutation intent admission", () => { effect: "NOT_APPLIED", }), ], + // Final review FR-01. The kind is the feature's mapProblem: the + // scaffold maps its declared code to a field error, and the reference + // gateway maps every 422 to one, whatever the code. + [ + "refused with a declared code the feature maps to a field error", + createFailure("VALIDATION_REJECTED", "CREATE_REPLAYED", 0, { + code: "WORKLOG_INVALID", + httpStatus: 400, + effect: "NOT_APPLIED", + validationIssues: [{ path: "name", code: "INVALID" }], + }), + ], + [ + "refused with 422 IDEMPOTENT_REQUEST_MISMATCH read as a field error", + createFailure("VALIDATION_REJECTED", "CREATE_REPLAYED", 0, { + code: "IDEMPOTENT_REQUEST_MISMATCH", + httpStatus: 422, + effect: "NOT_APPLIED", + }), + ], ])("keeps the request unknown with its key when the resend is %s", async (_label, refusal) => { const { hook, intents } = renderKeyedCreate([ unknown, diff --git a/tests/features/reference-feature/reference-page.test.tsx b/tests/features/reference-feature/reference-page.test.tsx index 71052085..783915ac 100644 --- a/tests/features/reference-feature/reference-page.test.tsx +++ b/tests/features/reference-feature/reference-page.test.tsx @@ -415,9 +415,10 @@ describe("reference feature page states", () => { /** * 1106/03-01 (D10 supplement). The form is never held for good after a * same-key resend. A refusal that may change (429) keeps the result - * unknown and the confirm and resend actions on screen. A field error the - * request gets on every attempt settles it: the field shows the error and - * the save is on again. + * unknown and the confirm and resend actions on screen. So does a 422 + * (final review FR-01): this feature's gateway reads every 422 as a field + * error whatever its code, and only the paired backend's own table codes + * settle a resent request. */ async function resendAnUnknownCreate(resendAnswer: ReferenceResult) { const user = userEvent.setup(); @@ -467,30 +468,26 @@ describe("reference feature page states", () => { expect(await screen.findByText("저장했습니다.")).toBeVisible(); }); - it("shows the field error and saves again when the resend is refused on its input", async () => { - const { user, name, createResource } = await resendAnUnknownCreate({ + it("keeps the confirm on screen when the resend is refused with a 422", async () => { + const { user, name } = await resendAnUnknownCreate({ ok: false, error: createFailure("VALIDATION_REJECTED", "CREATE_REFERENCE_RESOURCE", 0, { + code: "REFERENCE_RESOURCE_REJECTED", effect: "NOT_APPLIED", httpStatus: 422, validationIssues: [{ path: "name", code: "REQUIRED" }], }), }); - await waitFor(() => expect(name).toHaveAttribute("aria-invalid", "true")); - expect(screen.queryByText("변경 결과를 확인할 수 없습니다.")).not.toBeInTheDocument(); - expect(screen.getByRole("button", { name: "저장" })).toBeEnabled(); - await user.clear(name); - await user.type(name, "Corrected"); - await user.click(screen.getByRole("button", { name: "저장" })); + expect( + await screen.findByText(KO_MESSAGES["form.resendLater"]), + ).toBeVisible(); + expect(screen.getByText("변경 결과를 확인할 수 없습니다.")).toBeVisible(); + expect(name).not.toHaveAttribute("aria-invalid"); + expect(screen.getByRole("button", { name: "저장" })).toBeDisabled(); + await user.click(screen.getByRole("button", { name: "변경됨으로 확인" })); expect(await screen.findByText("저장했습니다.")).toBeVisible(); - const keys = createResource.mock.calls.map(([, context]) => context?.intent?.idempotencyKey); - expect(keys).toEqual([ - "reference-page-key-1", - "reference-page-key-1", - "reference-page-key-2", - ]); }); it.each([ @@ -729,7 +726,9 @@ describe("reference feature page states", () => { * 1106/08-01. The form the user came back to did not start the save * still running, and holds none of its result (review probe 08-R1). */ - async function leaveDuringSaveAndComeBack() { + async function leaveDuringSaveAndComeBack( + ...laterAnswers: readonly ReferenceResult[] + ) { const user = userEvent.setup(); let release: | ((result: ReferenceResult) => void) @@ -742,6 +741,7 @@ describe("reference feature page states", () => { release = resolve; }), ); + for (const answer of laterAnswers) createResource.mockResolvedValueOnce(answer); renderReference( inputWith({ createResource }), "/examples/reference-resources/new", @@ -761,12 +761,56 @@ describe("reference feature page states", () => { expect(await screen.findByText("변경 사항을 저장하고 있습니다.")).toBeVisible(); return { user, + createResource, release(result: ReferenceResult) { release?.(result); }, }; } + // 1106/03-01 (final review FR-01). The remounted form shows other values + // than the save it did not start; its resend sends that save, with its + // key. A 422 IDEMPOTENT_REQUEST_MISMATCH there says the key already + // reached the server, so the first request may have applied: it stays + // unknown and the confirm stays on screen. + it("keeps a resent request unknown on a remounted form when the server answers 422 IDEMPOTENT_REQUEST_MISMATCH", async () => { + const { user, createResource, release } = await leaveDuringSaveAndComeBack({ + ok: false, + error: createFailure("VALIDATION_REJECTED", "CREATE_REFERENCE_RESOURCE", 0, { + code: "IDEMPOTENT_REQUEST_MISMATCH", + effect: "NOT_APPLIED", + httpStatus: 422, + }), + }); + release({ + ok: false, + error: createFailure("SERVER_FAILURE", "CREATE_REFERENCE_RESOURCE", 0, { + effect: "MAYBE_APPLIED", + }), + }); + await screen.findByRole("button", { name: "같은 요청으로 다시 보내기" }); + await user.type( + screen.getByRole("textbox", { name: /새 항목 이름/ }), + "Second item", + ); + + await user.click( + screen.getByRole("button", { name: "같은 요청으로 다시 보내기" }), + ); + await waitFor(() => expect(createResource).toHaveBeenCalledTimes(2)); + + expect( + await screen.findByText(KO_MESSAGES["form.resendLater"]), + ).toBeVisible(); + expect(screen.getByText("변경 결과를 확인할 수 없습니다.")).toBeVisible(); + expect(screen.getByRole("button", { name: "변경됨으로 확인" })).toBeVisible(); + const [first, second] = createResource.mock.calls; + expect(second?.[0]).toEqual(first?.[0]); + expect(second?.[1]?.intent?.idempotencyKey).toBe( + first?.[1]?.intent?.idempotencyKey, + ); + }); + // 1106/08-01 (A). Confirming that save from the remounted form used to // show nothing and drop focus to the body: the form had no submitted // values to settle.