@ihazz/bitrix24 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -1
- package/src/inbound-handler.ts +3 -2
- package/src/media-service.ts +78 -25
- package/src/state-paths.ts +24 -0
- package/tests/access-control.test.ts +0 -398
- package/tests/api.test.ts +0 -226
- package/tests/channel-flow.test.ts +0 -1692
- package/tests/channel.test.ts +0 -842
- package/tests/commands.test.ts +0 -57
- package/tests/config.test.ts +0 -210
- package/tests/dedup.test.ts +0 -50
- package/tests/fixtures/onimbotjoinchat.json +0 -48
- package/tests/fixtures/onimbotmessageadd-file.json +0 -86
- package/tests/fixtures/onimbotmessageadd-text.json +0 -59
- package/tests/fixtures/onimcommandadd.json +0 -45
- package/tests/group-access.test.ts +0 -340
- package/tests/history-cache.test.ts +0 -117
- package/tests/i18n.test.ts +0 -90
- package/tests/inbound-handler.test.ts +0 -986
- package/tests/index.test.ts +0 -94
- package/tests/media-service.test.ts +0 -319
- package/tests/message-utils.test.ts +0 -184
- package/tests/polling-service.test.ts +0 -115
- package/tests/rate-limiter.test.ts +0 -52
- package/tests/send-service.test.ts +0 -162
- package/tsconfig.json +0 -22
- package/vitest.config.ts +0 -9
package/tests/api.test.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
-
import { Bitrix24Api } from '../src/api.js';
|
|
3
|
-
import { Bitrix24ApiError } from '../src/utils.js';
|
|
4
|
-
|
|
5
|
-
const silentLogger = {
|
|
6
|
-
info: vi.fn(),
|
|
7
|
-
warn: vi.fn(),
|
|
8
|
-
error: vi.fn(),
|
|
9
|
-
debug: vi.fn(),
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
describe('Bitrix24Api', () => {
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
vi.useRealTimers();
|
|
15
|
-
vi.restoreAllMocks();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('calls webhook with a request timeout signal', async () => {
|
|
19
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
20
|
-
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(
|
|
21
|
-
new Response(JSON.stringify({ result: { ok: true } }), { status: 200 }),
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const result = await api.callWebhook(
|
|
25
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
26
|
-
'profile',
|
|
27
|
-
{ foo: 'bar' },
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
expect(result).toEqual({ result: { ok: true } });
|
|
31
|
-
expect(fetchSpy).toHaveBeenCalledWith(
|
|
32
|
-
'https://test.bitrix24.com/rest/1/token/profile.json',
|
|
33
|
-
expect.objectContaining({
|
|
34
|
-
method: 'POST',
|
|
35
|
-
body: JSON.stringify({ foo: 'bar' }),
|
|
36
|
-
signal: expect.any(AbortSignal),
|
|
37
|
-
}),
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('maps timeout errors to Bitrix24ApiError', async () => {
|
|
42
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
43
|
-
vi.useFakeTimers();
|
|
44
|
-
vi.spyOn(globalThis, 'fetch').mockRejectedValue(
|
|
45
|
-
Object.assign(new Error('timed out'), { name: 'TimeoutError' }),
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
const assertion = expect(
|
|
49
|
-
api.callWebhook('https://test.bitrix24.com/rest/1/token/', 'profile'),
|
|
50
|
-
).rejects.toMatchObject({
|
|
51
|
-
name: 'Bitrix24ApiError',
|
|
52
|
-
code: 'TIMEOUT',
|
|
53
|
-
});
|
|
54
|
-
await vi.runAllTimersAsync();
|
|
55
|
-
await assertion;
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('retries transient network fetch failures', async () => {
|
|
59
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
60
|
-
vi.useFakeTimers();
|
|
61
|
-
const fetchSpy = vi.spyOn(globalThis, 'fetch')
|
|
62
|
-
.mockRejectedValueOnce(Object.assign(new TypeError('fetch failed'), {
|
|
63
|
-
cause: { code: 'ECONNRESET' },
|
|
64
|
-
}))
|
|
65
|
-
.mockResolvedValueOnce(
|
|
66
|
-
new Response(JSON.stringify({ result: { ok: true } }), { status: 200 }),
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const assertion = expect(api.callWebhook(
|
|
70
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
71
|
-
'profile',
|
|
72
|
-
{ foo: 'bar' },
|
|
73
|
-
)).resolves.toEqual({ result: { ok: true } });
|
|
74
|
-
await vi.runAllTimersAsync();
|
|
75
|
-
await assertion;
|
|
76
|
-
expect(fetchSpy).toHaveBeenCalledTimes(2);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('throws INVALID_RESPONSE when sendMessage result has no valid id', async () => {
|
|
80
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
81
|
-
vi.spyOn(api, 'callWebhook').mockResolvedValueOnce({
|
|
82
|
-
result: {} as never,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
await expect(api.sendMessage(
|
|
86
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
87
|
-
{ botId: 7, botToken: 'bot_token' },
|
|
88
|
-
'42',
|
|
89
|
-
'hello',
|
|
90
|
-
)).rejects.toMatchObject({
|
|
91
|
-
name: 'Bitrix24ApiError',
|
|
92
|
-
code: 'INVALID_RESPONSE',
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('sends native forwards via forwardIds', async () => {
|
|
97
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
98
|
-
const callWebhookSpy = vi.spyOn(api, 'callWebhook').mockResolvedValueOnce({
|
|
99
|
-
result: { id: null, uuidMap: {} } as never,
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
await expect(api.sendMessage(
|
|
103
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
104
|
-
{ botId: 7, botToken: 'bot_token' },
|
|
105
|
-
'42',
|
|
106
|
-
null,
|
|
107
|
-
{ forwardMessages: [31304] },
|
|
108
|
-
)).resolves.toBe(0);
|
|
109
|
-
|
|
110
|
-
expect(callWebhookSpy).toHaveBeenCalledWith(
|
|
111
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
112
|
-
'imbot.v2.Chat.Message.send',
|
|
113
|
-
expect.objectContaining({
|
|
114
|
-
botId: 7,
|
|
115
|
-
botToken: 'bot_token',
|
|
116
|
-
dialogId: '42',
|
|
117
|
-
fields: expect.any(Object),
|
|
118
|
-
}),
|
|
119
|
-
);
|
|
120
|
-
const callParams = callWebhookSpy.mock.calls[0][2] as Record<string, unknown>;
|
|
121
|
-
const fields = callParams.fields as Record<string, unknown>;
|
|
122
|
-
expect(fields.forwardIds).toBeTypeOf('object');
|
|
123
|
-
expect(fields.forwardIds).not.toBeNull();
|
|
124
|
-
expect(Object.values(fields.forwardIds as Record<string, number>)).toEqual([31304]);
|
|
125
|
-
expect(Object.keys(fields.forwardIds as Record<string, number>)[0]).toMatch(
|
|
126
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
127
|
-
);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('fetches a single message by id', async () => {
|
|
131
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
132
|
-
const callWebhookSpy = vi.spyOn(api, 'callWebhook').mockResolvedValueOnce({
|
|
133
|
-
result: {
|
|
134
|
-
message: { id: 789 },
|
|
135
|
-
user: { id: 1 },
|
|
136
|
-
} as never,
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
await expect(api.getMessage(
|
|
140
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
141
|
-
{ botId: 7, botToken: 'bot_token' },
|
|
142
|
-
789,
|
|
143
|
-
)).resolves.toEqual({
|
|
144
|
-
message: { id: 789 },
|
|
145
|
-
user: { id: 1 },
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
expect(callWebhookSpy).toHaveBeenCalledWith(
|
|
149
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
150
|
-
'imbot.v2.Chat.Message.get',
|
|
151
|
-
{
|
|
152
|
-
botId: 7,
|
|
153
|
-
botToken: 'bot_token',
|
|
154
|
-
messageId: 789,
|
|
155
|
-
},
|
|
156
|
-
);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('fetches message context window by id', async () => {
|
|
160
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
161
|
-
const callWebhookSpy = vi.spyOn(api, 'callWebhook').mockResolvedValueOnce({
|
|
162
|
-
result: {
|
|
163
|
-
messages: [],
|
|
164
|
-
users: [],
|
|
165
|
-
hasPrevPage: false,
|
|
166
|
-
hasNextPage: false,
|
|
167
|
-
} as never,
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
await expect(api.getMessageContext(
|
|
171
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
172
|
-
{ botId: 7, botToken: 'bot_token' },
|
|
173
|
-
789,
|
|
174
|
-
5,
|
|
175
|
-
)).resolves.toEqual({
|
|
176
|
-
messages: [],
|
|
177
|
-
users: [],
|
|
178
|
-
hasPrevPage: false,
|
|
179
|
-
hasNextPage: false,
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
expect(callWebhookSpy).toHaveBeenCalledWith(
|
|
183
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
184
|
-
'imbot.v2.Chat.Message.getContext',
|
|
185
|
-
{
|
|
186
|
-
botId: 7,
|
|
187
|
-
botToken: 'bot_token',
|
|
188
|
-
messageId: 789,
|
|
189
|
-
range: 5,
|
|
190
|
-
},
|
|
191
|
-
);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('fetches events with withUserEvents when agent mode is enabled', async () => {
|
|
195
|
-
const api = new Bitrix24Api({ logger: silentLogger });
|
|
196
|
-
const callWebhookSpy = vi.spyOn(api, 'callWebhook').mockResolvedValueOnce({
|
|
197
|
-
result: {
|
|
198
|
-
events: [],
|
|
199
|
-
lastEventId: 0,
|
|
200
|
-
hasMore: false,
|
|
201
|
-
} as never,
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
await expect(api.fetchEvents(
|
|
205
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
206
|
-
{ botId: 7, botToken: 'bot_token' },
|
|
207
|
-
{ offset: 100, limit: 50, withUserEvents: true },
|
|
208
|
-
)).resolves.toEqual({
|
|
209
|
-
events: [],
|
|
210
|
-
lastEventId: 0,
|
|
211
|
-
hasMore: false,
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
expect(callWebhookSpy).toHaveBeenCalledWith(
|
|
215
|
-
'https://test.bitrix24.com/rest/1/token/',
|
|
216
|
-
'imbot.v2.Event.get',
|
|
217
|
-
{
|
|
218
|
-
botId: 7,
|
|
219
|
-
botToken: 'bot_token',
|
|
220
|
-
offset: 100,
|
|
221
|
-
limit: 50,
|
|
222
|
-
withUserEvents: true,
|
|
223
|
-
},
|
|
224
|
-
);
|
|
225
|
-
});
|
|
226
|
-
});
|