|
3 | 3 | import httpx |
4 | 4 |
|
5 | 5 | from insforge import InsforgeClient |
| 6 | +from insforge.functions.models import FunctionDeleteResponse |
6 | 7 | from insforge.functions.models import FunctionDetails |
| 8 | +from insforge.functions.models import FunctionMutationResponse |
7 | 9 | from insforge.functions.models import FunctionMetadata |
8 | 10 |
|
9 | 11 |
|
@@ -208,3 +210,158 @@ async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.R |
208 | 210 | assert isinstance(result, FunctionDetails) |
209 | 211 | assert result.slug == "hello-world" |
210 | 212 | assert result.code.startswith("export default") |
| 213 | + |
| 214 | + |
| 215 | +def test_create_function_uses_api_path_and_returns_typed_mutation_response() -> None: |
| 216 | + async def scenario() -> tuple[object, dict[str, object]]: |
| 217 | + captured: dict[str, object] = {} |
| 218 | + |
| 219 | + async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.Response: |
| 220 | + captured["method"] = method |
| 221 | + captured["url"] = str(url) |
| 222 | + captured["kwargs"] = kwargs |
| 223 | + return httpx.Response( |
| 224 | + 201, |
| 225 | + json={ |
| 226 | + "success": True, |
| 227 | + "function": { |
| 228 | + "id": "123e4567-e89b-12d3-a456-426614174000", |
| 229 | + "slug": "hello-world", |
| 230 | + "name": "Hello World Function", |
| 231 | + "description": "Returns a greeting message", |
| 232 | + "status": "active", |
| 233 | + "created_at": "2024-01-21T10:30:00Z", |
| 234 | + "updated_at": "2024-01-21T10:35:00Z", |
| 235 | + "deployed_at": "2024-01-21T10:35:00Z", |
| 236 | + }, |
| 237 | + }, |
| 238 | + ) |
| 239 | + |
| 240 | + async with InsforgeClient( |
| 241 | + base_url="https://example.com", |
| 242 | + api_key="ins_test", |
| 243 | + ) as client: |
| 244 | + client.http_client.request = fake_request # type: ignore[method-assign] |
| 245 | + result = await client.functions.create_function( |
| 246 | + name="Hello World Function", |
| 247 | + slug="hello-world", |
| 248 | + code="export default async function () { return new Response('hi'); }", |
| 249 | + description="Returns a greeting message", |
| 250 | + status="active", |
| 251 | + access_token="admin_token", |
| 252 | + ) |
| 253 | + return result, captured |
| 254 | + |
| 255 | + result, captured = asyncio.run(scenario()) |
| 256 | + |
| 257 | + assert captured["method"] == "POST" |
| 258 | + assert captured["url"] == "https://example.com/api/functions" |
| 259 | + assert captured["kwargs"]["headers"]["X-API-Key"] == "ins_test" |
| 260 | + assert captured["kwargs"]["headers"]["Authorization"] == "Bearer admin_token" |
| 261 | + assert captured["kwargs"]["json"] == { |
| 262 | + "name": "Hello World Function", |
| 263 | + "slug": "hello-world", |
| 264 | + "code": "export default async function () { return new Response('hi'); }", |
| 265 | + "description": "Returns a greeting message", |
| 266 | + "status": "active", |
| 267 | + } |
| 268 | + assert isinstance(result, FunctionMutationResponse) |
| 269 | + assert result.success is True |
| 270 | + assert isinstance(result.function, FunctionMetadata) |
| 271 | + assert result.function.slug == "hello-world" |
| 272 | + |
| 273 | + |
| 274 | +def test_update_function_uses_api_path_and_returns_typed_mutation_response() -> None: |
| 275 | + async def scenario() -> tuple[object, dict[str, object]]: |
| 276 | + captured: dict[str, object] = {} |
| 277 | + |
| 278 | + async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.Response: |
| 279 | + captured["method"] = method |
| 280 | + captured["url"] = str(url) |
| 281 | + captured["kwargs"] = kwargs |
| 282 | + return httpx.Response( |
| 283 | + 200, |
| 284 | + json={ |
| 285 | + "success": True, |
| 286 | + "function": { |
| 287 | + "id": "123e4567-e89b-12d3-a456-426614174000", |
| 288 | + "slug": "hello-world", |
| 289 | + "name": "Hello World Function v2", |
| 290 | + "description": "Returns a greeting message", |
| 291 | + "status": "active", |
| 292 | + "created_at": "2024-01-21T10:30:00Z", |
| 293 | + "updated_at": "2024-01-21T11:00:00Z", |
| 294 | + "deployed_at": "2024-01-21T10:35:00Z", |
| 295 | + }, |
| 296 | + }, |
| 297 | + ) |
| 298 | + |
| 299 | + async with InsforgeClient( |
| 300 | + base_url="https://example.com", |
| 301 | + api_key="ins_test", |
| 302 | + ) as client: |
| 303 | + client.http_client.request = fake_request # type: ignore[method-assign] |
| 304 | + result = await client.functions.update_function( |
| 305 | + "hello-world", |
| 306 | + name="Hello World Function v2", |
| 307 | + code="export default async function () { return new Response('hi'); }", |
| 308 | + description="Returns a greeting message", |
| 309 | + status="active", |
| 310 | + access_token="admin_token", |
| 311 | + ) |
| 312 | + return result, captured |
| 313 | + |
| 314 | + result, captured = asyncio.run(scenario()) |
| 315 | + |
| 316 | + assert captured["method"] == "PUT" |
| 317 | + assert captured["url"] == "https://example.com/api/functions/hello-world" |
| 318 | + assert captured["kwargs"]["headers"]["X-API-Key"] == "ins_test" |
| 319 | + assert captured["kwargs"]["headers"]["Authorization"] == "Bearer admin_token" |
| 320 | + assert captured["kwargs"]["json"] == { |
| 321 | + "name": "Hello World Function v2", |
| 322 | + "code": "export default async function () { return new Response('hi'); }", |
| 323 | + "description": "Returns a greeting message", |
| 324 | + "status": "active", |
| 325 | + } |
| 326 | + assert isinstance(result, FunctionMutationResponse) |
| 327 | + assert result.success is True |
| 328 | + assert result.function.name == "Hello World Function v2" |
| 329 | + |
| 330 | + |
| 331 | +def test_delete_function_uses_api_path_and_returns_typed_delete_response() -> None: |
| 332 | + async def scenario() -> tuple[object, dict[str, object]]: |
| 333 | + captured: dict[str, object] = {} |
| 334 | + |
| 335 | + async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.Response: |
| 336 | + captured["method"] = method |
| 337 | + captured["url"] = str(url) |
| 338 | + captured["kwargs"] = kwargs |
| 339 | + return httpx.Response( |
| 340 | + 200, |
| 341 | + json={ |
| 342 | + "success": True, |
| 343 | + "message": "Function hello-world deleted successfully", |
| 344 | + }, |
| 345 | + ) |
| 346 | + |
| 347 | + async with InsforgeClient( |
| 348 | + base_url="https://example.com", |
| 349 | + api_key="ins_test", |
| 350 | + ) as client: |
| 351 | + client.http_client.request = fake_request # type: ignore[method-assign] |
| 352 | + result = await client.functions.delete_function( |
| 353 | + "hello-world", |
| 354 | + access_token="admin_token", |
| 355 | + ) |
| 356 | + return result, captured |
| 357 | + |
| 358 | + result, captured = asyncio.run(scenario()) |
| 359 | + |
| 360 | + assert captured["method"] == "DELETE" |
| 361 | + assert captured["url"] == "https://example.com/api/functions/hello-world" |
| 362 | + assert captured["kwargs"]["headers"]["X-API-Key"] == "ins_test" |
| 363 | + assert captured["kwargs"]["headers"]["Authorization"] == "Bearer admin_token" |
| 364 | + assert captured["kwargs"].get("json") is None |
| 365 | + assert isinstance(result, FunctionDeleteResponse) |
| 366 | + assert result.success is True |
| 367 | + assert result.message == "Function hello-world deleted successfully" |
0 commit comments