Files
clean-architecture-frontend…/tests/unit/backend-envelope.test.ts
T
DongHyeonka 1e8b3d049f feat(http): operation이 쓰기 전 업무 거절 code를 선언한다
문제
Envelope operation의 400·409·422는 백엔드 표의 세 code만 NOT_APPLIED이고
나머지는 모두 MAYBE_APPLIED다. 기능의 classifyProblem은 이를 바꿀 수 없다.
짝 백엔드 생성 기능의 create는 이름 규칙을 트랜잭션 전에 검사하고 400
<FEATURE>_INVALID를 준다. 그런데 폼은 "변경 결과를 확인할 수 없습니다"를
띄우고 저장 버튼을 끈다. 입력을 고쳐도 다시 저장할 수 없다(리뷰 실험
X-A·X-B). 401·403·429는 classifyStatus로 operation이 정할 수 있는데
400·409·422는 정할 자리가 없다.

고른 방법 (결정 D2 ①)
- CommandEffectDescriptor에 선택 칸 refusedBeforeWrite?: readonly string[].
  표 밖 400·409·422가 이 목록의 code면 NOT_APPLIED다. 함수가 아니라 code
  목록이라 리뷰할 수 있고, status 범위를 통째로 NOT_APPLIED로 만들 수 없다.
- 조립 검사(assertRefusedBeforeWrite): 목록은 Envelope operation에만,
  code는 백엔드 문법, 중복 금지. IDEMPOTENT_*·IDEMPOTENCY_*는 거절한다
  (409 IDEMPOTENT_IN_FLIGHT의 앞선 요청은 적용될 수 있다).
- 실행기도 같은 code와 표의 code(VALIDATION_FAILED 등)를 무시한다. 조립
  없이 실행기를 부르는 경로의 fail-close다.
- 조립이 목록을 얼려 복사한다(LIVE-03). classifier는 지금처럼 참조로
  두고, 설치본이 원본을 prototype으로 물려받아 classifier의 this가 그대로다.
- 매뉴얼 02 4절과 계약 문서 6.1절에 언제 적고 언제 적지 않는지,
  <FEATURE>_INVALID 예와 거부 문구를 적었다.

버린 대안
- 표 밖 400 전체를 NOT_APPLIED: BAD_PARAMETER·MAPPING_FAILED는 쓰기 뒤에도
  나온다.
- 분류기 판정 복원: 3261208이 버린 길. 400 전체를 NOT_APPLIED로 보는
  분류기가 키 보호를 푼다.
- 결과 불명 화면에 "적용되지 않음" 버튼: 사용자가 모르고 누르면 새 키로
  중복이 생긴다.
- 백엔드 단계 신호(error.applied): 두 repo 계약 변경이다(D2 ②).

위험·한계
- 쓰기 뒤에도 나오는 code를 잘못 적으면 결과 불명 요청이 새 키로 나가
  중복이 생길 수 있다. 책임은 선언한 기능 작성자에게 간다. 매뉴얼이 백엔드
  코드에서 던지는 곳을 확인한 code만 적으라고 한다.
- 생성기(scripts/create-feature.ts)는 이 칸을 아직 쓰지 않는다. 다른 작업이
  이어 받는다.
- 결과 불명 화면이 거절 code의 문구를 보이는 것(08-01 (가))은 하지 않았다.

검증
- RED: 새 테스트 17건이 이 변경 전 소스에서 실패(조립 규칙 10, 판정 3,
  envelope 통합 3, form-foundation 1).
- GREEN: tests/unit·integration·component·features·contract·runtime-schema
  253파일 3,199건 통과. 폼: WORKLOG_INVALID를 선언하면 결과 unavailable,
  저장 켜짐, 고친 값 저장 fetch 2회, 키 key-1 → key-2. 선언하지 않으면
  effect-unknown, 저장 꺼짐, fetch 1회.
- 변이: 실행기의 멱등성·표 code 무시 제거, 조립의 멱등성 규칙 제거, 목록
  복사 제거, 실행기가 목록을 넘기지 않음 모두 검출. 보존 조건 M5·M10·M24도
  계속 검출.
- check:types, lint, check:registries, check:architecture,
  verify:documentation 통과.

Refs: docs/reviews/2026-11-03-maturity-review/03-adapters-http-state.md 03-01
Refs: docs/reviews/2026-11-03-maturity-review/08-presentation.md 08-01
2026-11-04 20:38:12 +09:00

152 lines
6.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
envelopeFailureEffect,
envelopeFailureReading,
idempotencyRefusalReason,
invitesKeyedResend,
} 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");
});
// 1103/03-04. http-contract.md §2 and §4: the command may have run.
it.each([
[503, "IDEMPOTENCY_UNAVAILABLE", true],
[503, "IDEMPOTENCY_COMPLETION_UNKNOWN", false],
[500, "IDEMPOTENCY_RECOVERY_REQUIRED", false],
[503, "REQUEST_TIMEOUT", false],
] as const)("reads %i %s as maybe applied", (status, code, resend) => {
expect(envelopeFailureEffect(status, { code })).toBe("MAYBE_APPLIED");
expect(invitesKeyedResend(status, { code })).toBe(resend);
});
// 1103/03-01. An operation names the codes its endpoint raises only before
// it writes (`commandEffect.refusedBeforeWrite`).
describe("with the operation's declared pre-write refusals", () => {
const declared = ["WORKLOG_INVALID", "POSTER_ALREADY_EXISTS", "VALIDATION_FAILED"];
it.each([
[400, "WORKLOG_INVALID"],
[409, "POSTER_ALREADY_EXISTS"],
[422, "WORKLOG_INVALID"],
] as const)("reads a declared %i %s as not applied", (status, code) => {
expect(envelopeFailureEffect(status, { code }, declared)).toBe("NOT_APPLIED");
});
it("keeps every code the operation did not declare maybe applied", () => {
expect(envelopeFailureEffect(400, { code: "BAD_PARAMETER" }, declared)).toBe(
"MAYBE_APPLIED",
);
expect(envelopeFailureEffect(400, { code: "WORKLOG_INVALID" })).toBe("MAYBE_APPLIED");
});
it("never lets a declaration decide an idempotency answer or a table code", () => {
const reckless = [
"IDEMPOTENT_IN_FLIGHT",
"IDEMPOTENCY_UNAVAILABLE",
"IDEMPOTENCY_COMPLETION_UNKNOWN",
"VALIDATION_FAILED",
];
expect(envelopeFailureEffect(409, { code: "IDEMPOTENT_IN_FLIGHT" }, reckless)).toBe(
"MAYBE_APPLIED",
);
expect(
envelopeFailureEffect(503, { code: "IDEMPOTENCY_UNAVAILABLE" }, reckless),
).toBe("MAYBE_APPLIED");
expect(
envelopeFailureEffect(503, { code: "IDEMPOTENCY_COMPLETION_UNKNOWN" }, reckless),
).toBe("MAYBE_APPLIED");
// The table's status rule still holds for its own code.
expect(envelopeFailureEffect(409, { code: "VALIDATION_FAILED" }, reckless)).toBe(
"MAYBE_APPLIED",
);
});
it("does not reach statuses outside 400, 409 and 422", () => {
expect(envelopeFailureEffect(500, { code: "WORKLOG_INVALID" }, declared)).toBeUndefined();
expect(envelopeFailureEffect(503, { code: "REQUEST_TIMEOUT" }, ["REQUEST_TIMEOUT"])).toBe(
"MAYBE_APPLIED",
);
});
});
it("leaves a status the table does not speak for to the package", () => {
expect(envelopeFailureEffect(404, { code: "NOT_FOUND" })).toBeUndefined();
expect(envelopeFailureEffect(500, { code: "INTERNAL_ERROR" })).toBeUndefined();
// Status and code together: these codes under another 5xx are not the
// backend's documented answers.
expect(
envelopeFailureEffect(500, { code: "IDEMPOTENCY_COMPLETION_UNKNOWN" }),
).toBeUndefined();
expect(envelopeFailureEffect(502, { code: "REQUEST_TIMEOUT" })).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",
);
},
);
});