opc-agent 4.0.0 → 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 (75) hide show
  1. package/README.md +404 -80
  2. package/README.zh-CN.md +82 -0
  3. package/dist/cli/chat.d.ts +2 -0
  4. package/dist/cli/chat.js +134 -0
  5. package/dist/cli/setup.d.ts +4 -0
  6. package/dist/cli/setup.js +303 -0
  7. package/dist/cli.js +106 -6
  8. package/dist/hub/brain-seed.d.ts +14 -0
  9. package/dist/hub/brain-seed.js +77 -0
  10. package/dist/hub/client.d.ts +25 -0
  11. package/dist/hub/client.js +44 -0
  12. package/dist/index.d.ts +4 -2
  13. package/dist/index.js +12 -3
  14. package/dist/providers/index.d.ts +1 -1
  15. package/dist/providers/index.js +54 -1
  16. package/dist/scheduler/cron-engine.d.ts +41 -0
  17. package/dist/scheduler/cron-engine.js +200 -0
  18. package/dist/scheduler/index.d.ts +3 -0
  19. package/dist/scheduler/index.js +7 -0
  20. package/dist/skills/builtin/index.d.ts +6 -0
  21. package/dist/skills/builtin/index.js +402 -0
  22. package/dist/skills/marketplace.d.ts +30 -0
  23. package/dist/skills/marketplace.js +142 -0
  24. package/dist/skills/types.d.ts +34 -0
  25. package/dist/skills/types.js +16 -0
  26. package/dist/studio/server.d.ts +25 -0
  27. package/dist/studio/server.js +780 -0
  28. package/dist/studio/templates-data.d.ts +21 -0
  29. package/dist/studio/templates-data.js +148 -0
  30. package/dist/studio-ui/index.html +2502 -1073
  31. package/dist/tools/builtin/index.d.ts +1 -0
  32. package/dist/tools/builtin/index.js +7 -2
  33. package/dist/tools/builtin/web-search.d.ts +9 -0
  34. package/dist/tools/builtin/web-search.js +150 -0
  35. package/dist/tools/document-processor.d.ts +39 -0
  36. package/dist/tools/document-processor.js +188 -0
  37. package/dist/tools/image-generator.d.ts +42 -0
  38. package/dist/tools/image-generator.js +136 -0
  39. package/dist/tools/web-scraper.d.ts +20 -0
  40. package/dist/tools/web-scraper.js +148 -0
  41. package/dist/tools/web-search.d.ts +51 -0
  42. package/dist/tools/web-search.js +152 -0
  43. package/install.ps1 +154 -0
  44. package/install.sh +164 -0
  45. package/package.json +63 -52
  46. package/src/cli/chat.ts +99 -0
  47. package/src/cli/setup.ts +314 -0
  48. package/src/cli.ts +108 -6
  49. package/src/hub/brain-seed.ts +54 -0
  50. package/src/hub/client.ts +60 -0
  51. package/src/index.ts +4 -2
  52. package/src/providers/index.ts +64 -1
  53. package/src/scheduler/cron-engine.ts +191 -0
  54. package/src/scheduler/index.ts +2 -0
  55. package/src/skills/builtin/index.ts +408 -0
  56. package/src/skills/marketplace.ts +113 -0
  57. package/src/skills/types.ts +42 -0
  58. package/src/studio/server.ts +1591 -791
  59. package/src/studio/templates-data.ts +178 -0
  60. package/src/studio-ui/index.html +2502 -1073
  61. package/src/tools/builtin/index.ts +37 -35
  62. package/src/tools/builtin/web-search.ts +126 -0
  63. package/src/tools/document-processor.ts +213 -0
  64. package/src/tools/image-generator.ts +150 -0
  65. package/src/tools/web-scraper.ts +179 -0
  66. package/src/tools/web-search.ts +180 -0
  67. package/tests/cron-engine.test.ts +101 -0
  68. package/tests/document-processor.test.ts +69 -0
  69. package/tests/e2e-nocode.test.ts +442 -0
  70. package/tests/image-generator.test.ts +84 -0
  71. package/tests/settings-api.test.ts +148 -0
  72. package/tests/setup.test.ts +73 -0
  73. package/tests/studio.test.ts +402 -229
  74. package/tests/voice-interaction.test.ts +38 -0
  75. package/tests/web-search.test.ts +155 -0
@@ -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: Module nav items in real index.html
214
- it('real index.html contains module nav items', () => {
215
- const realHtml = readFileSync(join(__dirname, '../src/studio-ui/index.html'), 'utf-8');
216
- expect(realHtml).toContain('data-page="brain-module"');
217
- expect(realHtml).toContain('data-page="kits-module"');
218
- expect(realHtml).toContain('data-page="workstation-module"');
219
- expect(realHtml).toContain('data-page="modules"');
220
- });
221
-
222
- // Test 20: Iframe src correct in real index.html
223
- it('real index.html contains correct iframe srcs', () => {
224
- const realHtml = readFileSync(join(__dirname, '../src/studio-ui/index.html'), 'utf-8');
225
- expect(realHtml).toContain('src="/brain/"');
226
- expect(realHtml).toContain('src="/kits/"');
227
- expect(realHtml).toContain('src="/workstation/"');
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
+ });