import { Context } from '@deepseek-ai/cordis' import type { ConnectionHandle } from '@deepseek-ai/dsh-client-connection/client' import { RemoteStream, type RemoteStreamOptions } from '@deepseek-ai/dsh-api-gateway/client' import type { SessionId } from '@deepseek-ai/dsh-session/types' import { afterEach, describe, expect, it } from 'vitest' import * as JobClient from '../src/client/index.ts' import type { JobObserveFrame } from '../src/types.ts' const contexts = new Set() afterEach(async () => { await Promise.all([...contexts].map(async (ctx) => { await ctx.fiber.dispose() })) contexts.clear() }) /** Mount the client half over a captured `remote.job` namespace and a real Gateway stream carrier. */ async function mount(): Promise<{ ctx: Context; observeCalls: unknown[] }> { const ctx = new Context() contexts.add(ctx) const observeCalls: unknown[] = [] const connection: ConnectionHandle = { isLoopback: true, generation: { getSnapshot: () => ({ id: 1, host: { home: '/home/fixture' } }), subscribe: () => () => {} }, state: { getSnapshot: () => 'connected' as const, subscribe: () => () => {} }, rpc: { call: () => Promise.reject(new Error('unexpected generic RPC call')) }, reconnect: () => {}, registerGenerationSource: () => () => {}, start: () => ({ stop: () => {} }), } const job = { // The fake stream never yields; opening and releasing must leave no view behind. observe: (request: unknown) => { observeCalls.push(request) return (async function* (): AsyncGenerator {})() }, } ctx.reflect.provide('remote', { $stream: (options: RemoteStreamOptions) => new RemoteStream(connection, options), job, }) ctx.reflect.provide('remote.job', job) await ctx.plugin(JobClient) return { ctx, observeCalls } } async function flush(): Promise { for (let index = 0; index < 12; index++) await Promise.resolve() } describe('Job Controller Client apply', () => { it('declares the Remote services it binds', () => { expect(JobClient.inject).toEqual(['remote', 'remote.job']) }) it('installs ctx.jobOutput over the captured job namespace', async () => { const { ctx, observeCalls } = await mount() expect(ctx.jobOutput).toBeDefined() const stop = ctx.jobOutput.observe('session-1' as SessionId, 'bash-1' as never) await flush() expect(observeCalls).toEqual([{ sessionId: 'session-1', jobId: 'bash-1' }]) stop() await flush() expect(ctx.jobOutput.state.getSnapshot().observed).toEqual({}) }) })