95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import type { WebviewToExtension } from './types'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { DefinitionLinks } from './definitionLinks'
|
|
|
|
function setup(version: number | undefined): { links: DefinitionLinks, sent: WebviewToExtension[], probes: () => { id: number, version: number, offset: number }[] } {
|
|
const sent: WebviewToExtension[] = []
|
|
const links = new DefinitionLinks(msg => sent.push(msg))
|
|
links.setVersion(version)
|
|
const probes = (): { id: number, version: number, offset: number }[] => sent.flatMap(msg => msg.type === 'probeDefinition' ? [{ id: msg.id, version: msg.version, offset: msg.offset }] : [])
|
|
return { links, sent, probes }
|
|
}
|
|
|
|
describe('definitionLinks', () => {
|
|
it('问到「没有」后移开再指回来会重新问,语言扩展启动好了就能链上', () => {
|
|
const { links, probes } = setup(1)
|
|
links.point(54)
|
|
links.answer(probes()[0].id, false)
|
|
expect(links.linked).toBeUndefined()
|
|
|
|
links.point(undefined)
|
|
links.point(54)
|
|
const [first, second] = probes()
|
|
expect(second).toEqual({ id: second.id, version: 1, offset: 54 })
|
|
expect(second.id).not.toBe(first.id)
|
|
|
|
links.answer(second.id, true)
|
|
expect(links.linked).toBe(54)
|
|
})
|
|
|
|
it('修饰键 + 点击名字总交给扩展端去跳,问到过「没有」也一样', () => {
|
|
const { links, sent, probes } = setup(1)
|
|
links.point(54)
|
|
links.answer(probes()[0].id, false)
|
|
|
|
expect(links.click(54)).toBe(true)
|
|
expect(sent.at(-1)).toEqual({ type: 'goToDefinition', version: 1, offset: 54 })
|
|
})
|
|
|
|
it('还指着同一个名字不重问', () => {
|
|
const { links, probes } = setup(1)
|
|
links.point(54)
|
|
links.point(54)
|
|
expect(probes()).toHaveLength(1)
|
|
})
|
|
|
|
it('迟到的旧回答丢掉,只认最近一次询问的回答', () => {
|
|
const { links, probes } = setup(1)
|
|
links.point(54)
|
|
links.point(79)
|
|
links.point(54)
|
|
const [stale, , latest] = probes()
|
|
links.answer(latest.id, true)
|
|
links.answer(stale.id, false)
|
|
expect(links.linked).toBe(54)
|
|
})
|
|
|
|
it('松开修饰键或移开就不链接,之后到的回答也不再挂上', () => {
|
|
const { links, probes } = setup(1)
|
|
links.point(54)
|
|
const [probe] = probes()
|
|
links.answer(probe.id, true)
|
|
expect(links.linked).toBe(54)
|
|
|
|
links.point(undefined)
|
|
expect(links.linked).toBeUndefined()
|
|
links.answer(probe.id, true)
|
|
expect(links.linked).toBeUndefined()
|
|
})
|
|
|
|
it('换了文档版本:正指着的名字按新版本重问,旧版本的回答丢掉', () => {
|
|
const { links, probes } = setup(1)
|
|
links.point(54)
|
|
links.setVersion(2)
|
|
const [old, renewed] = probes()
|
|
expect(renewed).toEqual({ id: renewed.id, version: 2, offset: 54 })
|
|
|
|
links.answer(old.id, true)
|
|
expect(links.linked).toBeUndefined()
|
|
links.answer(renewed.id, true)
|
|
expect(links.linked).toBe(54)
|
|
})
|
|
|
|
it('还没收到文档时不问,点击也不拦', () => {
|
|
const { links, sent } = setup(undefined)
|
|
links.point(54)
|
|
expect(links.click(54)).toBe(false)
|
|
expect(sent).toEqual([])
|
|
})
|
|
|
|
it('没指着名字的点击不拦', () => {
|
|
const { links } = setup(1)
|
|
expect(links.click(undefined)).toBe(false)
|
|
})
|
|
})
|