Skip to content

Commit 968e955

Browse files
committed
test: cover wire shape, auth, empty input, batchSize, Retry-After date, override, and timeout with signal (CL-9107)
1 parent 1ff86ca commit 968e955

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎src/embed.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,56 @@ test("classifies a 200 that is not JSON instead of leaking a SyntaxError", async
111111
reason: { category: "protocol_mismatch" },
112112
});
113113
});
114+
115+
test("sends only configured fields to {baseURL}/embeddings", async () => {
116+
harness = setupHarness();
117+
const stream = harness.scenario.createStream();
118+
harness.scenario.whenRequestBodyMatches(
119+
(body, req) =>
120+
req.url === "https://embed.example/v1/embeddings" &&
121+
!req.headers.has("authorization") &&
122+
body === JSON.stringify({ model: CONFIG.model, input: ["a"] }),
123+
stream,
124+
);
125+
stream.enqueueAll(
126+
[new TextEncoder().encode('{"data":[{"index":0,"embedding":[1]}]}')],
127+
{ startAt: 1 },
128+
);
129+
const pending = embedTexts(["a"], CONFIG, { deps: harness.deps });
130+
expect(await run(harness, pending)).toEqual([[1]]);
131+
});
132+
133+
test("sends a bearer token only when apiKey is set", async () => {
134+
harness = setupHarness();
135+
const stream = harness.scenario.createStream();
136+
harness.scenario.whenRequestMatches(
137+
(req) => req.headers.get("authorization") === "Bearer sk-test",
138+
stream,
139+
);
140+
stream.enqueueAll(
141+
[new TextEncoder().encode('{"data":[{"index":0,"embedding":[1]}]}')],
142+
{ startAt: 1 },
143+
);
144+
const pending = embedTexts(
145+
["a"],
146+
{ ...CONFIG, apiKey: "sk-test" },
147+
{ deps: harness.deps },
148+
);
149+
expect(await run(harness, pending)).toEqual([[1]]);
150+
});
151+
152+
test("returns [] for empty input without a request", async () => {
153+
harness = setupHarness();
154+
const pending = embedTexts([], CONFIG, { deps: harness.deps });
155+
expect(await run(harness, pending)).toEqual([]);
156+
});
157+
158+
test("rejects a non-positive batchSize before any request", async () => {
159+
harness = setupHarness();
160+
for (const batchSize of [0, -1, 1.5]) {
161+
await expect(
162+
embedTexts(["a"], { ...CONFIG, batchSize }, { deps: harness.deps }),
163+
).rejects.toThrow();
164+
}
165+
await harness.run();
166+
});

‎tests/retry.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,46 @@ test("rejects a 401 without retrying", async () => {
5353
reason: { category: "credential_failure", statusCode: 401 },
5454
});
5555
});
56+
57+
test("waits until an HTTP-date Retry-After", async () => {
58+
harness = setupHarness({ enableInferenceTimers: true });
59+
const at = new Date(Date.now() + 20_000).toUTCString();
60+
reply(harness, "rate limited", 429, { "retry-after": at });
61+
reply(harness, JSON.stringify({ data: [{ index: 0, embedding: [1] }] }), 200);
62+
63+
const pending = embedTexts(["a"], CONFIG, { deps: harness.deps });
64+
await harness.run();
65+
66+
expect(await pending).toEqual([[1]]);
67+
expect(harness.clock.now()).toBeGreaterThanOrEqual(19_000);
68+
});
69+
70+
test("honors a caller-supplied extractRetryAfterMs", async () => {
71+
harness = setupHarness({ enableInferenceTimers: true });
72+
reply(harness, "rate limited", 429);
73+
reply(harness, JSON.stringify({ data: [{ index: 0, embedding: [1] }] }), 200);
74+
75+
const pending = embedTexts(["a"], CONFIG, {
76+
deps: harness.deps,
77+
extractRetryAfterMs: () => 45_000,
78+
});
79+
await harness.run();
80+
81+
expect(await pending).toEqual([[1]]);
82+
expect(harness.clock.now()).toBeGreaterThanOrEqual(45_000);
83+
});
84+
85+
test("applies timeoutMs even when the caller passes a signal", async () => {
86+
harness = setupHarness();
87+
const stream = harness.scenario.createStream();
88+
harness.scenario.whenRequestMatches(() => true, stream);
89+
90+
const pending = embedTexts(
91+
["a"],
92+
{ ...CONFIG, timeoutMs: 50 },
93+
{ deps: harness.deps, signal: new AbortController().signal },
94+
);
95+
await harness.run();
96+
97+
await expect(pending).rejects.toBeInstanceOf(EmbeddingRequestError);
98+
});

0 commit comments

Comments
 (0)