The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.
Rust agent-readableai-agentsarecclaude-codecodexcursorlocal-firstmacosprivacyrustscreen-recorderscreen-recordingtauritranscriptiontypescriptwindows
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
import { describe, expect, it } from "vitest";
import { ProviderError, isTransientProviderError, joinUrl, request } from "../src/providers/http.js";
/** A server that reads the whole upload, then answers with `status`. */
async function answerWith(status: number): Promise<{ url: string; close: () => Promise<void> }> {
const server = createServer(async (req, res) => {
for await (const _ of req);
res.statusCode = status;
res.end("{}");
});
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
const { port } = server.address() as AddressInfo;
return {
url: `http://127.0.0.1:${port}`,
close: () =>
new Promise((r) => {
server.closeAllConnections();
server.close(() => r());
}),
};
}
describe("request with an upload", () => {
// Uploads are streamed for progress. Node 24.14's fetch turned a 401 to a
// streamed body into a network error unless credentials are omitted.
it.each([401, 403, 500])("reports HTTP %i as that status, not as unreachable", async (status) => {
const server = await answerWith(status);
try {
const err = await request("test-provider", server.url, { method: "POST", body: new Uint8Array(200 * 1024) }).then(
() => null,
(e: unknown) => e,
);
expect(err).toBeInstanceOf(ProviderError);
expect((err as ProviderError).status).toBe(status);
} finally {
await server.close();
}
});
});
describe("ProviderError", () => {
it("uses the API-key message for HTTP 401", () => {
const error = new ProviderError("test-provider", 401);
expect(error.message).toBe(
"test-provider transcription failed (HTTP 401): check the API key",
);
});
it("uses the API-key message for HTTP 403", () => {
const error = new ProviderError("test-provider", 403);
expect(error.message).toBe(
"test-provider transcription failed (HTTP 403): check the API key",
);
});
it("uses the audio-size message for HTTP 413", () => {
const error = new ProviderError("test-provider", 413);
expect(error.message).toBe(
"test-provider transcription failed (HTTP 413): the audio is too large for this provider",
);
});
it("uses the rate-limit message for HTTP 429", () => {
const error = new ProviderError("test-provider", 429);
expect(error.message).toBe(
"test-provider transcription failed (HTTP 429): rate limited, try again later",
);
});
it("includes the detail for other HTTP statuses", () => {
const error = new ProviderError("test-provider", 500, "Internal server error");
expect(error.message).toBe(
"test-provider transcription failed (HTTP 500): Internal server error",
);
});
it("does not include a supplied detail for HTTP 401", () => {
const error = new ProviderError("test-provider", 401, "sensitive provider detail");
expect(error.message).toBe(
"test-provider transcription failed (HTTP 401): check the API key",
);
expect(error.message).not.toContain("sensitive provider detail");
});
it("uses the supplied detail when no HTTP status is available", () => {
const error = new ProviderError("test-provider", undefined, "network error");
expect(error.message).toBe(
"test-provider transcription failed: network error",
);
});
it("uses the default message when no status or detail is available", () => {
const error = new ProviderError("test-provider");
expect(error.message).toBe(
"test-provider transcription failed: the provider could not be reached",
);
});
});
describe("joinUrl", () => {
it("puts exactly one slash between the base URL and the path", () => {
for (const base of ["https://api.example.com", "https://api.example.com/"]) {
for (const path of ["v1/listen", "/v1/listen"]) {
expect(joinUrl(base, path)).toBe("https://api.example.com/v1/listen");
}
}
});
it("drops repeated slashes at the join", () => {
expect(joinUrl("http://localhost:8000/v1///", "///audio/transcriptions")).toBe("http://localhost:8000/v1/audio/transcriptions");
});
it("keeps a path in the base URL", () => {
expect(joinUrl("https://proxy.example.com/stt/deepgram/", "v1/listen")).toBe("https://proxy.example.com/stt/deepgram/v1/listen");
});
it("leaves slashes inside the path alone", () => {
expect(joinUrl("https://api.example.com", "v2/transcript//abc/")).toBe("https://api.example.com/v2/transcript//abc/");
});
});
describe("isTransientProviderError", () => {
it("classifies network errors, 408, 429, and 5xx as transient", () => {
expect(isTransientProviderError(new ProviderError("p", undefined, "network error (ECONNRESET)"))).toBe(true);
for (const status of [408, 429, 500, 502, 503, 504]) {
expect(isTransientProviderError(new ProviderError("p", status))).toBe(true);
}
});
it("treats permanent 4xx, 200 parse errors, and non-provider errors as non-transient", () => {
for (const status of [200, 400, 401, 403, 404, 413, 422]) {
expect(isTransientProviderError(new ProviderError("p", status))).toBe(false);
}
expect(isTransientProviderError(new Error("boom"))).toBe(false);
});
});