Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

share codex translate test helpers

Raine Virta7cab97d92053
diff --git a/src/providers/codex/translate/reducer.test.ts b/src/providers/codex/translate/reducer.test.ts
index 501f66f..2d2f2d7 100644
--- a/src/providers/codex/translate/reducer.test.ts
+++ b/src/providers/codex/translate/reducer.test.ts
@@ -1,27 +1,6 @@
import { describe, expect, it } from "bun:test";
import { reduceUpstream } from "./reducer.ts";
-
-const silentLog = {
- debug: () => {},
- info: () => {},
- warn: () => {},
- error: () => {},
- child: () => silentLog,
-};
-
-function sse(type: string, payload: Record<string, unknown>): string {
- return `data: ${JSON.stringify({ type, ...payload })}\n\n`;
-}
-
-function upstreamFromChunks(chunks: string[]): ReadableStream<Uint8Array> {
- const encoder = new TextEncoder();
- return new ReadableStream<Uint8Array>({
- start(controller) {
- for (const chunk of chunks) controller.enqueue(encoder.encode(chunk));
- controller.close();
- },
- });
-}
+import { sse, silentLog, upstreamFromChunks } from "./test-helpers.ts";
async function events(chunks: string[]) {
const out = [];
diff --git a/src/providers/codex/translate/stream.test.ts b/src/providers/codex/translate/stream.test.ts
index 964eb85..5acf960 100644
--- a/src/providers/codex/translate/stream.test.ts
+++ b/src/providers/codex/translate/stream.test.ts
@@ -1,65 +1,13 @@
import { describe, expect, it } from "bun:test";
import { translateStream } from "./stream.ts";
-
-const silentLog = {
- debug: () => {},
- info: () => {},
- warn: () => {},
- error: () => {},
- child: () => silentLog,
-};
-
-function sse(type: string, payload: Record<string, unknown>): string {
- return `data: ${JSON.stringify({ type, ...payload })}\n\n`;
-}
-
-function upstreamFromChunks(chunks: string[], advanceMs = 0): ReadableStream<Uint8Array> {
- const encoder = new TextEncoder();
- let index = 0;
- return new ReadableStream<Uint8Array>({
- pull(controller) {
- if (index >= chunks.length) {
- controller.close();
- return;
- }
- now += advanceMs;
- controller.enqueue(encoder.encode(chunks[index++]));
- },
- });
-}
-
-function abortingUpstream(err: Error): ReadableStream<Uint8Array> {
- return new ReadableStream<Uint8Array>({
- pull(controller) {
- controller.error(err);
- },
- });
-}
-
-function upstreamThatErrorsAfterChunks(chunks: string[], err: Error): ReadableStream<Uint8Array> {
- const encoder = new TextEncoder();
- let index = 0;
- return new ReadableStream<Uint8Array>({
- pull(controller) {
- if (index >= chunks.length) {
- controller.error(err);
- return;
- }
- controller.enqueue(encoder.encode(chunks[index++]));
- },
- });
-}
-
-async function collect(stream: ReadableStream<Uint8Array>): Promise<string> {
- const reader = stream.getReader();
- const decoder = new TextDecoder();
- let out = "";
- while (true) {
- const { done, value } = await reader.read();
- if (done) return out;
- out += decoder.decode(value, { stream: true });
- }
-}
+import {
+ abortingUpstream,
+ collect,
+ sse,
+ silentLog,
+ upstreamFromChunks,
+ upstreamThatErrorsAfterChunks,
+} from "./test-helpers.ts";
let now = 0;
const realNow = Date.now;
@@ -83,7 +31,9 @@ describe("translateStream", () => {
];
const output = await collect(
- translateStream(upstreamFromChunks(chunks, 16_000), {
+ translateStream(upstreamFromChunks(chunks, () => {
+ now += 16_000;
+ }), {
messageId: "msg_1",
model: "gpt-5.5",
log: silentLog,
@@ -118,7 +68,9 @@ describe("translateStream", () => {
];
const output = await collect(
- translateStream(upstreamFromChunks(chunks, 16_000), {
+ translateStream(upstreamFromChunks(chunks, () => {
+ now += 16_000;
+ }), {
messageId: "msg_1",
model: "gpt-5.5",
log: silentLog,
@@ -202,7 +154,9 @@ describe("translateStream", () => {
];
const output = await collect(
- translateStream(upstreamFromChunks(chunks, 121_000), {
+ translateStream(upstreamFromChunks(chunks, () => {
+ now += 121_000;
+ }), {
messageId: "msg_1",
model: "gpt-5.5",
log: silentLog,
diff --git a/src/providers/codex/translate/test-helpers.ts b/src/providers/codex/translate/test-helpers.ts
new file mode 100644
index 0000000..171d2ff
--- /dev/null
+++ b/src/providers/codex/translate/test-helpers.ts
@@ -0,0 +1,70 @@
+import type { Logger } from "../../../log.ts";
+
+export const silentLog: Logger = (() => {
+ const log: Logger = {
+ debug: () => {},
+ info: () => {},
+ warn: () => {},
+ error: () => {},
+ child: () => log,
+ };
+ return log;
+})();
+
+export function sse(type: string, payload: Record<string, unknown>): string {
+ return `data: ${JSON.stringify({ type, ...payload })}\n\n`;
+}
+
+export function upstreamFromChunks(
+ chunks: string[],
+ onChunk?: () => void,
+): ReadableStream<Uint8Array> {
+ const encoder = new TextEncoder();
+ let index = 0;
+ return new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (index >= chunks.length) {
+ controller.close();
+ return;
+ }
+ onChunk?.();
+ controller.enqueue(encoder.encode(chunks[index++]));
+ },
+ });
+}
+
+export function abortingUpstream(err: Error): ReadableStream<Uint8Array> {
+ return new ReadableStream<Uint8Array>({
+ pull(controller) {
+ controller.error(err);
+ },
+ });
+}
+
+export function upstreamThatErrorsAfterChunks(
+ chunks: string[],
+ err: Error,
+): ReadableStream<Uint8Array> {
+ const encoder = new TextEncoder();
+ let index = 0;
+ return new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (index >= chunks.length) {
+ controller.error(err);
+ return;
+ }
+ controller.enqueue(encoder.encode(chunks[index++]));
+ },
+ });
+}
+
+export async function collect(stream: ReadableStream<Uint8Array>): Promise<string> {
+ const reader = stream.getReader();
+ const decoder = new TextDecoder();
+ let out = "";
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) return out;
+ out += decoder.decode(value, { stream: true });
+ }
+}