refactor: fix ai slop

This commit is contained in:
2026-07-31 22:22:33 +07:00 Unverified
parent 8de32bcb3f
commit 255b895112
8 changed files with 214 additions and 74 deletions
+47
View File
@@ -0,0 +1,47 @@
import { describe, expect, test } from "bun:test";
import {
getQuestionResponseSettings,
getQuestionResponseType,
isImageOnlyResponse,
} from "@/lib/question-response-type";
describe("question response types", () => {
test("maps stored question settings to the four editor choices", () => {
expect(getQuestionResponseType({ required: true, imageAnswerMode: "none" }))
.toBe("answer-only");
expect(getQuestionResponseType({ required: true, imageAnswerMode: "optional" }))
.toBe("answer-optional-image");
expect(getQuestionResponseType({ required: false, imageAnswerMode: "required" }))
.toBe("image-only");
expect(getQuestionResponseType({ required: true, imageAnswerMode: "required" }))
.toBe("answer-required-image");
});
test("turns each editor choice into its validation settings", () => {
expect(getQuestionResponseSettings("answer-only", false)).toEqual({
required: false,
imageAnswerMode: "none",
});
expect(getQuestionResponseSettings("answer-only", true)).toEqual({
required: true,
imageAnswerMode: "none",
});
expect(getQuestionResponseSettings("answer-optional-image", false)).toEqual({
required: false,
imageAnswerMode: "optional",
});
expect(getQuestionResponseSettings("image-only")).toEqual({
required: false,
imageAnswerMode: "required",
});
expect(getQuestionResponseSettings("answer-required-image")).toEqual({
required: true,
imageAnswerMode: "required",
});
});
test("recognizes only required-image questions without a required answer as image-only", () => {
expect(isImageOnlyResponse({ required: false, imageAnswerMode: "required" })).toBeTrue();
expect(isImageOnlyResponse({ required: true, imageAnswerMode: "required" })).toBeFalse();
});
});
+44
View File
@@ -0,0 +1,44 @@
import type { ImageAnswerMode } from "@/db/schema/form";
export type QuestionResponseType =
| "answer-only"
| "answer-optional-image"
| "image-only"
| "answer-required-image";
type QuestionResponseSettings = {
required: boolean;
imageAnswerMode: ImageAnswerMode;
};
export function getQuestionResponseType(
question: QuestionResponseSettings,
): QuestionResponseType {
if (question.imageAnswerMode === "required") {
return question.required ? "answer-required-image" : "image-only";
}
return question.imageAnswerMode === "optional"
? "answer-optional-image"
: "answer-only";
}
export function getQuestionResponseSettings(
responseType: QuestionResponseType,
currentRequired = true,
): QuestionResponseSettings {
switch (responseType) {
case "answer-optional-image":
return { required: currentRequired, imageAnswerMode: "optional" };
case "image-only":
return { required: false, imageAnswerMode: "required" };
case "answer-required-image":
return { required: true, imageAnswerMode: "required" };
default:
return { required: currentRequired, imageAnswerMode: "none" };
}
}
export function isImageOnlyResponse(question: QuestionResponseSettings) {
return getQuestionResponseType(question) === "image-only";
}
+43
View File
@@ -0,0 +1,43 @@
import { describe, expect, test } from "bun:test";
import { normalizeSubmissionAnswers } from "@/lib/submission-intake";
const question = {
id: "question-1",
type: "text" as const,
label: "Proof",
options: null,
allowOther: false,
};
describe("image response submission validation", () => {
test("accepts an image-only response without text", () => {
expect(normalizeSubmissionAnswers(
[{ ...question, required: false, imageAnswerMode: "required" }],
[{ questionId: question.id, value: "", imageCdnId: "image-1" }],
)).toEqual([
{
questionId: question.id,
value: "",
imageCdnId: "image-1",
displayValue: "",
},
]);
});
test("requires both parts of an answer + required image response", () => {
expect(() => normalizeSubmissionAnswers(
[{ ...question, required: true, imageAnswerMode: "required" }],
[{ questionId: question.id, value: "", imageCdnId: "image-1" }],
)).toThrow("จำเป็นต้องตอบ");
expect(() => normalizeSubmissionAnswers(
[{ ...question, required: true, imageAnswerMode: "required" }],
[{ questionId: question.id, value: "answer", imageCdnId: null }],
)).toThrow("จำเป็นต้องอัปโหลดรูป");
expect(normalizeSubmissionAnswers(
[{ ...question, required: true, imageAnswerMode: "required" }],
[{ questionId: question.id, value: "answer", imageCdnId: "image-1" }],
)).toHaveLength(1);
});
});