browser-plugin.client.spec.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // @vitest-environment jsdom
  2. /**
  3. * ui-message-feedback browser half on a real cordis Context with fake slots/remote
  4. * faces: the plugin registers the feedback entry at
  5. * conversation.chat.assistant-actions, one controller per Session backs every
  6. * message in that Session, a reconnect refreshes only Sessions that were
  7. * already read, and registration plus controller disposal ride the plugin
  8. * fiber (HMR safety). The node half stays inert.
  9. */
  10. import { Context, Service } from '@deepseek-ai/cordis'
  11. import { afterEach, describe, expect, it } from 'vitest'
  12. import { cleanup } from '@testing-library/react'
  13. import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  14. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  15. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  16. import type { MessageId } from '@deepseek-ai/dsh-api-remotes/client'
  17. import type { MessageFeedbackItem, MessageFeedbackVersion } from '@deepseek-ai/dsh-message-feedback/types'
  18. import type { MessageFeedbackInjected } from '../src/client/slots.ts'
  19. import { apply, inject } from '../src/client/index.ts'
  20. import { apply as nodeApply } from '../src/index.ts'
  21. afterEach(cleanup)
  22. const sid = (k: string): SessionId => k as SessionId
  23. const MSG = 'm-1' as MessageId
  24. const seeded: MessageFeedbackItem = {
  25. messageId: MSG,
  26. rating: 'positive',
  27. version: 'v1' as MessageFeedbackVersion,
  28. createdAt: 1,
  29. updatedAt: 1,
  30. }
  31. /** Boot the plugin over fake faces; the Remote namespace records every call. */
  32. async function bench() {
  33. const ctx = new Context()
  34. const calls: { method: string; request: unknown }[] = []
  35. // The generated face wraps every business result in the carrier envelope.
  36. const carried = <T,>(value: T) => Promise.resolve({ ok: true as const, value })
  37. const messageFeedback = {
  38. list: (request: unknown) => {
  39. calls.push({ method: 'list', request })
  40. return carried({ ok: true as const, value: { items: [seeded] } })
  41. },
  42. put: (request: unknown) => {
  43. calls.push({ method: 'put', request })
  44. return carried({ ok: true as const, value: seeded })
  45. },
  46. delete: (request: unknown) => {
  47. calls.push({ method: 'delete', request })
  48. return carried({ ok: true as const, value: { absent: true as const } })
  49. },
  50. }
  51. class RemoteService extends Service {
  52. constructor(serviceCtx: Context) {
  53. super(serviceCtx, 'remote')
  54. }
  55. }
  56. new RemoteService(ctx)
  57. ctx.provide('remote.messageFeedback', messageFeedback)
  58. await ctx.plugin(SlotRegistry).await()
  59. ctx.slots.register({
  60. name: 'root',
  61. children: { 'conversation.chat.assistant-actions': { kind: 'list', scope: 'session' } },
  62. } as never, (() => null) as never)
  63. ctx.provide('locale', new LocaleRuntime(ctx))
  64. const fiber = ctx.plugin({ inject: [...inject], apply })
  65. return {
  66. ctx,
  67. fiber,
  68. calls,
  69. entry: () => {
  70. const entry = ctx.slots.entries('conversation.chat.assistant-actions')[0]
  71. if (entry === undefined) return undefined
  72. return {
  73. ...entry.options,
  74. locale: entry.locale,
  75. inject: entry.inject as unknown as ((sessionId: SessionId) => MessageFeedbackInjected) | undefined,
  76. }
  77. },
  78. }
  79. }
  80. describe('ui-message-feedback browser plugin', () => {
  81. it('registers the feedback entry with the documented id, order, and locale', async () => {
  82. const b = await bench()
  83. await b.fiber.await()
  84. expect(b.entry()).toMatchObject({ id: 'feedback', order: 10, locale: 'feedback' })
  85. expect(b.entry()?.inject).toBeTypeOf('function')
  86. })
  87. it('exposes the feedback hook plus the ensure/rate/clear verbs', async () => {
  88. const b = await bench()
  89. await b.fiber.await()
  90. const face = b.entry()!.inject!(sid('s1'))
  91. expect(face.hooks.feedback.getSnapshot()).toMatchObject({ status: 'cold' })
  92. expect(face.ensure).toBeTypeOf('function')
  93. expect(face.rate).toBeTypeOf('function')
  94. expect(face.clear).toBeTypeOf('function')
  95. })
  96. it('shares one controller across every message in the same Session', async () => {
  97. const b = await bench()
  98. await b.fiber.await()
  99. const first = b.entry()!.inject!(sid('s1'))
  100. const second = b.entry()!.inject!(sid('s1'))
  101. expect(first.hooks.feedback).toBe(second.hooks.feedback)
  102. await first.ensure()
  103. await second.ensure()
  104. expect(b.calls.filter(call => call.method === 'list')).toHaveLength(1)
  105. })
  106. it('keeps separate Sessions on separate controllers', async () => {
  107. const b = await bench()
  108. await b.fiber.await()
  109. const one = b.entry()!.inject!(sid('s1'))
  110. const two = b.entry()!.inject!(sid('s2'))
  111. expect(one.hooks.feedback).not.toBe(two.hooks.feedback)
  112. await one.ensure()
  113. await two.ensure()
  114. expect(b.calls.filter(call => call.method === 'list').map(call => call.request)).toEqual([
  115. { sessionId: 's1' },
  116. { sessionId: 's2' },
  117. ])
  118. })
  119. it('routes rate and clear to the Remote with the addressed message', async () => {
  120. const b = await bench()
  121. await b.fiber.await()
  122. const face = b.entry()!.inject!(sid('s1'))
  123. expect(await face.rate(MSG, 'negative', 'wrong answer')).toEqual({ ok: true })
  124. expect(await face.clear(MSG)).toEqual({ ok: true })
  125. expect(b.calls.filter(call => call.method === 'put')[0]?.request).toMatchObject({
  126. sessionId: 's1', messageId: MSG, rating: 'negative', note: 'wrong answer',
  127. })
  128. expect(b.calls.filter(call => call.method === 'delete')[0]?.request).toMatchObject({
  129. sessionId: 's1', messageId: MSG,
  130. })
  131. })
  132. it('routes toggle and clearNote to the controller', async () => {
  133. const b = await bench()
  134. await b.fiber.await()
  135. const face = b.entry()!.inject!(sid('s1'))
  136. expect(await face.toggle(MSG, 'negative')).toEqual({ ok: true })
  137. expect(await face.clearNote(MSG)).toEqual({ ok: true })
  138. // The seeded item is positive with no note, so a negative toggle replaces it
  139. // through put, and clearNote has nothing to drop and touches no wire.
  140. const puts = b.calls.filter(call => call.method === 'put').map(call => call.request)
  141. expect(puts).toHaveLength(1)
  142. expect(puts[0]).toMatchObject({ messageId: MSG, rating: 'negative' })
  143. })
  144. it('refreshes only Sessions already read when the connection resets', async () => {
  145. const b = await bench()
  146. await b.fiber.await()
  147. const warm = b.entry()!.inject!(sid('warm'))
  148. await warm.ensure()
  149. b.entry()!.inject!(sid('cold'))
  150. const before = b.calls.filter(call => call.method === 'list').length
  151. b.ctx.emit('connection/reset')
  152. await Promise.resolve()
  153. const reads = b.calls.filter(call => call.method === 'list')
  154. expect(reads).toHaveLength(before + 1)
  155. expect(reads.at(-1)?.request).toEqual({ sessionId: 'warm' })
  156. })
  157. it('withdraws the registration and disposes controllers with the plugin fiber', async () => {
  158. const b = await bench()
  159. await b.fiber.await()
  160. const face = b.entry()!.inject!(sid('s1'))
  161. await face.ensure()
  162. await b.fiber.dispose()
  163. expect(b.ctx.slots.entries('conversation.chat.assistant-actions')).toHaveLength(0)
  164. // A disposed controller refuses further mutations, so no request outlives the fiber.
  165. const before = b.calls.length
  166. expect(await face.rate(MSG, 'positive')).toMatchObject({ ok: false, error: { code: 'disposed' } })
  167. expect(b.calls).toHaveLength(before)
  168. })
  169. it('re-registers cleanly when the plugin is reloaded', async () => {
  170. const b = await bench()
  171. await b.fiber.await()
  172. await b.fiber.dispose()
  173. const reloaded = b.ctx.plugin({ inject: [...inject], apply })
  174. await reloaded.await()
  175. expect(b.ctx.slots.entries('conversation.chat.assistant-actions')).toHaveLength(1)
  176. expect(b.entry()).toMatchObject({ id: 'feedback' })
  177. })
  178. it('the node half applies without host-side behavior', () => {
  179. expect(() => { nodeApply() }).not.toThrow()
  180. })
  181. })