Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
extract cursor test helpers
Raine Virta425bc1e0897f
diff --git a/src/providers/cursor/client.test.ts b/src/providers/cursor/client.test.tsindex b0d9bf0..22e787c 100644--- a/src/providers/cursor/client.test.ts+++ b/src/providers/cursor/client.test.ts@@ -3,12 +3,15 @@ import { mkdtemp, readFile, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { decodeCursorStream, encodeConnectFrame, runCursorAgent } from "./client.ts";-import type { CursorProto, ProtoClass, ProtoMessage } from "./proto-loader.ts";+import {+ decodeFrameJson,+ fakeProtoMerged as fakeProto,+ frame,+ jsonBytes,+ streamFromChunks,+} from "./cursor-test-helpers.ts";import type { RequestContext } from "../types.ts";-const encoder = new TextEncoder();-const decoder = new TextDecoder();-describe("Cursor protocol client", () => {it("acks exec setup and KV messages on the HTTP/2 Run stream", async () => {const sentFrames: Uint8Array[] = [];@@ -479,60 +482,6 @@ describe("Cursor protocol client", () => {});});-const fakeProto: CursorProto = {- AgentServerMessage: jsonProtoClass(),- AgentClientMessage: jsonProtoClass(),-};--function jsonProtoClass(): ProtoClass {- return {- fromBinary(bytes: Uint8Array): ProtoMessage {- const json = JSON.parse(decoder.decode(bytes));- return messageFromJson(json);- },- fromJson(json: unknown): ProtoMessage {- return messageFromJson(json);- },- };-}--function messageFromJson(json: unknown): ProtoMessage {- return Object.assign(- {- toBinary(): Uint8Array {- return jsonBytes(json);- },- toJson(): unknown {- return json;- },- },- json && typeof json === "object" && !Array.isArray(json) ? json : {},- );-}--function frame(json: unknown): Uint8Array {- return encodeConnectFrame(jsonBytes(json));-}--function jsonBytes(json: unknown): Uint8Array {- return encoder.encode(JSON.stringify(json));-}--function decodeFrameJson(frame: Uint8Array): unknown {- const buf = Buffer.from(frame);- const len = buf.readUInt32BE(1);- return JSON.parse(decoder.decode(buf.subarray(5, 5 + len)));-}--function streamFromChunks(chunks: Uint8Array[]): ReadableStream<Uint8Array> {- return new ReadableStream<Uint8Array>({- start(controller) {- for (const chunk of chunks) controller.enqueue(chunk);- controller.close();- },- });-}-async function drain(stream: ReadableStream<Uint8Array>): Promise<void> {const reader = stream.getReader();try {diff --git a/src/providers/cursor/cursor-test-helpers.ts b/src/providers/cursor/cursor-test-helpers.tsnew file mode 100644index 0000000..affac99--- /dev/null+++ b/src/providers/cursor/cursor-test-helpers.ts@@ -0,0 +1,75 @@+import { encodeConnectFrame } from "./client.ts";+import type { CursorProto, ProtoClass, ProtoMessage } from "./proto-loader.ts";++const encoder = new TextEncoder();+const decoder = new TextDecoder();++export type JsonProtoClassOptions = {+ mergeFields?: boolean;+};++export function jsonProtoClass(options: JsonProtoClassOptions = {}): ProtoClass {+ const { mergeFields = false } = options;+ return {+ fromBinary(bytes: Uint8Array): ProtoMessage {+ return messageFromJson(JSON.parse(decoder.decode(bytes)), options);+ },+ fromJson(json: unknown): ProtoMessage {+ return messageFromJson(json, options);+ },+ };+}++export function messageFromJson(+ json: unknown,+ options: JsonProtoClassOptions = {},+): ProtoMessage {+ const { mergeFields = false } = options;+ const message: ProtoMessage = {+ toBinary(): Uint8Array {+ return jsonBytes(json);+ },+ toJson(): unknown {+ return json;+ },+ };+ if (!mergeFields) return message;+ return Object.assign(+ message,+ json && typeof json === "object" && !Array.isArray(json) ? json : {},+ );+}++export function createFakeProto(options: JsonProtoClassOptions = {}): CursorProto {+ const protoClass = jsonProtoClass(options);+ return {+ AgentServerMessage: protoClass,+ AgentClientMessage: protoClass,+ };+}++export const fakeProtoMerged = createFakeProto({ mergeFields: true });+export const fakeProto = createFakeProto();++export function frame(json: unknown): Uint8Array {+ return encodeConnectFrame(jsonBytes(json));+}++export function jsonBytes(json: unknown): Uint8Array {+ return encoder.encode(JSON.stringify(json));+}++export function decodeFrameJson(frameBytes: Uint8Array): unknown {+ const buf = Buffer.from(frameBytes);+ const len = buf.readUInt32BE(1);+ return JSON.parse(decoder.decode(buf.subarray(5, 5 + len)));+}++export function streamFromChunks(chunks: Uint8Array[]): ReadableStream<Uint8Array> {+ return new ReadableStream<Uint8Array>({+ start(controller) {+ for (const chunk of chunks) controller.enqueue(chunk);+ controller.close();+ },+ });+}diff --git a/src/providers/cursor/index.test.ts b/src/providers/cursor/index.test.tsindex e7d209f..2a315eb 100644--- a/src/providers/cursor/index.test.ts+++ b/src/providers/cursor/index.test.ts@@ -2,7 +2,13 @@ import { afterEach, describe, expect, it } from "bun:test";import { cursorProvider, createCursorProvider } from "./index.ts";import type { RequestContext } from "../types.ts";import { encodeConnectFrame, runCursorAgent } from "./client.ts";-import type { CursorProto, ProtoClass, ProtoMessage } from "./proto-loader.ts";+import {+ decodeFrameJson,+ fakeProtoMerged as fakeProto,+ frame,+ jsonBytes,+ streamFromChunks,+} from "./cursor-test-helpers.ts";import { parseSseStream } from "../../sse.ts";import { mkdir, mkdtemp, stat, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";@@ -712,58 +718,3 @@ function jwt(payload: Record<string, unknown>): string {"signature",].join(".");}--const encoder = new TextEncoder();-const decoder = new TextDecoder();--const fakeProto: CursorProto = {- AgentServerMessage: jsonProtoClass(),- AgentClientMessage: jsonProtoClass(),-};--function jsonProtoClass(): ProtoClass {- return {- fromBinary(bytes: Uint8Array): ProtoMessage {- return messageFromJson(JSON.parse(decoder.decode(bytes)));- },- fromJson(json: unknown): ProtoMessage {- return messageFromJson(json);- },- };-}--function messageFromJson(json: unknown): ProtoMessage {- return Object.assign(- {- toBinary(): Uint8Array {- return jsonBytes(json);- },- toJson(): unknown {- return json;- },- },- json && typeof json === "object" && !Array.isArray(json) ? json : {},- );-}--function frame(json: unknown): Uint8Array {- return encodeConnectFrame(jsonBytes(json));-}--function jsonBytes(json: unknown): Uint8Array {- return encoder.encode(JSON.stringify(json));-}--function decodeFrameJson(frame: Uint8Array): unknown {- const len = Buffer.from(frame).readUInt32BE(1);- return JSON.parse(decoder.decode(frame.slice(5, 5 + len)));-}--function streamFromChunks(chunks: Uint8Array[]): ReadableStream<Uint8Array> {- return new ReadableStream<Uint8Array>({- start(controller) {- for (const chunk of chunks) controller.enqueue(chunk);- controller.close();- },- });-}diff --git a/src/providers/cursor/translate/response.test.ts b/src/providers/cursor/translate/response.test.tsindex 697db4e..2f2140f 100644--- a/src/providers/cursor/translate/response.test.ts+++ b/src/providers/cursor/translate/response.test.ts@@ -1,8 +1,13 @@import { describe, expect, it } from "bun:test";import { gzipSync } from "node:zlib";import { parseSseStream } from "../../../sse.ts";-import type { CursorProto, ProtoClass, ProtoMessage } from "../proto-loader.ts";import { decodeCursorStream, encodeConnectFrame } from "../client.ts";+import {+ fakeProto,+ frame,+ jsonBytes,+ streamFromChunks,+} from "../cursor-test-helpers.ts";import {accumulateCursorResponse,cursorUsageToAnthropic,@@ -10,14 +15,6 @@ import {} from "./response.ts";import { createLogger } from "../../../log.ts";-const encoder = new TextEncoder();-const decoder = new TextDecoder();--const fakeProto: CursorProto = {- AgentServerMessage: jsonProtoClass(),- AgentClientMessage: jsonProtoClass(),-};-describe("Cursor response translation", () => {it("maps usage tokens including cache reads and writes", () => {expect(@@ -272,42 +269,3 @@ describe("Cursor response translation", () => {expect(events[2]?.data.error.message).toContain("free requests limit");});});--function jsonProtoClass(): ProtoClass {- return {- fromBinary(bytes: Uint8Array): ProtoMessage {- return messageFromJson(JSON.parse(decoder.decode(bytes)));- },- fromJson(json: unknown): ProtoMessage {- return messageFromJson(json);- },- };-}--function messageFromJson(json: unknown): ProtoMessage {- return {- toBinary(): Uint8Array {- return jsonBytes(json);- },- toJson(): unknown {- return json;- },- };-}--function frame(json: unknown): Uint8Array {- return encodeConnectFrame(jsonBytes(json));-}--function jsonBytes(json: unknown): Uint8Array {- return encoder.encode(JSON.stringify(json));-}--function streamFromChunks(chunks: Uint8Array[]): ReadableStream<Uint8Array> {- return new ReadableStream<Uint8Array>({- start(controller) {- for (const chunk of chunks) controller.enqueue(chunk);- controller.close();- },- });-}