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.
- package/README.md +404 -74
- package/README.zh-CN.md +82 -0
- package/dist/channels/dingtalk.d.ts +17 -0
- package/dist/channels/dingtalk.js +38 -0
- package/dist/channels/googlechat.d.ts +14 -0
- package/dist/channels/googlechat.js +37 -0
- package/dist/channels/imessage.d.ts +13 -0
- package/dist/channels/imessage.js +28 -0
- package/dist/channels/irc.d.ts +20 -0
- package/dist/channels/irc.js +71 -0
- package/dist/channels/line.d.ts +14 -0
- package/dist/channels/line.js +28 -0
- package/dist/channels/matrix.d.ts +15 -0
- package/dist/channels/matrix.js +28 -0
- package/dist/channels/mattermost.d.ts +18 -0
- package/dist/channels/mattermost.js +49 -0
- package/dist/channels/msteams.d.ts +14 -0
- package/dist/channels/msteams.js +28 -0
- package/dist/channels/nostr.d.ts +14 -0
- package/dist/channels/nostr.js +28 -0
- package/dist/channels/qq.d.ts +15 -0
- package/dist/channels/qq.js +28 -0
- package/dist/channels/signal.d.ts +14 -0
- package/dist/channels/signal.js +28 -0
- package/dist/channels/sms.d.ts +15 -0
- package/dist/channels/sms.js +28 -0
- package/dist/channels/twitch.d.ts +17 -0
- package/dist/channels/twitch.js +59 -0
- package/dist/channels/voice-call.d.ts +27 -0
- package/dist/channels/voice-call.js +82 -0
- package/dist/channels/whatsapp.d.ts +14 -0
- package/dist/channels/whatsapp.js +28 -0
- package/dist/cli/chat.d.ts +2 -0
- package/dist/cli/chat.js +134 -0
- package/dist/cli/setup.d.ts +4 -0
- package/dist/cli/setup.js +303 -0
- package/dist/cli.js +142 -6
- package/dist/core/api-server.d.ts +25 -0
- package/dist/core/api-server.js +286 -0
- package/dist/core/audio.d.ts +50 -0
- package/dist/core/audio.js +68 -0
- package/dist/core/context-discovery.d.ts +16 -0
- package/dist/core/context-discovery.js +107 -0
- package/dist/core/context-refs.d.ts +29 -0
- package/dist/core/context-refs.js +162 -0
- package/dist/core/gateway.d.ts +53 -0
- package/dist/core/gateway.js +80 -0
- package/dist/core/heartbeat.d.ts +19 -0
- package/dist/core/heartbeat.js +50 -0
- package/dist/core/hooks.d.ts +28 -0
- package/dist/core/hooks.js +82 -0
- package/dist/core/ide-bridge.d.ts +53 -0
- package/dist/core/ide-bridge.js +97 -0
- package/dist/core/node-network.d.ts +23 -0
- package/dist/core/node-network.js +77 -0
- package/dist/core/profiles.d.ts +27 -0
- package/dist/core/profiles.js +131 -0
- package/dist/core/sandbox.d.ts +25 -0
- package/dist/core/sandbox.js +84 -1
- package/dist/core/session-manager.d.ts +33 -0
- package/dist/core/session-manager.js +157 -0
- package/dist/core/vision.d.ts +45 -0
- package/dist/core/vision.js +177 -0
- package/dist/hub/brain-seed.d.ts +14 -0
- package/dist/hub/brain-seed.js +77 -0
- package/dist/hub/client.d.ts +25 -0
- package/dist/hub/client.js +44 -0
- package/dist/index.d.ts +66 -1
- package/dist/index.js +95 -3
- package/dist/memory/context-compressor.d.ts +43 -0
- package/dist/memory/context-compressor.js +167 -0
- package/dist/memory/index.d.ts +4 -0
- package/dist/memory/index.js +5 -1
- package/dist/memory/user-profiler.d.ts +50 -0
- package/dist/memory/user-profiler.js +201 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +54 -1
- package/dist/scheduler/cron-engine.d.ts +41 -0
- package/dist/scheduler/cron-engine.js +200 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +7 -0
- package/dist/schema/oad.d.ts +12 -12
- package/dist/security/approvals.d.ts +53 -0
- package/dist/security/approvals.js +115 -0
- package/dist/security/elevated.d.ts +41 -0
- package/dist/security/elevated.js +89 -0
- package/dist/security/index.d.ts +6 -0
- package/dist/security/index.js +7 -1
- package/dist/security/secrets.d.ts +34 -0
- package/dist/security/secrets.js +115 -0
- package/dist/skills/builtin/index.d.ts +6 -0
- package/dist/skills/builtin/index.js +402 -0
- package/dist/skills/marketplace.d.ts +30 -0
- package/dist/skills/marketplace.js +142 -0
- package/dist/skills/types.d.ts +34 -0
- package/dist/skills/types.js +16 -0
- package/dist/studio/server.d.ts +25 -0
- package/dist/studio/server.js +780 -0
- package/dist/studio/templates-data.d.ts +21 -0
- package/dist/studio/templates-data.js +148 -0
- package/dist/studio-ui/index.html +2502 -1073
- package/dist/tools/builtin/browser.d.ts +47 -0
- package/dist/tools/builtin/browser.js +284 -0
- package/dist/tools/builtin/home-assistant.d.ts +12 -0
- package/dist/tools/builtin/home-assistant.js +126 -0
- package/dist/tools/builtin/index.d.ts +7 -1
- package/dist/tools/builtin/index.js +23 -2
- package/dist/tools/builtin/rl-tools.d.ts +13 -0
- package/dist/tools/builtin/rl-tools.js +228 -0
- package/dist/tools/builtin/vision.d.ts +6 -0
- package/dist/tools/builtin/vision.js +61 -0
- package/dist/tools/builtin/web-search.d.ts +9 -0
- package/dist/tools/builtin/web-search.js +150 -0
- package/dist/tools/document-processor.d.ts +39 -0
- package/dist/tools/document-processor.js +188 -0
- package/dist/tools/image-generator.d.ts +42 -0
- package/dist/tools/image-generator.js +136 -0
- package/dist/tools/web-scraper.d.ts +20 -0
- package/dist/tools/web-scraper.js +148 -0
- package/dist/tools/web-search.d.ts +51 -0
- package/dist/tools/web-search.js +152 -0
- package/install.ps1 +154 -0
- package/install.sh +164 -0
- package/package.json +63 -52
- package/src/channels/dingtalk.ts +46 -0
- package/src/channels/googlechat.ts +42 -0
- package/src/channels/imessage.ts +32 -0
- package/src/channels/irc.ts +82 -0
- package/src/channels/line.ts +33 -0
- package/src/channels/matrix.ts +34 -0
- package/src/channels/mattermost.ts +57 -0
- package/src/channels/msteams.ts +33 -0
- package/src/channels/nostr.ts +33 -0
- package/src/channels/qq.ts +34 -0
- package/src/channels/signal.ts +33 -0
- package/src/channels/sms.ts +34 -0
- package/src/channels/twitch.ts +65 -0
- package/src/channels/voice-call.ts +100 -0
- package/src/channels/whatsapp.ts +33 -0
- package/src/cli/chat.ts +99 -0
- package/src/cli/setup.ts +314 -0
- package/src/cli.ts +148 -6
- package/src/core/api-server.ts +277 -0
- package/src/core/audio.ts +98 -0
- package/src/core/context-discovery.ts +85 -0
- package/src/core/context-refs.ts +140 -0
- package/src/core/gateway.ts +106 -0
- package/src/core/heartbeat.ts +51 -0
- package/src/core/hooks.ts +105 -0
- package/src/core/ide-bridge.ts +133 -0
- package/src/core/node-network.ts +86 -0
- package/src/core/profiles.ts +122 -0
- package/src/core/sandbox.ts +100 -0
- package/src/core/session-manager.ts +137 -0
- package/src/core/vision.ts +180 -0
- package/src/hub/brain-seed.ts +54 -0
- package/src/hub/client.ts +60 -0
- package/src/index.ts +86 -1
- package/src/memory/context-compressor.ts +189 -0
- package/src/memory/index.ts +4 -0
- package/src/memory/user-profiler.ts +215 -0
- package/src/providers/index.ts +64 -1
- package/src/scheduler/cron-engine.ts +191 -0
- package/src/scheduler/index.ts +2 -0
- package/src/security/approvals.ts +143 -0
- package/src/security/elevated.ts +105 -0
- package/src/security/index.ts +6 -0
- package/src/security/secrets.ts +129 -0
- package/src/skills/builtin/index.ts +408 -0
- package/src/skills/marketplace.ts +113 -0
- package/src/skills/types.ts +42 -0
- package/src/studio/server.ts +1591 -791
- package/src/studio/templates-data.ts +178 -0
- package/src/studio-ui/index.html +2502 -1073
- package/src/tools/builtin/browser.ts +299 -0
- package/src/tools/builtin/home-assistant.ts +116 -0
- package/src/tools/builtin/index.ts +37 -28
- package/src/tools/builtin/rl-tools.ts +243 -0
- package/src/tools/builtin/vision.ts +64 -0
- package/src/tools/builtin/web-search.ts +126 -0
- package/src/tools/document-processor.ts +213 -0
- package/src/tools/image-generator.ts +150 -0
- package/src/tools/web-scraper.ts +179 -0
- package/src/tools/web-search.ts +180 -0
- package/tests/api-server.test.ts +148 -0
- package/tests/approvals.test.ts +89 -0
- package/tests/audio.test.ts +40 -0
- package/tests/browser.test.ts +179 -0
- package/tests/builtin-tools.test.ts +83 -83
- package/tests/channels-extra.test.ts +45 -0
- package/tests/context-compressor.test.ts +172 -0
- package/tests/context-refs.test.ts +121 -0
- package/tests/cron-engine.test.ts +101 -0
- package/tests/document-processor.test.ts +69 -0
- package/tests/e2e-nocode.test.ts +442 -0
- package/tests/elevated.test.ts +69 -0
- package/tests/gateway.test.ts +63 -71
- package/tests/home-assistant.test.ts +40 -0
- package/tests/hooks.test.ts +79 -0
- package/tests/ide-bridge.test.ts +38 -0
- package/tests/image-generator.test.ts +84 -0
- package/tests/node-network.test.ts +74 -0
- package/tests/profiles.test.ts +61 -0
- package/tests/rl-tools.test.ts +93 -0
- package/tests/sandbox-manager.test.ts +46 -0
- package/tests/secrets.test.ts +107 -0
- package/tests/settings-api.test.ts +148 -0
- package/tests/setup.test.ts +73 -0
- package/tests/studio.test.ts +402 -229
- package/tests/tools/builtin-extended.test.ts +138 -138
- package/tests/user-profiler.test.ts +169 -0
- package/tests/v090-features.test.ts +254 -0
- package/tests/vision.test.ts +61 -0
- package/tests/voice-call.test.ts +47 -0
- package/tests/voice-interaction.test.ts +38 -0
- package/tests/web-search.test.ts +155 -0
package/tests/studio.test.ts
CHANGED
|
@@ -1,229 +1,402 @@
|
|
|
1
|
-
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
|
2
|
-
import { StudioServer } from '../src/studio/server';
|
|
3
|
-
import { createServer, IncomingMessage, ServerResponse } from 'http';
|
|
4
|
-
import { join } from 'path';
|
|
5
|
-
import { mkdirSync, writeFileSync, rmSync, existsSync, readFileSync } from 'fs';
|
|
6
|
-
import * as http from 'http';
|
|
7
|
-
|
|
8
|
-
const TEST_PORT = 14789;
|
|
9
|
-
const TEST_DIR = join(__dirname, '__studio_test_fixture__');
|
|
10
|
-
const STATIC_DIR = join(TEST_DIR, 'studio-ui');
|
|
11
|
-
|
|
12
|
-
function fetch(path: string, method = 'GET', body?: string): Promise<{ status: number; headers: any; body: string }> {
|
|
13
|
-
return new Promise((resolve, reject) => {
|
|
14
|
-
const opts: http.RequestOptions = {
|
|
15
|
-
hostname: 'localhost',
|
|
16
|
-
port: TEST_PORT,
|
|
17
|
-
path,
|
|
18
|
-
method,
|
|
19
|
-
headers: body ? { 'Content-Type': 'application/json' } : {},
|
|
20
|
-
};
|
|
21
|
-
const req = http.request(opts, (res) => {
|
|
22
|
-
let data = '';
|
|
23
|
-
res.on('data', (c) => (data += c));
|
|
24
|
-
res.on('end', () => resolve({ status: res.statusCode!, headers: res.headers, body: data }));
|
|
25
|
-
});
|
|
26
|
-
req.on('error', reject);
|
|
27
|
-
if (body) req.write(body);
|
|
28
|
-
req.end();
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe('StudioServer', () => {
|
|
33
|
-
let server: StudioServer;
|
|
34
|
-
|
|
35
|
-
beforeAll(async () => {
|
|
36
|
-
// Create test fixture
|
|
37
|
-
mkdirSync(STATIC_DIR, { recursive: true });
|
|
38
|
-
writeFileSync(join(STATIC_DIR, 'index.html'), '<html><body>OPC Studio</body></html>');
|
|
39
|
-
writeFileSync(join(STATIC_DIR, 'app.js'), 'console.log("hello")');
|
|
40
|
-
writeFileSync(join(STATIC_DIR, 'style.css'), 'body { margin: 0; }');
|
|
41
|
-
writeFileSync(join(STATIC_DIR, 'logo.png'), Buffer.from([0x89, 0x50, 0x4e, 0x47]));
|
|
42
|
-
writeFileSync(join(STATIC_DIR, 'icon.svg'), '<svg></svg>');
|
|
43
|
-
writeFileSync(join(TEST_DIR, 'package.json'), JSON.stringify({ name: 'test-agent', version: '1.2.3', description: 'Test agent' }));
|
|
44
|
-
|
|
45
|
-
server = new StudioServer({
|
|
46
|
-
port: TEST_PORT,
|
|
47
|
-
agentDir: TEST_DIR,
|
|
48
|
-
staticDir: STATIC_DIR,
|
|
49
|
-
});
|
|
50
|
-
await server.start();
|
|
51
|
-
// Give server time to bind
|
|
52
|
-
await new Promise((r) => setTimeout(r, 200));
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
afterAll(async () => {
|
|
56
|
-
await server.stop();
|
|
57
|
-
rmSync(TEST_DIR, { recursive: true, force: true });
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// Test 1: Constructor defaults
|
|
61
|
-
it('should have default config values', () => {
|
|
62
|
-
const s = new StudioServer();
|
|
63
|
-
const cfg = s.getConfig();
|
|
64
|
-
expect(cfg.port).toBe(4000);
|
|
65
|
-
expect(cfg.agentDir).toBe(process.cwd());
|
|
66
|
-
expect(cfg.staticDir).toContain('studio-ui');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
// Test 2: Constructor with custom port
|
|
70
|
-
it('should accept custom config', () => {
|
|
71
|
-
const s = new StudioServer({ port: 5555, agentDir: '/tmp/test' });
|
|
72
|
-
const cfg = s.getConfig();
|
|
73
|
-
expect(cfg.port).toBe(5555);
|
|
74
|
-
expect(cfg.agentDir).toBe('/tmp/test');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Test 3: /api/agent/info returns agent info
|
|
78
|
-
it('GET /api/agent/info returns agent info', async () => {
|
|
79
|
-
const res = await fetch('/api/agent/info');
|
|
80
|
-
expect(res.status).toBe(200);
|
|
81
|
-
const data = JSON.parse(res.body);
|
|
82
|
-
expect(data.name).toBe('test-agent');
|
|
83
|
-
expect(data.version).toBe('1.2.3');
|
|
84
|
-
expect(data.status).toBe('running');
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// Test 4: /api/tools/list returns tools
|
|
88
|
-
it('GET /api/tools/list returns tools array', async () => {
|
|
89
|
-
const res = await fetch('/api/tools/list');
|
|
90
|
-
expect(res.status).toBe(200);
|
|
91
|
-
const data = JSON.parse(res.body);
|
|
92
|
-
expect(data).toHaveProperty('tools');
|
|
93
|
-
expect(Array.isArray(data.tools)).toBe(true);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// Test 5: /api/doctor/check runs
|
|
97
|
-
it('GET /api/doctor/check returns result', async () => {
|
|
98
|
-
const res = await fetch('/api/doctor/check');
|
|
99
|
-
expect(res.status).toBe(200);
|
|
100
|
-
const data = JSON.parse(res.body);
|
|
101
|
-
expect(data).toBeDefined();
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// Test 6: unknown API returns 404
|
|
105
|
-
it('GET /api/unknown returns 404', async () => {
|
|
106
|
-
const res = await fetch('/api/nonexistent/route');
|
|
107
|
-
expect(res.status).toBe(404);
|
|
108
|
-
const data = JSON.parse(res.body);
|
|
109
|
-
expect(data.error).toBe('Not found');
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// Test 7: Static file serving - HTML
|
|
113
|
-
it('serves index.html at root', async () => {
|
|
114
|
-
const res = await fetch('/');
|
|
115
|
-
expect(res.status).toBe(200);
|
|
116
|
-
expect(res.headers['content-type']).toBe('text/html');
|
|
117
|
-
expect(res.body).toContain('OPC Studio');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Test 8: Static file serving - JS with correct MIME
|
|
121
|
-
it('serves .js with correct MIME type', async () => {
|
|
122
|
-
const res = await fetch('/app.js');
|
|
123
|
-
expect(res.status).toBe(200);
|
|
124
|
-
expect(res.headers['content-type']).toBe('application/javascript');
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Test 9: Static file serving - CSS with correct MIME
|
|
128
|
-
it('serves .css with correct MIME type', async () => {
|
|
129
|
-
const res = await fetch('/style.css');
|
|
130
|
-
expect(res.status).toBe(200);
|
|
131
|
-
expect(res.headers['content-type']).toBe('text/css');
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Test 10: CORS headers present on API responses
|
|
135
|
-
it('API responses include CORS headers', async () => {
|
|
136
|
-
const res = await fetch('/api/agent/info');
|
|
137
|
-
expect(res.headers['access-control-allow-origin']).toBe('*');
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// Test 11: SPA fallback for unknown static paths
|
|
141
|
-
it('falls back to index.html for unknown paths', async () => {
|
|
142
|
-
const res = await fetch('/some/deep/route');
|
|
143
|
-
expect(res.status).toBe(200);
|
|
144
|
-
expect(res.headers['content-type']).toBe('text/html');
|
|
145
|
-
expect(res.body).toContain('OPC Studio');
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// Test 12: /api/analytics/overview
|
|
149
|
-
it('GET /api/analytics/overview returns analytics', async () => {
|
|
150
|
-
const res = await fetch('/api/analytics/overview');
|
|
151
|
-
expect(res.status).toBe(200);
|
|
152
|
-
const data = JSON.parse(res.body);
|
|
153
|
-
expect(data).toHaveProperty('totalMessages');
|
|
154
|
-
expect(data).toHaveProperty('totalSessions');
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// Test 13: /api/security/approvals
|
|
158
|
-
it('GET /api/security/approvals returns empty approvals', async () => {
|
|
159
|
-
const res = await fetch('/api/security/approvals');
|
|
160
|
-
expect(res.status).toBe(200);
|
|
161
|
-
const data = JSON.parse(res.body);
|
|
162
|
-
expect(data.approvals).toEqual([]);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
// Test 14: /api/logs/recent returns lines array
|
|
166
|
-
it('GET /api/logs/recent returns lines', async () => {
|
|
167
|
-
const res = await fetch('/api/logs/recent');
|
|
168
|
-
expect(res.status).toBe(200);
|
|
169
|
-
const data = JSON.parse(res.body);
|
|
170
|
-
expect(Array.isArray(data.lines)).toBe(true);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
// Test 15: SVG MIME type
|
|
174
|
-
it('serves .svg with correct MIME type', async () => {
|
|
175
|
-
const res = await fetch('/icon.svg');
|
|
176
|
-
expect(res.status).toBe(200);
|
|
177
|
-
expect(res.headers['content-type']).toBe('image/svg+xml');
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
// Test 16: /api/modules returns module status
|
|
181
|
-
it('GET /api/modules returns module status', async () => {
|
|
182
|
-
const res = await fetch('/api/modules');
|
|
183
|
-
expect(res.status).toBe(200);
|
|
184
|
-
const data = JSON.parse(res.body);
|
|
185
|
-
expect(data).toHaveProperty('modules');
|
|
186
|
-
expect(data.modules).toHaveLength(3);
|
|
187
|
-
expect(data.modules[0]).toHaveProperty('name', 'DeepBrain');
|
|
188
|
-
expect(data.modules[0]).toHaveProperty('running');
|
|
189
|
-
expect(data.modules[0]).toHaveProperty('port', 4001);
|
|
190
|
-
expect(data.modules[1]).toHaveProperty('name', 'AgentKits');
|
|
191
|
-
expect(data.modules[2]).toHaveProperty('name', 'Workstation');
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// Test 17: Proxy returns 502 when module not running
|
|
195
|
-
it('proxy returns 502 with friendly message when module not running', async () => {
|
|
196
|
-
const res = await fetch('/brain/');
|
|
197
|
-
expect(res.status).toBe(502);
|
|
198
|
-
expect(res.body).toContain('Module not running');
|
|
199
|
-
expect(res.body).toContain('DeepBrain');
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
// Test 18: Proxy routes are configured for all modules
|
|
203
|
-
it('proxy routes configured for all modules', async () => {
|
|
204
|
-
const brainRes = await fetch('/brain/');
|
|
205
|
-
const kitsRes = await fetch('/kits/');
|
|
206
|
-
const wsRes = await fetch('/workstation/');
|
|
207
|
-
// All should get 502 (not 200/SPA fallback) since no modules running
|
|
208
|
-
expect(brainRes.status).toBe(502);
|
|
209
|
-
expect(kitsRes.status).toBe(502);
|
|
210
|
-
expect(wsRes.status).toBe(502);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
// Test 19:
|
|
214
|
-
it('real index.html contains
|
|
215
|
-
const realHtml = readFileSync(join(__dirname, '../src/studio-ui/index.html'), 'utf-8');
|
|
216
|
-
expect(realHtml).toContain('
|
|
217
|
-
expect(realHtml).toContain('
|
|
218
|
-
expect(realHtml).toContain('
|
|
219
|
-
expect(realHtml).toContain('
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
expect(realHtml).toContain('
|
|
227
|
-
expect(realHtml).toContain('
|
|
228
|
-
|
|
229
|
-
});
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
|
2
|
+
import { StudioServer } from '../src/studio/server';
|
|
3
|
+
import { createServer, IncomingMessage, ServerResponse } from 'http';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { mkdirSync, writeFileSync, rmSync, existsSync, readFileSync } from 'fs';
|
|
6
|
+
import * as http from 'http';
|
|
7
|
+
|
|
8
|
+
const TEST_PORT = 14789;
|
|
9
|
+
const TEST_DIR = join(__dirname, '__studio_test_fixture__');
|
|
10
|
+
const STATIC_DIR = join(TEST_DIR, 'studio-ui');
|
|
11
|
+
|
|
12
|
+
function fetch(path: string, method = 'GET', body?: string): Promise<{ status: number; headers: any; body: string }> {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
const opts: http.RequestOptions = {
|
|
15
|
+
hostname: 'localhost',
|
|
16
|
+
port: TEST_PORT,
|
|
17
|
+
path,
|
|
18
|
+
method,
|
|
19
|
+
headers: body ? { 'Content-Type': 'application/json' } : {},
|
|
20
|
+
};
|
|
21
|
+
const req = http.request(opts, (res) => {
|
|
22
|
+
let data = '';
|
|
23
|
+
res.on('data', (c) => (data += c));
|
|
24
|
+
res.on('end', () => resolve({ status: res.statusCode!, headers: res.headers, body: data }));
|
|
25
|
+
});
|
|
26
|
+
req.on('error', reject);
|
|
27
|
+
if (body) req.write(body);
|
|
28
|
+
req.end();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('StudioServer', () => {
|
|
33
|
+
let server: StudioServer;
|
|
34
|
+
|
|
35
|
+
beforeAll(async () => {
|
|
36
|
+
// Create test fixture
|
|
37
|
+
mkdirSync(STATIC_DIR, { recursive: true });
|
|
38
|
+
writeFileSync(join(STATIC_DIR, 'index.html'), '<html><body>OPC Studio</body></html>');
|
|
39
|
+
writeFileSync(join(STATIC_DIR, 'app.js'), 'console.log("hello")');
|
|
40
|
+
writeFileSync(join(STATIC_DIR, 'style.css'), 'body { margin: 0; }');
|
|
41
|
+
writeFileSync(join(STATIC_DIR, 'logo.png'), Buffer.from([0x89, 0x50, 0x4e, 0x47]));
|
|
42
|
+
writeFileSync(join(STATIC_DIR, 'icon.svg'), '<svg></svg>');
|
|
43
|
+
writeFileSync(join(TEST_DIR, 'package.json'), JSON.stringify({ name: 'test-agent', version: '1.2.3', description: 'Test agent' }));
|
|
44
|
+
|
|
45
|
+
server = new StudioServer({
|
|
46
|
+
port: TEST_PORT,
|
|
47
|
+
agentDir: TEST_DIR,
|
|
48
|
+
staticDir: STATIC_DIR,
|
|
49
|
+
});
|
|
50
|
+
await server.start();
|
|
51
|
+
// Give server time to bind
|
|
52
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterAll(async () => {
|
|
56
|
+
await server.stop();
|
|
57
|
+
rmSync(TEST_DIR, { recursive: true, force: true });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Test 1: Constructor defaults
|
|
61
|
+
it('should have default config values', () => {
|
|
62
|
+
const s = new StudioServer();
|
|
63
|
+
const cfg = s.getConfig();
|
|
64
|
+
expect(cfg.port).toBe(4000);
|
|
65
|
+
expect(cfg.agentDir).toBe(process.cwd());
|
|
66
|
+
expect(cfg.staticDir).toContain('studio-ui');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Test 2: Constructor with custom port
|
|
70
|
+
it('should accept custom config', () => {
|
|
71
|
+
const s = new StudioServer({ port: 5555, agentDir: '/tmp/test' });
|
|
72
|
+
const cfg = s.getConfig();
|
|
73
|
+
expect(cfg.port).toBe(5555);
|
|
74
|
+
expect(cfg.agentDir).toBe('/tmp/test');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Test 3: /api/agent/info returns agent info
|
|
78
|
+
it('GET /api/agent/info returns agent info', async () => {
|
|
79
|
+
const res = await fetch('/api/agent/info');
|
|
80
|
+
expect(res.status).toBe(200);
|
|
81
|
+
const data = JSON.parse(res.body);
|
|
82
|
+
expect(data.name).toBe('test-agent');
|
|
83
|
+
expect(data.version).toBe('1.2.3');
|
|
84
|
+
expect(data.status).toBe('running');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Test 4: /api/tools/list returns tools
|
|
88
|
+
it('GET /api/tools/list returns tools array', async () => {
|
|
89
|
+
const res = await fetch('/api/tools/list');
|
|
90
|
+
expect(res.status).toBe(200);
|
|
91
|
+
const data = JSON.parse(res.body);
|
|
92
|
+
expect(data).toHaveProperty('tools');
|
|
93
|
+
expect(Array.isArray(data.tools)).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Test 5: /api/doctor/check runs
|
|
97
|
+
it('GET /api/doctor/check returns result', async () => {
|
|
98
|
+
const res = await fetch('/api/doctor/check');
|
|
99
|
+
expect(res.status).toBe(200);
|
|
100
|
+
const data = JSON.parse(res.body);
|
|
101
|
+
expect(data).toBeDefined();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Test 6: unknown API returns 404
|
|
105
|
+
it('GET /api/unknown returns 404', async () => {
|
|
106
|
+
const res = await fetch('/api/nonexistent/route');
|
|
107
|
+
expect(res.status).toBe(404);
|
|
108
|
+
const data = JSON.parse(res.body);
|
|
109
|
+
expect(data.error).toBe('Not found');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Test 7: Static file serving - HTML
|
|
113
|
+
it('serves index.html at root', async () => {
|
|
114
|
+
const res = await fetch('/');
|
|
115
|
+
expect(res.status).toBe(200);
|
|
116
|
+
expect(res.headers['content-type']).toBe('text/html');
|
|
117
|
+
expect(res.body).toContain('OPC Studio');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Test 8: Static file serving - JS with correct MIME
|
|
121
|
+
it('serves .js with correct MIME type', async () => {
|
|
122
|
+
const res = await fetch('/app.js');
|
|
123
|
+
expect(res.status).toBe(200);
|
|
124
|
+
expect(res.headers['content-type']).toBe('application/javascript');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Test 9: Static file serving - CSS with correct MIME
|
|
128
|
+
it('serves .css with correct MIME type', async () => {
|
|
129
|
+
const res = await fetch('/style.css');
|
|
130
|
+
expect(res.status).toBe(200);
|
|
131
|
+
expect(res.headers['content-type']).toBe('text/css');
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// Test 10: CORS headers present on API responses
|
|
135
|
+
it('API responses include CORS headers', async () => {
|
|
136
|
+
const res = await fetch('/api/agent/info');
|
|
137
|
+
expect(res.headers['access-control-allow-origin']).toBe('*');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Test 11: SPA fallback for unknown static paths
|
|
141
|
+
it('falls back to index.html for unknown paths', async () => {
|
|
142
|
+
const res = await fetch('/some/deep/route');
|
|
143
|
+
expect(res.status).toBe(200);
|
|
144
|
+
expect(res.headers['content-type']).toBe('text/html');
|
|
145
|
+
expect(res.body).toContain('OPC Studio');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Test 12: /api/analytics/overview
|
|
149
|
+
it('GET /api/analytics/overview returns analytics', async () => {
|
|
150
|
+
const res = await fetch('/api/analytics/overview');
|
|
151
|
+
expect(res.status).toBe(200);
|
|
152
|
+
const data = JSON.parse(res.body);
|
|
153
|
+
expect(data).toHaveProperty('totalMessages');
|
|
154
|
+
expect(data).toHaveProperty('totalSessions');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Test 13: /api/security/approvals
|
|
158
|
+
it('GET /api/security/approvals returns empty approvals', async () => {
|
|
159
|
+
const res = await fetch('/api/security/approvals');
|
|
160
|
+
expect(res.status).toBe(200);
|
|
161
|
+
const data = JSON.parse(res.body);
|
|
162
|
+
expect(data.approvals).toEqual([]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Test 14: /api/logs/recent returns lines array
|
|
166
|
+
it('GET /api/logs/recent returns lines', async () => {
|
|
167
|
+
const res = await fetch('/api/logs/recent');
|
|
168
|
+
expect(res.status).toBe(200);
|
|
169
|
+
const data = JSON.parse(res.body);
|
|
170
|
+
expect(Array.isArray(data.lines)).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Test 15: SVG MIME type
|
|
174
|
+
it('serves .svg with correct MIME type', async () => {
|
|
175
|
+
const res = await fetch('/icon.svg');
|
|
176
|
+
expect(res.status).toBe(200);
|
|
177
|
+
expect(res.headers['content-type']).toBe('image/svg+xml');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Test 16: /api/modules returns module status
|
|
181
|
+
it('GET /api/modules returns module status', async () => {
|
|
182
|
+
const res = await fetch('/api/modules');
|
|
183
|
+
expect(res.status).toBe(200);
|
|
184
|
+
const data = JSON.parse(res.body);
|
|
185
|
+
expect(data).toHaveProperty('modules');
|
|
186
|
+
expect(data.modules).toHaveLength(3);
|
|
187
|
+
expect(data.modules[0]).toHaveProperty('name', 'DeepBrain');
|
|
188
|
+
expect(data.modules[0]).toHaveProperty('running');
|
|
189
|
+
expect(data.modules[0]).toHaveProperty('port', 4001);
|
|
190
|
+
expect(data.modules[1]).toHaveProperty('name', 'AgentKits');
|
|
191
|
+
expect(data.modules[2]).toHaveProperty('name', 'Workstation');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Test 17: Proxy returns 502 when module not running
|
|
195
|
+
it('proxy returns 502 with friendly message when module not running', async () => {
|
|
196
|
+
const res = await fetch('/brain/');
|
|
197
|
+
expect(res.status).toBe(502);
|
|
198
|
+
expect(res.body).toContain('Module not running');
|
|
199
|
+
expect(res.body).toContain('DeepBrain');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// Test 18: Proxy routes are configured for all modules
|
|
203
|
+
it('proxy routes configured for all modules', async () => {
|
|
204
|
+
const brainRes = await fetch('/brain/');
|
|
205
|
+
const kitsRes = await fetch('/kits/');
|
|
206
|
+
const wsRes = await fetch('/workstation/');
|
|
207
|
+
// All should get 502 (not 200/SPA fallback) since no modules running
|
|
208
|
+
expect(brainRes.status).toBe(502);
|
|
209
|
+
expect(kitsRes.status).toBe(502);
|
|
210
|
+
expect(wsRes.status).toBe(502);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Test 19: Studio UI contains dashboard and template pages
|
|
214
|
+
it('real index.html contains no-code agent pages', () => {
|
|
215
|
+
const realHtml = readFileSync(join(__dirname, '../src/studio-ui/index.html'), 'utf-8');
|
|
216
|
+
expect(realHtml).toContain('page-dashboard');
|
|
217
|
+
expect(realHtml).toContain('page-templates');
|
|
218
|
+
expect(realHtml).toContain('page-create');
|
|
219
|
+
expect(realHtml).toContain('page-chat');
|
|
220
|
+
expect(realHtml).toContain('page-memory');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// Test 20: Studio UI has navigation items
|
|
224
|
+
it('real index.html contains navigation items', () => {
|
|
225
|
+
const realHtml = readFileSync(join(__dirname, '../src/studio-ui/index.html'), 'utf-8');
|
|
226
|
+
expect(realHtml).toContain('data-page="dashboard"');
|
|
227
|
+
expect(realHtml).toContain('data-page="templates"');
|
|
228
|
+
expect(realHtml).toContain('data-page="create"');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// === No-Code Agent Platform API Tests ===
|
|
232
|
+
|
|
233
|
+
// Test 21: GET /api/templates returns templates list
|
|
234
|
+
it('GET /api/templates returns templates', async () => {
|
|
235
|
+
const res = await fetch('/api/templates');
|
|
236
|
+
expect(res.status).toBe(200);
|
|
237
|
+
const data = JSON.parse(res.body);
|
|
238
|
+
expect(data.templates).toBeDefined();
|
|
239
|
+
expect(data.templates.length).toBeGreaterThan(50);
|
|
240
|
+
expect(data.industries).toBeDefined();
|
|
241
|
+
expect(data.industries.length).toBe(19);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Test 22: GET /api/templates with industry filter
|
|
245
|
+
it('GET /api/templates?industry=technology filters by industry', async () => {
|
|
246
|
+
const res = await fetch('/api/templates?industry=technology');
|
|
247
|
+
expect(res.status).toBe(200);
|
|
248
|
+
const data = JSON.parse(res.body);
|
|
249
|
+
expect(data.templates.every((t: any) => t.industry === 'technology')).toBe(true);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// Test 23: GET /api/templates with search
|
|
253
|
+
it('GET /api/templates?q=code filters by search', async () => {
|
|
254
|
+
const res = await fetch('/api/templates?q=code');
|
|
255
|
+
expect(res.status).toBe(200);
|
|
256
|
+
const data = JSON.parse(res.body);
|
|
257
|
+
expect(data.templates.length).toBeGreaterThan(0);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Test 24: GET /api/templates/:id returns template detail
|
|
261
|
+
it('GET /api/templates/:id returns template', async () => {
|
|
262
|
+
const res = await fetch('/api/templates/code-reviewer');
|
|
263
|
+
expect(res.status).toBe(200);
|
|
264
|
+
const data = JSON.parse(res.body);
|
|
265
|
+
expect(data.id).toBe('code-reviewer');
|
|
266
|
+
expect(data.name).toContain('Code Reviewer');
|
|
267
|
+
expect(data.systemPrompt).toBeDefined();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Test 25: GET /api/templates/:id returns 404 for unknown
|
|
271
|
+
it('GET /api/templates/:id returns 404 for unknown', async () => {
|
|
272
|
+
const res = await fetch('/api/templates/nonexistent-xyz');
|
|
273
|
+
expect(res.status).toBe(404);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// Test 26: POST /api/agents creates an agent
|
|
277
|
+
it('POST /api/agents creates an agent', async () => {
|
|
278
|
+
const res = await fetch('/api/agents', 'POST', JSON.stringify({
|
|
279
|
+
name: 'Test Agent',
|
|
280
|
+
templateId: 'code-reviewer',
|
|
281
|
+
description: 'Test company',
|
|
282
|
+
model: 'gpt-4o-mini',
|
|
283
|
+
language: 'en',
|
|
284
|
+
}));
|
|
285
|
+
expect(res.status).toBe(201);
|
|
286
|
+
const data = JSON.parse(res.body);
|
|
287
|
+
expect(data.id).toBeDefined();
|
|
288
|
+
expect(data.name).toBe('Test Agent');
|
|
289
|
+
expect(data.templateId).toBe('code-reviewer');
|
|
290
|
+
expect(data.model).toBe('gpt-4o-mini');
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// Test 27: GET /api/agents lists agents
|
|
294
|
+
it('GET /api/agents lists created agents', async () => {
|
|
295
|
+
const res = await fetch('/api/agents');
|
|
296
|
+
expect(res.status).toBe(200);
|
|
297
|
+
const data = JSON.parse(res.body);
|
|
298
|
+
expect(data.agents.length).toBeGreaterThan(0);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// Test 28: GET /api/agents/:id returns agent detail
|
|
302
|
+
it('GET /api/agents/:id returns agent', async () => {
|
|
303
|
+
// First create one
|
|
304
|
+
const createRes = await fetch('/api/agents', 'POST', JSON.stringify({
|
|
305
|
+
name: 'Detail Test Agent',
|
|
306
|
+
templateId: 'tech-support',
|
|
307
|
+
}));
|
|
308
|
+
const agent = JSON.parse(createRes.body);
|
|
309
|
+
|
|
310
|
+
const res = await fetch(`/api/agents/${agent.id}`);
|
|
311
|
+
expect(res.status).toBe(200);
|
|
312
|
+
const data = JSON.parse(res.body);
|
|
313
|
+
expect(data.name).toBe('Detail Test Agent');
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// Test 29: PUT /api/agents/:id updates agent
|
|
317
|
+
it('PUT /api/agents/:id updates agent', async () => {
|
|
318
|
+
const createRes = await fetch('/api/agents', 'POST', JSON.stringify({ name: 'Old Name', templateId: 'tech-support' }));
|
|
319
|
+
const agent = JSON.parse(createRes.body);
|
|
320
|
+
|
|
321
|
+
const res = await fetch(`/api/agents/${agent.id}`, 'PUT', JSON.stringify({ name: 'New Name' }));
|
|
322
|
+
expect(res.status).toBe(200);
|
|
323
|
+
const data = JSON.parse(res.body);
|
|
324
|
+
expect(data.name).toBe('New Name');
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// Test 30: DELETE /api/agents/:id deletes agent
|
|
328
|
+
it('DELETE /api/agents/:id deletes agent', async () => {
|
|
329
|
+
const createRes = await fetch('/api/agents', 'POST', JSON.stringify({ name: 'To Delete', templateId: 'tech-support' }));
|
|
330
|
+
const agent = JSON.parse(createRes.body);
|
|
331
|
+
|
|
332
|
+
const delRes = await fetch(`/api/agents/${agent.id}`, 'DELETE');
|
|
333
|
+
expect(delRes.status).toBe(200);
|
|
334
|
+
|
|
335
|
+
const getRes = await fetch(`/api/agents/${agent.id}`);
|
|
336
|
+
const data = JSON.parse(getRes.body);
|
|
337
|
+
expect(data.error).toBeDefined();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// Test 31: GET /api/agents/:id/memory returns memory
|
|
341
|
+
it('GET /api/agents/:id/memory returns empty memory', async () => {
|
|
342
|
+
const createRes = await fetch('/api/agents', 'POST', JSON.stringify({ name: 'Memory Test', templateId: 'tech-support' }));
|
|
343
|
+
const agent = JSON.parse(createRes.body);
|
|
344
|
+
|
|
345
|
+
const res = await fetch(`/api/agents/${agent.id}/memory`);
|
|
346
|
+
expect(res.status).toBe(200);
|
|
347
|
+
const data = JSON.parse(res.body);
|
|
348
|
+
expect(data.entries).toBeDefined();
|
|
349
|
+
expect(data.timeline).toBeDefined();
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// Test 32: POST /api/agents/:id/chat returns streaming response
|
|
353
|
+
it('POST /api/agents/:id/chat returns response', async () => {
|
|
354
|
+
const createRes = await fetch('/api/agents', 'POST', JSON.stringify({ name: 'Chat Test', templateId: 'tech-support' }));
|
|
355
|
+
const agent = JSON.parse(createRes.body);
|
|
356
|
+
|
|
357
|
+
const res = await fetch(`/api/agents/${agent.id}/chat`, 'POST', JSON.stringify({
|
|
358
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
359
|
+
}));
|
|
360
|
+
expect(res.status).toBe(200);
|
|
361
|
+
expect(res.body.length).toBeGreaterThan(0);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
// Test 33: Unknown agent returns 404
|
|
365
|
+
it('GET /api/agents/unknown returns 404', async () => {
|
|
366
|
+
const res = await fetch('/api/agents/agent-nonexistent-xyz');
|
|
367
|
+
expect(res.status).toBe(404);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// Test 34: GET /api/first-run/status returns firstRun boolean
|
|
371
|
+
it('GET /api/first-run/status returns firstRun boolean and ollamaDetected', async () => {
|
|
372
|
+
const res = await fetch('/api/first-run/status');
|
|
373
|
+
expect(res.status).toBe(200);
|
|
374
|
+
const data = JSON.parse(res.body);
|
|
375
|
+
expect(data).toHaveProperty('firstRun');
|
|
376
|
+
expect(typeof data.firstRun).toBe('boolean');
|
|
377
|
+
expect(data).toHaveProperty('ollamaDetected');
|
|
378
|
+
expect(typeof data.ollamaDetected).toBe('boolean');
|
|
379
|
+
expect(data).toHaveProperty('ollamaModels');
|
|
380
|
+
expect(Array.isArray(data.ollamaModels)).toBe(true);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
// Test 35: POST /api/first-run/complete creates config
|
|
384
|
+
it('POST /api/first-run/complete saves setup choices and returns success', async () => {
|
|
385
|
+
const res = await fetch('/api/first-run/complete', 'POST', JSON.stringify({
|
|
386
|
+
templateId: 'customer-service',
|
|
387
|
+
model: 'qwen2.5:7b',
|
|
388
|
+
}));
|
|
389
|
+
expect(res.status).toBe(200);
|
|
390
|
+
const data = JSON.parse(res.body);
|
|
391
|
+
expect(data.success).toBe(true);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// Test 36: First run status is false after complete
|
|
395
|
+
it('first-run status shows firstRun=false after complete is called', async () => {
|
|
396
|
+
await fetch('/api/first-run/complete', 'POST', JSON.stringify({}));
|
|
397
|
+
const res = await fetch('/api/first-run/status');
|
|
398
|
+
expect(res.status).toBe(200);
|
|
399
|
+
const data = JSON.parse(res.body);
|
|
400
|
+
expect(data.firstRun).toBe(false);
|
|
401
|
+
});
|
|
402
|
+
});
|