stale-authorization.spec.tsx 4.9 KB

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