|
| 1 | +import { updateAgentApi, getAgentApi } from "../shared/elevenlabs-api"; |
| 2 | +import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js"; |
| 3 | + |
| 4 | +describe("Agent versioning and branch support", () => { |
| 5 | + function makeMockClient(opts: { |
| 6 | + versionId?: string; |
| 7 | + branchId?: string; |
| 8 | + mainBranchId?: string; |
| 9 | + } = {}) { |
| 10 | + const create = jest.fn().mockResolvedValue({ agentId: "agent_ver_123" }); |
| 11 | + const update = jest.fn().mockResolvedValue({ |
| 12 | + agentId: "agent_ver_123", |
| 13 | + versionId: opts.versionId ?? "ver_abc", |
| 14 | + branchId: opts.branchId ?? "branch_main", |
| 15 | + }); |
| 16 | + const get = jest.fn().mockResolvedValue({ |
| 17 | + agentId: "agent_ver_123", |
| 18 | + name: "Test Agent", |
| 19 | + versionId: opts.versionId ?? "ver_abc", |
| 20 | + branchId: opts.branchId ?? "branch_main", |
| 21 | + mainBranchId: opts.mainBranchId ?? "branch_main", |
| 22 | + conversationConfig: { |
| 23 | + agent: { prompt: { prompt: "Hello", temperature: 0.5 } }, |
| 24 | + }, |
| 25 | + platformSettings: {}, |
| 26 | + tags: [], |
| 27 | + }); |
| 28 | + |
| 29 | + return { |
| 30 | + conversationalAi: { |
| 31 | + agents: { create, update, get }, |
| 32 | + }, |
| 33 | + } as unknown as ElevenLabsClient; |
| 34 | + } |
| 35 | + |
| 36 | + describe("updateAgentApi", () => { |
| 37 | + it("should pass versionDescription to the API", async () => { |
| 38 | + const client = makeMockClient(); |
| 39 | + const conversationConfig = { |
| 40 | + agent: { prompt: { prompt: "hi", temperature: 0 } }, |
| 41 | + } as unknown as Record<string, unknown>; |
| 42 | + |
| 43 | + await updateAgentApi( |
| 44 | + client, |
| 45 | + "agent_ver_123", |
| 46 | + "Test Agent", |
| 47 | + conversationConfig, |
| 48 | + undefined, |
| 49 | + undefined, |
| 50 | + ["tag"], |
| 51 | + "release v1.0" |
| 52 | + ); |
| 53 | + |
| 54 | + expect(client.conversationalAi.agents.update).toHaveBeenCalledTimes(1); |
| 55 | + const [agentId, payload] = ( |
| 56 | + client.conversationalAi.agents.update as jest.Mock |
| 57 | + ).mock.calls[0]; |
| 58 | + |
| 59 | + expect(agentId).toBe("agent_ver_123"); |
| 60 | + expect(payload).toEqual( |
| 61 | + expect.objectContaining({ |
| 62 | + versionDescription: "release v1.0", |
| 63 | + }) |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should not include versionDescription when not provided", async () => { |
| 68 | + const client = makeMockClient(); |
| 69 | + const conversationConfig = { |
| 70 | + agent: { prompt: { prompt: "hi", temperature: 0 } }, |
| 71 | + } as unknown as Record<string, unknown>; |
| 72 | + |
| 73 | + await updateAgentApi( |
| 74 | + client, |
| 75 | + "agent_ver_123", |
| 76 | + "Test Agent", |
| 77 | + conversationConfig, |
| 78 | + undefined, |
| 79 | + undefined, |
| 80 | + [] |
| 81 | + ); |
| 82 | + |
| 83 | + const [, payload] = ( |
| 84 | + client.conversationalAi.agents.update as jest.Mock |
| 85 | + ).mock.calls[0]; |
| 86 | + |
| 87 | + expect(payload.versionDescription).toBeUndefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should return versionId and branchId from API response", async () => { |
| 91 | + const client = makeMockClient({ |
| 92 | + versionId: "ver_xyz", |
| 93 | + branchId: "branch_feat", |
| 94 | + }); |
| 95 | + const conversationConfig = { |
| 96 | + agent: { prompt: { prompt: "hi", temperature: 0 } }, |
| 97 | + } as unknown as Record<string, unknown>; |
| 98 | + |
| 99 | + const result = await updateAgentApi( |
| 100 | + client, |
| 101 | + "agent_ver_123", |
| 102 | + "Test Agent", |
| 103 | + conversationConfig, |
| 104 | + undefined, |
| 105 | + undefined, |
| 106 | + [], |
| 107 | + "my version" |
| 108 | + ); |
| 109 | + |
| 110 | + expect(result).toEqual({ |
| 111 | + agentId: "agent_ver_123", |
| 112 | + versionId: "ver_xyz", |
| 113 | + branchId: "branch_feat", |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should handle missing versionId/branchId in response", async () => { |
| 118 | + const client = makeMockClient(); |
| 119 | + // Override update to return response without version fields |
| 120 | + (client.conversationalAi.agents.update as jest.Mock).mockResolvedValue({ |
| 121 | + agentId: "agent_ver_123", |
| 122 | + }); |
| 123 | + |
| 124 | + const conversationConfig = { |
| 125 | + agent: { prompt: { prompt: "hi", temperature: 0 } }, |
| 126 | + } as unknown as Record<string, unknown>; |
| 127 | + |
| 128 | + const result = await updateAgentApi( |
| 129 | + client, |
| 130 | + "agent_ver_123", |
| 131 | + "Test Agent", |
| 132 | + conversationConfig |
| 133 | + ); |
| 134 | + |
| 135 | + expect(result).toEqual({ |
| 136 | + agentId: "agent_ver_123", |
| 137 | + versionId: undefined, |
| 138 | + branchId: undefined, |
| 139 | + }); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe("getAgentApi", () => { |
| 144 | + it("should return version_id, branch_id, main_branch_id in snake_case", async () => { |
| 145 | + const client = makeMockClient({ |
| 146 | + versionId: "ver_999", |
| 147 | + branchId: "branch_dev", |
| 148 | + mainBranchId: "branch_main", |
| 149 | + }); |
| 150 | + |
| 151 | + const response = await getAgentApi(client, "agent_ver_123"); |
| 152 | + const typed = response as Record<string, unknown>; |
| 153 | + |
| 154 | + // getAgentApi converts to snake_case via toSnakeCaseKeys |
| 155 | + expect(typed.version_id).toBe("ver_999"); |
| 156 | + expect(typed.branch_id).toBe("branch_dev"); |
| 157 | + expect(typed.main_branch_id).toBe("branch_main"); |
| 158 | + }); |
| 159 | + |
| 160 | + it("should handle agent without version/branch fields", async () => { |
| 161 | + const client = makeMockClient(); |
| 162 | + // Override get to return response without version fields |
| 163 | + (client.conversationalAi.agents.get as jest.Mock).mockResolvedValue({ |
| 164 | + agentId: "agent_ver_123", |
| 165 | + name: "Test Agent", |
| 166 | + conversationConfig: {}, |
| 167 | + platformSettings: {}, |
| 168 | + tags: [], |
| 169 | + }); |
| 170 | + |
| 171 | + const response = await getAgentApi(client, "agent_ver_123"); |
| 172 | + const typed = response as Record<string, unknown>; |
| 173 | + |
| 174 | + expect(typed.agent_id).toBe("agent_ver_123"); |
| 175 | + // Fields should simply be absent |
| 176 | + expect(typed.version_id).toBeUndefined(); |
| 177 | + expect(typed.branch_id).toBeUndefined(); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe("Versioning in push/pull agents.json persistence", () => { |
| 183 | + let tempDir: string; |
| 184 | + let agentsConfigPath: string; |
| 185 | + |
| 186 | + beforeEach(async () => { |
| 187 | + const fs = await import("fs-extra"); |
| 188 | + const path = await import("path"); |
| 189 | + const { tmpdir } = await import("os"); |
| 190 | + tempDir = await fs.mkdtemp(path.join(tmpdir(), "test-versioning-")); |
| 191 | + agentsConfigPath = path.join(tempDir, "agents.json"); |
| 192 | + }); |
| 193 | + |
| 194 | + afterEach(async () => { |
| 195 | + const fs = await import("fs-extra"); |
| 196 | + await fs.remove(tempDir); |
| 197 | + }); |
| 198 | + |
| 199 | + it("should store version_id and branch_id in agents.json structure", async () => { |
| 200 | + const { writeConfig, readConfig } = await import("../shared/utils"); |
| 201 | + |
| 202 | + // Simulate what push-impl and pull-impl do: write agents.json with version/branch |
| 203 | + const agentsConfig = { |
| 204 | + agents: [ |
| 205 | + { |
| 206 | + config: "agent_configs/My-Agent.json", |
| 207 | + id: "agent_123", |
| 208 | + version_id: "ver_abc", |
| 209 | + branch_id: "branch_main", |
| 210 | + }, |
| 211 | + { |
| 212 | + config: "agent_configs/Another-Agent.json", |
| 213 | + id: "agent_456", |
| 214 | + // no version/branch - should be fine |
| 215 | + }, |
| 216 | + ], |
| 217 | + }; |
| 218 | + |
| 219 | + await writeConfig(agentsConfigPath, agentsConfig); |
| 220 | + |
| 221 | + const loaded = await readConfig(agentsConfigPath) as { |
| 222 | + agents: Array<{ |
| 223 | + config: string; |
| 224 | + id?: string; |
| 225 | + version_id?: string; |
| 226 | + branch_id?: string; |
| 227 | + }>; |
| 228 | + }; |
| 229 | + |
| 230 | + expect(loaded.agents[0].version_id).toBe("ver_abc"); |
| 231 | + expect(loaded.agents[0].branch_id).toBe("branch_main"); |
| 232 | + expect(loaded.agents[1].version_id).toBeUndefined(); |
| 233 | + expect(loaded.agents[1].branch_id).toBeUndefined(); |
| 234 | + }); |
| 235 | + |
| 236 | + it("should update version_id and branch_id on subsequent pushes", async () => { |
| 237 | + const { writeConfig, readConfig } = await import("../shared/utils"); |
| 238 | + |
| 239 | + // Initial state |
| 240 | + const agentsConfig = { |
| 241 | + agents: [ |
| 242 | + { |
| 243 | + config: "agent_configs/My-Agent.json", |
| 244 | + id: "agent_123", |
| 245 | + version_id: "ver_1", |
| 246 | + branch_id: "branch_main", |
| 247 | + }, |
| 248 | + ], |
| 249 | + }; |
| 250 | + |
| 251 | + await writeConfig(agentsConfigPath, agentsConfig); |
| 252 | + |
| 253 | + // Simulate push updating version |
| 254 | + const loaded = await readConfig(agentsConfigPath) as { |
| 255 | + agents: Array<{ |
| 256 | + config: string; |
| 257 | + id?: string; |
| 258 | + version_id?: string; |
| 259 | + branch_id?: string; |
| 260 | + }>; |
| 261 | + }; |
| 262 | + |
| 263 | + loaded.agents[0].version_id = "ver_2"; |
| 264 | + await writeConfig(agentsConfigPath, loaded); |
| 265 | + |
| 266 | + const final = await readConfig(agentsConfigPath) as { |
| 267 | + agents: Array<{ |
| 268 | + config: string; |
| 269 | + id?: string; |
| 270 | + version_id?: string; |
| 271 | + branch_id?: string; |
| 272 | + }>; |
| 273 | + }; |
| 274 | + |
| 275 | + expect(final.agents[0].version_id).toBe("ver_2"); |
| 276 | + expect(final.agents[0].branch_id).toBe("branch_main"); |
| 277 | + }); |
| 278 | +}); |
0 commit comments