plugin.client.spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Context } from '@deepseek-ai/cordis'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { TestRemote } from '@deepseek-ai/dsh-client-test-runtime'
  4. import { apply, inject } from '../src/client/index.ts'
  5. import { SettingsSchemaService } from '../src/client/schema.ts'
  6. import { SettingsScopeBinder } from '../src/client/settings-scope.ts'
  7. function bench() {
  8. const describeCall = vi.fn().mockResolvedValue({
  9. ok: true, value: { writable: true, hasDocument: true, namespaces: [] },
  10. })
  11. const ctx = new Context()
  12. ctx.provide('connection', { api: {}, isLoopback: true } as never)
  13. const remote = new TestRemote(ctx, { settings: { describe: describeCall } })
  14. return { ctx, describeCall, remote, fiber: ctx.plugin({ inject: [...inject], apply }) }
  15. }
  16. describe('settings domain base plugin', () => {
  17. it('mounts the scope service under settingsScope and reads once eagerly', async () => {
  18. const { ctx, describeCall, fiber } = bench()
  19. await fiber.await()
  20. expect(ctx.get('settingsScope')).toBeInstanceOf(SettingsScopeBinder)
  21. expect(ctx.get('settingsSchema')).toBeInstanceOf(SettingsSchemaService)
  22. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  23. })
  24. it('refreshes the mirror on document commits and connection resets, once each', async () => {
  25. const { ctx, describeCall, remote, fiber } = bench()
  26. await fiber.await()
  27. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  28. remote.emit('settings/document-updated', ['ui-test', 0])
  29. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(2) })
  30. ctx.emit('connection/reset')
  31. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(3) })
  32. })
  33. it('fiber disposal retires the service and its invalidation subscriptions', async () => {
  34. const { ctx, describeCall, remote, fiber } = bench()
  35. await fiber.await()
  36. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  37. await fiber.dispose()
  38. expect(ctx.get('settingsScope')).toBeUndefined()
  39. expect(ctx.get('settingsSchema')).toBeUndefined()
  40. remote.emit('settings/document-updated', ['ui-test', 0])
  41. ctx.emit('connection/reset')
  42. await Promise.resolve()
  43. expect(describeCall).toHaveBeenCalledTimes(1)
  44. })
  45. })