File
Blob: src/cloudflare/internal/test/aig/aig-mock.js
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | export default { |
| 6 | async fetch(request, env, ctx) { |
| 7 | if (request.method === 'GET') { |
| 8 | if (request.url.endsWith('logs/404')) { |
| 9 | return Response.json( |
| 10 | { |
| 11 | errors: [ |
| 12 | { |
| 13 | code: 7002, |
| 14 | message: 'Not Found', |
| 15 | }, |
| 16 | ], |
| 17 | success: true, |
| 18 | }, |
| 19 | { status: 404 } |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | if (request.url.endsWith('url/openai')) { |
| 24 | return Response.json({ |
| 25 | result: { |
| 26 | url: 'https://gateway.ai.cloudflare.com/v1/account-tag-abc/my-gateway/openai', |
| 27 | }, |
| 28 | success: true, |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | if (request.url.endsWith('logs/500')) { |
| 33 | return Response.json( |
| 34 | { |
| 35 | errors: [ |
| 36 | { |
| 37 | code: 7000, |
| 38 | message: 'Internal Error', |
| 39 | }, |
| 40 | ], |
| 41 | success: true, |
| 42 | }, |
| 43 | { status: 500 } |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | return Response.json({ |
| 48 | success: true, |
| 49 | result: { |
| 50 | cached: false, |
| 51 | cost: 0, |
| 52 | created_at: '2019-08-24T14:15:22Z', |
| 53 | custom_cost: true, |
| 54 | duration: 0, |
| 55 | id: 'string', |
| 56 | metadata: 'string', |
| 57 | model: 'string', |
| 58 | model_type: 'string', |
| 59 | path: 'string', |
| 60 | provider: 'string', |
| 61 | request_content_type: 'string', |
| 62 | request_head: 'string', |
| 63 | request_head_complete: true, |
| 64 | request_size: 0, |
| 65 | request_type: 'string', |
| 66 | response_content_type: 'string', |
| 67 | response_head: 'string', |
| 68 | response_head_complete: true, |
| 69 | response_size: 0, |
| 70 | status_code: 0, |
| 71 | step: 0, |
| 72 | success: true, |
| 73 | tokens_in: 0, |
| 74 | tokens_out: 0, |
| 75 | }, |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | if (request.method === 'PATCH') { |
| 80 | if (request.url.endsWith('logs/404')) { |
| 81 | return Response.json( |
| 82 | { |
| 83 | errors: [ |
| 84 | { |
| 85 | code: 7002, |
| 86 | message: 'Not Found', |
| 87 | }, |
| 88 | ], |
| 89 | success: true, |
| 90 | }, |
| 91 | { status: 404 } |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | if (request.url.endsWith('logs/500')) { |
| 96 | return Response.json( |
| 97 | { |
| 98 | errors: [ |
| 99 | { |
| 100 | code: 7002, |
| 101 | message: 'Internal Error', |
| 102 | }, |
| 103 | ], |
| 104 | success: true, |
| 105 | }, |
| 106 | { status: 500 } |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return Response.json({ success: true }); |
| 111 | } |
| 112 | |
| 113 | if (request.method === 'POST') { |
| 114 | const body = await request.json(); |
| 115 | |
| 116 | if (body[0]?.endpoint === 'hang') { |
| 117 | // Never resolve unless the fetch is aborted. |
| 118 | await scheduler.wait(1e8); |
| 119 | } |
| 120 | |
| 121 | return Response.json({ |
| 122 | success: true, |
| 123 | result: body, |
| 124 | headers: Object.fromEntries(request.headers.entries()), |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | return Response.json({ success: false }, { status: 500 }); |
| 129 | }, |
| 130 | }; |