scope.client.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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('routes same-id generations independently while keeping untagged listeners shared', async () => {
  39. const root = new Context()
  40. const previous = createScope(root, sid('same'))
  41. const replacement = createScope(root, sid('same'))
  42. const seen: string[] = []
  43. previous.ctx.on('test/scope-probe', () => { seen.push('old'); return undefined })
  44. replacement.ctx.on('test/scope-probe', () => { seen.push('new'); return undefined })
  45. root.on('test/scope-probe', () => { seen.push('root'); return undefined })
  46. replacement.ctx.emit(replacement.ctx, 'test/scope-probe', { from: 'new' })
  47. expect(seen).toEqual(['new', 'root'])
  48. await previous.fiber.dispose()
  49. seen.length = 0
  50. replacement.ctx.emit(replacement.ctx, 'test/scope-probe', { from: 'new' })
  51. expect(seen).toEqual(['new', 'root'])
  52. await root.fiber.dispose()
  53. })
  54. it('tags the ctx (scopeOf) and leaves the root untagged', () => {
  55. const { root, a } = bench()
  56. expect(scopeOf(a.ctx)).toBe(sid('a'))
  57. expect(scopeOf(root)).toBeUndefined()
  58. })
  59. it('scoped dispatch reaches same-session and untagged listeners, never a foreign session', () => {
  60. const { root, a, b, seen, listen } = bench()
  61. listen('a', a.ctx)
  62. listen('b', b.ctx)
  63. listen('root', root)
  64. a.ctx.bail(a.ctx, 'test/scope-probe', { from: 'a' })
  65. expect(seen).toEqual(['a:a', 'root:a'])
  66. seen.length = 0
  67. b.ctx.emit(b.ctx, 'test/scope-probe', { from: 'b' })
  68. expect(seen).toEqual(['b:b', 'root:b'])
  69. })
  70. it('bail answers the first same-scope listener and skips filtered foreign ones', () => {
  71. const { a, b, listen } = bench()
  72. listen('b', b.ctx, true) // registered first, but foreign → filtered out
  73. expect(a.ctx.bail(a.ctx, 'test/scope-probe', { from: 'a' })).toBeUndefined()
  74. listen('a', a.ctx, true)
  75. expect(a.ctx.bail(a.ctx, 'test/scope-probe', { from: 'a' })).toBe(true)
  76. })
  77. it('a subject-less root dispatch is unfiltered (every listener hears it)', () => {
  78. const { root, a, b, seen, listen } = bench()
  79. listen('a', a.ctx)
  80. listen('b', b.ctx)
  81. listen('root', root)
  82. root.emit('test/scope-probe', { from: 'root' })
  83. expect(seen).toEqual(['a:root', 'b:root', 'root:root'])
  84. })
  85. it('fiber disposal removes scope-owned listeners', async () => {
  86. const { a, seen, listen } = bench()
  87. listen('a', a.ctx)
  88. await a.fiber.dispose()
  89. a.ctx.emit(a.ctx, 'test/scope-probe', { from: 'late' })
  90. expect(seen).toEqual([])
  91. })
  92. })