Files
apes-sidebar/tests/consumer-types.ts
T
Menghuan1918 0995a58454 fix: 回应独立 CR 的 P2/P3 意见(0×P1)
- P2-1 onOpen 收到真正落地的 tab:url 新建场景从 landed 态按 id 反查
  (补丁后对象),回调不再拿到未打 url 的 tab
- P2-2 插件设置行显式取值源:FeatureSettingsRows 新增 valueSource prop,
  插件行读自己的 pluginSettings blob——插件键与宿主 pref 同名时不再
  静默读宿主值(原投影合并有读宿主写插件的读写不对称);prop 命名避开
  Object.prototype.valueOf 冲突
- P2-3 consumer 类型检查接入工程:package.json 新增 check:consumer-types
  脚本 + CI 在 build 后执行;脚本补 lib/types 存在性前置检查(防假绿)
- P3-1 url seed 只在新建时应用:dedupe 聚焦的 tab 不再被覆写 path
- P3-2 closeTab/activateTab 未知 id 严格 no-op:store.reduce 对同引用
  结果短路(不 notify、不重写 localStorage),根因修复 + 回归测试
- P3-3 closeTab/activateTab 增加可选 scope(cwd 随回调传递),Sidebar
  双树激活/关闭路径传入 { sessionId, cwd }
- P3-4 定向打开不再自动展开不可见 session 的面板
- P3-5 Sidebar 终端 pty 释放查找覆盖双树(底部面板终端不再漏掉 HTTP
  兜底)
- P3-6/P3-7 与 Nits:AGENTS.md/指南文档化(dedupeKey 纯函数、meta 用
  null 清除、回调 scope 的 cwd 语义);consumer fixture 补常量导出覆盖

测试 +6(url 落地保真 / url 不覆写聚焦 / 未知 id 无 notify / 定向打开
不展开 / scope 随回调 / valueSource 键隔离),464 全绿;typecheck、
build、consumer 校验全部通过。
2026-08-14 21:28:41 +08:00

141 lines
5.2 KiB
TypeScript

/**
* Consumer-facing type surface compile gate (v0.12.0+): this file exercises
* EVERY public type and descriptor field exactly the way an external plugin
* would, so `pnpm typecheck` fails the moment the shipped declaration
* surface (service.ts re-exports / descriptor fields / service methods)
* drifts from what consumers can name. Type-only — erased at runtime, never
* executed by vitest (no `*.spec` suffix).
*
* Mirrors the "external consumer" fixture: what is importable here from
* `../src/client/service.ts` must also be importable from
* `dsh-better-sidebar/client/service` in the built package.
*/
import type {} from '../src/client/service.ts'
import {
SIDEBAR_FEATURES,
SIDEBAR_SERVICE_VERSION,
} from '../src/client/service.ts'
import type {
BetterSidebarService,
FileFetchStrategy,
FileViewerDescriptor,
FileViewerProps,
OpenTabSeed,
SidebarSettingsDeclaration,
SidebarSettingsRenderProps,
SidebarSettingToggle,
SidebarSettingToggleType,
TabComponentProps,
TabDescriptor,
} from '../src/client/service.ts'
import type {
SessionScope,
SidebarDiffRef,
SidebarPrefs,
SidebarSnapshot,
SidebarState,
SidebarStore,
SidebarTab,
TabType,
} from '../src/client/service.ts'
/** A full-featured external tab descriptor using every v0.12.0 field. */
const tab: TabDescriptor = {
id: 'my-plugin:db',
title: () => 'Database',
icon: (size: number) => null,
order: 50,
hidden: false,
available: (ctx, scope, state) => scope.sessionId !== '' && state.panelOpen && ctx !== null,
single: false,
dedupeKey: (t: SidebarTab) => t.id,
createTab: (state: SidebarState) => ({
tab: { id: `my-plugin:db:${state.nextTerminal}`, type: 'my-plugin:db', title: 'DB', meta: { n: state.nextTerminal } },
patch: { nextTerminal: state.nextTerminal + 1 },
}),
badge: (ctx, scope, state) => (state.expanded.length > 0 ? state.expanded.length : null),
onOpen: (tab: SidebarTab, scope: SessionScope) => { void tab; void scope },
onActivate: (tab: SidebarTab, scope: SessionScope) => { void tab; void scope },
onClose: (tab: SidebarTab, scope: SessionScope) => { void tab; void scope },
settings: {
toggles: [{ key: 'autoOpenSubagent', title: 'Auto-open', type: 'switch' }],
pluginToggles: [{
key: 'pageSize',
title: 'Page size',
type: 'number',
min: 1,
max: 100,
unit: 'rows',
}],
render: (props: SidebarSettingsRenderProps) => {
props.updatePluginSetting('refresh', true)
props.close()
return null
},
},
component: (props: TabComponentProps) => {
const { ctx, store, scope, tab, visible } = props
void ctx; void store; void scope; void tab; void visible
return null
},
}
/** A full-featured external viewer using the v0.12.0 load signal. */
const viewer: FileViewerDescriptor = {
id: 'my-plugin:csv',
title: 'CSV',
exts: ['csv'],
priority: 10,
fetchStrategy: 'custom',
detect: (_path, head: Uint8Array) => head.length > 0,
load: async (path: string, scope: SessionScope, signal?: AbortSignal) => {
void path; void scope; void signal
return { rows: [] }
},
settings: { pluginToggles: [{ key: 'delimiter', title: 'Delimiter', type: 'text', placeholder: ',' }] },
component: (props: FileViewerProps) => {
const { customData, content, truncated, mediaUrl, viewerId, path, title } = props
void customData; void content; void truncated; void mediaUrl; void viewerId; void path; void title
return null
},
}
/** The full service surface, exercised exactly as consumers call it. */
declare const ctx: { betterSidebar: BetterSidebarService }
const service: BetterSidebarService = ctx.betterSidebar
service.registerTab(tab)
service.registerFileViewer(viewer)
service.getTabs()
service.getFileViewers()
service.getTab('my-plugin:db')
service.isTabEnabled('my-plugin:db')
service.isViewerEnabled('my-plugin:csv')
service.matchFileViewer('a.csv', new Uint8Array([1]))
service.closeTab('tab:1')
service.subscribe(() => {})
const seed: OpenTabSeed = { type: 'my-plugin:db', title: 'DB', path: '/p', id: 'x', meta: { a: 1 } }
service.openTab(seed)
service.openTab(seed, { sessionId: 's1', cwd: '/p' })
service.version
service.features.includes('badge')
const snapshot: SidebarSnapshot | undefined = service.getSnapshot()
void snapshot
service.subscribeState(() => {})
service.updateTab('tab:1', { title: 'T', path: '/p', meta: 1 })
service.activateTab('tab:1')
service.openFile({ sessionId: 's1', cwd: '/p' }, '/p/a.csv', 'Data')
/** Named state vocabulary stays importable (the pre-0.12 gap). */
const diff: SidebarDiffRef = { kind: 'worktree', path: '/p/a.ts', staged: false }
const prefs: SidebarPrefs = { ...({} as SidebarPrefs) }
const store: SidebarStore = null as unknown as SidebarStore
const toggleType: SidebarSettingToggleType = 'number'
const toggle: SidebarSettingToggle = { key: 'k', title: 'K', type: toggleType }
const declaration: SidebarSettingsDeclaration = { toggles: [toggle], pluginToggles: [toggle] }
const typeName: TabType = 'my-plugin:db'
const version: string = SIDEBAR_SERVICE_VERSION
const features: readonly string[] = SIDEBAR_FEATURES
void version; void features
const strategy: FileFetchStrategy = 'mediaUrl'
void diff; void prefs; void store; void declaration; void typeName; void strategy