shadow-claw 1.23.4 → 1.24.0
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 +42 -5
- package/bin/cli.mjs +166 -0
- package/bin/cli.test.mjs +60 -0
- package/bin/commands/backup.mjs +70 -0
- package/bin/commands/clients.mjs +41 -0
- package/bin/commands/peer-id.mjs +76 -0
- package/bin/commands/peer-id.test.mjs +104 -0
- package/bin/commands/send.mjs +51 -0
- package/bin/commands/tasks.mjs +59 -0
- package/bin/commands/webrtc-listen.mjs +254 -0
- package/bin/prerender-dsd-shell/prerender-dsd-shell.test.mjs +46 -0
- package/bin/rolldown.config.test.mjs +401 -8
- package/bin/utils/control-client.mjs +243 -0
- package/bin/utils/control-client.test.mjs +83 -0
- package/bin/utils/ulid.mjs +45 -0
- package/bin/utils/webrtc-control-client.mjs +699 -0
- package/bin/utils/webrtc-control-client.test.mjs +318 -0
- package/dist/electron/main.cjs +1 -1
- package/dist/public/AGENTS.md +5 -3
- package/dist/public/README.md +42 -5
- package/dist/public/agent.worker.js +1 -1
- package/dist/public/backup-controller-C_s9in2t.js +1 -0
- package/dist/public/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.css +1 -0
- package/dist/public/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.html +1 -0
- package/dist/public/components/settings/shadow-claw-task-server/shadow-claw-task-server.css +1 -1
- package/dist/public/components/settings/shadow-claw-task-server/shadow-claw-task-server.html +1 -1
- package/dist/public/components/shadow-claw-settings/shadow-claw-settings.html +1 -1
- package/dist/public/config-CrHchneq.js +1 -0
- package/dist/public/{configurePeerJs-B4Ym9VMj.js → configurePeerJs-CxpZeDJJ.js} +1 -1
- package/dist/public/{connections-BEn6JYdu.js → connections-DvbgdkaB.js} +1 -1
- package/dist/public/{custom-element-security-DdYlUozq.js → custom-element-security-NCqQ5lmC.js} +1 -1
- package/dist/public/docs/README.md +3 -1
- package/dist/public/docs/architecture/overview.md +12 -12
- package/dist/public/docs/decisions/bundled-typescript-architecture.md +1 -1
- package/dist/public/docs/decisions/indexeddb-and-opfs-storage.md +2 -2
- package/dist/public/docs/decisions/native-web-components-and-signals.md +1 -1
- package/dist/public/docs/example/article/index.html +1 -1
- package/dist/public/docs/guides/configuring-messaging-channels.md +41 -6
- package/dist/public/docs/guides/server-development-configuration.md +24 -0
- package/dist/public/docs/publishing/index.html +1 -1
- package/dist/public/docs/skill-creator/index.html +1 -1
- package/dist/public/docs/subsystems/backup.md +56 -0
- package/dist/public/docs/subsystems/cli.md +124 -0
- package/dist/public/docs/subsystems/control-plane.md +158 -0
- package/dist/public/docs/subsystems/notifications.md +54 -2
- package/dist/public/docs/subsystems/reactive-ui.md +1 -0
- package/dist/public/downloadGroupFile-By0GR6eV.js +1 -0
- package/dist/public/e2e/README.md +5 -1
- package/dist/public/{e2e-bridge-BQ4KzUt5.js → e2e-bridge-IGWsRl4i.js} +1 -1
- package/dist/public/file-viewer-D0eflgt6.js +1 -0
- package/dist/public/getAllTasks-vpBlgGsc.js +1 -0
- package/dist/public/getGroupDir-P1h9wl6S.js +1 -0
- package/dist/public/{git-DEUvQTUA.js → git-7Vq_SJSp.js} +2 -2
- package/dist/public/{iframe-sanitizer-0NS4XLOd.js → iframe-sanitizer-y2Z1pPwY.js} +1 -1
- package/dist/public/index.html +1 -1
- package/dist/public/index.js +1 -1
- package/dist/public/{initChatSplitResize-llGkSJXg.js → initChatSplitResize-CzV2E7Ul.js} +1 -1
- package/dist/public/initControlPlane-C9jpnWev.js +1 -0
- package/dist/public/llms.txt +1 -1
- package/dist/public/main/index.html +1 -1
- package/dist/public/main/memory/index.html +1 -1
- package/dist/public/{markdown-CyYR4App.js → markdown-BnpKxSxl.js} +1 -1
- package/dist/public/{mcp-reconnect-CSx4aMwy.js → mcp-reconnect-9emsqGRe.js} +1 -1
- package/dist/public/{memoryStorage-Dwg8NhAZ.js → memoryStorage-C0KvLNUp.js} +1 -1
- package/dist/public/{orchestrator-DRfEudXk.js → orchestrator-3h9xxHUm.js} +676 -676
- package/dist/public/orchestrator-nJj59RqD.js +15 -0
- package/dist/public/{peerjs-CurBuKvk.js → peerjs-BP4laMXj.js} +1 -1
- package/dist/public/{push-client-Dii-vPEh.js → push-client-CrjEXK3U.js} +1 -1
- package/dist/public/service-worker/push-handler.js +1 -1
- package/dist/public/service-worker.js +1 -1
- package/dist/public/{shadow-claw-5IHaDDMJ.js → shadow-claw-CbwETBWf.js} +2 -2
- package/dist/public/{shadow-claw-accounts-CZvz4iQz.js → shadow-claw-accounts-B4sBTQEj.js} +1 -1
- package/dist/public/{shadow-claw-channels-BKXFyRS3.js → shadow-claw-channels-BDWG5Qa3.js} +1 -1
- package/dist/public/{shadow-claw-chat-CCLuhWdI.js → shadow-claw-chat-Crv0DTCY.js} +6 -6
- package/dist/public/shadow-claw-control-plane-CX1IGbh9.js +199 -0
- package/dist/public/{shadow-claw-conversations-DV-87UUh.js → shadow-claw-conversations-_9qAaRXk.js} +1 -1
- package/dist/public/shadow-claw-dialog-BrOwAOdk.js +1 -0
- package/dist/public/{shadow-claw-file-viewer-Dk2dbAUd.js → shadow-claw-file-viewer-Blf6yGVD.js} +1 -1
- package/dist/public/shadow-claw-files-CYySBDCl.js +840 -0
- package/dist/public/{shadow-claw-git-DRA7_qck.js → shadow-claw-git-v8S0JjuR.js} +2 -2
- package/dist/public/{shadow-claw-integrations-1Xu5snuV.js → shadow-claw-integrations-D7ufpwhX.js} +1 -1
- package/dist/public/{shadow-claw-llm-6Aci1BDP.js → shadow-claw-llm-GWgoVfhI.js} +2 -2
- package/dist/public/{shadow-claw-mcp-remote-BUjAeVXC.js → shadow-claw-mcp-remote-kY4LMFNN.js} +1 -1
- package/dist/public/{shadow-claw-networking-FQe4nCp3.js → shadow-claw-networking-qHbP95z9.js} +1 -1
- package/dist/public/{shadow-claw-notifications-DV2wibL3.js → shadow-claw-notifications-YyHOK2Fr.js} +1 -1
- package/dist/public/{shadow-claw-page-header-B63S4HTU.js → shadow-claw-page-header-DMKbkv3s.js} +1 -1
- package/dist/public/{shadow-claw-pages-DE3jWj5K.js → shadow-claw-pages-DFZHhzFZ.js} +1 -1
- package/dist/public/{shadow-claw-settings-S6DAKfYx.js → shadow-claw-settings-DhxrKIfA.js} +10 -3
- package/dist/public/{shadow-claw-storage-DaacA3_K.js → shadow-claw-storage-BFynbP0j.js} +2 -2
- package/dist/public/{shadow-claw-task-server-IjKX042p.js → shadow-claw-task-server-3BIeb5oZ.js} +25 -4
- package/dist/public/{shadow-claw-tasks-Cy0JYmGU.js → shadow-claw-tasks-Caxc9atL.js} +1 -1
- package/dist/public/{shadow-claw-terminal-znAt7HyP.js → shadow-claw-terminal-Beb5qF-U.js} +1 -1
- package/dist/public/{shadow-claw-tools-Cutv9Si5.js → shadow-claw-tools-BfJx3A9W.js} +1 -1
- package/dist/public/{shadow-claw-webvm-BH8mPDEu.js → shadow-claw-webvm-BCS8ykeP.js} +1 -1
- package/dist/public/{syncWebMcpRegistration-BqJxaMGR.js → syncWebMcpRegistration-B_M2fCjH.js} +1 -1
- package/dist/server.js +286 -189
- package/package.json +2 -1
- package/src/__mocks__/jszip.cjs +15 -5
- package/src/components/settings/shadow-claw-accounts/shadow-claw-accounts.test.ts +35 -0
- package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.css +117 -0
- package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.html +81 -0
- package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.test.ts +127 -0
- package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.ts +292 -0
- package/src/components/settings/shadow-claw-git/shadow-claw-git.test.ts +72 -0
- package/src/components/settings/shadow-claw-integrations/connection-test-auth.test.ts +20 -30
- package/src/components/settings/shadow-claw-integrations/shadow-claw-integrations.test.ts +116 -0
- package/src/components/settings/shadow-claw-llm/model-ranking.test.ts +143 -51
- package/src/components/settings/shadow-claw-llm/shadow-claw-llm.test.ts +307 -4
- package/src/components/settings/shadow-claw-llm/shadow-claw-llm.ts +8 -8
- package/src/components/settings/shadow-claw-mcp-remote/shadow-claw-mcp-remote.test.ts +31 -0
- package/src/components/settings/shadow-claw-peerjs/shadow-claw-peerjs.test.ts +186 -0
- package/src/components/settings/shadow-claw-storage/shadow-claw-storage.test.ts +76 -0
- package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.css +23 -2
- package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.html +1 -1
- package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.test.ts +19 -0
- package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.ts +2 -1
- package/src/components/shadow-claw/utils/initChatSplitResize.test.ts +180 -13
- package/src/components/shadow-claw/utils/setupEffects.test.ts +39 -0
- package/src/components/shadow-claw-a2ui/catalog/basic/shared.test.ts +117 -0
- package/src/components/shadow-claw-a2ui/shadow-claw-a2ui.test.ts +1159 -1
- package/src/components/shadow-claw-chat/shadow-claw-chat.test.ts +1523 -19
- package/src/components/shadow-claw-chat/utils/escapeHtml.test.ts +13 -0
- package/src/components/shadow-claw-chat/utils/getPeerChatDisplayStatus.test.ts +20 -0
- package/src/components/shadow-claw-conversations/shadow-claw-conversations.test.ts +494 -0
- package/src/components/shadow-claw-dialog/shadow-claw-dialog.ts +13 -2
- package/src/components/shadow-claw-file-viewer/shadow-claw-file-viewer.test.ts +273 -0
- package/src/components/shadow-claw-files/shadow-claw-files.test.ts +408 -1
- package/src/components/shadow-claw-pdf-viewer/utils/installGetOrInsertComputedPolyfill.test.ts +65 -0
- package/src/components/shadow-claw-settings/shadow-claw-settings.html +7 -0
- package/src/components/shadow-claw-settings/shadow-claw-settings.test.ts +221 -0
- package/src/components/shadow-claw-settings/shadow-claw-settings.ts +2 -0
- package/src/components/shadow-claw-tasks/shadow-claw-tasks.test.ts +407 -0
- package/src/components/shadow-claw-terminal/utils/consumeEscapeSequence.test.ts +58 -0
- package/src/components/shadow-claw-tools/shadow-claw-tools.test.ts +80 -0
- package/src/config/config.ts +10 -0
- package/src/core/backup-controller.test.ts +101 -0
- package/src/core/backup-controller.ts +137 -0
- package/src/core/control-plane-client.test.ts +408 -0
- package/src/core/control-plane-client.ts +425 -0
- package/src/core/orchestrator/orchestrator.ts +4 -0
- package/src/core/orchestrator/utils/compactContext.test.ts +78 -0
- package/src/core/orchestrator/utils/operations/channel.test.ts +254 -9
- package/src/core/orchestrator/utils/operations/provider.test.ts +326 -34
- package/src/core/orchestrator/utils/operations/vm.test.ts +114 -2
- package/src/core/orchestrator/utils/syncProxyConfigToServiceWorker.test.ts +60 -0
- package/src/core/utils/initControlPlane.test.ts +276 -0
- package/src/core/utils/initControlPlane.ts +437 -0
- package/src/core/utils/initializeApp.ts +18 -0
- package/src/db/getSession.test.ts +13 -1
- package/src/server/app.test.ts +12 -0
- package/src/server/app.ts +18 -8
- package/src/server/client-registry.test.ts +250 -0
- package/src/server/client-registry.ts +477 -0
- package/src/server/config.ts +15 -0
- package/src/server/control-plane-types.ts +120 -0
- package/src/server/control-plane.test.ts +472 -0
- package/src/server/control-plane.ts +581 -0
- package/src/server/routes/backup.test.ts +263 -0
- package/src/server/routes/backup.ts +262 -0
- package/src/server/routes/integrations-email.test.ts +532 -0
- package/src/server/routes/llamafile.test.ts +172 -48
- package/src/server/routes/mesh-llm.test.ts +192 -0
- package/src/server/routes/ollama.test.ts +49 -0
- package/src/server/server-peer.test.ts +86 -0
- package/src/server/server-peer.ts +330 -0
- package/src/server/server.ts +35 -0
- package/src/server/services/llamafile-manager.test.ts +230 -6
- package/src/server/services/transformers-runtime.test.ts +186 -18
- package/src/server/webrtc.test.ts +21 -0
- package/src/server/webrtc.ts +72 -0
- package/src/service-worker/fetch-proxy.test.ts +64 -1
- package/src/service-worker/push-handler.ts +39 -0
- package/src/shell/vm.test.ts +744 -12
- package/src/storage/ensureMainGroupIndex.test.ts +153 -0
- package/src/storage/getGroupFile.test.ts +66 -0
- package/src/storage/memoryStorage.test.ts +82 -2
- package/src/storage/writeGroupFileBytes.test.ts +143 -0
- package/src/stores/chat-ui.test.ts +20 -0
- package/src/stores/orchestrator.test.ts +134 -0
- package/src/stores/toast.test.ts +50 -0
- package/src/stores/tools.test.ts +147 -10
- package/src/subsystems/channels/imessage.test.ts +59 -0
- package/src/subsystems/channels/peerjs.test.ts +500 -62
- package/src/subsystems/channels/peerjs.ts +71 -4
- package/src/subsystems/channels/telegram.test.ts +177 -0
- package/src/subsystems/email/catalog.test.ts +1 -0
- package/src/subsystems/email/connections.test.ts +39 -1
- package/src/subsystems/git/credentials.test.ts +160 -825
- package/src/subsystems/git/git.test.ts +463 -0
- package/src/subsystems/notifications/__mocks__/node-sqlite.cjs +12 -1
- package/src/subsystems/notifications/push-routes.test.ts +33 -0
- package/src/subsystems/notifications/push-routes.ts +23 -0
- package/src/subsystems/notifications/push-store.ts +1 -1
- package/src/subsystems/providers/builtin-ai-tasks.test.ts +74 -0
- package/src/subsystems/providers/litert-lm-provider.test.ts +212 -51
- package/src/subsystems/providers/model-registry.test.ts +115 -0
- package/src/subsystems/providers/providers.test.ts +94 -1
- package/src/subsystems/providers/transformers-js-provider.test.ts +279 -0
- package/src/subsystems/providers/utils/createModelCacheFetch.test.ts +2 -2
- package/src/subsystems/providers/utils/downloadModelToCache.test.ts +2 -2
- package/src/subsystems/providers/utils/loadModelStream.test.ts +2 -2
- package/src/subsystems/skills/activateSkill.test.ts +101 -0
- package/src/subsystems/skills/discoverSkills.test.ts +251 -0
- package/src/subsystems/tools/cron.test.ts +64 -0
- package/src/subsystems/tools/declarative.test.ts +255 -89
- package/src/testing/assets/v86.9pfs/libv86.mjs +63 -0
- package/src/testing/assets/v86.ext2/libv86.mjs +3 -0
- package/src/testing/assets_missing_v86/assets/v86.9pfs/libv86.mjs +1 -0
- package/src/testing/jest-setup.ts +328 -40
- package/src/testing/mock-v86.mjs +49 -0
- package/src/ui/a2ui/types.ts +4 -3
- package/src/ui/a2ui/utils/normaliseComponentsToMap.test.ts +102 -0
- package/src/ui/a2ui/utils/resolveChildIds.test.ts +63 -0
- package/src/ui/a2ui/utils/resolveDynamicNumber.test.ts +13 -1
- package/src/worker/tools/bash/utils/waitForVMReady.test.ts +40 -0
- package/src/worker/tools/email/email.test.ts +193 -8
- package/src/worker/tools/email/utils/asEmailAttachmentInputs.test.ts +47 -0
- package/src/worker/tools/git/git.test.ts +214 -3
- package/src/worker/tools/rooms/invite-to-room.test.ts +54 -0
- package/src/worker/tools/rooms/leave-room.test.ts +40 -0
- package/src/worker/tools/rooms/list-room-members.test.ts +75 -0
- package/src/worker/tools/tasks/tasks-utils.test.ts +22 -0
- package/src/worker/tools/ui/ask-user.test.ts +50 -0
- package/src/worker/tools/ui/clear-chat.test.ts +19 -0
- package/src/worker/tools/ui/get-current-time.test.ts +19 -0
- package/src/worker/tools/ui/javascript.test.ts +116 -0
- package/src/worker/tools/ui/list-components.test.ts +10 -0
- package/src/worker/tools/ui/list-tool-profiles.test.ts +42 -0
- package/src/worker/tools/ui/manage-tools.test.ts +34 -0
- package/src/worker/tools/ui/render-component.test.ts +346 -0
- package/src/worker/tools/ui/send-notification.test.ts +40 -0
- package/src/worker/tools/ui/utils/getAllowFullInternetAccess.test.ts +31 -0
- package/src/worker/tools/ui/utils/parseBooleanConfig.test.ts +25 -0
- package/src/worker/tools/workspace/attach-file.test.ts +84 -0
- package/src/worker/tools/workspace/copy-file.test.ts +69 -0
- package/src/worker/tools/workspace/create-directory.test.ts +40 -0
- package/src/worker/tools/workspace/delete-file.test.ts +73 -0
- package/src/worker/tools/workspace/diff-files.test.ts +94 -0
- package/src/worker/tools/workspace/list-files.test.ts +36 -0
- package/src/worker/tools/workspace/move-file.test.ts +66 -0
- package/src/worker/tools/workspace/open-file.test.ts +57 -0
- package/src/worker/tools/workspace/patch-file.test.ts +81 -0
- package/src/worker/tools/workspace/read-file.test.ts +116 -0
- package/src/worker/tools/workspace/send-file.test.ts +82 -0
- package/src/worker/tools/workspace/update-memory.test.ts +29 -0
- package/src/worker/tools/workspace/write-file.test.ts +28 -0
- package/src/worker/utils/rate-limit.test.ts +199 -0
- package/src/worker/utils/tool-state.test.ts +29 -0
- package/src/worker/utils/transformers-js-utils.test.ts +59 -0
- package/dist/public/config-BYAuxx3j.js +0 -1
- package/dist/public/downloadGroupFile-Drdvg1Ev.js +0 -1
- package/dist/public/file-viewer-CcZYw-PS.js +0 -1
- package/dist/public/orchestrator-o9Fj67BN.js +0 -15
- package/dist/public/shadow-claw-dialog-ByyHgxM9.js +0 -1
- package/dist/public/shadow-claw-files-BpfpJIuP.js +0 -840
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import express from "express";
|
|
3
|
+
import { WebSocket } from "ws";
|
|
4
|
+
import { openClientStore, closeClientStore } from "./client-registry.js";
|
|
5
|
+
import { createControlPlane } from "./control-plane.js";
|
|
6
|
+
import type { AddressInfo } from "node:net";
|
|
7
|
+
import type {
|
|
8
|
+
ControlMessage,
|
|
9
|
+
ClientRegisterPayload,
|
|
10
|
+
CommandResultPayload,
|
|
11
|
+
} from "./control-plane-types.js";
|
|
12
|
+
|
|
13
|
+
function makeHttpRequest(options: {
|
|
14
|
+
method?: string;
|
|
15
|
+
path: string;
|
|
16
|
+
port: number;
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
body?: any;
|
|
19
|
+
}): Promise<{ status: number; data: any; headers: http.IncomingHttpHeaders }> {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const req = http.request(
|
|
22
|
+
{
|
|
23
|
+
hostname: "127.0.0.1",
|
|
24
|
+
port: options.port,
|
|
25
|
+
path: options.path,
|
|
26
|
+
method: options.method || "GET",
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
...options.headers,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
(res) => {
|
|
33
|
+
let body = "";
|
|
34
|
+
res.on("data", (chunk) => (body += chunk));
|
|
35
|
+
res.on("end", () => {
|
|
36
|
+
let parsed = body;
|
|
37
|
+
try {
|
|
38
|
+
parsed = JSON.parse(body);
|
|
39
|
+
} catch (_) {}
|
|
40
|
+
resolve({
|
|
41
|
+
status: res.statusCode || 0,
|
|
42
|
+
data: parsed,
|
|
43
|
+
headers: res.headers,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
req.on("error", reject);
|
|
49
|
+
if (options.body) {
|
|
50
|
+
req.write(
|
|
51
|
+
typeof options.body === "string"
|
|
52
|
+
? options.body
|
|
53
|
+
: JSON.stringify(options.body),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
req.end();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe("control-plane", () => {
|
|
61
|
+
let app: express.Express;
|
|
62
|
+
let server: http.Server;
|
|
63
|
+
let port: number;
|
|
64
|
+
let token: string;
|
|
65
|
+
let controlPlane: ReturnType<typeof createControlPlane>;
|
|
66
|
+
|
|
67
|
+
beforeEach(async () => {
|
|
68
|
+
openClientStore(":memory:");
|
|
69
|
+
token = "test-secret-token";
|
|
70
|
+
|
|
71
|
+
app = express();
|
|
72
|
+
app.use(express.json());
|
|
73
|
+
server = http.createServer(app);
|
|
74
|
+
|
|
75
|
+
controlPlane = createControlPlane({
|
|
76
|
+
httpServer: server,
|
|
77
|
+
app,
|
|
78
|
+
token,
|
|
79
|
+
heartbeatTimeoutMs: 1000,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
await new Promise<void>((resolve) => {
|
|
83
|
+
server.listen(0, "127.0.0.1", () => {
|
|
84
|
+
port = (server.address() as AddressInfo).port;
|
|
85
|
+
resolve();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
afterEach(async () => {
|
|
91
|
+
controlPlane.close();
|
|
92
|
+
await new Promise<void>((resolve) => {
|
|
93
|
+
server.close(() => resolve());
|
|
94
|
+
});
|
|
95
|
+
closeClientStore();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("WebSocket connection and authentication", () => {
|
|
99
|
+
it("rejects cross-origin connection with invalid or missing token", async () => {
|
|
100
|
+
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws/control`, {
|
|
101
|
+
headers: { Origin: "https://unauthorized-origin.com" },
|
|
102
|
+
});
|
|
103
|
+
const closeEvent = await new Promise<{ code: number; reason: string }>(
|
|
104
|
+
(resolve) => {
|
|
105
|
+
ws.on("close", (code, reason) =>
|
|
106
|
+
resolve({ code, reason: reason.toString() }),
|
|
107
|
+
);
|
|
108
|
+
},
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(closeEvent.code).toBe(4001); // Unauthorized code
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("accepts connection with valid query token", async () => {
|
|
115
|
+
const ws = new WebSocket(
|
|
116
|
+
`ws://127.0.0.1:${port}/ws/control?token=${token}`,
|
|
117
|
+
);
|
|
118
|
+
await new Promise<void>((resolve, reject) => {
|
|
119
|
+
ws.on("open", () => resolve());
|
|
120
|
+
ws.on("error", reject);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(ws.readyState).toBe(WebSocket.OPEN);
|
|
124
|
+
ws.close();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("accepts same-origin connection without token", async () => {
|
|
128
|
+
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws/control`, {
|
|
129
|
+
headers: { Origin: `http://127.0.0.1:${port}` },
|
|
130
|
+
});
|
|
131
|
+
await new Promise<void>((resolve, reject) => {
|
|
132
|
+
ws.on("open", () => resolve());
|
|
133
|
+
ws.on("error", reject);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
expect(ws.readyState).toBe(WebSocket.OPEN);
|
|
137
|
+
ws.close();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("Client registration and heartbeat", () => {
|
|
142
|
+
it("handles client:register message and responds with server:registered", async () => {
|
|
143
|
+
const ws = new WebSocket(
|
|
144
|
+
`ws://127.0.0.1:${port}/ws/control?token=${token}`,
|
|
145
|
+
);
|
|
146
|
+
await new Promise<void>((resolve) => ws.on("open", () => resolve()));
|
|
147
|
+
|
|
148
|
+
const registerMsg: ControlMessage = {
|
|
149
|
+
id: "msg-1",
|
|
150
|
+
type: "client:register",
|
|
151
|
+
payload: {
|
|
152
|
+
clientId: "test-client-1",
|
|
153
|
+
deviceLabel: "Test Device",
|
|
154
|
+
capabilities: ["opfs", "webmcp"],
|
|
155
|
+
version: "1.0.0",
|
|
156
|
+
} as ClientRegisterPayload,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const responsePromise = new Promise<ControlMessage>((resolve) => {
|
|
160
|
+
ws.on("message", (data) => {
|
|
161
|
+
resolve(JSON.parse(data.toString()));
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
ws.send(JSON.stringify(registerMsg));
|
|
166
|
+
const response = await responsePromise;
|
|
167
|
+
|
|
168
|
+
expect(response.type).toBe("server:registered");
|
|
169
|
+
expect(response.replyTo).toBe("msg-1");
|
|
170
|
+
expect(controlPlane.isClientConnected("test-client-1")).toBe(true);
|
|
171
|
+
|
|
172
|
+
ws.close();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("handles client:heartbeat and updates client registry", async () => {
|
|
176
|
+
const ws = new WebSocket(
|
|
177
|
+
`ws://127.0.0.1:${port}/ws/control?token=${token}`,
|
|
178
|
+
);
|
|
179
|
+
await new Promise<void>((resolve) => ws.on("open", () => resolve()));
|
|
180
|
+
|
|
181
|
+
ws.send(
|
|
182
|
+
JSON.stringify({
|
|
183
|
+
id: "reg-1",
|
|
184
|
+
type: "client:register",
|
|
185
|
+
payload: {
|
|
186
|
+
clientId: "test-client-hb",
|
|
187
|
+
deviceLabel: "Test Device",
|
|
188
|
+
capabilities: ["opfs"],
|
|
189
|
+
version: "1.0.0",
|
|
190
|
+
},
|
|
191
|
+
}),
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// Wait for registration
|
|
195
|
+
await new Promise((resolve) => ws.once("message", resolve));
|
|
196
|
+
|
|
197
|
+
const heartbeatMsg: ControlMessage = {
|
|
198
|
+
id: "hb-1",
|
|
199
|
+
type: "client:heartbeat",
|
|
200
|
+
payload: {
|
|
201
|
+
clientId: "test-client-hb",
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
ws.send(JSON.stringify(heartbeatMsg));
|
|
206
|
+
// Give server a moment to update
|
|
207
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
208
|
+
|
|
209
|
+
const clients = controlPlane.getConnectedClients();
|
|
210
|
+
expect(clients.some((c) => c.clientId === "test-client-hb")).toBe(true);
|
|
211
|
+
|
|
212
|
+
ws.close();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe("Command execution", () => {
|
|
217
|
+
it("dispatches command to client and receives result", async () => {
|
|
218
|
+
const ws = new WebSocket(
|
|
219
|
+
`ws://127.0.0.1:${port}/ws/control?token=${token}`,
|
|
220
|
+
);
|
|
221
|
+
await new Promise<void>((resolve) => ws.on("open", () => resolve()));
|
|
222
|
+
|
|
223
|
+
ws.send(
|
|
224
|
+
JSON.stringify({
|
|
225
|
+
id: "reg-cmd",
|
|
226
|
+
type: "client:register",
|
|
227
|
+
payload: {
|
|
228
|
+
clientId: "cmd-client",
|
|
229
|
+
deviceLabel: "Cmd Client",
|
|
230
|
+
capabilities: ["opfs"],
|
|
231
|
+
version: "1.0.0",
|
|
232
|
+
},
|
|
233
|
+
}),
|
|
234
|
+
);
|
|
235
|
+
await new Promise((resolve) => ws.once("message", resolve));
|
|
236
|
+
|
|
237
|
+
// Listen for incoming command and respond
|
|
238
|
+
ws.on("message", (data) => {
|
|
239
|
+
const msg = JSON.parse(data.toString()) as ControlMessage;
|
|
240
|
+
if (msg.type === "command:execute") {
|
|
241
|
+
const payload = msg.payload as any;
|
|
242
|
+
const resultMsg: ControlMessage = {
|
|
243
|
+
id: "res-1",
|
|
244
|
+
type: "command:result",
|
|
245
|
+
replyTo: msg.id,
|
|
246
|
+
payload: {
|
|
247
|
+
commandId: payload.commandId,
|
|
248
|
+
success: true,
|
|
249
|
+
data: { answer: 42 },
|
|
250
|
+
} as CommandResultPayload,
|
|
251
|
+
};
|
|
252
|
+
ws.send(JSON.stringify(resultMsg));
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const result = await controlPlane.sendCommand(
|
|
257
|
+
"cmd-client",
|
|
258
|
+
"read-state",
|
|
259
|
+
{ key: "foo" },
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
expect(result.success).toBe(true);
|
|
263
|
+
expect(result.data).toEqual({ answer: 42 });
|
|
264
|
+
|
|
265
|
+
ws.close();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("rejects when sending command to unconnected client", async () => {
|
|
269
|
+
await expect(
|
|
270
|
+
controlPlane.sendCommand("nonexistent-client", "list-tasks", {}),
|
|
271
|
+
).rejects.toThrow(/not connected/i);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("times out if client does not respond in time", async () => {
|
|
275
|
+
const ws = new WebSocket(
|
|
276
|
+
`ws://127.0.0.1:${port}/ws/control?token=${token}`,
|
|
277
|
+
);
|
|
278
|
+
await new Promise<void>((resolve) => ws.on("open", () => resolve()));
|
|
279
|
+
|
|
280
|
+
ws.send(
|
|
281
|
+
JSON.stringify({
|
|
282
|
+
id: "reg-timeout",
|
|
283
|
+
type: "client:register",
|
|
284
|
+
payload: {
|
|
285
|
+
clientId: "timeout-client",
|
|
286
|
+
deviceLabel: "Timeout Client",
|
|
287
|
+
capabilities: [],
|
|
288
|
+
version: "1.0.0",
|
|
289
|
+
},
|
|
290
|
+
}),
|
|
291
|
+
);
|
|
292
|
+
await new Promise((resolve) => ws.once("message", resolve));
|
|
293
|
+
|
|
294
|
+
// Do not respond to command, specify short timeout
|
|
295
|
+
await expect(
|
|
296
|
+
controlPlane.sendCommand(
|
|
297
|
+
"timeout-client",
|
|
298
|
+
"list-tasks",
|
|
299
|
+
{},
|
|
300
|
+
100, // 100ms timeout
|
|
301
|
+
),
|
|
302
|
+
).rejects.toThrow(/timed out/i);
|
|
303
|
+
|
|
304
|
+
ws.close();
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
describe("REST / SSE endpoints", () => {
|
|
309
|
+
it("GET /api/control/clients returns connected clients with valid token", async () => {
|
|
310
|
+
const res = await makeHttpRequest({
|
|
311
|
+
port,
|
|
312
|
+
path: "/api/control/clients",
|
|
313
|
+
headers: { "x-control-token": token },
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
expect(res.status).toBe(200);
|
|
317
|
+
expect(Array.isArray(res.data.clients)).toBe(true);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("GET /api/control/clients returns 401 with invalid token from unauthorized origin", async () => {
|
|
321
|
+
const res = await makeHttpRequest({
|
|
322
|
+
port,
|
|
323
|
+
path: "/api/control/clients",
|
|
324
|
+
headers: {
|
|
325
|
+
Origin: "https://unauthorized-origin.com",
|
|
326
|
+
"x-control-token": "wrong-token",
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
expect(res.status).toBe(401);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("POST /api/control/messages processes message over HTTP", async () => {
|
|
334
|
+
const res = await makeHttpRequest({
|
|
335
|
+
port,
|
|
336
|
+
path: "/api/control/messages",
|
|
337
|
+
method: "POST",
|
|
338
|
+
headers: { "x-control-token": token },
|
|
339
|
+
body: {
|
|
340
|
+
id: "http-msg-1",
|
|
341
|
+
type: "client:register",
|
|
342
|
+
payload: {
|
|
343
|
+
clientId: "http-client-1",
|
|
344
|
+
deviceLabel: "HTTP Client",
|
|
345
|
+
capabilities: ["opfs"],
|
|
346
|
+
version: "1.0.0",
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
expect(res.status).toBe(200);
|
|
352
|
+
expect(res.data.status).toBe("received");
|
|
353
|
+
expect(res.data.reply?.type).toBe("server:registered");
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("dispatches command to SSE client and resolves when client POSTs result", async () => {
|
|
357
|
+
// 1. Establish SSE connection
|
|
358
|
+
let sseReq: http.ClientRequest;
|
|
359
|
+
const sseEventsPromise = new Promise<{
|
|
360
|
+
sseReq: http.ClientRequest;
|
|
361
|
+
onEvent: (fn: (event: string, data: any) => void) => void;
|
|
362
|
+
}>((resolve, reject) => {
|
|
363
|
+
sseReq = http.request(
|
|
364
|
+
{
|
|
365
|
+
hostname: "127.0.0.1",
|
|
366
|
+
port,
|
|
367
|
+
path: `/api/control/events?clientId=sse-cmd-client&token=${token}`,
|
|
368
|
+
method: "GET",
|
|
369
|
+
headers: {
|
|
370
|
+
Accept: "text/event-stream",
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
(res) => {
|
|
374
|
+
let buffer = "";
|
|
375
|
+
let eventHandler: ((event: string, data: any) => void) | null =
|
|
376
|
+
null;
|
|
377
|
+
|
|
378
|
+
res.on("data", (chunk) => {
|
|
379
|
+
buffer += chunk.toString();
|
|
380
|
+
const lines = buffer.split("\n\n");
|
|
381
|
+
buffer = lines.pop() || "";
|
|
382
|
+
for (const block of lines) {
|
|
383
|
+
if (!block.trim()) continue;
|
|
384
|
+
let currentEvent = "message";
|
|
385
|
+
let dataStr = "";
|
|
386
|
+
for (const line of block.split("\n")) {
|
|
387
|
+
if (line.startsWith("event: ")) {
|
|
388
|
+
currentEvent = line.slice(7).trim();
|
|
389
|
+
} else if (line.startsWith("data: ")) {
|
|
390
|
+
dataStr = line.slice(6).trim();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (dataStr && eventHandler) {
|
|
394
|
+
try {
|
|
395
|
+
eventHandler(currentEvent, JSON.parse(dataStr));
|
|
396
|
+
} catch (_) {}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
resolve({
|
|
402
|
+
sseReq,
|
|
403
|
+
onEvent: (fn) => {
|
|
404
|
+
eventHandler = fn;
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
},
|
|
408
|
+
);
|
|
409
|
+
sseReq.on("error", reject);
|
|
410
|
+
sseReq.end();
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
const { onEvent } = await sseEventsPromise;
|
|
414
|
+
|
|
415
|
+
// 2. Register client via HTTP POST
|
|
416
|
+
await makeHttpRequest({
|
|
417
|
+
port,
|
|
418
|
+
path: "/api/control/messages",
|
|
419
|
+
method: "POST",
|
|
420
|
+
headers: { "x-control-token": token },
|
|
421
|
+
body: {
|
|
422
|
+
id: "reg-sse-1",
|
|
423
|
+
type: "client:register",
|
|
424
|
+
payload: {
|
|
425
|
+
clientId: "sse-cmd-client",
|
|
426
|
+
deviceLabel: "SSE Cmd Client",
|
|
427
|
+
capabilities: ["opfs"],
|
|
428
|
+
version: "1.0.0",
|
|
429
|
+
transport: "sse",
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
expect(controlPlane.isClientConnected("sse-cmd-client")).toBe(true);
|
|
435
|
+
|
|
436
|
+
// 3. Listen for command:execute over SSE stream and reply via POST /api/control/messages
|
|
437
|
+
onEvent(async (eventName, data) => {
|
|
438
|
+
if (eventName === "command:execute") {
|
|
439
|
+
const payload = data.payload;
|
|
440
|
+
await makeHttpRequest({
|
|
441
|
+
port,
|
|
442
|
+
path: "/api/control/messages",
|
|
443
|
+
method: "POST",
|
|
444
|
+
headers: { "x-control-token": token },
|
|
445
|
+
body: {
|
|
446
|
+
id: "res-sse-1",
|
|
447
|
+
type: "command:result",
|
|
448
|
+
replyTo: data.id,
|
|
449
|
+
payload: {
|
|
450
|
+
commandId: payload.commandId,
|
|
451
|
+
success: true,
|
|
452
|
+
data: { sseResult: "success from sse" },
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
// 4. Send command from server
|
|
460
|
+
const result = await controlPlane.sendCommand(
|
|
461
|
+
"sse-cmd-client",
|
|
462
|
+
"read-state",
|
|
463
|
+
{ test: true },
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
expect(result.success).toBe(true);
|
|
467
|
+
expect(result.data).toEqual({ sseResult: "success from sse" });
|
|
468
|
+
|
|
469
|
+
sseReq!.destroy();
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
});
|