remote.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /** Test-owned Remote face: `$on` subscriptions with an explicit test event driver. */
  2. import type { Context } from '@deepseek-ai/cordis'
  3. /**
  4. * Remote service test double for the forwarded-event path. Feature specs need
  5. * `ctx.remote.$on` to exist (their plugins inject `remote`) and need forwarded
  6. * Host events to reach those subscribers, but not the wire — so this double
  7. * implements subscription plus an explicit `emit` driver available only on the
  8. * concrete test object. A spec that also calls one namespace scripts it through
  9. * the constructor rather than reaching the real Client Remote service.
  10. *
  11. * `$mount` rejects: a spec that needs a real generated contribution installed —
  12. * codecs, descriptors, and the wire — has outgrown this double and needs the
  13. * real Client Remote service.
  14. *
  15. * One deliberate asymmetry with production: a throwing listener propagates out
  16. * of the emit instead of being contained and logged, so a spec cannot lean on
  17. * this double for the containment guarantee `$on` documents — assert that
  18. * against the real service.
  19. */
  20. export class TestRemote {
  21. private readonly subscriptions = new Map<string, Set<(...args: never[]) => void>>()
  22. /**
  23. * Register the double as `ctx.remote`, plus one service per scripted
  24. * namespace so a plugin injecting `remote.<name>` also unparks.
  25. * @param ctx - the spec's root Context.
  26. * @param namespaces - scripted namespace faces reached as `ctx.remote.<name>`.
  27. */
  28. constructor(ctx: Context, namespaces: Readonly<Record<string, object>> = {}) {
  29. for (const name of Object.keys(namespaces)) {
  30. // A namespace named after one of the double's own members would replace
  31. // it, and `$mount`'s rejection is the contract a spec relies on.
  32. if (name in TestRemote.prototype || name === 'subscriptions') {
  33. throw new TypeError(`TestRemote: scripted namespace "${name}" would shadow the double's own member`)
  34. }
  35. }
  36. Object.assign(this, namespaces)
  37. ctx.provide('remote', this)
  38. for (const [name, face] of Object.entries(namespaces)) ctx.provide(`remote.${name}`, face)
  39. }
  40. /**
  41. * Deliver one forwarded host event to its subscribers, standing in for the
  42. * carrier that owns the frame sink.
  43. * @param event - forwarded host event name.
  44. * @param args - the Host argument list, verbatim.
  45. */
  46. emit(event: string, args: readonly unknown[]): void {
  47. const listeners = this.subscriptions.get(event)
  48. if (listeners === undefined) return
  49. for (const listener of [...listeners]) listener(...args as never[])
  50. }
  51. /**
  52. * Subscribe to one forwarded host event.
  53. * @param event - forwarded host event name.
  54. * @param listener - receives the Host argument list verbatim.
  55. * @returns disposer removing this subscription.
  56. */
  57. $on(event: string, listener: (...args: never[]) => void): () => void {
  58. const listeners = this.subscriptions.get(event) ?? new Set()
  59. this.subscriptions.set(event, listeners)
  60. listeners.add(listener)
  61. return () => { listeners.delete(listener) }
  62. }
  63. /**
  64. * Generated-namespace mount, unsupported by this double.
  65. * @returns never; always rejects.
  66. */
  67. $mount(): Promise<() => Promise<void>> {
  68. return Promise.reject(new Error('TestRemote: $mount needs the real Client Remote service'))
  69. }
  70. }