| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- import { Context } from '@deepseek-ai/cordis'
- import { describe, expect, it, vi } from 'vitest'
- import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
- import { RemoteError, TestRemote } from '@deepseek-ai/dsh-client-test-runtime'
- import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
- import { apply, inject } from '@deepseek-ai/dsh-client-ui-workspace/client'
- import type { WorkspaceBrowserInjected, WorkspacePickerInjected } from '@deepseek-ai/dsh-client-ui-workspace/client'
- import { WorkspaceBrowser } from '../src/client/rows/WorkspaceBrowser.tsx'
- import { WorkspacePicker } from '../src/client/WorkspacePicker.tsx'
- import { apply as hostApply } from '../src/index.ts'
- import type { SessionReference } from '@deepseek-ai/dsh-api-session-controller/client'
- async function bench() {
- const ctx = new Context()
- await ctx.plugin(SlotRegistry).await()
- const create = vi.fn(async (input: { name: string } | { path: string }) => ({
- workspaceId: 'ws-new' as never,
- path: 'name' in input ? `/projects/${input.name}` : input.path,
- title: 'new', sessionIds: [], createdAt: '0', updatedAt: '0',
- }))
- const rename = vi.fn(async () => ({}))
- const selectPanel = vi.fn()
- ctx.provide('layout', { selectPanel, beginNavigation: () => new AbortController().signal })
- const search = vi.fn(async () => ({
- ok: true as const,
- value: { items: [{ sessionId: 'session' as never, snippet: 'match' }], hasMore: false },
- }))
- const renameSession = vi.fn(async (title: string) => ({ ok: true, value: { title, seq: 1 } }))
- const binding = vi.fn((_id: string) => ({ session: { rename: renameSession } }))
- const retain = vi.fn((target: string) => {
- const resolved = binding(target)
- const release = vi.fn()
- return {
- sessionId: target,
- binding: resolved,
- ready: Promise.resolve(resolved),
- release,
- [Symbol.dispose]: release,
- } as unknown as SessionReference
- })
- const using = vi.fn(async (
- target: string,
- _options: unknown,
- operation: (reference: SessionReference) => unknown,
- ) => await operation(retain(target)))
- const fork = vi.fn(async () => 'forked' as never)
- const subscribe = () => () => {}
- ctx.provide('workspaces', {
- list: {
- getSnapshot: () => ({
- items: [], archivedSessionIds: [], state: 'idle', phase: 'ready', error: null,
- }),
- subscribe,
- },
- create,
- rename,
- delete: vi.fn(async () => undefined),
- insertBefore: vi.fn(async () => undefined),
- archiveSession: vi.fn(async () => undefined),
- insertSessionBefore: vi.fn(async () => ({})),
- } as never)
- ctx.provide('sessions', {
- list: {
- getSnapshot: () => ({
- ids: [], byId: {}, current: undefined, phase: 'ready',
- subagentsByParent: {}, jobsBySession: {}, currentAddress: undefined,
- }),
- subscribe,
- },
- create: vi.fn(async () => 'created' as never),
- retain,
- using,
- search,
- searchResultLimit: 20,
- binding,
- subagentAddress: vi.fn(() => undefined),
- refreshSubagents: vi.fn(() => Promise.resolve()),
- fork,
- } as never)
- const pickDirectory = vi.fn(() => Promise.resolve({ ok: true as const, value: '/projects/picked' }))
- const directoryPicker = { pick: pickDirectory }
- Object.assign(new TestRemote(ctx), { directoryPicker })
- ctx.provide('remote.directoryPicker', directoryPicker as never)
- const locale = new LocaleRuntime(ctx)
- // These specs assert the shipped Chinese copy. There is no jsdom `window`
- // in this lane, so browser-language detection never runs and the locale
- // comes from FALLBACK_LOCALE (en): state the asserted locale explicitly.
- locale.setLocale('zh')
- ctx.provide('locale', locale)
- return {
- ctx, slots: ctx.get('slots') as SlotRegistry, locale, create, rename,
- retain, using, selectPanel, search, renameSession, binding, fork, pickDirectory,
- }
- }
- type HoleName = 'sidebar.workspaces' | 'conversation.hero.workspace' | 'conversation.empty.workspace'
- /** Declare any subset of the holes with a single root registration ('root' is a single slot). */
- function declare(slots: SlotRegistry, ...names: HoleName[]): () => void {
- const children = Object.fromEntries(names.map(name => [name, { kind: 'single', scope: 'root' }]))
- return slots.register({ name: 'root', children } as never, () => null)
- }
- describe('ui-workspace apply', () => {
- it('keeps the host Loader entry inert', () => {
- expect(hostApply).not.toThrow()
- })
- it('declares the services it drives', () => {
- expect(inject).toEqual([
- 'slots', 'sessions', 'workspaces', 'locale', 'remote', 'remote.directoryPicker', 'layout',
- ])
- })
- it('registers browser and pickers for declarations arriving before or after apply', async () => {
- const before = await bench()
- declare(before.slots, 'sidebar.workspaces')
- await before.ctx.plugin({ inject: [...inject], apply }).await()
- expect(before.slots.entries('sidebar.workspaces')[0]!.component).toBe(WorkspaceBrowser)
- // Copy rides the standard locale seat: the entry declares the namespace
- // and apply registered both dictionaries.
- expect(before.slots.entries('sidebar.workspaces')[0]!.locale).toBe('workspace')
- expect(before.locale.bind('workspace')('session.new')).toBe('新会话')
- const after = await bench()
- await after.ctx.plugin({ inject: [...inject], apply }).await()
- declare(after.slots, 'conversation.hero.workspace', 'conversation.empty.workspace')
- await Promise.resolve()
- expect(after.slots.entries('conversation.hero.workspace')[0]!.component).toBe(WorkspacePicker)
- // expect(after.slots.entries('conversation.empty.workspace')[0]!.component).toBe(WorkspacePicker)
- })
- it('routes browser actions and picker creation to the services', async () => {
- const b = await bench()
- declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
- await b.ctx.plugin({ inject: [...inject], apply }).await()
- const startSession = vi.spyOn(b.ctx.uiWorkspace, 'startSession').mockImplementation(() => undefined)
- const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
- // Both arms delegate to the shared Session navigation action.
- browser.startSession('ws' as never)
- expect(startSession).toHaveBeenCalledWith('ws')
- browser.startSession()
- expect(startSession).toHaveBeenLastCalledWith(undefined)
- browser.open('session' as never)
- expect(b.retain).toHaveBeenCalledWith('session', { source: 'mainView' })
- const signal = new AbortController().signal
- await expect(browser.searchSessions('match', signal)).resolves.toEqual({
- items: [{ sessionId: 'session', snippet: 'match' }],
- hasMore: false,
- })
- expect(b.search).toHaveBeenCalledWith('match', signal)
- expect(browser.searchResultLimit).toBe(20)
- await browser.renameSession('session' as never, 'renamed session')
- expect(b.using).toHaveBeenCalledWith(
- 'session', { source: 'workspaceOperation' }, expect.any(Function),
- )
- expect(b.renameSession).toHaveBeenCalledWith('renamed session')
- browser.forkSession('session' as never)
- await vi.waitFor(() => {
- expect(b.retain).toHaveBeenCalledWith('forked', { source: 'mainView' })
- })
- expect(b.fork).toHaveBeenCalledWith({ sessionId: 'session', increaseTitle: true })
- await browser.renameWorkspace('ws' as never, 'renamed')
- expect(b.rename).toHaveBeenCalledWith('ws', 'renamed')
- await browser.createWorkspace({ path: '/tmp/browser-project' })
- expect(b.create).toHaveBeenCalledWith({ path: '/tmp/browser-project' })
- const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
- await picker.createWorkspace({ path: '/tmp/project' })
- expect(b.create).toHaveBeenCalledWith({ path: '/tmp/project' })
- })
- it('declares the two directory-flow holes and reports their occupancy per surface', async () => {
- const b = await bench()
- declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace')
- await b.ctx.plugin({ inject: [...inject], apply }).await()
- // Registration declared the child holes (declaration = render authorization).
- expect(b.slots.spec('sidebar.workspaces.directoryFlow')).toMatchObject({ kind: 'single' })
- expect(b.slots.spec('conversation.hero.workspace.directoryFlow')).toMatchObject({ kind: 'single' })
- const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
- const picker = (b.slots.entries('conversation.hero.workspace')[0]!.inject as () => WorkspacePickerInjected)()
- expect(browser.hooks.directoryFlow.getSnapshot()).toBe(false)
- expect(browser.hooks.hostInfo.getSnapshot()).toMatchObject({ home: undefined })
- expect(picker.hooks.directoryFlow.getSnapshot()).toBe(false)
- // A flow occupant flips exactly its own surface, and the source notifies.
- const notified = vi.fn()
- const unsubscribe = browser.hooks.directoryFlow.subscribe(notified)
- const dispose = b.slots.register({ name: 'sidebar.workspaces.directoryFlow' } as never, () => null)
- expect(browser.hooks.directoryFlow.getSnapshot()).toBe(true)
- expect(picker.hooks.directoryFlow.getSnapshot()).toBe(false)
- await Promise.resolve()
- expect(notified).toHaveBeenCalled()
- dispose()
- expect(browser.hooks.directoryFlow.getSnapshot()).toBe(false)
- unsubscribe()
- })
- it('rejects the browser search callback on a Session Controller business error', async () => {
- const b = await bench()
- b.search.mockImplementationOnce(async () => ({
- ok: false,
- error: new RemoteError('gateway/internal', 'index unavailable', {}),
- }) as never)
- declare(b.slots, 'sidebar.workspaces')
- await b.ctx.plugin({ inject: [...inject], apply }).await()
- const browser = (b.slots.entries('sidebar.workspaces')[0]!.inject as () => WorkspaceBrowserInjected)()
- await expect(browser.searchSessions('needle', new AbortController().signal))
- .rejects.toThrow('index unavailable')
- })
- it('unregisters every entry on teardown', async () => {
- const b = await bench()
- declare(b.slots, 'sidebar.workspaces', 'conversation.hero.workspace', 'conversation.empty.workspace')
- const fiber = b.ctx.plugin({ inject: [...inject], apply })
- await fiber.await()
- await fiber.dispose()
- expect(b.slots.entries('sidebar.workspaces')).toHaveLength(0)
- expect(b.slots.entries('conversation.hero.workspace')).toHaveLength(0)
- // expect(b.slots.entries('conversation.empty.workspace')).toHaveLength(0)
- })
- })
|