import { describe, it } from "@std/testing/bdd"; import { expect } from "@std/expect"; import { createSchemaTransformerV2 } from "../../src/plugin.ts"; import { asObjectSchema, getTypeFromCode } from "../utils.ts"; describe("Schema: Type aliases and shared types", () => { it("handles basic Cell/Stream/Default aliases", async () => { const code = ` interface Default {} type MyCell = Cell; type StringCell = Cell; type MyStream = Stream; type WithDefault = Default; type CellArray = Cell; type StreamOfCells = Stream>; interface TypeAliasTest { genericCell: MyCell; specificCell: StringCell; genericStream: MyStream; withDefault: WithDefault; cellOfArray: CellArray; streamOfCells: StreamOfCells; nestedAlias: MyCell[]>[]; } `; const { type, checker } = await getTypeFromCode(code, "TypeAliasTest"); const s = asObjectSchema( createSchemaTransformerV2().generateSchema(type, checker), ); const genericCell = s.properties?.genericCell as any; expect(genericCell?.type).toBe("string"); expect(genericCell?.asCell).toBe(true); const specificCell = s.properties?.specificCell as any; expect(specificCell?.type).toBe("string"); expect(specificCell?.asCell).toBe(true); const genericStream = s.properties?.genericStream as any; expect(genericStream?.type).toBe("number"); expect(genericStream?.asStream).toBe(true); const withDefault = s.properties?.withDefault as any; expect(withDefault?.type).toBe("string"); expect(withDefault?.default).toBe("hello"); const coa = s.properties?.cellOfArray as any; expect(coa.type).toBe("array"); expect(coa.items?.type).toBe("number"); expect(coa.asCell).toBe(true); const soc = s.properties?.streamOfCells as any; expect(soc.type).toBe("string"); expect(soc.asStream).toBe(true); expect(soc.asCell).toBeUndefined(); const na = s.properties?.nestedAlias as any; expect(na.type).toBe("array"); expect(na.items?.type).toBe("array"); expect(na.items?.items?.type).toBe("string"); }); it("hoists shared object type to definitions and references via $ref", async () => { const code = ` interface B { value: string; } interface A { b1: B; b2: B; } `; const { type, checker } = await getTypeFromCode(code, "A"); const s = asObjectSchema( createSchemaTransformerV2().generateSchema(type, checker), ); const b1 = s.properties?.b1 as any; const b2 = s.properties?.b2 as any; expect(b1.$ref).toBe("#/$defs/B"); expect(b2.$ref).toBe("#/$defs/B"); const defB = (s as any).$defs?.B as any; expect(defB.type).toBe("object"); expect(defB.properties?.value?.type).toBe("string"); expect(defB.required).toContain("value"); }); });