opc-agent 3.0.1 → 4.0.1

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 (216) hide show
  1. package/README.md +404 -74
  2. package/README.zh-CN.md +82 -0
  3. package/dist/channels/dingtalk.d.ts +17 -0
  4. package/dist/channels/dingtalk.js +38 -0
  5. package/dist/channels/googlechat.d.ts +14 -0
  6. package/dist/channels/googlechat.js +37 -0
  7. package/dist/channels/imessage.d.ts +13 -0
  8. package/dist/channels/imessage.js +28 -0
  9. package/dist/channels/irc.d.ts +20 -0
  10. package/dist/channels/irc.js +71 -0
  11. package/dist/channels/line.d.ts +14 -0
  12. package/dist/channels/line.js +28 -0
  13. package/dist/channels/matrix.d.ts +15 -0
  14. package/dist/channels/matrix.js +28 -0
  15. package/dist/channels/mattermost.d.ts +18 -0
  16. package/dist/channels/mattermost.js +49 -0
  17. package/dist/channels/msteams.d.ts +14 -0
  18. package/dist/channels/msteams.js +28 -0
  19. package/dist/channels/nostr.d.ts +14 -0
  20. package/dist/channels/nostr.js +28 -0
  21. package/dist/channels/qq.d.ts +15 -0
  22. package/dist/channels/qq.js +28 -0
  23. package/dist/channels/signal.d.ts +14 -0
  24. package/dist/channels/signal.js +28 -0
  25. package/dist/channels/sms.d.ts +15 -0
  26. package/dist/channels/sms.js +28 -0
  27. package/dist/channels/twitch.d.ts +17 -0
  28. package/dist/channels/twitch.js +59 -0
  29. package/dist/channels/voice-call.d.ts +27 -0
  30. package/dist/channels/voice-call.js +82 -0
  31. package/dist/channels/whatsapp.d.ts +14 -0
  32. package/dist/channels/whatsapp.js +28 -0
  33. package/dist/cli/chat.d.ts +2 -0
  34. package/dist/cli/chat.js +134 -0
  35. package/dist/cli/setup.d.ts +4 -0
  36. package/dist/cli/setup.js +303 -0
  37. package/dist/cli.js +142 -6
  38. package/dist/core/api-server.d.ts +25 -0
  39. package/dist/core/api-server.js +286 -0
  40. package/dist/core/audio.d.ts +50 -0
  41. package/dist/core/audio.js +68 -0
  42. package/dist/core/context-discovery.d.ts +16 -0
  43. package/dist/core/context-discovery.js +107 -0
  44. package/dist/core/context-refs.d.ts +29 -0
  45. package/dist/core/context-refs.js +162 -0
  46. package/dist/core/gateway.d.ts +53 -0
  47. package/dist/core/gateway.js +80 -0
  48. package/dist/core/heartbeat.d.ts +19 -0
  49. package/dist/core/heartbeat.js +50 -0
  50. package/dist/core/hooks.d.ts +28 -0
  51. package/dist/core/hooks.js +82 -0
  52. package/dist/core/ide-bridge.d.ts +53 -0
  53. package/dist/core/ide-bridge.js +97 -0
  54. package/dist/core/node-network.d.ts +23 -0
  55. package/dist/core/node-network.js +77 -0
  56. package/dist/core/profiles.d.ts +27 -0
  57. package/dist/core/profiles.js +131 -0
  58. package/dist/core/sandbox.d.ts +25 -0
  59. package/dist/core/sandbox.js +84 -1
  60. package/dist/core/session-manager.d.ts +33 -0
  61. package/dist/core/session-manager.js +157 -0
  62. package/dist/core/vision.d.ts +45 -0
  63. package/dist/core/vision.js +177 -0
  64. package/dist/hub/brain-seed.d.ts +14 -0
  65. package/dist/hub/brain-seed.js +77 -0
  66. package/dist/hub/client.d.ts +25 -0
  67. package/dist/hub/client.js +44 -0
  68. package/dist/index.d.ts +66 -1
  69. package/dist/index.js +95 -3
  70. package/dist/memory/context-compressor.d.ts +43 -0
  71. package/dist/memory/context-compressor.js +167 -0
  72. package/dist/memory/index.d.ts +4 -0
  73. package/dist/memory/index.js +5 -1
  74. package/dist/memory/user-profiler.d.ts +50 -0
  75. package/dist/memory/user-profiler.js +201 -0
  76. package/dist/providers/index.d.ts +1 -1
  77. package/dist/providers/index.js +54 -1
  78. package/dist/scheduler/cron-engine.d.ts +41 -0
  79. package/dist/scheduler/cron-engine.js +200 -0
  80. package/dist/scheduler/index.d.ts +3 -0
  81. package/dist/scheduler/index.js +7 -0
  82. package/dist/schema/oad.d.ts +12 -12
  83. package/dist/security/approvals.d.ts +53 -0
  84. package/dist/security/approvals.js +115 -0
  85. package/dist/security/elevated.d.ts +41 -0
  86. package/dist/security/elevated.js +89 -0
  87. package/dist/security/index.d.ts +6 -0
  88. package/dist/security/index.js +7 -1
  89. package/dist/security/secrets.d.ts +34 -0
  90. package/dist/security/secrets.js +115 -0
  91. package/dist/skills/builtin/index.d.ts +6 -0
  92. package/dist/skills/builtin/index.js +402 -0
  93. package/dist/skills/marketplace.d.ts +30 -0
  94. package/dist/skills/marketplace.js +142 -0
  95. package/dist/skills/types.d.ts +34 -0
  96. package/dist/skills/types.js +16 -0
  97. package/dist/studio/server.d.ts +25 -0
  98. package/dist/studio/server.js +780 -0
  99. package/dist/studio/templates-data.d.ts +21 -0
  100. package/dist/studio/templates-data.js +148 -0
  101. package/dist/studio-ui/index.html +2502 -1073
  102. package/dist/tools/builtin/browser.d.ts +47 -0
  103. package/dist/tools/builtin/browser.js +284 -0
  104. package/dist/tools/builtin/home-assistant.d.ts +12 -0
  105. package/dist/tools/builtin/home-assistant.js +126 -0
  106. package/dist/tools/builtin/index.d.ts +7 -1
  107. package/dist/tools/builtin/index.js +23 -2
  108. package/dist/tools/builtin/rl-tools.d.ts +13 -0
  109. package/dist/tools/builtin/rl-tools.js +228 -0
  110. package/dist/tools/builtin/vision.d.ts +6 -0
  111. package/dist/tools/builtin/vision.js +61 -0
  112. package/dist/tools/builtin/web-search.d.ts +9 -0
  113. package/dist/tools/builtin/web-search.js +150 -0
  114. package/dist/tools/document-processor.d.ts +39 -0
  115. package/dist/tools/document-processor.js +188 -0
  116. package/dist/tools/image-generator.d.ts +42 -0
  117. package/dist/tools/image-generator.js +136 -0
  118. package/dist/tools/web-scraper.d.ts +20 -0
  119. package/dist/tools/web-scraper.js +148 -0
  120. package/dist/tools/web-search.d.ts +51 -0
  121. package/dist/tools/web-search.js +152 -0
  122. package/install.ps1 +154 -0
  123. package/install.sh +164 -0
  124. package/package.json +63 -52
  125. package/src/channels/dingtalk.ts +46 -0
  126. package/src/channels/googlechat.ts +42 -0
  127. package/src/channels/imessage.ts +32 -0
  128. package/src/channels/irc.ts +82 -0
  129. package/src/channels/line.ts +33 -0
  130. package/src/channels/matrix.ts +34 -0
  131. package/src/channels/mattermost.ts +57 -0
  132. package/src/channels/msteams.ts +33 -0
  133. package/src/channels/nostr.ts +33 -0
  134. package/src/channels/qq.ts +34 -0
  135. package/src/channels/signal.ts +33 -0
  136. package/src/channels/sms.ts +34 -0
  137. package/src/channels/twitch.ts +65 -0
  138. package/src/channels/voice-call.ts +100 -0
  139. package/src/channels/whatsapp.ts +33 -0
  140. package/src/cli/chat.ts +99 -0
  141. package/src/cli/setup.ts +314 -0
  142. package/src/cli.ts +148 -6
  143. package/src/core/api-server.ts +277 -0
  144. package/src/core/audio.ts +98 -0
  145. package/src/core/context-discovery.ts +85 -0
  146. package/src/core/context-refs.ts +140 -0
  147. package/src/core/gateway.ts +106 -0
  148. package/src/core/heartbeat.ts +51 -0
  149. package/src/core/hooks.ts +105 -0
  150. package/src/core/ide-bridge.ts +133 -0
  151. package/src/core/node-network.ts +86 -0
  152. package/src/core/profiles.ts +122 -0
  153. package/src/core/sandbox.ts +100 -0
  154. package/src/core/session-manager.ts +137 -0
  155. package/src/core/vision.ts +180 -0
  156. package/src/hub/brain-seed.ts +54 -0
  157. package/src/hub/client.ts +60 -0
  158. package/src/index.ts +86 -1
  159. package/src/memory/context-compressor.ts +189 -0
  160. package/src/memory/index.ts +4 -0
  161. package/src/memory/user-profiler.ts +215 -0
  162. package/src/providers/index.ts +64 -1
  163. package/src/scheduler/cron-engine.ts +191 -0
  164. package/src/scheduler/index.ts +2 -0
  165. package/src/security/approvals.ts +143 -0
  166. package/src/security/elevated.ts +105 -0
  167. package/src/security/index.ts +6 -0
  168. package/src/security/secrets.ts +129 -0
  169. package/src/skills/builtin/index.ts +408 -0
  170. package/src/skills/marketplace.ts +113 -0
  171. package/src/skills/types.ts +42 -0
  172. package/src/studio/server.ts +1591 -791
  173. package/src/studio/templates-data.ts +178 -0
  174. package/src/studio-ui/index.html +2502 -1073
  175. package/src/tools/builtin/browser.ts +299 -0
  176. package/src/tools/builtin/home-assistant.ts +116 -0
  177. package/src/tools/builtin/index.ts +37 -28
  178. package/src/tools/builtin/rl-tools.ts +243 -0
  179. package/src/tools/builtin/vision.ts +64 -0
  180. package/src/tools/builtin/web-search.ts +126 -0
  181. package/src/tools/document-processor.ts +213 -0
  182. package/src/tools/image-generator.ts +150 -0
  183. package/src/tools/web-scraper.ts +179 -0
  184. package/src/tools/web-search.ts +180 -0
  185. package/tests/api-server.test.ts +148 -0
  186. package/tests/approvals.test.ts +89 -0
  187. package/tests/audio.test.ts +40 -0
  188. package/tests/browser.test.ts +179 -0
  189. package/tests/builtin-tools.test.ts +83 -83
  190. package/tests/channels-extra.test.ts +45 -0
  191. package/tests/context-compressor.test.ts +172 -0
  192. package/tests/context-refs.test.ts +121 -0
  193. package/tests/cron-engine.test.ts +101 -0
  194. package/tests/document-processor.test.ts +69 -0
  195. package/tests/e2e-nocode.test.ts +442 -0
  196. package/tests/elevated.test.ts +69 -0
  197. package/tests/gateway.test.ts +63 -71
  198. package/tests/home-assistant.test.ts +40 -0
  199. package/tests/hooks.test.ts +79 -0
  200. package/tests/ide-bridge.test.ts +38 -0
  201. package/tests/image-generator.test.ts +84 -0
  202. package/tests/node-network.test.ts +74 -0
  203. package/tests/profiles.test.ts +61 -0
  204. package/tests/rl-tools.test.ts +93 -0
  205. package/tests/sandbox-manager.test.ts +46 -0
  206. package/tests/secrets.test.ts +107 -0
  207. package/tests/settings-api.test.ts +148 -0
  208. package/tests/setup.test.ts +73 -0
  209. package/tests/studio.test.ts +402 -229
  210. package/tests/tools/builtin-extended.test.ts +138 -138
  211. package/tests/user-profiler.test.ts +169 -0
  212. package/tests/v090-features.test.ts +254 -0
  213. package/tests/vision.test.ts +61 -0
  214. package/tests/voice-call.test.ts +47 -0
  215. package/tests/voice-interaction.test.ts +38 -0
  216. package/tests/web-search.test.ts +155 -0
@@ -0,0 +1,254 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { DingTalkChannel } from '../src/channels/dingtalk';
3
+ import { MattermostChannel } from '../src/channels/mattermost';
4
+ import { GoogleChatChannel } from '../src/channels/googlechat';
5
+ import { TwitchChannel } from '../src/channels/twitch';
6
+ import { IRCChannel } from '../src/channels/irc';
7
+ import { ContextDiscovery } from '../src/core/context-discovery';
8
+ import { SessionManager } from '../src/core/session-manager';
9
+ import { HeartbeatManager } from '../src/core/heartbeat';
10
+ import * as fs from 'fs';
11
+ import * as path from 'path';
12
+ import * as os from 'os';
13
+
14
+ // ── Channel Tests (10) ──
15
+
16
+ describe('DingTalkChannel', () => {
17
+ it('should require webhookUrl', () => {
18
+ expect(() => new DingTalkChannel({ webhookUrl: '' })).toThrow('webhookUrl');
19
+ });
20
+ it('should throw when sending before start', async () => {
21
+ const ch = new DingTalkChannel({ webhookUrl: 'https://example.com/hook' });
22
+ await expect(ch.send('chat', 'hi')).rejects.toThrow('not started');
23
+ });
24
+ });
25
+
26
+ describe('MattermostChannel', () => {
27
+ it('should require serverUrl and token', () => {
28
+ expect(() => new MattermostChannel({ serverUrl: '', token: '' })).toThrow('serverUrl');
29
+ });
30
+ it('should throw when sending before start', async () => {
31
+ const ch = new MattermostChannel({ serverUrl: 'https://mm.example.com', token: 'tok' });
32
+ await expect(ch.send('ch1', 'hi')).rejects.toThrow('not started');
33
+ });
34
+ });
35
+
36
+ describe('GoogleChatChannel', () => {
37
+ it('should require webhookUrl', () => {
38
+ expect(() => new GoogleChatChannel({ webhookUrl: '' })).toThrow('webhookUrl');
39
+ });
40
+ it('should throw when sending before start', async () => {
41
+ const ch = new GoogleChatChannel({ webhookUrl: 'https://chat.googleapis.com/v1/spaces/x/messages?key=y' });
42
+ await expect(ch.send('space', 'hi')).rejects.toThrow('not started');
43
+ });
44
+ });
45
+
46
+ describe('TwitchChannel', () => {
47
+ it('should require username, oauthToken, channels', () => {
48
+ expect(() => new TwitchChannel({ username: '', oauthToken: '', channels: [] })).toThrow('username');
49
+ });
50
+ it('should throw when starting without tmi.js', async () => {
51
+ const ch = new TwitchChannel({ username: 'bot', oauthToken: 'oauth:xxx', channels: ['#test'] });
52
+ await expect(ch.start()).rejects.toThrow('tmi.js');
53
+ });
54
+ });
55
+
56
+ describe('IRCChannel', () => {
57
+ it('should require host, nick, channels', () => {
58
+ expect(() => new IRCChannel({ host: '', nick: '', channels: [] })).toThrow('host');
59
+ });
60
+ it('should throw when starting without irc-framework', async () => {
61
+ const ch = new IRCChannel({ host: 'irc.libera.chat', nick: 'bot', channels: ['#test'] });
62
+ await expect(ch.start()).rejects.toThrow('irc-framework');
63
+ });
64
+ });
65
+
66
+ // ── Context Discovery Tests (8) ──
67
+
68
+ describe('ContextDiscovery', () => {
69
+ let tmpDir: string;
70
+ let cd: ContextDiscovery;
71
+
72
+ beforeEach(() => {
73
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ctx-'));
74
+ cd = new ContextDiscovery();
75
+ });
76
+
77
+ afterEach(() => {
78
+ cd.stopWatching();
79
+ fs.rmSync(tmpDir, { recursive: true, force: true });
80
+ });
81
+
82
+ it('should discover AGENTS.md', () => {
83
+ fs.writeFileSync(path.join(tmpDir, 'AGENTS.md'), '# Agents');
84
+ const files = cd.discover(tmpDir);
85
+ expect(files.length).toBe(1);
86
+ expect(files[0].type).toBe('agents');
87
+ });
88
+
89
+ it('should discover multiple standard files', () => {
90
+ fs.writeFileSync(path.join(tmpDir, 'SOUL.md'), '# Soul');
91
+ fs.writeFileSync(path.join(tmpDir, 'USER.md'), '# User');
92
+ const files = cd.discover(tmpDir);
93
+ expect(files.length).toBe(2);
94
+ });
95
+
96
+ it('should return empty for no context files', () => {
97
+ expect(cd.discover(tmpDir)).toEqual([]);
98
+ });
99
+
100
+ it('should load and merge files into prompt', () => {
101
+ fs.writeFileSync(path.join(tmpDir, 'SOUL.md'), 'Be nice');
102
+ fs.writeFileSync(path.join(tmpDir, 'TOOLS.md'), 'Use web');
103
+ const files = cd.discover(tmpDir);
104
+ const prompt = cd.load(files);
105
+ expect(prompt).toContain('Be nice');
106
+ expect(prompt).toContain('Use web');
107
+ });
108
+
109
+ it('should add and discover custom files', () => {
110
+ const customPath = path.join(tmpDir, 'CUSTOM.md');
111
+ fs.writeFileSync(customPath, '# Custom');
112
+ cd.addCustomFile(customPath);
113
+ const files = cd.discover(tmpDir);
114
+ expect(files.some(f => f.type === 'custom' && f.content.includes('Custom'))).toBe(true);
115
+ });
116
+
117
+ it('should not duplicate custom files', () => {
118
+ cd.addCustomFile('test.md');
119
+ cd.addCustomFile('test.md');
120
+ // Internal check - discover won't find it but custom list should have 1
121
+ expect(cd.discover(tmpDir).filter(f => f.path.includes('test.md')).length).toBeLessThanOrEqual(1);
122
+ });
123
+
124
+ it('should assign correct types', () => {
125
+ fs.writeFileSync(path.join(tmpDir, 'MEMORY.md'), 'mem');
126
+ fs.writeFileSync(path.join(tmpDir, 'IDENTITY.md'), 'id');
127
+ const files = cd.discover(tmpDir);
128
+ expect(files.find(f => f.type === 'memory')).toBeTruthy();
129
+ expect(files.find(f => f.type === 'identity')).toBeTruthy();
130
+ });
131
+
132
+ it('should handle watch without errors', () => {
133
+ // Just ensure no throw
134
+ cd.watch(tmpDir, () => {});
135
+ cd.stopWatching();
136
+ });
137
+ });
138
+
139
+ // ── Session Manager Tests (8) ──
140
+
141
+ describe('SessionManager', () => {
142
+ let sm: SessionManager;
143
+ let tmpDir: string;
144
+
145
+ beforeEach(() => {
146
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sess-'));
147
+ sm = new SessionManager(tmpDir);
148
+ });
149
+
150
+ afterEach(() => {
151
+ fs.rmSync(tmpDir, { recursive: true, force: true });
152
+ });
153
+
154
+ it('should create a session', () => {
155
+ const s = sm.create('agent1', 'telegram');
156
+ expect(s.id).toBeTruthy();
157
+ expect(s.agentId).toBe('agent1');
158
+ });
159
+
160
+ it('should get a session by id', () => {
161
+ const s = sm.create('agent1', 'telegram');
162
+ expect(sm.get(s.id)).toEqual(s);
163
+ expect(sm.get('nonexistent')).toBeNull();
164
+ });
165
+
166
+ it('should add messages', () => {
167
+ const s = sm.create('a', 'c');
168
+ sm.addMessage(s.id, { id: '1', role: 'user', content: 'hi', timestamp: Date.now() });
169
+ expect(sm.get(s.id)!.messages.length).toBe(1);
170
+ });
171
+
172
+ it('should list with filters', () => {
173
+ sm.create('a1', 'telegram');
174
+ sm.create('a2', 'discord');
175
+ expect(sm.list({ agentId: 'a1' }).length).toBe(1);
176
+ expect(sm.list({ channel: 'discord' }).length).toBe(1);
177
+ });
178
+
179
+ it('should compact sessions', async () => {
180
+ const s = sm.create('a', 'c');
181
+ for (let i = 0; i < 15; i++) {
182
+ sm.addMessage(s.id, { id: `${i}`, role: 'user', content: `msg ${i}`, timestamp: Date.now() });
183
+ }
184
+ await sm.compact(s.id);
185
+ expect(sm.get(s.id)!.messages.length).toBeLessThan(15);
186
+ expect(sm.get(s.id)!.compactedAt).toBeTruthy();
187
+ });
188
+
189
+ it('should prune old sessions', () => {
190
+ const s = sm.create('a', 'c');
191
+ // Manually set old lastActivity
192
+ (sm.get(s.id) as any).lastActivity = Date.now() - 999999;
193
+ expect(sm.prune(1000)).toBe(1);
194
+ });
195
+
196
+ it('should fork and get lineage', () => {
197
+ const parent = sm.create('a', 'c');
198
+ const child = sm.fork(parent.id);
199
+ expect(child.parentId).toBe(parent.id);
200
+ const lineage = sm.getLineage(child.id);
201
+ expect(lineage.length).toBe(2);
202
+ expect(lineage[0].id).toBe(parent.id);
203
+ });
204
+
205
+ it('should export as markdown and save/load', () => {
206
+ const s = sm.create('a', 'c');
207
+ sm.addMessage(s.id, { id: '1', role: 'user', content: 'hello', timestamp: Date.now() });
208
+ const md = sm.export(s.id);
209
+ expect(md).toContain('hello');
210
+ sm.save();
211
+ const sm2 = new SessionManager(tmpDir);
212
+ sm2.load();
213
+ expect(sm2.get(s.id)).toBeTruthy();
214
+ });
215
+ });
216
+
217
+ // ── Heartbeat Tests (4) ──
218
+
219
+ describe('HeartbeatManager', () => {
220
+ it('should require valid config', () => {
221
+ expect(() => new HeartbeatManager({ interval: 0, checkFn: async () => 'ok' })).toThrow('interval');
222
+ expect(() => new HeartbeatManager({ interval: 1000, checkFn: null as any })).toThrow('checkFn');
223
+ });
224
+
225
+ it('should start and stop', () => {
226
+ const hb = new HeartbeatManager({ interval: 100, checkFn: async () => 'ok' });
227
+ hb.start();
228
+ hb.stop();
229
+ // Double stop should be safe
230
+ hb.stop();
231
+ });
232
+
233
+ it('should fire callbacks on beat', async () => {
234
+ const hb = new HeartbeatManager({ interval: 100, checkFn: async () => 'alive' });
235
+ const statuses: string[] = [];
236
+ hb.onBeat(s => statuses.push(s));
237
+ hb.start();
238
+ await new Promise(r => setTimeout(r, 350));
239
+ hb.stop();
240
+ expect(statuses.length).toBeGreaterThanOrEqual(2);
241
+ expect(statuses[0]).toBe('alive');
242
+ });
243
+
244
+ it('should report last beat', async () => {
245
+ const hb = new HeartbeatManager({ interval: 100, checkFn: async () => 'ok' });
246
+ expect(hb.getLastBeat()).toBeNull();
247
+ hb.start();
248
+ await new Promise(r => setTimeout(r, 200));
249
+ hb.stop();
250
+ const beat = hb.getLastBeat();
251
+ expect(beat).toBeTruthy();
252
+ expect(beat!.status).toBe('ok');
253
+ });
254
+ });
@@ -0,0 +1,61 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { VisionManager, detectMimeType } from '../src/core/vision';
3
+ import type { ImageInput, VisionResult } from '../src/core/vision';
4
+
5
+ describe('VisionManager', () => {
6
+ it('should create VisionManager with defaults', () => {
7
+ const vm = new VisionManager();
8
+ expect(vm).toBeDefined();
9
+ });
10
+
11
+ it('should create VisionManager with custom config', () => {
12
+ const vm = new VisionManager({ model: 'gpt-4o-mini', maxImageSize: 5 * 1024 * 1024 });
13
+ expect(vm).toBeDefined();
14
+ });
15
+
16
+ it('should detect PNG mime type', () => {
17
+ const buf = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
18
+ expect(detectMimeType(buf)).toBe('image/png');
19
+ });
20
+
21
+ it('should detect JPEG mime type', () => {
22
+ const buf = Buffer.from([0xFF, 0xD8, 0xFF, 0xE0]);
23
+ expect(detectMimeType(buf)).toBe('image/jpeg');
24
+ });
25
+
26
+ it('should detect GIF mime type', () => {
27
+ const buf = Buffer.from([0x47, 0x49, 0x46, 0x38, 0x39, 0x61]);
28
+ expect(detectMimeType(buf)).toBe('image/gif');
29
+ });
30
+
31
+ it('should detect WebP mime type', () => {
32
+ const buf = Buffer.from([0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00]);
33
+ expect(detectMimeType(buf)).toBe('image/webp');
34
+ });
35
+
36
+ it('should return octet-stream for unknown', () => {
37
+ const buf = Buffer.from([0x00, 0x01, 0x02]);
38
+ expect(detectMimeType(buf)).toBe('application/octet-stream');
39
+ });
40
+
41
+ it('should prepare multimodal message with URL images', () => {
42
+ const vm = new VisionManager();
43
+ const images: ImageInput[] = [{ type: 'url', data: 'https://example.com/img.png' }];
44
+ const msgs = vm.prepareMessage(images, 'Describe this');
45
+ expect(msgs).toHaveLength(1);
46
+ expect((msgs[0] as any).content).toHaveLength(2);
47
+ expect((msgs[0] as any).content[0].type).toBe('text');
48
+ expect((msgs[0] as any).content[1].type).toBe('image_url');
49
+ });
50
+
51
+ it('should throw on analyze without API key', async () => {
52
+ const vm = new VisionManager({ apiKey: '' });
53
+ const input: ImageInput = { type: 'url', data: 'https://example.com/img.png' };
54
+ await expect(vm.analyze(input)).rejects.toThrow('Vision API key not configured');
55
+ });
56
+
57
+ it('should throw on compareImages with less than 2 images', async () => {
58
+ const vm = new VisionManager({ apiKey: 'test' });
59
+ await expect(vm.compareImages([{ type: 'url', data: 'https://example.com/1.png' }])).rejects.toThrow('at least 2');
60
+ });
61
+ });
@@ -0,0 +1,47 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { VoiceCallManager } from '../src/channels/voice-call';
3
+
4
+ describe('VoiceCallManager', () => {
5
+ it('should create with config', () => {
6
+ const mgr = new VoiceCallManager({ provider: 'twilio', credentials: { sid: 'x', token: 'y' } });
7
+ expect(mgr).toBeInstanceOf(VoiceCallManager);
8
+ });
9
+
10
+ it('should throw without credentials on startCall', async () => {
11
+ const mgr = new VoiceCallManager({ provider: 'twilio' });
12
+ await expect(mgr.startCall('+1234')).rejects.toThrow('credentials');
13
+ });
14
+
15
+ it('should start and end a call', async () => {
16
+ const mgr = new VoiceCallManager({ provider: 'webrtc', credentials: { key: 'val' } });
17
+ const callId = await mgr.startCall('+1234');
18
+ expect(callId).toBeTruthy();
19
+ expect(mgr.getCallStatus(callId)).toBe('ringing');
20
+ await mgr.endCall(callId);
21
+ expect(mgr.getCallStatus(callId)).toBe('ended');
22
+ });
23
+
24
+ it('should handle incoming calls', async () => {
25
+ const mgr = new VoiceCallManager({ provider: 'sip', credentials: { key: 'val' } });
26
+ const result = await new Promise<{ callId: string; from: string }>((resolve) => {
27
+ mgr.onIncoming((callId, from) => resolve({ callId, from }));
28
+ mgr.simulateIncoming('+5678');
29
+ });
30
+ expect(result.callId).toBeTruthy();
31
+ expect(result.from).toBe('+5678');
32
+ });
33
+
34
+ it('should list active calls', async () => {
35
+ const mgr = new VoiceCallManager({ provider: 'twilio', credentials: { sid: 'x', token: 'y' } });
36
+ const id1 = await mgr.startCall('+111');
37
+ const id2 = await mgr.startCall('+222');
38
+ expect(mgr.listActiveCalls().length).toBe(2);
39
+ await mgr.endCall(id1);
40
+ expect(mgr.listActiveCalls().length).toBe(1);
41
+ });
42
+
43
+ it('should throw on unknown callId', () => {
44
+ const mgr = new VoiceCallManager({ provider: 'twilio', credentials: { sid: 'x' } });
45
+ expect(() => mgr.getCallStatus('unknown')).toThrow('not found');
46
+ });
47
+ });
@@ -0,0 +1,38 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ /**
4
+ * Voice interaction tests — browser-only APIs (Web Speech API).
5
+ * These tests verify the integration logic rather than the browser APIs themselves.
6
+ */
7
+ describe('Voice Interaction', () => {
8
+ it('should define voice feature flags', () => {
9
+ // In a browser context, these would be available
10
+ // Here we just verify our integration assumptions
11
+ expect(typeof globalThis).toBe('object');
12
+ });
13
+
14
+ it('should handle missing SpeechRecognition gracefully', () => {
15
+ // Simulate no SpeechRecognition API
16
+ const SpeechRecognition = (globalThis as any).SpeechRecognition || (globalThis as any).webkitSpeechRecognition;
17
+ expect(SpeechRecognition).toBeUndefined(); // Not available in Node
18
+ });
19
+
20
+ it('should handle missing speechSynthesis gracefully', () => {
21
+ const synth = (globalThis as any).speechSynthesis;
22
+ expect(synth).toBeUndefined(); // Not available in Node
23
+ });
24
+ });
25
+
26
+ describe('Voice TTS button injection', () => {
27
+ it('should create a button element with correct attributes', () => {
28
+ // Simulate DOM creation logic
29
+ const btn = {
30
+ className: 'tts-btn',
31
+ textContent: '🔊',
32
+ title: 'Read aloud',
33
+ };
34
+ expect(btn.className).toBe('tts-btn');
35
+ expect(btn.textContent).toBe('🔊');
36
+ expect(btn.title).toBe('Read aloud');
37
+ });
38
+ });
@@ -0,0 +1,155 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { parseDuckDuckGoHTML, DEFAULT_SEARCH_CONFIG, webSearch } from '../src/tools/web-search';
3
+ import { extractReadableContent } from '../src/tools/web-scraper';
4
+
5
+ describe('Web Search - DuckDuckGo HTML Parser', () => {
6
+ const mockDDGHTML = `
7
+ <html><body>
8
+ <div class="result__body">
9
+ <a class="result__a" href="https://duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Fpage1">Example Page One</a>
10
+ <a class="result__snippet">This is the first result snippet about example.</a>
11
+ </div>
12
+ <div class="result__body">
13
+ <a class="result__a" href="https://duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.org%2Fpage2">Example &amp; Page Two</a>
14
+ <a class="result__snippet">Second result with <b>bold</b> text.</a>
15
+ </div>
16
+ <div class="result__body">
17
+ <a class="result__a" href="https://direct-url.com/page3">Direct URL Page</a>
18
+ <a class="result__snippet">Third result snippet.</a>
19
+ </div>
20
+ </body></html>
21
+ `;
22
+
23
+ it('should parse search results from DuckDuckGo HTML', () => {
24
+ const results = parseDuckDuckGoHTML(mockDDGHTML);
25
+ expect(results.length).toBe(3);
26
+ });
27
+
28
+ it('should extract title and URL correctly', () => {
29
+ const results = parseDuckDuckGoHTML(mockDDGHTML);
30
+ expect(results[0].title).toBe('Example Page One');
31
+ expect(results[0].url).toBe('https://example.com/page1');
32
+ });
33
+
34
+ it('should decode uddg redirect URLs', () => {
35
+ const results = parseDuckDuckGoHTML(mockDDGHTML);
36
+ expect(results[0].url).toBe('https://example.com/page1');
37
+ expect(results[1].url).toBe('https://example.org/page2');
38
+ });
39
+
40
+ it('should handle direct URLs without uddg', () => {
41
+ const results = parseDuckDuckGoHTML(mockDDGHTML);
42
+ expect(results[2].url).toBe('https://direct-url.com/page3');
43
+ });
44
+
45
+ it('should extract snippets and strip HTML', () => {
46
+ const results = parseDuckDuckGoHTML(mockDDGHTML);
47
+ expect(results[0].snippet).toBe('This is the first result snippet about example.');
48
+ expect(results[1].snippet).toBe('Second result with bold text.');
49
+ });
50
+
51
+ it('should decode HTML entities in titles', () => {
52
+ const results = parseDuckDuckGoHTML(mockDDGHTML);
53
+ expect(results[1].title).toBe('Example & Page Two');
54
+ });
55
+
56
+ it('should return empty array for empty HTML', () => {
57
+ expect(parseDuckDuckGoHTML('')).toEqual([]);
58
+ expect(parseDuckDuckGoHTML('<html><body></body></html>')).toEqual([]);
59
+ });
60
+
61
+ it('should have correct default config', () => {
62
+ expect(DEFAULT_SEARCH_CONFIG.defaultEngine).toBe('duckduckgo');
63
+ expect(DEFAULT_SEARCH_CONFIG.enabled).toBe(true);
64
+ });
65
+ });
66
+
67
+ describe('Web Scraper - Content Extraction', () => {
68
+ it('should extract title from HTML', () => {
69
+ const html = '<html><head><title>Test Page</title></head><body><p>Hello world</p></body></html>';
70
+ const result = extractReadableContent(html, 'https://example.com');
71
+ expect(result.title).toBe('Test Page');
72
+ });
73
+
74
+ it('should extract content and convert to markdown', () => {
75
+ const html = `
76
+ <html><head><title>Test</title></head><body>
77
+ <article>
78
+ <h1>Main Title</h1>
79
+ <p>This is a <strong>bold</strong> paragraph.</p>
80
+ <p>Second paragraph with <a href="https://link.com">a link</a>.</p>
81
+ </article>
82
+ </body></html>
83
+ `;
84
+ const result = extractReadableContent(html, 'https://example.com');
85
+ expect(result.content).toContain('# Main Title');
86
+ expect(result.content).toContain('**bold**');
87
+ expect(result.content).toContain('[a link](https://link.com)');
88
+ });
89
+
90
+ it('should remove script and style tags', () => {
91
+ const html = `
92
+ <html><head><title>Test</title></head><body>
93
+ <script>alert('xss')</script>
94
+ <style>.hidden { display: none; }</style>
95
+ <p>Visible content</p>
96
+ </body></html>
97
+ `;
98
+ const result = extractReadableContent(html, 'https://example.com');
99
+ expect(result.content).not.toContain('alert');
100
+ expect(result.content).not.toContain('.hidden');
101
+ expect(result.content).toContain('Visible content');
102
+ });
103
+
104
+ it('should remove nav and footer', () => {
105
+ const html = `
106
+ <html><head><title>Test</title></head><body>
107
+ <nav><a href="/">Home</a><a href="/about">About</a></nav>
108
+ <main><p>Main content here</p></main>
109
+ <footer>Copyright 2024</footer>
110
+ </body></html>
111
+ `;
112
+ const result = extractReadableContent(html, 'https://example.com');
113
+ expect(result.content).toContain('Main content here');
114
+ });
115
+
116
+ it('should truncate content at maxLength', () => {
117
+ const longContent = '<html><head><title>Test</title></head><body><p>' + 'a'.repeat(10000) + '</p></body></html>';
118
+ const result = extractReadableContent(longContent, 'https://example.com', 100);
119
+ expect(result.content.length).toBeLessThanOrEqual(120); // 100 + truncation message
120
+ expect(result.content).toContain('[truncated]');
121
+ });
122
+
123
+ it('should track word count', () => {
124
+ const html = '<html><head><title>Test</title></head><body><p>One two three four five</p></body></html>';
125
+ const result = extractReadableContent(html, 'https://example.com');
126
+ expect(result.wordCount).toBeGreaterThan(0);
127
+ });
128
+
129
+ it('should use URL as title when no title tag', () => {
130
+ const html = '<html><body><p>No title page</p></body></html>';
131
+ const result = extractReadableContent(html, 'https://example.com');
132
+ expect(result.title).toBe('https://example.com');
133
+ });
134
+
135
+ it('should decode HTML entities', () => {
136
+ const html = '<html><head><title>Test &amp; Page</title></head><body><p>Content with &lt;brackets&gt; and &quot;quotes&quot;</p></body></html>';
137
+ const result = extractReadableContent(html, 'https://example.com');
138
+ expect(result.title).toBe('Test & Page');
139
+ expect(result.content).toContain('<brackets>');
140
+ });
141
+ });
142
+
143
+ describe('Web Search Config', () => {
144
+ it('should have sensible defaults', () => {
145
+ expect(DEFAULT_SEARCH_CONFIG.enabled).toBe(true);
146
+ expect(DEFAULT_SEARCH_CONFIG.defaultEngine).toBe('duckduckgo');
147
+ expect(DEFAULT_SEARCH_CONFIG.engines.duckduckgo?.enabled).toBe(true);
148
+ });
149
+
150
+ it('should return empty when disabled', async () => {
151
+ const config = { ...DEFAULT_SEARCH_CONFIG, enabled: false };
152
+ const results = await webSearch('test', config);
153
+ expect(results).toEqual([]);
154
+ });
155
+ });