diff --git a/__tests__/ai/__snapshots__/model.test.js.snap b/__tests__/ai/__snapshots__/model.test.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..ddd0bfbf54fd210d20d9d9608b27857c5d0a5fa2 --- /dev/null +++ b/__tests__/ai/__snapshots__/model.test.js.snap @@ -0,0 +1,47 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`ai model > given no jwt sended > should respond with a proper body 1`] = ` +{ + "message": "No access token found. Access denied.", +} +`; + +exports[`ai model > given no matching model found > should respond with a proper body 1`] = ` +{ + "message": "model 'xxx' not found", +} +`; + +exports[`ai model > given no valid jwt sended > should respond with a proper body 1`] = ` +{ + "message": "Access token is no longer valid. Access denied.", +} +`; + +exports[`ai model > given required fields are missing > should respond with a proper body 1`] = ` +{ + "message": "Validation errors. Please check the error messages.", + "validationErrors": { + "model": "Required", + }, +} +`; + +exports[`ai model > given the inputs are valid > should respond with a proper body 1`] = ` +{ + "details": { + "families": [ + "llama", + ], + "family": "llama", + "format": "gguf", + "parameter_size": "8.0B", + "parent_model": "", + "quantization_level": "Q4_0", + }, + "license": Any<String>, + "modelfile": Any<String>, + "parameters": Any<String>, + "template": Any<String>, +} +`; diff --git a/__tests__/ai/model.test.js b/__tests__/ai/model.test.js new file mode 100644 index 0000000000000000000000000000000000000000..8a4536bedc8e9ddcd592c0a999cccff59f95744b --- /dev/null +++ b/__tests__/ai/model.test.js @@ -0,0 +1,185 @@ +// import vitest, supertest & app +import { vi, beforeAll, beforeEach, describe, expect, expectTypeOf, test, it, afterEach } from 'vitest'; +import supertest from "supertest"; +import app from "../../app.js"; +import jwt from 'jsonwebtoken'; + +// set route +const ROUTE = '/ai/model'; +// prepare response of each test +let response; + +// ############################ +// OBJECTS +// ############################ + + +const mockedVals = vi.hoisted(() => { + return { + foundUser: { + _id: '66a29da2942b3eb', + username: 'snoopy', + name: 'My User', + email: 'user@mail.local', + verified: true, + role: 0, + createdAt: '2024-07 - 25T18: 46: 58.982Z', + updatedAt: '2024-07 - 25T18: 46: 58.982Z', + __v: 0, + fullname: '', + id: '66a29da2942b3ebcaf047f07' + }, + foundModel: { + "details": { + "families": [ + "llama", + ], + "family": "llama", + "format": "gguf", + "parameter_size": "8.0B", + "parent_model": "", + "quantization_level": "Q4_0", + }, + "license": "someTexts", + "modelfile": "someTexts", + "parameters": "someTexts", + "template": "someTexts" + }, + validInput: { + model: 'llama3' + } + }; +}); + +// ############################ +// MOCKS +// ############################ +import * as dbService from '../../utils/handleDB.js'; +// mock dbService +vi.mock('../../utils/handleDB.js', async (importOriginal) => { + return { + ...await importOriginal(), + dbConnection: vi.fn(() => 'mocked'), + findOneRecord: vi.fn(() => mockedVals.foundUser), + }; +}); + +// mock aiService +import * as aiService from '../../utils/handleAI.js'; +vi.mock('../../utils/handleAI.js', async (importOriginal) => { + return { + ...await importOriginal(), + aiGetModel: vi.fn(() => mockedVals.foundModel) + }; +}); + +// ############################ +// TESTS +// ############################ + + +describe('ai model', () => { + const _jwt = () => { + return jwt.sign({ id: mockedVals.foundUser.id, role: mockedVals.foundUser.role }, process.env.JWT_SECRET_KEY, { expiresIn: process.env.JWT_TTL }); + }; + + describe('given the inputs are valid', () => { + beforeAll(async () => { + response = await supertest(app) + .post(ROUTE) + .set('Authorization', `Bearer ${_jwt()}`) + .send(mockedVals.validInput); + }); + it('should return a proper status code', () => { + expect(response.status).toBe(200); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot({ + license: expect.any(String), + modelfile: expect.any(String), + parameters: expect.any(String), + template: expect.any(String), + }); + }); + }); + + + + // ############################ + + describe('given no matching model found', () => { + beforeAll(async () => { + let error = new Error("model 'xxx' not found"); + error.name = 'ResponseError'; + error.status = 400; + aiService.aiGetModel.mockImplementation(() => { throw error; }); + + response = await supertest(app) + .post(ROUTE) + .set('Authorization', `Bearer ${_jwt()}`) + .send({ model: 'xxx' }); + }); + it('should return a proper status code', () => { + expect(response.status).toBe(500); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + + // ############################ + + describe('given required fields are missing', () => { + beforeAll(async () => { + const { model, ...input } = mockedVals.validInput; + + response = await supertest(app) + .post(ROUTE) + .set('Authorization', `Bearer ${_jwt()}`) + .send(input); + }); + + it('should return a proper status code status', () => { + expect(response.status).toBe(400); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + + // ############################ + + describe('given no valid jwt sended', () => { + beforeAll(async () => { + response = await supertest(app) + .post(ROUTE) + .set('Authorization', `Bearer invalid`) + .send(mockedVals.validInput); + }); + + it('should return a proper status code status', () => { + expect(response.status).toBe(403); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + + // ############################ + + describe('given no jwt sended', () => { + beforeAll(async () => { + response = await supertest(app) + .post(ROUTE) + .send(mockedVals.validInput); + }); + + it('should return a proper status code status', () => { + expect(response.status).toBe(401); + }); + it('should respond with a proper body', () => { + expect(response.body).toMatchSnapshot(); + }); + }); + +}); \ No newline at end of file diff --git a/logs/__tests__/ai/model.test.js b/logs/__tests__/ai/model.test.js deleted file mode 100644 index 16bbd508bda36fa7f35b27a3ff15b5289b0afbdd..0000000000000000000000000000000000000000 --- a/logs/__tests__/ai/model.test.js +++ /dev/null @@ -1,148 +0,0 @@ -// import vitest, supertest & app -import { vi, beforeAll, beforeEach, describe, expect, expectTypeOf, test, it, afterEach } from 'vitest'; -import supertest from "supertest"; -import app from "../../app.js"; -// ignore expiration of the (self-signed) certificate -process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; -// set timeout -const BEFORE_ALL_TIMEOUT = 30000; // 30 sec -// set route -const ROUTE = '/ai/model'; -// prepare response of each test -let response; - -// ############################ -// OBJECTS -// ############################ -const modelFoundResponse = { - "license": "META LLAMA 3 COMMUNITY LICENSE AGREEMENT WALL OF TEXT", - "modelfile": "# Modelfile generated by \"ollama show\"\n# ANOTHER WALL OF TEXT", - "parameters": "num_keep 24\nstop \"<|start_header_id|>\"\nstop \"<|end_header_id|>\"\nstop \"<|eot_id|>\"", - "template": "{{ if .System }}<|start_header_id|>system<|end_header_id|>\n\n{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>\n\n{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>\n\n{{ .Response }}<|eot_id|>", - "details": { - "parent_model": "", - "format": "gguf", - "family": "llama", - "families": [ - "llama" - ], - "parameter_size": "8.0B", - "quantization_level": "Q4_0" - } -}; - -const noModelFoundResponse = { - code: 400, - message: "model 'a' not found", - data: {} -}; -// ############################ -// MOCKS -// ############################ -// import PocketBase Service -import * as pbService from '../../utils/pocketbase/handlePocketBase.js'; -// mock pbService -vi.mock('../../utils/pocketbase/handlePocketBase.js', async (importOriginal) => { - return { - ...await importOriginal(), - pbVerifyAccessToken: vi.fn().mockImplementation((req, res, next) => { - next(); - }) - }; -}); - -// import AI Service -import * as aiService from '../../utils/handleAI.js'; -// const spyIsRunning = vi.spyOn(aiService, 'isRunning'); -// mock aiService -vi.mock('../../utils/handleAI.js', async (importOriginal) => { - return { - ...await importOriginal(), - aiGetModel: vi.fn(() => modelFoundResponse) - }; -}); - -// ############################ -// TESTS -// ############################ - - -// "message": "model 'a' not found" - -describe('ai model', () => { - describe('given the inputs are valid', () => { - beforeAll(async () => { - response = await supertest(app) - .post(ROUTE) - .set('Authorization', 'Bearer 123valid') - .send({ model: 'validModelName' }); - }, BEFORE_ALL_TIMEOUT); - it('should call required mocks', () => { - expect(aiService.aiGetModel()).toEqual(modelFoundResponse); - }); - it('should return a proper status code', () => { - expect(response.status).toBe(200); - }); - it('should respond with matching models', () => { - expect(response.body).toEqual(modelFoundResponse); - }); - }); - - // ############################ - - describe('given no valid JWT sent', () => { - beforeAll(async () => { - pbService.pbVerifyAccessToken.mockImplementationOnce((req, res, next) => { - return res.status(403).json({ message: 'You are not logged in.' }); - }); - - response = await supertest(app) - .post(ROUTE) - .send({ model: 'validModelName' }); - }, BEFORE_ALL_TIMEOUT); - it('should return a proper status code', () => { - expect(response.status).toBe(403); - }); - it('should respond with a proper message', () => { - expect(response.body.message).toEqual('You are not logged in.'); - }); - }); - - // ############################ - - describe('given no model name sent', () => { - beforeAll(async () => { - response = await supertest(app) - .post(ROUTE) - .set('Authorization', 'Bearer 123valid'); - }, BEFORE_ALL_TIMEOUT); - it('should return a proper status code', () => { - expect(response.status).toBe(400); - }); - it('should respond with a proper message', () => { - expect(response.body.validationErrors.model).toEqual('Required'); - }); - }); - - describe('given no matching model found', () => { - beforeAll(async () => { - let error = new Error("model 'a' not found"); - error.name = 'ResponseError'; - error.response = noModelFoundResponse; - error.status = 400; - aiService.aiGetModel.mockImplementation(() => { throw error; }); - - response = await supertest(app) - .post(ROUTE) - .set('Authorization', 'Bearer 123valid') - .send({ model: 'a' }); - }, BEFORE_ALL_TIMEOUT); - it('should return a proper status code', () => { - expect(response.status).toBe(500); - }); - it('should respond with a proper message', () => { - expect(response.body.message).toEqual("model 'a' not found"); - }); - }); - -}); \ No newline at end of file