import { expect, test } from "bun:test"; import { hasRequestSection, parseApiEntries, parseApiSections } from "../src/api-sections.ts"; test("api section scanning ignores keywords in literals, comments, and nested objects", () => { const source = ` // request { body { forged: string } } response { return { label: "request { not a section }", nested: { body: true } } } `; expect(hasRequestSection(source)).toBe(false); expect(parseApiSections(source)?.response).toContain("nested"); }); test("api entries normalize methods and reject malformed request fields", () => { expect( parseApiEntries(`lookup get /users/:id { response { return data } }`)[0], ).toMatchObject({ name: "lookup", method: "GET", path: "/users/:id", }); expect(() => parseApiSections("request { body { missingType } } response { return data }"), ).toThrow('Expected "name: type"'); }); test("api entries reject truncated blocks and bare response bodies", () => { expect(() => parseApiEntries("lookup GET /users { response { return data }")).toThrow(); expect(() => parseApiEntries("lookup GET /users { return data }")).toThrow( 'declare a "response { }" section', ); });