diff --git a/web/components/quiz/QuizVerdictSummary.tsx b/web/components/quiz/QuizVerdictSummary.tsx index 5bfb8f6c5..a5431aa4f 100644 --- a/web/components/quiz/QuizVerdictSummary.tsx +++ b/web/components/quiz/QuizVerdictSummary.tsx @@ -32,29 +32,34 @@ const REASON_LABELS: Record = { export default function QuizVerdictSummary({ verdict, + showResult = true, }: { verdict: QuizVerdict; + /** False when exact matching, not the judge, decided the grade. */ + showResult?: boolean; }) { const { t } = useTranslation(); const hasPoints = verdict.awarded_points !== undefined && verdict.max_points !== undefined; return (
-
- - {t(RESULT_LABELS[verdict.result])} - - {hasPoints && ( - - {t("{{awarded}} / {{max}} points", { - awarded: verdict.awarded_points, - max: verdict.max_points, - })} + {showResult && ( +
+ + {t(RESULT_LABELS[verdict.result])} - )} -
+ {hasPoints && ( + + {t("{{awarded}} / {{max}} points", { + awarded: verdict.awarded_points, + max: verdict.max_points, + })} + + )} +
+ )} {verdict.reason && (
{t(REASON_LABELS[verdict.reason])} diff --git a/web/components/quiz/QuizViewer.tsx b/web/components/quiz/QuizViewer.tsx index a316f52ec..c39506853 100644 --- a/web/components/quiz/QuizViewer.tsx +++ b/web/components/quiz/QuizViewer.tsx @@ -453,6 +453,9 @@ export default function QuizViewer({ ? getAssessment(q, ans, judgments[idx]?.verdict).result : undefined; + // Reported once per set of answers, with the submit-time grade: each post + // adds a ``[Quiz Performance]`` chat message, so a verdict that arrives + // later is stored through the entry PATCH only (the server keeps it). const submittedResults = useMemo( () => questions.flatMap((question, questionIdx) => { @@ -468,15 +471,11 @@ export default function QuizViewer({ correct_answer: question.correct_answer, explanation: question.explanation ?? "", difficulty: question.difficulty ?? "", - ...getAssessment( - question, - answer, - judgments[questionIdx]?.verdict, - ), + ...getAssessment(question, answer, null), }, ]; }), - [answers, judgments, questions], + [answers, questions], ); useEffect(() => { @@ -1417,7 +1416,11 @@ export default function QuizViewer({ )}
{judgment.verdict && !judgment.isStreaming && ( - + )} {judgment.error ? (
diff --git a/web/lib/quiz-verdict.ts b/web/lib/quiz-verdict.ts index 789c8064d..b5b26d38f 100644 --- a/web/lib/quiz-verdict.ts +++ b/web/lib/quiz-verdict.ts @@ -42,12 +42,20 @@ function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } -/** Validate a verdict from the wire or the notebook; ``null`` if unusable. */ +/** + * Check the shape of a verdict from the wire or the notebook; ``null`` if + * unusable. The server owns ``final``/``is_correct``/points (and the entry + * PATCH rejects inconsistent verdicts), so they are not re-derived here. + */ export function parseQuizVerdict(value: unknown): QuizVerdict | null { - if (!isRecord(value) || typeof value.result !== "string") return null; - if (!RESULTS.has(value.result)) return null; - const result = value.result as QuizVerdictResult; - const final = value.final === true && result !== "ungraded"; + if ( + !isRecord(value) || + typeof value.result !== "string" || + !RESULTS.has(value.result) || + typeof value.final !== "boolean" + ) { + return null; + } const steps = Array.isArray(value.steps) ? value.steps.flatMap((step) => isRecord(step) && @@ -64,15 +72,9 @@ export function parseQuizVerdict(value: unknown): QuizVerdict | null { ) : []; const verdict: QuizVerdict = { - result: final ? result : "ungraded", - // Same mapping as ``Verdict.receipt``: partial is neither right nor wrong. - is_correct: - final && result === "correct" - ? true - : final && result === "incorrect" - ? false - : null, - final, + result: value.result as QuizVerdictResult, + is_correct: typeof value.is_correct === "boolean" ? value.is_correct : null, + final: value.final, confidence: typeof value.confidence === "number" ? value.confidence : 0, steps, }; @@ -80,12 +82,8 @@ export function parseQuizVerdict(value: unknown): QuizVerdict | null { verdict.feedback = value.feedback; } if ( - final && typeof value.awarded_points === "number" && - typeof value.max_points === "number" && - value.max_points > 0 && - value.awarded_points >= 0 && - value.awarded_points <= value.max_points + typeof value.max_points === "number" ) { verdict.awarded_points = value.awarded_points; verdict.max_points = value.max_points; diff --git a/web/tests/quiz/QuizViewer.smoke.spec.tsx b/web/tests/quiz/QuizViewer.smoke.spec.tsx index 5b30f6bdb..fcc2b04da 100644 --- a/web/tests/quiz/QuizViewer.smoke.spec.tsx +++ b/web/tests/quiz/QuizViewer.smoke.spec.tsx @@ -397,13 +397,40 @@ describe("QuizViewer smoke", () => { }); expect(screen.getByTestId("quiz-verdict")).toHaveTextContent("Correct"); expect(screen.getByText("Light reaction")).toBeInTheDocument(); - // The single question is complete: results carry the judged result. - await waitFor(() => { - const calls = mocks.recordQuizResults.mock.calls; - expect(calls.at(-1)?.[1]).toEqual([ - expect.objectContaining({ is_correct: true, result: "correct" }), - ]); + // Results were reported once, when the quiz was complete. The verdict is + // stored through the entry PATCH; it must not post another + // ``[Quiz Performance]`` chat message. + expect(mocks.recordQuizResults).toHaveBeenCalledTimes(1); + expect(mocks.recordQuizResults.mock.calls[0][1]).toEqual([ + expect.objectContaining({ is_correct: false, result: "ungraded" }), + ]); + }); + + it("shows one result badge when exact matching and the judge disagree", async () => { + const user = userEvent.setup(); + renderViewer([choiceQuestion]); + + await user.click(screen.getByRole("button", { name: /Paris/ })); + await user.click(screen.getByRole("button", { name: /Check Answer/ })); + await user.click(screen.getByRole("button", { name: /AI Judge/ })); + act(() => { + mocks.judgeHandlers!.onVerdict!({ + result: "incorrect", + is_correct: false, + final: true, + confidence: 0.9, + steps: [{ label: "Capital", ok: false, comment: "Unclear." }], + }); + mocks.judgeHandlers!.onDone("Unclear."); }); + + // Exact matching accepted the answer, so its badge stays authoritative; + // the verdict keeps its step feedback but no contradicting badge. + await waitFor(() => { + expect(screen.getByText("Capital")).toBeInTheDocument(); + }); + expect(screen.getAllByText("Correct")).toHaveLength(1); + expect(screen.queryByText("Incorrect")).toBeNull(); }); it("closes in-flight judge handles on unmount", async () => { diff --git a/web/tests/quiz/quiz-verdict.spec.tsx b/web/tests/quiz/quiz-verdict.spec.tsx index 27a4b38ef..68e4d22d6 100644 --- a/web/tests/quiz/quiz-verdict.spec.tsx +++ b/web/tests/quiz/quiz-verdict.spec.tsx @@ -35,20 +35,17 @@ describe("parseQuizVerdict", () => { expect(parseQuizVerdict(null)).toBeNull(); }); - it("never trusts a non-final verdict as graded or its points", () => { - const verdict = parseQuizVerdict({ ...judged, result: "correct", final: false }); - expect(verdict).toMatchObject({ result: "ungraded", is_correct: null }); - expect(verdict?.awarded_points).toBeUndefined(); + it("requires a known result and a boolean final", () => { + expect(parseQuizVerdict({ result: "correct" })).toBeNull(); + expect(parseQuizVerdict({ result: "correct", final: "yes" })).toBeNull(); }); - it("drops malformed steps and inconsistent points", () => { + it("drops malformed steps", () => { const verdict = parseQuizVerdict({ ...judged, - steps: [{ label: "ok", ok: true }, { label: 3 }, "x"], - awarded_points: 9, + steps: [{ label: "ok", ok: true }, { label: 3 }, { label: "x", ok: null }, "x"], }); expect(verdict?.steps).toEqual([{ label: "ok", ok: true, comment: "" }]); - expect(verdict?.max_points).toBeUndefined(); }); }); @@ -83,6 +80,13 @@ describe("resolveQuizAssessment", () => { ).toEqual({ is_correct: true, result: "correct" }); }); + it("ignores a verdict that is not final", () => { + const unsure = { ...judged, result: "correct" as const, final: false }; + expect(resolveQuizAssessment({ ...base, verdict: unsure })).toEqual({ + is_correct: false, + }); + }); + it("keeps photo answers ungraded until a final verdict exists", () => { expect( resolveQuizAssessment({ ...base, hasImages: true, verdict: null }), @@ -105,6 +109,13 @@ describe("resolveQuizAssessment", () => { }); describe("QuizVerdictSummary", () => { + it("hides the result badge and points when exact matching decided", () => { + render(); + expect(screen.queryByText("Partially correct")).toBeNull(); + expect(screen.queryByText("3 / 5 points")).toBeNull(); + expect(screen.getByText("Minority interest")).toBeInTheDocument(); + }); + it("renders badge, points and step feedback", () => { render(); expect(screen.getByText("Partially correct")).toBeInTheDocument();