mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-11 20:43:42 +08:00
test: add unit test
This commit is contained in:
52
app/utils/__tests__/model.test.ts
Normal file
52
app/utils/__tests__/model.test.ts
Normal 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: "",
|
||||
});
|
||||
});
|
||||
});
|
@@ -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(",")
|
||||
|
Reference in New Issue
Block a user