Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
import assert from 'node:assert/strict'
import test from 'node:test'
import {
ACTIVE_QUESTION_TOP_OFFSET_PX,
QUESTION_MINIMAP_TRACK_MAX_PX,
QUESTION_TICK_GAP_PX,
QUESTION_TICK_INSET_PX,
QUESTION_TICK_MOUNTAIN_GAIN,
CURRENT_TICK_SCALE,
activeQuestionId,
answerPreviewText,
collectOutlineMessages,
groupOutlineBounds,
isChatOverflowing,
mapQuestionTicks,
nearestTickId,
offsetFromScrollContent,
questionDisplayText,
questionMinimapTrackHeight,
shouldShowQuestionMinimap,
tickDisplayScale,
tickMountainScale,
viewportBand,
visibleMessageIds,
} from './chatQuestionMinimap.ts'
test('isChatOverflowing is false when content fits or matches the viewport', () => {
assert.equal(isChatOverflowing(800, 800), false)
assert.equal(isChatOverflowing(799, 800), false)
assert.equal(isChatOverflowing(801, 800), true)
})
test('questionMinimapTrackHeight packs ticks with a small gap instead of filling the rail', () => {
assert.equal(questionMinimapTrackHeight(1, 800), QUESTION_TICK_INSET_PX * 2)
assert.equal(questionMinimapTrackHeight(3, 800), 2 * QUESTION_TICK_GAP_PX + 2 * QUESTION_TICK_INSET_PX)
assert.equal(questionMinimapTrackHeight(0, 800), 0)
assert.equal(questionMinimapTrackHeight(3, 0), 0)
})
test('questionMinimapTrackHeight still caps a long cluster to the centered rail', () => {
assert.equal(questionMinimapTrackHeight(100, 800), QUESTION_MINIMAP_TRACK_MAX_PX)
assert.equal(questionMinimapTrackHeight(100, 400), 200)
})
test('shouldShowQuestionMinimap requires overflow, a turn, and room beside the messages', () => {
assert.equal(shouldShowQuestionMinimap(true, 1, 60), true)
assert.equal(shouldShowQuestionMinimap(true, 2, 44), true)
assert.equal(shouldShowQuestionMinimap(true, 0, 60), false)
assert.equal(shouldShowQuestionMinimap(false, 4, 60), false)
assert.equal(shouldShowQuestionMinimap(true, 4, 43), false)
assert.equal(shouldShowQuestionMinimap(true, 4, 0), false)
assert.equal(shouldShowQuestionMinimap(true, 4, -20), false)
})
test('collectOutlineMessages groups each query and its answers into one turn', () => {
const questions = collectOutlineMessages([
{ role: 'assistant', id: 'a1', content: 'hi' },
{ role: 'user', content: 'no id yet' },
{ role: 'user', id: 'u1', content: 'first', images: [{}] },
{ role: 'assistant', id: 'a2', content: 'answer one' },
{ role: 'user', id: 'u2', content: ' ', attachments: [{}] },
])
assert.deepEqual(
questions.map((q) => ({ id: q.id, hasAttachments: q.hasAttachments, answerContent: q.answerContent })),
[
{ id: 'a1', hasAttachments: false, answerContent: '' },
{ id: 'u1', hasAttachments: true, answerContent: 'answer one' },
{ id: 'u2', hasAttachments: true, answerContent: '' },
],
)
assert.deepEqual(questions.map(q => q.role), ['assistant', 'user', 'user'])
assert.deepEqual(questions.map(q => q.messageIds), [['a1'], ['u1', 'a2'], ['u2']])
})
test('all messages intersecting the viewport are highlighted, including partial and long answers', () => {
const items = [
{ id: 'above', offsetTop: 0, offsetBottom: 100 },
{ id: 'user', offsetTop: 90, offsetBottom: 125 },
{ id: 'assistant', offsetTop: 140, offsetBottom: 280 },
{ id: 'next-user', offsetTop: 290, offsetBottom: 330 },
{ id: 'below', offsetTop: 300, offsetBottom: 400 },
{ id: 'hidden', offsetTop: 150, offsetBottom: 150 },
]
assert.deepEqual([...visibleMessageIds(items, 100, 200)], ['user', 'assistant', 'next-user'])
assert.deepEqual([...visibleMessageIds([
{ id: 'long-answer', offsetTop: 0, offsetBottom: 2000 },
], 500, 200)], ['long-answer'])
assert.equal(visibleMessageIds(items, 100, 0).size, 0)
})
test('messages hidden under the sticky composer are not highlighted', () => {
const items = [
{ id: 'query-above', offsetTop: 0, offsetBottom: 80 },
{ id: 'long-answer', offsetTop: 80, offsetBottom: 340 },
{ id: 'query-under-composer', offsetTop: 400, offsetBottom: 440 },
{ id: 'answer-under-composer', offsetTop: 450, offsetBottom: 800 },
]
assert.deepEqual([...visibleMessageIds(items, 100, 500, 200)], ['long-answer'])
assert.deepEqual([...visibleMessageIds(items, 300, 500, 200)],
['long-answer', 'query-under-composer', 'answer-under-composer'])
assert.deepEqual([...visibleMessageIds(items, 450, 500, 200)], ['answer-under-composer'])
assert.equal(visibleMessageIds(items, 100, 200, 250).size, 0)
})
test('answerPreviewText strips markdown and stays empty when there is no answer yet', () => {
assert.equal(
answerPreviewText('## Hello\n**world** and [link](https://x)'),
'Hello world and link',
)
assert.equal(answerPreviewText(' '), '')
assert.equal(answerPreviewText(undefined), '')
})
test('tickMountainScale is 1 without a pointer and peaks at the hovered tick', () => {
assert.equal(tickMountainScale(100, null), 1)
assert.equal(tickMountainScale(100, 100), 1 + QUESTION_TICK_MOUNTAIN_GAIN)
assert.ok(tickMountainScale(100, 100) > tickMountainScale(108, 100))
assert.ok(tickMountainScale(108, 100) > tickMountainScale(200, 100))
})
test('tickDisplayScale lengthens the current tick at rest and follows the pointer when hovering', () => {
assert.equal(tickDisplayScale(100, null, false), 1)
assert.equal(tickDisplayScale(100, null, true), CURRENT_TICK_SCALE)
assert.equal(tickDisplayScale(100, 100, false), 1 + QUESTION_TICK_MOUNTAIN_GAIN)
})
test('nearestTickId picks the closest tick to the pointer', () => {
const ticks = [
{ id: 'u1', yPx: 10 },
{ id: 'u2', yPx: 45 },
{ id: 'u3', yPx: 80 },
]
assert.equal(nearestTickId(ticks, 12), 'u1')
assert.equal(nearestTickId(ticks, 50), 'u2')
assert.equal(nearestTickId([], 10), null)
})
test('questionDisplayText uses trimmed content, else the placeholder', () => {
assert.equal(questionDisplayText(' hello\nworld ', '(附件)'), 'hello world')
assert.equal(questionDisplayText(' ', '(附件)'), '(附件)')
assert.equal(questionDisplayText(undefined, '(附件)'), '(附件)')
})
test('offsetFromScrollContent is viewport-relative plus scrollTop', () => {
assert.equal(offsetFromScrollContent(120, 40, 80), 160)
})
test('mapQuestionTicks clusters ticks in the center with a compact equal gap', () => {
const ticks = mapQuestionTicks(
[
{ id: 'u1' },
{ id: 'u2' },
{ id: 'u3' },
],
200,
)
assert.equal(ticks[1].yPx - ticks[0].yPx, QUESTION_TICK_GAP_PX)
assert.equal(ticks[2].yPx - ticks[1].yPx, QUESTION_TICK_GAP_PX)
assert.equal((ticks[0].yPx + ticks[2].yPx) / 2, 100)
})
test('mapQuestionTicks ignores message offsets and keeps equal gaps', () => {
const ticks = mapQuestionTicks(
[
{ id: 'u1', offsetTop: 0 },
{ id: 'u2', offsetTop: 2 },
],
200,
)
assert.equal(ticks[1].yPx - ticks[0].yPx, QUESTION_TICK_GAP_PX)
assert.equal((ticks[0].yPx + ticks[1].yPx) / 2, 100)
})
test('mapQuestionTicks still fits many ticks inside a short rail', () => {
const ticks = mapQuestionTicks(
[
{ id: 'a' },
{ id: 'b' },
{ id: 'c' },
{ id: 'd' },
],
20,
)
assert.equal(ticks.length, 4)
assert.ok(ticks[0].yPx >= 0)
assert.ok(ticks[3].yPx <= 20)
const gap = ticks[1].yPx - ticks[0].yPx
for (let i = 1; i < ticks.length; i++) {
assert.ok(Math.abs(ticks[i].yPx - ticks[i - 1].yPx - gap) < 1e-9)
}
})
test('viewportBand maps the visible window onto the track with a 16px floor', () => {
const band = viewportBand(250, 250, 1000, 200)
assert.equal(band.topPx, 50)
assert.equal(band.heightPx, 50)
const tiny = viewportBand(0, 10, 1000, 200)
assert.equal(tiny.topPx, 0)
assert.equal(tiny.heightPx, 16)
})
test('activeQuestionId picks the last question at or above the top offset', () => {
const items = [
{ id: 'u1', offsetTop: 0 },
{ id: 'u2', offsetTop: 400 },
{ id: 'u3', offsetTop: 800 },
]
assert.equal(activeQuestionId(items, 0), 'u1')
assert.equal(activeQuestionId(items, 400 - ACTIVE_QUESTION_TOP_OFFSET_PX), 'u2')
assert.equal(activeQuestionId(items, 900), 'u3')
assert.equal(activeQuestionId([], 0), null)
})
test('one turn tick stays highlighted for either query or any assistant segment', () => {
const turns = collectOutlineMessages([
{ role: 'user', id: 'u1', content: 'question' },
{ role: 'assistant', id: 'a1', content: 'first segment' },
{ role: 'tool', id: 'tool' },
{ role: 'assistant', id: 'a2', content: 'continued answer' },
{ role: 'user', id: 'u2', content: 'next question' },
{ role: 'assistant', id: 'a3', content: 'next answer' },
])
assert.deepEqual(turns.map(turn => turn.id), ['u1', 'u2'])
assert.equal(turns[0].answerContent, 'first segment\n\ncontinued answer')
const bounds = groupOutlineBounds(turns, [
{ id: 'u1', offsetTop: 0, offsetBottom: 60 },
{ id: 'a1', offsetTop: 80, offsetBottom: 500 },
{ id: 'a2', offsetTop: 520, offsetBottom: 1000 },
{ id: 'u2', offsetTop: 1020, offsetBottom: 1080 },
{ id: 'a3', offsetTop: 1100, offsetBottom: 1800 },
])
assert.equal(mapQuestionTicks(bounds, 100).length, 2)
for (const top of [0, 100, 600]) {
assert.deepEqual([...visibleMessageIds(bounds, top, 100)], ['u1'])
}
assert.deepEqual([...visibleMessageIds(bounds, 900, 200)], ['u1', 'u2'])
assert.deepEqual([...visibleMessageIds(bounds, 1300, 200)], ['u2'])
assert.equal(visibleMessageIds(bounds, 60, 20).size, 0, 'the gap alone is not visible content')
assert.equal(groupOutlineBounds(turns, []).length, 0, 'unrendered messages do not create ticks')
})