| 27 | }; |
| 28 | |
| 29 | function createTextCacheApp() { |
| 30 | let originHits = 0; |
| 31 | const app = new Hono<TestEnv>() |
| 32 | .use("*", async (c, next) => { |
| 33 | c.set("log", testLog); |
| 34 | c.set("requestId", "test-request"); |
| 35 | await next(); |
| 36 | }) |
| 37 | .post( |
| 38 | "/v1/chat/completions", |
| 39 | validator("json", CreateChatCompletionRequestSchema), |
| 40 | textCache, |
| 41 | async (c) => { |
| 42 | originHits += 1; |
| 43 | return new Response( |
| 44 | JSON.stringify({ |
| 45 | originHits, |
| 46 | body: await c.req.text(), |
| 47 | }), |
| 48 | { |
| 49 | headers: { |
| 50 | "Content-Type": "application/json; charset=utf-8", |
| 51 | "x-model-used": "openai-fast", |
| 52 | "x-usage-prompt-text-tokens": "12", |
| 53 | "x-moderation-hate-severity": "safe", |
| 54 | }, |
| 55 | }, |
| 56 | ); |
| 57 | }, |
| 58 | ) |
| 59 | .get("/text/:prompt", textCache, async (c) => { |
| 60 | originHits += 1; |
| 61 | return new Response(`hit:${originHits}:${c.req.param("prompt")}`, { |
| 62 | headers: { "Content-Type": "text/plain" }, |
| 63 | }); |
| 64 | }) |
| 65 | .get("/v1/models", async () => Response.json({ data: [] })); |
| 66 | |
| 67 | return { |
| 68 | app, |
| 69 | get originHits() { |
| 70 | return originHits; |
| 71 | }, |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | function createTextCacheEnv(bucket = createTestR2Bucket()): CloudflareBindings { |
| 76 | return { |