import { ServerContext, Status } from "../server.ts";
import {
assert,
assertEquals,
assertStringIncludes,
delay,
puppeteer,
} from "./deps.ts";
import manifest from "./fixture_plugin/fresh.gen.ts";
import options from "./fixture_plugin/options.ts";
import { runBuild, startFreshServer } from "./test_utils.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
const handler = ctx.handler();
const router = (req: Request) => {
return handler(req, {
remoteAddr: {
transport: "tcp",
hostname: "127.0.0.1",
port: 80,
},
});
};
Deno.test("/static page prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/static"));
assert(resp);
assertEquals(resp.status, Status.OK);
const body = await resp.text();
assertStringIncludes(body, '');
assert(!body.includes(`>{"v":[[],[]]}`));
assert(!body.includes(`import`));
assertStringIncludes(
body,
'',
);
});
Deno.test("/with-island prerender", async () => {
const resp = await router(new Request("https://fresh.deno.dev/with-island"));
assert(resp);
assertEquals(resp.status, Status.OK);
const body = await resp.text();
assertStringIncludes(
body,
'',
);
assertStringIncludes(body, `>{"v":[[{}],["JS injected!"]]}`);
assertStringIncludes(body, `/plugin-js-inject-main.js"`);
assertStringIncludes(
body,
'',
);
});
Deno.test("plugin routes and middleware", async () => {
const resp = await router(new Request("https://fresh.deno.dev/test"));
assert(resp);
assertEquals(resp.status, Status.OK);
const body = await resp.text();
assertStringIncludes(
body,
`
look, i'm set from a plugin!
`,
);
assertStringIncludes(
body,
`Title Set From Plugin Config`,
);
});
Deno.test("plugin middleware multiple handlers", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/lots-of-middleware"),
);
assert(resp);
assertEquals(resp.status, Status.OK);
const body = await resp.text();
assertStringIncludes(
body,
`3
`,
);
});
Deno.test("plugin route no leading slash", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/no-leading-slash-here"),
);
assert(resp);
assertEquals(resp.status, Status.OK);
const body = await resp.text();
assertStringIncludes(
body,
`Hello
`,
);
});
Deno.test({
name: "/with-island hydration",
async fn(t) {
// Preparation
const { lines, serverProcess, address } = await startFreshServer({
args: ["run", "-A", "./tests/fixture_plugin/main.ts"],
});
await delay(100);
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto(`${address}/with-island`, {
waitUntil: "networkidle2",
});
await t.step("island is revived", async () => {
await page.waitForSelector("#csr");
});
await t.step("title was updated", async () => {
const title = await page.title();
assertEquals(title, "JS injected!");
});
await browser.close();
serverProcess.kill("SIGTERM");
await serverProcess.status;
// Drain the lines stream
for await (const _ of lines) { /* noop */ }
},
});
Deno.test("calls buildStart() and buildEnd()", async () => {
const result = await runBuild("./tests/fixture_plugin_lifecycle/dev.ts");
const out = result.stdout.split("\n").filter((line) =>
line.startsWith("Plugin")
);
assertEquals(out, [
"Plugin a: buildStart",
"Plugin b: buildStart",
"Plugin a: buildEnd",
"Plugin b: buildEnd",
]);
});