@mosadd/mcp 3.0.0-alpha.5 → 3.0.0-alpha.50
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/CHANGELOG.md +309 -0
- package/LICENSE +201 -201
- package/README.md +150 -126
- package/dist/bin/mcp.js +42 -3
- package/dist/chunk-L4PB6A6Z.js +3800 -0
- package/dist/index.d.ts +118 -5
- package/dist/index.js +7 -3
- package/package.json +10 -7
- package/src/__tests__/channel-e2ee.test.ts +62 -0
- package/src/__tests__/golden-path.test.ts +139 -137
- package/src/__tests__/integration.test.ts +182 -178
- package/src/__tests__/knowledge-ingest.test.ts +114 -0
- package/src/__tests__/mdm-e2ee.test.ts +212 -178
- package/src/__tests__/mirc-invite.test.ts +112 -0
- package/src/__tests__/mtalk.test.ts +122 -122
- package/src/__tests__/presence.test.ts +199 -0
- package/src/__tests__/server.test.ts +71 -67
- package/src/__tests__/serverJsonVersion.test.ts +45 -0
- package/src/__tests__/tool-count-consistency.test.ts +199 -0
- package/src/bin/login.ts +138 -138
- package/src/bin/mcp.ts +145 -97
- package/src/config.ts +66 -66
- package/src/crypto/channel-e2ee.ts +288 -0
- package/src/crypto/mdm-session.ts +502 -403
- package/src/index.ts +19 -17
- package/src/observability/sentry.ts +79 -79
- package/src/providers/blob-upload.ts +116 -0
- package/src/providers/memory-voice.ts +79 -79
- package/src/providers/supabase-dm.ts +122 -117
- package/src/providers/supabase-voice.ts +91 -91
- package/src/providers/supabase.ts +231 -96
- package/src/server.ts +162 -105
- package/src/tools/__tests__/ragDeleteFoldsSourceId.test.ts +67 -0
- package/src/tools/actions.ts +166 -0
- package/src/tools/attachments.ts +276 -0
- package/src/tools/capabilities.ts +70 -68
- package/src/tools/embed.ts +110 -0
- package/src/tools/index.ts +181 -51
- package/src/tools/knowledge-graph.ts +186 -0
- package/src/tools/knowledge.ts +344 -132
- package/src/tools/mail-agentbox.ts +155 -0
- package/src/tools/mail-aliases.ts +25 -0
- package/src/tools/mail-provenance.ts +137 -0
- package/src/tools/mail.ts +403 -157
- package/src/tools/mcall.ts +216 -216
- package/src/tools/mdm-as-agent.ts +126 -0
- package/src/tools/mdm-voice.ts +220 -206
- package/src/tools/mdm.ts +73 -26
- package/src/tools/mirc-edge.ts +196 -190
- package/src/tools/mirc-members.ts +287 -177
- package/src/tools/mirc-messages.ts +76 -27
- package/src/tools/mirc.ts +308 -170
- package/src/tools/mroom-messages.ts +142 -142
- package/src/tools/mroom.ts +221 -221
- package/src/tools/mtalk.ts +131 -121
- package/src/tools/murl.ts +314 -0
- package/src/tools/presence.ts +442 -0
- package/src/tools/ptt-ingest.ts +99 -0
- package/src/tools/threat.ts +123 -0
- package/src/tools/voice-format.ts +28 -0
- package/src/types.ts +121 -85
- package/src/version.ts +8 -0
- package/src/voice/floor.ts +132 -132
- package/dist/chunk-4C36JDTJ.js +0 -1836
|
@@ -1,178 +1,212 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mDM E2EE golden path (LINEAR-2357) — the "done" bar.
|
|
3
|
-
*
|
|
4
|
-
* Two parties (alice, bob) sharing ONE backend that acts as both the message
|
|
5
|
-
* store AND the key directory (decision "1a": prekey bundles ride the same
|
|
6
|
-
* DmProvider as messages). Proves:
|
|
7
|
-
* 1. both publish prekey bundles via the provider,
|
|
8
|
-
* 2. alice mDM_send → X3DH handshake + ratchet → bob mDM_list decrypts it,
|
|
9
|
-
* 3. the bytes on the wire are CIPHERTEXT, not plaintext (no test-theater),
|
|
10
|
-
* 4. bob replies on the established session → alice mDM_list decrypts it.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { describe, expect, it } from "vitest";
|
|
14
|
-
import type {
|
|
15
|
-
DmProvider,
|
|
16
|
-
DmSendArgs,
|
|
17
|
-
DmListArgs,
|
|
18
|
-
DmSendResult,
|
|
19
|
-
DmListResult,
|
|
20
|
-
} from "@mosadd/providers";
|
|
21
|
-
import { mdmTools } from "../tools/mdm.js";
|
|
22
|
-
import { InMemoryMdmKeyStore } from "../crypto/mdm-session.js";
|
|
23
|
-
import { InMemoryVoiceProvider } from "../providers/memory-voice.js";
|
|
24
|
-
import
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
readonly
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
private readonly
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
expect(wireStr).toContain("
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
expect(fromAlice?.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
expect(ownByAlice?.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
(alice.providers.keys as {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
await tool("
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
obj
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* mDM E2EE golden path (LINEAR-2357) — the "done" bar.
|
|
3
|
+
*
|
|
4
|
+
* Two parties (alice, bob) sharing ONE backend that acts as both the message
|
|
5
|
+
* store AND the key directory (decision "1a": prekey bundles ride the same
|
|
6
|
+
* DmProvider as messages). Proves:
|
|
7
|
+
* 1. both publish prekey bundles via the provider,
|
|
8
|
+
* 2. alice mDM_send → X3DH handshake + ratchet → bob mDM_list decrypts it,
|
|
9
|
+
* 3. the bytes on the wire are CIPHERTEXT, not plaintext (no test-theater),
|
|
10
|
+
* 4. bob replies on the established session → alice mDM_list decrypts it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, expect, it } from "vitest";
|
|
14
|
+
import type {
|
|
15
|
+
DmProvider,
|
|
16
|
+
DmSendArgs,
|
|
17
|
+
DmListArgs,
|
|
18
|
+
DmSendResult,
|
|
19
|
+
DmListResult,
|
|
20
|
+
} from "@mosadd/providers";
|
|
21
|
+
import { mdmTools } from "../tools/mdm.js";
|
|
22
|
+
import { InMemoryMdmKeyStore } from "../crypto/mdm-session.js";
|
|
23
|
+
import { InMemoryVoiceProvider } from "../providers/memory-voice.js";
|
|
24
|
+
import { createMosaddServer } from "../server.js";
|
|
25
|
+
import type { MosaddToolContext } from "../types.js";
|
|
26
|
+
|
|
27
|
+
/** Shared store: message log + key directory, both keyed in one place. */
|
|
28
|
+
class SharedBackend {
|
|
29
|
+
readonly messages: Array<{ id: string; senderId: string; payload: Uint8Array; threadId: string }> = [];
|
|
30
|
+
readonly bundles = new Map<string, Uint8Array>();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One participant's view onto the shared backend. */
|
|
34
|
+
class ClientProvider implements DmProvider {
|
|
35
|
+
constructor(
|
|
36
|
+
private readonly backend: SharedBackend,
|
|
37
|
+
private readonly myId: string,
|
|
38
|
+
) {}
|
|
39
|
+
async selfId(): Promise<string> {
|
|
40
|
+
return this.myId;
|
|
41
|
+
}
|
|
42
|
+
async send(args: DmSendArgs): Promise<DmSendResult> {
|
|
43
|
+
const id = `m${this.backend.messages.length + 1}`;
|
|
44
|
+
this.backend.messages.push({ id, senderId: this.myId, payload: args.payload, threadId: args.threadId });
|
|
45
|
+
return { id, deliveredAt: new Date(0).toISOString() };
|
|
46
|
+
}
|
|
47
|
+
async list(args: DmListArgs): Promise<DmListResult> {
|
|
48
|
+
return {
|
|
49
|
+
messages: this.backend.messages
|
|
50
|
+
.filter((m) => m.threadId === args.threadId)
|
|
51
|
+
.map((m) => ({
|
|
52
|
+
id: m.id,
|
|
53
|
+
senderId: m.senderId,
|
|
54
|
+
payload: m.payload,
|
|
55
|
+
timestamp: new Date(0).toISOString(),
|
|
56
|
+
threadId: m.threadId,
|
|
57
|
+
})),
|
|
58
|
+
nextCursor: null,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async publishPrekeyBundle(bundle: Uint8Array): Promise<void> {
|
|
62
|
+
this.backend.bundles.set(this.myId, bundle);
|
|
63
|
+
}
|
|
64
|
+
async fetchPrekeyBundle(peerId: string): Promise<Uint8Array | null> {
|
|
65
|
+
return this.backend.bundles.get(peerId) ?? null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const tool = (name: string) => {
|
|
70
|
+
const t = mdmTools.find((x) => x.name === name);
|
|
71
|
+
if (!t) throw new Error(`tool ${name} not found`);
|
|
72
|
+
return t.handler;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function ctxFor(backend: SharedBackend, id: string): MosaddToolContext {
|
|
76
|
+
return {
|
|
77
|
+
options: { mode: "local", hubUrl: "https://mcp.mosadd.com" },
|
|
78
|
+
providers: {
|
|
79
|
+
dm: new ClientProvider(backend, id),
|
|
80
|
+
keys: new InMemoryMdmKeyStore(),
|
|
81
|
+
voice: new InMemoryVoiceProvider(id),
|
|
82
|
+
},
|
|
83
|
+
log: () => {},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
describe("mDM E2EE golden path — X3DH handshake + ratchet, both directions", () => {
|
|
88
|
+
it("delivers an encrypted message alice→bob and a reply bob→alice", async () => {
|
|
89
|
+
const backend = new SharedBackend();
|
|
90
|
+
const alice = ctxFor(backend, "alice");
|
|
91
|
+
const bob = ctxFor(backend, "bob");
|
|
92
|
+
|
|
93
|
+
// 1. Both publish their prekey bundles (so first contact is possible).
|
|
94
|
+
await tool("mDM_publish_keys")({}, alice);
|
|
95
|
+
const bobPub = (await tool("mDM_publish_keys")({}, bob)) as { one_time_prekeys: number };
|
|
96
|
+
expect(bobPub.one_time_prekeys).toBeGreaterThan(0);
|
|
97
|
+
|
|
98
|
+
// 2. Alice sends an encrypted DM to Bob (first contact → X3DH handshake).
|
|
99
|
+
const sent = (await tool("mDM_send")({ to: "bob", text: "first contact" }, alice)) as {
|
|
100
|
+
encrypted: boolean;
|
|
101
|
+
};
|
|
102
|
+
expect(sent.encrypted).toBe(true);
|
|
103
|
+
|
|
104
|
+
// 3. The wire bytes must be CIPHERTEXT — the plaintext must NOT appear.
|
|
105
|
+
const wire = backend.messages[0]!.payload;
|
|
106
|
+
const wireStr = Buffer.from(wire).toString("utf8");
|
|
107
|
+
expect(wireStr).not.toContain("first contact");
|
|
108
|
+
expect(wireStr).toContain("mosadd.e2ee.v2"); // DH-ratchet sealed envelope (PCS — LINEAR-3409)
|
|
109
|
+
|
|
110
|
+
// 4. Bob lists the thread and decrypts Alice's message.
|
|
111
|
+
const bobView = (await tool("mDM_list")({ contact_id: "alice" }, bob)) as {
|
|
112
|
+
messages: Array<{ text: string; sender_identity_id: string; encrypted: boolean }>;
|
|
113
|
+
};
|
|
114
|
+
const fromAlice = bobView.messages.find((m) => m.sender_identity_id === "alice");
|
|
115
|
+
expect(fromAlice?.text).toBe("first contact");
|
|
116
|
+
expect(fromAlice?.encrypted).toBe(true);
|
|
117
|
+
|
|
118
|
+
// 5. Bob replies on the now-established session (no new handshake).
|
|
119
|
+
await tool("mDM_send")({ to: "alice", text: "copy that" }, bob);
|
|
120
|
+
|
|
121
|
+
// 6. Alice decrypts Bob's reply AND reads back her OWN sent message
|
|
122
|
+
// (sent-items cache — the ratchet can't re-derive own plaintext on read).
|
|
123
|
+
const aliceView = (await tool("mDM_list")({ contact_id: "bob" }, alice)) as {
|
|
124
|
+
messages: Array<{ text: string; sender_identity_id: string; encrypted: boolean }>;
|
|
125
|
+
};
|
|
126
|
+
const fromBob = aliceView.messages.find((m) => m.sender_identity_id === "bob");
|
|
127
|
+
expect(fromBob?.text).toBe("copy that");
|
|
128
|
+
const ownByAlice = aliceView.messages.find((m) => m.sender_identity_id === "alice");
|
|
129
|
+
expect(ownByAlice?.text).toBe("first contact"); // not "<encrypted · sent by you>"
|
|
130
|
+
expect(ownByAlice?.encrypted).toBe(true);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("falls back to a marker for own messages when no sent-items cache exists", async () => {
|
|
134
|
+
// A keystore WITHOUT the optional sent-items cache must not crash; the
|
|
135
|
+
// sender simply sees the marker for their own outgoing messages.
|
|
136
|
+
const backend = new SharedBackend();
|
|
137
|
+
const alice = ctxFor(backend, "alice");
|
|
138
|
+
const bob = ctxFor(backend, "bob");
|
|
139
|
+
// Strip the optional cache methods from alice's keystore.
|
|
140
|
+
(alice.providers.keys as { putSentMessage?: unknown }).putSentMessage = undefined;
|
|
141
|
+
(alice.providers.keys as { getSentMessage?: unknown }).getSentMessage = undefined;
|
|
142
|
+
|
|
143
|
+
await tool("mDM_publish_keys")({}, bob);
|
|
144
|
+
await tool("mDM_send")({ to: "bob", text: "no cache here" }, alice);
|
|
145
|
+
|
|
146
|
+
const aliceView = (await tool("mDM_list")({ contact_id: "bob" }, alice)) as {
|
|
147
|
+
messages: Array<{ text: string; sender_identity_id: string }>;
|
|
148
|
+
};
|
|
149
|
+
const own = aliceView.messages.find((m) => m.sender_identity_id === "alice");
|
|
150
|
+
expect(own?.text).toBe("<encrypted · sent by you>");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("mDM_send fails with an actionable error when the peer has no published keys", async () => {
|
|
154
|
+
const backend = new SharedBackend();
|
|
155
|
+
const alice = ctxFor(backend, "alice");
|
|
156
|
+
// Bob never published — no bundle to fetch.
|
|
157
|
+
await expect(tool("mDM_send")({ to: "bob", text: "hi" }, alice)).rejects.toThrow(
|
|
158
|
+
/has not published a prekey bundle/i,
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("rejects a tampered (MITM) prekey bundle instead of starting a session", async () => {
|
|
163
|
+
const backend = new SharedBackend();
|
|
164
|
+
const alice = ctxFor(backend, "alice");
|
|
165
|
+
const bob = ctxFor(backend, "bob");
|
|
166
|
+
|
|
167
|
+
await tool("mDM_publish_keys")({}, bob);
|
|
168
|
+
|
|
169
|
+
// MITM swaps Bob's DH identity key (a SIGNED field) in the directory entry,
|
|
170
|
+
// keeping it valid base64 so it parses — only the signature should catch it.
|
|
171
|
+
const obj = JSON.parse(Buffer.from(backend.bundles.get("bob")!).toString("utf8"));
|
|
172
|
+
obj.ik = (obj.ik[0] === "A" ? "B" : "A") + obj.ik.slice(1);
|
|
173
|
+
backend.bundles.set("bob", new Uint8Array(Buffer.from(JSON.stringify(obj), "utf8")));
|
|
174
|
+
|
|
175
|
+
await expect(tool("mDM_send")({ to: "bob", text: "intercept me" }, alice)).rejects.toThrow(
|
|
176
|
+
/signature verification|man-in-the-middle/i,
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("auto-publish prekeys on startup (LINEAR-3104 cold-start fix)", () => {
|
|
182
|
+
it("publishes the local prekey bundle when autoPublishKeys is set", async () => {
|
|
183
|
+
const backend = new SharedBackend();
|
|
184
|
+
createMosaddServer({
|
|
185
|
+
mode: "local",
|
|
186
|
+
autoPublishKeys: true,
|
|
187
|
+
providers: {
|
|
188
|
+
dm: new ClientProvider(backend, "self-agent"),
|
|
189
|
+
keys: new InMemoryMdmKeyStore(),
|
|
190
|
+
voice: new InMemoryVoiceProvider("self-agent"),
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
// fire-and-forget; getOwnMaterial() does async key generation, so flush.
|
|
194
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
195
|
+
expect(backend.bundles.has("self-agent")).toBe(true);
|
|
196
|
+
expect(backend.bundles.get("self-agent")!.byteLength).toBeGreaterThan(0);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("does NOT publish when autoPublishKeys is unset (hosted/default)", async () => {
|
|
200
|
+
const backend = new SharedBackend();
|
|
201
|
+
createMosaddServer({
|
|
202
|
+
mode: "local",
|
|
203
|
+
providers: {
|
|
204
|
+
dm: new ClientProvider(backend, "quiet-agent"),
|
|
205
|
+
keys: new InMemoryMdmKeyStore(),
|
|
206
|
+
voice: new InMemoryVoiceProvider("quiet-agent"),
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
210
|
+
expect(backend.bundles.has("quiet-agent")).toBe(false);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mIRC_invite — the wrapper that lets a channel created from the toolkit STOP being mute.
|
|
3
|
+
*
|
|
4
|
+
* mIRC_join adds the KEY's identity (the account, usually a human) to a channel, while
|
|
5
|
+
* message-send signs channel posts with the AGENT — so until the agent itself is a member
|
|
6
|
+
* every post 403s `agent_not_in_channel`. channel-members-manage `invite` has done the right
|
|
7
|
+
* thing since LINEAR-5596 (own agent → auto_joined, no code); the toolkit just never exposed it.
|
|
8
|
+
*
|
|
9
|
+
* Pinned here, because each would silently regress:
|
|
10
|
+
* 1. the tool exists, is a network tool, and its description names BOTH outcomes;
|
|
11
|
+
* 2. the field-name normalizer does NOT rename `role` → `new_role` for invite — the EF's
|
|
12
|
+
* invite branch reads `body.role`, so the rename would drop every requested role to
|
|
13
|
+
* "member" without an error (set-role keeps the rename: that branch reads `new_role`);
|
|
14
|
+
* 3. mIRC_create's `invite_agents` fan-out never forwards the field to channel-manage,
|
|
15
|
+
* reports per-agent failures in `invites[]`, and never throws the created channel away.
|
|
16
|
+
*/
|
|
17
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
18
|
+
import type { MosaddTool } from "../types.js";
|
|
19
|
+
|
|
20
|
+
const CHANNEL_ID = "6d0f1c1e-1111-4a2b-9c3d-000000000001";
|
|
21
|
+
const AGENT_A = "0489d4c6-8727-442f-bf83-68747a469492";
|
|
22
|
+
const AGENT_B = "b228b6ae-dda6-4823-9882-40ea91ed1530";
|
|
23
|
+
|
|
24
|
+
const calls: { fn: string; body: Record<string, unknown> }[] = [];
|
|
25
|
+
let inviteReply: (body: Record<string, unknown>) => unknown = () => ({ auto_joined: true, member: {} });
|
|
26
|
+
|
|
27
|
+
vi.mock("../providers/supabase.js", () => ({
|
|
28
|
+
readSupabaseEnv: () => ({ url: "https://example.supabase.co", anonKey: "anon", userJwt: "jwt" }),
|
|
29
|
+
invokeFunction: async (fn: string, body: Record<string, unknown>) => {
|
|
30
|
+
calls.push({ fn, body });
|
|
31
|
+
if (fn === "channel-manage") return { channel: { id: CHANNEL_ID, name: body.name } };
|
|
32
|
+
if (fn === "channel-members-manage") return inviteReply(body);
|
|
33
|
+
throw new Error(`unexpected edge function ${fn}`);
|
|
34
|
+
},
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
const ctx = { log: () => {} } as never;
|
|
38
|
+
/** Import after the mock is registered, so the modules under test pick it up. */
|
|
39
|
+
const { mircMembersTools } = await import("../tools/mirc-members.js");
|
|
40
|
+
const { mircTools } = await import("../tools/mirc.js");
|
|
41
|
+
|
|
42
|
+
function tool(list: readonly MosaddTool[], name: string): MosaddTool {
|
|
43
|
+
const t = list.find((x) => x.name === name);
|
|
44
|
+
if (!t) throw new Error(`${name} is not registered`);
|
|
45
|
+
return t;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe("mIRC_invite", () => {
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
calls.length = 0;
|
|
51
|
+
inviteReply = () => ({ auto_joined: true, member: {} });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("is registered as a network tool whose description names both outcomes", () => {
|
|
55
|
+
const invite = tool(mircMembersTools, "mIRC_invite");
|
|
56
|
+
expect(invite.requires).toBe("network");
|
|
57
|
+
expect(invite.description).toContain("auto_joined");
|
|
58
|
+
expect(invite.description).toContain("invite code");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("sends `role` under its own name — the EF's invite branch reads body.role, not new_role", async () => {
|
|
62
|
+
const invite = tool(mircMembersTools, "mIRC_invite");
|
|
63
|
+
await invite.handler({ channel_id: CHANNEL_ID, identity_id: AGENT_A, role: "moderator" }, ctx);
|
|
64
|
+
expect(calls).toHaveLength(1);
|
|
65
|
+
expect(calls[0].fn).toBe("channel-members-manage");
|
|
66
|
+
expect(calls[0].body).toMatchObject({ action: "invite", channel_id: CHANNEL_ID, target_identity_id: AGENT_A, role: "moderator" });
|
|
67
|
+
expect(calls[0].body).not.toHaveProperty("new_role");
|
|
68
|
+
expect(calls[0].body).not.toHaveProperty("identity_id");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("keeps the role → new_role rename for set-role (the exemption is invite-only)", async () => {
|
|
72
|
+
const setRole = tool(mircMembersTools, "mIRC_set_role");
|
|
73
|
+
await setRole.handler({ channel_id: CHANNEL_ID, identity_id: AGENT_A, role: "admin" }, ctx);
|
|
74
|
+
expect(calls[0].body).toMatchObject({ action: "set-role", new_role: "admin" });
|
|
75
|
+
expect(calls[0].body).not.toHaveProperty("role");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("mIRC_create invite_agents fan-out", () => {
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
calls.length = 0;
|
|
82
|
+
inviteReply = () => ({ auto_joined: true, member: {} });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("creates first, then invites each agent; a per-agent failure lands in invites[] and never undoes the channel", async () => {
|
|
86
|
+
inviteReply = (body) => {
|
|
87
|
+
if (body.target_identity_id === AGENT_B) throw new Error("channel-members-manage (400): Already a member of this channel");
|
|
88
|
+
return { auto_joined: true, member: { identity_id: body.target_identity_id } };
|
|
89
|
+
};
|
|
90
|
+
const create = tool(mircTools, "mIRC_create");
|
|
91
|
+
const out = (await create.handler({ name: "ops", access_mode: "open", invite_agents: [AGENT_A, AGENT_B] }, ctx)) as {
|
|
92
|
+
channel: { id: string };
|
|
93
|
+
invites: Array<{ identity_id: string; ok: boolean; auto_joined?: boolean; error?: string }>;
|
|
94
|
+
};
|
|
95
|
+
expect(out.channel.id).toBe(CHANNEL_ID);
|
|
96
|
+
expect(calls.map((c) => c.fn)).toEqual(["channel-manage", "channel-members-manage", "channel-members-manage"]);
|
|
97
|
+
// invite_agents is toolkit-side composition — channel-manage must never see it.
|
|
98
|
+
expect(calls[0].body).not.toHaveProperty("invite_agents");
|
|
99
|
+
expect(calls[1].body).toMatchObject({ action: "invite", channel_id: CHANNEL_ID, target_identity_id: AGENT_A });
|
|
100
|
+
expect(out.invites).toHaveLength(2);
|
|
101
|
+
expect(out.invites[0]).toMatchObject({ identity_id: AGENT_A, ok: true, auto_joined: true });
|
|
102
|
+
expect(out.invites[1]).toMatchObject({ identity_id: AGENT_B, ok: false });
|
|
103
|
+
expect(out.invites[1].error).toContain("Already a member");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("without invite_agents behaves exactly as before — one call, no invites key", async () => {
|
|
107
|
+
const create = tool(mircTools, "mIRC_create");
|
|
108
|
+
const out = (await create.handler({ name: "ops", access_mode: "open" }, ctx)) as Record<string, unknown>;
|
|
109
|
+
expect(calls).toHaveLength(1);
|
|
110
|
+
expect(out).not.toHaveProperty("invites");
|
|
111
|
+
});
|
|
112
|
+
});
|