lobe-chat

Форк
0
83 строки · 2.6 Кб
1
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2

3
import { BrowserDB } from '../../core/db';
4
import { DB_File } from '../../schemas/files';
5
import { FileModel } from '../file';
6

7
// Assuming LocalDB is already mocked or using an in-memory database
8
// and LocalFileSchema has been imported correctly.
9

10
describe('_FileModel', () => {
11
  let fileData: DB_File;
12

13
  beforeEach(() => {
14
    // Set up file data with the correct structure according to LocalFileSchema
15
    fileData = {
16
      data: new ArrayBuffer(10),
17
      fileType: 'image/png',
18
      name: 'test.png',
19
      saveMode: 'local',
20
      size: 10,
21
      // url is optional, only needed if saveMode is 'url'
22
    };
23
  });
24

25
  afterEach(async () => {
26
    // Clean up the database after each test
27
    const db = new BrowserDB();
28
    await db.files.clear();
29
    db.close();
30
  });
31

32
  it('should create a file record', async () => {
33
    // First, create a file to test the create method
34
    const fileData: DB_File = {
35
      data: new ArrayBuffer(10),
36
      fileType: 'image/png',
37
      name: 'test.png',
38
      saveMode: 'local',
39
      size: 10,
40
    };
41

42
    const result = await FileModel.create(fileData);
43

44
    expect(result).toHaveProperty('id');
45
    expect(result.id).toMatch(/^file-/);
46

47
    // Verify that the file has been added to the database
48
    const fileInDb = await FileModel.findById(result.id);
49

50
    expect(fileInDb).toEqual(expect.objectContaining(fileData));
51
  });
52

53
  it('should find a file by id', async () => {
54
    // First, create a file to test the findById method
55
    const createdFile = await FileModel.create(fileData);
56
    const foundFile = await FileModel.findById(createdFile.id);
57

58
    expect(foundFile).toEqual(expect.objectContaining(fileData));
59
  });
60

61
  it('should delete a file by id', async () => {
62
    // First, create a file to test the delete method
63
    const createdFile = await FileModel.create(fileData);
64
    await FileModel.delete(createdFile.id);
65

66
    // Verify that the file has been removed from the database
67
    const fileInDb = await FileModel.findById(createdFile.id);
68
    expect(fileInDb).toBeUndefined();
69
  });
70

71
  it('should clear all files', async () => {
72
    // First, create a file to test the delete method
73
    const createdFile = await FileModel.create(fileData);
74
    const createdFile2 = await FileModel.create(fileData);
75
    await FileModel.clear();
76

77
    // Verify that the file has been removed from the database
78
    const fileInDb = await FileModel.findById(createdFile.id);
79
    expect(fileInDb).toBeUndefined();
80
    const fileInDb2 = await FileModel.findById(createdFile2.id);
81
    expect(fileInDb2).toBeUndefined();
82
  });
83
});
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.