fix(runtime): stabilize navigation and custom errors
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { afterEach, expect, test } from "bun:test";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { buildRouter } from "@wrnexus/router";
|
||||
import { createHandlers, type RuntimeDeps } from "../src/runtime.ts";
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(() => roots.splice(0).forEach((root) => rmSync(root, { recursive: true, force: true })));
|
||||
|
||||
function customNotFoundRuntime() {
|
||||
const root = mkdtempSync(join(tmpdir(), "wrnexus-not-found-"));
|
||||
roots.push(root);
|
||||
const app = join(root, "app");
|
||||
mkdirSync(join(app, "pages"), { recursive: true });
|
||||
mkdirSync(join(app, "api"), { recursive: true });
|
||||
writeFileSync(join(app, "pages/404.ts"), "export default () => '';");
|
||||
writeFileSync(join(app, "api/404.ts"), "export const GET = () => null;");
|
||||
return createHandlers({
|
||||
mode: "production",
|
||||
hmr: false,
|
||||
router: buildRouter(app),
|
||||
loadModule: async (file) =>
|
||||
file.includes(`${join("api", "404")}.ts`)
|
||||
? { GET: () => Response.json({ code: "CUSTOM_NOT_FOUND" }, { headers: { "x-custom": "yes" } }) }
|
||||
: { default: () => "<main><h1>That page is gone</h1></main>" },
|
||||
getMiddleware: async () => [],
|
||||
assets: { serve: async () => null },
|
||||
} satisfies RuntimeDeps);
|
||||
}
|
||||
|
||||
const server = { upgrade: () => false };
|
||||
|
||||
test("renders app/pages/404 with an HTTP 404 status", async () => {
|
||||
const response = await customNotFoundRuntime().fetch(
|
||||
new Request("https://example.test/missing"),
|
||||
server,
|
||||
);
|
||||
expect(response?.status).toBe(404);
|
||||
expect(await response?.text()).toContain("That page is gone");
|
||||
});
|
||||
|
||||
test("uses app/api/404 for unmatched API routes and preserves headers", async () => {
|
||||
const response = await customNotFoundRuntime().fetch(
|
||||
new Request("https://example.test/api/missing"),
|
||||
server,
|
||||
);
|
||||
expect(response?.status).toBe(404);
|
||||
expect(response?.headers.get("x-custom")).toBe("yes");
|
||||
expect(await response?.json()).toEqual({ code: "CUSTOM_NOT_FOUND" });
|
||||
});
|
||||
Reference in New Issue
Block a user