@aria-cli/tools 1.0.9 → 1.0.11
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/package.json +9 -5
- package/src/__tests__/web-fetch-download.test.ts +0 -433
- package/src/__tests__/web-tools.test.ts +0 -619
- package/src/ask-user-interaction.ts +0 -33
- package/src/cache/web-cache.ts +0 -110
- package/src/definitions/arion.ts +0 -118
- package/src/definitions/browser/browser.ts +0 -502
- package/src/definitions/browser/index.ts +0 -5
- package/src/definitions/browser/pw-downloads.ts +0 -142
- package/src/definitions/browser/pw-interactions.ts +0 -282
- package/src/definitions/browser/pw-responses.ts +0 -98
- package/src/definitions/browser/pw-session.ts +0 -405
- package/src/definitions/browser/pw-shared.ts +0 -85
- package/src/definitions/browser/pw-snapshot.ts +0 -383
- package/src/definitions/browser/pw-state.ts +0 -101
- package/src/definitions/browser/types.ts +0 -203
- package/src/definitions/code-intelligence.ts +0 -526
- package/src/definitions/core.ts +0 -118
- package/src/definitions/delegation.ts +0 -567
- package/src/definitions/deploy.ts +0 -73
- package/src/definitions/filesystem.ts +0 -217
- package/src/definitions/frg.ts +0 -67
- package/src/definitions/index.ts +0 -28
- package/src/definitions/memory.ts +0 -150
- package/src/definitions/messaging.ts +0 -734
- package/src/definitions/meta.ts +0 -392
- package/src/definitions/network.ts +0 -179
- package/src/definitions/outlook.ts +0 -318
- package/src/definitions/patch/apply-patch.ts +0 -235
- package/src/definitions/patch/fuzzy-match.ts +0 -217
- package/src/definitions/patch/index.ts +0 -1
- package/src/definitions/patch/patch-parser.ts +0 -297
- package/src/definitions/patch/sandbox-paths.ts +0 -129
- package/src/definitions/process/index.ts +0 -5
- package/src/definitions/process/process-registry.ts +0 -303
- package/src/definitions/process/process.ts +0 -456
- package/src/definitions/process/pty-keys.ts +0 -298
- package/src/definitions/process/session-slug.ts +0 -147
- package/src/definitions/quip.ts +0 -225
- package/src/definitions/search.ts +0 -67
- package/src/definitions/session-history.ts +0 -79
- package/src/definitions/shell.ts +0 -202
- package/src/definitions/slack.ts +0 -211
- package/src/definitions/web.ts +0 -119
- package/src/executors/apply-patch.ts +0 -1035
- package/src/executors/arion.ts +0 -199
- package/src/executors/code-intelligence.ts +0 -1179
- package/src/executors/deploy.ts +0 -1066
- package/src/executors/filesystem.ts +0 -1428
- package/src/executors/frg-freshness.ts +0 -743
- package/src/executors/frg.ts +0 -394
- package/src/executors/index.ts +0 -280
- package/src/executors/learning-meta.ts +0 -1367
- package/src/executors/lsp-client.ts +0 -355
- package/src/executors/memory.ts +0 -978
- package/src/executors/meta.ts +0 -293
- package/src/executors/process-registry.ts +0 -570
- package/src/executors/pty-session-store.ts +0 -43
- package/src/executors/pty.ts +0 -342
- package/src/executors/restart.ts +0 -133
- package/src/executors/search-freshness.ts +0 -249
- package/src/executors/search-types.ts +0 -98
- package/src/executors/search.ts +0 -89
- package/src/executors/self-diagnose.ts +0 -552
- package/src/executors/session-history.ts +0 -435
- package/src/executors/shell-safety.ts +0 -519
- package/src/executors/shell.ts +0 -1243
- package/src/executors/utils.ts +0 -40
- package/src/executors/web.ts +0 -786
- package/src/extraction/content-extraction.ts +0 -281
- package/src/extraction/index.ts +0 -5
- package/src/headless-control-contract.ts +0 -1149
- package/src/index.ts +0 -788
- package/src/local-control-http-auth.ts +0 -2
- package/src/mcp/client.ts +0 -218
- package/src/mcp/connection.ts +0 -568
- package/src/mcp/index.ts +0 -11
- package/src/mcp/jsonrpc.ts +0 -195
- package/src/mcp/types.ts +0 -199
- package/src/network-control-adapter.ts +0 -88
- package/src/network-runtime/address-types.ts +0 -218
- package/src/network-runtime/db-owner-fencing.ts +0 -91
- package/src/network-runtime/delivery-receipts.ts +0 -372
- package/src/network-runtime/direct-endpoint-authority.ts +0 -35
- package/src/network-runtime/index.ts +0 -316
- package/src/network-runtime/local-control-contract.ts +0 -784
- package/src/network-runtime/node-store-contract.ts +0 -46
- package/src/network-runtime/pair-route-contract.ts +0 -97
- package/src/network-runtime/peer-capabilities.ts +0 -48
- package/src/network-runtime/peer-principal-ref.ts +0 -20
- package/src/network-runtime/peer-state-machine.ts +0 -160
- package/src/network-runtime/protocol-schemas.ts +0 -265
- package/src/network-runtime/runtime-bootstrap-contract.ts +0 -83
- package/src/outlook/desktop-session.ts +0 -409
- package/src/policy.ts +0 -171
- package/src/providers/brave.ts +0 -80
- package/src/providers/duckduckgo.ts +0 -199
- package/src/providers/exa.ts +0 -85
- package/src/providers/firecrawl.ts +0 -77
- package/src/providers/index.ts +0 -8
- package/src/providers/jina.ts +0 -70
- package/src/providers/router.ts +0 -121
- package/src/providers/search-provider.ts +0 -74
- package/src/providers/tavily.ts +0 -74
- package/src/quip/desktop-session.ts +0 -435
- package/src/registry/index.ts +0 -1
- package/src/registry/registry.ts +0 -905
- package/src/runtime-socket-local-control-client.ts +0 -632
- package/src/security/dns-normalization.ts +0 -34
- package/src/security/dns-pinning.ts +0 -138
- package/src/security/external-content.ts +0 -129
- package/src/security/ssrf.ts +0 -207
- package/src/slack/desktop-session.ts +0 -493
- package/src/tool-factory.ts +0 -91
- package/src/types.ts +0 -1341
- package/src/utils/retry.ts +0 -163
- package/src/utils/safe-parse-json.ts +0 -176
- package/src/utils/url.ts +0 -20
- package/tests/benchmarks/registry.bench.ts +0 -57
- package/tests/cache/web-cache.test.ts +0 -147
- package/tests/critical-integration.test.ts +0 -1465
- package/tests/definitions/apply-patch.test.ts +0 -586
- package/tests/definitions/browser.test.ts +0 -495
- package/tests/definitions/delegation-pause-resume.test.ts +0 -758
- package/tests/definitions/execution.test.ts +0 -671
- package/tests/definitions/messaging-inbox-scope.test.ts +0 -229
- package/tests/definitions/messaging.test.ts +0 -1468
- package/tests/definitions/outlook.test.ts +0 -30
- package/tests/definitions/process.test.ts +0 -469
- package/tests/definitions/slack.test.ts +0 -28
- package/tests/definitions/tool-inventory.test.ts +0 -218
- package/tests/e2e/delegation-quest-orchestration.e2e.test.ts +0 -433
- package/tests/e2e/memory-tool-discovery-contract.e2e.test.ts +0 -81
- package/tests/executors/apply-patch.test.ts +0 -538
- package/tests/executors/arion.test.ts +0 -309
- package/tests/executors/conversation-primitives.test.ts +0 -250
- package/tests/executors/deploy.test.ts +0 -746
- package/tests/executors/filesystem-tools.test.ts +0 -357
- package/tests/executors/filesystem.test.ts +0 -959
- package/tests/executors/frg-freshness.test.ts +0 -136
- package/tests/executors/frg-merge.test.ts +0 -70
- package/tests/executors/frg-session-content.test.ts +0 -40
- package/tests/executors/frg.test.ts +0 -56
- package/tests/executors/memory-bugfixes.test.ts +0 -257
- package/tests/executors/memory-real-memoria.integration.test.ts +0 -316
- package/tests/executors/memory.test.ts +0 -853
- package/tests/executors/meta-tools.test.ts +0 -411
- package/tests/executors/meta.test.ts +0 -683
- package/tests/executors/path-containment.test.ts +0 -51
- package/tests/executors/process-registry.test.ts +0 -505
- package/tests/executors/pty.test.ts +0 -664
- package/tests/executors/quest-security.test.ts +0 -249
- package/tests/executors/read-file-media.test.ts +0 -230
- package/tests/executors/recall-knowledge-schema.test.ts +0 -209
- package/tests/executors/recall-tags.test.ts +0 -278
- package/tests/executors/remember-null-safety.contract.test.ts +0 -41
- package/tests/executors/restart.test.ts +0 -67
- package/tests/executors/search-unified.test.ts +0 -381
- package/tests/executors/session-history.test.ts +0 -340
- package/tests/executors/session-transcript.test.ts +0 -561
- package/tests/executors/shell-abort.test.ts +0 -416
- package/tests/executors/shell-env-blocklist.test.ts +0 -648
- package/tests/executors/shell-env-process.test.ts +0 -245
- package/tests/executors/shell-process-registry.test.ts +0 -334
- package/tests/executors/shell-tools.test.ts +0 -393
- package/tests/executors/shell.test.ts +0 -690
- package/tests/executors/web-abort-vs-timeout.test.ts +0 -213
- package/tests/executors/web-integration.test.ts +0 -633
- package/tests/executors/web-symlink.test.ts +0 -18
- package/tests/executors/web.test.ts +0 -1400
- package/tests/executors/write-stdin.test.ts +0 -145
- package/tests/extraction/content-extraction.test.ts +0 -153
- package/tests/guards/tools-default-test-lane.integration.test.ts +0 -21
- package/tests/guards/tools-package-test-commands.e2e.test.ts +0 -43
- package/tests/guards/tools-test-lane-manifest.contract.test.ts +0 -76
- package/tests/guards/tools-vitest-workspace-alias.contract.test.ts +0 -63
- package/tests/helpers/async-waits.ts +0 -53
- package/tests/integration/headless-control-contract.integration.test.ts +0 -153
- package/tests/integration/memory-tool-schema-parity.integration.test.ts +0 -67
- package/tests/integration/meta-tools-round-trip.integration.test.ts +0 -506
- package/tests/integration/quest-round-trip.test.ts +0 -303
- package/tests/integration/registry-executor-flow.test.ts +0 -85
- package/tests/integration.test.ts +0 -177
- package/tests/loading-tier.test.ts +0 -126
- package/tests/mcp/client-reconnect.test.ts +0 -267
- package/tests/mcp/connection.test.ts +0 -846
- package/tests/mcp/injectable-logger.test.ts +0 -83
- package/tests/mcp/jsonrpc.test.ts +0 -109
- package/tests/mcp/lifecycle.test.ts +0 -879
- package/tests/network-runtime/address-types.contract.test.ts +0 -143
- package/tests/network-runtime/continuity-bind-schema.contract.test.ts +0 -203
- package/tests/network-runtime/local-control-contract.test.ts +0 -869
- package/tests/network-runtime/local-control-invite-token.contract.test.ts +0 -146
- package/tests/network-runtime/node-store-contract.test.ts +0 -11
- package/tests/network-runtime/pair-protocol-nodeid.contract.test.ts +0 -15
- package/tests/network-runtime/peer-state-machine.contract.test.ts +0 -148
- package/tests/network-runtime/protocol-schemas.contract.test.ts +0 -512
- package/tests/network-runtime/relay-pending-nodeid.contract.test.ts +0 -62
- package/tests/network-runtime/runtime-bootstrap-contract.test.ts +0 -227
- package/tests/network-runtime/runtime-socket-local-control-client.test.ts +0 -621
- package/tests/network-runtime/wait-for-message-script.test.ts +0 -288
- package/tests/parallel.test.ts +0 -71
- package/tests/policy.test.ts +0 -184
- package/tests/print-default-test-lane.ts +0 -14
- package/tests/print-test-lane-manifest.ts +0 -22
- package/tests/providers/brave.test.ts +0 -159
- package/tests/providers/duckduckgo.test.ts +0 -207
- package/tests/providers/exa.test.ts +0 -175
- package/tests/providers/firecrawl.test.ts +0 -168
- package/tests/providers/jina.test.ts +0 -144
- package/tests/providers/router.test.ts +0 -328
- package/tests/providers/tavily.test.ts +0 -165
- package/tests/registry/discovery.test.ts +0 -154
- package/tests/registry/injectable-logger.test.ts +0 -230
- package/tests/registry/input-validation.test.ts +0 -361
- package/tests/registry/interface-completeness.test.ts +0 -85
- package/tests/registry/mcp-integration.test.ts +0 -103
- package/tests/registry/mcp-read-only-hint.test.ts +0 -60
- package/tests/registry/memoria-discovery.test.ts +0 -390
- package/tests/registry/nested-validation.test.ts +0 -283
- package/tests/registry/pseudo-tool-filtering.test.ts +0 -258
- package/tests/registry/registration-lifecycle.test.ts +0 -133
- package/tests/registry-validation.test.ts +0 -424
- package/tests/registry.test.ts +0 -460
- package/tests/security/dns-pinning.test.ts +0 -162
- package/tests/security/external-content.test.ts +0 -144
- package/tests/security/ssrf.test.ts +0 -118
- package/tests/shell-safety-integration.test.ts +0 -32
- package/tests/shell-safety.test.ts +0 -365
- package/tests/slack/desktop-session.test.ts +0 -50
- package/tests/test-lane-manifest.ts +0 -440
- package/tests/test-utils.ts +0 -27
- package/tests/tool-factory.test.ts +0 -188
- package/tests/utils/retry.test.ts +0 -231
- package/tests/utils/url.test.ts +0 -63
- package/tsconfig.cjs.json +0 -24
- package/tsconfig.json +0 -12
- package/vitest.config.ts +0 -55
- package/vitest.e2e.config.ts +0 -24
- package/vitest.integration.config.ts +0 -24
- package/vitest.native.config.ts +0 -24
|
@@ -1,1468 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @aria/tools - messaging tool integration tests
|
|
3
|
-
*
|
|
4
|
-
* Tests the 4 messaging tool execute functions with REAL SQLite-backed
|
|
5
|
-
* MessageStore instances. No mocks for message storage — every assertion
|
|
6
|
-
* is verified against the actual database.
|
|
7
|
-
*
|
|
8
|
-
* Error-path tests (missing messageStore) remain as-is since those
|
|
9
|
-
* exercise tool behavior when no store is wired.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
13
|
-
import { createRequire } from "node:module";
|
|
14
|
-
import { dirname, join } from "node:path";
|
|
15
|
-
import { fileURLToPath } from "node:url";
|
|
16
|
-
import { MESSAGING_TOOL_DEFINITIONS } from "../../src/definitions/messaging.js";
|
|
17
|
-
import { MessageStore } from "@aria-cli/memoria/storage";
|
|
18
|
-
import type { ToolContext } from "../../src/types.js";
|
|
19
|
-
|
|
20
|
-
// Resolve better-sqlite3 through @aria/memoria's dependency tree.
|
|
21
|
-
// pnpm strict hoisting means it's only in memoria's node_modules.
|
|
22
|
-
// We use createRequire pointed at memoria's package.json to access its deps.
|
|
23
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
24
|
-
const memoriaDir = join(__dirname, "../../node_modules/@aria/memoria");
|
|
25
|
-
const memoriaRequire = createRequire(join(memoriaDir, "package.json"));
|
|
26
|
-
const Database = memoriaRequire("better-sqlite3");
|
|
27
|
-
type DatabaseType = InstanceType<typeof Database>;
|
|
28
|
-
|
|
29
|
-
// ── Helpers ────────────────────────────────────────────────────────────
|
|
30
|
-
|
|
31
|
-
/** Find a tool by name from the definitions array */
|
|
32
|
-
function findTool(name: string) {
|
|
33
|
-
const tool = MESSAGING_TOOL_DEFINITIONS.find((t) => t.name === name);
|
|
34
|
-
if (!tool) throw new Error(`Tool "${name}" not found in MESSAGING_TOOL_DEFINITIONS`);
|
|
35
|
-
return tool;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Create a real in-memory SQLite DB with the messages table and indexes */
|
|
39
|
-
function createTestDb(): DatabaseType {
|
|
40
|
-
const db = new Database(":memory:");
|
|
41
|
-
db.pragma("journal_mode = WAL");
|
|
42
|
-
db.pragma("foreign_keys = ON");
|
|
43
|
-
|
|
44
|
-
db.exec(`CREATE TABLE IF NOT EXISTS messages (
|
|
45
|
-
id TEXT PRIMARY KEY,
|
|
46
|
-
sender_id TEXT NOT NULL,
|
|
47
|
-
sender_name TEXT NOT NULL,
|
|
48
|
-
sender_type TEXT NOT NULL CHECK (sender_type IN ('arion', 'worker', 'leader')),
|
|
49
|
-
recipient_id TEXT NOT NULL,
|
|
50
|
-
recipient_name TEXT NOT NULL,
|
|
51
|
-
reply_to TEXT,
|
|
52
|
-
correlation_id TEXT,
|
|
53
|
-
quest_id TEXT,
|
|
54
|
-
type TEXT NOT NULL,
|
|
55
|
-
content TEXT NOT NULL,
|
|
56
|
-
metadata TEXT,
|
|
57
|
-
priority INTEGER DEFAULT 2 CHECK (priority >= 0 AND priority <= 4),
|
|
58
|
-
direction TEXT NOT NULL CHECK (direction IN ('sent', 'received')),
|
|
59
|
-
read_at INTEGER,
|
|
60
|
-
signature TEXT,
|
|
61
|
-
created_at INTEGER NOT NULL,
|
|
62
|
-
inbox_address_kind TEXT,
|
|
63
|
-
inbox_address_id TEXT
|
|
64
|
-
)`);
|
|
65
|
-
db.exec(`CREATE INDEX IF NOT EXISTS idx_messages_correlation ON messages(correlation_id)`);
|
|
66
|
-
db.exec(`CREATE INDEX IF NOT EXISTS idx_messages_sender ON messages(sender_name)`);
|
|
67
|
-
db.exec(`CREATE INDEX IF NOT EXISTS idx_messages_type ON messages(type)`);
|
|
68
|
-
db.exec(
|
|
69
|
-
`CREATE INDEX IF NOT EXISTS idx_messages_unread ON messages(direction, read_at) WHERE read_at IS NULL`,
|
|
70
|
-
);
|
|
71
|
-
db.exec(`CREATE INDEX IF NOT EXISTS idx_messages_reply_to ON messages(reply_to)`);
|
|
72
|
-
db.exec(`CREATE INDEX IF NOT EXISTS idx_messages_created ON messages(created_at)`);
|
|
73
|
-
|
|
74
|
-
return db;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Create a real ToolContext backed by a real MessageStore */
|
|
78
|
-
function createRealContext(opts?: {
|
|
79
|
-
db?: DatabaseType;
|
|
80
|
-
mailbox?: ToolContext["mailbox"];
|
|
81
|
-
arion?: ToolContext["arion"];
|
|
82
|
-
runtimeId?: string;
|
|
83
|
-
networkControl?: ToolContext["networkControl"];
|
|
84
|
-
}): { context: ToolContext; db: DatabaseType; store: MessageStore } {
|
|
85
|
-
const db = opts?.db ?? createTestDb();
|
|
86
|
-
const store = new MessageStore(db);
|
|
87
|
-
const runtimeId = opts?.runtimeId ?? opts?.arion?.id ?? "recipient-1";
|
|
88
|
-
const context = {
|
|
89
|
-
workingDir: "/tmp",
|
|
90
|
-
env: {},
|
|
91
|
-
confirm: async () => true,
|
|
92
|
-
messageStore: store,
|
|
93
|
-
mailbox: opts?.mailbox,
|
|
94
|
-
arion: opts?.arion,
|
|
95
|
-
networkControl: opts?.networkControl,
|
|
96
|
-
runtimeId,
|
|
97
|
-
nodeId: runtimeId,
|
|
98
|
-
inboxAddress: { kind: "node", nodeId: runtimeId },
|
|
99
|
-
} as ToolContext;
|
|
100
|
-
return { context, db, store };
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Create a bare ToolContext with no messageStore (for error-path tests) */
|
|
104
|
-
function createBareContext(): ToolContext {
|
|
105
|
-
return {
|
|
106
|
-
workingDir: "/tmp",
|
|
107
|
-
env: {},
|
|
108
|
-
confirm: async () => true,
|
|
109
|
-
} as ToolContext;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Helper to insert a message directly into the DB (bypass tool layer) */
|
|
113
|
-
function insertMessage(
|
|
114
|
-
db: DatabaseType,
|
|
115
|
-
overrides: Partial<{
|
|
116
|
-
id: string;
|
|
117
|
-
sender_id: string;
|
|
118
|
-
sender_name: string;
|
|
119
|
-
sender_type: string;
|
|
120
|
-
recipient_id: string;
|
|
121
|
-
recipient_name: string;
|
|
122
|
-
reply_to: string | null;
|
|
123
|
-
correlation_id: string | null;
|
|
124
|
-
type: string;
|
|
125
|
-
content: string;
|
|
126
|
-
metadata: string | null;
|
|
127
|
-
priority: number;
|
|
128
|
-
direction: string;
|
|
129
|
-
read_at: number | null;
|
|
130
|
-
signature: string | null;
|
|
131
|
-
created_at: number;
|
|
132
|
-
inbox_address_kind: "client" | "node";
|
|
133
|
-
inbox_address_id: string;
|
|
134
|
-
}> = {},
|
|
135
|
-
) {
|
|
136
|
-
const msg = {
|
|
137
|
-
id: `msg-${Math.random().toString(36).slice(2, 10)}`,
|
|
138
|
-
sender_id: "sender-1",
|
|
139
|
-
sender_name: "Alice",
|
|
140
|
-
sender_type: "arion",
|
|
141
|
-
recipient_id: "recipient-1",
|
|
142
|
-
recipient_name: "Bob",
|
|
143
|
-
reply_to: null,
|
|
144
|
-
correlation_id: null,
|
|
145
|
-
type: "finding",
|
|
146
|
-
content: "Default test content",
|
|
147
|
-
metadata: null,
|
|
148
|
-
priority: 2,
|
|
149
|
-
direction: "received",
|
|
150
|
-
read_at: null,
|
|
151
|
-
signature: null,
|
|
152
|
-
created_at: Date.now(),
|
|
153
|
-
inbox_address_kind: "node" as const,
|
|
154
|
-
inbox_address_id: "recipient-1",
|
|
155
|
-
...overrides,
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
db.prepare(
|
|
159
|
-
`INSERT INTO messages (
|
|
160
|
-
id, sender_id, sender_name, sender_type,
|
|
161
|
-
recipient_id, recipient_name,
|
|
162
|
-
reply_to, correlation_id, type, content, metadata,
|
|
163
|
-
priority, direction, read_at, signature, created_at,
|
|
164
|
-
inbox_address_kind, inbox_address_id
|
|
165
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
166
|
-
).run(
|
|
167
|
-
msg.id,
|
|
168
|
-
msg.sender_id,
|
|
169
|
-
msg.sender_name,
|
|
170
|
-
msg.sender_type,
|
|
171
|
-
msg.recipient_id,
|
|
172
|
-
msg.recipient_name,
|
|
173
|
-
msg.reply_to,
|
|
174
|
-
msg.correlation_id,
|
|
175
|
-
msg.type,
|
|
176
|
-
msg.content,
|
|
177
|
-
msg.metadata,
|
|
178
|
-
msg.priority,
|
|
179
|
-
msg.direction,
|
|
180
|
-
msg.read_at,
|
|
181
|
-
msg.signature,
|
|
182
|
-
msg.created_at,
|
|
183
|
-
msg.inbox_address_kind,
|
|
184
|
-
msg.inbox_address_id,
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
return msg;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// ── send_message ────────────────────────────────────────────────────────
|
|
191
|
-
|
|
192
|
-
describe("messaging tools", () => {
|
|
193
|
-
describe("send_message", () => {
|
|
194
|
-
const tool = findTool("send_message");
|
|
195
|
-
let db: DatabaseType;
|
|
196
|
-
|
|
197
|
-
afterEach(() => {
|
|
198
|
-
db?.close();
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it("returns error when messageStore is missing", async () => {
|
|
202
|
-
const ctx = createBareContext();
|
|
203
|
-
const result = await tool.execute({ to: "Bob", type: "finding", content: "Hello" }, ctx);
|
|
204
|
-
|
|
205
|
-
expect(result.success).toBe(false);
|
|
206
|
-
expect(result.message).toMatch(/[Mm]essage store not available/);
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
it("stores message in real DB when no mailbox", async () => {
|
|
210
|
-
const setup = createRealContext();
|
|
211
|
-
db = setup.db;
|
|
212
|
-
|
|
213
|
-
const result = await tool.execute(
|
|
214
|
-
{ to: "Bob", type: "finding", content: "Hello Bob" },
|
|
215
|
-
setup.context,
|
|
216
|
-
);
|
|
217
|
-
|
|
218
|
-
expect(result.success).toBe(true);
|
|
219
|
-
|
|
220
|
-
// Verify directly in the real database
|
|
221
|
-
const rows = db.prepare("SELECT * FROM messages").all();
|
|
222
|
-
expect(rows).toHaveLength(1);
|
|
223
|
-
|
|
224
|
-
const row = rows[0] as Record<string, unknown>;
|
|
225
|
-
expect(row.direction).toBe("sent");
|
|
226
|
-
expect(row.content).toBe("Hello Bob");
|
|
227
|
-
expect(row.recipient_name).toBe("Bob");
|
|
228
|
-
expect(row.type).toBe("finding");
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it("delivers via mailbox when available", async () => {
|
|
232
|
-
const mailbox = {
|
|
233
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
234
|
-
transport: "relay",
|
|
235
|
-
delivered: true,
|
|
236
|
-
queued: false,
|
|
237
|
-
}),
|
|
238
|
-
has: vi.fn().mockReturnValue(false),
|
|
239
|
-
hasByName: vi.fn().mockReturnValue(true),
|
|
240
|
-
resolveId: vi.fn((name: string) => (name === "Bob" ? "bob-id" : undefined)),
|
|
241
|
-
};
|
|
242
|
-
const setup = createRealContext({ mailbox });
|
|
243
|
-
db = setup.db;
|
|
244
|
-
|
|
245
|
-
const result = await tool.execute(
|
|
246
|
-
{ to: "Bob", type: "finding", content: "Hello via mailbox" },
|
|
247
|
-
setup.context,
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
expect(result.success).toBe(true);
|
|
251
|
-
expect(mailbox.sendDurable).toHaveBeenCalledOnce();
|
|
252
|
-
expect(result.data).toHaveProperty("delivered", true);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it("fails closed when a unique peer display snapshot is supplied instead of a durable peer principal", async () => {
|
|
256
|
-
const mailbox = {
|
|
257
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
258
|
-
transport: "tunnel",
|
|
259
|
-
delivered: true,
|
|
260
|
-
queued: false,
|
|
261
|
-
}),
|
|
262
|
-
has: vi.fn().mockReturnValue(false),
|
|
263
|
-
hasByName: vi.fn().mockReturnValue(false),
|
|
264
|
-
resolveId: vi.fn(),
|
|
265
|
-
};
|
|
266
|
-
const networkControl = {
|
|
267
|
-
invite: vi.fn(),
|
|
268
|
-
revokePeer: vi.fn(),
|
|
269
|
-
listPeers: vi.fn(() => [
|
|
270
|
-
{
|
|
271
|
-
nodeId: "peer-bravo-1",
|
|
272
|
-
transportPublicKey: "wg-bravo-1",
|
|
273
|
-
displayNameSnapshot: "Bravo",
|
|
274
|
-
endpointHost: "10.0.0.22",
|
|
275
|
-
endpointPort: 51820,
|
|
276
|
-
controlEndpoint: null,
|
|
277
|
-
signingPublicKey: "signing-bravo-1",
|
|
278
|
-
status: "active" as const,
|
|
279
|
-
lastHandshake: null,
|
|
280
|
-
createdAt: Date.now(),
|
|
281
|
-
},
|
|
282
|
-
]),
|
|
283
|
-
status: vi.fn(),
|
|
284
|
-
};
|
|
285
|
-
const setup = createRealContext({
|
|
286
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
287
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
288
|
-
});
|
|
289
|
-
db = setup.db;
|
|
290
|
-
|
|
291
|
-
const result = await tool.execute(
|
|
292
|
-
{ to: "Bravo", type: "finding", content: "Hello via tunnel" },
|
|
293
|
-
setup.context,
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
expect(result.success).toBe(false);
|
|
297
|
-
expect(result.message).toMatch(/display name|peer principal/i);
|
|
298
|
-
expect(mailbox.sendDurable).not.toHaveBeenCalled();
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
it("keeps local recipient fallback when no remote peer principal matches the input", async () => {
|
|
302
|
-
const networkControl = {
|
|
303
|
-
invite: vi.fn(),
|
|
304
|
-
revokePeer: vi.fn(),
|
|
305
|
-
listPeers: vi.fn(() => [
|
|
306
|
-
{
|
|
307
|
-
nodeId: "peer-bravo-1",
|
|
308
|
-
transportPublicKey: "wg-bravo-1",
|
|
309
|
-
displayNameSnapshot: "Bravo",
|
|
310
|
-
endpointHost: "10.0.0.22",
|
|
311
|
-
endpointPort: 51820,
|
|
312
|
-
controlEndpoint: null,
|
|
313
|
-
signingPublicKey: "signing-bravo-1",
|
|
314
|
-
status: "active" as const,
|
|
315
|
-
lastHandshake: null,
|
|
316
|
-
createdAt: Date.now(),
|
|
317
|
-
},
|
|
318
|
-
]),
|
|
319
|
-
status: vi.fn(),
|
|
320
|
-
};
|
|
321
|
-
const setup = createRealContext({
|
|
322
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
323
|
-
});
|
|
324
|
-
db = setup.db;
|
|
325
|
-
|
|
326
|
-
const result = await tool.execute(
|
|
327
|
-
{ to: "Charlie", type: "finding", content: "Hello Charlie" },
|
|
328
|
-
setup.context,
|
|
329
|
-
);
|
|
330
|
-
|
|
331
|
-
expect(result.success).toBe(true);
|
|
332
|
-
const rows = db.prepare("SELECT recipient_id, recipient_name FROM messages").all() as Array<{
|
|
333
|
-
recipient_id: string;
|
|
334
|
-
recipient_name: string;
|
|
335
|
-
}>;
|
|
336
|
-
expect(rows).toEqual([
|
|
337
|
-
{
|
|
338
|
-
recipient_id: "Charlie",
|
|
339
|
-
recipient_name: "Charlie",
|
|
340
|
-
},
|
|
341
|
-
]);
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
it("fails closed for attached-client senders when the recipient cannot be validated", async () => {
|
|
345
|
-
const setup = createRealContext();
|
|
346
|
-
db = setup.db;
|
|
347
|
-
setup.context.inboxAddress = { kind: "client", clientId: "client-self" };
|
|
348
|
-
|
|
349
|
-
const result = await tool.execute(
|
|
350
|
-
{ to: "client-other", type: "finding", content: "Hello other client" },
|
|
351
|
-
setup.context,
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
expect(result.success).toBe(false);
|
|
355
|
-
expect(result.message).toMatch(/exact .*clientId|list_clients|unknown recipient/i);
|
|
356
|
-
expect(db.prepare("SELECT * FROM messages").all()).toHaveLength(0);
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
it("keeps raw recipient ids valid when a mailbox is present but delivery falls back locally", async () => {
|
|
360
|
-
const mailbox = {
|
|
361
|
-
sendDurable: vi.fn().mockRejectedValue(new Error('Cannot route message to "ghost-peer"')),
|
|
362
|
-
has: vi.fn().mockReturnValue(false),
|
|
363
|
-
hasByName: vi.fn().mockReturnValue(false),
|
|
364
|
-
};
|
|
365
|
-
const networkControl = {
|
|
366
|
-
invite: vi.fn(),
|
|
367
|
-
revokePeer: vi.fn(),
|
|
368
|
-
listPeers: vi.fn(() => [
|
|
369
|
-
{
|
|
370
|
-
nodeId: "node-bravo",
|
|
371
|
-
transportPublicKey: "wg-bravo-1",
|
|
372
|
-
displayNameSnapshot: "Bravo",
|
|
373
|
-
endpointHost: "10.0.0.22",
|
|
374
|
-
endpointPort: 51820,
|
|
375
|
-
controlEndpoint: null,
|
|
376
|
-
signingPublicKey: "signing-bravo-1",
|
|
377
|
-
status: "active" as const,
|
|
378
|
-
lastHandshake: null,
|
|
379
|
-
createdAt: Date.now(),
|
|
380
|
-
},
|
|
381
|
-
]),
|
|
382
|
-
status: vi.fn(),
|
|
383
|
-
};
|
|
384
|
-
const setup = createRealContext({
|
|
385
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
386
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
387
|
-
});
|
|
388
|
-
db = setup.db;
|
|
389
|
-
|
|
390
|
-
const result = await tool.execute(
|
|
391
|
-
{ to: "ghost-peer", type: "finding", content: "Hello ghost" },
|
|
392
|
-
setup.context,
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
expect(result.success).toBe(true);
|
|
396
|
-
expect(result.message).toMatch(/stored locally|delivery failed/i);
|
|
397
|
-
expect(mailbox.sendDurable).toHaveBeenCalledOnce();
|
|
398
|
-
const rows = db
|
|
399
|
-
.prepare("SELECT recipient_id, recipient_name, direction, content FROM messages")
|
|
400
|
-
.all() as Array<{
|
|
401
|
-
recipient_id: string;
|
|
402
|
-
recipient_name: string;
|
|
403
|
-
direction: string;
|
|
404
|
-
content: string;
|
|
405
|
-
}>;
|
|
406
|
-
expect(rows).toEqual([
|
|
407
|
-
{
|
|
408
|
-
recipient_id: "ghost-peer",
|
|
409
|
-
recipient_name: "ghost-peer",
|
|
410
|
-
direction: "sent",
|
|
411
|
-
content: "Hello ghost",
|
|
412
|
-
},
|
|
413
|
-
]);
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
it("accepts an explicit local recipient id when the mailbox registry confirms it", async () => {
|
|
417
|
-
const mailbox = {
|
|
418
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
419
|
-
transport: "in_process",
|
|
420
|
-
delivered: true,
|
|
421
|
-
queued: false,
|
|
422
|
-
}),
|
|
423
|
-
has: vi.fn((id: string) => id === "rex-id"),
|
|
424
|
-
hasByName: vi.fn().mockReturnValue(false),
|
|
425
|
-
};
|
|
426
|
-
const setup = createRealContext({
|
|
427
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
428
|
-
});
|
|
429
|
-
db = setup.db;
|
|
430
|
-
|
|
431
|
-
const result = await tool.execute(
|
|
432
|
-
{ to: "rex-id", type: "finding", content: "Hello Rex" },
|
|
433
|
-
setup.context,
|
|
434
|
-
);
|
|
435
|
-
|
|
436
|
-
expect(result.success).toBe(true);
|
|
437
|
-
expect(mailbox.sendDurable).toHaveBeenCalledWith(
|
|
438
|
-
expect.objectContaining({
|
|
439
|
-
recipient: expect.objectContaining({
|
|
440
|
-
id: "rex-id",
|
|
441
|
-
name: "rex-id",
|
|
442
|
-
}),
|
|
443
|
-
}),
|
|
444
|
-
);
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
it("resolves a local recipient alias to the canonical mailbox id before delivery", async () => {
|
|
448
|
-
const mailbox = {
|
|
449
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
450
|
-
transport: "in_process",
|
|
451
|
-
delivered: true,
|
|
452
|
-
queued: false,
|
|
453
|
-
}),
|
|
454
|
-
has: vi.fn().mockReturnValue(false),
|
|
455
|
-
hasByName: vi.fn((name: string) => name === "Rex display"),
|
|
456
|
-
resolveId: vi.fn((name: string) => (name === "Rex display" ? "rex-id" : undefined)),
|
|
457
|
-
};
|
|
458
|
-
const setup = createRealContext({
|
|
459
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
460
|
-
});
|
|
461
|
-
db = setup.db;
|
|
462
|
-
|
|
463
|
-
const result = await tool.execute(
|
|
464
|
-
{ to: "Rex display", type: "finding", content: "Hello alias" },
|
|
465
|
-
setup.context,
|
|
466
|
-
);
|
|
467
|
-
|
|
468
|
-
expect(result.success).toBe(true);
|
|
469
|
-
expect(mailbox.sendDurable).toHaveBeenCalledWith(
|
|
470
|
-
expect.objectContaining({
|
|
471
|
-
recipient: expect.objectContaining({
|
|
472
|
-
id: "rex-id",
|
|
473
|
-
name: "Rex display",
|
|
474
|
-
}),
|
|
475
|
-
}),
|
|
476
|
-
);
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
it("prefers a registered local alias over an invalid remote display snapshot match", async () => {
|
|
480
|
-
const mailbox = {
|
|
481
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
482
|
-
transport: "in_process",
|
|
483
|
-
delivered: true,
|
|
484
|
-
queued: false,
|
|
485
|
-
}),
|
|
486
|
-
has: vi.fn().mockReturnValue(false),
|
|
487
|
-
hasByName: vi.fn((name: string) => name === "Bravo"),
|
|
488
|
-
resolveId: vi.fn((name: string) => (name === "Bravo" ? "local-bravo-id" : undefined)),
|
|
489
|
-
};
|
|
490
|
-
const networkControl = {
|
|
491
|
-
invite: vi.fn(),
|
|
492
|
-
revokePeer: vi.fn(),
|
|
493
|
-
listPeers: vi.fn(() => [
|
|
494
|
-
{
|
|
495
|
-
nodeId: "node-bravo",
|
|
496
|
-
transportPublicKey: "wg-bravo-1",
|
|
497
|
-
displayNameSnapshot: "Bravo",
|
|
498
|
-
endpointHost: "10.0.0.22",
|
|
499
|
-
endpointPort: 51820,
|
|
500
|
-
controlEndpoint: null,
|
|
501
|
-
signingPublicKey: "signing-bravo-1",
|
|
502
|
-
status: "active" as const,
|
|
503
|
-
lastHandshake: null,
|
|
504
|
-
createdAt: Date.now(),
|
|
505
|
-
},
|
|
506
|
-
]),
|
|
507
|
-
status: vi.fn(),
|
|
508
|
-
};
|
|
509
|
-
const setup = createRealContext({
|
|
510
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
511
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
512
|
-
});
|
|
513
|
-
db = setup.db;
|
|
514
|
-
|
|
515
|
-
const result = await tool.execute(
|
|
516
|
-
{ to: "Bravo", type: "finding", content: "Hello local Bravo" },
|
|
517
|
-
setup.context,
|
|
518
|
-
);
|
|
519
|
-
|
|
520
|
-
expect(result.success).toBe(true);
|
|
521
|
-
expect(mailbox.sendDurable).toHaveBeenCalledWith(
|
|
522
|
-
expect.objectContaining({
|
|
523
|
-
recipient: expect.objectContaining({
|
|
524
|
-
id: "local-bravo-id",
|
|
525
|
-
name: "Bravo",
|
|
526
|
-
}),
|
|
527
|
-
}),
|
|
528
|
-
);
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
it("accepts an explicit remote nodeId while routing delivery by nodeId", async () => {
|
|
532
|
-
const mailbox = {
|
|
533
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
534
|
-
transport: "tunnel",
|
|
535
|
-
delivered: true,
|
|
536
|
-
queued: false,
|
|
537
|
-
}),
|
|
538
|
-
};
|
|
539
|
-
const networkControl = {
|
|
540
|
-
invite: vi.fn(),
|
|
541
|
-
revokePeer: vi.fn(),
|
|
542
|
-
listPeers: vi.fn(() => [
|
|
543
|
-
{
|
|
544
|
-
nodeId: "node-bravo",
|
|
545
|
-
transportPublicKey: "wg-bravo-1",
|
|
546
|
-
displayNameSnapshot: "Bravo",
|
|
547
|
-
endpointHost: "10.0.0.22",
|
|
548
|
-
endpointPort: 51820,
|
|
549
|
-
controlEndpoint: null,
|
|
550
|
-
signingPublicKey: "signing-bravo-1",
|
|
551
|
-
status: "active" as const,
|
|
552
|
-
lastHandshake: null,
|
|
553
|
-
createdAt: Date.now(),
|
|
554
|
-
},
|
|
555
|
-
]),
|
|
556
|
-
status: vi.fn(),
|
|
557
|
-
};
|
|
558
|
-
const setup = createRealContext({
|
|
559
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
560
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
561
|
-
});
|
|
562
|
-
db = setup.db;
|
|
563
|
-
|
|
564
|
-
const result = await tool.execute(
|
|
565
|
-
{ to: "node-bravo", type: "finding", content: "Hello via nodeId" },
|
|
566
|
-
setup.context,
|
|
567
|
-
);
|
|
568
|
-
|
|
569
|
-
expect(result.success).toBe(true);
|
|
570
|
-
expect(mailbox.sendDurable).toHaveBeenCalledWith(
|
|
571
|
-
expect.objectContaining({
|
|
572
|
-
recipient: expect.objectContaining({
|
|
573
|
-
id: "node-bravo",
|
|
574
|
-
name: "Bravo",
|
|
575
|
-
}),
|
|
576
|
-
}),
|
|
577
|
-
);
|
|
578
|
-
});
|
|
579
|
-
|
|
580
|
-
it("fails closed when a peer display snapshot is ambiguous", async () => {
|
|
581
|
-
const mailbox = {
|
|
582
|
-
sendDurable: vi.fn(),
|
|
583
|
-
has: vi.fn().mockReturnValue(false),
|
|
584
|
-
hasByName: vi.fn().mockReturnValue(false),
|
|
585
|
-
resolveId: vi.fn(),
|
|
586
|
-
};
|
|
587
|
-
const networkControl = {
|
|
588
|
-
invite: vi.fn(),
|
|
589
|
-
revokePeer: vi.fn(),
|
|
590
|
-
listPeers: vi.fn(() => [
|
|
591
|
-
{
|
|
592
|
-
nodeId: "peer-bravo-1",
|
|
593
|
-
transportPublicKey: "wg-bravo-1",
|
|
594
|
-
displayNameSnapshot: "Bravo",
|
|
595
|
-
endpointHost: "10.0.0.22",
|
|
596
|
-
endpointPort: 51820,
|
|
597
|
-
controlEndpoint: null,
|
|
598
|
-
signingPublicKey: "signing-bravo-1",
|
|
599
|
-
status: "active" as const,
|
|
600
|
-
lastHandshake: null,
|
|
601
|
-
createdAt: Date.now(),
|
|
602
|
-
},
|
|
603
|
-
{
|
|
604
|
-
nodeId: "peer-bravo-2",
|
|
605
|
-
transportPublicKey: "wg-bravo-2",
|
|
606
|
-
displayNameSnapshot: "Bravo",
|
|
607
|
-
endpointHost: "10.0.0.23",
|
|
608
|
-
endpointPort: 51820,
|
|
609
|
-
controlEndpoint: null,
|
|
610
|
-
signingPublicKey: "signing-bravo-2",
|
|
611
|
-
status: "active" as const,
|
|
612
|
-
lastHandshake: null,
|
|
613
|
-
createdAt: Date.now(),
|
|
614
|
-
},
|
|
615
|
-
]),
|
|
616
|
-
status: vi.fn(),
|
|
617
|
-
};
|
|
618
|
-
const setup = createRealContext({
|
|
619
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
620
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
621
|
-
});
|
|
622
|
-
db = setup.db;
|
|
623
|
-
|
|
624
|
-
const result = await tool.execute(
|
|
625
|
-
{ to: "Bravo", type: "finding", content: "Ambiguous hello" },
|
|
626
|
-
setup.context,
|
|
627
|
-
);
|
|
628
|
-
|
|
629
|
-
expect(result.success).toBe(false);
|
|
630
|
-
expect(result.message).toMatch(/ambiguous/i);
|
|
631
|
-
expect(mailbox.sendDurable).not.toHaveBeenCalled();
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
it("fails closed when durable delivery to a known remote peer errors", async () => {
|
|
635
|
-
const mailbox = {
|
|
636
|
-
sendDurable: vi.fn().mockRejectedValue(new Error("Connection refused")),
|
|
637
|
-
};
|
|
638
|
-
const networkControl = {
|
|
639
|
-
invite: vi.fn(),
|
|
640
|
-
revokePeer: vi.fn(),
|
|
641
|
-
listPeers: vi.fn(() => [
|
|
642
|
-
{
|
|
643
|
-
nodeId: "peer-bravo-1",
|
|
644
|
-
transportPublicKey: "wg-bravo-1",
|
|
645
|
-
displayNameSnapshot: "Bravo",
|
|
646
|
-
endpointHost: "10.0.0.22",
|
|
647
|
-
endpointPort: 51820,
|
|
648
|
-
controlEndpoint: null,
|
|
649
|
-
signingPublicKey: "signing-bravo-1",
|
|
650
|
-
status: "active" as const,
|
|
651
|
-
lastHandshake: null,
|
|
652
|
-
createdAt: Date.now(),
|
|
653
|
-
},
|
|
654
|
-
]),
|
|
655
|
-
status: vi.fn(),
|
|
656
|
-
};
|
|
657
|
-
const setup = createRealContext({
|
|
658
|
-
mailbox: mailbox as ToolContext["mailbox"],
|
|
659
|
-
networkControl: networkControl as ToolContext["networkControl"],
|
|
660
|
-
});
|
|
661
|
-
db = setup.db;
|
|
662
|
-
|
|
663
|
-
const result = await tool.execute(
|
|
664
|
-
{ to: "peer-bravo-1", type: "finding", content: "Should fail closed" },
|
|
665
|
-
setup.context,
|
|
666
|
-
);
|
|
667
|
-
|
|
668
|
-
expect(result.success).toBe(false);
|
|
669
|
-
expect(result.message).toMatch(/delivery failed/i);
|
|
670
|
-
expect(result.data).toMatchObject({
|
|
671
|
-
to: "peer-bravo-1",
|
|
672
|
-
delivered: false,
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
const rows = db.prepare("SELECT * FROM messages").all();
|
|
676
|
-
expect(rows).toHaveLength(1);
|
|
677
|
-
expect((rows[0] as Record<string, unknown>).direction).toBe("sent");
|
|
678
|
-
});
|
|
679
|
-
|
|
680
|
-
it("surfaces queued tunnel sends instead of claiming delivery", async () => {
|
|
681
|
-
const mailbox = {
|
|
682
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
683
|
-
transport: "tunnel",
|
|
684
|
-
delivered: false,
|
|
685
|
-
queued: true,
|
|
686
|
-
}),
|
|
687
|
-
has: vi.fn().mockReturnValue(false),
|
|
688
|
-
hasByName: vi.fn().mockReturnValue(true),
|
|
689
|
-
resolveId: vi.fn((name: string) => (name === "Bob" ? "bob-id" : undefined)),
|
|
690
|
-
};
|
|
691
|
-
const setup = createRealContext({ mailbox: mailbox as ToolContext["mailbox"] });
|
|
692
|
-
db = setup.db;
|
|
693
|
-
|
|
694
|
-
const result = await tool.execute(
|
|
695
|
-
{ to: "Bob", type: "finding", content: "Queued until tunnel connects" },
|
|
696
|
-
setup.context,
|
|
697
|
-
);
|
|
698
|
-
|
|
699
|
-
expect(result.success).toBe(true);
|
|
700
|
-
expect(mailbox.sendDurable).toHaveBeenCalledOnce();
|
|
701
|
-
expect(result.message).toMatch(/queued/i);
|
|
702
|
-
expect(result.data).toMatchObject({
|
|
703
|
-
delivered: false,
|
|
704
|
-
queued: true,
|
|
705
|
-
transport: "tunnel",
|
|
706
|
-
});
|
|
707
|
-
});
|
|
708
|
-
|
|
709
|
-
it("surfaces unacknowledged tunnel sends instead of claiming delivery", async () => {
|
|
710
|
-
const mailbox = {
|
|
711
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
712
|
-
transport: "tunnel",
|
|
713
|
-
delivered: false,
|
|
714
|
-
queued: true,
|
|
715
|
-
accepted: true,
|
|
716
|
-
deliveryState: "dispatching",
|
|
717
|
-
sessionState: "connected",
|
|
718
|
-
deliveryReadiness: "can_send_now",
|
|
719
|
-
queuedReason: "awaiting_remote_ack",
|
|
720
|
-
}),
|
|
721
|
-
has: vi.fn().mockReturnValue(false),
|
|
722
|
-
hasByName: vi.fn().mockReturnValue(true),
|
|
723
|
-
resolveId: vi.fn((name: string) => (name === "Bob" ? "bob-id" : undefined)),
|
|
724
|
-
};
|
|
725
|
-
const setup = createRealContext({ mailbox: mailbox as ToolContext["mailbox"] });
|
|
726
|
-
db = setup.db;
|
|
727
|
-
|
|
728
|
-
const result = await tool.execute(
|
|
729
|
-
{ to: "Bob", type: "finding", content: "Waiting for remote persistence ack" },
|
|
730
|
-
setup.context,
|
|
731
|
-
);
|
|
732
|
-
|
|
733
|
-
expect(result.success).toBe(true);
|
|
734
|
-
expect(mailbox.sendDurable).toHaveBeenCalledOnce();
|
|
735
|
-
expect(result.message).toMatch(/awaiting/i);
|
|
736
|
-
expect(result.data).toMatchObject({
|
|
737
|
-
delivered: false,
|
|
738
|
-
queued: true,
|
|
739
|
-
transport: "tunnel",
|
|
740
|
-
deliveryState: "dispatching",
|
|
741
|
-
queuedReason: "awaiting_remote_ack",
|
|
742
|
-
});
|
|
743
|
-
});
|
|
744
|
-
|
|
745
|
-
it("falls back to local store on mailbox failure", async () => {
|
|
746
|
-
const mailbox = {
|
|
747
|
-
sendDurable: vi.fn().mockRejectedValue(new Error("Connection refused")),
|
|
748
|
-
has: vi.fn().mockReturnValue(false),
|
|
749
|
-
hasByName: vi.fn().mockReturnValue(true),
|
|
750
|
-
resolveId: vi.fn((name: string) => (name === "Bob" ? "bob-id" : undefined)),
|
|
751
|
-
};
|
|
752
|
-
const setup = createRealContext({ mailbox });
|
|
753
|
-
db = setup.db;
|
|
754
|
-
|
|
755
|
-
const result = await tool.execute(
|
|
756
|
-
{ to: "Bob", type: "finding", content: "Fallback message" },
|
|
757
|
-
setup.context,
|
|
758
|
-
);
|
|
759
|
-
|
|
760
|
-
expect(result.success).toBe(true);
|
|
761
|
-
expect(result.message).toMatch(/delivery failed/);
|
|
762
|
-
expect(result.data).toHaveProperty("delivered", false);
|
|
763
|
-
|
|
764
|
-
// Verify it was stored locally in the real DB
|
|
765
|
-
const rows = db.prepare("SELECT * FROM messages").all();
|
|
766
|
-
expect(rows).toHaveLength(1);
|
|
767
|
-
expect((rows[0] as Record<string, unknown>).direction).toBe("sent");
|
|
768
|
-
expect((rows[0] as Record<string, unknown>).content).toBe("Fallback message");
|
|
769
|
-
});
|
|
770
|
-
|
|
771
|
-
it("sets version:1 on message sent via mailbox", async () => {
|
|
772
|
-
const mailbox = {
|
|
773
|
-
sendDurable: vi.fn().mockResolvedValue({
|
|
774
|
-
transport: "relay",
|
|
775
|
-
delivered: true,
|
|
776
|
-
queued: false,
|
|
777
|
-
}),
|
|
778
|
-
has: vi.fn().mockReturnValue(false),
|
|
779
|
-
hasByName: vi.fn().mockReturnValue(true),
|
|
780
|
-
resolveId: vi.fn((name: string) => (name === "Bob" ? "bob-id" : undefined)),
|
|
781
|
-
};
|
|
782
|
-
const setup = createRealContext({ mailbox });
|
|
783
|
-
db = setup.db;
|
|
784
|
-
|
|
785
|
-
await tool.execute({ to: "Bob", type: "quest", content: "Do something" }, setup.context);
|
|
786
|
-
|
|
787
|
-
// The message passed to mailbox.sendDurable should have version: 1
|
|
788
|
-
const sentMsg = mailbox.sendDurable.mock.calls[0][0];
|
|
789
|
-
expect(sentMsg.version).toBe(1);
|
|
790
|
-
});
|
|
791
|
-
|
|
792
|
-
it("generates UUID for message id", async () => {
|
|
793
|
-
const setup = createRealContext();
|
|
794
|
-
db = setup.db;
|
|
795
|
-
|
|
796
|
-
await tool.execute({ to: "Bob", type: "finding", content: "UUID test" }, setup.context);
|
|
797
|
-
|
|
798
|
-
const row = db.prepare("SELECT id FROM messages").get() as { id: string };
|
|
799
|
-
// UUID v4 format
|
|
800
|
-
expect(row.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
it("uses arion context for sender when available", async () => {
|
|
804
|
-
const setup = createRealContext({
|
|
805
|
-
arion: { id: "rex-id", name: "Rex", emoji: "dog" },
|
|
806
|
-
});
|
|
807
|
-
db = setup.db;
|
|
808
|
-
|
|
809
|
-
await tool.execute({ to: "Bob", type: "finding", content: "From Rex" }, setup.context);
|
|
810
|
-
|
|
811
|
-
const row = db.prepare("SELECT sender_id, sender_name, sender_type FROM messages").get() as {
|
|
812
|
-
sender_id: string;
|
|
813
|
-
sender_name: string;
|
|
814
|
-
sender_type: string;
|
|
815
|
-
};
|
|
816
|
-
expect(row.sender_id).toBe("rex-id");
|
|
817
|
-
expect(row.sender_name).toBe("Rex");
|
|
818
|
-
expect(row.sender_type).toBe("arion");
|
|
819
|
-
});
|
|
820
|
-
|
|
821
|
-
it("uses the runtime-bound inbox identity when no arion context is active", async () => {
|
|
822
|
-
const setup = createRealContext();
|
|
823
|
-
db = setup.db;
|
|
824
|
-
|
|
825
|
-
await tool.execute({ to: "Bob", type: "finding", content: "From leader" }, setup.context);
|
|
826
|
-
|
|
827
|
-
const row = db.prepare("SELECT sender_id, sender_name, sender_type FROM messages").get() as {
|
|
828
|
-
sender_id: string;
|
|
829
|
-
sender_name: string;
|
|
830
|
-
sender_type: string;
|
|
831
|
-
};
|
|
832
|
-
expect(row.sender_id).toBe("recipient-1");
|
|
833
|
-
expect(row.sender_name).toBe("ARIA");
|
|
834
|
-
expect(row.sender_type).toBe("leader");
|
|
835
|
-
});
|
|
836
|
-
|
|
837
|
-
it("uses senderType from context when no arion (worker propagation)", async () => {
|
|
838
|
-
const setup = createRealContext();
|
|
839
|
-
db = setup.db;
|
|
840
|
-
// Simulate a worker context: no arion, senderType explicitly set to "worker"
|
|
841
|
-
setup.context.senderType = "worker";
|
|
842
|
-
|
|
843
|
-
await tool.execute(
|
|
844
|
-
{ to: "Leader", type: "progress", content: "Task 50% done" },
|
|
845
|
-
setup.context,
|
|
846
|
-
);
|
|
847
|
-
|
|
848
|
-
const row = db.prepare("SELECT sender_type FROM messages").get() as {
|
|
849
|
-
sender_type: string;
|
|
850
|
-
};
|
|
851
|
-
expect(row.sender_type).toBe("worker");
|
|
852
|
-
});
|
|
853
|
-
|
|
854
|
-
it("includes replyTo and correlationId when provided", async () => {
|
|
855
|
-
const setup = createRealContext();
|
|
856
|
-
db = setup.db;
|
|
857
|
-
|
|
858
|
-
await tool.execute(
|
|
859
|
-
{
|
|
860
|
-
to: "Bob",
|
|
861
|
-
type: "answer",
|
|
862
|
-
content: "Yes",
|
|
863
|
-
replyTo: "msg-abc",
|
|
864
|
-
correlationId: "quest-xyz",
|
|
865
|
-
},
|
|
866
|
-
setup.context,
|
|
867
|
-
);
|
|
868
|
-
|
|
869
|
-
const row = db.prepare("SELECT reply_to, correlation_id FROM messages").get() as {
|
|
870
|
-
reply_to: string;
|
|
871
|
-
correlation_id: string;
|
|
872
|
-
};
|
|
873
|
-
expect(row.reply_to).toBe("msg-abc");
|
|
874
|
-
expect(row.correlation_id).toBe("quest-xyz");
|
|
875
|
-
});
|
|
876
|
-
|
|
877
|
-
it("defaults priority to 2 when not specified", async () => {
|
|
878
|
-
const setup = createRealContext();
|
|
879
|
-
db = setup.db;
|
|
880
|
-
|
|
881
|
-
await tool.execute(
|
|
882
|
-
{ to: "Bob", type: "finding", content: "Default priority" },
|
|
883
|
-
setup.context,
|
|
884
|
-
);
|
|
885
|
-
|
|
886
|
-
const row = db.prepare("SELECT priority FROM messages").get() as { priority: number };
|
|
887
|
-
expect(row.priority).toBe(2);
|
|
888
|
-
});
|
|
889
|
-
|
|
890
|
-
it("respects explicit priority", async () => {
|
|
891
|
-
const setup = createRealContext();
|
|
892
|
-
db = setup.db;
|
|
893
|
-
|
|
894
|
-
await tool.execute(
|
|
895
|
-
{ to: "Bob", type: "error", content: "Critical!", priority: 0 },
|
|
896
|
-
setup.context,
|
|
897
|
-
);
|
|
898
|
-
|
|
899
|
-
const row = db.prepare("SELECT priority FROM messages").get() as { priority: number };
|
|
900
|
-
expect(row.priority).toBe(0);
|
|
901
|
-
});
|
|
902
|
-
|
|
903
|
-
it("result data includes message id, to, and type", async () => {
|
|
904
|
-
const setup = createRealContext();
|
|
905
|
-
db = setup.db;
|
|
906
|
-
|
|
907
|
-
const result = await tool.execute(
|
|
908
|
-
{ to: "Bob", type: "directive", content: "Do this" },
|
|
909
|
-
setup.context,
|
|
910
|
-
);
|
|
911
|
-
|
|
912
|
-
expect(result.data).toHaveProperty("id");
|
|
913
|
-
expect(result.data).toHaveProperty("to", "Bob");
|
|
914
|
-
expect(result.data).toHaveProperty("type", "directive");
|
|
915
|
-
|
|
916
|
-
// And the id in result.data should match what's in the DB
|
|
917
|
-
const row = db.prepare("SELECT id FROM messages").get() as { id: string };
|
|
918
|
-
expect((result.data as Record<string, unknown>).id).toBe(row.id);
|
|
919
|
-
});
|
|
920
|
-
});
|
|
921
|
-
|
|
922
|
-
// ── check_messages ──────────────────────────────────────────────────────
|
|
923
|
-
|
|
924
|
-
describe("check_messages", () => {
|
|
925
|
-
const tool = findTool("check_messages");
|
|
926
|
-
let db: DatabaseType;
|
|
927
|
-
|
|
928
|
-
afterEach(() => {
|
|
929
|
-
db?.close();
|
|
930
|
-
});
|
|
931
|
-
|
|
932
|
-
it("returns error when messageStore is missing", async () => {
|
|
933
|
-
const ctx = createBareContext();
|
|
934
|
-
const result = await tool.execute({}, ctx);
|
|
935
|
-
|
|
936
|
-
expect(result.success).toBe(false);
|
|
937
|
-
expect(result.message).toMatch(/[Mm]essage store not available/);
|
|
938
|
-
});
|
|
939
|
-
|
|
940
|
-
it("returns unread messages from real DB", async () => {
|
|
941
|
-
const setup = createRealContext();
|
|
942
|
-
db = setup.db;
|
|
943
|
-
|
|
944
|
-
// Insert messages directly into the DB
|
|
945
|
-
const now = Date.now();
|
|
946
|
-
insertMessage(db, {
|
|
947
|
-
id: "msg-1",
|
|
948
|
-
sender_name: "Alice",
|
|
949
|
-
type: "finding",
|
|
950
|
-
content: "Found it",
|
|
951
|
-
direction: "received",
|
|
952
|
-
created_at: now,
|
|
953
|
-
});
|
|
954
|
-
insertMessage(db, {
|
|
955
|
-
id: "msg-2",
|
|
956
|
-
sender_name: "Bob",
|
|
957
|
-
type: "question",
|
|
958
|
-
content: "How?",
|
|
959
|
-
direction: "received",
|
|
960
|
-
created_at: now + 100,
|
|
961
|
-
});
|
|
962
|
-
|
|
963
|
-
const result = await tool.execute({}, setup.context);
|
|
964
|
-
|
|
965
|
-
expect(result.success).toBe(true);
|
|
966
|
-
expect(result.message).toMatch(/2 message\(s\) received/);
|
|
967
|
-
expect(result.data).toHaveProperty("messages");
|
|
968
|
-
expect((result.data as { messages: unknown[] }).messages).toHaveLength(2);
|
|
969
|
-
});
|
|
970
|
-
|
|
971
|
-
it("auto-marks messages as read in the real DB", async () => {
|
|
972
|
-
const setup = createRealContext();
|
|
973
|
-
db = setup.db;
|
|
974
|
-
|
|
975
|
-
const now = Date.now();
|
|
976
|
-
insertMessage(db, {
|
|
977
|
-
id: "msg-1",
|
|
978
|
-
direction: "received",
|
|
979
|
-
created_at: now,
|
|
980
|
-
});
|
|
981
|
-
insertMessage(db, {
|
|
982
|
-
id: "msg-2",
|
|
983
|
-
direction: "received",
|
|
984
|
-
created_at: now + 100,
|
|
985
|
-
});
|
|
986
|
-
|
|
987
|
-
await tool.execute({}, setup.context);
|
|
988
|
-
|
|
989
|
-
// Verify read_at was set in the real DB
|
|
990
|
-
const rows = db.prepare("SELECT id, read_at FROM messages ORDER BY id").all() as Array<{
|
|
991
|
-
id: string;
|
|
992
|
-
read_at: number | null;
|
|
993
|
-
}>;
|
|
994
|
-
for (const row of rows) {
|
|
995
|
-
expect(row.read_at).not.toBeNull();
|
|
996
|
-
expect(row.read_at).toBeGreaterThan(0);
|
|
997
|
-
}
|
|
998
|
-
});
|
|
999
|
-
|
|
1000
|
-
it("returns 'No unread messages' when inbox is empty", async () => {
|
|
1001
|
-
const setup = createRealContext();
|
|
1002
|
-
db = setup.db;
|
|
1003
|
-
|
|
1004
|
-
// Empty DB — no messages at all
|
|
1005
|
-
const result = await tool.execute({}, setup.context);
|
|
1006
|
-
|
|
1007
|
-
expect(result.success).toBe(true);
|
|
1008
|
-
expect(result.message).toMatch(/[Nn]o unread messages/);
|
|
1009
|
-
expect((result.data as { messages: unknown[] }).messages).toEqual([]);
|
|
1010
|
-
});
|
|
1011
|
-
|
|
1012
|
-
it("uses search with filters when unreadOnly=false", async () => {
|
|
1013
|
-
const setup = createRealContext();
|
|
1014
|
-
db = setup.db;
|
|
1015
|
-
|
|
1016
|
-
const now = Date.now();
|
|
1017
|
-
insertMessage(db, {
|
|
1018
|
-
id: "msg-alice-finding",
|
|
1019
|
-
sender_name: "Alice",
|
|
1020
|
-
type: "finding",
|
|
1021
|
-
content: "Found a bug",
|
|
1022
|
-
direction: "received",
|
|
1023
|
-
created_at: now,
|
|
1024
|
-
});
|
|
1025
|
-
insertMessage(db, {
|
|
1026
|
-
id: "msg-bob-question",
|
|
1027
|
-
sender_name: "Bob",
|
|
1028
|
-
type: "question",
|
|
1029
|
-
content: "How does this work?",
|
|
1030
|
-
direction: "received",
|
|
1031
|
-
created_at: now + 100,
|
|
1032
|
-
});
|
|
1033
|
-
|
|
1034
|
-
const result = await tool.execute(
|
|
1035
|
-
{ unreadOnly: false, from: "Alice", type: "finding", limit: 10 },
|
|
1036
|
-
setup.context,
|
|
1037
|
-
);
|
|
1038
|
-
|
|
1039
|
-
expect(result.success).toBe(true);
|
|
1040
|
-
const messages = (result.data as { messages: Array<{ id: string }> }).messages;
|
|
1041
|
-
expect(messages).toHaveLength(1);
|
|
1042
|
-
expect(messages[0].id).toBe("msg-alice-finding");
|
|
1043
|
-
});
|
|
1044
|
-
|
|
1045
|
-
it("defaults limit to 20 (does not return more)", async () => {
|
|
1046
|
-
const setup = createRealContext();
|
|
1047
|
-
db = setup.db;
|
|
1048
|
-
|
|
1049
|
-
// Insert 25 unread messages
|
|
1050
|
-
const now = Date.now();
|
|
1051
|
-
for (let i = 0; i < 25; i++) {
|
|
1052
|
-
insertMessage(db, {
|
|
1053
|
-
id: `msg-limit-${i}`,
|
|
1054
|
-
direction: "received",
|
|
1055
|
-
created_at: now + i,
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
const result = await tool.execute({}, setup.context);
|
|
1060
|
-
|
|
1061
|
-
expect(result.success).toBe(true);
|
|
1062
|
-
const messages = (result.data as { messages: unknown[] }).messages;
|
|
1063
|
-
expect(messages).toHaveLength(20);
|
|
1064
|
-
});
|
|
1065
|
-
});
|
|
1066
|
-
|
|
1067
|
-
// ── search_messages ──────────────────────────────────────────────────────
|
|
1068
|
-
|
|
1069
|
-
describe("search_messages", () => {
|
|
1070
|
-
const tool = findTool("search_messages");
|
|
1071
|
-
let db: DatabaseType;
|
|
1072
|
-
|
|
1073
|
-
afterEach(() => {
|
|
1074
|
-
db?.close();
|
|
1075
|
-
});
|
|
1076
|
-
|
|
1077
|
-
it("returns error when messageStore is missing", async () => {
|
|
1078
|
-
const ctx = createBareContext();
|
|
1079
|
-
const result = await tool.execute({ query: "test" }, ctx);
|
|
1080
|
-
|
|
1081
|
-
expect(result.success).toBe(false);
|
|
1082
|
-
expect(result.message).toMatch(/[Mm]essage store not available/);
|
|
1083
|
-
});
|
|
1084
|
-
|
|
1085
|
-
it("searches by content in real DB", async () => {
|
|
1086
|
-
const setup = createRealContext();
|
|
1087
|
-
db = setup.db;
|
|
1088
|
-
|
|
1089
|
-
const now = Date.now();
|
|
1090
|
-
insertMessage(db, {
|
|
1091
|
-
id: "msg-deploy-1",
|
|
1092
|
-
content: "Deploying to production",
|
|
1093
|
-
direction: "sent",
|
|
1094
|
-
created_at: now,
|
|
1095
|
-
});
|
|
1096
|
-
insertMessage(db, {
|
|
1097
|
-
id: "msg-deploy-2",
|
|
1098
|
-
content: "Deploy succeeded",
|
|
1099
|
-
direction: "received",
|
|
1100
|
-
created_at: now + 100,
|
|
1101
|
-
});
|
|
1102
|
-
insertMessage(db, {
|
|
1103
|
-
id: "msg-other",
|
|
1104
|
-
content: "Reviewing the code",
|
|
1105
|
-
direction: "sent",
|
|
1106
|
-
created_at: now + 200,
|
|
1107
|
-
});
|
|
1108
|
-
|
|
1109
|
-
const result = await tool.execute({ query: "deploy" }, setup.context);
|
|
1110
|
-
|
|
1111
|
-
expect(result.success).toBe(true);
|
|
1112
|
-
// Case-insensitive LIKE match: "Deploying" and "Deploy" both match "deploy"
|
|
1113
|
-
// Actually SQLite LIKE is case-insensitive for ASCII
|
|
1114
|
-
const messages = (result.data as { messages: Array<{ id: string }> }).messages;
|
|
1115
|
-
expect(messages).toHaveLength(2);
|
|
1116
|
-
expect(messages.map((m) => m.id).sort()).toEqual(["msg-deploy-1", "msg-deploy-2"]);
|
|
1117
|
-
});
|
|
1118
|
-
|
|
1119
|
-
it("filters by sender, type, correlationId, and after", async () => {
|
|
1120
|
-
const setup = createRealContext();
|
|
1121
|
-
db = setup.db;
|
|
1122
|
-
|
|
1123
|
-
const now = Date.now();
|
|
1124
|
-
insertMessage(db, {
|
|
1125
|
-
id: "msg-rex-progress",
|
|
1126
|
-
sender_name: "Rex",
|
|
1127
|
-
type: "progress",
|
|
1128
|
-
content: "Parser work ongoing",
|
|
1129
|
-
correlation_id: "quest-42",
|
|
1130
|
-
direction: "received",
|
|
1131
|
-
created_at: now + 100,
|
|
1132
|
-
});
|
|
1133
|
-
insertMessage(db, {
|
|
1134
|
-
id: "msg-rex-finding",
|
|
1135
|
-
sender_name: "Rex",
|
|
1136
|
-
type: "finding",
|
|
1137
|
-
content: "Parser bug found",
|
|
1138
|
-
direction: "received",
|
|
1139
|
-
created_at: now + 200,
|
|
1140
|
-
});
|
|
1141
|
-
insertMessage(db, {
|
|
1142
|
-
id: "msg-alice-progress",
|
|
1143
|
-
sender_name: "Alice",
|
|
1144
|
-
type: "progress",
|
|
1145
|
-
content: "Parser refactored",
|
|
1146
|
-
direction: "sent",
|
|
1147
|
-
created_at: now + 300,
|
|
1148
|
-
});
|
|
1149
|
-
|
|
1150
|
-
const result = await tool.execute(
|
|
1151
|
-
{
|
|
1152
|
-
query: "Parser",
|
|
1153
|
-
from: "Rex",
|
|
1154
|
-
type: "progress",
|
|
1155
|
-
correlationId: "quest-42",
|
|
1156
|
-
limit: 5,
|
|
1157
|
-
},
|
|
1158
|
-
setup.context,
|
|
1159
|
-
);
|
|
1160
|
-
|
|
1161
|
-
expect(result.success).toBe(true);
|
|
1162
|
-
const messages = (result.data as { messages: Array<{ id: string }> }).messages;
|
|
1163
|
-
expect(messages).toHaveLength(1);
|
|
1164
|
-
expect(messages[0].id).toBe("msg-rex-progress");
|
|
1165
|
-
});
|
|
1166
|
-
|
|
1167
|
-
it("defaults limit to 20", async () => {
|
|
1168
|
-
const setup = createRealContext();
|
|
1169
|
-
db = setup.db;
|
|
1170
|
-
|
|
1171
|
-
// Insert 25 messages all matching the query
|
|
1172
|
-
const now = Date.now();
|
|
1173
|
-
for (let i = 0; i < 25; i++) {
|
|
1174
|
-
insertMessage(db, {
|
|
1175
|
-
id: `msg-search-${i}`,
|
|
1176
|
-
content: "searchable content here",
|
|
1177
|
-
direction: "sent",
|
|
1178
|
-
created_at: now + i,
|
|
1179
|
-
});
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
const result = await tool.execute({ query: "searchable" }, setup.context);
|
|
1183
|
-
|
|
1184
|
-
const messages = (result.data as { messages: unknown[] }).messages;
|
|
1185
|
-
expect(messages).toHaveLength(20);
|
|
1186
|
-
});
|
|
1187
|
-
|
|
1188
|
-
it("returns found message count in message", async () => {
|
|
1189
|
-
const setup = createRealContext();
|
|
1190
|
-
db = setup.db;
|
|
1191
|
-
|
|
1192
|
-
const now = Date.now();
|
|
1193
|
-
insertMessage(db, { id: "s1", content: "test data A", created_at: now, direction: "sent" });
|
|
1194
|
-
insertMessage(db, {
|
|
1195
|
-
id: "s2",
|
|
1196
|
-
content: "test data B",
|
|
1197
|
-
created_at: now + 1,
|
|
1198
|
-
direction: "sent",
|
|
1199
|
-
});
|
|
1200
|
-
insertMessage(db, {
|
|
1201
|
-
id: "s3",
|
|
1202
|
-
content: "test data C",
|
|
1203
|
-
created_at: now + 2,
|
|
1204
|
-
direction: "sent",
|
|
1205
|
-
});
|
|
1206
|
-
|
|
1207
|
-
const result = await tool.execute({ query: "test data" }, setup.context);
|
|
1208
|
-
|
|
1209
|
-
expect(result.message).toMatch(/3 message\(s\) found/);
|
|
1210
|
-
expect((result.data as { messages: unknown[] }).messages).toHaveLength(3);
|
|
1211
|
-
});
|
|
1212
|
-
|
|
1213
|
-
it("returns empty results gracefully", async () => {
|
|
1214
|
-
const setup = createRealContext();
|
|
1215
|
-
db = setup.db;
|
|
1216
|
-
|
|
1217
|
-
const result = await tool.execute({ query: "nonexistent" }, setup.context);
|
|
1218
|
-
|
|
1219
|
-
expect(result.success).toBe(true);
|
|
1220
|
-
expect(result.message).toMatch(/0 message\(s\) found/);
|
|
1221
|
-
});
|
|
1222
|
-
});
|
|
1223
|
-
|
|
1224
|
-
// ── get_thread ──────────────────────────────────────────────────────────
|
|
1225
|
-
|
|
1226
|
-
describe("get_thread", () => {
|
|
1227
|
-
const tool = findTool("get_thread");
|
|
1228
|
-
let db: DatabaseType;
|
|
1229
|
-
|
|
1230
|
-
afterEach(() => {
|
|
1231
|
-
db?.close();
|
|
1232
|
-
});
|
|
1233
|
-
|
|
1234
|
-
it("returns error when messageStore is missing", async () => {
|
|
1235
|
-
const ctx = createBareContext();
|
|
1236
|
-
const result = await tool.execute({ messageId: "msg-1" }, ctx);
|
|
1237
|
-
|
|
1238
|
-
expect(result.success).toBe(false);
|
|
1239
|
-
expect(result.message).toMatch(/[Mm]essage store not available/);
|
|
1240
|
-
});
|
|
1241
|
-
|
|
1242
|
-
it("reconstructs a thread from real DB with reply_to chains", async () => {
|
|
1243
|
-
const setup = createRealContext();
|
|
1244
|
-
db = setup.db;
|
|
1245
|
-
|
|
1246
|
-
const now = Date.now();
|
|
1247
|
-
// Create a 3-message thread: A -> B -> C
|
|
1248
|
-
insertMessage(db, {
|
|
1249
|
-
id: "msg-1",
|
|
1250
|
-
sender_name: "Alice",
|
|
1251
|
-
type: "question",
|
|
1252
|
-
content: "How?",
|
|
1253
|
-
reply_to: null,
|
|
1254
|
-
direction: "sent",
|
|
1255
|
-
created_at: now,
|
|
1256
|
-
});
|
|
1257
|
-
insertMessage(db, {
|
|
1258
|
-
id: "msg-2",
|
|
1259
|
-
sender_name: "Bob",
|
|
1260
|
-
type: "answer",
|
|
1261
|
-
content: "Like this",
|
|
1262
|
-
reply_to: "msg-1",
|
|
1263
|
-
direction: "received",
|
|
1264
|
-
created_at: now + 100,
|
|
1265
|
-
});
|
|
1266
|
-
insertMessage(db, {
|
|
1267
|
-
id: "msg-3",
|
|
1268
|
-
sender_name: "Alice",
|
|
1269
|
-
type: "answer",
|
|
1270
|
-
content: "Thanks!",
|
|
1271
|
-
reply_to: "msg-2",
|
|
1272
|
-
direction: "sent",
|
|
1273
|
-
created_at: now + 200,
|
|
1274
|
-
});
|
|
1275
|
-
|
|
1276
|
-
// Get thread starting from the leaf
|
|
1277
|
-
const result = await tool.execute({ messageId: "msg-3" }, setup.context);
|
|
1278
|
-
|
|
1279
|
-
expect(result.success).toBe(true);
|
|
1280
|
-
expect(result.message).toMatch(/3 message\(s\)/);
|
|
1281
|
-
const thread = (result.data as { thread: Array<{ id: string }> }).thread;
|
|
1282
|
-
expect(thread).toHaveLength(3);
|
|
1283
|
-
expect(thread[0].id).toBe("msg-1");
|
|
1284
|
-
expect(thread[1].id).toBe("msg-2");
|
|
1285
|
-
expect(thread[2].id).toBe("msg-3");
|
|
1286
|
-
});
|
|
1287
|
-
|
|
1288
|
-
it("returns error for empty thread (nonexistent message)", async () => {
|
|
1289
|
-
const setup = createRealContext();
|
|
1290
|
-
db = setup.db;
|
|
1291
|
-
|
|
1292
|
-
const result = await tool.execute({ messageId: "nonexistent-msg" }, setup.context);
|
|
1293
|
-
|
|
1294
|
-
expect(result.success).toBe(false);
|
|
1295
|
-
expect(result.message).toMatch(/[Nn]o thread found/);
|
|
1296
|
-
});
|
|
1297
|
-
|
|
1298
|
-
it("error message includes the message ID", async () => {
|
|
1299
|
-
const setup = createRealContext();
|
|
1300
|
-
db = setup.db;
|
|
1301
|
-
|
|
1302
|
-
const result = await tool.execute({ messageId: "xyz-123" }, setup.context);
|
|
1303
|
-
|
|
1304
|
-
expect(result.message).toContain("xyz-123");
|
|
1305
|
-
});
|
|
1306
|
-
|
|
1307
|
-
it("returns single message thread when no replies exist", async () => {
|
|
1308
|
-
const setup = createRealContext();
|
|
1309
|
-
db = setup.db;
|
|
1310
|
-
|
|
1311
|
-
insertMessage(db, {
|
|
1312
|
-
id: "msg-solo",
|
|
1313
|
-
content: "Solo message",
|
|
1314
|
-
reply_to: null,
|
|
1315
|
-
direction: "received",
|
|
1316
|
-
created_at: Date.now(),
|
|
1317
|
-
});
|
|
1318
|
-
|
|
1319
|
-
const result = await tool.execute({ messageId: "msg-solo" }, setup.context);
|
|
1320
|
-
|
|
1321
|
-
expect(result.success).toBe(true);
|
|
1322
|
-
const thread = (result.data as { thread: Array<{ id: string }> }).thread;
|
|
1323
|
-
expect(thread).toHaveLength(1);
|
|
1324
|
-
expect(thread[0].id).toBe("msg-solo");
|
|
1325
|
-
});
|
|
1326
|
-
|
|
1327
|
-
it("finds full thread from any message in the chain", async () => {
|
|
1328
|
-
const setup = createRealContext();
|
|
1329
|
-
db = setup.db;
|
|
1330
|
-
|
|
1331
|
-
const now = Date.now();
|
|
1332
|
-
insertMessage(db, {
|
|
1333
|
-
id: "msg-root",
|
|
1334
|
-
content: "Root",
|
|
1335
|
-
reply_to: null,
|
|
1336
|
-
direction: "sent",
|
|
1337
|
-
created_at: now,
|
|
1338
|
-
});
|
|
1339
|
-
insertMessage(db, {
|
|
1340
|
-
id: "msg-reply",
|
|
1341
|
-
content: "Reply",
|
|
1342
|
-
reply_to: "msg-root",
|
|
1343
|
-
direction: "received",
|
|
1344
|
-
created_at: now + 100,
|
|
1345
|
-
});
|
|
1346
|
-
|
|
1347
|
-
// Query from the root (not the leaf)
|
|
1348
|
-
const result = await tool.execute({ messageId: "msg-root" }, setup.context);
|
|
1349
|
-
|
|
1350
|
-
expect(result.success).toBe(true);
|
|
1351
|
-
const thread = (result.data as { thread: Array<{ id: string }> }).thread;
|
|
1352
|
-
expect(thread).toHaveLength(2);
|
|
1353
|
-
expect(thread[0].id).toBe("msg-root");
|
|
1354
|
-
expect(thread[1].id).toBe("msg-reply");
|
|
1355
|
-
});
|
|
1356
|
-
});
|
|
1357
|
-
|
|
1358
|
-
// ── AgentRunner path integration: nodeId-only context ─────────────────
|
|
1359
|
-
|
|
1360
|
-
describe("AgentRunner inbox path (nodeId only, no inboxAddress)", () => {
|
|
1361
|
-
/**
|
|
1362
|
-
* This describe block tests the EXACT ToolContext shape that the AgentRunner
|
|
1363
|
-
* path produces: nodeId is set, inboxAddress is NOT set. This exercises the
|
|
1364
|
-
* resolveInboxAddress() fallback from context.nodeId.
|
|
1365
|
-
*
|
|
1366
|
-
* The standard createRealContext() sets context.inboxAddress directly, which
|
|
1367
|
-
* bypasses resolveInboxAddress() entirely. These tests prove messages actually
|
|
1368
|
-
* route through the nodeId path.
|
|
1369
|
-
*/
|
|
1370
|
-
|
|
1371
|
-
function createAgentRunnerContext(opts?: {
|
|
1372
|
-
db?: DatabaseType;
|
|
1373
|
-
mailbox?: ToolContext["mailbox"];
|
|
1374
|
-
networkControl?: ToolContext["networkControl"];
|
|
1375
|
-
}): { context: ToolContext; db: DatabaseType; store: MessageStore } {
|
|
1376
|
-
const db = opts?.db ?? createTestDb();
|
|
1377
|
-
const store = new MessageStore(db);
|
|
1378
|
-
const nodeId = "node-test-runner";
|
|
1379
|
-
// Deliberately NO inboxAddress — only nodeId, mimicking tool-executor.ts
|
|
1380
|
-
const context = {
|
|
1381
|
-
workingDir: "/tmp",
|
|
1382
|
-
env: {},
|
|
1383
|
-
confirm: async () => true,
|
|
1384
|
-
messageStore: store,
|
|
1385
|
-
mailbox: opts?.mailbox,
|
|
1386
|
-
networkControl: opts?.networkControl,
|
|
1387
|
-
nodeId,
|
|
1388
|
-
} as ToolContext;
|
|
1389
|
-
return { context, db, store };
|
|
1390
|
-
}
|
|
1391
|
-
|
|
1392
|
-
let db: DatabaseType;
|
|
1393
|
-
|
|
1394
|
-
afterEach(() => {
|
|
1395
|
-
db?.close();
|
|
1396
|
-
});
|
|
1397
|
-
|
|
1398
|
-
it("send_message delivers through nodeId-only context (AgentRunner path)", async () => {
|
|
1399
|
-
const tool = findTool("send_message");
|
|
1400
|
-
const setup = createAgentRunnerContext();
|
|
1401
|
-
db = setup.db;
|
|
1402
|
-
|
|
1403
|
-
const result = await tool.execute(
|
|
1404
|
-
{ to: "Bob", type: "finding", content: "Hello from runner" },
|
|
1405
|
-
setup.context,
|
|
1406
|
-
);
|
|
1407
|
-
|
|
1408
|
-
expect(result.success).toBe(true);
|
|
1409
|
-
|
|
1410
|
-
// Verify the message is in the DB with correct inbox address
|
|
1411
|
-
const rows = db.prepare("SELECT * FROM messages").all() as Array<Record<string, unknown>>;
|
|
1412
|
-
expect(rows).toHaveLength(1);
|
|
1413
|
-
expect(rows[0].content).toBe("Hello from runner");
|
|
1414
|
-
expect(rows[0].direction).toBe("sent");
|
|
1415
|
-
expect(rows[0].inbox_address_kind).toBe("node");
|
|
1416
|
-
expect(rows[0].inbox_address_id).toBe("node-test-runner");
|
|
1417
|
-
});
|
|
1418
|
-
|
|
1419
|
-
it("check_messages reads through nodeId-only context (AgentRunner path)", async () => {
|
|
1420
|
-
const tool = findTool("check_messages");
|
|
1421
|
-
const setup = createAgentRunnerContext();
|
|
1422
|
-
db = setup.db;
|
|
1423
|
-
|
|
1424
|
-
// Insert a message addressed to this node
|
|
1425
|
-
insertMessage(db, {
|
|
1426
|
-
id: "msg-runner-1",
|
|
1427
|
-
sender_name: "Alice",
|
|
1428
|
-
type: "finding",
|
|
1429
|
-
content: "Found via runner",
|
|
1430
|
-
direction: "received",
|
|
1431
|
-
created_at: Date.now(),
|
|
1432
|
-
inbox_address_kind: "node",
|
|
1433
|
-
inbox_address_id: "node-test-runner",
|
|
1434
|
-
});
|
|
1435
|
-
|
|
1436
|
-
const result = await tool.execute({}, setup.context);
|
|
1437
|
-
|
|
1438
|
-
expect(result.success).toBe(true);
|
|
1439
|
-
const messages = (result.data as { messages: Array<{ id: string }> }).messages;
|
|
1440
|
-
expect(messages).toHaveLength(1);
|
|
1441
|
-
expect(messages[0].id).toBe("msg-runner-1");
|
|
1442
|
-
});
|
|
1443
|
-
|
|
1444
|
-
it("throws descriptive error when NEITHER nodeId NOR inboxAddress is set", async () => {
|
|
1445
|
-
const tool = findTool("send_message");
|
|
1446
|
-
const dbt = createTestDb();
|
|
1447
|
-
const store = new MessageStore(dbt);
|
|
1448
|
-
const context = {
|
|
1449
|
-
workingDir: "/tmp",
|
|
1450
|
-
env: {},
|
|
1451
|
-
confirm: async () => true,
|
|
1452
|
-
messageStore: store,
|
|
1453
|
-
// NO nodeId, NO inboxAddress — this is the wiring bug scenario
|
|
1454
|
-
} as ToolContext;
|
|
1455
|
-
|
|
1456
|
-
try {
|
|
1457
|
-
await tool.execute({ to: "Bob", type: "finding", content: "This should fail" }, context);
|
|
1458
|
-
expect.fail("Should have thrown");
|
|
1459
|
-
} catch (e) {
|
|
1460
|
-
expect((e as Error).message).toContain("Inbox address not available");
|
|
1461
|
-
expect((e as Error).message).toContain("wiring bug");
|
|
1462
|
-
expect((e as Error).message).toContain("tool-executor.ts");
|
|
1463
|
-
} finally {
|
|
1464
|
-
dbt.close();
|
|
1465
|
-
}
|
|
1466
|
-
});
|
|
1467
|
-
});
|
|
1468
|
-
});
|