2026-07-26 00:02:46 +08:00
|
|
|
/** Sidebar shell slot registration and its plain runtime/layout callbacks. */
|
2026-07-19 21:17:57 +08:00
|
|
|
import { Context } from 'cordis'
|
2026-07-23 01:40:30 +08:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2026-07-19 21:17:57 +08:00
|
|
|
import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
|
|
|
|
|
import { apply, inject } from '@deepseek-ai/dsh-client-ui-sidebar/client'
|
2026-07-23 01:40:30 +08:00
|
|
|
import type { SidebarRootInjected } from '@deepseek-ai/dsh-client-ui-sidebar/client'
|
2026-07-19 21:17:57 +08:00
|
|
|
|
2026-07-25 16:04:48 +08:00
|
|
|
async function bench(declare = true) {
|
2026-07-19 21:17:57 +08:00
|
|
|
const ctx = new Context()
|
|
|
|
|
await ctx.plugin(SlotsService).await()
|
2026-07-23 01:40:30 +08:00
|
|
|
const layout = { toggleSidebar: vi.fn() }
|
2026-07-27 08:51:05 +08:00
|
|
|
const workspaces = { startSession: vi.fn() }
|
2026-07-27 03:17:52 +08:00
|
|
|
const sessions = { open: vi.fn(), clear: vi.fn() }
|
2026-07-19 21:17:57 +08:00
|
|
|
ctx.provide('layout', layout)
|
2026-07-27 03:17:52 +08:00
|
|
|
ctx.provide('sessions', sessions as never)
|
2026-07-25 16:04:48 +08:00
|
|
|
ctx.provide('workspaces', workspaces as never)
|
2026-07-19 21:17:57 +08:00
|
|
|
const slots = ctx.get('slots') as SlotsService
|
2026-07-25 16:04:48 +08:00
|
|
|
if (declare) {
|
|
|
|
|
slots.register(
|
|
|
|
|
{ name: 'root', children: { 'sidebar': { kind: 'single', scope: 'root' } } } as never,
|
|
|
|
|
() => null,
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-07-27 03:17:52 +08:00
|
|
|
return { ctx, slots, layout, workspaces, sessions }
|
2026-07-19 21:17:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-25 16:04:48 +08:00
|
|
|
describe('ui-sidebar apply', () => {
|
|
|
|
|
it('declares only the services it uses', () => {
|
2026-07-27 03:17:52 +08:00
|
|
|
expect(inject).toEqual(['slots', 'layout', 'sessions', 'workspaces'])
|
2026-07-23 01:40:30 +08:00
|
|
|
})
|
|
|
|
|
|
2026-07-26 00:02:46 +08:00
|
|
|
it('registers the shell and declares the browsing-region hole', async () => {
|
2026-07-25 16:04:48 +08:00
|
|
|
const b = await bench()
|
|
|
|
|
await b.ctx.plugin({ inject: [...inject], apply }).await()
|
|
|
|
|
expect(b.slots.entries('sidebar')).toHaveLength(1)
|
2026-07-26 00:02:46 +08:00
|
|
|
expect(b.slots.spec('sidebar.workspaces')).toEqual({ kind: 'single', scope: 'root' })
|
2026-07-25 16:04:48 +08:00
|
|
|
const injected = (b.slots.entries('sidebar')[0]!.inject as () => SidebarRootInjected)()
|
2026-07-26 00:02:46 +08:00
|
|
|
expect(Object.keys(injected)).toEqual(['startSession', 'toggleSidebar'])
|
2026-07-27 08:51:05 +08:00
|
|
|
// Both arms delegate to the runtime's shared New Session action.
|
2026-07-27 03:17:52 +08:00
|
|
|
injected.startSession('workspace' as never)
|
2026-07-27 08:51:05 +08:00
|
|
|
expect(b.workspaces.startSession).toHaveBeenCalledWith('workspace')
|
2026-07-27 03:17:52 +08:00
|
|
|
injected.startSession()
|
2026-07-27 08:51:05 +08:00
|
|
|
expect(b.workspaces.startSession).toHaveBeenLastCalledWith(undefined)
|
2026-07-25 16:04:48 +08:00
|
|
|
injected.toggleSidebar()
|
|
|
|
|
expect(b.layout.toggleSidebar).toHaveBeenCalledOnce()
|
2026-07-19 21:17:57 +08:00
|
|
|
})
|
|
|
|
|
|
2026-07-25 16:04:48 +08:00
|
|
|
it('fails when no live owner declared the sidebar slot', async () => {
|
|
|
|
|
const b = await bench(false)
|
|
|
|
|
await expect(b.ctx.plugin({ inject: [...inject], apply })).rejects.toThrow(/not declared/)
|
2026-07-19 21:17:57 +08:00
|
|
|
})
|
|
|
|
|
|
2026-07-25 16:04:48 +08:00
|
|
|
it('removes the entry and child declaration on teardown', async () => {
|
|
|
|
|
const b = await bench()
|
|
|
|
|
const fiber = b.ctx.plugin({ inject: [...inject], apply })
|
2026-07-19 21:17:57 +08:00
|
|
|
await fiber.await()
|
|
|
|
|
await fiber.dispose()
|
2026-07-25 16:04:48 +08:00
|
|
|
expect(b.slots.entries('sidebar')).toHaveLength(0)
|
2026-07-26 00:02:46 +08:00
|
|
|
expect(b.slots.spec('sidebar.workspaces')).toBeUndefined()
|
2026-07-19 21:17:57 +08:00
|
|
|
})
|
|
|
|
|
})
|