Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/evaluate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
import { createDefaultScheduler } from "@intx/inference";

import type { EvaluateDeps } from "./client";
Expand Down Expand Up @@ -168,6 +168,32 @@ describe("evaluate without a transport", () => {
});
});

describe("evaluate with the default transport", () => {
test("calls the global fetch unbound, as browsers require", async () => {
const fetchSpy = spyOn(globalThis, "fetch").mockImplementation(
Object.assign(
function browserLikeFetch(this: unknown): Promise<Response> {
if (this !== undefined && this !== globalThis) {
throw new TypeError("Illegal invocation");
}
return Promise.reject(new TypeError("offline"));
},
{ preconnect: globalThis.fetch.preconnect },
),
);
try {
const result = await evaluate({
state: {},
questions,
config: { apiKey: "key" },
});
expect(result).toMatchObject({ reason: "network", detail: "offline" });
} finally {
fetchSpy.mockRestore();
}
});
});

// Records the bearer key and timeout of the one POST `evaluate` makes; the
// POST fails at once, so each call ends in a network fallback.
function recordingDeps(): {
Expand Down
3 changes: 2 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ export async function evaluate(
options?: EvaluateOptions,
): Promise<EvaluateResult | FallbackResult> {
const deps: EvaluateDeps = options?.deps ?? {
fetch: globalThis.fetch,
// Browsers reject fetch called as a method of another object.
fetch: (input, init) => fetch(input, init),
scheduler: createDefaultScheduler(),
};
const start = deps.scheduler.now();
Expand Down
Loading