diff --git a/src/adapters/http/client.ts b/src/adapters/http/client.ts index e8721266..97c0c5c1 100644 --- a/src/adapters/http/client.ts +++ b/src/adapters/http/client.ts @@ -1013,6 +1013,9 @@ async function parseResponse( // the same failure as a 422, so it gets the same kind and issues and a form // maps it with the same allowed fields. A 400 VALIDATION_FAILED whose body // did not parse carries `{cause}`, not a list, and keeps its reading. + // Field validation is checked when the request is bound, before the + // command runs, so it is NOT_APPLIED: the coordinator must not hold it as an + // unknown effect, or the form cannot save the corrected value. const fieldValidation = response.status === 422 || (response.status === 400 && @@ -1027,9 +1030,12 @@ async function parseResponse( ok: false, error: failure(kind, operation.operationId, attempt, { code, - ...(idempotencyRefusalReason(code, response.status) === undefined - ? {} - : { effect: "NOT_APPLIED" as const }), + // A refusal the server made before running the command: not applied, + // so the mutation coordinator does not hold it as an unknown effect. + ...(fieldValidation || + idempotencyRefusalReason(code, response.status) !== undefined + ? { effect: "NOT_APPLIED" as const } + : {}), httpStatus: response.status, requestId: safeMeta(envelopeRecord.meta).requestId, traceId: safeMeta(envelopeRecord.meta).traceId, diff --git a/tests/component/form-foundation.test.tsx b/tests/component/form-foundation.test.tsx index 9d4c9277..26c0bca2 100644 --- a/tests/component/form-foundation.test.tsx +++ b/tests/component/form-foundation.test.tsx @@ -9,7 +9,10 @@ import { describe, expect, it, vi } from "vitest"; import { createHttpClient } from "../../src/adapters/http/client.ts"; import { createFailure } from "../../src/contracts/errors.ts"; +import { createRuntimeIdentityRegistry } from "../../src/contracts/query-keys.ts"; +import { useApplicationMutation } from "../../src/presentation/adapters/query/application-query.ts"; import { TEST_HTTP_CONTRACT } from "../helpers/http-contract-fixture.ts"; +import { queryClient, scopeSnapshot, wrapper } from "./application-query-fixture.tsx"; import { Button } from "../../src/presentation/components/ui/button.ts"; import { DirtyNavigationDialog, @@ -578,3 +581,112 @@ describe("submission timing", () => { ).not.toBeInTheDocument(); }); }); + +/** + * A server validation refusal through the V2 client and the mutation + * coordinator, with the reference form page's gate: the save button is off + * while the coordinator holds an unknown effect. + */ +describe("correcting a field after a V2 validation refusal", () => { + let token = 0; + const scope = { + ...scopeSnapshot(), + identities: createRuntimeIdentityRegistry({ + tokenFactory: () => `form-identity-token-${String(++token).padStart(4, "0")}`, + }), + }; + + function CoordinatedForm(props: Readonly<{ fetcher: typeof fetch }>) { + const [client] = useState(() => + createHttpClient({ + ...TEST_HTTP_CONTRACT, + baseUrl: "https://api.test", + fetcher: props.fetcher, + }), + ); + const mutation = useApplicationMutation< + Readonly<{ name: string; note?: string }>, + unknown + >({ + definitionId: "v2-create-v1", + definitionVersion: 1, + operationId: "CREATE_ENTITY", + requiresIdempotencyKey: true, + owner: "platform-test", + duplicatePolicy: "REJECT_WHILE_ACTIVE", + scope, + invalidate: [], + execute: (command, { intent }) => + client.execute("CREATE_ENTITY", { + body: { name: command.name }, + idempotencyKey: intent.idempotencyKey, + }), + }); + const form = useAppForm({ + schema, + defaultValues: defaults, + allowedServerFields: ["name", "note"], + mapToCommand: (values) => ({ name: values.name }), + submit: mutation.submit, + }); + const blocked = + mutation.state.indicator === "mutation-effect-unknown" || + mutation.state.indicator === "mutation-pending"; + return ( +
void form.submitForm(event)}> + + + {form.result} + + ); + } + + const meta = { requestId: "request-1", traceId: "trace-1" }; + + // 백엔드 1101/02-01 후속. A 422 in the frontend contract's shape, and the + // paired backend's 400 VALIDATION_FAILED (its docs/web/http-contract.md §4). + it.each([ + { status: 422, error: { code: "INVALID_INPUT", details: { issues: [{ path: "name", code: "REQUIRED" }] } } }, + { + status: 400, + error: { + code: "VALIDATION_FAILED", + details: [{ field: "name", code: "NOT_BLANK", message: "필수 값이 없습니다." }], + }, + }, + ])("saves the corrected value after a $status field error", async ({ status, error }) => { + const user = userEvent.setup(); + const fetcher = vi + .fn() + .mockResolvedValueOnce( + Response.json({ success: false, error, meta }, { status }), + ) + .mockImplementation(async () => + Response.json({ + success: true, + data: { id: "entity-1", name: "Fixed" }, + meta, + }), + ); + render(, { + wrapper: wrapper(queryClient()), + }); + const name = screen.getByRole("textbox", { name: /Name/ }); + + await user.type(name, "Rejected"); + await user.click(screen.getByRole("button", { name: "Submit" })); + await waitFor(() => expect(name).toHaveAttribute("aria-invalid", "true")); + + await user.clear(name); + await user.type(name, "Fixed"); + expect(screen.getByRole("button", { name: "Submit" })).toBeEnabled(); + await user.click(screen.getByRole("button", { name: "Submit" })); + + await waitFor(() => + expect(screen.getByTestId("result")).toHaveTextContent("success"), + ); + expect(fetcher).toHaveBeenCalledTimes(2); + }); +}); diff --git a/tests/features/reference-feature/reference-production-vertical.test.tsx b/tests/features/reference-feature/reference-production-vertical.test.tsx index 685119e4..4c7c3fb5 100644 --- a/tests/features/reference-feature/reference-production-vertical.test.tsx +++ b/tests/features/reference-feature/reference-production-vertical.test.tsx @@ -388,4 +388,55 @@ describe("reference feature production vertical path", () => { expect(name).toHaveAttribute("aria-invalid", "true"); expect(document.body).not.toHaveTextContent("backend copy"); }); + + // A validation refusal is not an unknown effect: the corrected value can + // be saved (the create classifier answers 422 NOT_APPLIED). + it("saves the corrected value after a server 422 field error", async () => { + const user = userEvent.setup(); + let sent = 0; + mockApi.server.use( + http.post("https://api.test/api/reference-resources", async ({ request }) => { + sent += 1; + if (sent === 1) { + return HttpResponse.json( + { + type: "https://api.test/problems/validation_rejected", + title: "VALIDATION_REJECTED", + status: 422, + code: "VALIDATION_REJECTED", + issues: [{ path: "name", code: "too_big" }], + }, + { status: 422 }, + ); + } + const body = (await request.json()) as { name: string }; + return HttpResponse.json( + { id: "reference-created-1", name: body.name }, + { status: 201 }, + ); + }), + ); + const composition = await createRuntimeComposition({ + fetcher: absoluteFetch, + host: {}, + }); + window.history.pushState({}, "", "/examples/reference-resources/new"); + render(); + + await user.click( + await screen.findByRole("button", { name: "로그인 시작" }), + ); + const name = await screen.findByRole("textbox", { name: /새 항목 이름/ }); + await user.type(name, "Too long for the server"); + await user.click(screen.getByRole("button", { name: "저장" })); + await waitFor(() => expect(name).toHaveAttribute("aria-invalid", "true")); + + await user.clear(name); + await user.type(name, "Short"); + expect(screen.getByRole("button", { name: "저장" })).toBeEnabled(); + await user.click(screen.getByRole("button", { name: "저장" })); + + expect(await screen.findByText("저장했습니다.")).toBeVisible(); + expect(sent).toBe(2); + }); }); diff --git a/tests/integration/http-client.test.ts b/tests/integration/http-client.test.ts index d640a775..d20c8622 100644 --- a/tests/integration/http-client.test.ts +++ b/tests/integration/http-client.test.ts @@ -663,6 +663,7 @@ describe("paired backend 400 VALIDATION_FAILED", () => { code: "VALIDATION_FAILED", httpStatus: 400, retryable: false, + effect: "NOT_APPLIED", validationIssues: [ { path: "name", code: "NOT_BLANK" }, { path: "items.name", code: "SIZE" }, @@ -686,5 +687,6 @@ describe("paired backend 400 VALIDATION_FAILED", () => { error: { kind: "UNKNOWN_CLIENT_FAILURE", code, httpStatus: 400 }, }); expect(result.ok ? null : result.error.validationIssues).toBeUndefined(); + expect(result.ok ? null : result.error.effect).toBeUndefined(); }); }); diff --git a/tests/integration/http-diagnostics.test.ts b/tests/integration/http-diagnostics.test.ts index 96a33996..cc175c76 100644 --- a/tests/integration/http-diagnostics.test.ts +++ b/tests/integration/http-diagnostics.test.ts @@ -255,15 +255,20 @@ describe("HTTP diagnostics and terminal telemetry", () => { }, ); - it("keeps an unrelated 422 effect-free and reason-free", async () => { + it.each([ + // A field validation refusal is not applied, but names no idempotency cause. + { status: 422, code: "INVALID_INPUT", effect: "NOT_APPLIED" }, + // Any other refusal keeps no effect: the coordinator holds it as unknown. + { status: 409, code: "SOMETHING_NEW", effect: undefined }, + ])("gives an unrelated $status $code no reason", async ({ status, code, effect }) => { const fetcher = vi.fn(async () => Response.json( { success: false, - error: { code: "INVALID_INPUT" }, + error: { code }, meta: { requestId: "request-1", traceId: "trace-1" }, }, - { status: 422 }, + { status }, ), ); const { client, records } = harness(fetcher); @@ -274,7 +279,7 @@ describe("HTTP diagnostics and terminal telemetry", () => { body: { name: "n" }, }); - expect(result.ok ? undefined : result.error.effect).toBeUndefined(); + expect(result.ok ? undefined : result.error.effect).toBe(effect); expect(records[0]?.context).not.toHaveProperty("reason"); }); });