This lands the clue collection line on top of origin/dev (v0.32.1, DSH 0.2.2-rc.2). The 14 commits that carried it are squashed into this one because every one of them overlapped upstream's own changes (version, 19 dictionaries, CHANGELOG), and each intermediate commit would have failed to compile against rc.2's menu API. The pre-integration head is kept at backup/clues-uiux-refresh-pre-rebase. What the feature is: - a Clues tab that reads local files, Feishu tasks/docs and GitHub issues/pulls through one source registry, with a scope row, shared search, filters, paging and stable [[dsh-clue:...]] references; - one window host for detail, pack management, source configuration, new pack and sharing (preview and edit are separate paths: a read is never saved over); - .cluepack export/import with per-row resolvability previews; - a source runtime that owns dependency installs and connection authorization (gh, lark-cli device code) and only probes on an explicit read; - the public betterSidebarClues service and the windows capability (features gains clues / windowHost). Merged with upstream rather than over it: - the settings section keeps upstream's start-page takeover group and this branch's clue sources group; navigation is now six groups; - the dictionaries keep both sides' new keys (1211 each, verified equal across all 19), the external-plugin guide and CHANGELOG keep both histories, and service features list both capability sets. Adapted to 0.2.2-rc.2: - menu call sites use anchorPoint instead of getAnchorRect; - the shared SelectMenu mounts the host menu only while open, because rc.2's Menu reads the DOM during render and broke server-rendered settings surfaces; - the submenu flip workaround stays deleted (the host positions submenus now), pinned by an updated layout-css assertion. Version 0.33.0 (three synced spots) with a new 未发布 / Unreleased changelog section; package.json regains the ./clues export, lib/clues.js and the uqr dependency that upstream never had. Gates: typecheck, lint, 5410 unit tests, build, check:consumer-types and the full mount lane (35 passed) against dsh 0.2.2-rc.2 in Chromium.
173 lines
6.9 KiB
TypeScript
173 lines
6.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { createElement } from 'react'
|
|
import { createRoot } from 'react-dom/client'
|
|
import { act } from 'react-dom/test-utils'
|
|
import { Sidebar } from '../src/client/Sidebar.tsx'
|
|
import { createSidebarStore } from '../src/client/state.ts'
|
|
import { createBetterSidebarService } from '../src/client/service.ts'
|
|
import { setupReactAct } from './test-utils.ts'
|
|
|
|
setupReactAct()
|
|
|
|
afterEach(() => {
|
|
localStorage.clear()
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
function mount(cwd: string, options: { root?: string; phase?: 'pending' | 'ready'; state?: 'idle' | 'loading' | 'error'; chip?: boolean; workspaceService?: boolean } = {}) {
|
|
vi.stubGlobal('WebSocket', class { close() {} })
|
|
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
const store = createSidebarStore()
|
|
store.setSession('s1')
|
|
const service = createBetterSidebarService(store)
|
|
const sessions = { current: 's1', byId: { s1: { id: 's1', cwd } } }
|
|
const locale = { active: 'en' }
|
|
let draft = 'before'
|
|
let draftRev = 1
|
|
const setDraft = vi.fn((text: string) => { draft = text; draftRev++ })
|
|
const emit = vi.fn((_event: string, payload: { text: string }) => {
|
|
if (options.chip) { draft += payload.text; draftRev++ }
|
|
})
|
|
const insertReference = vi.fn((_ref: unknown, _span: unknown) => {
|
|
if (!options.chip) return false
|
|
draftRev++
|
|
return true
|
|
})
|
|
const focus = vi.fn()
|
|
const input = {
|
|
state: { getSnapshot: () => ({ draft, draftRev }) },
|
|
setDraft,
|
|
focus,
|
|
...options.chip === false ? {} : { insertReference },
|
|
}
|
|
const workspaces = { list: { getSnapshot: () => ({
|
|
phase: options.phase ?? 'ready',
|
|
state: options.state ?? 'idle',
|
|
items: [
|
|
{ path: '/decoy', sessionIds: ['other'] },
|
|
...(options.root === undefined ? [] : [{ path: options.root, sessionIds: ['s1'] }]),
|
|
],
|
|
}) } }
|
|
const ctx = {
|
|
locale: { subscribe: () => () => {}, getSnapshot: () => locale },
|
|
sessions: {
|
|
list: { subscribe: () => () => {}, getSnapshot: () => sessions },
|
|
scope: () => ({ emit }),
|
|
},
|
|
get: (name: string) => ({
|
|
betterSidebar: service,
|
|
conversation: { input: { for: () => input } },
|
|
workspaces: options.workspaceService === false ? undefined : workspaces,
|
|
})[name],
|
|
}
|
|
let reference: ((path: string, isDir: boolean) => void) | undefined
|
|
service.registerTab({ id: 'reference-test', title: 'Reference', component: props => {
|
|
reference = props.onReferenceFile
|
|
return null
|
|
} })
|
|
service.openTab({ type: 'reference-test', title: 'Reference' })
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root = createRoot(container)
|
|
act(() => { root.render(createElement(Sidebar, { ctx: ctx as never, store })) })
|
|
expect(reference).toBeTypeOf('function')
|
|
return {
|
|
insert: (path: string, isDir: boolean) => { act(() => { reference!(path, isDir) }) },
|
|
draft: () => draft, emit, focus, insertReference, setDraft, warn,
|
|
unmount: () => { act(() => { root.unmount() }); container.remove() },
|
|
}
|
|
}
|
|
|
|
describe('Sidebar workspace references', () => {
|
|
it.each(['/work', '/work/src'])('uses workspace membership with session cwd %s', cwd => {
|
|
const view = mount(cwd, { root: '/work' })
|
|
try {
|
|
view.insert('/work/my dir', true)
|
|
expect(view.draft()).toBe('before @"my dir/" ')
|
|
expect(view.insertReference).not.toHaveBeenCalled()
|
|
expect(view.setDraft).toHaveBeenCalledTimes(1)
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it('uses workspace-relative file chips outside the session cwd', () => {
|
|
const view = mount('/work/src', { root: '/work', chip: true })
|
|
try {
|
|
view.insert('/work/docs/my notes.md', false)
|
|
// The draft `before` ends in a non-space, so a separator is emitted
|
|
// first: the revision and the append offset both move one place.
|
|
expect(view.insertReference).toHaveBeenCalledWith({
|
|
source: 'reference', ref: '@"docs/my notes.md"', label: 'my notes.md', appearance: 'file', clipboardText: '@"docs/my notes.md"',
|
|
}, { draftRev: 2, start: 7, end: 7 })
|
|
expect(view.setDraft).not.toHaveBeenCalled()
|
|
expect(view.focus).toHaveBeenCalledTimes(1)
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it.each(['src', 'my dir', 'docs/my dir'])('separates a directory %s from the next file chip', directory => {
|
|
const view = mount('/work', { root: '/work', chip: true })
|
|
try {
|
|
view.insert(`/work/${directory}`, true)
|
|
const beforeFile = view.draft()
|
|
expect(beforeFile).toMatch(/\s$/)
|
|
expect(view.focus).toHaveBeenCalledTimes(1)
|
|
view.insert('/work/notes.md', false)
|
|
expect(view.insertReference).toHaveBeenCalledWith({
|
|
source: 'reference', ref: '@notes.md', label: 'notes.md', appearance: 'file', clipboardText: '@notes.md',
|
|
}, { draftRev: 2, start: beforeFile.length, end: beforeFile.length })
|
|
expect(view.setDraft).toHaveBeenCalledTimes(1)
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it('keeps quoted file text when the chip event is not handled', () => {
|
|
const view = mount('/work', { root: '/work' })
|
|
try {
|
|
view.insert('/work/my notes.md', false)
|
|
expect(view.draft()).toBe('before @"my notes.md"')
|
|
expect(view.focus).toHaveBeenCalledTimes(1)
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it('normalizes Windows drive casing and mixed separators', () => {
|
|
const view = mount('C:\\Work\\src', { root: 'C:\\Work\\' })
|
|
try {
|
|
view.insert('c:/WORK/my dir\\nested', true)
|
|
expect(view.draft()).toBe('before @"my dir/nested/" ')
|
|
view.insert('c:/work', true)
|
|
expect(view.draft()).toBe('before @"my dir/nested/" @./ ')
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it.each([
|
|
{ root: undefined },
|
|
{ root: '/work', workspaceService: false },
|
|
{ root: '/work', phase: 'pending' as const },
|
|
{ root: '/work', state: 'loading' as const },
|
|
{ root: '/work', state: 'error' as const },
|
|
])('leaves the draft intact and logs when workspace data is unavailable: %j', options => {
|
|
const view = mount('/work/src', options)
|
|
try {
|
|
view.insert('/work/src/a.ts', false)
|
|
expect(view.draft()).toBe('before')
|
|
expect(view.setDraft).not.toHaveBeenCalled()
|
|
expect(view.warn).toHaveBeenCalled()
|
|
// Resolve the host snapshot later: the next click must see fresh data.
|
|
Object.assign(options, { root: '/work', phase: 'ready', state: 'idle', workspaceService: true })
|
|
view.insert('/work/src', true)
|
|
expect(view.draft()).toBe('before @src/ ')
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it.each(['/outside/file.ts', '/work/a"b', '/work/a\nb'])('never inserts an unsafe path %j as a file or directory', path => {
|
|
const view = mount('/work', { root: '/work' })
|
|
try {
|
|
view.insert(path, false)
|
|
view.insert(path, true)
|
|
expect(view.draft()).toBe('before')
|
|
expect(view.setDraft).not.toHaveBeenCalled()
|
|
expect(view.warn).toHaveBeenCalled()
|
|
} finally { view.unmount() }
|
|
})
|
|
})
|