|
| 1 | +import { AgentOps } from '../../src/sdk'; |
| 2 | +import { Agent } from '../../src/sdk/agent'; |
| 3 | +import type { AgentView } from '../../src/resources/agents'; |
| 4 | + |
| 5 | +// Mock the Agent class |
| 6 | +jest.mock('../../src/sdk/agent'); |
| 7 | + |
| 8 | +describe('AgentOps', () => { |
| 9 | + let mockClient: any; |
| 10 | + let agentOps: AgentOps; |
| 11 | + let mockAgentData: AgentView; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Create mock client |
| 15 | + mockClient = { |
| 16 | + agents: { |
| 17 | + create: jest.fn(), |
| 18 | + retrieve: jest.fn(), |
| 19 | + list: jest.fn(), |
| 20 | + }, |
| 21 | + } as any; |
| 22 | + |
| 23 | + agentOps = new AgentOps(mockClient); |
| 24 | + |
| 25 | + // Mock agent data |
| 26 | + mockAgentData = { |
| 27 | + id: 'agent-123', |
| 28 | + create_time_ms: Date.now(), |
| 29 | + name: 'test-agent', |
| 30 | + is_public: false, |
| 31 | + source: { |
| 32 | + type: 'npm', |
| 33 | + npm: { |
| 34 | + package_name: '@runloop/example-agent', |
| 35 | + }, |
| 36 | + }, |
| 37 | + }; |
| 38 | + |
| 39 | + // Mock Agent.create to return a mock Agent instance |
| 40 | + (Agent.create as jest.Mock).mockResolvedValue(new Agent(mockClient, 'agent-123')); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('createFromNpm', () => { |
| 44 | + it('should create an agent from npm package', async () => { |
| 45 | + await agentOps.createFromNpm({ |
| 46 | + name: 'test-agent', |
| 47 | + package_name: '@runloop/example-agent', |
| 48 | + }); |
| 49 | + |
| 50 | + expect(Agent.create).toHaveBeenCalledWith( |
| 51 | + mockClient, |
| 52 | + { |
| 53 | + name: 'test-agent', |
| 54 | + source: { |
| 55 | + type: 'npm', |
| 56 | + npm: { |
| 57 | + package_name: '@runloop/example-agent', |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + undefined, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should create an agent with all npm options', async () => { |
| 66 | + await agentOps.createFromNpm({ |
| 67 | + name: 'test-agent', |
| 68 | + package_name: '@runloop/example-agent', |
| 69 | + npm_version: '1.2.3', |
| 70 | + registry_url: 'https://registry.example.com', |
| 71 | + agent_setup: ['npm install', 'npm run setup'], |
| 72 | + }); |
| 73 | + |
| 74 | + expect(Agent.create).toHaveBeenCalledWith( |
| 75 | + mockClient, |
| 76 | + { |
| 77 | + name: 'test-agent', |
| 78 | + source: { |
| 79 | + type: 'npm', |
| 80 | + npm: { |
| 81 | + package_name: '@runloop/example-agent', |
| 82 | + npm_version: '1.2.3', |
| 83 | + registry_url: 'https://registry.example.com', |
| 84 | + agent_setup: ['npm install', 'npm run setup'], |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + undefined, |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('createFromPip', () => { |
| 94 | + it('should create an agent from pip package', async () => { |
| 95 | + await agentOps.createFromPip({ |
| 96 | + name: 'test-agent', |
| 97 | + package_name: 'runloop-example-agent', |
| 98 | + }); |
| 99 | + |
| 100 | + expect(Agent.create).toHaveBeenCalledWith( |
| 101 | + mockClient, |
| 102 | + { |
| 103 | + name: 'test-agent', |
| 104 | + source: { |
| 105 | + type: 'pip', |
| 106 | + pip: { |
| 107 | + package_name: 'runloop-example-agent', |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + undefined, |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should create an agent with all pip options', async () => { |
| 116 | + await agentOps.createFromPip({ |
| 117 | + name: 'test-agent', |
| 118 | + package_name: 'runloop-example-agent', |
| 119 | + pip_version: '1.2.3', |
| 120 | + registry_url: 'https://pypi.example.com', |
| 121 | + agent_setup: ['pip install extra-deps'], |
| 122 | + }); |
| 123 | + |
| 124 | + expect(Agent.create).toHaveBeenCalledWith( |
| 125 | + mockClient, |
| 126 | + { |
| 127 | + name: 'test-agent', |
| 128 | + source: { |
| 129 | + type: 'pip', |
| 130 | + pip: { |
| 131 | + package_name: 'runloop-example-agent', |
| 132 | + pip_version: '1.2.3', |
| 133 | + registry_url: 'https://pypi.example.com', |
| 134 | + agent_setup: ['pip install extra-deps'], |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + undefined, |
| 139 | + ); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('createFromGit', () => { |
| 144 | + it('should create an agent from git repository', async () => { |
| 145 | + await agentOps.createFromGit({ |
| 146 | + name: 'test-agent', |
| 147 | + repository: 'https://github.com/example/agent-repo', |
| 148 | + }); |
| 149 | + |
| 150 | + expect(Agent.create).toHaveBeenCalledWith( |
| 151 | + mockClient, |
| 152 | + { |
| 153 | + name: 'test-agent', |
| 154 | + source: { |
| 155 | + type: 'git', |
| 156 | + git: { |
| 157 | + repository: 'https://github.com/example/agent-repo', |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + undefined, |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should create an agent with all git options', async () => { |
| 166 | + await agentOps.createFromGit({ |
| 167 | + name: 'test-agent', |
| 168 | + repository: 'https://github.com/example/agent-repo', |
| 169 | + ref: 'develop', |
| 170 | + agent_setup: ['npm install', 'npm run build'], |
| 171 | + }); |
| 172 | + |
| 173 | + expect(Agent.create).toHaveBeenCalledWith( |
| 174 | + mockClient, |
| 175 | + { |
| 176 | + name: 'test-agent', |
| 177 | + source: { |
| 178 | + type: 'git', |
| 179 | + git: { |
| 180 | + repository: 'https://github.com/example/agent-repo', |
| 181 | + ref: 'develop', |
| 182 | + agent_setup: ['npm install', 'npm run build'], |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + undefined, |
| 187 | + ); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + describe('createFromObject', () => { |
| 192 | + it('should create an agent from object', async () => { |
| 193 | + await agentOps.createFromObject({ |
| 194 | + name: 'test-agent', |
| 195 | + object_id: 'obj_123', |
| 196 | + }); |
| 197 | + |
| 198 | + expect(Agent.create).toHaveBeenCalledWith( |
| 199 | + mockClient, |
| 200 | + { |
| 201 | + name: 'test-agent', |
| 202 | + source: { |
| 203 | + type: 'object', |
| 204 | + object: { |
| 205 | + object_id: 'obj_123', |
| 206 | + }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + undefined, |
| 210 | + ); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should create an agent with agent_setup', async () => { |
| 214 | + await agentOps.createFromObject({ |
| 215 | + name: 'test-agent', |
| 216 | + object_id: 'obj_123', |
| 217 | + agent_setup: ['chmod +x setup.sh', './setup.sh'], |
| 218 | + }); |
| 219 | + |
| 220 | + expect(Agent.create).toHaveBeenCalledWith( |
| 221 | + mockClient, |
| 222 | + { |
| 223 | + name: 'test-agent', |
| 224 | + source: { |
| 225 | + type: 'object', |
| 226 | + object: { |
| 227 | + object_id: 'obj_123', |
| 228 | + agent_setup: ['chmod +x setup.sh', './setup.sh'], |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + undefined, |
| 233 | + ); |
| 234 | + }); |
| 235 | + }); |
| 236 | +}); |
0 commit comments