v0.2.0
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const lmStudio = require("../lmstudio");
|
||||
|
||||
function jsonResponse(body, ok = true, status = 200, text = "") {
|
||||
return {
|
||||
ok,
|
||||
status,
|
||||
async json() { return body; },
|
||||
async text() { return text || JSON.stringify(body); }
|
||||
};
|
||||
}
|
||||
|
||||
test("warmUp accepts an exact loaded model key", async t => {
|
||||
const originalFetch = global.fetch;
|
||||
t.after(() => global.fetch = originalFetch);
|
||||
|
||||
const calls = [];
|
||||
global.fetch = async url => {
|
||||
calls.push(String(url));
|
||||
return jsonResponse({
|
||||
models: [{ key: "gemma-4-31b-it", loaded_instances: [{ id: "instance-1" }] }]
|
||||
});
|
||||
};
|
||||
|
||||
await lmStudio.warmUp({ model: "gemma-4-31b-it" });
|
||||
assert.equal(calls.length, 1);
|
||||
});
|
||||
|
||||
test("warmUp accepts an exact loaded instance id", async t => {
|
||||
const originalFetch = global.fetch;
|
||||
t.after(() => global.fetch = originalFetch);
|
||||
|
||||
global.fetch = async url => {
|
||||
if (String(url).endsWith("/api/v1/models")) {
|
||||
return jsonResponse({
|
||||
models: [{ key: "gemma-4-31b-it", loaded_instances: [{ id: "remote-instance" }] }]
|
||||
});
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${url}`);
|
||||
};
|
||||
|
||||
await lmStudio.warmUp({ model: "remote-instance" });
|
||||
});
|
||||
|
||||
|
||||
test("warmUp loads an exact installed model without requiring the compatibility model list", async t => {
|
||||
const originalFetch = global.fetch;
|
||||
t.after(() => global.fetch = originalFetch);
|
||||
|
||||
const calls = [];
|
||||
global.fetch = async (url, options = {}) => {
|
||||
const target = String(url);
|
||||
calls.push(target);
|
||||
if (target.endsWith("/api/v1/models")) {
|
||||
return jsonResponse({ models: [{ key: "gemma-4-31b-it", loaded_instances: [] }] });
|
||||
}
|
||||
if (target.endsWith("/api/v1/models/load")) {
|
||||
assert.deepEqual(JSON.parse(options.body), { model: "gemma-4-31b-it" });
|
||||
return jsonResponse({});
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${url}`);
|
||||
};
|
||||
|
||||
await lmStudio.warmUp({ model: "gemma-4-31b-it" });
|
||||
assert.equal(calls.length, 2);
|
||||
});
|
||||
|
||||
test("warmUp loads the exact requested model and fails fast when LM Studio refuses", async t => {
|
||||
const originalFetch = global.fetch;
|
||||
t.after(() => global.fetch = originalFetch);
|
||||
|
||||
let loadBody = null;
|
||||
global.fetch = async (url, options = {}) => {
|
||||
const target = String(url);
|
||||
if (target.endsWith("/api/v1/models")) return jsonResponse({ models: [] });
|
||||
if (target.endsWith("/v1/models")) return jsonResponse({ data: [] });
|
||||
if (target.endsWith("/api/v1/models/load")) {
|
||||
loadBody = JSON.parse(options.body);
|
||||
return jsonResponse({}, false, 500, "model not found");
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${url}`);
|
||||
};
|
||||
|
||||
await assert.rejects(
|
||||
() => lmStudio.warmUp({ model: "exact-model-name" }),
|
||||
error => {
|
||||
assert.equal(error.retryable, false);
|
||||
assert.match(error.message, /exact-model-name/);
|
||||
assert.match(error.message, /model not found/);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
assert.deepEqual(loadBody, { model: "exact-model-name" });
|
||||
});
|
||||
|
||||
function withFakeResponse(t, responseObject, inspectPayload = null) {
|
||||
const http = require("node:http");
|
||||
const { EventEmitter } = require("node:events");
|
||||
const originalRequest = http.request;
|
||||
t.after(() => http.request = originalRequest);
|
||||
|
||||
http.request = (options, onResponse) => {
|
||||
const request = new EventEmitter();
|
||||
request.destroy = () => {};
|
||||
request.write = payload => {
|
||||
if (inspectPayload) inspectPayload(JSON.parse(payload));
|
||||
};
|
||||
request.end = () => {
|
||||
const response = new EventEmitter();
|
||||
response.statusCode = 200;
|
||||
response.setEncoding = () => {};
|
||||
response.destroy = () => {};
|
||||
onResponse(response);
|
||||
process.nextTick(() => {
|
||||
response.emit("data", `data: ${JSON.stringify({ type: "response.completed", response: responseObject })}\n\n`);
|
||||
response.emit("end");
|
||||
});
|
||||
};
|
||||
return request;
|
||||
};
|
||||
}
|
||||
|
||||
test("LM Studio receives only Perry conversation fields and preserves exact model identity", async t => {
|
||||
let body = null;
|
||||
withFakeResponse(t, {
|
||||
id: "response-1",
|
||||
model: "exact-model",
|
||||
status: "completed",
|
||||
output: [{ type: "message", content: [{ text: "done" }] }],
|
||||
usage: { total_tokens: 12 }
|
||||
}, payload => body = payload);
|
||||
|
||||
const reply = await lmStudio.send({
|
||||
systemPrompt: "system",
|
||||
messages: [{ role: "user", content: "hello", reasoning: "do not send", providerField: "do not send" }]
|
||||
}, {
|
||||
model: "exact-model",
|
||||
reasoning: "high"
|
||||
});
|
||||
|
||||
assert.deepEqual(body.input, [{ role: "user", content: "hello" }]);
|
||||
assert.equal(body.model, "exact-model");
|
||||
assert.equal(body.instructions, "system");
|
||||
assert.equal("reasoning" in body, false);
|
||||
assert.equal(reply.content, "done");
|
||||
assert.equal(reply.provider.model, "exact-model");
|
||||
});
|
||||
|
||||
test("LM Studio rejects an explicitly different response model", async t => {
|
||||
withFakeResponse(t, {
|
||||
id: "response-2",
|
||||
model: "different-model",
|
||||
status: "completed",
|
||||
output: [{ type: "message", content: [{ text: "wrong worker" }] }]
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
() => lmStudio.send({ systemPrompt: null, messages: [] }, { model: "exact-model" }),
|
||||
error => {
|
||||
assert.equal(error.code, "PERRY_MODEL_MISMATCH");
|
||||
assert.equal(error.retryable, false);
|
||||
assert.equal(error.expectedModel, "exact-model");
|
||||
assert.equal(error.actualModel, "different-model");
|
||||
return true;
|
||||
}
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user