@dyyz1993/create-agent 2.0.0 → 2.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 (141) hide show
  1. package/package.json +1 -1
  2. package/src/lib/copy.ts +23 -1
  3. package/templates/agent/eslint.config.mjs +15 -2
  4. package/templates/agent/package.json +10 -2
  5. package/templates/agent/src/__tests__/hybrid-mode.test.ts +126 -0
  6. package/templates/agent/src/__tests__/server-config-security.test.ts +55 -0
  7. package/templates/agent/src/bun/index.ts +96 -41
  8. package/templates/agent/src/bun/three.d.ts +1 -0
  9. package/templates/agent/src/gateway/__tests__/ws-handler-token.test.ts +118 -0
  10. package/templates/agent/src/gateway/http-routes.ts +1 -1
  11. package/templates/agent/src/gateway/ws-handler.ts +84 -56
  12. package/templates/agent/src/mainview/__tests__/i18n/i18n.test.ts +1 -0
  13. package/templates/agent/src/mainview/__tests__/setup.ts +32 -29
  14. package/templates/agent/src/mainview/__tests__/theme-variables.test.ts +36 -0
  15. package/templates/agent/src/mainview/components/common/ErrorBoundary.tsx +1 -58
  16. package/templates/agent/src/mainview/components/common/LanguageSwitcher.tsx +1 -0
  17. package/templates/agent/src/mainview/components/common/ThemeToggle.tsx +1 -0
  18. package/templates/agent/src/mainview/components/feed/FeedPanel.tsx +232 -210
  19. package/templates/agent/src/mainview/components/todo/TodoPanel.tsx +149 -122
  20. package/templates/agent/src/mainview/lib/api-client.ts +182 -172
  21. package/templates/agent/src/server-config.ts +34 -32
  22. package/templates/agent/src/server.ts +43 -54
  23. package/templates/agent/src/shared/handlers/__tests__/chat-concurrency.test.ts +127 -0
  24. package/templates/agent/src/shared/handlers/__tests__/handler-isolation.test.ts +244 -0
  25. package/templates/agent/src/shared/handlers/bash.ts +65 -65
  26. package/templates/agent/src/shared/handlers/chat.ts +189 -155
  27. package/templates/agent/src/shared/handlers/debug.ts +8 -0
  28. package/templates/agent/src/shared/handlers/feed.ts +31 -32
  29. package/templates/agent/src/shared/handlers/index.ts +1 -0
  30. package/templates/agent/src/shared/handlers/rules.ts +31 -31
  31. package/templates/agent/src/shared/handlers/todo.ts +31 -31
  32. package/templates/agent/src/shared/http-routes.ts +250 -0
  33. package/templates/agent/src/shared/lib/__tests__/web-server.test.ts +190 -0
  34. package/templates/agent/src/shared/lib/logger.ts +117 -95
  35. package/templates/agent/src/shared/lib/web-server.ts +128 -0
  36. package/templates/agent/src/shared/modules/debug.ts +8 -0
  37. package/templates/agent/src/shared/rpc-schema.ts +16 -3
  38. package/templates/agent/test-upload.txt +0 -0
  39. package/templates/agent/tsconfig.ipc.json +10 -0
  40. package/templates/agent/tsconfig.json +10 -2
  41. package/templates/chat/eslint.config.mjs +15 -2
  42. package/templates/chat/package.json +10 -2
  43. package/templates/chat/src/__tests__/hybrid-mode.test.ts +125 -0
  44. package/templates/chat/src/__tests__/server-config-security.test.ts +55 -0
  45. package/templates/chat/src/__tests__/server-config.test.ts +46 -44
  46. package/templates/chat/src/bun/index.ts +94 -44
  47. package/templates/chat/src/bun/three.d.ts +1 -0
  48. package/templates/chat/src/gateway/http-routes.ts +1 -1
  49. package/templates/chat/src/gateway/ws-handler.ts +84 -56
  50. package/templates/chat/src/mainview/App.tsx +6 -0
  51. package/templates/chat/src/mainview/__tests__/i18n/i18n.test.ts +1 -0
  52. package/templates/chat/src/mainview/__tests__/theme-variables.test.ts +36 -0
  53. package/templates/chat/src/mainview/components/common/LanguageSwitcher.tsx +1 -0
  54. package/templates/chat/src/mainview/components/common/ThemeToggle.tsx +1 -0
  55. package/templates/chat/src/mainview/lib/api-client.ts +183 -156
  56. package/templates/chat/src/mainview/lib/rpc-cache.ts +84 -0
  57. package/templates/chat/src/mainview/main.tsx +4 -3
  58. package/templates/chat/src/server-config.ts +33 -31
  59. package/templates/chat/src/server.ts +43 -60
  60. package/templates/chat/src/shared/handlers/chat.ts +196 -162
  61. package/templates/chat/src/shared/handlers/debug.ts +8 -0
  62. package/templates/chat/src/shared/handlers/index.ts +1 -0
  63. package/templates/chat/src/shared/http-routes.ts +250 -0
  64. package/templates/chat/src/shared/lib/__tests__/logger.test.ts +92 -0
  65. package/templates/chat/src/shared/lib/logger.ts +108 -95
  66. package/templates/chat/src/shared/lib/web-server.ts +128 -0
  67. package/templates/chat/src/shared/modules/debug.ts +8 -0
  68. package/templates/chat/src/shared/rpc-schema.ts +3 -2
  69. package/templates/chat/test-upload.txt +0 -0
  70. package/templates/chat/tsconfig.ipc.json +10 -0
  71. package/templates/general/eslint.config.mjs +15 -2
  72. package/templates/general/package.json +10 -2
  73. package/templates/general/src/__tests__/hybrid-mode.test.ts +125 -0
  74. package/templates/general/src/__tests__/server-config-security.test.ts +55 -0
  75. package/templates/general/src/__tests__/server-config.test.ts +46 -44
  76. package/templates/general/src/bun/index.ts +94 -44
  77. package/templates/general/src/bun/three.d.ts +1 -0
  78. package/templates/general/src/gateway/http-routes.ts +1 -1
  79. package/templates/general/src/gateway/ws-handler.ts +84 -56
  80. package/templates/general/src/mainview/__tests__/i18n/i18n.test.ts +1 -0
  81. package/templates/general/src/mainview/__tests__/theme-variables.test.ts +36 -0
  82. package/templates/general/src/mainview/components/common/LanguageSwitcher.tsx +1 -0
  83. package/templates/general/src/mainview/components/common/ThemeToggle.tsx +1 -0
  84. package/templates/general/src/mainview/components/feed/FeedPanel.tsx +241 -219
  85. package/templates/general/src/mainview/components/layout/AppLayout.tsx +6 -0
  86. package/templates/general/src/mainview/lib/api-client.ts +183 -156
  87. package/templates/general/src/mainview/lib/rpc-cache.ts +84 -0
  88. package/templates/general/src/mainview/main.tsx +4 -3
  89. package/templates/general/src/server-config.ts +33 -31
  90. package/templates/general/src/server.ts +43 -60
  91. package/templates/general/src/shared/handlers/__tests__/chat.test.ts +165 -160
  92. package/templates/general/src/shared/handlers/chat.ts +189 -155
  93. package/templates/general/src/shared/handlers/debug.ts +8 -0
  94. package/templates/general/src/shared/handlers/feed.ts +38 -40
  95. package/templates/general/src/shared/handlers/index.ts +1 -0
  96. package/templates/general/src/shared/http-routes.ts +250 -0
  97. package/templates/general/src/shared/lib/__tests__/logger.test.ts +92 -0
  98. package/templates/general/src/shared/lib/logger.ts +117 -95
  99. package/templates/general/src/shared/lib/web-server.ts +128 -0
  100. package/templates/general/src/shared/modules/debug.ts +8 -0
  101. package/templates/general/src/shared/rpc-schema.ts +12 -2
  102. package/templates/general/test-upload.txt +0 -0
  103. package/templates/general/tsconfig.ipc.json +10 -0
  104. package/templates/shared/__tests__/cors-security.test.ts +165 -0
  105. package/templates/shared/__tests__/file-path-security.test.ts +260 -0
  106. package/templates/shared/__tests__/http-routes.test.ts +174 -0
  107. package/templates/shared/__tests__/logger.test.ts +186 -0
  108. package/templates/shared/components/ErrorBoundary.tsx +58 -0
  109. package/templates/shared/env.d.ts +11 -0
  110. package/templates/shared/http-routes.ts +230 -0
  111. package/templates/shared/logger.ts +129 -0
  112. package/templates/shared/test-upload.txt +0 -0
  113. package/templates/shared/vite-base.config.ts +92 -0
  114. package/templates/shared/vitest-base.config.ts +42 -0
  115. package/templates/agent/eslint-plugin-rpc/index.js +0 -50
  116. package/templates/agent/eslint-plugin-rpc/package.json +0 -6
  117. package/templates/agent/eslint-plugin-rpc/rules/module-file-naming.js +0 -99
  118. package/templates/agent/eslint-plugin-rpc/rules/no-bare-method.js +0 -78
  119. package/templates/agent/eslint-plugin-rpc/rules/no-direct-register.js +0 -65
  120. package/templates/agent/eslint-plugin-rpc/rules/no-hardcoded-strings.js +0 -69
  121. package/templates/agent/eslint-plugin-rpc/rules/require-api-client.js +0 -58
  122. package/templates/agent/eslint-plugin-rpc/rules/require-typed-register.js +0 -78
  123. package/templates/agent/eslint-plugin-rpc/rules/schema-merge-only.js +0 -55
  124. package/templates/chat/eslint-plugin-rpc/index.js +0 -50
  125. package/templates/chat/eslint-plugin-rpc/package.json +0 -6
  126. package/templates/chat/eslint-plugin-rpc/rules/module-file-naming.js +0 -99
  127. package/templates/chat/eslint-plugin-rpc/rules/no-bare-method.js +0 -78
  128. package/templates/chat/eslint-plugin-rpc/rules/no-direct-register.js +0 -65
  129. package/templates/chat/eslint-plugin-rpc/rules/no-hardcoded-strings.js +0 -69
  130. package/templates/chat/eslint-plugin-rpc/rules/require-api-client.js +0 -58
  131. package/templates/chat/eslint-plugin-rpc/rules/require-typed-register.js +0 -78
  132. package/templates/chat/eslint-plugin-rpc/rules/schema-merge-only.js +0 -55
  133. package/templates/general/eslint-plugin-rpc/index.js +0 -50
  134. package/templates/general/eslint-plugin-rpc/package.json +0 -6
  135. package/templates/general/eslint-plugin-rpc/rules/module-file-naming.js +0 -99
  136. package/templates/general/eslint-plugin-rpc/rules/no-bare-method.js +0 -78
  137. package/templates/general/eslint-plugin-rpc/rules/no-direct-register.js +0 -65
  138. package/templates/general/eslint-plugin-rpc/rules/no-hardcoded-strings.js +0 -69
  139. package/templates/general/eslint-plugin-rpc/rules/require-api-client.js +0 -58
  140. package/templates/general/eslint-plugin-rpc/rules/require-typed-register.js +0 -78
  141. package/templates/general/eslint-plugin-rpc/rules/schema-merge-only.js +0 -55
@@ -0,0 +1,128 @@
1
+ import { createServer, type Server } from "http";
2
+ import { networkInterfaces } from "os";
3
+ import { createHttpHandler } from "../http-routes";
4
+ import { createWsHandler } from "../../gateway/ws-handler";
5
+ import { createLogger } from "./logger";
6
+
7
+ const log = createLogger("web-server");
8
+
9
+ export interface WebServerConfig {
10
+ port: number;
11
+ authToken: string;
12
+ maxUploadSize: number;
13
+ corsOrigin: string;
14
+ }
15
+
16
+ export interface WebServerResult {
17
+ httpServer: Server;
18
+ wss: ReturnType<typeof createWsHandler>;
19
+ port: number;
20
+ authToken: string;
21
+ close: () => Promise<void>;
22
+ }
23
+
24
+ export function getLocalIP(): string {
25
+ const nets = networkInterfaces();
26
+ for (const name of Object.keys(nets)) {
27
+ const interfaces = nets[name];
28
+ if (!interfaces) continue;
29
+ for (const net of interfaces) {
30
+ if (net.family === "IPv4" && !net.internal) {
31
+ return net.address;
32
+ }
33
+ }
34
+ }
35
+ return "127.0.0.1";
36
+ }
37
+
38
+ function checkPort(port: number): Promise<number> {
39
+ return new Promise((resolve, reject) => {
40
+ const testServer = createServer();
41
+ testServer.once("error", (err: NodeJS.ErrnoException) => {
42
+ testServer.close();
43
+ reject(err);
44
+ });
45
+ testServer.once("listening", () => {
46
+ testServer.close();
47
+ resolve(port);
48
+ });
49
+ testServer.listen(port);
50
+ });
51
+ }
52
+
53
+ export async function findAvailablePort(startPort: number, maxRetries = 10): Promise<number> {
54
+ for (let port = startPort; port < startPort + maxRetries; port++) {
55
+ try {
56
+ await checkPort(port);
57
+ return port;
58
+ } catch {
59
+ continue;
60
+ }
61
+ }
62
+ throw new Error(`No available port found after ${maxRetries} retries starting from ${startPort}`);
63
+ }
64
+
65
+ function getFreePort(): Promise<number> {
66
+ return new Promise((resolve, reject) => {
67
+ const s = createServer();
68
+ s.listen(0, () => {
69
+ const addr = s.address();
70
+ s.close(() => {
71
+ if (typeof addr === "object" && addr) {
72
+ resolve(addr.port);
73
+ } else {
74
+ reject(new Error("Failed to allocate port"));
75
+ }
76
+ });
77
+ });
78
+ s.on("error", reject);
79
+ });
80
+ }
81
+
82
+ export async function createWebServer(config: WebServerConfig): Promise<WebServerResult> {
83
+ const port = config.port === 0 ? await getFreePort() : await findAvailablePort(config.port);
84
+
85
+ const httpServer = createServer();
86
+
87
+ const wss = createWsHandler(httpServer, {
88
+ config: {
89
+ port,
90
+ authToken: config.authToken,
91
+ maxUploadSize: config.maxUploadSize,
92
+ },
93
+ });
94
+
95
+ httpServer.on(
96
+ "request",
97
+ createHttpHandler({
98
+ config: {
99
+ port,
100
+ authToken: config.authToken,
101
+ maxUploadSize: config.maxUploadSize,
102
+ corsOrigin: config.corsOrigin,
103
+ },
104
+ getWebSocketClientCount: () => wss.clients.size,
105
+ })
106
+ );
107
+
108
+ return new Promise((resolve, reject) => {
109
+ httpServer.listen(port, () => {
110
+ log.info("Web server started", { port, corsOrigin: config.corsOrigin });
111
+
112
+ resolve({
113
+ httpServer,
114
+ wss,
115
+ port,
116
+ authToken: config.authToken,
117
+ close: () =>
118
+ new Promise<void>((res) => {
119
+ wss.clients.forEach((ws) => ws.close());
120
+ wss.close(() => {
121
+ httpServer.close(() => res());
122
+ });
123
+ }),
124
+ });
125
+ });
126
+ httpServer.on("error", reject);
127
+ });
128
+ }
@@ -0,0 +1,8 @@
1
+ export interface DebugMethods {
2
+ "debug.subscriptions": {
3
+ params: {};
4
+ result: {
5
+ subscriptions: Array<{ id: string; eventType: string; filter: Record<string, unknown> }>;
6
+ };
7
+ };
8
+ }
@@ -8,6 +8,7 @@ import type { FeedMethods, FeedEvents } from "./modules/feed";
8
8
  import type { BashMethods, BashEvents } from "./modules/bash";
9
9
  import type { RulesMethods } from "./modules/rules";
10
10
  import type { TodoMethods } from "./modules/todo";
11
+ import type { DebugMethods } from "./modules/debug";
11
12
 
12
13
  /**
13
14
  * RPC 方法定义 — 前后端共享的唯一类型入口
@@ -19,7 +20,19 @@ import type { TodoMethods } from "./modules/todo";
19
20
  * handlers/ 自动发现,无需修改 register-all-handlers.ts
20
21
  * 入口文件(bun/index.ts, server.ts)无需修改
21
22
  */
22
- export interface RPCMethods extends AnyMethods, SystemMethods, FileMethods, TimerMethods, ChatMethods, GitMethods, FeedMethods, BashMethods, RulesMethods, TodoMethods {}
23
+ export interface RPCMethods
24
+ extends
25
+ AnyMethods,
26
+ SystemMethods,
27
+ FileMethods,
28
+ TimerMethods,
29
+ ChatMethods,
30
+ GitMethods,
31
+ FeedMethods,
32
+ BashMethods,
33
+ RulesMethods,
34
+ TodoMethods,
35
+ DebugMethods {}
23
36
 
24
37
  /**
25
38
  * RPC 事件定义 — 合并所有模块事件
@@ -30,6 +43,6 @@ export interface RPCEvents extends TimerEvents, ChatEvents, FeedEvents, BashEven
30
43
  * Handler 注册选项
31
44
  */
32
45
  export interface HandlerOptions {
33
- platform: "desktop" | "web";
34
- enableBash?: boolean;
46
+ platform: "desktop" | "web";
47
+ enableBash?: boolean;
35
48
  }
File without changes
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "noUnusedLocals": false,
5
+ "noUnusedParameters": false,
6
+ "skipLibCheck": true
7
+ },
8
+ "include": ["src/bun", "src/gateway/ipc-transport.ts", "src/shared"],
9
+ "exclude": ["node_modules", "dist", "build", "**/__tests__/**", "**/*.test.ts", "**/*.test.tsx"]
10
+ }
@@ -17,10 +17,18 @@
17
17
  "noFallthroughCasesInSwitch": true,
18
18
  "baseUrl": ".",
19
19
  "paths": {
20
- "@dyyz1993/rpc-core": ["../packages/rpc-core/src/index.ts"],
20
+ "@dyyz1993/rpc-core": ["../../packages/rpc-core/src/index.ts"],
21
21
  "@shared/*": ["../shared/*"]
22
22
  }
23
23
  },
24
24
  "include": ["src", "../shared"],
25
- "exclude": ["node_modules", "dist", "build", "../../package/dist", "**/__tests__/**", "**/*.test.ts", "**/*.test.tsx"]
25
+ "exclude": [
26
+ "node_modules",
27
+ "dist",
28
+ "build",
29
+ "../../package/dist",
30
+ "**/__tests__/**",
31
+ "**/*.test.ts",
32
+ "**/*.test.tsx"
33
+ ]
26
34
  }
@@ -1,6 +1,9 @@
1
1
  import js from '@eslint/js';
2
2
  import tseslint from 'typescript-eslint';
3
- import rpcPlugin from './eslint-plugin-rpc/index.js';
3
+ import rpcPlugin from '@dyyz1993/eslint-plugin-rpc';
4
+ import reactHooks from 'eslint-plugin-react-hooks';
5
+ import reactRefresh from 'eslint-plugin-react-refresh';
6
+ import jsxA11y from 'eslint-plugin-jsx-a11y';
4
7
 
5
8
  export default tseslint.config(
6
9
  js.configs.recommended,
@@ -10,7 +13,6 @@ export default tseslint.config(
10
13
  'node_modules/**',
11
14
  'build/**',
12
15
  'dist/**',
13
- 'eslint-plugin-rpc/**',
14
16
  'postcss.config.js',
15
17
  'tailwind.config.js',
16
18
  ],
@@ -19,8 +21,19 @@ export default tseslint.config(
19
21
  files: ['src/**/*.{ts,tsx}'],
20
22
  plugins: {
21
23
  rpc: rpcPlugin,
24
+ 'react-hooks': reactHooks,
25
+ 'react-refresh': reactRefresh,
26
+ 'jsx-a11y': jsxA11y,
22
27
  },
23
28
  rules: {
29
+ ...reactHooks.configs.recommended.rules,
30
+ 'react-hooks/rules-of-hooks': 'warn',
31
+ 'react-hooks/exhaustive-deps': 'warn',
32
+ 'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
33
+ ...jsxA11y.configs.recommended.rules,
34
+ 'jsx-a11y/click-events-have-key-events': 'warn',
35
+ 'jsx-a11y/no-static-element-interactions': 'warn',
36
+ 'jsx-a11y/no-autofocus': 'warn',
24
37
  // TS 严格规则
25
38
  '@typescript-eslint/no-explicit-any': 'error',
26
39
  '@typescript-eslint/no-empty-object-type': ['error', { allowObjectTypes: 'always', allowInterfaces: 'with-single-extends' }],
@@ -9,6 +9,9 @@
9
9
  "dev:server": "bun src/server.ts",
10
10
  "build": "vite build",
11
11
  "build:canary": "vite build && electrobun build --env=canary",
12
+ "build:mac": "vite build && electrobun build --env=canary",
13
+ "build:win": "vite build && electrobun build --env=canary",
14
+ "build:linux": "vite build && electrobun build --env=canary",
12
15
  "lint": "eslint .",
13
16
  "lint:fix": "eslint . --fix",
14
17
  "prepare": "husky",
@@ -19,7 +22,7 @@
19
22
  "dependencies": {
20
23
  "@dyyz1993/rpc-core": "^1.2.0",
21
24
  "@tanstack/react-virtual": "^3.13.24",
22
- "electrobun": "1.13.1",
25
+ "electrobun": "1.18.1",
23
26
  "i18next": "^24.2.2",
24
27
  "i18next-browser-languagedetector": "^8.0.4",
25
28
  "lucide-react": "^1.9.0",
@@ -32,7 +35,8 @@
32
35
  "zustand": "^5.0.12"
33
36
  },
34
37
  "devDependencies": {
35
- "@eslint/js": "^9.0.0",
38
+ "@dyyz1993/eslint-plugin-rpc": "workspace:*",
39
+ "@eslint/js": "^9.0.0",
36
40
  "@testing-library/jest-dom": "^6.9.1",
37
41
  "@testing-library/react": "^16.3.2",
38
42
  "@testing-library/user-event": "^14.6.1",
@@ -40,10 +44,14 @@
40
44
  "@types/react": "^18.3.12",
41
45
  "@types/react-dom": "^18.3.1",
42
46
  "@types/ws": "^8.18.1",
47
+ "ws": "^8.18.0",
43
48
  "@vitejs/plugin-react": "^4.3.4",
44
49
  "autoprefixer": "^10.4.20",
45
50
  "concurrently": "^9.1.0",
46
51
  "eslint": "^9.0.0",
52
+ "eslint-plugin-jsx-a11y": "^6.10.0",
53
+ "eslint-plugin-react-hooks": "^5.2.0",
54
+ "eslint-plugin-react-refresh": "^0.4.20",
47
55
  "happy-dom": "^20.9.0",
48
56
  "husky": "^9.1.0",
49
57
  "jsdom": "^29.1.1",
@@ -0,0 +1,125 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+
3
+ vi.mock("electrobun/bun", () => ({
4
+ BrowserWindow: {
5
+ defineRPC: vi.fn(() => ({})),
6
+ },
7
+ BrowserView: {
8
+ defineRPC: vi.fn(() => ({
9
+ requests: {},
10
+ messages: {},
11
+ })),
12
+ },
13
+ Updater: {
14
+ localInfo: vi.fn(() => ({ channel: vi.fn(() => "dev") })),
15
+ },
16
+ ApplicationMenu: {
17
+ setApplicationMenu: vi.fn(),
18
+ },
19
+ }));
20
+
21
+ vi.mock("electrobun", () => ({}));
22
+
23
+ describe("混合模式", () => {
24
+ beforeEach(() => {
25
+ vi.resetModules();
26
+ delete process.env.ENABLE_WEB_SERVICE;
27
+ delete process.env.AUTH_TOKEN;
28
+ process.env.NODE_ENV = "test";
29
+ });
30
+
31
+ afterEach(() => {
32
+ vi.restoreAllMocks();
33
+ });
34
+
35
+ it("默认不启动 Web 服务(enableWebService 为 false)", async () => {
36
+ const { config } = await import("../server-config");
37
+ expect(config.enableWebService).toBe(false);
38
+ });
39
+
40
+ it("enableWebService=true 时启动 HTTP + WS 服务", async () => {
41
+ process.env.ENABLE_WEB_SERVICE = "true";
42
+ const { config } = await import("../server-config");
43
+ expect(config.enableWebService).toBe(true);
44
+ });
45
+
46
+ it("IPC 和 WS 服务同时工作,状态隔离", async () => {
47
+ const { ElectrobunTransport } = await import("../gateway/ipc-transport");
48
+ const ipcTransport = new ElectrobunTransport();
49
+ expect(ipcTransport.isConnected()).toBe(false);
50
+
51
+ ipcTransport.setBrowserView({ executeJavascript: vi.fn() });
52
+ expect(ipcTransport.isConnected()).toBe(true);
53
+
54
+ const { createWebServer } = await import("../shared/lib/web-server");
55
+ const webServer = await createWebServer({
56
+ port: 0,
57
+ authToken: "test-token",
58
+ maxUploadSize: 1024,
59
+ corsOrigin: "*",
60
+ });
61
+ expect(webServer.port).toBeGreaterThan(0);
62
+ expect(ipcTransport.isConnected()).toBe(true);
63
+ await webServer.close();
64
+ });
65
+
66
+ it("WS 客户端需要 token 才能连接", async () => {
67
+ const { createWebServer } = await import("../shared/lib/web-server");
68
+ const webServer = await createWebServer({
69
+ port: 0,
70
+ authToken: "test-token",
71
+ maxUploadSize: 1024,
72
+ corsOrigin: "*",
73
+ });
74
+ expect(webServer.authToken).toBeTruthy();
75
+ expect(webServer.authToken.length).toBeGreaterThan(0);
76
+ await webServer.close();
77
+ });
78
+
79
+ it("IPC 客户端不需要 token", async () => {
80
+ const { ElectrobunTransport } = await import("../gateway/ipc-transport");
81
+ const transport = new ElectrobunTransport();
82
+ transport.setBrowserView({ executeJavascript: vi.fn() });
83
+ expect(transport.isConnected()).toBe(true);
84
+ });
85
+
86
+ it("动态生成的 token 每次启动不同", async () => {
87
+ const { createWebServer } = await import("../shared/lib/web-server");
88
+ const ws1 = await createWebServer({
89
+ port: 0,
90
+ authToken: `token-${Date.now()}-1`,
91
+ maxUploadSize: 1024,
92
+ corsOrigin: "*",
93
+ });
94
+ const ws2 = await createWebServer({
95
+ port: 0,
96
+ authToken: `token-${Date.now()}-2`,
97
+ maxUploadSize: 1024,
98
+ corsOrigin: "*",
99
+ });
100
+ expect(ws1.authToken).not.toBe(ws2.authToken);
101
+ await ws1.close();
102
+ await ws2.close();
103
+ });
104
+
105
+ it("关闭桌面端时 Web 服务也关闭", async () => {
106
+ const { createWebServer } = await import("../shared/lib/web-server");
107
+ const webServer = await createWebServer({
108
+ port: 0,
109
+ authToken: "test-token",
110
+ maxUploadSize: 1024,
111
+ corsOrigin: "*",
112
+ });
113
+ expect(webServer.httpServer.listening).toBe(true);
114
+ await webServer.close();
115
+ await new Promise((r) => setTimeout(r, 50));
116
+ expect(webServer.httpServer.listening).toBe(false);
117
+ });
118
+
119
+ it("getLocalIP 返回有效 IP 地址", async () => {
120
+ const { getLocalIP } = await import("../shared/lib/web-server");
121
+ const ip = getLocalIP();
122
+ expect(ip).toBeTruthy();
123
+ expect(ip).toMatch(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
124
+ });
125
+ });
@@ -0,0 +1,55 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+
3
+ describe("Server Config 安全策略", () => {
4
+ const originalEnv = process.env;
5
+
6
+ beforeEach(() => {
7
+ vi.resetModules();
8
+ process.env = { ...originalEnv };
9
+ });
10
+
11
+ afterEach(() => {
12
+ process.env = originalEnv;
13
+ });
14
+
15
+ it("生产环境未设 AUTH_TOKEN 应抛出致命错误", async () => {
16
+ process.env.NODE_ENV = "production";
17
+ delete process.env.AUTH_TOKEN;
18
+ await expect(import("../server-config")).rejects.toThrow(/AUTH_TOKEN/);
19
+ });
20
+
21
+ it("开发环境未设 AUTH_TOKEN 应自动生成随机 token", async () => {
22
+ process.env.NODE_ENV = "development";
23
+ delete process.env.AUTH_TOKEN;
24
+ const mod = await import("../server-config");
25
+ expect(mod.config.authToken).toBeTruthy();
26
+ expect(mod.config.authToken).not.toBe("pi-agent-template-token");
27
+ expect(mod.config.authToken.length).toBeGreaterThan(20);
28
+ });
29
+
30
+ it("显式设置 AUTH_TOKEN 应使用用户提供的值", async () => {
31
+ delete process.env.NODE_ENV;
32
+ process.env.AUTH_TOKEN = "my-custom-token-12345";
33
+ const mod = await import("../server-config");
34
+ expect(mod.config.authToken).toBe("my-custom-token-12345");
35
+ });
36
+
37
+ it("自动生成的 token 每次不同", async () => {
38
+ process.env.NODE_ENV = "development";
39
+ delete process.env.AUTH_TOKEN;
40
+ vi.resetModules();
41
+ const mod1 = await import("../server-config");
42
+ const token1 = mod1.config.authToken;
43
+ vi.resetModules();
44
+ const mod2 = await import("../server-config");
45
+ const token2 = mod2.config.authToken;
46
+ expect(token1).not.toBe(token2);
47
+ });
48
+
49
+ it("生产环境设了 AUTH_TOKEN 不应抛错", async () => {
50
+ process.env.NODE_ENV = "production";
51
+ process.env.AUTH_TOKEN = "production-secure-token";
52
+ const mod = await import("../server-config");
53
+ expect(mod.config.authToken).toBe("production-secure-token");
54
+ });
55
+ });
@@ -2,58 +2,60 @@ import { describe, it, expect } from "vitest";
2
2
  import { parseEnvInt } from "../server-config";
3
3
 
4
4
  describe("parseEnvInt", () => {
5
- it("should return default when value is undefined", () => {
6
- expect(parseEnvInt("TEST_KEY", undefined, 3100, 1024, 65535)).toBe(3100);
7
- });
5
+ it("should return default when value is undefined", () => {
6
+ expect(parseEnvInt("TEST_KEY", undefined, 3100, 1024, 65535)).toBe(3100);
7
+ });
8
8
 
9
- it("should return default when value is empty string", () => {
10
- expect(parseEnvInt("TEST_KEY", "", 3100, 1024, 65535)).toBe(3100);
11
- });
9
+ it("should return default when value is empty string", () => {
10
+ expect(parseEnvInt("TEST_KEY", "", 3100, 1024, 65535)).toBe(3100);
11
+ });
12
12
 
13
- it("should parse valid number", () => {
14
- expect(parseEnvInt("TEST_KEY", "8080", 3100, 1024, 65535)).toBe(8080);
15
- });
13
+ it("should parse valid number", () => {
14
+ expect(parseEnvInt("TEST_KEY", "8080", 3100, 1024, 65535)).toBe(8080);
15
+ });
16
16
 
17
- it("should return default for NaN input", () => {
18
- expect(parseEnvInt("TEST_KEY", "abc", 3100, 1024, 65535)).toBe(3100);
19
- });
17
+ it("should return default for NaN input", () => {
18
+ expect(parseEnvInt("TEST_KEY", "abc", 3100, 1024, 65535)).toBe(3100);
19
+ });
20
20
 
21
- it("should return default for out-of-range value (too low)", () => {
22
- expect(parseEnvInt("TEST_KEY", "80", 3100, 1024, 65535)).toBe(3100);
23
- });
21
+ it("should return default for out-of-range value (too low)", () => {
22
+ expect(parseEnvInt("TEST_KEY", "80", 3100, 1024, 65535)).toBe(3100);
23
+ });
24
24
 
25
- it("should return default for out-of-range value (too high)", () => {
26
- expect(parseEnvInt("TEST_KEY", "99999", 3100, 1024, 65535)).toBe(3100);
27
- });
25
+ it("should return default for out-of-range value (too high)", () => {
26
+ expect(parseEnvInt("TEST_KEY", "99999", 3100, 1024, 65535)).toBe(3100);
27
+ });
28
28
 
29
- it("should accept boundary min value", () => {
30
- expect(parseEnvInt("TEST_KEY", "1024", 3100, 1024, 65535)).toBe(1024);
31
- });
29
+ it("should accept boundary min value", () => {
30
+ expect(parseEnvInt("TEST_KEY", "1024", 3100, 1024, 65535)).toBe(1024);
31
+ });
32
32
 
33
- it("should accept boundary max value", () => {
34
- expect(parseEnvInt("TEST_KEY", "65535", 3100, 1024, 65535)).toBe(65535);
35
- });
33
+ it("should accept boundary max value", () => {
34
+ expect(parseEnvInt("TEST_KEY", "65535", 3100, 1024, 65535)).toBe(65535);
35
+ });
36
36
  });
37
37
 
38
38
  describe("server-config exports", () => {
39
- it("should export config with all required fields", async () => {
40
- const { config } = await import("../server-config");
41
- expect(config).toHaveProperty("port");
42
- expect(config).toHaveProperty("authToken");
43
- expect(config).toHaveProperty("maxUploadSize");
44
- expect(config).toHaveProperty("logDir");
45
- expect(config).toHaveProperty("corsOrigin");
46
- expect(typeof config.port).toBe("number");
47
- expect(typeof config.authToken).toBe("string");
48
- expect(typeof config.maxUploadSize).toBe("number");
49
- expect(typeof config.corsOrigin).toBe("string");
50
- });
51
-
52
- it("should have sensible defaults", async () => {
53
- const { config } = await import("../server-config");
54
- expect(config.port).toBeGreaterThanOrEqual(1024);
55
- expect(config.port).toBeLessThanOrEqual(65535);
56
- expect(config.maxUploadSize).toBeGreaterThan(0);
57
- expect(config.corsOrigin).toBeTruthy();
58
- });
39
+ it("should export config with all required fields", async () => {
40
+ const { config } = await import("../server-config");
41
+ expect(config).toHaveProperty("port");
42
+ expect(config).toHaveProperty("authToken");
43
+ expect(config).toHaveProperty("maxUploadSize");
44
+ expect(config).toHaveProperty("logDir");
45
+ expect(config).toHaveProperty("enableWebService");
46
+ expect(config).toHaveProperty("corsOrigin");
47
+ expect(typeof config.port).toBe("number");
48
+ expect(typeof config.authToken).toBe("string");
49
+ expect(typeof config.maxUploadSize).toBe("number");
50
+ expect(typeof config.enableWebService).toBe("boolean");
51
+ expect(typeof config.corsOrigin).toBe("string");
52
+ });
53
+
54
+ it("should have sensible defaults", async () => {
55
+ const { config } = await import("../server-config");
56
+ expect(config.port).toBeGreaterThanOrEqual(1024);
57
+ expect(config.port).toBeLessThanOrEqual(65535);
58
+ expect(config.maxUploadSize).toBeGreaterThan(0);
59
+ expect(config.corsOrigin).toBeTruthy();
60
+ });
59
61
  });