Files
apes-sidebar/tests/quote-flash.spec.ts
T
xiangzz 805a03f91f feat(sidebar): 划选「添加到对话」落可点击引用 chip(点击回跳源文件并闪行段)
- 移植到 dev 基线(#1052 / #1002 之上):只加「可点击 + 行段落地」,不重做 chip 本体
- 自有 input-trigger source `betterSidebarQuote`:`openReference` 校验会话后回跳,
  `codec.serialize` 在发送时还原成与升级前字节一致的 `路径:起止` + 围栏正文
- 行段末行走模块级一次性旁路 `src/client/line-target.ts`(key = path + line,cap 32),
  不写进宿主导航参数(宿主 `SidebarRightResourceParamsMap.file` 只声明了 `line`)
- 引用记录存 `localStorage`(`dsh-better-sidebar:quote-refs`,FIFO 200):模块同时进
  core bundle 与 lazy editor chunk,两个实例只能靠它互见;插入失败即回滚记录
- 行段闪光用 CodeMirror 行装饰 + 皮肤令牌 `--dsw-alias-interactive-bg-hover-accent`
- 三级降级:自有 chip → #1052 内置 chip → 纯文本,永不比升级前更差
- 新增 peer `@deepseek-ai/dsh-client-ui-input-trigger` `^0.2.1-rc.1`(DSH peer 增至 14 条)
- 文档:指南新增 §7.2,并补 `openFile` options / `FileViewerProps.lineEnd` / peer 说明;
  AGENTS §3、CHANGELOG 双语、README 双语、`docs/plans/2026-11-03-selection-quote-chip.md`
- 测试:`selection-quote` 23、`quote-flash` 13、`line-target` 7,`conversation-draft` +2
2026-11-04 01:47:15 +08:00

174 lines
6.3 KiB
TypeScript

// @vitest-environment jsdom
/**
* Unit tests for the quote-chip landing flash (src/client/quote-flash.ts):
* the line-range clamp, the CodeMirror state field that carries the flash
* across document edits, the decorations it renders, the one-shot style
* injection (skin-token based, never a hardcoded colour), and the
* self-clearing entry point the viewer calls on landing.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { EditorState } from '@codemirror/state'
import { Decoration, EditorView } from '@codemirror/view'
import type { RangeSet } from '@codemirror/state'
import {
QUOTE_FLASH_MS,
QUOTE_FLASH_STYLE_ID,
clampQuoteRange,
ensureQuoteFlashStyle,
flashQuoteLines,
quoteFlashEffect,
quoteFlashField,
quoteFlashHighlight,
} from '../src/client/quote-flash.ts'
const DOC = 'l1\nl2\nl3\nl4\nl5'
const flashedState = (doc = DOC): ReturnType<EditorState['update']>['state'] => {
const state = EditorState.create({ doc, extensions: [quoteFlashField, quoteFlashHighlight] })
const from = state.doc.line(2).from
const to = state.doc.line(3).to
return state.update({ effects: quoteFlashEffect.of({ from, to }) }).state
}
/** The literal line classes the flash facet currently renders, in order. */
function flashClasses(state: EditorState): string[] {
const sets = state.facet(EditorView.decorations) as unknown as RangeSet<Decoration>[]
const classes: string[] = []
for (const set of sets) {
const cursor = set.iter()
while (cursor.value !== null) {
const kind = cursor.value.spec as { class?: string }
if (kind.class === 'cm-quote-flash') classes.push(kind.class)
cursor.next()
}
}
return classes
}
describe('clampQuoteRange', () => {
it('passes a range that lies inside the document through', () => {
expect(clampQuoteRange(100, 5, 9)).toEqual({ first: 5, last: 9 })
})
it('defaults the end to the start line', () => {
expect(clampQuoteRange(100, 7, undefined)).toEqual({ first: 7, last: 7 })
})
it('clamps an end above the start back down to it', () => {
expect(clampQuoteRange(100, 5, 2)).toEqual({ first: 5, last: 5 })
})
it('clamps an end beyond the document to its last line', () => {
expect(clampQuoteRange(10, 8, 99)).toEqual({ first: 8, last: 10 })
})
it('refuses a start outside the document', () => {
expect(clampQuoteRange(10, 0, 3)).toBeUndefined()
expect(clampQuoteRange(10, 11, 12)).toBeUndefined()
expect(clampQuoteRange(10, Number.NaN, undefined)).toBeUndefined()
})
})
describe('quoteFlashField', () => {
it('carries the flash range and clears on an undefined effect', () => {
const flashed = flashedState()
expect(flashClasses(flashed)).toEqual(['cm-quote-flash', 'cm-quote-flash'])
const cleared = flashed.update({ effects: quoteFlashEffect.of(undefined) }).state
expect(flashClasses(cleared)).toEqual([])
})
it('maps the range through document edits (an inserted line shifts it down)', () => {
const flashed = flashedState()
const edited = flashed.update({ changes: { insert: 'new\n', from: 0 } }).state
expect(flashClasses(edited)).toEqual(['cm-quote-flash', 'cm-quote-flash'])
// The decorations moved from lines 2-3 to lines 3-4 (positions mapped).
expect(edited.field(quoteFlashField)).toEqual({
from: edited.doc.line(3).from,
to: edited.doc.line(4).to,
})
})
it('starts inert: no flash, no decorations', () => {
const state = EditorState.create({ doc: DOC, extensions: [quoteFlashField, quoteFlashHighlight] })
expect(state.field(quoteFlashField)).toBeNull()
expect(flashClasses(state)).toEqual([])
})
})
describe('ensureQuoteFlashStyle', () => {
beforeEach(() => {
document.getElementById(QUOTE_FLASH_STYLE_ID)?.remove()
})
it('injects the stylesheet once and is idempotent', () => {
expect(document.getElementById(QUOTE_FLASH_STYLE_ID)).toBeNull()
ensureQuoteFlashStyle()
const first = document.getElementById(QUOTE_FLASH_STYLE_ID)
expect(first).not.toBeNull()
expect(first?.textContent).toContain('.cm-quote-flash')
ensureQuoteFlashStyle()
expect(document.querySelectorAll(`#${QUOTE_FLASH_STYLE_ID}`)).toHaveLength(1)
})
it('paints from the skin token, never a literal colour', () => {
ensureQuoteFlashStyle()
const css = document.getElementById(QUOTE_FLASH_STYLE_ID)?.textContent ?? ''
// The sidebar's own hover/active fill, mixed toward transparent so the
// text under it stays readable in every scheme (AGENTS §5: no hardcoded
// colours).
expect(css).toContain('var(--dsw-alias-interactive-bg-hover-accent)')
expect(css).not.toMatch(/#[0-9a-f]{3,8}\b/iu)
expect(css).not.toMatch(/rgba?\(/u)
})
})
describe('flashQuoteLines', () => {
afterEach(() => {
vi.useRealTimers()
})
it('flashes the span, then clears itself after the hold', () => {
vi.useFakeTimers()
const parent = document.createElement('div')
document.body.appendChild(parent)
const view = new EditorView({ parent, state: EditorState.create({ doc: DOC, extensions: [quoteFlashField, quoteFlashHighlight] }) })
flashQuoteLines(view, 2, 3)
expect(flashClasses(view.state)).toEqual(['cm-quote-flash', 'cm-quote-flash'])
vi.advanceTimersByTime(QUOTE_FLASH_MS + 1)
expect(flashClasses(view.state)).toEqual([])
view.destroy()
parent.remove()
})
it('a second landing replaces the first one instead of stacking', () => {
vi.useFakeTimers()
const parent = document.createElement('div')
document.body.appendChild(parent)
const view = new EditorView({ parent, state: EditorState.create({ doc: DOC, extensions: [quoteFlashField, quoteFlashHighlight] }) })
flashQuoteLines(view, 1, 1)
flashQuoteLines(view, 4, 5)
const ranges = view.state.field(quoteFlashField)
expect(ranges).toEqual({ from: view.state.doc.line(4).from, to: view.state.doc.line(5).to })
vi.advanceTimersByTime(QUOTE_FLASH_MS + 1)
expect(flashClasses(view.state)).toEqual([])
view.destroy()
parent.remove()
})
it('does nothing when the view is already torn down', () => {
const parent = document.createElement('div')
document.body.appendChild(parent)
const view = new EditorView({ parent, state: EditorState.create({ doc: DOC, extensions: [quoteFlashField, quoteFlashHighlight] }) })
view.destroy()
expect(() => { flashQuoteLines(view, 1, 2) }).not.toThrow()
parent.remove()
})
})