stale-authorization.spec.tsx 5.0 KB

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