diff --git a/src/features/tech-log/presentation/studio/studio-document-server-state.ts b/src/features/tech-log/presentation/studio/studio-document-server-state.ts index e21ef8c..95afe4d 100644 --- a/src/features/tech-log/presentation/studio/studio-document-server-state.ts +++ b/src/features/tech-log/presentation/studio/studio-document-server-state.ts @@ -221,7 +221,23 @@ function publishSnapshotQueryDefinition( }); } -function commandMutation( +/* + 명령을 가리키는 이름은 「어느 문서에 대한 무슨 명령인가」다. 본문이 아니다. + + 이 훅을 주지 않으면 플랫폼은 입력 전체를 명령의 신원으로 삼는데, 그 신원은 + `canonicalize` 를 지나고 거기 문자열 하나당 2KB 한도가 있다(`contracts/query-keys.ts`). + 본문이 2KB를 넘는 순간 신원을 만들다 던지고, 명령은 `NOT_STARTED` 로 끝난다 — 요청이 + 나가지 않으니 화면에는 이유 없는 「저장하지 못했습니다.」만 남았다. 갓 만든 문서는 본문이 + 비어 있어 통과하고 글을 쓴 문서만 저장이 안 되는, 원인을 짚기 어려운 모양이 된다. + + 같은 문서의 저장을 한 번에 하나만 돌리는 것이 `REJECT_WHILE_ACTIVE` 가 원하는 바이므로 + 문서 id 만으로 충분하다. +*/ +export function STUDIO_COMMAND_IDENTITY({ documentId }: DocumentMutationInput) { + return Object.freeze({ documentId }); +} + +function commandMutation, Value>( scope: ReturnType, definitionId: string, operationId: string, @@ -239,6 +255,7 @@ function commandMutation( owner: "tech-log", duplicatePolicy: "REJECT_WHILE_ACTIVE", scope, + identity: STUDIO_COMMAND_IDENTITY, invalidate: Object.freeze([]), execute: (input, { signal, intent }) => studioMutationResult(operationId, () => diff --git a/tests/features/tech-log/studio-command-identity.test.ts b/tests/features/tech-log/studio-command-identity.test.ts new file mode 100644 index 0000000..2581544 --- /dev/null +++ b/tests/features/tech-log/studio-command-identity.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from "vitest"; + +import { canonicalize } from "../../../src/contracts/query-keys.ts"; +import { STUDIO_COMMAND_IDENTITY } from "../../../src/features/tech-log/presentation/studio/studio-document-server-state.ts"; + +/* + 명령의 신원은 `canonicalize` 를 지나고, 거기에는 문자열 하나당 2KB 한도가 있다. 입력을 + 그대로 신원으로 쓰면 본문이 2KB를 넘는 문서에서 신원을 만들다 던지고 — 요청은 나가지 않고 + 화면에는 이유 없는 「저장하지 못했습니다.」만 남는다. 갓 만든 문서는 본문이 비어 통과하므로 + 글을 쓴 문서만 저장이 안 되는, 원인을 짚기 어려운 모양이 된다. + + 그래서 두 가지를 같이 본다: 본문을 통째로 넣으면 실제로 던진다는 것과, 우리가 넘기는 + 신원은 던지지 않는다는 것. 앞의 절반이 없으면 이 검사는 한도가 사라져도 통과한다. +*/ +const BODY = "가".repeat(4_000); // 2KB 한도를 넘기는 실제 본문 크기대 +const INPUT = Object.freeze({ + documentId: "8a9ca3d4-7d6e-4f38-862d-296b2640d288", + command: Object.freeze({ + expectedVersion: 2, + document: Object.freeze({ title: "제목", bodyMarkdown: BODY }), + }), +}); + +describe("스튜디오 문서 명령의 신원", () => { + it("입력을 그대로 쓰면 본문 크기에 걸려 명령이 시작조차 못 한다", () => { + expect(() => canonicalize(INPUT)).toThrow(/byte budget/u); + }); + + it("우리가 넘기는 신원은 문서를 가리킬 뿐이라 본문 크기와 무관하다", () => { + expect(() => canonicalize(STUDIO_COMMAND_IDENTITY(INPUT))).not.toThrow(); + expect(STUDIO_COMMAND_IDENTITY(INPUT)).toEqual({ documentId: INPUT.documentId }); + }); +});