Files
clean-architecture-frontend…/tests/unit/backend-envelope.test.ts
T
DongHyeonka 613c1cfd0c fix(http): 필드 목록이 없는 422를 입력 오류로 읽지 않는다
문제
Envelope 실패 해석이 422를 모두 필드 검증으로 본다
(isFieldValidationFailure). 짝 백엔드의 422 IDEMPOTENT_REQUEST_MISMATCH는
"같은 키를 다른 본문으로 보냈다"는 뜻이고 필드 목록이 없다. 그런데 kind가
VALIDATION_REJECTED가 되어 폼이 "입력값을 다시 확인해 주세요."를 띄운다.
사용자는 고칠 것이 없는 입력을 고치라는 말을 듣고, validation-error 상태는
지원 참조도 보이지 않는다. fileserver의 422(INTEGRITY_MISMATCH 등)도 같다.

고른 방법
422는 details에서 필드 목록(safeValidationIssues가 읽는 모양)이 나올 때만
필드 검증으로 본다. 필드 목록이 없는 422의 kind는 UNKNOWN_CLIENT_FAILURE다.
400 IDEMPOTENCY_KEY_INVALID와 같은 읽기다. 폼은 unavailable 상태와 지원
참조를 보인다. effect 판정(표)은 바꾸지 않았다. 422 MISMATCH는 그대로
NOT_APPLIED, 표 밖 422는 그대로 MAYBE_APPLIED다.

버린 대안
IDEMPOTENT_REQUEST_MISMATCH 한 code만 빼기: fileserver 422처럼 필드 목록
없는 다른 422가 계속 입력 오류로 보인다.

위험·한계
422로 필드 오류를 보내면서 details에 목록을 싣지 않는 백엔드는 이제 필드
오류 대신 실패 상태를 보인다. 짝 백엔드는 필드 검증을 400으로 보낸다.

검증
- RED: backend-envelope 단위 1건, envelope 통합 2건(422 MISMATCH·
  INTEGRITY_MISMATCH의 kind), form-foundation 1건(결과 unavailable, 문구
  없음) 실패.
- GREEN: tests/unit·integration·component·features·contract 250파일
  3,114건 통과. details.issues가 있는 422는 그대로 필드 오류다
  ("keeps the save off after a 422 the backend may have applied").
- check:types:app·test, eslint 통과.

Refs: docs/reviews/2026-11-03-maturity-review/08-presentation.md 08-05
2026-11-04 20:27:53 +09:00

83 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
envelopeFailureEffect,
envelopeFailureReading,
idempotencyRefusalReason,
} from "../../src/adapters/http/backend-envelope.ts";
/**
* 1103/10-02. The backend's refusal table, read status by status. The
* integration test sends each table code with its own status; these rows pin
* the edges it does not reach: a table code under another status, a 422
* outside the table, and the gateway statuses that keep the server-failure
* kind.
*/
describe("backend Envelope failure table", () => {
it.each([
[400, "VALIDATION_FAILED", undefined],
[400, "IDEMPOTENCY_KEY_INVALID", "IDEMPOTENCY_KEY_RULE_MISMATCH"],
[422, "IDEMPOTENT_REQUEST_MISMATCH", "IDEMPOTENT_REQUEST_MISMATCH"],
] as const)("reads %i %s as refused before the write", (status, code, reason) => {
expect(envelopeFailureEffect(status, { code })).toBe("NOT_APPLIED");
expect(idempotencyRefusalReason(code, status)).toBe(reason);
});
// The table names a status and a code together. The same code under
// another status is not the refusal the backend documents.
it.each([
[409, "VALIDATION_FAILED"],
[422, "VALIDATION_FAILED"],
[400, "IDEMPOTENT_REQUEST_MISMATCH"],
[409, "IDEMPOTENT_REQUEST_MISMATCH"],
[422, "IDEMPOTENCY_KEY_INVALID"],
] as const)("reads a table code under another status (%i %s) as maybe applied", (status, code) => {
expect(envelopeFailureEffect(status, { code })).toBe("MAYBE_APPLIED");
expect(idempotencyRefusalReason(code, status)).toBeUndefined();
});
it.each([
[400, "BAD_PARAMETER"],
[409, "IDEMPOTENT_IN_FLIGHT"],
[409, "RESOURCE_CONFLICT"],
// A 422 outside the table stays with the backend's verdict, never with a
// package that reads every 422 as refused (fileserver writes first).
[422, "INTEGRITY_MISMATCH"],
] as const)("keeps %i %s, outside the table, maybe applied", (status, code) => {
expect(envelopeFailureEffect(status, { code })).toBe("MAYBE_APPLIED");
});
it("leaves a status the table does not speak for to the package", () => {
expect(envelopeFailureEffect(404, { code: "NOT_FOUND" })).toBeUndefined();
});
// 1103/08-05. Only a field list makes a 422 an input error.
it("reads a 422 without a field list as a client failure, not an input error", () => {
expect(
envelopeFailureReading(422, { code: "IDEMPOTENT_REQUEST_MISMATCH" }),
).toEqual({ kind: "UNKNOWN_CLIENT_FAILURE", code: "IDEMPOTENT_REQUEST_MISMATCH" });
expect(envelopeFailureReading(422, { code: "INTEGRITY_MISMATCH" }).kind).toBe(
"UNKNOWN_CLIENT_FAILURE",
);
expect(
envelopeFailureReading(422, {
code: "INVALID_INPUT",
details: { issues: [{ path: "name", code: "REQUIRED" }] },
}),
).toEqual({
kind: "VALIDATION_REJECTED",
code: "INVALID_INPUT",
validationIssues: [{ path: "name", code: "REQUIRED" }],
});
});
it.each([408, 425, 500, 502, 503, 504])(
"reads a gateway %i as a server failure, not a client one",
(status) => {
expect(envelopeFailureReading(status, { code: "GATEWAY_REFUSED" }).kind).toBe(
"SERVER_FAILURE",
);
},
);
});