| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- /**
- * SlotsService terminal-design account (design.md §11-3 main landing):
- * built-in 'root', the three load-time throws (duplicate declaration /
- * undeclared contribution / cross-scope store handle), the renderer install
- * seam (double install / not installed / non-root key), store instance
- * resolution and lifecycle on the ledger axis, and the entry-unload cascade.
- */
- import { Context } from 'cordis'
- import { describe, expect, it, vi } from 'vitest'
- import type { FC } from 'react'
- import type { SlotRendererHost } from '@deepseek-ai/dsh-client-ui-slots'
- import { SlotsService } from '../src/client/slots.ts'
- // Test-only slot keys (merged so the typed entries/spec faces accept them).
- declare module '@deepseek-ai/dsh-client-ui-slots' {
- interface SlotMap {
- 't.host': { kind: 'single'; scope: 'root' }
- 't.panel': { kind: 'single'; scope: 'session' }
- 't.rows': { kind: 'list'; scope: 'root' }
- }
- }
- const C: FC<object> = () => null
- /**
- * Register/install/renderSlot through a type-erased view: the typed register
- * face rides wave-1 ui-slots types (red until that wave lands); the runtime
- * semantics under test are final.
- */
- interface ErasedService {
- register(options: object, component: unknown): () => void
- install(renderer: object): void
- renderSlot(key: string, owner: object): unknown
- }
- interface Bench {
- ctx: Context
- svc: SlotsService
- erased: ErasedService
- }
- async function boot(): Promise<Bench> {
- const ctx = new Context()
- const fiber = ctx.plugin(SlotsService)
- await fiber
- // Service accessor (ctx.get reads the reflect store, which Service-class
- // plugins do not write; the accessor is the product path).
- const svc = ctx.slots
- return { ctx, svc, erased: svc as unknown as ErasedService }
- }
- /** Engine-shaped instance stub (bare-source form: subscribe/getSnapshot + baked actions + clearPersisted). */
- interface FakeInstance {
- getSnapshot: () => undefined
- subscribe: () => () => void
- actions: Record<string, never>
- clearPersisted: ReturnType<typeof vi.fn>
- }
- /** Fake store handle factory (create-count and clearPersisted observable). */
- function fakeHandle() {
- const created: FakeInstance[] = []
- const handle = {
- create: vi.fn((_scopeKey?: string): FakeInstance => {
- const instance: FakeInstance = {
- getSnapshot: () => undefined, subscribe: () => () => undefined,
- actions: {}, clearPersisted: vi.fn(),
- }
- created.push(instance)
- return instance
- }),
- }
- return { handle, created }
- }
- /**
- * Install a capturing renderer, occupy 'root' (declaring `children` in the
- * same call — 'root' is single, so the one occupant is also the declarer),
- * and pull the host face out through renderSlot('root').
- */
- function captureHost(bench: Bench, children?: object): SlotRendererHost {
- let host: SlotRendererHost | undefined
- bench.erased.install({
- renderRoot: (h: SlotRendererHost) => { host = h; return 'rendered' },
- })
- bench.erased.register({ name: 'root', ...(children !== undefined ? { children } : {}) }, C)
- bench.ctx.reflect.provide('sessions', fakeSessions())
- bench.ctx.reflect.provide('workspaces', fakeWorkspaces())
- bench.erased.renderSlot('root', {})
- if (host === undefined) throw new Error('renderer never received the host')
- return host
- }
- /** Minimal independent Workspace list source for the renderer host seam. */
- function fakeWorkspaces() {
- const state = { items: [], phase: 'ready' as const }
- return { list: { getSnapshot: () => state, subscribe: () => () => undefined } }
- }
- /** Minimal sessions face for the host seam (list observable + current provide projection). */
- function fakeSessions() {
- const state = { ids: [], byId: {}, current: undefined as string | undefined }
- const absentInfo = { sessionId: undefined, hooks: { session: undefined }, props: {} }
- return {
- list: { getSnapshot: () => state, subscribe: () => () => undefined },
- currentProvideInfo: { getSnapshot: () => absentInfo, subscribe: () => () => undefined },
- }
- }
- describe("built-in 'root'", () => {
- it('is declared at construction: spec readable, occupancy open, no plugin needed', async () => {
- const bench = await boot()
- expect(bench.svc.spec('root')).toEqual({ kind: 'single', scope: 'root' })
- expect(() => bench.erased.register({ name: 'root' }, C)).not.toThrow()
- expect(bench.svc.entries('root')).toHaveLength(1)
- })
- it('rejects a second declaration of root, attributing the built-in row', async () => {
- const bench = await boot()
- expect(() => bench.erased.register({
- name: 'root', children: { 'root': { kind: 'single', scope: 'root' } },
- }, C)).toThrow(/already declared.*built-in/)
- })
- })
- describe('load-time validation', () => {
- it('throws on contributing into an undeclared slot', async () => {
- const bench = await boot()
- expect(() => bench.erased.register({ name: 't.host' }, C)).toThrow(/slot "t.host" is not declared/)
- })
- it('throws on a duplicate declaration, naming the slot and the prior declarant', async () => {
- const bench = await boot()
- bench.erased.register({ name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } } }, C)
- bench.erased.register({
- name: 't.host', children: { 't.rows': { kind: 'list', scope: 'root' } },
- }, C)
- expect(() => bench.erased.register({
- name: 't.rows', id: 'r1', children: { 't.rows': { kind: 'list', scope: 'root' } },
- }, C)).toThrow(/slot "t.rows" is already declared.*"t.host"/)
- })
- it('throws when one store handle is bound to two scopes', async () => {
- const bench = await boot()
- bench.erased.register({
- name: 'root',
- children: {
- 't.host': { kind: 'single', scope: 'root' },
- 't.panel': { kind: 'single', scope: 'session' },
- },
- }, C)
- const { handle } = fakeHandle()
- bench.erased.register({ name: 't.host', store: handle }, C)
- expect(() => bench.erased.register({ name: 't.panel', store: handle }, C))
- .toThrow(/one handle, one scope/)
- })
- it('commits nothing when the core rejects the entry (children stay undeclared)', async () => {
- const bench = await boot()
- bench.erased.register({ name: 'root' }, C) // 'root' single slot now occupied
- expect(() => bench.erased.register({
- name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } },
- }, C)).toThrow(/already has a registration/)
- // The failing call's declaration must not have landed.
- expect(() => bench.erased.register({ name: 't.host' }, C)).toThrow(/is not declared/)
- })
- })
- describe('renderer install seam', () => {
- it('throws on renderSlot before install (boot-order guidance)', async () => {
- const bench = await boot()
- expect(() => bench.erased.renderSlot('root', {})).toThrow(/renderer not installed/)
- })
- it('throws on double install', async () => {
- const bench = await boot()
- bench.erased.install({ renderRoot: () => null })
- expect(() => { bench.erased.install({ renderRoot: () => null }) }).toThrow(/already installed/)
- })
- it('throws on any non-root key (single ctx-level entry)', async () => {
- const bench = await boot()
- bench.erased.install({ renderRoot: () => null })
- expect(() => bench.erased.renderSlot('t.host', {})).toThrow(/only renders 'root'/)
- })
- it("throws on renderSlot('root') before any root registration", async () => {
- const bench = await boot()
- bench.erased.install({ renderRoot: () => null })
- expect(() => bench.erased.renderSlot('root', {})).toThrow(/no registration/)
- })
- it('renders through the installed renderer and returns its product', async () => {
- const bench = await boot()
- const renderRoot = vi.fn(() => 'tree')
- bench.erased.install({ renderRoot })
- bench.erased.register({ name: 'root' }, C)
- bench.ctx.reflect.provide('sessions', fakeSessions())
- bench.ctx.reflect.provide('workspaces', fakeWorkspaces())
- expect(bench.erased.renderSlot('root', {})).toBe('tree')
- expect(renderRoot).toHaveBeenCalledTimes(1)
- })
- it('fails before rendering when the Workspace object layer is absent', async () => {
- const bench = await boot()
- bench.erased.install({ renderRoot: () => null })
- bench.erased.register({ name: 'root' }, C)
- bench.ctx.reflect.provide('sessions', fakeSessions())
- expect(() => bench.erased.renderSlot('root', {})).toThrow(/workspaces service mounted/)
- })
- })
- describe('host face', () => {
- it('serves entriesOf/specOf/isLive off the ledger and flips isLive on disposal', async () => {
- const bench = await boot()
- const host = captureHost(bench, { 't.host': { kind: 'single', scope: 'root' } })
- const dispose = bench.erased.register({ name: 't.host' }, C)
- const rootEntry = host.entriesOf('root')[0]
- expect(rootEntry).toBeDefined()
- expect(rootEntry?.component).toBe(C)
- expect(host.specOf('root')).toEqual({ kind: 'single', scope: 'root' })
- expect(host.specOf('t.host')).toEqual({ kind: 'single', scope: 'root' })
- const childEntry = host.entriesOf('t.host')[0]
- expect(host.isLive(childEntry as never)).toBe(true)
- dispose()
- expect(host.isLive(childEntry as never)).toBe(false)
- expect(host.entriesOf('t.host')).toHaveLength(0)
- })
- it('exposes the session list and the atomic current provide projection', async () => {
- const bench = await boot()
- const host = captureHost(bench)
- expect(host.sessions.list.getSnapshot()).toMatchObject({ ids: [] })
- expect(host.sessions.provideInfo.getSnapshot()).toMatchObject({ sessionId: undefined })
- })
- it('exposes the independent Workspace list source', async () => {
- const bench = await boot()
- const host = captureHost(bench)
- expect(host.workspaces.list.getSnapshot()).toEqual({ items: [], phase: 'ready' })
- })
- })
- describe('store instance axis', () => {
- /** Boot with 'root' occupied and the three test children declared. */
- async function storeBench() {
- const bench = await boot()
- const host = captureHost(bench, {
- 't.host': { kind: 'single', scope: 'root' },
- 't.rows': { kind: 'list', scope: 'root' },
- 't.panel': { kind: 'single', scope: 'session' },
- })
- return { bench, host }
- }
- it('resolves one instance per (handle x root scope) shared across entries', async () => {
- const { bench, host } = await storeBench()
- const { handle } = fakeHandle()
- bench.erased.register({ name: 't.host', store: handle }, C)
- bench.erased.register({ name: 't.rows', id: 'a', store: handle }, C)
- const [hostEntry] = host.entriesOf('t.host')
- const [rowEntry] = host.entriesOf('t.rows')
- const a = host.storeOf(hostEntry as never, undefined)
- const b = host.storeOf(rowEntry as never, undefined)
- expect(a).toBeDefined()
- expect(a).toBe(b) // shared handle, same scope key = same instance
- expect(handle.create).toHaveBeenCalledTimes(1)
- expect(handle.create).toHaveBeenCalledWith() // root scope: keyless create
- })
- it('resolves per-session instances keyed by session id, created with the scope key', async () => {
- const { bench, host } = await storeBench()
- const { handle } = fakeHandle()
- bench.erased.register({ name: 't.panel', store: handle }, C)
- const [entry] = host.entriesOf('t.panel')
- const s1 = host.storeOf(entry as never, 's1')
- const s2 = host.storeOf(entry as never, 's2')
- expect(s1).not.toBe(s2)
- expect(host.storeOf(entry as never, 's1')).toBe(s1) // cached per key
- expect(handle.create).toHaveBeenCalledWith('s1')
- expect(handle.create).toHaveBeenCalledWith('s2')
- expect(() => host.storeOf(entry as never, undefined)).toThrow(/requires a session id/)
- })
- it('mints a fresh handle per register for the factory (exclusive) form', async () => {
- const { bench, host } = await storeBench()
- const factory = vi.fn(() => fakeHandle().handle)
- bench.erased.register({ name: 't.host', store: factory }, C)
- bench.erased.register({ name: 't.rows', id: 'a', store: factory }, C)
- expect(factory).toHaveBeenCalledTimes(2)
- const a = host.storeOf(host.entriesOf('t.host')[0] as never, undefined)
- const b = host.storeOf(host.entriesOf('t.rows')[0] as never, undefined)
- expect(a).not.toBe(b) // two mints, two instances
- })
- it('drops instances with the last holding entry and refuses stale resolution', async () => {
- const { bench, host } = await storeBench()
- const { handle } = fakeHandle()
- const d1 = bench.erased.register({ name: 't.host', store: handle }, C)
- bench.erased.register({ name: 't.rows', id: 'a', store: handle }, C)
- const rowEntry = host.entriesOf('t.rows')[0]
- const hostEntry = host.entriesOf('t.host')[0]
- const shared = host.storeOf(rowEntry as never, undefined)
- d1() // one holder left: record (and instance) survive
- expect(host.storeOf(rowEntry as never, undefined)).toBe(shared)
- expect(() => host.storeOf(hostEntry as never, undefined)).not.toThrow() // handle still live via the row entry
- // Note: dropping the row entry would sever the last reference; stale
- // resolution is covered through the cascade spec below.
- })
- it('pruneStoreScope clears persisted state per dead session, including never-materialized ones', async () => {
- const { bench, host } = await storeBench()
- const { handle, created } = fakeHandle()
- bench.erased.register({ name: 't.panel', store: handle }, C)
- const [entry] = host.entriesOf('t.panel')
- const s1 = host.storeOf(entry as never, 's1')
- expect(s1).toBe(created[0]) // the resolved instance is the fake the handle minted
- bench.svc.pruneStoreScope('s1')
- expect(created[0]?.clearPersisted).toHaveBeenCalledTimes(1)
- expect(host.storeOf(entry as never, 's1')).not.toBe(s1) // instance dropped, next resolve mints anew
- // Never-rendered dead session: a transient instance is created just to clear storage.
- const before = created.length
- bench.svc.pruneStoreScope('s-never')
- expect(created.length).toBe(before + 1)
- expect(created[created.length - 1]?.clearPersisted).toHaveBeenCalledTimes(1)
- })
- })
- describe('entry-unload cascade', () => {
- it('kills declared children, their contributions, and the ledger rows with the entry', async () => {
- const bench = await boot()
- let host: SlotRendererHost | undefined
- bench.erased.install({
- renderRoot: (h: SlotRendererHost) => { host = h; return 'rendered' },
- })
- bench.ctx.reflect.provide('sessions', fakeSessions())
- bench.ctx.reflect.provide('workspaces', fakeWorkspaces())
- // The declarer here is NOT the root occupant: root stays occupied by a
- // separate entry so disposing the declarer only kills its children.
- const disposeRoot = bench.erased.register({ name: 'root' }, C)
- bench.erased.renderSlot('root', {})
- if (host === undefined) throw new Error('renderer never received the host')
- disposeRoot()
- const disposeDeclarer = bench.erased.register({
- name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } },
- }, C)
- bench.erased.register({ name: 't.host' }, C)
- const [childEntry] = host.entriesOf('t.host')
- expect(childEntry).toBeDefined()
- disposeDeclarer()
- expect(bench.svc.spec('t.host')).toBeUndefined() // ledger row gone
- expect(host.specOf('t.host')).toBeUndefined() // outlets now render empty
- expect(bench.svc.entries('t.host')).toHaveLength(0) // contribution cleared
- expect(host.isLive(childEntry as never)).toBe(false) // stale bindings will throw upstream
- // The freed key is re-declarable by a new entry (no residue).
- expect(() => bench.erased.register({
- name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } },
- }, C)).not.toThrow()
- })
- it('cascades through cordis fiber disposal (plugin unload = full cleanup)', async () => {
- const bench = await boot()
- bench.erased.register({
- name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } },
- }, C)
- const fiber = bench.ctx.plugin({
- name: 'occupant',
- inject: ['slots'],
- apply: (pluginCtx: Context) => {
- ;(pluginCtx.slots as unknown as ErasedService).register({ name: 't.host' }, C)
- },
- })
- await fiber.await()
- expect(bench.svc.entries('t.host')).toHaveLength(1)
- await fiber.dispose()
- expect(bench.svc.entries('t.host')).toHaveLength(0)
- expect(bench.svc.spec('t.host')).toBeDefined() // declarer still live; slot stays declared
- })
- it('disposer is idempotent (stale second call is a no-op)', async () => {
- const bench = await boot()
- const dispose = bench.erased.register({
- name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } },
- }, C)
- dispose()
- expect(() => { dispose() }).not.toThrow()
- expect(() => bench.erased.register({
- name: 'root', children: { 't.host': { kind: 'single', scope: 'root' } },
- }, C)).not.toThrow()
- })
- })
- describe('event bridge', () => {
- it("re-emits entry writes and child declarations as 'slots/changed'", async () => {
- const bench = await boot()
- const seen: string[] = []
- bench.ctx.on('slots/changed', (key) => { seen.push(key) })
- bench.erased.register({
- name: 'root', children: { 't.rows': { kind: 'list', scope: 'root' } },
- }, C)
- bench.erased.register({ name: 't.rows', id: 'a' }, C)
- expect(seen).toEqual(['root', 't.rows', 't.rows'])
- })
- })
|