scope.client.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Agent-scope primitive spec: the actx minted by createScope carries the
  3. * tag and the dispatch filter itself, so plain cordis dispatch with the actx
  4. * as subject routes by agent — same-agent tagged listeners receive,
  5. * foreign-agent ones are filtered out, untagged listeners hear everything,
  6. * and a subject-less root dispatch stays unfiltered. Scope-owned listeners
  7. * dispose with the fiber.
  8. */
  9. import { Context } from '@deepseek-ai/cordis'
  10. import { describe, expect, it } from 'vitest'
  11. import type { SessionId } from '@deepseek-ai/dsh-api-remotes/client'
  12. import { createScope, scopeOf } from '../src/client/scope.ts'
  13. const sid = (k: string): SessionId => k as SessionId
  14. declare module '@deepseek-ai/cordis' {
  15. interface Events {
  16. /**
  17. * Test-only routed probe event.
  18. * @param payload - marker payload.
  19. * @mode bail
  20. */
  21. 'test/scope-probe'(payload: { from: string }): true | undefined
  22. }
  23. }
  24. function bench() {
  25. const root = new Context()
  26. const a = createScope(root, sid('a'))
  27. const b = createScope(root, sid('b'))
  28. const seen: string[] = []
  29. const listen = (label: string, ctx: Context, answer?: true) => {
  30. ctx.on('test/scope-probe', (payload) => {
  31. seen.push(`${label}:${payload.from}`)
  32. return answer
  33. })
  34. }
  35. return { root, a, b, seen, listen }
  36. }
  37. describe('createScope', () => {
  38. it('tags the ctx (scopeOf) and leaves the root untagged', () => {
  39. const { root, a } = bench()
  40. expect(scopeOf(a.ctx)).toBe(sid('a'))
  41. expect(scopeOf(root)).toBeUndefined()
  42. })
  43. it('scoped dispatch reaches same-session and untagged listeners, never a foreign session', () => {
  44. const { root, a, b, seen, listen } = bench()
  45. listen('a', a.ctx)
  46. listen('b', b.ctx)
  47. listen('root', root)
  48. a.ctx.bail(a.ctx, 'test/scope-probe', { from: 'a' })
  49. expect(seen).toEqual(['a:a', 'root:a'])
  50. seen.length = 0
  51. b.ctx.emit(b.ctx, 'test/scope-probe', { from: 'b' })
  52. expect(seen).toEqual(['b:b', 'root:b'])
  53. })
  54. it('bail answers the first same-scope listener and skips filtered foreign ones', () => {
  55. const { a, b, listen } = bench()
  56. listen('b', b.ctx, true) // registered first, but foreign → filtered out
  57. expect(a.ctx.bail(a.ctx, 'test/scope-probe', { from: 'a' })).toBeUndefined()
  58. listen('a', a.ctx, true)
  59. expect(a.ctx.bail(a.ctx, 'test/scope-probe', { from: 'a' })).toBe(true)
  60. })
  61. it('a subject-less root dispatch is unfiltered (every listener hears it)', () => {
  62. const { root, a, b, seen, listen } = bench()
  63. listen('a', a.ctx)
  64. listen('b', b.ctx)
  65. listen('root', root)
  66. root.emit('test/scope-probe', { from: 'root' })
  67. expect(seen).toEqual(['a:root', 'b:root', 'root:root'])
  68. })
  69. it('fiber disposal removes scope-owned listeners', async () => {
  70. const { a, seen, listen } = bench()
  71. listen('a', a.ctx)
  72. await a.fiber.dispose()
  73. a.ctx.emit(a.ctx, 'test/scope-probe', { from: 'late' })
  74. expect(seen).toEqual([])
  75. })
  76. })