|
1 | 1 | import { describe, expect, it } from 'bun:test'; |
| 2 | +import { RpcResultValidationError } from '../index.js'; |
2 | 3 | import { Agent } from '../sdk/agent.js'; |
3 | 4 | import { AutohandSDK } from '../sdk/index.js'; |
4 | 5 | import { RPCClient } from '../rpc/client.js'; |
@@ -293,3 +294,145 @@ describe('auto-mode control RPCs', () => { |
293 | 294 | await expect(agent.getAutomodeLog(params)).resolves.toEqual(log); |
294 | 295 | }); |
295 | 296 | }); |
| 297 | + |
| 298 | +describe('session control RPC result validation', () => { |
| 299 | + const malformedResults: Array<{ |
| 300 | + name: string; |
| 301 | + method: string; |
| 302 | + result: unknown; |
| 303 | + expectedPath: string; |
| 304 | + invoke: (client: RPCClient) => Promise<unknown>; |
| 305 | + }> = [ |
| 306 | + { |
| 307 | + name: 'reset results with a non-string session ID', |
| 308 | + method: 'autohand.reset', |
| 309 | + result: { sessionId: 42 }, |
| 310 | + expectedPath: '$.sessionId', |
| 311 | + invoke: (client) => client.reset(), |
| 312 | + }, |
| 313 | + { |
| 314 | + name: 'browser handoff creation results with a malformed URL', |
| 315 | + method: 'autohand.browserHandoff.create', |
| 316 | + result: { |
| 317 | + token: 'handoff-token', |
| 318 | + sessionId: 'session-browser', |
| 319 | + workspaceRoot: '/workspace', |
| 320 | + createdAt: '2026-07-20T00:00:00.000Z', |
| 321 | + expiresAt: '2026-07-20T00:10:00.000Z', |
| 322 | + url: 42, |
| 323 | + }, |
| 324 | + expectedPath: '$.url', |
| 325 | + invoke: (client) => client.createBrowserHandoff(), |
| 326 | + }, |
| 327 | + { |
| 328 | + name: 'browser handoff attachment results with a malformed optional count', |
| 329 | + method: 'autohand.browserHandoff.attach', |
| 330 | + result: { success: true, messageCount: 'three' }, |
| 331 | + expectedPath: '$.messageCount', |
| 332 | + invoke: (client) => client.attachBrowserHandoff({ token: 'handoff-token' }), |
| 333 | + }, |
| 334 | + { |
| 335 | + name: 'latest browser handoff results with a malformed success flag', |
| 336 | + method: 'autohand.browserHandoff.attachLatest', |
| 337 | + result: { success: 'yes' }, |
| 338 | + expectedPath: '$.success', |
| 339 | + invoke: (client) => client.attachLatestBrowserHandoff(), |
| 340 | + }, |
| 341 | + { |
| 342 | + name: 'auto-mode start results with a malformed optional error', |
| 343 | + method: 'autohand.automode.start', |
| 344 | + result: { success: false, error: 17 }, |
| 345 | + expectedPath: '$.error', |
| 346 | + invoke: (client) => client.startAutomode({ prompt: 'Ship the SDK' }), |
| 347 | + }, |
| 348 | + { |
| 349 | + name: 'auto-mode status results with an unknown nested status', |
| 350 | + method: 'autohand.automode.status', |
| 351 | + result: { |
| 352 | + active: true, |
| 353 | + paused: false, |
| 354 | + state: { |
| 355 | + sessionId: 'automode-session', |
| 356 | + status: 'queued', |
| 357 | + currentIteration: 1, |
| 358 | + maxIterations: 10, |
| 359 | + filesCreated: 0, |
| 360 | + filesModified: 1, |
| 361 | + }, |
| 362 | + }, |
| 363 | + expectedPath: '$.state.status', |
| 364 | + invoke: (client) => client.getAutomodeStatus(), |
| 365 | + }, |
| 366 | + { |
| 367 | + name: 'auto-mode pause results with a malformed success flag', |
| 368 | + method: 'autohand.automode.pause', |
| 369 | + result: { success: 1 }, |
| 370 | + expectedPath: '$.success', |
| 371 | + invoke: (client) => client.pauseAutomode(), |
| 372 | + }, |
| 373 | + { |
| 374 | + name: 'auto-mode resume results with a malformed optional error', |
| 375 | + method: 'autohand.automode.resume', |
| 376 | + result: { success: false, error: { message: 'not paused' } }, |
| 377 | + expectedPath: '$.error', |
| 378 | + invoke: (client) => client.resumeAutomode(), |
| 379 | + }, |
| 380 | + { |
| 381 | + name: 'auto-mode cancellation results that are not objects', |
| 382 | + method: 'autohand.automode.cancel', |
| 383 | + result: null, |
| 384 | + expectedPath: '$', |
| 385 | + invoke: (client) => client.cancelAutomode(), |
| 386 | + }, |
| 387 | + { |
| 388 | + name: 'auto-mode log results with a malformed nested checkpoint', |
| 389 | + method: 'autohand.automode.getLog', |
| 390 | + result: { |
| 391 | + success: true, |
| 392 | + iterations: [{ |
| 393 | + iteration: 1, |
| 394 | + timestamp: '2026-07-20T00:01:00.000Z', |
| 395 | + actions: ['edited src/index.ts'], |
| 396 | + checkpoint: { commit: 17, message: 'iteration 1' }, |
| 397 | + }], |
| 398 | + }, |
| 399 | + expectedPath: '$.iterations[0].checkpoint.commit', |
| 400 | + invoke: (client) => client.getAutomodeLog({ limit: 1 }), |
| 401 | + }, |
| 402 | + ]; |
| 403 | + |
| 404 | + for (const malformed of malformedResults) { |
| 405 | + it(`rejects ${malformed.name}`, async () => { |
| 406 | + const client = new RPCClient(); |
| 407 | + getTransport(client).request = async () => malformed.result; |
| 408 | + |
| 409 | + try { |
| 410 | + await malformed.invoke(client); |
| 411 | + throw new Error('Expected the malformed RPC result to be rejected'); |
| 412 | + } catch (error) { |
| 413 | + expect(error).toBeInstanceOf(RpcResultValidationError); |
| 414 | + if (error instanceof RpcResultValidationError) { |
| 415 | + expect(error.method).toBe(malformed.method); |
| 416 | + expect(error.path).toBe(malformed.expectedPath); |
| 417 | + } |
| 418 | + } |
| 419 | + }); |
| 420 | + } |
| 421 | + |
| 422 | + it('accepts omitted optional result fields', async () => { |
| 423 | + const client = new RPCClient(); |
| 424 | + const results = [ |
| 425 | + { success: false }, |
| 426 | + { active: false, paused: false }, |
| 427 | + ]; |
| 428 | + getTransport(client).request = async () => results.shift(); |
| 429 | + |
| 430 | + await expect( |
| 431 | + client.attachBrowserHandoff({ token: 'missing-handoff' }) |
| 432 | + ).resolves.toEqual({ success: false }); |
| 433 | + await expect(client.getAutomodeStatus()).resolves.toEqual({ |
| 434 | + active: false, |
| 435 | + paused: false, |
| 436 | + }); |
| 437 | + }); |
| 438 | +}); |
0 commit comments