Skip to content

Commit

Permalink
refactor: clean up test files; remove unused imports, improve type an…
Browse files Browse the repository at this point in the history
…notations, and enhance mock request handling
  • Loading branch information
Zoltan Erdos committed Feb 2, 2025
1 parent 89e5929 commit e32d3bd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
7 changes: 3 additions & 4 deletions packages/spike.land/src/anthropicHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { type Mock, vi } from "vitest";
import { base64Encode, handleAnthropicRequest } from "./anthropicHandler";
import { handleCMSIndexRequest } from "./chat";
import type Env from "./env";
import { KVLogger } from "./Logs";
import { handleCORS, readRequestBody } from "./utils";
import { readRequestBody } from "./utils";

// Mock dependencies
vi.mock("@anthropic-ai/sdk", () => {
Expand Down Expand Up @@ -67,8 +66,8 @@ describe("AnthropicHandler", () => {
};

mockCtx = {
waitUntil: vi.fn(),
} as any;
waitUntil: vi.fn() as any,
} as ExecutionContext;
});

describe("base64Encode", () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/spike.land/src/openaiHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import OpenAI from "openai";
import { beforeEach, describe, expect, it } from "vitest";
import { type Mock, vi } from "vitest";
import type Env from "./env";
import { KVLogger } from "./Logs";
import { handleGPT4Request } from "./openaiHandler";
import { handleCORS, readRequestBody } from "./utils";
import { readRequestBody } from "./utils";

// Mock dependencies
vi.mock("openai", () => {
Expand Down Expand Up @@ -60,8 +59,8 @@ describe("OpenAIHandler", () => {
};

mockCtx = {
waitUntil: vi.fn(),
} as any;
waitUntil: vi.fn() as any,
} as ExecutionContext;
});

describe("Text-to-Speech (TTS) Request", () => {
Expand Down
39 changes: 28 additions & 11 deletions packages/spike.land/src/r2bucket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import type {
Iso3166Alpha2Code,
R2Bucket,
R2Object,
Request as CloudflareRequest,
Request,
DurableObjectNamespace,
KVNamespace,
Ai,
AiModels,
AiModelsSearchParams,
AiModelsSearchObject,
Socket,
SocketAddress,
SocketOptions,
} from "@cloudflare/workers-types";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Mock } from "vitest";
Expand Down Expand Up @@ -107,15 +105,15 @@ describe("R2BucketHandler", () => {
const createMockRequest = (
method: string,
url = "https://example.com/test-key",
): CloudflareRequest => {
) => {
// Create a custom Headers object that mimics CloudflareHeaders
const mockHeaders: CloudflareHeaders = Object.assign(new Headers(), {
getAll: vi.fn().mockReturnValue([]),
});

// Create a mock Cloudflare request with all required properties
const mockCfProperties: IncomingRequestCfProperties = {
asn: "0",
const mockCfProperties: IncomingRequestCfProperties<unknown> = {
asn: "0" as any,
asOrganization: "Test Org",
colo: "Test Colo",
edgeRequestKeepAliveStatus: 0,
Expand All @@ -132,16 +130,29 @@ describe("R2BucketHandler", () => {
country: "US" as Iso3166Alpha2Code,
latitude: "0",
longitude: "0",
hostMetadata: "",
postalCode: "",
clientTrustScore: 0,
metroCode: "",
timezone: "",
httpProtocol: "HTTP/1.1",
tlsCipher: "",
botManagement: {
score: 0,
ja3Hash: "",
verifiedBot: false,
corporateProxy: false,
staticResource: false,
detectionIds: []
}
};

return new Request(url, {
const mockRequest = {
method,
url,
headers: mockHeaders,
cf: mockCfProperties,
}) as CloudflareRequest;
} as Request<unknown, IncomingRequestCfProperties<unknown>>;
return mockRequest;
};

describe("PUT Request Handling", () => {
Expand All @@ -150,7 +161,13 @@ describe("R2BucketHandler", () => {
const mockRequest = createMockRequest("PUT");
Object.defineProperty(mockRequest, "body", { value: mockBlob });

const response = await R2BucketHandler.fetch!(mockRequest, mockEnv, {} as ExecutionContext);
const response = await R2BucketHandler.fetch!(
mockRequest,
mockEnv,
{} as ExecutionContext,
)

//!(mockRequest as , mockEnv, {} as ExecutionContext);

expect(mockEnv.R2.put).toHaveBeenCalledWith("test-key", mockBlob);
expect(response.status).toBe(200);
Expand Down

0 comments on commit e32d3bd

Please sign in to comment.