문제: 짝 백엔드 답 표 JSON(backend-idempotency-answers.json)이 pin한
백엔드 http-contract.md와 같은지 아무 검사도 보지 않는다.
backend-contract-pin.test.ts는 pin과 답 표 두 파일만 읽고, drift
스크립트는 pin과 HEAD 사이에 문서 글자가 바뀌었는지만 본다. 그래서
답 표 status 오타나 옮기지 않은 행(§4 RATE_LIMIT_*)은 어디서도 걸리지
않는다. drift 스크립트는 옆 checkout이 없으면 "skipped"로 rc=0이고
(worktree에서 늘 그렇다), 매뉴얼에는 스크립트가 없다.
고른 방법: 게이트가 백엔드 checkout 없이 볼 수 있게, 백엔드 §2·§4 표의
`|` 줄을 pin 커밋 그대로 config/contracts/backend-http-contract-excerpt.json
에 옮겼다. 읽는 함수는 scripts/lib/backend-contract-excerpt.ts 하나다.
- test:unit(backend-contract-pin.test.ts): 답 표의 행마다 사본의 같은 절에
같은 code·status 행이 있는지, 사본의 행마다 답 표에 있거나 pin의
`notCopied`에 이유와 함께 있는지 본다. `notCopied`에는 RATE_LIMIT_* 둘을
적었다(리뷰가 지적한 sectionsCompared §4의 실제 범위).
- check:backend-contract-drift(게이트 밖): pin 커밋의 문서에서 같은 표를
읽어 사본과 글자 단위로 비교하고, 답 표도 그 표와 비교하고, pin 뒤
HEAD에서 바뀐 사본 표 행을 찍는다. checkout이 없거나 pin 커밋·문서가
없으면 rc=2("not compared")로 끝난다. 기본 경로는 main checkout 옆이라
linked worktree에서도 같은 백엔드를 찾는다. `--write-excerpt`는 pin을
올릴 때 사본을 pin 커밋에서 다시 옮긴다.
- 매뉴얼 01 Envelope 단계와 계약 문서 4.1에 언제 무엇을 돌리는지 적었다.
버린 대안: 백엔드 문서의 digest만 pin에 두는 것. 게이트는 digest로 답
표를 확인할 수 없다. 계약 패키지(리뷰 안 D)는 배포 경로가 아직 없다.
위험·한계: 사본과 백엔드 문서의 일치는 여전히 백엔드 checkout이 있을
때만 drift 스크립트가 본다. 게이트는 답 표와 사본의 일치만 본다. 사본을
손으로 같이 고치면 게이트는 통과한다(drift 스크립트가 잡는다). 답 표의
refusedBeforeWrite·sameKeyResend는 백엔드 문장(클라이언트 행동)을 사람이
읽은 값이라 기계로 대조하지 않는다. 그 문장이 바뀌면 사본 diff와 drift
출력에 보인다. 백엔드 표 형식(머리글 `error.code`·`코드`·`status`)이
바뀌면 파서도 고친다. pin의 comparedOn을 사본을 옮긴 날로 올렸다.
검증: desktop batch에서 확인.
node scripts/run-vitest.ts run tests/unit/backend-contract-pin.test.ts tests/unit/backend-contract-drift.test.ts
drift 테스트는 가짜 백엔드 git repo로 D6g(pin 뒤 status 변경), 답 표
status 오타, 사본 오타, 옮기지도 적지도 않은 행, checkout 없음,
--write-excerpt를 돈다.
Refs: 1105/07-01
210 lines
7.1 KiB
TypeScript
210 lines
7.1 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
import { z } from "zod";
|
|
|
|
import {
|
|
answerTableProblems,
|
|
excerptAnswerRows,
|
|
} from "../../scripts/lib/backend-contract-excerpt.ts";
|
|
import {
|
|
envelopeFailureEffect,
|
|
invitesKeyedResend,
|
|
maybeAppliedAnswerRows,
|
|
refusedBeforeWriteRows,
|
|
} from "../../src/adapters/http/backend-envelope.ts";
|
|
|
|
/**
|
|
* 1103/12-06. The paired backend's contract is pinned in a file a machine
|
|
* reads, and its answer table (http-contract.md §2, with §4's async timeout)
|
|
* is copied next to the pin. This test holds the frontend's Envelope table to
|
|
* that copy, in both directions. When the backend adds or changes an answer,
|
|
* the copy changes with the pin and this test says which frontend verdict has
|
|
* to follow.
|
|
*
|
|
* 1105/07-01. The answer table is also held to the backend's own table rows,
|
|
* copied word for word at the pin (`tables.backendExcerpt`), so a mistyped
|
|
* status or a row left out fails here without a backend checkout.
|
|
* `check:backend-contract-drift` holds that copy to the backend document.
|
|
*/
|
|
const COMMIT = /^[0-9a-f]{40}$/u;
|
|
|
|
const pinSchema = z
|
|
.object({
|
|
schemaVersion: z.literal(1),
|
|
backend: z
|
|
.object({
|
|
repository: z.string().min(1),
|
|
commit: z.string().regex(COMMIT),
|
|
document: z.string().min(1),
|
|
sectionsCompared: z.array(z.string().min(1)).min(1),
|
|
})
|
|
.strict(),
|
|
frontend: z
|
|
.object({
|
|
contractDocument: z.string().min(1),
|
|
contractSections: z.array(z.string().min(1)).min(1),
|
|
readers: z.array(z.string().min(1)).min(1),
|
|
})
|
|
.strict(),
|
|
tables: z
|
|
.object({
|
|
idempotencyAnswers: z.string().min(1),
|
|
backendExcerpt: z.string().min(1),
|
|
})
|
|
.strict(),
|
|
// Rows of the compared sections the answer table leaves out, each with why.
|
|
notCopied: z.array(
|
|
z
|
|
.object({
|
|
section: z.string().min(1),
|
|
code: z.string().regex(/^[A-Z0-9_]{1,64}$/u),
|
|
reason: z.string().min(1),
|
|
})
|
|
.strict(),
|
|
),
|
|
owner: z.string().min(1),
|
|
comparedOn: z.string().regex(/^\d{4}-\d{2}-\d{2}$/u),
|
|
})
|
|
.strict();
|
|
|
|
const answersSchema = z
|
|
.object({
|
|
schemaVersion: z.literal(1),
|
|
source: z
|
|
.object({
|
|
repository: z.string().min(1),
|
|
commit: z.string().regex(COMMIT),
|
|
document: z.string().min(1),
|
|
})
|
|
.strict(),
|
|
answers: z
|
|
.array(
|
|
z
|
|
.object({
|
|
section: z.string().min(1),
|
|
code: z.string().regex(/^[A-Z0-9_]{1,64}$/u),
|
|
status: z.number().int().min(400).max(599),
|
|
// The backend says the command did not run and will not.
|
|
refusedBeforeWrite: z.boolean(),
|
|
// The backend asks for the same request with the same key.
|
|
sameKeyResend: z.boolean(),
|
|
})
|
|
.strict(),
|
|
)
|
|
.min(1),
|
|
})
|
|
.strict();
|
|
|
|
const pin = pinSchema.parse(
|
|
JSON.parse(readFileSync("config/contracts/backend-contract-pin.json", "utf8")),
|
|
);
|
|
const table = answersSchema.parse(
|
|
JSON.parse(readFileSync(pin.tables.idempotencyAnswers, "utf8")),
|
|
);
|
|
|
|
const excerptSchema = z
|
|
.object({
|
|
schemaVersion: z.literal(1),
|
|
source: z
|
|
.object({
|
|
repository: z.string().min(1),
|
|
commit: z.string().regex(COMMIT),
|
|
document: z.string().min(1),
|
|
})
|
|
.strict(),
|
|
// Section number → its tables, each the `|` lines as the backend wrote them.
|
|
sections: z.record(
|
|
z.string().regex(/^\d+$/u),
|
|
z.array(z.array(z.string().startsWith("|")).min(2)).min(1),
|
|
),
|
|
})
|
|
.strict();
|
|
const excerpt = excerptSchema.parse(
|
|
JSON.parse(readFileSync(pin.tables.backendExcerpt, "utf8")),
|
|
);
|
|
|
|
/**
|
|
* The refusal-table codes that come from the frontend's own reading of the
|
|
* backend code (contract 6.1), not from the backend's answer table.
|
|
*/
|
|
const FRONTEND_CODE_READINGS: readonly Readonly<{ code: string; status: number }>[] = [
|
|
{ code: "VALIDATION_FAILED", status: 400 },
|
|
];
|
|
|
|
describe("paired backend contract pin", () => {
|
|
it("names one backend commit for the pin and the copied table", () => {
|
|
expect(table.source).toEqual({
|
|
repository: pin.backend.repository,
|
|
commit: pin.backend.commit,
|
|
document: pin.backend.document,
|
|
});
|
|
expect(excerpt.source).toEqual(table.source);
|
|
expect(pin.frontend.readers).toContain("src/adapters/http/backend-envelope.ts");
|
|
});
|
|
|
|
// 1105/07-01. Each answer is a row of the backend table it names, with the
|
|
// same code and status, and each backend row is either copied or listed in
|
|
// `notCopied` with a reason. The excerpt covers the sections the answers
|
|
// come from, and the parser finds rows in each.
|
|
it("copies the answers from the backend table rows at the pin", () => {
|
|
const sections = [...new Set(table.answers.map((answer) => answer.section))].sort();
|
|
expect(Object.keys(excerpt.sections).sort()).toEqual(sections);
|
|
expect(sections.every((section) => pin.backend.sectionsCompared.includes(section))).toBe(
|
|
true,
|
|
);
|
|
const rows = excerptAnswerRows(excerpt.sections);
|
|
for (const section of sections) {
|
|
expect(
|
|
rows.some((row) => row.section === section),
|
|
`no answer row read from §${section}`,
|
|
).toBe(true);
|
|
}
|
|
expect(answerTableProblems(table.answers, pin.notCopied, rows)).toEqual([]);
|
|
});
|
|
|
|
it("copies each backend answer once", () => {
|
|
const keys = table.answers.map((answer) => `${answer.status} ${answer.code}`);
|
|
expect(new Set(keys).size).toBe(keys.length);
|
|
});
|
|
|
|
// What the frontend does with each backend answer, from the two facts the
|
|
// backend states. A refusal the backend asks to resend with the same key
|
|
// stays unknown, because only an unknown result keeps the key.
|
|
it.each(table.answers.map((answer) => [`${answer.status} ${answer.code}`, answer] as const))(
|
|
"reads the backend's %s as its table says",
|
|
(_label, answer) => {
|
|
const error = { code: answer.code };
|
|
const expected =
|
|
answer.refusedBeforeWrite && !answer.sameKeyResend
|
|
? "NOT_APPLIED"
|
|
: "MAYBE_APPLIED";
|
|
expect(envelopeFailureEffect(answer.status, error)).toBe(expected);
|
|
expect(invitesKeyedResend(answer.status, error)).toBe(
|
|
answer.refusedBeforeWrite && answer.sameKeyResend,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("backs every frontend table row with the backend table or the 6.1 reading", () => {
|
|
const backed = (row: Readonly<{ code: string; status: number }>) =>
|
|
table.answers.some(
|
|
(answer) => answer.code === row.code && answer.status === row.status,
|
|
);
|
|
for (const row of refusedBeforeWriteRows()) {
|
|
const reading = FRONTEND_CODE_READINGS.some(
|
|
(candidate) => candidate.code === row.code && candidate.status === row.status,
|
|
);
|
|
expect(reading || backed(row), `${row.status} ${row.code}`).toBe(true);
|
|
if (!reading) {
|
|
expect(
|
|
table.answers.find((answer) => answer.code === row.code)?.refusedBeforeWrite,
|
|
).toBe(true);
|
|
}
|
|
}
|
|
for (const row of maybeAppliedAnswerRows()) {
|
|
expect(backed(row), `${row.status} ${row.code}`).toBe(true);
|
|
}
|
|
});
|
|
});
|