plugin.client.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * The settings domain base plugin's own mounting behavior: it stands up
  3. * `ctx.settingsScope` over one shared describe mirror, keeps that mirror
  4. * fresh on settings-document and connection-reset invalidations, and retires
  5. * both the service and the subscriptions with its fiber.
  6. */
  7. import { Context } from '@deepseek-ai/cordis'
  8. import { describe, expect, it, vi } from 'vitest'
  9. import { TestRemote } from '@deepseek-ai/dsh-client-test-runtime'
  10. import { apply, inject, SettingsScopeBinder } from '../src/client/index.ts'
  11. /** Boot the browser half over a fake loopback connection and test remote. */
  12. function bench() {
  13. const describeCall = vi.fn().mockResolvedValue({
  14. rpcId: 'plugin-bench' as never,
  15. result: { ok: true, value: { writable: true, hasDocument: true, namespaces: [] } },
  16. })
  17. const ctx = new Context()
  18. ctx.provide('connection', {
  19. api: { settings: { describe: describeCall } },
  20. isLoopback: true,
  21. } as never)
  22. new TestRemote(ctx)
  23. return { ctx, describeCall, fiber: ctx.plugin({ inject: [...inject], apply }) }
  24. }
  25. describe('settings domain base plugin', () => {
  26. it('mounts the scope service under settingsScope and reads once eagerly', async () => {
  27. const { ctx, describeCall, fiber } = bench()
  28. await fiber.await()
  29. expect(ctx.get('settingsScope')).toBeInstanceOf(SettingsScopeBinder)
  30. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  31. })
  32. it('refreshes the mirror on document commits and connection resets, once each', async () => {
  33. const { ctx, describeCall, fiber } = bench()
  34. await fiber.await()
  35. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  36. ctx.remote.$dispatch('settings/document-updated', ['ui-test', 0])
  37. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(2) })
  38. ctx.emit('connection/reset')
  39. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(3) })
  40. })
  41. it('fiber disposal retires the service and its invalidation subscriptions', async () => {
  42. const { ctx, describeCall, fiber } = bench()
  43. await fiber.await()
  44. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  45. await fiber.dispose()
  46. expect(ctx.get('settingsScope')).toBeUndefined()
  47. ctx.remote.$dispatch('settings/document-updated', ['ui-test', 0])
  48. ctx.emit('connection/reset')
  49. await Promise.resolve()
  50. expect(describeCall).toHaveBeenCalledTimes(1)
  51. })
  52. })