2020-05-14 09:24:38 +00:00
|
|
|
import {
|
2021-07-27 20:17:38 +00:00
|
|
|
describe,
|
2020-05-14 09:24:38 +00:00
|
|
|
expect,
|
2020-12-02 21:06:45 +00:00
|
|
|
it,
|
2020-05-14 09:24:38 +00:00
|
|
|
mockfn,
|
2021-07-27 20:17:38 +00:00
|
|
|
} from "https://code.thunderk.net/typescript/devtools/raw/1.3.0/testing.ts";
|
|
|
|
import { processRequest, Response } from "./server.ts";
|
2020-05-14 09:24:38 +00:00
|
|
|
|
2021-07-27 20:17:38 +00:00
|
|
|
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",
|
|
|
|
});
|
2020-05-14 09:24:38 +00:00
|
|
|
});
|
|
|
|
|
2021-07-27 20:17:38 +00:00
|
|
|
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();
|
2020-05-14 09:24:38 +00:00
|
|
|
});
|
|
|
|
|
2021-07-27 20:17:38 +00:00
|
|
|
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();
|
2020-05-14 09:24:38 +00:00
|
|
|
});
|
|
|
|
|
2021-07-27 20:17:38 +00:00
|
|
|
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();
|
|
|
|
});
|
2020-05-14 09:24:38 +00:00
|
|
|
|
2021-07-27 20:17:38 +00:00
|
|
|
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");',
|
|
|
|
},
|
|
|
|
);
|
2020-05-14 09:24:38 +00:00
|
|
|
});
|
2021-07-27 20:17:38 +00:00
|
|
|
});
|
2020-05-14 09:24:38 +00:00
|
|
|
|
2021-07-27 20:17:38 +00:00
|
|
|
type CallOptions = { method: string; run: ReturnType<typeof mockfn> };
|
|
|
|
async function call(
|
|
|
|
url: string,
|
|
|
|
options: Partial<CallOptions> = {},
|
|
|
|
): Promise<[Response, ReturnType<typeof mockfn>]> {
|
|
|
|
const method = options.method ?? "GET";
|
|
|
|
const run = options.run ?? mockfn();
|
2020-05-14 09:24:38 +00:00
|
|
|
const response = await processRequest(
|
2021-07-27 20:17:38 +00:00
|
|
|
{ method, url } as any,
|
|
|
|
"git.example.com/libs",
|
|
|
|
run,
|
2020-05-14 09:24:38 +00:00
|
|
|
);
|
2021-07-27 20:17:38 +00:00
|
|
|
return [response, run];
|
|
|
|
}
|