stale-authorization.spec.tsx 4.8 KB

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