Skip to content

Commit 3d643f7

Browse files
authored
perf(test): reduce LM Studio stream suite setup (#124136)
1 parent c9da900 commit 3d643f7

1 file changed

Lines changed: 25 additions & 32 deletions

File tree

‎extensions/lmstudio/src/stream.test.ts‎

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
22
import { createAssistantMessageEventStream } from "openclaw/plugin-sdk/llm";
33
// Lmstudio tests cover stream plugin behavior.
44
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
5-
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
66

77
let wrapLmstudioInferencePreload: typeof import("./stream.js").wrapLmstudioInferencePreload;
8+
let defaultBaseUrl: string;
9+
let defaultBaseUrlSequence = 0;
810

911
const ensureLmstudioModelLoadedMock = vi.hoisted(() => vi.fn());
1012
const resolveLmstudioProviderHeadersMock = vi.hoisted(() =>
@@ -14,27 +16,17 @@ const resolveLmstudioRuntimeApiKeyMock = vi.hoisted(() =>
1416
vi.fn(async (_params?: unknown) => undefined),
1517
);
1618

17-
vi.mock("./models.fetch.js", async (importOriginal) => {
18-
const actual = await importOriginal<typeof import("./models.fetch.js")>();
19-
return {
20-
...actual,
21-
ensureLmstudioModelLoaded: (params: unknown) => ensureLmstudioModelLoadedMock(params),
22-
};
23-
});
19+
vi.mock("./models.fetch.js", () => ({
20+
ensureLmstudioModelLoaded: (params: unknown) => ensureLmstudioModelLoadedMock(params),
21+
}));
2422

25-
vi.mock("./runtime.js", async (importOriginal) => {
26-
const actual = await importOriginal<typeof import("./runtime.js")>();
27-
return {
28-
...actual,
29-
resolveLmstudioProviderHeaders: (params: unknown) => resolveLmstudioProviderHeadersMock(params),
30-
resolveLmstudioRuntimeApiKey: (params: unknown) => resolveLmstudioRuntimeApiKeyMock(params),
31-
};
32-
});
23+
vi.mock("./runtime.js", () => ({
24+
resolveLmstudioProviderHeaders: (params: unknown) => resolveLmstudioProviderHeadersMock(params),
25+
resolveLmstudioRuntimeApiKey: (params: unknown) => resolveLmstudioRuntimeApiKeyMock(params),
26+
}));
3327

34-
afterAll(() => {
35-
vi.doUnmock("./models.fetch.js");
36-
vi.doUnmock("./runtime.js");
37-
vi.resetModules();
28+
beforeAll(async () => {
29+
({ wrapLmstudioInferencePreload } = await import("./stream.js"));
3830
});
3931

4032
type StreamEvent = { type: string } & Record<string, unknown>;
@@ -143,7 +135,7 @@ function createWrappedLmstudioStream(
143135
models: {
144136
providers: {
145137
lmstudio: {
146-
baseUrl: params?.baseUrl ?? "http://localhost:1234",
138+
baseUrl: params?.baseUrl ?? defaultBaseUrl,
147139
models: [],
148140
},
149141
},
@@ -190,9 +182,10 @@ function runWrappedLmstudioStream(
190182
}
191183

192184
describe("lmstudio stream wrapper", () => {
193-
beforeEach(async () => {
194-
vi.resetModules();
195-
({ wrapLmstudioInferencePreload } = await import("./stream.js"));
185+
beforeEach(() => {
186+
// Production preload state is keyed by base URL, model, and context length.
187+
// Give each test a real cache namespace while preserving within-test reuse.
188+
defaultBaseUrl = `http://lmstudio-test-${defaultBaseUrlSequence++}.localhost:1234`;
196189
});
197190

198191
afterEach(() => {
@@ -238,7 +231,7 @@ describe("lmstudio stream wrapper", () => {
238231
expectSingleDoneEvent(events);
239232
expectEnsureLoadedFields({
240233
modelKey: variantKey,
241-
baseUrl: "http://localhost:1234/v1",
234+
baseUrl: `${defaultBaseUrl}/v1`,
242235
});
243236
expectBaseStreamModelFields(baseStream, {
244237
provider: "lmstudio",
@@ -305,7 +298,7 @@ describe("lmstudio stream wrapper", () => {
305298
models: {
306299
providers: {
307300
lmstudio: {
308-
baseUrl: "http://localhost:1234",
301+
baseUrl: defaultBaseUrl,
309302
models: [],
310303
},
311304
},
@@ -388,7 +381,7 @@ describe("lmstudio stream wrapper", () => {
388381
models: {
389382
providers: {
390383
lmstudio: {
391-
baseUrl: "http://localhost:1234",
384+
baseUrl: defaultBaseUrl,
392385
params: { preload: false },
393386
models: [],
394387
},
@@ -438,7 +431,7 @@ describe("lmstudio stream wrapper", () => {
438431
models: {
439432
providers: {
440433
lmstudio: {
441-
baseUrl: "http://localhost:1234",
434+
baseUrl: defaultBaseUrl,
442435
models: [],
443436
},
444437
},
@@ -557,7 +550,7 @@ describe("lmstudio stream wrapper", () => {
557550
models: {
558551
providers: {
559552
lmstudio: {
560-
baseUrl: "http://localhost:1234",
553+
baseUrl: defaultBaseUrl,
561554
models: [],
562555
},
563556
},
@@ -634,7 +627,7 @@ describe("lmstudio stream wrapper", () => {
634627
models: {
635628
providers: {
636629
lmstudio: {
637-
baseUrl: "http://localhost:1234",
630+
baseUrl: defaultBaseUrl,
638631
models: [],
639632
},
640633
},
@@ -720,7 +713,7 @@ describe("lmstudio stream wrapper", () => {
720713
models: {
721714
providers: {
722715
lmstudio: {
723-
baseUrl: "http://localhost:1234",
716+
baseUrl: defaultBaseUrl,
724717
models: [],
725718
},
726719
},
@@ -806,7 +799,7 @@ describe("lmstudio stream wrapper", () => {
806799
models: {
807800
providers: {
808801
lmstudio: {
809-
baseUrl: "http://localhost:1234",
802+
baseUrl: defaultBaseUrl,
810803
params: { preload: false },
811804
models: [],
812805
},

0 commit comments

Comments
 (0)