File size: 5,288 Bytes
aa2e6af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const { createChunkProcessor, splitTextIntoChunks } = require('./streamAudio');
const { Message } = require('~/models/Message');

jest.mock('~/models/Message', () => ({
  Message: {
    findOne: jest.fn().mockReturnValue({
      lean: jest.fn(),
    }),
  },
}));

describe('processChunks', () => {
  let processChunks;

  beforeEach(() => {
    processChunks = createChunkProcessor('message-id');
    Message.findOne.mockClear();
    Message.findOne().lean.mockClear();
  });

  it('should return an empty array when the message is not found', async () => {
    Message.findOne().lean.mockResolvedValueOnce(null);

    const result = await processChunks();

    expect(result).toEqual([]);
    expect(Message.findOne).toHaveBeenCalledWith({ messageId: 'message-id' }, 'text unfinished');
    expect(Message.findOne().lean).toHaveBeenCalled();
  });

  it('should return an empty array when the message does not have a text property', async () => {
    Message.findOne().lean.mockResolvedValueOnce({ unfinished: true });

    const result = await processChunks();

    expect(result).toEqual([]);
    expect(Message.findOne).toHaveBeenCalledWith({ messageId: 'message-id' }, 'text unfinished');
    expect(Message.findOne().lean).toHaveBeenCalled();
  });

  it('should return chunks for an unfinished message with separators', async () => {
    const messageText = 'This is a long message. It should be split into chunks. Lol hi mom';
    Message.findOne().lean.mockResolvedValueOnce({ text: messageText, unfinished: true });

    const result = await processChunks();

    expect(result).toEqual([
      { text: 'This is a long message. It should be split into chunks.', isFinished: false },
    ]);
    expect(Message.findOne).toHaveBeenCalledWith({ messageId: 'message-id' }, 'text unfinished');
    expect(Message.findOne().lean).toHaveBeenCalled();
  });

  it('should return chunks for an unfinished message without separators', async () => {
    const messageText = 'This is a long message without separators hello there my friend';
    Message.findOne().lean.mockResolvedValueOnce({ text: messageText, unfinished: true });

    const result = await processChunks();

    expect(result).toEqual([{ text: messageText, isFinished: false }]);
    expect(Message.findOne).toHaveBeenCalledWith({ messageId: 'message-id' }, 'text unfinished');
    expect(Message.findOne().lean).toHaveBeenCalled();
  });

  it('should return the remaining text as a chunk for a finished message', async () => {
    const messageText = 'This is a finished message.';
    Message.findOne().lean.mockResolvedValueOnce({ text: messageText, unfinished: false });

    const result = await processChunks();

    expect(result).toEqual([{ text: messageText, isFinished: true }]);
    expect(Message.findOne).toHaveBeenCalledWith({ messageId: 'message-id' }, 'text unfinished');
    expect(Message.findOne().lean).toHaveBeenCalled();
  });

  it('should return an empty array for a finished message with no remaining text', async () => {
    const messageText = 'This is a finished message.';
    Message.findOne().lean.mockResolvedValueOnce({ text: messageText, unfinished: false });

    await processChunks();
    Message.findOne().lean.mockResolvedValueOnce({ text: messageText, unfinished: false });
    const result = await processChunks();

    expect(result).toEqual([]);
    expect(Message.findOne).toHaveBeenCalledWith({ messageId: 'message-id' }, 'text unfinished');
    expect(Message.findOne().lean).toHaveBeenCalledTimes(2);
  });
});

describe('splitTextIntoChunks', () => {
  test('splits text into chunks of specified size with default separators', () => {
    const text = 'This is a test. This is only a test! Make sure it works properly? Okay.';
    const chunkSize = 20;
    const expectedChunks = [
      { text: 'This is a test.', isFinished: false },
      { text: 'This is only a test!', isFinished: false },
      { text: 'Make sure it works p', isFinished: false },
      { text: 'roperly? Okay.', isFinished: true },
    ];

    const result = splitTextIntoChunks(text, chunkSize);
    expect(result).toEqual(expectedChunks);
  });

  test('splits text into chunks with default size', () => {
    const text = 'A'.repeat(8000) + '. The end.';
    const expectedChunks = [
      { text: 'A'.repeat(4000), isFinished: false },
      { text: 'A'.repeat(4000), isFinished: false },
      { text: '. The end.', isFinished: true },
    ];

    const result = splitTextIntoChunks(text);
    expect(result).toEqual(expectedChunks);
  });

  test('returns a single chunk if text length is less than chunk size', () => {
    const text = 'Short text.';
    const expectedChunks = [{ text: 'Short text.', isFinished: true }];

    const result = splitTextIntoChunks(text, 4000);
    expect(result).toEqual(expectedChunks);
  });

  test('handles text with no separators correctly', () => {
    const text = 'ThisTextHasNoSeparatorsAndIsVeryLong'.repeat(100);
    const chunkSize = 4000;
    const expectedChunks = [{ text: text, isFinished: true }];

    const result = splitTextIntoChunks(text, chunkSize);
    expect(result).toEqual(expectedChunks);
  });

  test('throws an error when text is empty', () => {
    expect(() => splitTextIntoChunks('')).toThrow('Text is required');
  });
});