diff --git a/__tests__/openai.test.ts b/__tests__/openai.test.ts index 355c761..40ee024 100644 --- a/__tests__/openai.test.ts +++ b/__tests__/openai.test.ts @@ -1,45 +1,14 @@ -import { createIsomorphicNativeFetcher } from "@block65/rest-client"; -import { MockAgent, fetch as undiciFetch } from "undici"; -import { describe, expect, test } from "vitest"; +import { expect, test } from "vitest"; import { CreateModerationCommand } from "./fixtures/openai/commands.ts"; -import { OpenAiApiRestClient } from "./fixtures/openai/main.ts"; -const mockAgent = new MockAgent(); -mockAgent.disableNetConnect(); +// the input is the whole body, so none of it moves to the query or path +test("createModeration sends its input as a JSON body", () => { + const command = new CreateModerationCommand({ input: "This is a test" }); -const apiUrl = "http://192.2.0.1"; - -describe("OpenAI", () => { - const pool = mockAgent.get(apiUrl); - - pool - .intercept({ - path: "/moderations", - method: "POST", - }) - .reply(200, { - id: "modr-123", - model: "text-moderation-latest", - results: [], - }) - .times(1); - - test("CreateModeration", async () => { - const openAiClient = new OpenAiApiRestClient(apiUrl, { - logger: console.debug, - fetcher: createIsomorphicNativeFetcher({ - retry: { retries: 0 }, - fetch: (input, init) => - undiciFetch(input, { ...init, dispatcher: mockAgent }), - }), - }); - - const command = new CreateModerationCommand({ - input: "This is a test", - }); - - const result = await openAiClient.json(command); - - expect(result).toBeTruthy(); + expect(command.toJSON()).toStrictEqual({ + method: "post", + pathname: "/moderations", + body: JSON.stringify({ input: "This is a test" }), + query: undefined, }); }); diff --git a/__tests__/petstore.test.ts b/__tests__/petstore.test.ts index d2a0174..be070c8 100644 --- a/__tests__/petstore.test.ts +++ b/__tests__/petstore.test.ts @@ -1,52 +1,13 @@ -import { createIsomorphicNativeFetcher } from "@block65/rest-client"; -import { MockAgent, fetch as undiciFetch } from "undici"; -import { describe, expect, test, vi } from "vitest"; +import { expect, test } from "vitest"; import { FindPetsCommand } from "./fixtures/petstore/commands.ts"; -import { SwaggerPetstoreRestClient } from "./fixtures/petstore/main.ts"; -const mockAgent = new MockAgent(); -mockAgent.disableNetConnect(); +test("findPets carries its method, path and query", () => { + const command = new FindPetsCommand({ limit: "10", tags: ["tag1", "tag2"] }); -const apiUrl = "http://192.2.0.1"; - -describe("Petstore", () => { - const pool = mockAgent.get(apiUrl); - - const bodySpy = vi.fn<(body: string) => { ok: boolean }>(() => ({ - ok: true, - })); - - pool - .intercept({ - // `style: form` + default `explode: true` → repeated keys - path: "/pets?tags=tag1&tags=tag2&limit=10", - method: "GET", - body(body) { - bodySpy(body); - return true; - }, - }) - .reply(200, { ok: 1 }) - .times(1); - - test("find pets", async () => { - const petStoreClient = new SwaggerPetstoreRestClient(apiUrl, { - logger: console.log, - fetcher: createIsomorphicNativeFetcher({ - retry: { retries: 0 }, - fetch: (input, init) => - undiciFetch(input, { ...init, dispatcher: mockAgent }), - }), - }); - const command = new FindPetsCommand({ - limit: "10", - tags: ["tag1", "tag2"], - }); - - const result = await petStoreClient - .json(command) - .catch((err: unknown) => err); - - expect(result).toBeTruthy(); + expect(command.toJSON()).toStrictEqual({ + method: "get", + pathname: "/pets", + body: undefined, + query: { tags: ["tag1", "tag2"], limit: "10" }, }); }); diff --git a/__tests__/test1.test.ts b/__tests__/test1.test.ts index 781db7a..fc9d3d0 100644 --- a/__tests__/test1.test.ts +++ b/__tests__/test1.test.ts @@ -3,7 +3,7 @@ import { type ParsedStreamEvent, } from "@block65/rest-client"; import { MockAgent, fetch as undiciFetch } from "undici"; -import { describe, expect, expectTypeOf, test, vi } from "vitest"; +import { describe, expect, expectTypeOf, test } from "vitest"; import { GetBillingAccountCommand, ListBillingAccountsCommand, @@ -20,22 +20,6 @@ const apiUrl = "http://192.2.0.1"; describe("Test1", () => { const pool = mockAgent.get(apiUrl); - const bodySpy = vi.fn<(body: string) => { ok: boolean }>(() => ({ - ok: true, - })); - - pool - .intercept({ - path: "/billing-accounts/1234", - method: "GET", - body(body) { - bodySpy(body); - return true; - }, - }) - .reply(200, { ok: 1 }) - .times(1); - const operationId = "00000000-0000-4000-8000-000000000000"; pool @@ -50,22 +34,13 @@ describe("Test1", () => { expect(command.pathname).toBe("/billing-accounts"); }); - test("get billing account", async () => { - const client = new BillingServiceRestApiRestClient(apiUrl, { - logger: console.debug, - fetcher: createIsomorphicNativeFetcher({ - retry: { retries: 0 }, - fetch: (input, init) => - undiciFetch(input, { ...init, dispatcher: mockAgent }), - }), - }); + test("a path parameter fills its segment", () => { const command = new GetBillingAccountCommand({ billingAccountId: "1234", }); - await client.json(command); - - expect(bodySpy).toBeTruthy(); + expect(command.method).toBe("get"); + expect(command.pathname).toBe("/billing-accounts/1234"); }); test("event stream yields each event with its data decoded", async () => {