test: add unit test

This commit is contained in:
rxliuli
2024-08-26 02:23:58 +08:00
parent 78111a1bc1
commit 4d8047d0d9
4 changed files with 490 additions and 25 deletions

View File

@@ -0,0 +1,52 @@
import { describe, it, expect } from "vitest";
import { parseModelName } from "../model";
describe("parseModelName", () => {
it('should parse a simple model name without "@"', () => {
const result = parseModelName("simpleModel");
expect(result).toEqual({
customModelName: "simpleModel",
customProviderName: "",
});
});
it('should parse a model name with "@"', () => {
const result = parseModelName("modelName@providerName");
expect(result).toEqual({
customModelName: "modelName",
customProviderName: "providerName",
});
});
it('should parse a quoted model name without "@"', () => {
const result = parseModelName('"quotedModel"');
expect(result).toEqual({
customModelName: "quotedModel",
customProviderName: "quotedModel",
});
});
it('should parse a quoted model name with "@"', () => {
const result = parseModelName('"quotedModel@providerName"');
expect(result).toEqual({
customModelName: "quotedModel@providerName",
customProviderName: "quotedModel@providerName",
});
});
it('should parse a model name with multiple "@" symbols', () => {
const result = parseModelName("modelName@providerName@extra");
expect(result).toEqual({
customModelName: "modelName",
customProviderName: "providerName@extra",
});
});
it("should handle incorrect format gracefully", () => {
const result = parseModelName("incorrectFormat@");
expect(result).toEqual({
customModelName: "incorrectFormat",
customProviderName: "",
});
});
});

View File

@@ -37,6 +37,25 @@ const sortModelTable = (models: ReturnType<typeof collectModels>) =>
}
});
export function parseModelName(name: string): {
customModelName: string;
customProviderName: string;
} {
let customModelName = name.split("@")[0];
let customProviderName = name.slice(customModelName.length + 1);
if (name.startsWith("'") || name.startsWith('"')) {
const match = name.match(/^(['"])(.*?)\1(@.*)?$/);
if (match) {
customModelName = match[2];
customProviderName = match[3]?.slice(1) || customModelName;
}
}
return { customModelName, customProviderName } as {
customModelName: string;
customProviderName: string;
};
}
export function collectModelTable(
models: readonly LLMModel[],
customModels: string,
@@ -62,28 +81,6 @@ export function collectModelTable(
};
});
function parseModelName(name: string): {
customModelName: string;
customProviderName: string;
} {
let customModelName, customProviderName;
if (name.startsWith("'") || name.startsWith('"')) {
const match = name.match(/^(['"])(.*?)\1(@.*)?$/);
if (match) {
customModelName = match[2];
customProviderName = match[3]?.slice(1) || customModelName;
} else {
[customModelName, customProviderName] = name.split("@");
}
} else {
[customModelName, customProviderName] = name.split("@");
}
return { customModelName, customProviderName } as {
customModelName: string;
customProviderName: string;
};
}
// server custom models
customModels
.split(",")