diff --git a/packages/sdk/server/tests/server.spec.ts b/packages/sdk/server/tests/server.spec.ts index 53def0be33..633ebf4079 100644 --- a/packages/sdk/server/tests/server.spec.ts +++ b/packages/sdk/server/tests/server.spec.ts @@ -7,7 +7,7 @@ import type { IncomingMessage, Server, ServerResponse } from 'node:http' import { mkdtemp, mkdir, realpath, rm } from 'node:fs/promises' import { join } from 'node:path' import { tmpdir } from 'node:os' -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest' import { Context } from '@deepseek-ai/cordis' import { scopeTarget } from '@deepseek-ai/dsh-scope' import { randomUUID } from 'node:crypto' @@ -38,6 +38,12 @@ class FakeTransport implements JsonRpcTransportPeer { const servers: Server[] = [] +function testContext(): Context { + const ctx = new Context() + onTestFinished(() => ctx.fiber.dispose()) + return ctx +} + afterEach(async () => { await Promise.all(servers.splice(0).map(server => new Promise(resolve => server.close(resolve)))) vi.unstubAllEnvs() @@ -301,11 +307,8 @@ describe('HarnessSdkJsonRpcServer', () => { const create = vi.fn(async (options: { sessionId: SessionId }) => String(options.sessionId) === 'main' ? mainHandle : otherHandle) const liveAgents = new Map([['main', mainAgent], ['other', otherAgent]]) - const ctx = { - on: vi.fn(() => () => undefined), - agents: { create, get: (id: SessionId) => liveAgents.get(String(id)) }, - get: () => undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { create, get: (id: SessionId) => liveAgents.get(String(id)) }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) // This isolated prompt test begins after the handshake boundary. ;(server as unknown as { initialized: boolean }).initialized = true @@ -337,11 +340,9 @@ describe('HarnessSdkJsonRpcServer', () => { height: 1, } const saveImages = vi.fn(async () => [ref]) - const ctx = { - on: vi.fn(() => () => undefined), - agents: { create: vi.fn(async () => handle), get: () => agent }, - get: (name: string) => name === 'attachments' ? { saveImages } : undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { create: vi.fn(async () => handle), get: () => agent }) + ctx.provide('attachments', { saveImages }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) // This isolated prompt test begins after the handshake boundary. ;(server as unknown as { initialized: boolean }).initialized = true @@ -366,11 +367,8 @@ describe('HarnessSdkJsonRpcServer', () => { const followup = vi.fn() const agent = ({ id: SessionId('image'), followup } satisfies Pick) as unknown as Agent const handle = { agent, dispose: vi.fn(() => Promise.resolve()) } - const ctx = { - on: vi.fn(() => () => undefined), - agents: { create: vi.fn(async () => handle), get: () => agent }, - get: () => undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { create: vi.fn(async () => handle), get: () => agent }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) // This isolated prompt test begins after the handshake boundary. ;(server as unknown as { initialized: boolean }).initialized = true @@ -394,14 +392,12 @@ describe('HarnessSdkJsonRpcServer', () => { }>>() const saveImages = vi.fn(() => admitted.promise) let live = true - const ctx = { - on: vi.fn(() => () => undefined), - agents: { - create: vi.fn(async () => handle), - get: () => live ? agent : undefined, - }, - get: (name: string) => name === 'attachments' ? { saveImages } : undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { + create: vi.fn(async () => handle), + get: () => live ? agent : undefined, + }) + ctx.provide('attachments', { saveImages }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) // This isolated prompt test begins after the handshake boundary. ;(server as unknown as { initialized: boolean }).initialized = true @@ -430,14 +426,11 @@ describe('HarnessSdkJsonRpcServer', () => { // The registry drops the agent after creation, modelling an agent-loop-only // reload that leaves the server's SessionRecord pointing at a detached agent. let live = true - const ctx = { - on: vi.fn(() => () => undefined), - agents: { - create: vi.fn(async () => handle), - get: (id: SessionId) => (live && String(id) === 'zombie' ? agent : undefined), - }, - get: () => undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { + create: vi.fn(async () => handle), + get: (id: SessionId) => (live && String(id) === 'zombie' ? agent : undefined), + }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) // This isolated prompt test begins after the handshake boundary. ;(server as unknown as { initialized: boolean }).initialized = true @@ -1220,11 +1213,8 @@ describe('HarnessSdkJsonRpcServer', () => { .mockReturnValueOnce(sharedCreation) .mockRejectedValueOnce(new Error('creation failed')) .mockResolvedValueOnce(retryHandle) - const ctx = { - on: vi.fn(() => () => undefined), - agents: { create, get: () => undefined }, - get: () => undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { create, get: () => undefined }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) as unknown as { getOrCreateSession(sessionId: string): Promise<{ handle: AgentHandle }> shutdown(): Promise> @@ -1286,11 +1276,8 @@ describe('HarnessSdkJsonRpcServer', () => { it('settles every teardown and aggregates multiple failures', async () => { const firstDispose = vi.fn(() => { throw new Error('first teardown failed') }) const secondDispose = vi.fn(() => Promise.reject(new Error('second teardown failed'))) - const ctx = { - on: vi.fn(() => () => undefined), - agents: { create: vi.fn(), get: () => undefined }, - get: () => undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { create: vi.fn(), get: () => undefined }) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) as unknown as { sessions: Map shutdown(): Promise> @@ -1312,13 +1299,12 @@ describe('HarnessSdkJsonRpcServer', () => { return () => { disposed.push(id) if (id === subscription) throw listenerFailure + return true } }) - const ctx = { - on, - agents: { create: vi.fn(), get: () => undefined }, - get: () => undefined, - } as unknown as Context + const ctx = testContext() + ctx.provide('agents', { create: vi.fn(), get: () => undefined }) + vi.spyOn(ctx, 'on').mockImplementation(on) const server = new HarnessSdkJsonRpcServer(ctx, new FakeTransport()) await expect(server.shutdown()).rejects.toBe(listenerFailure) diff --git a/python/sdk/tests/test_plan_input.py b/python/sdk/tests/test_plan_input.py index 938b9629f2..70b500cd08 100644 --- a/python/sdk/tests/test_plan_input.py +++ b/python/sdk/tests/test_plan_input.py @@ -27,7 +27,7 @@ import threading root = Path(sys.argv[1]) lock = threading.Lock() stop = threading.Event() -wait_id = None +session_id = None serial = 0 def send(frame): @@ -44,11 +44,18 @@ def ask(session_id): "intent": {"kind": "plan-review", "approve": "Approve"}}], }}) +def finish(): + # These cases isolate input handling from native plan turn outcomes. + send({"method": "session.query.completed", "params": { + "sessionId": session_id, "commandId": "plan-command", + "finalResponse": "", "finishReason": "completed", + }}) + def cancel(): while not stop.wait(0.01): if (root / "cancel").exists(): - send({"method": "user-question.cancelled", "params": {"requestId": "review-1"}}) - send({"id": wait_id, "result": {}}) + send({"method": "user-question.cancelled", "params": {"requestId": "review-" + str(serial)}}) + finish() return watcher = threading.Thread(target=cancel) @@ -65,15 +72,20 @@ try: if answers and answers[0].get("selected") == ["Keep planning"]: ask(session_id) else: - send({"id": wait_id, "result": {}}) + finish() elif method == "initialize": send({"id": frame["id"], "result": {"serverInfo": {"name": "plan-input-peer"}}}) elif method == "session/query": + session_id = frame["params"]["sessionId"] + for event_type, data in [ + ("command/run", {"commandId": "plan-command", "name": "plan"}), + ("command/done", {"commandId": "plan-command", "kind": "success"}), + ]: + send({"method": "session.event", "params": { + "sessionId": session_id, "event": {"type": event_type, "data": data}, + }}) send({"id": frame["id"], "result": {"kind": "command", "execution": { "commandId": "plan-command", "result": {"kind": "success", "text": "Plan mode entered"}}}}) - elif method == "session/wait": - wait_id = frame["id"] - session_id = frame["params"]["sessionId"] ask(session_id) elif method == "shutdown": (root / "shutdown").write_text("done") diff --git a/scripts/no-unknown-casts.baseline.json b/scripts/no-unknown-casts.baseline.json index e775fe254f..a874d2f38e 100644 --- a/scripts/no-unknown-casts.baseline.json +++ b/scripts/no-unknown-casts.baseline.json @@ -1452,19 +1452,11 @@ "6214651eb1154a364fe011aaf0c89b0a6474bb9a6c6be82d00a0b25d4d9479ed": 1 }, "packages/sdk/server/tests/server.spec.ts": { - "1305ff452c4a44d797b7183012d24a22dae0d8db51ee4104dd18a9d72471f1b5": 1, - "1e04616b7ebb8d39f6f8ebe3a37e818dc869ed192904956f4e4409c9403d26ea": 1, "2ebb3092eeca4a2ce280c148caf4c4dc665442292f278d3fe15d9b0471281491": 2, "43ac5cd0dcbc6818e678a5c15173052eaceb5125b1873dd4c2aa245dd9920bc5": 1, "51d0436f82db36e4f96258fd28bfbf1916f2c3c400e6c649144932fbbbc9dd30": 1, - "66dc9c6e95154cfee02c5e2f03371b36e5572efe6fbcf3c5b4adbb9440e59008": 1, "71fa45c63f1d2dc12e6a4da1120a9ca227ef1b1ffbce510cd0d8847d85f278c0": 9, - "80fa2ee81263d11fc1fafe6b7dbb608e8ed6e7c7e8e7fa3e726b600e82425294": 1, "82025e3228e5dab025f1c3434c0303baf066b7f40f30352764abc7374a693928": 1, - "926edbfe0199937da6ec9d119cfd16f6cbc674a242de8f433980da6e6ea8b13b": 1, - "9b52ca4ca1a86eb1d34576ca3c210d2e09677553e2a0b0e505675782034c147e": 1, - "9c3213cd8f09df05b9aceba2033b68f6b61c1abe2231167622ef3fa462d77424": 1, - "a03ef245e286d905868294bee2e81ac282cfa0a5161a1edd6d8b0aa167932fb6": 1, "ae0162b189937f84fb9a162eabbc7496f7e196c00e960aef4fe8e5b3e4b87d8d": 3, "d54d74835af1518a4723a7cc803efc0d4e8e337ebb80b813b6ef76bdb813be17": 1 },