feat: add low-level Wrenn client resources
Implement WrennClient with typed resource mappings for auth, account, API keys, users, teams, capsules, files, snapshots, hosts, and channels. Add endpoint mapping tests plus live integration tess that authenticate with WRENN_TEST_EMAIL/WRENN_TEST_PASS and use WRENN_API_KEY for API-key scoped endpoints
This commit is contained in:
85
tests/integration/client.integration.test.ts
Normal file
85
tests/integration/client.integration.test.ts
Normal file
@ -0,0 +1,85 @@
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { WrennClient } from "../../src/client.js";
|
||||
import { DEFAULT_BASE_URL } from "../../src/config.js";
|
||||
|
||||
const baseUrl = process.env.WRENN_BASE_URL ?? DEFAULT_BASE_URL;
|
||||
const apiKey = process.env.WRENN_API_KEY;
|
||||
const testEmail = process.env.WRENN_TEST_EMAIL;
|
||||
const testPassword = process.env.WRENN_TEST_PASS;
|
||||
|
||||
const describeWithApiKey = apiKey ? describe : describe.skip;
|
||||
const describeWithLogin = testEmail && testPassword ? describe : describe.skip;
|
||||
|
||||
describeWithApiKey("WrennClient live API key integration", () => {
|
||||
const client = new WrennClient({ apiKey, baseUrl });
|
||||
|
||||
it("lists capsules from the real Wrenn API", async () => {
|
||||
const capsules = await client.capsules.list();
|
||||
|
||||
expect(Array.isArray(capsules)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists snapshot templates from the real Wrenn API", async () => {
|
||||
const snapshots = await client.snapshots.list();
|
||||
|
||||
expect(Array.isArray(snapshots)).toBe(true);
|
||||
});
|
||||
|
||||
it("gets capsule stats from the real Wrenn API", async () => {
|
||||
const stats = await client.capsules.stats({ range: "1h" });
|
||||
|
||||
expect(stats).toBeTypeOf("object");
|
||||
});
|
||||
|
||||
it("gets capsule usage from the real Wrenn API", async () => {
|
||||
const usage = await client.capsules.usage();
|
||||
|
||||
expect(usage).toBeTypeOf("object");
|
||||
});
|
||||
});
|
||||
|
||||
describeWithLogin("WrennClient live login integration", () => {
|
||||
let client: WrennClient;
|
||||
|
||||
beforeAll(async () => {
|
||||
const authClient = new WrennClient({ baseUrl });
|
||||
const auth = await authClient.auth.login({
|
||||
email: testEmail as string,
|
||||
password: testPassword as string,
|
||||
});
|
||||
|
||||
expect(auth.token).toBeTypeOf("string");
|
||||
client = new WrennClient({ baseUrl, token: auth.token });
|
||||
});
|
||||
|
||||
it("gets the current account profile from the real Wrenn API", async () => {
|
||||
const me = await client.account.getMe();
|
||||
|
||||
expect(me).toBeTypeOf("object");
|
||||
});
|
||||
|
||||
it("lists teams from the real Wrenn API", async () => {
|
||||
const teams = await client.teams.list();
|
||||
|
||||
expect(Array.isArray(teams)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists API keys from the real Wrenn API", async () => {
|
||||
const keys = await client.apiKeys.list();
|
||||
|
||||
expect(Array.isArray(keys)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists notification channels from the real Wrenn API", async () => {
|
||||
const channels = await client.channels.list();
|
||||
|
||||
expect(Array.isArray(channels)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists hosts from the real Wrenn API", async () => {
|
||||
const hosts = await client.hosts.list();
|
||||
|
||||
expect(Array.isArray(hosts)).toBe(true);
|
||||
});
|
||||
});
|
||||
@ -1,183 +0,0 @@
|
||||
import {
|
||||
createServer,
|
||||
type IncomingMessage,
|
||||
type ServerResponse,
|
||||
} from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { WebSocketServer } from "ws";
|
||||
|
||||
import { HttpClient } from "../../src/_shared/http.js";
|
||||
import { WsConnection } from "../../src/_shared/websocket.js";
|
||||
import { ConflictError, TimeoutError } from "../../src/exceptions.js";
|
||||
|
||||
interface CapturedRequest {
|
||||
method?: string;
|
||||
url?: string;
|
||||
headers: IncomingMessage["headers"];
|
||||
body: string;
|
||||
}
|
||||
|
||||
function readRequestBody(request: IncomingMessage): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let body = "";
|
||||
request.setEncoding("utf8");
|
||||
request.on("data", (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
request.on("end", () => resolve(body));
|
||||
request.on("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
function listen(server: ReturnType<typeof createServer>): Promise<number> {
|
||||
return new Promise((resolve) => {
|
||||
server.listen(0, "127.0.0.1", () => {
|
||||
const address = server.address() as AddressInfo;
|
||||
resolve(address.port);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function closeHttpServer(
|
||||
server: ReturnType<typeof createServer>,
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error) reject(error);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function closeWebSocketServer(server: WebSocketServer): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error) reject(error);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("foundation integration", () => {
|
||||
const cleanup: Array<() => Promise<void>> = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(cleanup.splice(0).map((close) => close()));
|
||||
});
|
||||
|
||||
it("sends JSON requests through a real HTTP server", async () => {
|
||||
let captured: CapturedRequest | undefined;
|
||||
const server = createServer(async (request, response: ServerResponse) => {
|
||||
captured = {
|
||||
method: request.method,
|
||||
url: request.url,
|
||||
headers: request.headers,
|
||||
body: await readRequestBody(request),
|
||||
};
|
||||
response.writeHead(200, { "Content-Type": "application/json" });
|
||||
response.end(JSON.stringify({ ok: true }));
|
||||
});
|
||||
const port = await listen(server);
|
||||
cleanup.push(() => closeHttpServer(server));
|
||||
|
||||
const client = new HttpClient({
|
||||
baseUrl: `http://127.0.0.1:${port}`,
|
||||
apiKey: "api-key",
|
||||
token: "jwt-token",
|
||||
hostToken: "host-token",
|
||||
});
|
||||
|
||||
await expect(
|
||||
client.post(
|
||||
"/v1/foundation",
|
||||
{ hello: "world" },
|
||||
{ params: { page: 1 } },
|
||||
),
|
||||
).resolves.toEqual({ ok: true });
|
||||
|
||||
expect(captured).toMatchObject({
|
||||
method: "POST",
|
||||
url: "/v1/foundation?page=1",
|
||||
body: JSON.stringify({ hello: "world" }),
|
||||
});
|
||||
expect(captured?.headers["content-type"]).toBe("application/json");
|
||||
expect(captured?.headers.accept).toBe("application/json");
|
||||
expect(captured?.headers.authorization).toBe("Bearer jwt-token");
|
||||
expect(captured?.headers["x-api-key"]).toBe("api-key");
|
||||
expect(captured?.headers["x-host-token"]).toBe("host-token");
|
||||
});
|
||||
|
||||
it("maps real HTTP error responses to SDK errors", async () => {
|
||||
const server = createServer((_request, response) => {
|
||||
response.writeHead(409, { "Content-Type": "application/json" });
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
error: { code: "capsule_busy", message: "Capsule is busy" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
const port = await listen(server);
|
||||
cleanup.push(() => closeHttpServer(server));
|
||||
|
||||
const client = new HttpClient({ baseUrl: `http://127.0.0.1:${port}` });
|
||||
|
||||
await expect(client.get("/v1/error")).rejects.toMatchObject({
|
||||
code: "capsule_busy",
|
||||
message: "Capsule is busy",
|
||||
statusCode: 409,
|
||||
});
|
||||
await expect(client.get("/v1/error")).rejects.toBeInstanceOf(ConflictError);
|
||||
});
|
||||
|
||||
it("aborts real HTTP requests using timeoutMs", async () => {
|
||||
const server = createServer((_request, response) => {
|
||||
setTimeout(() => {
|
||||
response.writeHead(200, { "Content-Type": "application/json" });
|
||||
response.end(JSON.stringify({ ok: true }));
|
||||
}, 200);
|
||||
});
|
||||
const port = await listen(server);
|
||||
cleanup.push(() => closeHttpServer(server));
|
||||
|
||||
const client = new HttpClient({ baseUrl: `http://127.0.0.1:${port}` });
|
||||
|
||||
await expect(
|
||||
client.get("/v1/slow", { timeoutMs: 10 }),
|
||||
).rejects.toBeInstanceOf(TimeoutError);
|
||||
});
|
||||
|
||||
it("exchanges JSON messages through a real WebSocket server", async () => {
|
||||
const server = new WebSocketServer({ port: 0, host: "127.0.0.1" });
|
||||
cleanup.push(() => closeWebSocketServer(server));
|
||||
|
||||
const receivedByServer = new Promise<unknown>((resolve) => {
|
||||
server.on("connection", (socket, request) => {
|
||||
expect(request.headers["x-api-key"]).toBe("api-key");
|
||||
expect(request.headers["x-host-token"]).toBe("host-token");
|
||||
socket.on("message", (raw) => resolve(JSON.parse(raw.toString())));
|
||||
socket.send(JSON.stringify({ type: "ready" }));
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve) => server.once("listening", resolve));
|
||||
const address = server.address() as AddressInfo;
|
||||
|
||||
const messages: unknown[] = [];
|
||||
const connection = await WsConnection.connect({
|
||||
baseUrl: `http://127.0.0.1:${address.port}`,
|
||||
path: "/v1/ws",
|
||||
apiKey: "api-key",
|
||||
hostToken: "host-token",
|
||||
onMessage: (message) => messages.push(message),
|
||||
});
|
||||
|
||||
connection.send({ type: "start" });
|
||||
|
||||
await expect(receivedByServer).resolves.toEqual({ type: "start" });
|
||||
expect(messages).toEqual([{ type: "ready" }]);
|
||||
expect(connection.isClosed).toBe(false);
|
||||
|
||||
connection.close();
|
||||
});
|
||||
});
|
||||
85
tests/integration/live-client.integration.test.ts
Normal file
85
tests/integration/live-client.integration.test.ts
Normal file
@ -0,0 +1,85 @@
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { WrennClient } from "../../src/client.js";
|
||||
import { DEFAULT_BASE_URL } from "../../src/config.js";
|
||||
|
||||
const baseUrl = process.env.WRENN_BASE_URL ?? DEFAULT_BASE_URL;
|
||||
const apiKey = process.env.WRENN_API_KEY;
|
||||
const testEmail = process.env.WRENN_TEST_EMAIL;
|
||||
const testPassword = process.env.WRENN_TEST_PASS;
|
||||
|
||||
const describeWithApiKey = apiKey ? describe : describe.skip;
|
||||
const describeWithLogin = testEmail && testPassword ? describe : describe.skip;
|
||||
|
||||
describeWithApiKey("WrennClient live API key integration", () => {
|
||||
const client = new WrennClient({ apiKey, baseUrl });
|
||||
|
||||
it("lists capsules from the real Wrenn API", async () => {
|
||||
const capsules = await client.capsules.list();
|
||||
|
||||
expect(Array.isArray(capsules)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists snapshot templates from the real Wrenn API", async () => {
|
||||
const snapshots = await client.snapshots.list();
|
||||
|
||||
expect(Array.isArray(snapshots)).toBe(true);
|
||||
});
|
||||
|
||||
it("gets capsule stats from the real Wrenn API", async () => {
|
||||
const stats = await client.capsules.stats({ range: "1h" });
|
||||
|
||||
expect(stats).toBeTypeOf("object");
|
||||
});
|
||||
|
||||
it("gets capsule usage from the real Wrenn API", async () => {
|
||||
const usage = await client.capsules.usage();
|
||||
|
||||
expect(usage).toBeTypeOf("object");
|
||||
});
|
||||
});
|
||||
|
||||
describeWithLogin("WrennClient live login integration", () => {
|
||||
let client: WrennClient;
|
||||
|
||||
beforeAll(async () => {
|
||||
const authClient = new WrennClient({ baseUrl });
|
||||
const auth = await authClient.auth.login({
|
||||
email: testEmail as string,
|
||||
password: testPassword as string,
|
||||
});
|
||||
|
||||
expect(auth.token).toBeTypeOf("string");
|
||||
client = new WrennClient({ baseUrl, token: auth.token });
|
||||
});
|
||||
|
||||
it("gets the current account profile from the real Wrenn API", async () => {
|
||||
const me = await client.account.getMe();
|
||||
|
||||
expect(me).toBeTypeOf("object");
|
||||
});
|
||||
|
||||
it("lists teams from the real Wrenn API", async () => {
|
||||
const teams = await client.teams.list();
|
||||
|
||||
expect(Array.isArray(teams)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists API keys from the real Wrenn API", async () => {
|
||||
const keys = await client.apiKeys.list();
|
||||
|
||||
expect(Array.isArray(keys)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists notification channels from the real Wrenn API", async () => {
|
||||
const channels = await client.channels.list();
|
||||
|
||||
expect(Array.isArray(channels)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists hosts from the real Wrenn API", async () => {
|
||||
const hosts = await client.hosts.list();
|
||||
|
||||
expect(Array.isArray(hosts)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user