Files
deepseek-harness/packages/api/job-controller/tests/client-apply.client.spec.ts
T
Yichen Jiang b7ce934b29 refactor(job-controller): own the roster stream and the browser jobs service
- job.rows({ sessionId }) streams the jobs one session can see as whole-set
  frames: once on open, then after each coalesced burst of lifecycle
  commits; output appends never refresh it
- job.observe reads through visibleTo(sessionId) and carries JobView in its
  opened and status frames; the wire chunk is the registry's JobChunk
- the client installs ctx.jobs (ClientJobs over ClientJobsModel): watchRows
  and observe are reference-counted streams, rows drop with the last
  watcher or a terminal failure, observations keep their render tail
- the controller no longer needs the agent registry
2026-09-03 17:00:49 +08:00

74 lines
2.9 KiB
TypeScript

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, JobRowsFrame } from '../src/types.ts'
const contexts = new Set<Context>()
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[]; rowsCalls: unknown[] }> {
const ctx = new Context()
contexts.add(ctx)
const observeCalls: unknown[] = []
const rowsCalls: 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 streams never yield; opening and releasing must leave no state behind.
rows: (request: unknown) => {
rowsCalls.push(request)
return (async function* (): AsyncGenerator<JobRowsFrame> {})()
},
observe: (request: unknown) => {
observeCalls.push(request)
return (async function* (): AsyncGenerator<JobObserveFrame> {})()
},
}
ctx.reflect.provide('remote', {
$stream: <Item>(options: RemoteStreamOptions<Item>) => new RemoteStream(connection, options),
job,
})
ctx.reflect.provide('remote.job', job)
await ctx.plugin(JobClient)
return { ctx, observeCalls, rowsCalls }
}
async function flush(): Promise<void> {
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.jobs over the captured job namespace', async () => {
const { ctx, observeCalls, rowsCalls } = await mount()
expect(ctx.jobs).toBeDefined()
const stopRows = ctx.jobs.watchRows('session-1' as SessionId)
const stopObserve = ctx.jobs.observe('session-1' as SessionId, 'bash-1' as never)
await flush()
expect(rowsCalls).toEqual([{ sessionId: 'session-1' }])
expect(observeCalls).toEqual([{ sessionId: 'session-1', jobId: 'bash-1' }])
stopRows()
stopObserve()
await flush()
expect(ctx.jobs.state.getSnapshot()).toEqual({ rows: {}, observed: {} })
})
})