Skip to content

Commit 6d466bb

Browse files
committed
Add a jest test on settings
1 parent b8e1ba6 commit 6d466bb

File tree

7 files changed

+4518
-103
lines changed

7 files changed

+4518
-103
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,6 @@ Untitled*.ipynb
130130
# Integration tests
131131
ui-tests/test-results/
132132
ui-tests/playwright-report/
133+
134+
# jest test reports
135+
junit.xml

babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Copyright (c) Jupyter Development Team.
3+
* Distributed under the terms of the Modified BSD License.
4+
*/
5+
6+
module.exports = require('@jupyterlab/testing/lib/babel-config');

jest.config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Jupyter Development Team.
3+
* Distributed under the terms of the Modified BSD License.
4+
*/
5+
6+
const jestJupyterLab = require('@jupyterlab/testing/lib/jest-config');
7+
8+
const esModules = [
9+
'@codemirror',
10+
'@jupyterlab/',
11+
'exenv-es6',
12+
'lib0',
13+
'nanoid',
14+
'vscode-ws-jsonrpc',
15+
].join('|');
16+
17+
const baseConfig = jestJupyterLab(__dirname);
18+
19+
module.exports = {
20+
...baseConfig,
21+
automock: false,
22+
collectCoverageFrom: [
23+
'src/**/*.{ts,tsx}',
24+
'!src/**/*.d.ts',
25+
'!src/**/.ipynb_checkpoints/*'
26+
],
27+
coverageReporters: ['lcov', 'text'],
28+
testRegex: 'src/.*/.*.spec.ts[x]?$',
29+
transformIgnorePatterns: [`/node_modules/(?!${esModules}).+`]
30+
};

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"settings:check": "node ./scripts/settings-checker.js",
5252
"stylelint": "jlpm stylelint:check --fix",
5353
"stylelint:check": "stylelint --cache \"style/**/*.css\"",
54+
"test": "jest --coverage",
5455
"watch": "run-p watch:src watch:labextension",
5556
"watch:src": "tsc -w --sourceMap",
5657
"watch:labextension": "jupyter labextension watch ."
@@ -90,8 +91,10 @@
9091
},
9192
"devDependencies": {
9293
"@jupyterlab/builder": "^4.4.0",
94+
"@jupyterlab/testing": "^4.4.0",
9395
"@stylistic/eslint-plugin": "^3.0.1",
9496
"@types/chrome": "^0.0.304",
97+
"@types/jest": "^29.2.0",
9598
"@types/json-schema": "^7.0.11",
9699
"@types/react": "^18.0.26",
97100
"@types/react-addons-linked-state-mixin": "^0.14.22",
@@ -102,6 +105,8 @@
102105
"eslint": "^8.36.0",
103106
"eslint-config-prettier": "^8.8.0",
104107
"eslint-plugin-prettier": "^5.0.0",
108+
"jest": "^29.2.0",
109+
"json-schema-library": "^10.1.2",
105110
"npm-run-all": "^4.1.5",
106111
"prettier": "^3.0.0",
107112
"rimraf": "^5.0.1",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { compileSchema, SchemaNode } from 'json-schema-library';
2+
import { ReadableStream } from 'web-streams-polyfill';
3+
// @ts-expect-error
4+
globalThis.ReadableStream = ReadableStream;
5+
// if (typeof global.ReadableStream === undefined) {
6+
// global.ReadableStream = ReadableStream;
7+
// }
8+
9+
import { ChatAnthropic } from '@langchain/anthropic';
10+
// import { ChatWebLLM } from '@langchain/community/chat_models/webllm';
11+
import { ChromeAI } from '@langchain/community/experimental/llms/chrome_ai';
12+
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
13+
import { ChatMistralAI } from '@langchain/mistralai';
14+
import { ChatOllama } from '@langchain/ollama';
15+
import { ChatOpenAI } from '@langchain/openai';
16+
17+
// Import Settings
18+
import AnthropicSettings from '../default-providers/Anthropic/settings-schema.json';
19+
import ChromeAISettings from '../default-providers/ChromeAI/settings-schema.json';
20+
import GeminiSettings from '../default-providers/Gemini/settings-schema.json';
21+
import MistralAISettings from '../default-providers/MistralAI/settings-schema.json';
22+
import OllamaAISettings from '../default-providers/Ollama/settings-schema.json';
23+
import OpenAISettings from '../default-providers/OpenAI/settings-schema.json';
24+
// import WebLLMSettings from '../default-providers/WebLLM/settings-schema.json';
25+
import { IAIProvider, IType } from '../tokens';
26+
import { BaseChatModel, BaseChatModelCallOptions } from '@langchain/core/language_models/chat_models';
27+
import { AIMessageChunk } from '@langchain/core/messages';
28+
29+
interface IAIProviderWithChat extends IAIProvider {
30+
chat: IType<BaseChatModel<BaseChatModelCallOptions, AIMessageChunk>>;
31+
}
32+
const AIProviders: IAIProviderWithChat[] = [
33+
{
34+
name: 'Anthropic',
35+
chat: ChatAnthropic,
36+
settingsSchema: AnthropicSettings,
37+
},
38+
{
39+
name: 'ChromeAI',
40+
// TODO: fix
41+
// @ts-expect-error: missing properties
42+
chat: ChromeAI,
43+
settingsSchema: ChromeAISettings,
44+
},
45+
{
46+
name: 'MistralAI',
47+
chat: ChatMistralAI,
48+
settingsSchema: MistralAISettings
49+
},
50+
{
51+
name: 'Ollama',
52+
chat: ChatOllama,
53+
settingsSchema: OllamaAISettings
54+
},
55+
{
56+
name: 'Gemini',
57+
chat: ChatGoogleGenerativeAI,
58+
settingsSchema: GeminiSettings
59+
},
60+
{
61+
name: 'OpenAI',
62+
chat: ChatOpenAI,
63+
settingsSchema: OpenAISettings
64+
},
65+
// {
66+
// name: 'WebLLM',
67+
// chat: ChatWebLLM,
68+
// settingsSchema: WebLLMSettings
69+
// }
70+
];
71+
72+
it('test provider settings', () => {
73+
AIProviders.forEach(provider => {
74+
console.log(`PROVIDER: ${provider.name}`);
75+
const schema: SchemaNode = compileSchema(provider.settingsSchema);
76+
const defaultSettings = schema.getData(undefined, { addOptionalProps: true });
77+
78+
// Set a value for apiKey to avoid errors at instantiation.
79+
if (defaultSettings.apiKey !== undefined) {
80+
defaultSettings.apiKey = 'abc';
81+
}
82+
defaultSettings.bnla = 'ble';
83+
const model = new provider.chat(defaultSettings);
84+
85+
Object.entries(defaultSettings).forEach(([key, value]) => {
86+
try {
87+
// @ts-expect-error
88+
expect(JSON.stringify(model[key])).toEqual(JSON.stringify(value));
89+
} catch (err) {
90+
// @ts-expect-error
91+
err.message = `${err.message}\nproperty: ${key}\n`;
92+
throw err; // throw the error so test fails as expected
93+
}
94+
95+
});
96+
});
97+
});

tsconfig.test.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"types": ["jest"]
5+
}
6+
}

0 commit comments

Comments
 (0)