| 1 | import * as Config from "@effect/ai-codegen/Config" |
| 2 | import type { DiscoveredProvider } from "@effect/ai-codegen/Discovery" |
| 3 | import * as Generator from "@effect/ai-codegen/Generator" |
| 4 | import * as OpenApiGenerator from "@effect/openapi-generator/OpenApiGenerator" |
| 5 | import * as NodeServices from "@effect/platform-node/NodeServices" |
| 6 | import { assert, describe, it } from "@effect/vitest" |
| 7 | import * as Effect from "effect/Effect" |
| 8 | import * as Layer from "effect/Layer" |
| 9 | import * as Path from "effect/Path" |
| 10 | |
| 11 | const TestLayer = Generator.layer.pipe( |
| 12 | Layer.provide(Layer.succeed(OpenApiGenerator.OpenApiGenerator, { |
| 13 | generate: (spec) => Effect.succeed(JSON.stringify(spec)) |
| 14 | })), |
| 15 | Layer.provideMerge(NodeServices.layer) |
| 16 | ) |
| 17 | |
| 18 | const provider = (patch: string): DiscoveredProvider => ({ |
| 19 | name: "test", |
| 20 | packagePath: "/providers/test", |
| 21 | config: new Config.CodegenConfig({ |
| 22 | spec: "spec.json", |
| 23 | output: "Generated.ts", |
| 24 | patches: [patch] |
| 25 | }), |
| 26 | specSource: Config.SpecSource.File("spec.json"), |
| 27 | outputPath: "Generated.ts" |
| 28 | }) |
| 29 | |
| 30 | const generateWithPatch = (patch: string) => |
| 31 | Effect.gen(function*() { |
| 32 | const generator = yield* Generator.CodeGenerator |
| 33 | const output = yield* generator.generate(provider(patch), { |
| 34 | openapi: "3.1.0", |
| 35 | info: { title: "Before", version: "1.0.0" }, |
| 36 | paths: {} |
| 37 | }) |
| 38 | return JSON.parse(output) |
| 39 | }) |
| 40 | |
| 41 | describe("CodeGenerator patch inputs", () => { |
| 42 | it.effect("accepts inline patches with leading whitespace", () => |
| 43 | Effect.gen(function*() { |
| 44 | const result = yield* generateWithPatch( |
| 45 | " \n[{\"op\":\"replace\",\"path\":\"/info/title\",\"value\":\"After\"}]" |
| 46 | ) |
| 47 | assert.deepStrictEqual(result, { |
| 48 | openapi: "3.1.0", |
| 49 | info: { title: "After", version: "1.0.0" }, |
| 50 | paths: {} |
| 51 | }) |
| 52 | }).pipe(Effect.provide(TestLayer))) |
| 53 | |
| 54 | it.effect("accepts absolute paths with leading whitespace", () => |
| 55 | Effect.gen(function*() { |
| 56 | const path = yield* Path.Path |
| 57 | const patch = path.join( |
| 58 | import.meta.dirname, |
| 59 | "../../openapi-generator/test/fixtures/patches/valid-replace.json" |
| 60 | ) |
| 61 | const result = yield* generateWithPatch(` ${patch}`) |
| 62 | assert.deepStrictEqual(result, { |
| 63 | openapi: "3.1.0", |
| 64 | info: { title: "Updated API Title", version: "1.0.0" }, |
| 65 | paths: {} |
| 66 | }) |
| 67 | }).pipe(Effect.provide(TestLayer))) |
| 68 | }) |