@ihazz/bitrix24 1.1.1 → 1.1.3
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/README.md +5 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/src/access-control.d.ts +43 -0
- package/dist/src/access-control.d.ts.map +1 -0
- package/dist/src/access-control.js +128 -0
- package/dist/src/access-control.js.map +1 -0
- package/dist/src/api.d.ts +161 -0
- package/dist/src/api.d.ts.map +1 -0
- package/dist/src/api.js +357 -0
- package/dist/src/api.js.map +1 -0
- package/dist/src/bot-avatar.d.ts +7 -0
- package/dist/src/bot-avatar.d.ts.map +1 -0
- package/dist/src/bot-avatar.js +7 -0
- package/dist/src/bot-avatar.js.map +1 -0
- package/dist/src/channel.d.ts +216 -0
- package/dist/src/channel.d.ts.map +1 -0
- package/dist/src/channel.js +2324 -0
- package/dist/src/channel.js.map +1 -0
- package/dist/src/commands.d.ts +22 -0
- package/dist/src/commands.d.ts.map +1 -0
- package/dist/src/commands.js +160 -0
- package/dist/src/commands.js.map +1 -0
- package/dist/src/config-schema.d.ts +356 -0
- package/dist/src/config-schema.d.ts.map +1 -0
- package/dist/src/config-schema.js +43 -0
- package/dist/src/config-schema.js.map +1 -0
- package/dist/src/config.d.ts +11 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +50 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/dedup.d.ts +22 -0
- package/dist/src/dedup.d.ts.map +1 -0
- package/dist/src/dedup.js +49 -0
- package/dist/src/dedup.js.map +1 -0
- package/dist/src/group-access.d.ts +52 -0
- package/dist/src/group-access.d.ts.map +1 -0
- package/dist/src/group-access.js +180 -0
- package/dist/src/group-access.js.map +1 -0
- package/dist/src/history-cache.d.ts +41 -0
- package/dist/src/history-cache.d.ts.map +1 -0
- package/dist/src/history-cache.js +82 -0
- package/dist/src/history-cache.js.map +1 -0
- package/dist/src/i18n.d.ts +22 -0
- package/dist/src/i18n.d.ts.map +1 -0
- package/dist/src/i18n.js +175 -0
- package/dist/src/i18n.js.map +1 -0
- package/dist/src/inbound-handler.d.ts +92 -0
- package/dist/src/inbound-handler.d.ts.map +1 -0
- package/dist/src/inbound-handler.js +417 -0
- package/dist/src/inbound-handler.js.map +1 -0
- package/dist/src/media-service.d.ts +52 -0
- package/dist/src/media-service.d.ts.map +1 -0
- package/dist/src/media-service.js +423 -0
- package/dist/src/media-service.js.map +1 -0
- package/dist/src/message-utils.d.ts +34 -0
- package/dist/src/message-utils.d.ts.map +1 -0
- package/dist/src/message-utils.js +392 -0
- package/dist/src/message-utils.js.map +1 -0
- package/dist/src/polling-service.d.ts +39 -0
- package/dist/src/polling-service.d.ts.map +1 -0
- package/dist/src/polling-service.js +204 -0
- package/dist/src/polling-service.js.map +1 -0
- package/dist/src/rate-limiter.d.ts +22 -0
- package/dist/src/rate-limiter.d.ts.map +1 -0
- package/dist/src/rate-limiter.js +72 -0
- package/dist/src/rate-limiter.js.map +1 -0
- package/dist/src/runtime.d.ts +106 -0
- package/dist/src/runtime.d.ts.map +1 -0
- package/dist/src/runtime.js +11 -0
- package/dist/src/runtime.js.map +1 -0
- package/dist/src/send-service.d.ts +66 -0
- package/dist/src/send-service.d.ts.map +1 -0
- package/dist/src/send-service.js +177 -0
- package/dist/src/send-service.js.map +1 -0
- package/dist/src/state-paths.d.ts +3 -0
- package/dist/src/state-paths.d.ts.map +1 -0
- package/dist/src/state-paths.js +23 -0
- package/dist/src/state-paths.js.map +1 -0
- package/dist/src/types.d.ts +381 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils.d.ts +60 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +131 -0
- package/dist/src/utils.js.map +1 -0
- package/index.ts +1 -1
- package/openclaw.plugin.json +278 -1
- package/package.json +19 -2
- package/src/api.ts +0 -3
- package/src/channel.ts +76 -73
- package/src/config-schema.ts +1 -2
- package/src/config.ts +6 -8
- package/src/group-access.ts +1 -8
- package/src/inbound-handler.ts +128 -15
- package/src/media-service.ts +229 -61
- package/src/polling-service.ts +2 -3
- package/src/send-service.ts +4 -3
- package/src/state-paths.ts +28 -0
- package/src/types.ts +1 -2
- package/src/utils.ts +31 -4
- 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 -1033
- 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
|
-
});
|