|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { DefaultAdapterFactory } from "../dist/index.js"; |
| 5 | + |
| 6 | +test("DefaultAdapterFactory merges sandbox and endpoint headers for code requests", async () => { |
| 7 | + const recorded = []; |
| 8 | + const fetchImpl = async (input, init = {}) => { |
| 9 | + const request = input instanceof Request ? input : new Request(input, init); |
| 10 | + const url = new URL(request.url); |
| 11 | + const headers = Object.fromEntries(request.headers.entries()); |
| 12 | + recorded.push({ |
| 13 | + url: request.url, |
| 14 | + method: request.method, |
| 15 | + headers, |
| 16 | + }); |
| 17 | + |
| 18 | + if (url.pathname === "/code/context") { |
| 19 | + return new Response(JSON.stringify({ id: "ctx-1", language: "python" }), { |
| 20 | + status: 200, |
| 21 | + headers: { "content-type": "application/json" }, |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + return new Response( |
| 26 | + [ |
| 27 | + JSON.stringify({ type: "stdout", text: "hello", timestamp: 1 }), |
| 28 | + JSON.stringify({ type: "execution_complete", execution_time: 2, timestamp: 2 }), |
| 29 | + ].join("\n"), |
| 30 | + { |
| 31 | + status: 200, |
| 32 | + headers: { "content-type": "text/event-stream" }, |
| 33 | + } |
| 34 | + ); |
| 35 | + }; |
| 36 | + |
| 37 | + const sandbox = { |
| 38 | + connectionConfig: { |
| 39 | + headers: { "x-global": "global" }, |
| 40 | + fetch: fetchImpl, |
| 41 | + sseFetch: fetchImpl, |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + const factory = new DefaultAdapterFactory(); |
| 46 | + const codes = factory.createCodes({ |
| 47 | + sandbox, |
| 48 | + execdBaseUrl: "http://sandbox.internal:3456", |
| 49 | + endpointHeaders: { "x-endpoint": "endpoint" }, |
| 50 | + }); |
| 51 | + |
| 52 | + const context = await codes.createContext("python"); |
| 53 | + assert.equal(context.id, "ctx-1"); |
| 54 | + |
| 55 | + const execution = await codes.run("print('hello')"); |
| 56 | + assert.equal(execution.logs.stdout[0]?.text, "hello"); |
| 57 | + |
| 58 | + assert.equal(recorded.length, 2); |
| 59 | + assert.equal(recorded[0].url, "http://sandbox.internal:3456/code/context"); |
| 60 | + assert.equal(recorded[0].headers["x-global"], "global"); |
| 61 | + assert.equal(recorded[0].headers["x-endpoint"], "endpoint"); |
| 62 | + assert.equal(recorded[1].url, "http://sandbox.internal:3456/code"); |
| 63 | + assert.equal(recorded[1].headers["x-global"], "global"); |
| 64 | + assert.equal(recorded[1].headers["x-endpoint"], "endpoint"); |
| 65 | + assert.equal(recorded[1].headers.accept, "text/event-stream"); |
| 66 | +}); |
0 commit comments