plugin.client.spec.ts 2.3 KB

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