Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
tests with mocked fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Bam4d committed Dec 22, 2023
1 parent df8cc10 commit 3cd24d7
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 30 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/build_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:

jobs:

lint:
lint_and_test:
runs-on: ubuntu-latest

steps:
Expand All @@ -34,13 +34,18 @@ jobs:
run: |
npm install
# Ruff
# Eslint
- name: ESlint check
run: |
./node_modules/.bin/eslint .
npm lint
# Run tests
- name: Run tests
run: |
npm test
publish:
needs: lint
needs: lint_and_test
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags')

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "module",
"main": "src/client.js",
"scripts": {
"lint": "./node_modules/.bin/eslint .",
"test": "node --experimental-vm-modules node_modules/.bin/jest"
},
"jest": {
Expand Down
102 changes: 76 additions & 26 deletions tests/client.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import MistralClient from '../src/client';
import jest from 'jest-mock';
import {
mockListModels,
mockFetch,
mockChatResponseStreamingPayload,
mockEmbeddingResponsePayload,
mockChatResponsePayload,
mockFetchStream,
} from './utils';

// Test the list models endpoint
describe('Mistral Client', () => {
Expand All @@ -8,37 +15,80 @@ describe('Mistral Client', () => {
client = new MistralClient();
});

const mockFetch = (status, payload) => {
return jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve(payload),
text: () => Promise.resolve(JSON.stringify(payload)),
status,
}),
);
};

describe('listModels()', () => {
it('should return a list of models', async() => {
describe('chat()', () => {
it('should return a chat response object', async() => {
// Mock the fetch function
globalThis.fetch = mockFetch(200, {
models: [
'mistral-tiny',
'mistral-small',
'mistral-large',
'mistral-mega',
const mockResponse = mockChatResponsePayload();
globalThis.fetch = mockFetch(200, mockResponse);

const response = await client.chat({
model: 'mistral-small',
messages: [
{
role: 'user',
content: 'What is the best French cheese?',
},
],
});
expect(response).toEqual(mockResponse);
});
});

describe('chatStream()', () => {
it('should return parsed, streamed response', async() => {
// Mock the fetch function
const mockResponse = mockChatResponseStreamingPayload();
globalThis.fetch = mockFetchStream(200, mockResponse);

const models = await client.listModels();
expect(models).toEqual({
models: [
'mistral-tiny',
'mistral-small',
'mistral-large',
'mistral-mega',
const response = await client.chatStream({
model: 'mistral-small',
messages: [
{
role: 'user',
content: 'What is the best French cheese?',
},
],
});

const parsedResponse = [];
for await (const r of response) {
parsedResponse.push(r);
}

expect(parsedResponse.length).toEqual(11);
});
});

describe('embeddings()', () => {
it('should return embeddings', async() => {
// Mock the fetch function
const mockResponse = mockEmbeddingResponsePayload();
globalThis.fetch = mockFetch(200, mockResponse);

const response = await client.listModels();
expect(response).toEqual(mockResponse);
});
});

describe('embeddings() batched', () => {
it('should return batched embeddings', async() => {
// Mock the fetch function
const mockResponse = mockEmbeddingResponsePayload(10);
globalThis.fetch = mockFetch(200, mockResponse);

const response = await client.listModels();
expect(response).toEqual(mockResponse);
});
});

describe('listModels()', () => {
it('should return a list of models', async() => {
// Mock the fetch function
const mockResponse = mockListModels();
globalThis.fetch = mockFetch(200, mockResponse);

const response = await client.listModels();
expect(response).toEqual(mockResponse);
});
});
});
Loading

0 comments on commit 3cd24d7

Please sign in to comment.