plugin.client.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. const remote = new TestRemote(ctx, { settings: { describe: describeCall } })
  13. return { ctx, describeCall, remote, fiber: ctx.plugin({ inject: [...inject], apply }) }
  14. }
  15. describe('settings domain base plugin', () => {
  16. it('mounts the scope service under settingsScope and reads once eagerly', async () => {
  17. const { ctx, describeCall, fiber } = bench()
  18. await fiber.await()
  19. expect(ctx.get('settingsScope')).toBeInstanceOf(SettingsScopeBinder)
  20. expect(ctx.get('settingsSchema')).toBeInstanceOf(SettingsSchemaService)
  21. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  22. })
  23. it('refreshes the mirror on document commits and connection resets, once each', async () => {
  24. const { ctx, describeCall, remote, fiber } = bench()
  25. await fiber.await()
  26. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  27. remote.emit('settings/document-updated', ['ui-test', 0])
  28. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(2) })
  29. ctx.emit('connection/reset')
  30. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(3) })
  31. })
  32. it('fiber disposal retires the service and its invalidation subscriptions', async () => {
  33. const { ctx, describeCall, remote, fiber } = bench()
  34. await fiber.await()
  35. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  36. await fiber.dispose()
  37. expect(ctx.get('settingsScope')).toBeUndefined()
  38. expect(ctx.get('settingsSchema')).toBeUndefined()
  39. remote.emit('settings/document-updated', ['ui-test', 0])
  40. ctx.emit('connection/reset')
  41. await Promise.resolve()
  42. expect(describeCall).toHaveBeenCalledTimes(1)
  43. })
  44. })