import { expect, test } from "bun:test";
import { parse } from "@wrnexus/syntax";
import { generate } from "../src/codegen.ts";
const SOURCE = `page Probe {
apis {
searchUsers POST /api/users {
request { body { name?: string } }
response { return data.users }
error { return [] }
}
}
load server directory {
return await api.searchUsers({ name: "a" })
}
view { x }
}
`;
test("the server module declares an api object with the block's path and method", () => {
const generated = generate(parse(SOURCE));
expect(generated).toContain("const api =");
expect(generated).toContain("searchUsers");
expect(generated).toContain('"/api/users"');
expect(generated).toContain('"POST"');
});
test("the response body is spliced in", () => {
expect(generate(parse(SOURCE))).toContain("data.users");
});
test("a block with no error section rethrows rather than resolving undefined", () => {
const generated = generate(
parse(`page P {
apis { a GET /api/a { response { return data } } }
load server x { return await api.a() }
view { x }
}
`),
);
expect(generated).toContain("throw");
});