| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /** Test-owned Remote face: `$on` subscriptions with an explicit test event driver. */
- import type { Context } from '@deepseek-ai/cordis'
- /**
- * Remote service test double for the forwarded-event path. Feature specs need
- * `ctx.remote.$on` to exist (their plugins inject `remote`) and need forwarded
- * Host events to reach those subscribers, but not the generated namespaces or
- * the wire — so this double implements subscription plus an explicit `emit`
- * driver available only on the concrete test object.
- *
- * `$mount` rejects: a spec that reaches a generated namespace through this
- * double has outgrown it and needs the real Client Remote service.
- *
- * One deliberate asymmetry with production: a throwing listener propagates out
- * of the emit instead of being contained and logged, so a spec cannot lean on
- * this double for the containment guarantee `$on` documents — assert that
- * against the real service.
- */
- export class TestRemote {
- private readonly subscriptions = new Map<string, Set<(...args: never[]) => void>>()
- /**
- * Register the double as `ctx.remote`.
- * @param ctx - the spec's root Context.
- */
- constructor(ctx: Context) {
- ctx.provide('remote', this)
- }
- /**
- * Deliver one forwarded host event to its subscribers, standing in for the
- * carrier that owns the frame sink.
- * @param event - forwarded host event name.
- * @param args - the Host argument list, verbatim.
- */
- emit(event: string, args: readonly unknown[]): void {
- const listeners = this.subscriptions.get(event)
- if (listeners === undefined) return
- for (const listener of [...listeners]) listener(...args as never[])
- }
- /**
- * Subscribe to one forwarded host event.
- * @param event - forwarded host event name.
- * @param listener - receives the Host argument list verbatim.
- * @returns disposer removing this subscription.
- */
- $on(event: string, listener: (...args: never[]) => void): () => void {
- const listeners = this.subscriptions.get(event) ?? new Set()
- this.subscriptions.set(event, listeners)
- listeners.add(listener)
- return () => { listeners.delete(listener) }
- }
- /**
- * Generated-namespace mount, unsupported by this double.
- * @returns never; always rejects.
- */
- $mount(): Promise<() => Promise<void>> {
- return Promise.reject(new Error('TestRemote: $mount needs the real Client Remote service'))
- }
- }
|