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.
288 lines
11 KiB
TypeScript
288 lines
11 KiB
TypeScript
/**
|
|
* Interactive tests for the settings popup's text/number/select rows: the
|
|
* draft is local state committed on blur through the parent's onCommit; the
|
|
* parent's canonical return is adopted (clamped numbers, stored value for
|
|
* invalid input), and the row remounts when the committed pref value changes
|
|
* (a failed commit reverts prefs → the stored value reappears). Select rows
|
|
* commit the picked option's value (single) or the picked values in options
|
|
* order (multi) through onSelectValue.
|
|
*
|
|
* Rendered with createRoot + act() in jsdom (the SSR specs stay in
|
|
* side-card-section.spec.tsx; this file exercises the event paths).
|
|
*/
|
|
// @vitest-environment jsdom
|
|
import { describe, expect, it } from 'vitest'
|
|
import { createElement } from 'react'
|
|
import { act } from 'react-dom/test-utils'
|
|
import { renderRoot, setupReactAct } from './test-utils.ts'
|
|
|
|
// The act() environment flag (React 18.2 reads it before flushing effects).
|
|
setupReactAct()
|
|
import type { SidebarSettingToggle } from '../src/client/service.ts'
|
|
import { FeatureSettingsRows } from '../src/client/SideCardSection.tsx'
|
|
import { SIDEBAR_PREFS_DEFAULTS } from '../src/prefs-shared.ts'
|
|
|
|
/** Type into an input and commit it via blur (React 18: input event +
|
|
* focusout). The native setter bypasses React's value tracker so the
|
|
* change is actually seen. */
|
|
function typeAndBlur(input: HTMLInputElement, value: string): void {
|
|
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!
|
|
act(() => {
|
|
setter.call(input, value)
|
|
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
input.dispatchEvent(new FocusEvent('focusout', { bubbles: true }))
|
|
})
|
|
}
|
|
|
|
const prefs = { ...SIDEBAR_PREFS_DEFAULTS }
|
|
/** The row renderer reads `prefs[toggle.key]`, so a plugin-shaped fixture key
|
|
* needs a widened copy (a fresh literal with an undeclared key fails
|
|
* excess-property checking). */
|
|
const withPluginKey = (key: string, value: unknown): typeof prefs => ({ ...prefs, [key]: value })
|
|
|
|
describe('FeatureSettingsRows typed rows (interactive)', () => {
|
|
it('commits the raw text on blur and adopts the canonical return', () => {
|
|
const commits: Array<[string, string]> = []
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'myPluginFontFamily',
|
|
type: 'text',
|
|
title: () => 'Font family',
|
|
}
|
|
const { container, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs,
|
|
onToggle: () => {},
|
|
onCommit: (t, raw) => {
|
|
commits.push([t.key, raw])
|
|
return raw
|
|
},
|
|
}))
|
|
const input = container.querySelector('input')!
|
|
typeAndBlur(input, 'Monaco')
|
|
expect(commits).toEqual([['myPluginFontFamily', 'Monaco']])
|
|
// The canonical return is adopted into the draft.
|
|
expect(input.value).toBe('Monaco')
|
|
unmount()
|
|
})
|
|
|
|
it('clamps numbers into the declared bounds on commit', () => {
|
|
const commits: Array<[string, number]> = []
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'myPluginRowPx',
|
|
type: 'number',
|
|
title: () => 'Row height',
|
|
min: 0,
|
|
max: 120,
|
|
}
|
|
const { container, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs: withPluginKey('myPluginRowPx', 40),
|
|
onToggle: () => {},
|
|
onCommit: (t, raw) => {
|
|
const parsed = Number(raw)
|
|
const clamped = Math.min(120, Math.max(0, Math.round(parsed)))
|
|
commits.push([t.key, clamped])
|
|
return String(clamped)
|
|
},
|
|
}))
|
|
const input = container.querySelector('input')!
|
|
// Above the declared max: the commit clamps to 120.
|
|
typeAndBlur(input, '130')
|
|
expect(commits).toEqual([['myPluginRowPx', 120]])
|
|
expect(input.value).toBe('120')
|
|
unmount()
|
|
})
|
|
|
|
it('clamps an emptied number input to the lower bound on commit (width-row precedent)', () => {
|
|
const commits: Array<[string, number]> = []
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'myPluginRowPx',
|
|
type: 'number',
|
|
title: () => 'Row height',
|
|
min: 0,
|
|
max: 120,
|
|
}
|
|
const { container, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs: withPluginKey('myPluginRowPx', 40),
|
|
onToggle: () => {},
|
|
// The parent mirrors the real handler: an emptied number parses to 0
|
|
// and clamps into the bounds (a browser number input never holds a
|
|
// non-numeric string — the draft can only be empty or numeric).
|
|
onCommit: (t, raw) => {
|
|
const clamped = Math.min(120, Math.max(0, Math.round(Number(raw))))
|
|
commits.push([t.key, clamped])
|
|
return String(clamped)
|
|
},
|
|
}))
|
|
const input = container.querySelector('input')!
|
|
typeAndBlur(input, '')
|
|
expect(commits).toEqual([['myPluginRowPx', 0]])
|
|
expect(input.value).toBe('0')
|
|
unmount()
|
|
})
|
|
|
|
it('reverts the draft to the committed value after a failed commit reverts prefs', () => {
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'terminalFontFamily',
|
|
type: 'text',
|
|
title: () => 'Font family',
|
|
}
|
|
// The parent mirrors the real handler: the optimistic commit adopts the
|
|
// typed value, and a FAILED write reverts prefs to the stored one.
|
|
let prefsNow = { ...prefs, terminalFontFamily: 'Old' }
|
|
const { container, rerender, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs: prefsNow,
|
|
onToggle: () => {},
|
|
onCommit: (_t, raw) => raw,
|
|
}))
|
|
const input = container.querySelector('input')!
|
|
typeAndBlur(input, 'New')
|
|
// Optimistic commit: the draft adopts the typed value.
|
|
expect(input.value).toBe('New')
|
|
// The optimistic pref lands (parent state): the key changes, the row
|
|
// remounts with the same value — no visible reset while editing.
|
|
prefsNow = { ...prefsNow, terminalFontFamily: 'New' }
|
|
rerender(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs: prefsNow,
|
|
onToggle: () => {},
|
|
onCommit: (_t, raw) => raw,
|
|
}))
|
|
expect(container.querySelector('input')!.value).toBe('New')
|
|
// The write fails: prefs revert to the stored value and the row
|
|
// remounts with it (the stale draft is gone).
|
|
prefsNow = { ...prefsNow, terminalFontFamily: 'Old' }
|
|
rerender(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs: prefsNow,
|
|
onToggle: () => {},
|
|
onCommit: (_t, raw) => raw,
|
|
}))
|
|
expect(container.querySelector('input')!.value).toBe('Old')
|
|
unmount()
|
|
})
|
|
})
|
|
|
|
/** Open one select row's dropdown: click the anchor, the Menu portals its
|
|
* list into document.body (role="menuitem" rows). */
|
|
function openSelect(container: HTMLDivElement): HTMLElement[] {
|
|
const anchor = container.querySelector<HTMLButtonElement>('button[aria-haspopup="menu"]')!
|
|
act(() => { anchor.click() })
|
|
return [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')]
|
|
}
|
|
|
|
describe('FeatureSettingsRows select rows (interactive)', () => {
|
|
const options: NonNullable<SidebarSettingToggle['options']> = [
|
|
{ value: true, icon: (size: number) => createElement('i', { 'data-size': size }), title: () => 'Merged', desc: () => 'Docked tree' },
|
|
{ value: false, icon: (size: number) => createElement('i', { 'data-size': size }), title: () => 'Split', desc: () => 'Two tabs' },
|
|
]
|
|
|
|
it('commits the picked option value and closes (iconed single select)', () => {
|
|
const commits: Array<[string, unknown]> = []
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'editorExplorer',
|
|
type: 'select',
|
|
title: () => 'Editor explorer',
|
|
options,
|
|
}
|
|
const { container, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
// Merged is the selected option for this scenario (the default is now
|
|
// separate — the anchor must render whatever value the prefs carry).
|
|
prefs: { ...prefs, editorExplorer: true },
|
|
onToggle: () => {},
|
|
onSelectValue: (t, next) => { commits.push([t.key, next]) },
|
|
}))
|
|
// The closed anchor shows the selected option's title (icon variant).
|
|
expect(container.textContent).toContain('Merged')
|
|
const items = openSelect(container)
|
|
// Big-icon cards: title + desc per option.
|
|
expect(items.map(item => item.textContent)).toEqual(['MergedDocked tree', 'SplitTwo tabs'])
|
|
act(() => { items[1]!.click() })
|
|
expect(commits).toEqual([['editorExplorer', false]])
|
|
// Single-pick closes the dropdown.
|
|
expect(document.querySelectorAll('[role="menuitem"]')).toHaveLength(0)
|
|
unmount()
|
|
})
|
|
|
|
it('toggles membership and commits the picked values in options order (multi)', () => {
|
|
const commits: Array<[string, unknown]> = []
|
|
const plain: NonNullable<SidebarSettingToggle['options']> = [
|
|
{ value: 'a', title: () => 'Alpha' },
|
|
{ value: 'b', title: () => 'Beta' },
|
|
{ value: 'c', title: () => 'Gamma' },
|
|
]
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'pluginKey',
|
|
type: 'select',
|
|
multi: true,
|
|
title: () => 'Pick several',
|
|
options: plain,
|
|
}
|
|
const rows = (value: unknown) => createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs,
|
|
onToggle: () => {},
|
|
onSelectValue: (t, next) => { commits.push([t.key, next]) },
|
|
valueSource: () => value,
|
|
})
|
|
const { container, rerender, unmount } = renderRoot(rows(['c', 'a']))
|
|
// The anchor shows the picked titles; options follow the declared order.
|
|
const items = openSelect(container)
|
|
expect(items.map(item => item.textContent)).toEqual(['Alpha', 'Beta', 'Gamma'])
|
|
// Deselect 'a' → ['c'] is normalized to options order… (['c'] here).
|
|
act(() => { items[0]!.click() })
|
|
expect(commits).toEqual([['pluginKey', ['c']]])
|
|
// The menu stays open under multi; re-render with the committed value.
|
|
rerender(rows(['c']))
|
|
const again = [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')]
|
|
expect(again).toHaveLength(3)
|
|
// Select 'b' → committed in OPTIONS order, not pick order.
|
|
act(() => { again[1]!.click() })
|
|
expect(commits[1]).toEqual(['pluginKey', ['b', 'c']])
|
|
unmount()
|
|
})
|
|
|
|
it('keeps numeric, boolean and string values distinct through the shared menu', () => {
|
|
const commits: unknown[] = []
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'scalarChoice', type: 'select', title: 'Scalar choice',
|
|
options: [
|
|
{ value: 1, title: 'Number one' },
|
|
{ value: '1', title: 'String one' },
|
|
{ value: true, title: 'Boolean true' },
|
|
{ value: 'true', title: 'String true' },
|
|
],
|
|
}
|
|
const { container, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle], prefs, onToggle: () => {}, valueSource: () => 1,
|
|
onSelectValue: (_toggle, next) => { commits.push(next) },
|
|
}))
|
|
for (const index of [1, 2, 3]) {
|
|
const items = openSelect(container)
|
|
act(() => { items[index]!.click() })
|
|
}
|
|
expect(commits).toEqual(['1', true, 'true'])
|
|
unmount()
|
|
})
|
|
|
|
it('shows an em dash when nothing is selected', () => {
|
|
const toggle: SidebarSettingToggle = {
|
|
key: 'editorExplorer',
|
|
type: 'select',
|
|
title: () => 'Editor explorer',
|
|
options,
|
|
}
|
|
const { container, unmount } = renderRoot(createElement(FeatureSettingsRows, {
|
|
toggles: [toggle],
|
|
prefs,
|
|
onToggle: () => {},
|
|
valueSource: () => undefined,
|
|
}))
|
|
expect(container.querySelector('button[aria-haspopup="menu"]')!.textContent).toContain('—')
|
|
unmount()
|
|
})
|
|
})
|