stale-authorization.client.spec.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // @vitest-environment jsdom
  2. /**
  3. * A retained render binding dies with its entry. Re-registering the same key
  4. * creates a new binding rather than reviving the stale closure.
  5. */
  6. import { describe, expect, it } from 'vitest'
  7. import { act, render } from '@testing-library/react'
  8. import type { ReactNode } from 'react'
  9. import {
  10. StaleAuthorizationError, type SlotEntryDef, type SlotSpec, type StoredEntry,
  11. } from '@deepseek-ai/dsh-client-ui-slots'
  12. import type {
  13. RenderOpts, SlotRendererHost, SlotScopeAdapter, StandardSourceBinding,
  14. } from '@deepseek-ai/dsh-client-ui-renderer/client'
  15. import { createSlotRenderer } from '../src/client/scoped-slots.tsx'
  16. type RenderSlotFn = (key: string, owner: object, opts?: RenderOpts) => ReactNode
  17. type DeclaredSpec = SlotSpec<SlotEntryDef>
  18. /** Ledger-shaped fake: add/dispose maintain the live set the way the runtime ledger does. */
  19. function makeHost() {
  20. const entries = new Map<string, StoredEntry[]>()
  21. const versions = new Map<string, number>()
  22. const subs = new Map<string, Set<() => void>>()
  23. const live = new Set<StoredEntry>()
  24. const absentBinding: StandardSourceBinding = {
  25. key: undefined,
  26. hooks: {},
  27. keyedHooks: {},
  28. props: {},
  29. }
  30. const bindingSource = {
  31. getSnapshot: () => absentBinding,
  32. subscribe: () => () => {},
  33. }
  34. const sessionAdapter: SlotScopeAdapter = {
  35. current: bindingSource,
  36. resolve: () => undefined,
  37. }
  38. const bump = (key: string) => {
  39. versions.set(key, (versions.get(key) ?? 0) + 1)
  40. for (const fn of [...(subs.get(key) ?? [])]) fn()
  41. }
  42. const host: SlotRendererHost = {
  43. subscribe: (key, fn) => {
  44. const set = subs.get(key) ?? new Set()
  45. set.add(fn)
  46. subs.set(key, set)
  47. return () => { set.delete(fn) }
  48. },
  49. getVersion: key => versions.get(key) ?? 0,
  50. entriesOf: key => entries.get(key) ?? [],
  51. // Single-kind everywhere and no crashes in this suite: the projection is
  52. // the raw view and crash reports never fire.
  53. entriesOfSlot: key => entries.get(key) ?? [],
  54. reportEntryError: () => {},
  55. specOf: () => ({ kind: 'single', scope: 'root' }),
  56. isLive: entry => live.has(entry),
  57. storeOf: () => undefined,
  58. root: bindingSource,
  59. scopeRevision: { getSnapshot: () => 0, subscribe: () => () => {} },
  60. scope: () => sessionAdapter,
  61. }
  62. return {
  63. host,
  64. add: (key: string, entry: StoredEntry) => {
  65. entries.set(key, [...(entries.get(key) ?? []), entry])
  66. live.add(entry)
  67. bump(key)
  68. return () => {
  69. entries.set(key, (entries.get(key) ?? []).filter(e => e !== entry))
  70. live.delete(entry)
  71. bump(key)
  72. }
  73. },
  74. }
  75. }
  76. const CHILD: DeclaredSpec = { kind: 'single', scope: 'root' }
  77. /**
  78. * Mount a root entry that leaks its binding to the test, then render. The
  79. * returned dispose unmounts the view FIRST: an empty 'root' makes the live
  80. * root outlet rethrow its boot-order failure (fail-loud, covered in the
  81. * scoped-slots suite); the retained-closure scenario under test here is a
  82. * dead entry whose binding outlives the tree.
  83. */
  84. function mountCapturing(h: ReturnType<typeof makeHost>) {
  85. let captured: RenderSlotFn | undefined
  86. const entry: StoredEntry = {
  87. component: (props: { renderSlot: RenderSlotFn }) => {
  88. captured = props.renderSlot
  89. return null
  90. },
  91. options: {},
  92. children: { 'k.child': CHILD },
  93. }
  94. const disposeEntry = h.add('root', entry)
  95. const view = render(<>{createSlotRenderer().renderRoot(h.host, {})}</>)
  96. return {
  97. binding: captured!,
  98. entry,
  99. dispose: () => {
  100. view.unmount()
  101. disposeEntry()
  102. },
  103. }
  104. }
  105. describe('stale authorization', () => {
  106. it('a live binding renders; the same closure throws after its entry is disposed', () => {
  107. const h = makeHost()
  108. const { binding, dispose } = mountCapturing(h)
  109. expect(binding('k.child', {})).not.toBeUndefined() // live: returns an element
  110. act(() => { dispose() })
  111. expect(() => binding('k.child', {})).toThrow(StaleAuthorizationError)
  112. expect(() => binding('k.child', {})).toThrow(/disposed registration/)
  113. })
  114. it('stale check precedes the ownership check: a dead binding throws stale even for undeclared keys', () => {
  115. const h = makeHost()
  116. const { binding, dispose } = mountCapturing(h)
  117. act(() => { dispose() })
  118. // Were ownership checked first this would be SlotOwnershipError; the dead
  119. // entry must fail on liveness regardless of the key asked for.
  120. expect(() => binding('k.undeclared', {})).toThrow(StaleAuthorizationError)
  121. })
  122. it('HMR reload (same key, new entry) mints a fresh binding; the old one stays dead', () => {
  123. const h = makeHost()
  124. const first = mountCapturing(h)
  125. act(() => { first.dispose() })
  126. // Reload: a new entry object for the same slot key (new registration identity).
  127. let secondBinding: RenderSlotFn | undefined
  128. const secondEntry: StoredEntry = {
  129. component: (props: { renderSlot: RenderSlotFn }) => {
  130. secondBinding = props.renderSlot
  131. return null
  132. },
  133. options: {},
  134. children: { 'k.child': CHILD },
  135. }
  136. h.add('root', secondEntry)
  137. render(<>{createSlotRenderer().renderRoot(h.host, {})}</>)
  138. expect(secondBinding).toBeDefined()
  139. expect(secondBinding).not.toBe(first.binding) // new identity, no revival
  140. expect(secondBinding!('k.child', {})).not.toBeUndefined() // new binding is live
  141. expect(() => first.binding('k.child', {})).toThrow(StaleAuthorizationError) // old stays dead
  142. })
  143. })