use-projection.spec.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // @vitest-environment jsdom
  2. /**
  3. * useProjection standard-kit delivery (session-projection subsystem page:
  4. * docs/subsystems/session-projection.md): the fifth
  5. * framework hook seat rides the same provide channel as useSession — a
  6. * session slot component receives `useProjection` in its kit, key-addressed
  7. * over the bundle's projection face; unresolved keys (no value, no face, no
  8. * session) uniformly read `undefined`; live value changes re-render; the
  9. * selector overload runs over the whole value.
  10. */
  11. import { describe, expect, it } from 'vitest'
  12. import { act, render } from '@testing-library/react'
  13. import type { SessionMaybeProvideInfo, StoredEntry } from '@deepseek-ai/dsh-client-ui-slots'
  14. import { createSlotRenderer, type SlotRendererHost } from '@deepseek-ai/dsh-client-web-react'
  15. function observable<T>(initial: T) {
  16. let value = initial
  17. const subs = new Set<() => void>()
  18. return {
  19. getSnapshot: () => value,
  20. subscribe: (fn: () => void) => { subs.add(fn); return () => { subs.delete(fn) } },
  21. set: (next: T) => { value = next; for (const fn of [...subs]) fn() },
  22. }
  23. }
  24. type UseProjectionProp = (key: string, selector?: (v: unknown) => unknown) => unknown
  25. function makeHost() {
  26. const absentInfo: SessionMaybeProvideInfo = { sessionId: undefined, hooks: { session: undefined }, props: {} }
  27. const provide = observable<SessionMaybeProvideInfo>(absentInfo)
  28. const cells = new Map<string, ReturnType<typeof observable<unknown>>>()
  29. /** Store-parallel face: always defined per key; an unseen key snapshots undefined. */
  30. const absent = { getSnapshot: () => undefined, subscribe: () => () => {} }
  31. const sessionEntries: StoredEntry[] = []
  32. let withFace = true
  33. const rootEntry: StoredEntry = {
  34. component: (props: { renderSlot: (key: string, owner: object) => React.ReactNode }) =>
  35. <>{props.renderSlot('k.session', {})}</>,
  36. options: {},
  37. children: { 'k.session': { kind: 'single', scope: 'session' } },
  38. }
  39. const info = (id: string): SessionMaybeProvideInfo => ({
  40. sessionId: id,
  41. hooks: { session: { getSnapshot: () => ({ sid: id }), subscribe: () => () => {} } },
  42. props: {},
  43. ...(withFace ? { projections: { faceOf: (key: string) => cells.get(key) ?? absent } } : {}),
  44. })
  45. const host: SlotRendererHost = {
  46. subscribe: () => () => {},
  47. getVersion: () => 0,
  48. entriesOf: key => key === 'root' ? [rootEntry] : sessionEntries,
  49. specOf: key => key === 'k.session' ? { kind: 'single', scope: 'session' } : undefined,
  50. isLive: () => true,
  51. storeOf: () => undefined,
  52. sessions: {
  53. list: observable<unknown>({ ids: [] }),
  54. provideInfo: provide,
  55. },
  56. workspaces: { list: observable<unknown>({ items: [] }) },
  57. }
  58. return {
  59. host,
  60. cells,
  61. // Same driver surface as before the atomic provide source: set(id)
  62. // publishes the resolved bundle (or the absent projection) through it.
  63. current: { set: (id: string | undefined) => { provide.set(id === undefined ? absentInfo : info(id)) } },
  64. dropFace: () => { withFace = false },
  65. registerSession: (entry: StoredEntry) => { sessionEntries.push(entry) },
  66. }
  67. }
  68. describe('useProjection standard-kit delivery', () => {
  69. it('reads the projected value through the kit, undefined for unresolved keys, and follows live changes', () => {
  70. const h = makeHost()
  71. const cell = observable<unknown>({ marks: ['a'] })
  72. h.cells.set('test/marks', cell)
  73. const reads: Record<string, unknown>[] = []
  74. h.registerSession({
  75. component: (props: { useProjection: UseProjectionProp }) => {
  76. reads.push({
  77. marks: props.useProjection('test/marks'),
  78. ghost: props.useProjection('test/ghost'),
  79. })
  80. return null
  81. },
  82. options: {},
  83. })
  84. render(<>{createSlotRenderer().renderRoot(h.host, {})}</>)
  85. act(() => { h.current.set('s1') })
  86. expect(reads.at(-1)).toEqual({ marks: { marks: ['a'] }, ghost: undefined })
  87. // Live change re-renders with the new whole value.
  88. act(() => { cell.set({ marks: ['a', 'b'] }) })
  89. expect(reads.at(-1)).toEqual({ marks: { marks: ['a', 'b'] }, ghost: undefined })
  90. })
  91. it('runs the selector overload over the whole value (and over undefined when absent)', () => {
  92. const h = makeHost()
  93. h.cells.set('test/marks', observable<unknown>({ marks: ['x', 'y'] }))
  94. const reads: unknown[] = []
  95. h.registerSession({
  96. component: (props: { useProjection: UseProjectionProp }) => {
  97. reads.push(props.useProjection('test/marks', v => (v as { marks: string[] } | undefined)?.marks.length ?? -1))
  98. reads.push(props.useProjection('test/ghost', v => (v === undefined ? 'absent' : 'present')))
  99. return null
  100. },
  101. options: {},
  102. })
  103. render(<>{createSlotRenderer().renderRoot(h.host, {})}</>)
  104. act(() => { h.current.set('s1') })
  105. expect(reads.slice(-2)).toEqual([2, 'absent'])
  106. })
  107. it('treats a bundle without the projections face as all-absent (capability absence)', () => {
  108. const h = makeHost()
  109. h.cells.set('test/marks', observable<unknown>({ marks: ['a'] }))
  110. h.dropFace()
  111. const reads: unknown[] = []
  112. h.registerSession({
  113. component: (props: { useProjection: UseProjectionProp }) => {
  114. reads.push(props.useProjection('test/marks'))
  115. return null
  116. },
  117. options: {},
  118. })
  119. render(<>{createSlotRenderer().renderRoot(h.host, {})}</>)
  120. act(() => { h.current.set('s1') })
  121. expect(reads.at(-1)).toBeUndefined()
  122. })
  123. })