@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.
Files changed (127) hide show
  1. package/README.md +5 -0
  2. package/dist/index.d.ts +30 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +55 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/src/access-control.d.ts +43 -0
  7. package/dist/src/access-control.d.ts.map +1 -0
  8. package/dist/src/access-control.js +128 -0
  9. package/dist/src/access-control.js.map +1 -0
  10. package/dist/src/api.d.ts +161 -0
  11. package/dist/src/api.d.ts.map +1 -0
  12. package/dist/src/api.js +357 -0
  13. package/dist/src/api.js.map +1 -0
  14. package/dist/src/bot-avatar.d.ts +7 -0
  15. package/dist/src/bot-avatar.d.ts.map +1 -0
  16. package/dist/src/bot-avatar.js +7 -0
  17. package/dist/src/bot-avatar.js.map +1 -0
  18. package/dist/src/channel.d.ts +216 -0
  19. package/dist/src/channel.d.ts.map +1 -0
  20. package/dist/src/channel.js +2324 -0
  21. package/dist/src/channel.js.map +1 -0
  22. package/dist/src/commands.d.ts +22 -0
  23. package/dist/src/commands.d.ts.map +1 -0
  24. package/dist/src/commands.js +160 -0
  25. package/dist/src/commands.js.map +1 -0
  26. package/dist/src/config-schema.d.ts +356 -0
  27. package/dist/src/config-schema.d.ts.map +1 -0
  28. package/dist/src/config-schema.js +43 -0
  29. package/dist/src/config-schema.js.map +1 -0
  30. package/dist/src/config.d.ts +11 -0
  31. package/dist/src/config.d.ts.map +1 -0
  32. package/dist/src/config.js +50 -0
  33. package/dist/src/config.js.map +1 -0
  34. package/dist/src/dedup.d.ts +22 -0
  35. package/dist/src/dedup.d.ts.map +1 -0
  36. package/dist/src/dedup.js +49 -0
  37. package/dist/src/dedup.js.map +1 -0
  38. package/dist/src/group-access.d.ts +52 -0
  39. package/dist/src/group-access.d.ts.map +1 -0
  40. package/dist/src/group-access.js +180 -0
  41. package/dist/src/group-access.js.map +1 -0
  42. package/dist/src/history-cache.d.ts +41 -0
  43. package/dist/src/history-cache.d.ts.map +1 -0
  44. package/dist/src/history-cache.js +82 -0
  45. package/dist/src/history-cache.js.map +1 -0
  46. package/dist/src/i18n.d.ts +22 -0
  47. package/dist/src/i18n.d.ts.map +1 -0
  48. package/dist/src/i18n.js +175 -0
  49. package/dist/src/i18n.js.map +1 -0
  50. package/dist/src/inbound-handler.d.ts +92 -0
  51. package/dist/src/inbound-handler.d.ts.map +1 -0
  52. package/dist/src/inbound-handler.js +417 -0
  53. package/dist/src/inbound-handler.js.map +1 -0
  54. package/dist/src/media-service.d.ts +52 -0
  55. package/dist/src/media-service.d.ts.map +1 -0
  56. package/dist/src/media-service.js +423 -0
  57. package/dist/src/media-service.js.map +1 -0
  58. package/dist/src/message-utils.d.ts +34 -0
  59. package/dist/src/message-utils.d.ts.map +1 -0
  60. package/dist/src/message-utils.js +392 -0
  61. package/dist/src/message-utils.js.map +1 -0
  62. package/dist/src/polling-service.d.ts +39 -0
  63. package/dist/src/polling-service.d.ts.map +1 -0
  64. package/dist/src/polling-service.js +204 -0
  65. package/dist/src/polling-service.js.map +1 -0
  66. package/dist/src/rate-limiter.d.ts +22 -0
  67. package/dist/src/rate-limiter.d.ts.map +1 -0
  68. package/dist/src/rate-limiter.js +72 -0
  69. package/dist/src/rate-limiter.js.map +1 -0
  70. package/dist/src/runtime.d.ts +106 -0
  71. package/dist/src/runtime.d.ts.map +1 -0
  72. package/dist/src/runtime.js +11 -0
  73. package/dist/src/runtime.js.map +1 -0
  74. package/dist/src/send-service.d.ts +66 -0
  75. package/dist/src/send-service.d.ts.map +1 -0
  76. package/dist/src/send-service.js +177 -0
  77. package/dist/src/send-service.js.map +1 -0
  78. package/dist/src/state-paths.d.ts +3 -0
  79. package/dist/src/state-paths.d.ts.map +1 -0
  80. package/dist/src/state-paths.js +23 -0
  81. package/dist/src/state-paths.js.map +1 -0
  82. package/dist/src/types.d.ts +381 -0
  83. package/dist/src/types.d.ts.map +1 -0
  84. package/dist/src/types.js +3 -0
  85. package/dist/src/types.js.map +1 -0
  86. package/dist/src/utils.d.ts +60 -0
  87. package/dist/src/utils.d.ts.map +1 -0
  88. package/dist/src/utils.js +131 -0
  89. package/dist/src/utils.js.map +1 -0
  90. package/index.ts +1 -1
  91. package/openclaw.plugin.json +278 -1
  92. package/package.json +19 -2
  93. package/src/api.ts +0 -3
  94. package/src/channel.ts +76 -73
  95. package/src/config-schema.ts +1 -2
  96. package/src/config.ts +6 -8
  97. package/src/group-access.ts +1 -8
  98. package/src/inbound-handler.ts +128 -15
  99. package/src/media-service.ts +229 -61
  100. package/src/polling-service.ts +2 -3
  101. package/src/send-service.ts +4 -3
  102. package/src/state-paths.ts +28 -0
  103. package/src/types.ts +1 -2
  104. package/src/utils.ts +31 -4
  105. package/tests/access-control.test.ts +0 -398
  106. package/tests/api.test.ts +0 -226
  107. package/tests/channel-flow.test.ts +0 -1692
  108. package/tests/channel.test.ts +0 -842
  109. package/tests/commands.test.ts +0 -57
  110. package/tests/config.test.ts +0 -210
  111. package/tests/dedup.test.ts +0 -50
  112. package/tests/fixtures/onimbotjoinchat.json +0 -48
  113. package/tests/fixtures/onimbotmessageadd-file.json +0 -86
  114. package/tests/fixtures/onimbotmessageadd-text.json +0 -59
  115. package/tests/fixtures/onimcommandadd.json +0 -45
  116. package/tests/group-access.test.ts +0 -340
  117. package/tests/history-cache.test.ts +0 -117
  118. package/tests/i18n.test.ts +0 -90
  119. package/tests/inbound-handler.test.ts +0 -1033
  120. package/tests/index.test.ts +0 -94
  121. package/tests/media-service.test.ts +0 -319
  122. package/tests/message-utils.test.ts +0 -184
  123. package/tests/polling-service.test.ts +0 -115
  124. package/tests/rate-limiter.test.ts +0 -52
  125. package/tests/send-service.test.ts +0 -162
  126. package/tsconfig.json +0 -22
  127. 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
- });