contexts.test.mjs
1 import assert from "node:assert/strict"; 2 import test from "node:test"; 3 4 import { DefaultAdapterFactory, SupportedLanguages } from "../dist/index.js"; 5 6 test("DefaultAdapterFactory exposes context CRUD and interrupt operations", 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 recorded.push({ 12 method: request.method, 13 url: request.url, 14 headers: Object.fromEntries(request.headers.entries()), 15 }); 16 17 if (url.pathname === "/code/context" && request.method === "POST") { 18 return Response.json({ id: "ctx-1", language: "python" }); 19 } 20 if (url.pathname === "/code/contexts/ctx-1" && request.method === "GET") { 21 return Response.json({ id: "ctx-1", language: "python" }); 22 } 23 if (url.pathname === "/code/contexts" && request.method === "GET") { 24 const language = url.searchParams.get("language"); 25 return Response.json( 26 language 27 ? [{ id: "ctx-2", language }] 28 : [ 29 { id: "ctx-1", language: "python" }, 30 { id: "ctx-2", language: "go" }, 31 ], 32 ); 33 } 34 if (url.pathname === "/code/contexts/ctx-1" && request.method === "DELETE") { 35 return new Response(null, { status: 204 }); 36 } 37 if (url.pathname === "/code/contexts" && request.method === "DELETE") { 38 return new Response(null, { status: 204 }); 39 } 40 if (url.pathname === "/code" && request.method === "DELETE") { 41 return new Response(null, { status: 204 }); 42 } 43 throw new Error(`Unexpected request: ${request.method} ${request.url}`); 44 }; 45 46 const factory = new DefaultAdapterFactory(); 47 const codes = factory.createCodes({ 48 sandbox: { 49 connectionConfig: { 50 headers: { "x-global": "global" }, 51 fetch: fetchImpl, 52 sseFetch: fetchImpl, 53 }, 54 }, 55 execdBaseUrl: "http://sandbox.internal:3456", 56 endpointHeaders: { "x-endpoint": "endpoint" }, 57 }); 58 59 const created = await codes.createContext(SupportedLanguages.PYTHON); 60 const fetched = await codes.getContext("ctx-1"); 61 const allContexts = await codes.listContexts(); 62 const goContexts = await codes.listContexts(SupportedLanguages.GO); 63 await codes.deleteContext("ctx-1"); 64 await codes.deleteContexts(SupportedLanguages.GO); 65 await codes.interrupt("exec-1"); 66 67 assert.deepEqual(created, { id: "ctx-1", language: "python" }); 68 assert.deepEqual(fetched, { id: "ctx-1", language: "python" }); 69 assert.equal(allContexts.length, 2); 70 assert.deepEqual(goContexts, [{ id: "ctx-2", language: "go" }]); 71 assert.deepEqual( 72 recorded.map((entry) => `${entry.method} ${entry.url}`), 73 [ 74 "POST http://sandbox.internal:3456/code/context", 75 "GET http://sandbox.internal:3456/code/contexts/ctx-1", 76 "GET http://sandbox.internal:3456/code/contexts", 77 "GET http://sandbox.internal:3456/code/contexts?language=go", 78 "DELETE http://sandbox.internal:3456/code/contexts/ctx-1", 79 "DELETE http://sandbox.internal:3456/code/contexts?language=go", 80 "DELETE http://sandbox.internal:3456/code?id=exec-1", 81 ], 82 ); 83 for (const entry of recorded) { 84 assert.equal(entry.headers["x-global"], "global"); 85 assert.equal(entry.headers["x-endpoint"], "endpoint"); 86 } 87 });