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. import { apply as hostApply } from '../src/index.ts'
  8. function bench() {
  9. const describeCall = vi.fn().mockResolvedValue({
  10. ok: true, value: { writable: true, hasDocument: true, namespaces: [] },
  11. })
  12. const ctx = new Context()
  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('keeps the host Loader entry inert', () => {
  18. expect(hostApply).not.toThrow()
  19. })
  20. it('mounts the scope service under settingsScope and reads once eagerly', async () => {
  21. const { ctx, describeCall, fiber } = bench()
  22. await fiber.await()
  23. expect(ctx.get('settingsScope')).toBeInstanceOf(SettingsScopeBinder)
  24. expect(ctx.get('settingsSchema')).toBeInstanceOf(SettingsSchemaService)
  25. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  26. })
  27. it('refreshes the mirror on document commits and connection resets, once each', async () => {
  28. const { ctx, describeCall, remote, fiber } = bench()
  29. await fiber.await()
  30. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  31. remote.emit('settings/document-updated', ['ui-test', 0])
  32. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(2) })
  33. ctx.emit('connection/reset')
  34. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(3) })
  35. })
  36. it('fiber disposal retires the service and its invalidation subscriptions', async () => {
  37. const { ctx, describeCall, remote, fiber } = bench()
  38. await fiber.await()
  39. await vi.waitFor(() => { expect(describeCall).toHaveBeenCalledTimes(1) })
  40. await fiber.dispose()
  41. expect(ctx.get('settingsScope')).toBeUndefined()
  42. expect(ctx.get('settingsSchema')).toBeUndefined()
  43. remote.emit('settings/document-updated', ['ui-test', 0])
  44. ctx.emit('connection/reset')
  45. await Promise.resolve()
  46. expect(describeCall).toHaveBeenCalledTimes(1)
  47. })
  48. })