From b4b6c109e768f19e8af4b97f71bdeb617402f174 Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Wed, 6 May 2026 14:59:56 -0700 Subject: [PATCH] test(agent-sdk): add agent-reuse multi-session test --- .../tests/agent-reuse.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sdks/typescript-agent-sdk/tests/agent-reuse.test.ts diff --git a/sdks/typescript-agent-sdk/tests/agent-reuse.test.ts b/sdks/typescript-agent-sdk/tests/agent-reuse.test.ts new file mode 100644 index 00000000000..36736b8ada4 --- /dev/null +++ b/sdks/typescript-agent-sdk/tests/agent-reuse.test.ts @@ -0,0 +1,59 @@ +/** + * Validation #3 — Agent reuse across sessions. + * + * Creates ONE agent, then 3 sessions under it. Asserts: same agentId, + * distinct sessionIds, distinct VMs. + */ + +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { Agent } from "../src/index.js"; +import { MockProxy } from "./mock-proxy.js"; + +describe("agent reuse across sessions", () => { + let proxy: MockProxy; + let baseUrl: string; + + beforeEach(async () => { + proxy = new MockProxy(); + baseUrl = await proxy.start(); + }); + + afterEach(async () => { + await proxy.stop(); + }); + + it("creates 3 sessions under one agent with distinct IDs and distinct VMs", async () => { + const agent = await Agent.create({ + apiKey: "test-key", + baseUrl, + name: "shared", + model: { id: "test-model" }, + }); + + const sessions = await Promise.all([ + agent.createSession(), + agent.createSession(), + agent.createSession(), + ]); + + // All sessions share the agent ID. + for (const s of sessions) { + expect(s.agentId).toBe(agent.id); + } + + // Session IDs are unique. + const ids = sessions.map((s) => s.id); + expect(new Set(ids).size).toBe(3); + + // Mock proxy assigns each session a distinct VM ID. + const proxyAgent = proxy.agents.get(agent.id); + expect(proxyAgent).toBeDefined(); + const vmIds = [...proxyAgent!.sessions.values()].map((s) => s.vmId); + expect(new Set(vmIds).size).toBe(3); + + // listSessions reflects all 3. + const listed = await agent.listSessions(); + expect(listed.items.length).toBe(3); + expect(new Set(listed.items.map((i) => i.id))).toEqual(new Set(ids)); + }); +});