@elizaos/autonomous 2.0.0-alpha.10

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 (241) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +270 -0
  3. package/src/actions/emote.ts +101 -0
  4. package/src/actions/restart.ts +101 -0
  5. package/src/actions/send-message.ts +168 -0
  6. package/src/actions/stream-control.ts +439 -0
  7. package/src/actions/switch-stream-source.ts +126 -0
  8. package/src/actions/terminal.ts +186 -0
  9. package/src/api/agent-admin-routes.ts +178 -0
  10. package/src/api/agent-lifecycle-routes.ts +129 -0
  11. package/src/api/agent-model.ts +143 -0
  12. package/src/api/agent-transfer-routes.ts +211 -0
  13. package/src/api/apps-routes.ts +210 -0
  14. package/src/api/auth-routes.ts +90 -0
  15. package/src/api/bsc-trade.ts +736 -0
  16. package/src/api/bug-report-routes.ts +161 -0
  17. package/src/api/character-routes.ts +421 -0
  18. package/src/api/cloud-billing-routes.ts +598 -0
  19. package/src/api/cloud-compat-routes.ts +192 -0
  20. package/src/api/cloud-routes.ts +529 -0
  21. package/src/api/cloud-status-routes.ts +234 -0
  22. package/src/api/compat-utils.ts +154 -0
  23. package/src/api/connector-health.ts +135 -0
  24. package/src/api/coordinator-wiring.ts +179 -0
  25. package/src/api/credit-detection.ts +47 -0
  26. package/src/api/database.ts +1357 -0
  27. package/src/api/diagnostics-routes.ts +389 -0
  28. package/src/api/drop-service.ts +205 -0
  29. package/src/api/early-logs.ts +111 -0
  30. package/src/api/http-helpers.ts +252 -0
  31. package/src/api/index.ts +85 -0
  32. package/src/api/knowledge-routes.ts +1189 -0
  33. package/src/api/knowledge-service-loader.ts +92 -0
  34. package/src/api/memory-bounds.ts +121 -0
  35. package/src/api/memory-routes.ts +349 -0
  36. package/src/api/merkle-tree.ts +239 -0
  37. package/src/api/models-routes.ts +72 -0
  38. package/src/api/nfa-routes.ts +169 -0
  39. package/src/api/nft-verify.ts +188 -0
  40. package/src/api/og-tracker.ts +72 -0
  41. package/src/api/parse-action-block.ts +145 -0
  42. package/src/api/permissions-routes.ts +222 -0
  43. package/src/api/plugin-validation.ts +355 -0
  44. package/src/api/provider-switch-config.ts +455 -0
  45. package/src/api/registry-routes.ts +165 -0
  46. package/src/api/registry-service.ts +292 -0
  47. package/src/api/route-helpers.ts +21 -0
  48. package/src/api/sandbox-routes.ts +1480 -0
  49. package/src/api/server.ts +17674 -0
  50. package/src/api/signal-routes.ts +265 -0
  51. package/src/api/stream-persistence.ts +297 -0
  52. package/src/api/stream-route-state.ts +48 -0
  53. package/src/api/stream-routes.ts +1046 -0
  54. package/src/api/stream-voice-routes.ts +208 -0
  55. package/src/api/streaming-text.ts +129 -0
  56. package/src/api/streaming-types.ts +23 -0
  57. package/src/api/subscription-routes.ts +283 -0
  58. package/src/api/terminal-run-limits.ts +31 -0
  59. package/src/api/training-backend-check.ts +40 -0
  60. package/src/api/training-routes.ts +314 -0
  61. package/src/api/training-service-like.ts +46 -0
  62. package/src/api/trajectory-routes.ts +714 -0
  63. package/src/api/trigger-routes.ts +438 -0
  64. package/src/api/twitter-verify.ts +226 -0
  65. package/src/api/tx-service.ts +193 -0
  66. package/src/api/wallet-dex-prices.ts +206 -0
  67. package/src/api/wallet-evm-balance.ts +989 -0
  68. package/src/api/wallet-routes.ts +505 -0
  69. package/src/api/wallet-rpc.ts +523 -0
  70. package/src/api/wallet-trading-profile.ts +694 -0
  71. package/src/api/wallet.ts +745 -0
  72. package/src/api/whatsapp-routes.ts +282 -0
  73. package/src/api/zip-utils.ts +130 -0
  74. package/src/auth/anthropic.ts +63 -0
  75. package/src/auth/apply-stealth.ts +38 -0
  76. package/src/auth/claude-code-stealth.ts +141 -0
  77. package/src/auth/credentials.ts +226 -0
  78. package/src/auth/index.ts +18 -0
  79. package/src/auth/openai-codex.ts +94 -0
  80. package/src/auth/types.ts +24 -0
  81. package/src/awareness/registry.ts +220 -0
  82. package/src/bin.ts +10 -0
  83. package/src/cli/index.ts +36 -0
  84. package/src/cli/parse-duration.ts +43 -0
  85. package/src/cloud/auth.test.ts +370 -0
  86. package/src/cloud/auth.ts +176 -0
  87. package/src/cloud/backup.test.ts +150 -0
  88. package/src/cloud/backup.ts +50 -0
  89. package/src/cloud/base-url.ts +45 -0
  90. package/src/cloud/bridge-client.test.ts +481 -0
  91. package/src/cloud/bridge-client.ts +307 -0
  92. package/src/cloud/cloud-manager.test.ts +223 -0
  93. package/src/cloud/cloud-manager.ts +151 -0
  94. package/src/cloud/cloud-proxy.test.ts +122 -0
  95. package/src/cloud/cloud-proxy.ts +52 -0
  96. package/src/cloud/index.ts +23 -0
  97. package/src/cloud/reconnect.test.ts +178 -0
  98. package/src/cloud/reconnect.ts +108 -0
  99. package/src/cloud/validate-url.test.ts +147 -0
  100. package/src/cloud/validate-url.ts +176 -0
  101. package/src/config/character-schema.ts +44 -0
  102. package/src/config/config.ts +149 -0
  103. package/src/config/env-vars.ts +86 -0
  104. package/src/config/includes.ts +196 -0
  105. package/src/config/index.ts +15 -0
  106. package/src/config/object-utils.ts +10 -0
  107. package/src/config/paths.ts +92 -0
  108. package/src/config/plugin-auto-enable.ts +520 -0
  109. package/src/config/schema.ts +1342 -0
  110. package/src/config/telegram-custom-commands.ts +99 -0
  111. package/src/config/types.agent-defaults.ts +342 -0
  112. package/src/config/types.agents.ts +112 -0
  113. package/src/config/types.gateway.ts +243 -0
  114. package/src/config/types.hooks.ts +124 -0
  115. package/src/config/types.messages.ts +201 -0
  116. package/src/config/types.milady.ts +791 -0
  117. package/src/config/types.tools.ts +416 -0
  118. package/src/config/types.ts +7 -0
  119. package/src/config/zod-schema.agent-runtime.ts +777 -0
  120. package/src/config/zod-schema.core.ts +778 -0
  121. package/src/config/zod-schema.hooks.ts +139 -0
  122. package/src/config/zod-schema.providers-core.ts +1126 -0
  123. package/src/config/zod-schema.session.ts +98 -0
  124. package/src/config/zod-schema.ts +865 -0
  125. package/src/contracts/apps.ts +46 -0
  126. package/src/contracts/awareness.ts +56 -0
  127. package/src/contracts/config.ts +172 -0
  128. package/src/contracts/drop.ts +21 -0
  129. package/src/contracts/index.ts +8 -0
  130. package/src/contracts/onboarding.ts +592 -0
  131. package/src/contracts/permissions.ts +52 -0
  132. package/src/contracts/verification.ts +9 -0
  133. package/src/contracts/wallet.ts +503 -0
  134. package/src/diagnostics/integration-observability.ts +132 -0
  135. package/src/emotes/catalog.ts +655 -0
  136. package/src/external-modules.d.ts +7 -0
  137. package/src/hooks/discovery.test.ts +357 -0
  138. package/src/hooks/discovery.ts +231 -0
  139. package/src/hooks/eligibility.ts +146 -0
  140. package/src/hooks/hooks.test.ts +320 -0
  141. package/src/hooks/index.ts +8 -0
  142. package/src/hooks/loader.test.ts +418 -0
  143. package/src/hooks/loader.ts +256 -0
  144. package/src/hooks/registry.test.ts +168 -0
  145. package/src/hooks/registry.ts +74 -0
  146. package/src/hooks/types.ts +121 -0
  147. package/src/index.ts +19 -0
  148. package/src/onboarding-presets.ts +828 -0
  149. package/src/plugins/custom-rtmp/index.ts +40 -0
  150. package/src/providers/admin-trust.ts +76 -0
  151. package/src/providers/session-bridge.ts +143 -0
  152. package/src/providers/session-utils.ts +42 -0
  153. package/src/providers/simple-mode.ts +113 -0
  154. package/src/providers/ui-catalog.ts +135 -0
  155. package/src/providers/workspace-provider.ts +213 -0
  156. package/src/providers/workspace.ts +497 -0
  157. package/src/runtime/agent-event-service.ts +57 -0
  158. package/src/runtime/cloud-onboarding.test.ts +489 -0
  159. package/src/runtime/cloud-onboarding.ts +408 -0
  160. package/src/runtime/core-plugins.ts +53 -0
  161. package/src/runtime/custom-actions.ts +605 -0
  162. package/src/runtime/eliza.ts +4941 -0
  163. package/src/runtime/embedding-presets.ts +73 -0
  164. package/src/runtime/index.ts +8 -0
  165. package/src/runtime/milady-plugin.ts +180 -0
  166. package/src/runtime/onboarding-names.ts +76 -0
  167. package/src/runtime/release-plugin-policy.ts +119 -0
  168. package/src/runtime/restart.ts +59 -0
  169. package/src/runtime/trajectory-persistence.ts +2584 -0
  170. package/src/runtime/version.ts +6 -0
  171. package/src/security/audit-log.ts +222 -0
  172. package/src/security/network-policy.ts +91 -0
  173. package/src/server/index.ts +6 -0
  174. package/src/services/agent-export.ts +976 -0
  175. package/src/services/app-manager.ts +755 -0
  176. package/src/services/browser-capture.ts +215 -0
  177. package/src/services/coding-agent-context.ts +355 -0
  178. package/src/services/fallback-training-service.ts +196 -0
  179. package/src/services/index.ts +17 -0
  180. package/src/services/mcp-marketplace.ts +327 -0
  181. package/src/services/plugin-manager-types.ts +185 -0
  182. package/src/services/privy-wallets.ts +352 -0
  183. package/src/services/registry-client-app-meta.ts +201 -0
  184. package/src/services/registry-client-endpoints.ts +253 -0
  185. package/src/services/registry-client-local.ts +485 -0
  186. package/src/services/registry-client-network.ts +173 -0
  187. package/src/services/registry-client-queries.ts +176 -0
  188. package/src/services/registry-client-types.ts +104 -0
  189. package/src/services/registry-client.ts +366 -0
  190. package/src/services/remote-signing-service.ts +261 -0
  191. package/src/services/sandbox-engine.ts +753 -0
  192. package/src/services/sandbox-manager.ts +503 -0
  193. package/src/services/self-updater.ts +213 -0
  194. package/src/services/signal-pairing.ts +189 -0
  195. package/src/services/signing-policy.ts +230 -0
  196. package/src/services/skill-catalog-client.ts +195 -0
  197. package/src/services/skill-marketplace.ts +909 -0
  198. package/src/services/stream-manager.ts +707 -0
  199. package/src/services/tts-stream-bridge.ts +465 -0
  200. package/src/services/update-checker.ts +163 -0
  201. package/src/services/version-compat.ts +367 -0
  202. package/src/services/whatsapp-pairing.ts +279 -0
  203. package/src/shared/ui-catalog-prompt.ts +1158 -0
  204. package/src/test-support/process-helpers.ts +35 -0
  205. package/src/test-support/route-test-helpers.ts +113 -0
  206. package/src/test-support/test-helpers.ts +304 -0
  207. package/src/testing/index.ts +3 -0
  208. package/src/triggers/action.ts +342 -0
  209. package/src/triggers/runtime.ts +432 -0
  210. package/src/triggers/scheduling.ts +472 -0
  211. package/src/triggers/types.ts +133 -0
  212. package/src/types/app-hyperscape-routes-shim.d.ts +29 -0
  213. package/src/types/external-modules.d.ts +7 -0
  214. package/src/utils/exec-safety.ts +23 -0
  215. package/src/utils/number-parsing.ts +112 -0
  216. package/src/utils/spoken-text.ts +65 -0
  217. package/src/version-resolver.ts +60 -0
  218. package/test/api/agent-admin-routes.test.ts +160 -0
  219. package/test/api/agent-lifecycle-routes.test.ts +164 -0
  220. package/test/api/agent-transfer-routes.test.ts +136 -0
  221. package/test/api/apps-routes.test.ts +140 -0
  222. package/test/api/auth-routes.test.ts +160 -0
  223. package/test/api/bug-report-routes.test.ts +88 -0
  224. package/test/api/knowledge-routes.test.ts +73 -0
  225. package/test/api/lifecycle.test.ts +342 -0
  226. package/test/api/memory-routes.test.ts +74 -0
  227. package/test/api/models-routes.test.ts +112 -0
  228. package/test/api/nfa-routes.test.ts +78 -0
  229. package/test/api/permissions-routes.test.ts +185 -0
  230. package/test/api/registry-routes.test.ts +157 -0
  231. package/test/api/signal-routes.test.ts +113 -0
  232. package/test/api/subscription-routes.test.ts +90 -0
  233. package/test/api/trigger-routes.test.ts +87 -0
  234. package/test/api/wallet-routes.observability.test.ts +191 -0
  235. package/test/api/wallet-routes.test.ts +502 -0
  236. package/test/diagnostics/integration-observability.test.ts +135 -0
  237. package/test/security/audit-log.test.ts +229 -0
  238. package/test/security/network-policy.test.ts +143 -0
  239. package/test/services/version-compat.test.ts +127 -0
  240. package/tsconfig.build.json +21 -0
  241. package/tsconfig.json +19 -0
@@ -0,0 +1,140 @@
1
+ import { describe, test, expect, vi } from "vitest";
2
+ import {
3
+ createMockIncomingMessage,
4
+ createMockHttpResponse,
5
+ } from "../../src/test-support/test-helpers";
6
+ import type {
7
+ AppsRouteContext,
8
+ AppManagerLike,
9
+ PluginManagerLike,
10
+ } from "../../src/api/apps-routes";
11
+ import { handleAppsRoutes } from "../../src/api/apps-routes";
12
+
13
+ function buildPluginManager(
14
+ overrides: Partial<PluginManagerLike> = {},
15
+ ): PluginManagerLike {
16
+ return {
17
+ listInstalledPlugins: vi.fn(async () => []),
18
+ getRegistryPlugin: vi.fn(async () => null),
19
+ refreshRegistry: vi.fn(async () => new Map()),
20
+ searchRegistry: vi.fn(async () => []),
21
+ installPlugin: vi.fn(async () => ({
22
+ success: true,
23
+ pluginName: "test",
24
+ version: "1.0.0",
25
+ installPath: "/tmp",
26
+ requiresRestart: false,
27
+ })),
28
+ uninstallPlugin: vi.fn(async () => ({
29
+ success: true,
30
+ pluginName: "test",
31
+ requiresRestart: false,
32
+ })),
33
+ listEjectedPlugins: vi.fn(async () => []),
34
+ ejectPlugin: vi.fn(async () => ({
35
+ success: true,
36
+ pluginName: "test",
37
+ ejectedPath: "/tmp",
38
+ requiresRestart: false,
39
+ })),
40
+ syncPlugin: vi.fn(async () => ({
41
+ success: true,
42
+ pluginName: "test",
43
+ ejectedPath: "/tmp",
44
+ requiresRestart: false,
45
+ })),
46
+ reinjectPlugin: vi.fn(async () => ({
47
+ success: true,
48
+ pluginName: "test",
49
+ removedPath: "/tmp",
50
+ requiresRestart: false,
51
+ })),
52
+ ...overrides,
53
+ };
54
+ }
55
+
56
+ function buildAppManager(
57
+ overrides: Partial<AppManagerLike> = {},
58
+ ): AppManagerLike {
59
+ return {
60
+ listAvailable: vi.fn(async () => []),
61
+ search: vi.fn(async () => []),
62
+ listInstalled: vi.fn(async () => []),
63
+ launch: vi.fn(async () => ({ success: true })),
64
+ stop: vi.fn(async () => ({ success: true })),
65
+ getInfo: vi.fn(async () => null),
66
+ ...overrides,
67
+ };
68
+ }
69
+
70
+ function buildCtx(
71
+ overrides: Partial<AppsRouteContext> = {},
72
+ ): AppsRouteContext {
73
+ const { res } = createMockHttpResponse();
74
+ return {
75
+ req: createMockIncomingMessage({ method: "GET", url: "/" }),
76
+ res,
77
+ method: "GET",
78
+ pathname: "/",
79
+ url: new URL("http://localhost:2138/"),
80
+ appManager: buildAppManager(),
81
+ getPluginManager: () => buildPluginManager(),
82
+ parseBoundedLimit: vi.fn((raw, fallback = 20) =>
83
+ raw ? Math.min(Math.max(1, Number(raw)), 100) : fallback,
84
+ ),
85
+ json: vi.fn((r, data, status = 200) => {
86
+ r.writeHead(status);
87
+ r.end(JSON.stringify(data));
88
+ }),
89
+ error: vi.fn((r, message, status = 500) => {
90
+ r.writeHead(status);
91
+ r.end(JSON.stringify({ error: message }));
92
+ }),
93
+ readJsonBody: vi.fn(async () => null),
94
+ runtime: null,
95
+ ...overrides,
96
+ };
97
+ }
98
+
99
+ describe("handleAppsRoutes", () => {
100
+ test("returns false for unrelated path", async () => {
101
+ const ctx = buildCtx({ pathname: "/api/other" });
102
+ const handled = await handleAppsRoutes(ctx);
103
+ expect(handled).toBe(false);
104
+ });
105
+
106
+ test("GET /api/apps returns available apps list", async () => {
107
+ const { res, getStatus, getJson } = createMockHttpResponse();
108
+ const apps = [{ name: "test-app", version: "1.0.0" }];
109
+ const ctx = buildCtx({
110
+ method: "GET",
111
+ pathname: "/api/apps",
112
+ res,
113
+ appManager: buildAppManager({
114
+ listAvailable: vi.fn(async () => apps),
115
+ }),
116
+ });
117
+
118
+ const handled = await handleAppsRoutes(ctx);
119
+
120
+ expect(handled).toBe(true);
121
+ expect(getStatus()).toBe(200);
122
+ expect(getJson()).toEqual(apps);
123
+ });
124
+
125
+ test("GET /api/apps/search returns empty array for blank query", async () => {
126
+ const { res, getStatus, getJson } = createMockHttpResponse();
127
+ const ctx = buildCtx({
128
+ method: "GET",
129
+ pathname: "/api/apps/search",
130
+ url: new URL("http://localhost:2138/api/apps/search?q="),
131
+ res,
132
+ });
133
+
134
+ const handled = await handleAppsRoutes(ctx);
135
+
136
+ expect(handled).toBe(true);
137
+ expect(getStatus()).toBe(200);
138
+ expect(getJson()).toEqual([]);
139
+ });
140
+ });
@@ -0,0 +1,160 @@
1
+ import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import {
3
+ createMockIncomingMessage,
4
+ createMockHttpResponse,
5
+ } from "../../src/test-support/test-helpers";
6
+ import { handleAuthRoutes } from "../../src/api/auth-routes";
7
+ import type { AuthRouteContext } from "../../src/api/auth-routes";
8
+
9
+ let envBackup: string | undefined;
10
+
11
+ beforeEach(() => {
12
+ envBackup = process.env.MILADY_API_TOKEN;
13
+ process.env.MILADY_API_TOKEN = "test-token-secret";
14
+ });
15
+
16
+ afterEach(() => {
17
+ if (envBackup === undefined) delete process.env.MILADY_API_TOKEN;
18
+ else process.env.MILADY_API_TOKEN = envBackup;
19
+ });
20
+
21
+ function buildCtx(
22
+ method: string,
23
+ pathname: string,
24
+ overrides?: Partial<AuthRouteContext>,
25
+ ): AuthRouteContext & { getStatus: () => number; getJson: () => unknown } {
26
+ const { res, getStatus, getJson } = createMockHttpResponse();
27
+ const req = createMockIncomingMessage({ method, url: pathname });
28
+ (req as any).socket = { remoteAddress: "127.0.0.1" };
29
+ const ctx = {
30
+ req,
31
+ res,
32
+ method,
33
+ pathname,
34
+ json: vi.fn((r, data, status = 200) => {
35
+ r.writeHead(status);
36
+ r.end(JSON.stringify(data));
37
+ }),
38
+ error: vi.fn((r, msg, status = 500) => {
39
+ r.writeHead(status);
40
+ r.end(JSON.stringify({ error: msg }));
41
+ }),
42
+ readJsonBody: vi.fn(async () => null),
43
+ pairingEnabled: () => true,
44
+ ensurePairingCode: () => "ABC123",
45
+ normalizePairingCode: (code: string) => code.toUpperCase().trim(),
46
+ rateLimitPairing: () => true,
47
+ getPairingExpiresAt: () => Date.now() + 60_000,
48
+ clearPairing: vi.fn(),
49
+ ...overrides,
50
+ } as AuthRouteContext & { getStatus: () => number; getJson: () => unknown };
51
+ (ctx as any).getStatus = getStatus;
52
+ (ctx as any).getJson = getJson;
53
+ return ctx;
54
+ }
55
+
56
+ describe("auth-routes", () => {
57
+ describe("GET /api/auth/status", () => {
58
+ test("returns pairing status", async () => {
59
+ const ctx = buildCtx("GET", "/api/auth/status");
60
+ const handled = await handleAuthRoutes(ctx);
61
+ expect(handled).toBe(true);
62
+ expect(ctx.json).toHaveBeenCalledOnce();
63
+ const payload = (ctx.json as ReturnType<typeof vi.fn>).mock.calls[0][1];
64
+ expect(payload).toHaveProperty("pairingEnabled", true);
65
+ expect(payload).toHaveProperty("required");
66
+ expect(payload).toHaveProperty("expiresAt");
67
+ });
68
+
69
+ test("returns pairingEnabled false when disabled", async () => {
70
+ const ctx = buildCtx("GET", "/api/auth/status", {
71
+ pairingEnabled: () => false,
72
+ });
73
+ await handleAuthRoutes(ctx);
74
+ const payload = (ctx.json as ReturnType<typeof vi.fn>).mock.calls[0][1];
75
+ expect(payload.pairingEnabled).toBe(false);
76
+ expect(payload.expiresAt).toBeNull();
77
+ });
78
+ });
79
+
80
+ describe("POST /api/auth/pair", () => {
81
+ test("succeeds with valid code and returns token", async () => {
82
+ const ctx = buildCtx("POST", "/api/auth/pair", {
83
+ readJsonBody: vi.fn(async () => ({ code: "ABC123" })),
84
+ });
85
+ const handled = await handleAuthRoutes(ctx);
86
+ expect(handled).toBe(true);
87
+ expect(ctx.json).toHaveBeenCalledOnce();
88
+ const payload = (ctx.json as ReturnType<typeof vi.fn>).mock.calls[0][1];
89
+ expect(payload).toHaveProperty("token");
90
+ expect(ctx.clearPairing).toHaveBeenCalled();
91
+ });
92
+
93
+ test("rejects when pairing disabled", async () => {
94
+ const ctx = buildCtx("POST", "/api/auth/pair", {
95
+ pairingEnabled: () => false,
96
+ readJsonBody: vi.fn(async () => ({ code: "ABC123" })),
97
+ rateLimitPairing: () => true,
98
+ });
99
+ await handleAuthRoutes(ctx);
100
+ expect(ctx.error).toHaveBeenCalled();
101
+ const args = (ctx.error as ReturnType<typeof vi.fn>).mock.calls[0];
102
+ expect(args[2]).toBe(403);
103
+ });
104
+
105
+ test("rejects when rate limited", async () => {
106
+ const ctx = buildCtx("POST", "/api/auth/pair", {
107
+ rateLimitPairing: () => false,
108
+ readJsonBody: vi.fn(async () => ({ code: "ABC123" })),
109
+ });
110
+ await handleAuthRoutes(ctx);
111
+ expect(ctx.error).toHaveBeenCalled();
112
+ const args = (ctx.error as ReturnType<typeof vi.fn>).mock.calls[0];
113
+ expect(args[2]).toBe(429);
114
+ });
115
+
116
+ test("rejects with wrong code", async () => {
117
+ const ctx = buildCtx("POST", "/api/auth/pair", {
118
+ readJsonBody: vi.fn(async () => ({ code: "WRONG" })),
119
+ });
120
+ await handleAuthRoutes(ctx);
121
+ expect(ctx.error).toHaveBeenCalled();
122
+ const args = (ctx.error as ReturnType<typeof vi.fn>).mock.calls[0];
123
+ expect(args[2]).toBe(403);
124
+ });
125
+
126
+ test("rejects with no API token set", async () => {
127
+ delete process.env.MILADY_API_TOKEN;
128
+ const ctx = buildCtx("POST", "/api/auth/pair", {
129
+ readJsonBody: vi.fn(async () => ({ code: "ABC123" })),
130
+ });
131
+ await handleAuthRoutes(ctx);
132
+ expect(ctx.error).toHaveBeenCalled();
133
+ const args = (ctx.error as ReturnType<typeof vi.fn>).mock.calls[0];
134
+ expect(args[2]).toBe(400);
135
+ });
136
+
137
+ test("rejects expired code", async () => {
138
+ const ctx = buildCtx("POST", "/api/auth/pair", {
139
+ getPairingExpiresAt: () => Date.now() - 1000,
140
+ readJsonBody: vi.fn(async () => ({ code: "ABC123" })),
141
+ });
142
+ await handleAuthRoutes(ctx);
143
+ expect(ctx.error).toHaveBeenCalled();
144
+ const args = (ctx.error as ReturnType<typeof vi.fn>).mock.calls[0];
145
+ expect(args[2]).toBe(410);
146
+ });
147
+ });
148
+
149
+ describe("routing", () => {
150
+ test("unrelated path returns false", async () => {
151
+ const ctx = buildCtx("GET", "/api/other");
152
+ expect(await handleAuthRoutes(ctx)).toBe(false);
153
+ });
154
+
155
+ test("/api/auth/ prefix but unknown sub-path returns false", async () => {
156
+ const ctx = buildCtx("GET", "/api/auth/unknown");
157
+ expect(await handleAuthRoutes(ctx)).toBe(false);
158
+ });
159
+ });
160
+ });
@@ -0,0 +1,88 @@
1
+ import { describe, test, expect, vi } from "vitest";
2
+ import {
3
+ createMockIncomingMessage,
4
+ createMockHttpResponse,
5
+ } from "../../src/test-support/test-helpers";
6
+ import {
7
+ handleBugReportRoutes,
8
+ rateLimitBugReport,
9
+ resetBugReportRateLimit,
10
+ sanitize,
11
+ } from "../../src/api/bug-report-routes";
12
+ import type { RouteRequestContext } from "../../src/api/route-helpers";
13
+
14
+ function buildCtx(
15
+ overrides: Partial<RouteRequestContext> = {},
16
+ ): RouteRequestContext {
17
+ const { res } = createMockHttpResponse();
18
+ return {
19
+ req: createMockIncomingMessage({ method: "GET", url: "/" }),
20
+ res,
21
+ method: "GET",
22
+ pathname: "/",
23
+ json: vi.fn((r, data, status = 200) => {
24
+ r.writeHead(status);
25
+ r.end(JSON.stringify(data));
26
+ }),
27
+ error: vi.fn((r, message, status = 500) => {
28
+ r.writeHead(status);
29
+ r.end(JSON.stringify({ error: message }));
30
+ }),
31
+ readJsonBody: vi.fn(async () => null),
32
+ ...overrides,
33
+ };
34
+ }
35
+
36
+ describe("handleBugReportRoutes", () => {
37
+ test("returns false for unrelated path", async () => {
38
+ const ctx = buildCtx({ pathname: "/api/other" });
39
+ const handled = await handleBugReportRoutes(ctx);
40
+ expect(handled).toBe(false);
41
+ });
42
+
43
+ test("GET /api/bug-report/info returns node version and platform", async () => {
44
+ const { res, getStatus, getJson } = createMockHttpResponse();
45
+ const ctx = buildCtx({
46
+ method: "GET",
47
+ pathname: "/api/bug-report/info",
48
+ res,
49
+ });
50
+
51
+ const handled = await handleBugReportRoutes(ctx);
52
+
53
+ expect(handled).toBe(true);
54
+ expect(getStatus()).toBe(200);
55
+ const json = getJson<{ nodeVersion: string; platform: string }>();
56
+ expect(json.nodeVersion).toBe(process.version);
57
+ expect(typeof json.platform).toBe("string");
58
+ });
59
+ });
60
+
61
+ describe("rateLimitBugReport", () => {
62
+ test("allows first submission", () => {
63
+ resetBugReportRateLimit();
64
+ expect(rateLimitBugReport("127.0.0.1")).toBe(true);
65
+ });
66
+
67
+ test("blocks after max submissions", () => {
68
+ resetBugReportRateLimit();
69
+ const ip = "10.0.0.1";
70
+ for (let i = 0; i < 5; i++) {
71
+ rateLimitBugReport(ip);
72
+ }
73
+ expect(rateLimitBugReport(ip)).toBe(false);
74
+ });
75
+ });
76
+
77
+ describe("sanitize", () => {
78
+ test("strips HTML tags", () => {
79
+ expect(sanitize("<script>alert('xss')</script>hello")).toBe(
80
+ "alert('xss')hello",
81
+ );
82
+ });
83
+
84
+ test("truncates to maxLen", () => {
85
+ const long = "a".repeat(200);
86
+ expect(sanitize(long, 50).length).toBe(50);
87
+ });
88
+ });
@@ -0,0 +1,73 @@
1
+ import { describe, test, expect, vi } from "vitest";
2
+ import {
3
+ createMockIncomingMessage,
4
+ createMockHttpResponse,
5
+ } from "../../src/test-support/test-helpers";
6
+ import { handleKnowledgeRoutes } from "../../src/api/knowledge-routes";
7
+ import type { KnowledgeRouteContext } from "../../src/api/knowledge-routes";
8
+
9
+ function buildCtx(
10
+ method: string,
11
+ pathname: string,
12
+ query = "",
13
+ overrides?: Partial<KnowledgeRouteContext>,
14
+ ): KnowledgeRouteContext {
15
+ const fullUrl = query ? `${pathname}?${query}` : pathname;
16
+ const { res } = createMockHttpResponse();
17
+ return {
18
+ req: createMockIncomingMessage({ method, url: fullUrl }),
19
+ res,
20
+ method,
21
+ pathname,
22
+ url: new URL(fullUrl, "http://localhost:2138"),
23
+ json: vi.fn((r, data, status = 200) => {
24
+ r.writeHead(status);
25
+ r.end(JSON.stringify(data));
26
+ }),
27
+ error: vi.fn((r, msg, status = 500) => {
28
+ r.writeHead(status);
29
+ r.end(JSON.stringify({ error: msg }));
30
+ }),
31
+ readJsonBody: vi.fn(async () => ({})),
32
+ runtime: null,
33
+ ...overrides,
34
+ } as KnowledgeRouteContext;
35
+ }
36
+
37
+ describe("knowledge-routes", () => {
38
+ describe("GET /api/knowledge", () => {
39
+ test("requires runtime", async () => {
40
+ const ctx = buildCtx("GET", "/api/knowledge");
41
+ const handled = await handleKnowledgeRoutes(ctx);
42
+ expect(handled).toBe(true);
43
+ expect(ctx.error).toHaveBeenCalled();
44
+ });
45
+ });
46
+
47
+ describe("GET /api/knowledge/search", () => {
48
+ test("requires runtime", async () => {
49
+ const ctx = buildCtx("GET", "/api/knowledge/search", "q=test");
50
+ const handled = await handleKnowledgeRoutes(ctx);
51
+ expect(handled).toBe(true);
52
+ expect(ctx.error).toHaveBeenCalled();
53
+ });
54
+ });
55
+
56
+ describe("POST /api/knowledge", () => {
57
+ test("requires runtime for upload", async () => {
58
+ const ctx = buildCtx("POST", "/api/knowledge", "", {
59
+ readJsonBody: vi.fn(async () => ({ title: "test", content: "data" })),
60
+ });
61
+ const handled = await handleKnowledgeRoutes(ctx);
62
+ expect(handled).toBe(true);
63
+ expect(ctx.error).toHaveBeenCalled();
64
+ });
65
+ });
66
+
67
+ describe("routing", () => {
68
+ test("unrelated path returns false", async () => {
69
+ const ctx = buildCtx("GET", "/api/other");
70
+ expect(await handleKnowledgeRoutes(ctx)).toBe(false);
71
+ });
72
+ });
73
+ });