Files
DeepTutor/web/tests/quiz-autoscroll.test.ts
ZQR1101 7edddbec61 fix(web): pin chat to late-mounted quiz cards (#960)
* fix(web): pin chat to late-mounted quiz cards

QuizViewer loads after the 4s post-stream scroll window, so mastery quiz cards stay below the fold until the user scrolls. Watch [data-chat-grow] for 12s without re-pinning unrelated details expands.

Related to #955

* fix(web): extend the existing post-stream window for late viewers

The post-stream effect already existed for exactly this case — its comment
names QuizViewer — so a second, parallel MutationObserver with its own 12s
window meant two observers racing for the same pin, each unaware of the
other's deadline.

Fold the extension into that one effect: the window starts at 4s and only a
*newly appearing* ``[data-chat-grow]`` node pushes it out to 12s. Gating on
appearance rather than presence matters — presence alone is satisfied
permanently by any quiz card from an earlier turn, which would have re-pinned
the user on every later DOM mutation, the precise mis-classification the
short window exists to avoid. The teardown timer follows the current
deadline, so a turn with no late viewer stops observing at 4s as before.

---------

Co-authored-by: ZQR1101 <278034003+ZQR1101@users.noreply.github.com>
Co-authored-by: Frank <bingxizhao@novix.science>
2026-08-24 16:52:00 +08:00

34 lines
1.3 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import path from "node:path";
const quizViewer = readFileSync(
path.resolve(process.cwd(), "components/quiz/QuizViewer.tsx"),
"utf8",
);
const autoScroll = readFileSync(
path.resolve(process.cwd(), "hooks/useChatAutoScroll.ts"),
"utf8",
);
test("QuizViewer marks itself as late-growing chat content", () => {
assert.match(quizViewer, /data-chat-grow="quiz"/);
});
test("the post-stream window extends only for late-mounted viewers", () => {
assert.match(autoScroll, /POST_STREAM_AUTOSCROLL_WINDOW_MS = 4000/);
assert.match(autoScroll, /LATE_VIEWER_AUTOSCROLL_WINDOW_MS = 12_000/);
// The extension must be gated on a newly appearing tagged node, not on one
// merely being present — an old quiz card must not keep re-pinning the user.
assert.match(autoScroll, /\[data-chat-grow\]/);
assert.match(autoScroll, /tagged > growTargets/);
});
test("one post-stream observer owns the pin, not two", () => {
// A second parallel observer would race the first and re-pin post-turn user
// interactions that the short window deliberately excludes.
const observers = autoScroll.match(/new MutationObserver\(/g) ?? [];
assert.equal(observers.length, 2, "expected one streaming + one post-stream");
});