import { describe, expect, it, mockfn, } from "https://code.thunderk.net/typescript/devtools/raw/1.3.0/testing.ts"; import { processRequest, Response } from "./server.ts"; describe("serveBundles", () => { it("calls deno bundle if asking for js", async () => { const run = mockfn(() => { return { output: () => Promise.resolve(new TextEncoder().encode("abc")), status: () => Promise.resolve({ code: 0, success: true }), } as any; }); const [response, _] = await call("/greatlib@1.0.0/reader/file.js", { run }); expect(response).toEqual({ body: new TextEncoder().encode("abc") }); expect(run).toHaveBeenCalledTimes(1); expect(run).toHaveBeenCalledWith({ cmd: [ "deno", "bundle", "https://git.example.com/libs/greatlib/raw/1.0.0/reader/file.ts", ], stdout: "piped", }); }); it("redirects to raw file if asking for anything other than js", async () => { const [response, run] = await call("/greatlib@1.0.0/reader/file.ts"); expect(response).toEqual({ status: 301, headers: new Headers({ Location: "https://git.example.com/libs/greatlib/raw/1.0.0/reader/file.ts", }), }); expect(run).not.toHaveBeenCalled(); }); it("handles bad method", async () => { const [response, run] = await call("/greatlib@1.0.0/reader/file.ts", { method: "POST", }); expect(response).toEqual({ status: 405 }); expect(run).not.toHaveBeenCalled(); }); it("handles bad path", async () => { const [response, run] = await call("/greatlib@1.0.0/reader{}.ts"); expect(response).toEqual( { status: 400, body: 'console.error("bundler error - Invalid path https://git.example.com/libs/greatlib/raw/1.0.0/reader{}.ts");', }, ); expect(run).not.toHaveBeenCalled(); }); it("handles bundle failure", async () => { const run = mockfn(() => { return { output: () => Promise.resolve(undefined), status: () => Promise.resolve({ code: 1, success: false }), } as any; }); const [response, _] = await call("/great_lib@1.0.0-dev1/reader.js", { run, }); expect(response).toEqual( { status: 500, body: 'console.error("bundler error - Failed to bundle https://git.example.com/libs/great_lib/raw/1.0.0-dev1/reader.ts");', }, ); }); }); type CallOptions = { method: string; run: ReturnType }; async function call( url: string, options: Partial = {}, ): Promise<[Response, ReturnType]> { const method = options.method ?? "GET"; const run = options.run ?? mockfn(); const response = await processRequest( { method, url } as any, "git.example.com/libs", run, ); return [response, run]; }