63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { parse } from "@wrnexus/syntax";
|
|
import { generate } from "../src/codegen.ts";
|
|
|
|
function serverModule(inner: string): string {
|
|
return generate(
|
|
parse(`page Repro {
|
|
ssr {
|
|
${inner}
|
|
}
|
|
|
|
view { <main><p api="ssrUsers">loading</p></main> }
|
|
}
|
|
`),
|
|
);
|
|
}
|
|
|
|
test("a sectioned ssr block binds the payload to data", () => {
|
|
const generated = serverModule(` api ssrUsers GET /api/users {
|
|
response {
|
|
return data.users.length
|
|
}
|
|
}`);
|
|
|
|
expect(generated).toContain("data.users.length");
|
|
});
|
|
|
|
test("a legacy ssr block is unchanged", () => {
|
|
const generated = serverModule(` api ssrUsers GET /api/users {
|
|
return users.length
|
|
}`);
|
|
|
|
expect(generated).toContain("users.length");
|
|
});
|
|
|
|
test("an ssr block with an error section emits the error body and binds status/message/data", () => {
|
|
const generated = serverModule(` api ssrUsers GET /api/users {
|
|
response {
|
|
return data.users.length
|
|
}
|
|
error {
|
|
return message + status + data
|
|
}
|
|
}`);
|
|
|
|
expect(generated).toContain('"errorBody"');
|
|
expect(generated).toContain("return message + status + data");
|
|
expect(generated).toContain("const status = $status");
|
|
expect(generated).toContain("const message = $message");
|
|
expect(generated).toContain("const data = $data");
|
|
expect(generated).toContain("__wrnexusEvalError");
|
|
});
|
|
|
|
test("an ssr block without an error section emits no catch entry for that binding", () => {
|
|
const generated = serverModule(` api ssrUsers GET /api/users {
|
|
response {
|
|
return data.users.length
|
|
}
|
|
}`);
|
|
|
|
expect(generated).not.toContain('"errorBody"');
|
|
});
|