Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 9 additions & 40 deletions __tests__/openai.test.ts
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 4 in __tests__/openai.test.ts

View workflow job for this annotation

GitHub Actions / test (26, ubuntu-latest)

block65(no-absence-comment)

__tests__/openai.test.ts:4:1: Negative framing: "so none" → say what the consequence is. Remove it, or reword it to state the condition directly rather than by exclusion.

Check warning on line 4 in __tests__/openai.test.ts

View workflow job for this annotation

GitHub Actions / test (26, ubuntu-latest)

block65(no-absence-comment)

__tests__/openai.test.ts:4:1: Negative framing: "so none" → say what the consequence is. Remove it, or reword it to state the condition directly rather than by exclusion.
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,
});
});
55 changes: 8 additions & 47 deletions __tests__/petstore.test.ts
Original file line number Diff line number Diff line change
@@ -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" },
});
});
33 changes: 4 additions & 29 deletions __tests__/test1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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 () => {
Expand Down
Loading