@niwa-labs/messages 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/package.json +36 -4
- package/src/actions.ts +48 -0
- package/src/cockpit/index.tsx +182 -0
- package/src/envelope.ts +167 -0
- package/src/index.ts +237 -0
- package/src/records-entry.ts +36 -0
- package/src/records.ts +141 -0
- package/src/render.ts +63 -0
- package/src/text.ts +10 -0
- package/test/cockpit-text.test.ts +22 -0
- package/test/deliver.test.ts +296 -0
- package/test/json-values.test.ts +55 -0
- package/test/list.test.ts +36 -0
- package/test/sender.test.ts +38 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// The delivery of a message to a session: the envelope is unfolded into text, and the text
|
|
2
|
+
// goes into the session.
|
|
3
|
+
//
|
|
4
|
+
// The scenario runs in one process, against a real host on a temporary home:
|
|
5
|
+
//
|
|
6
|
+
// 1. a message to a session reaches the session, once, as the text of the envelope
|
|
7
|
+
// 2. the receipt is recorded on the message
|
|
8
|
+
// 3. a message to another address reaches no session at all
|
|
9
|
+
// 4. a host that is closed while the send is in flight, and started again, does not
|
|
10
|
+
// send the message a second time
|
|
11
|
+
//
|
|
12
|
+
// Only the public API is used: the entries of core, messages and sessions, and testing.
|
|
13
|
+
|
|
14
|
+
import { expect, test } from "bun:test";
|
|
15
|
+
|
|
16
|
+
import { CORE_TYPES, seq } from "@niwa-labs/core";
|
|
17
|
+
import type { RecordTypeName, StoredRecord } from "@niwa-labs/core";
|
|
18
|
+
import { createNiwa, eventually, harnessId, harnessKind, machineId, sessionId, sessions, tempHome } from "@niwa-labs/testing";
|
|
19
|
+
import type {
|
|
20
|
+
AskAnswer,
|
|
21
|
+
AskOptions,
|
|
22
|
+
DeliveryReceipt,
|
|
23
|
+
Harness,
|
|
24
|
+
Host,
|
|
25
|
+
LifeEvent,
|
|
26
|
+
SessionControl,
|
|
27
|
+
SessionId,
|
|
28
|
+
SessionState,
|
|
29
|
+
StartOptions,
|
|
30
|
+
} from "@niwa-labs/testing";
|
|
31
|
+
|
|
32
|
+
import { MessageDelivered, MessageSent, messageReceive, messageSend, messages } from "../src/index.ts";
|
|
33
|
+
|
|
34
|
+
/** The harness the fake control side serves. */
|
|
35
|
+
const HARNESS = "fake-harness";
|
|
36
|
+
|
|
37
|
+
/** The session the message goes to. */
|
|
38
|
+
const SESSION = "session-1";
|
|
39
|
+
|
|
40
|
+
/** The text of the message. */
|
|
41
|
+
const TEXT = "Move the parsing into its own module.";
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The fake control side.
|
|
45
|
+
*
|
|
46
|
+
* It records what it was sent, and it can be made to block: a send that never returns is a
|
|
47
|
+
* call that was cut off, which is what the restart is about.
|
|
48
|
+
*/
|
|
49
|
+
class FakeControl implements SessionControl {
|
|
50
|
+
/** The texts that were sent into the session, in order. */
|
|
51
|
+
readonly sent: string[] = [];
|
|
52
|
+
/** The sessions the texts were sent to, in order. */
|
|
53
|
+
readonly sentTo: SessionId[] = [];
|
|
54
|
+
/** How many times a send was begun, blocked or not. */
|
|
55
|
+
entered = 0;
|
|
56
|
+
/** Whether a send blocks instead of answering. */
|
|
57
|
+
block = false;
|
|
58
|
+
|
|
59
|
+
async start(): Promise<{ session: SessionId }> {
|
|
60
|
+
return { session: sessionId(harnessId(HARNESS), SESSION) };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async resume(): Promise<void> {}
|
|
64
|
+
|
|
65
|
+
async send(id: SessionId, message: string): Promise<DeliveryReceipt> {
|
|
66
|
+
this.entered += 1;
|
|
67
|
+
this.sent.push(message);
|
|
68
|
+
this.sentTo.push(id);
|
|
69
|
+
if (this.block) await new Promise<void>(() => {});
|
|
70
|
+
return { status: "started", turn: 1 };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async interrupt(): Promise<void> {}
|
|
74
|
+
|
|
75
|
+
async ask<T>(_id: SessionId, _options: AskOptions<T>): Promise<AskAnswer<T>> {
|
|
76
|
+
throw new Error("the fake control side does not ask");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async *life(): AsyncIterable<LifeEvent> {
|
|
80
|
+
// The signs of life are not what this test is about.
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async state(): Promise<SessionState> {
|
|
84
|
+
return "idle";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The installation the fake control side serves. */
|
|
89
|
+
function installation(): Harness {
|
|
90
|
+
return {
|
|
91
|
+
id: harnessId(HARNESS),
|
|
92
|
+
kind: harnessKind("fake-kind"),
|
|
93
|
+
machine: machineId("machine-1"),
|
|
94
|
+
home: "/tmp/fake-home",
|
|
95
|
+
sides: {
|
|
96
|
+
control: {
|
|
97
|
+
start: true,
|
|
98
|
+
resume: true,
|
|
99
|
+
send: true,
|
|
100
|
+
interrupt: false,
|
|
101
|
+
ask: false,
|
|
102
|
+
structuredOutput: false, launch: { model: false, effort: [], modes: [] },
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A host with the messages module and the sessions module over the fake. */
|
|
109
|
+
async function serve(control: FakeControl, home: string): Promise<Host> {
|
|
110
|
+
const niwa = createNiwa({ home });
|
|
111
|
+
niwa.use(messages({ bind: () => undefined }));
|
|
112
|
+
niwa.use(sessions({ harnesses: [{ instance: installation(), control }] }));
|
|
113
|
+
await niwa.serve();
|
|
114
|
+
return niwa;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The records of one type the host holds. */
|
|
118
|
+
async function recordsOf(niwa: Host, type: RecordTypeName): Promise<StoredRecord[]> {
|
|
119
|
+
return niwa.records.read({ types: [type] });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A message to the session of the fake. */
|
|
123
|
+
function toSession() {
|
|
124
|
+
return {
|
|
125
|
+
from: { kind: "agent" as const, id: "rin" },
|
|
126
|
+
to: { kind: "session" as const, harness: HARNESS, id: SESSION },
|
|
127
|
+
body: { type: "assignment" as const, text: TEXT },
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
test("a message to a session is delivered as text, and the receipt is recorded on the message", async () => {
|
|
132
|
+
const home = await tempHome();
|
|
133
|
+
const control = new FakeControl();
|
|
134
|
+
const niwa = await serve(control, home.path);
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await niwa.call(messageSend(), toSession());
|
|
138
|
+
|
|
139
|
+
// The session got the text of the envelope, once.
|
|
140
|
+
await eventually(() => control.sent.length === 1, 5_000, "the message should reach the session");
|
|
141
|
+
const text = control.sent[0] ?? "";
|
|
142
|
+
expect(text).toContain("From: agent rin");
|
|
143
|
+
expect(text).toContain(`To: session ${HARNESS}/${SESSION}`);
|
|
144
|
+
expect(text).toContain("Kind: assignment");
|
|
145
|
+
expect(text).toContain(TEXT);
|
|
146
|
+
expect(control.entered).toBe(1);
|
|
147
|
+
|
|
148
|
+
// The receipt is on the message.
|
|
149
|
+
await eventually(
|
|
150
|
+
async () => (await recordsOf(niwa, MessageDelivered.name)).length === 1,
|
|
151
|
+
5_000,
|
|
152
|
+
"the delivery should be recorded",
|
|
153
|
+
);
|
|
154
|
+
const delivered = await recordsOf(niwa, MessageDelivered.name);
|
|
155
|
+
const data = delivered[0]?.data as { message: { id: string }; receipt: DeliveryReceipt };
|
|
156
|
+
expect(data.receipt).toEqual({ status: "started", turn: 1 });
|
|
157
|
+
expect(data.message.id.length).toBeGreaterThan(0);
|
|
158
|
+
|
|
159
|
+
// A message to another address reaches no session.
|
|
160
|
+
await niwa.call(messageSend(), {
|
|
161
|
+
from: { kind: "agent", id: "rin" },
|
|
162
|
+
to: { kind: "agent", id: "executor" },
|
|
163
|
+
body: { type: "update", text: "no session here" },
|
|
164
|
+
});
|
|
165
|
+
await eventually(
|
|
166
|
+
async () => (await recordsOf(niwa, MessageSent.name)).length === 2,
|
|
167
|
+
5_000,
|
|
168
|
+
"the second message should be sent",
|
|
169
|
+
);
|
|
170
|
+
expect(control.sent.length).toBe(1);
|
|
171
|
+
expect(control.entered).toBe(1);
|
|
172
|
+
|
|
173
|
+
// Only the message to the session woke the delivery handler.
|
|
174
|
+
const admitted = await recordsOf(niwa, CORE_TYPES.invocationAdmitted);
|
|
175
|
+
expect(admitted.length).toBe(1);
|
|
176
|
+
expect((admitted[0]?.data as { handler: string }).handler).toBe("messages/message.sent:session");
|
|
177
|
+
} finally {
|
|
178
|
+
await niwa.close();
|
|
179
|
+
await home.cleanup();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("a message that came in from outside for a session is delivered once, and its receipt is recorded", async () => {
|
|
184
|
+
const home = await tempHome();
|
|
185
|
+
const control = new FakeControl();
|
|
186
|
+
const niwa = await serve(control, home.path);
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
await niwa.call(messageReceive(), { ...toSession(), from: { kind: "client", id: "outsider" } });
|
|
190
|
+
|
|
191
|
+
await eventually(() => control.sent.length === 1, 5_000, "the message should reach the session");
|
|
192
|
+
expect(control.sent[0]).toContain("From: client outsider");
|
|
193
|
+
expect(control.sent[0]).toContain(TEXT);
|
|
194
|
+
await eventually(
|
|
195
|
+
async () => (await recordsOf(niwa, MessageDelivered.name)).length === 1,
|
|
196
|
+
5_000,
|
|
197
|
+
"the delivery should be recorded",
|
|
198
|
+
);
|
|
199
|
+
expect(control.entered).toBe(1);
|
|
200
|
+
expect(control.sent.length).toBe(1);
|
|
201
|
+
} finally {
|
|
202
|
+
await niwa.close();
|
|
203
|
+
await home.cleanup();
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("a host that is closed while the send is in flight does not send the message again", async () => {
|
|
208
|
+
const home = await tempHome();
|
|
209
|
+
const control = new FakeControl();
|
|
210
|
+
control.block = true;
|
|
211
|
+
const first = await serve(control, home.path);
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
await first.call(messageSend(), toSession());
|
|
215
|
+
// The send is begun and never answers: the call is cut off.
|
|
216
|
+
await eventually(() => control.entered === 1, 5_000, "the send should be begun");
|
|
217
|
+
expect(control.sent.length).toBe(1);
|
|
218
|
+
} finally {
|
|
219
|
+
await first.close();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The second host replays the unfinished invocation. The call was cut off and cannot be
|
|
223
|
+
// made again, so the session is not sent the message twice.
|
|
224
|
+
const second = await serve(control, home.path);
|
|
225
|
+
try {
|
|
226
|
+
await eventually(
|
|
227
|
+
async () =>
|
|
228
|
+
(await recordsOf(second, CORE_TYPES.invocationState)).some(
|
|
229
|
+
(record) => (record.data as { state?: string }).state === "paused",
|
|
230
|
+
),
|
|
231
|
+
5_000,
|
|
232
|
+
"the replay of the cut-off call should pause the invocation",
|
|
233
|
+
);
|
|
234
|
+
expect(control.entered).toBe(1);
|
|
235
|
+
expect(control.sent.length).toBe(1);
|
|
236
|
+
} finally {
|
|
237
|
+
await second.close();
|
|
238
|
+
await home.cleanup();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("a delivery that was cut off is not made again when its paused step is retried", async () => {
|
|
243
|
+
const home = await tempHome();
|
|
244
|
+
const control = new FakeControl();
|
|
245
|
+
control.block = true;
|
|
246
|
+
const first = await serve(control, home.path);
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
await first.call(messageSend(), toSession());
|
|
250
|
+
await eventually(() => control.entered === 1, 5_000, "the send should be begun");
|
|
251
|
+
} finally {
|
|
252
|
+
await first.close();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// The replay pauses the cut-off step; the owner decides to try it again.
|
|
256
|
+
const second = await serve(control, home.path);
|
|
257
|
+
try {
|
|
258
|
+
const paused = async (): Promise<StoredRecord[]> =>
|
|
259
|
+
(await recordsOf(second, CORE_TYPES.invocationState)).filter((record) => (record.data as { state?: string }).state === "paused");
|
|
260
|
+
await eventually(async () => (await paused()).length === 1, 5_000, "the replay should pause the invocation");
|
|
261
|
+
const invocation = (((await paused())[0]?.data ?? {}) as { invocation?: string }).invocation ?? "";
|
|
262
|
+
control.block = false;
|
|
263
|
+
await second.call("niwa.invocation.resolve" as never, { invocation, choice: "retry" } as never);
|
|
264
|
+
|
|
265
|
+
// The sessions module knows the send by the message, so it says the outcome is not known
|
|
266
|
+
// instead of sending the text into the session a second time.
|
|
267
|
+
await eventually(async () => (await paused()).length === 2, 5_000, "the retry should pause again");
|
|
268
|
+
expect(control.entered).toBe(1);
|
|
269
|
+
expect(control.sent.length).toBe(1);
|
|
270
|
+
} finally {
|
|
271
|
+
await second.close();
|
|
272
|
+
await home.cleanup();
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("a message to a session whose harness has whitespace is refused, and any other address reaches exactly the session it names", async () => {
|
|
277
|
+
const home = await tempHome();
|
|
278
|
+
const control = new FakeControl();
|
|
279
|
+
const niwa = await serve(control, home.path);
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
// Joined and split again, this pair would name the harness "fake" and a native id "harness session-1".
|
|
283
|
+
const split = { ...toSession(), to: { kind: "session" as const, harness: "fake harness", id: SESSION } };
|
|
284
|
+
await expect(niwa.call(messageSend(), split)).rejects.toThrow();
|
|
285
|
+
expect(await recordsOf(niwa, MessageSent.name)).toEqual([]);
|
|
286
|
+
|
|
287
|
+
// A native id may hold whitespace: it comes back as it was given.
|
|
288
|
+
const spaced = { ...toSession(), to: { kind: "session" as const, harness: HARNESS, id: "team west 1234" } };
|
|
289
|
+
await niwa.call(messageSend(), spaced);
|
|
290
|
+
await eventually(() => control.entered === 1, 5_000, "the message should reach the session");
|
|
291
|
+
expect(control.sentTo).toEqual([{ harness: HARNESS, native: "team west 1234" }]);
|
|
292
|
+
} finally {
|
|
293
|
+
await niwa.close();
|
|
294
|
+
await home.cleanup();
|
|
295
|
+
}
|
|
296
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The values that are JSON by contract are JSON for the type and for the parse: a value that is not JSON is refused
|
|
2
|
+
// where it comes in, not carried on as `unknown`.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { JsonSchemaValue, JsonValueSchema, jsonOf } from "@niwa-labs/core";
|
|
7
|
+
import { ToolCallRecordSchema, ToolResultRecordSchema, manifestVersion } from "@niwa-labs/testing";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
|
|
10
|
+
import { MessageBody } from "../src/index.ts";
|
|
11
|
+
|
|
12
|
+
const BASE = { locator: "loc", incomplete: false };
|
|
13
|
+
|
|
14
|
+
test("a message carries JSON data and refuses data that is not JSON", () => {
|
|
15
|
+
expect(MessageBody.safeParse({ type: "update", text: "x", data: { a: [1, "b", null], c: { d: true } } }).success).toBe(true);
|
|
16
|
+
expect(MessageBody.safeParse({ type: "update", text: "x", data: { when: new Date() } }).success).toBe(false);
|
|
17
|
+
expect(MessageBody.safeParse({ type: "result", text: "x", data: { n: Number.NaN } }).success).toBe(false);
|
|
18
|
+
expect(MessageBody.safeParse({ type: "failure", text: "x", data: { gone: undefined } }).success).toBe(false);
|
|
19
|
+
expect(MessageBody.safeParse({ type: "answer", text: "x" }).success).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("the input of a tool call and the output of its result are JSON", () => {
|
|
23
|
+
const call = { kind: "tool-call", callId: "c1", name: "Read", category: "read", ...BASE };
|
|
24
|
+
expect(ToolCallRecordSchema.safeParse({ ...call, input: { path: "a" } }).success).toBe(true);
|
|
25
|
+
expect(ToolCallRecordSchema.safeParse({ ...call }).success).toBe(true);
|
|
26
|
+
expect(ToolCallRecordSchema.safeParse({ ...call, input: () => 1 }).success).toBe(false);
|
|
27
|
+
const result = { kind: "tool-result", callId: "c1", ...BASE };
|
|
28
|
+
expect(ToolResultRecordSchema.safeParse({ ...result, output: ["x"] }).success).toBe(true);
|
|
29
|
+
expect(ToolResultRecordSchema.safeParse({ ...result, output: { big: 10n } }).success).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a JSON Schema is what z.toJSONSchema makes, and a member that is not JSON is refused", () => {
|
|
33
|
+
const made = z.toJSONSchema(z.object({ text: z.string() }));
|
|
34
|
+
expect(JsonSchemaValue.parse(made)).toEqual(JSON.parse(JSON.stringify(made)));
|
|
35
|
+
expect(JsonSchemaValue.safeParse({ type: "object", default: undefined }).success).toBe(false);
|
|
36
|
+
expect(JsonSchemaValue.safeParse({ type: "object", default: new Date() }).success).toBe(false);
|
|
37
|
+
expect(JsonSchemaValue.safeParse([]).success).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("jsonOf leaves out what a journal leaves out and refuses what has no JSON form", () => {
|
|
41
|
+
expect(jsonOf({ a: 1, b: undefined, c: [undefined] })).toEqual({ a: 1, c: [null] });
|
|
42
|
+
expect(jsonOf(undefined)).toBeNull();
|
|
43
|
+
expect(jsonOf(new Date(0))).toBe("1970-01-01T00:00:00.000Z");
|
|
44
|
+
expect(() => jsonOf({ n: 1n })).toThrow();
|
|
45
|
+
expect(JsonValueSchema.safeParse(undefined).success).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("the version of a package is a non-empty text, and any other version is none", () => {
|
|
49
|
+
expect(manifestVersion({ name: "p", version: "1.2.3" })).toBe("1.2.3");
|
|
50
|
+
expect(manifestVersion({ name: "p", version: 3 })).toBeUndefined();
|
|
51
|
+
expect(manifestVersion({ name: "p", version: "" })).toBeUndefined();
|
|
52
|
+
expect(manifestVersion({ name: "p" })).toBeUndefined();
|
|
53
|
+
expect(manifestVersion(null)).toBeUndefined();
|
|
54
|
+
expect(manifestVersion("1.0.0")).toBeUndefined();
|
|
55
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// What the list says about a message that can be taken.
|
|
2
|
+
//
|
|
3
|
+
// Only the public API is used: the entries of core and messages, and testing.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { createNiwa, tempHome } from "@niwa-labs/testing";
|
|
8
|
+
import type { Host } from "@niwa-labs/testing";
|
|
9
|
+
|
|
10
|
+
import { messageListRef, messageSend, messages } from "../src/index.ts";
|
|
11
|
+
|
|
12
|
+
/** A host with the messages module alone. */
|
|
13
|
+
async function serve(home: string): Promise<Host> {
|
|
14
|
+
const niwa = createNiwa({ home });
|
|
15
|
+
niwa.use(messages({ bind: () => undefined }));
|
|
16
|
+
await niwa.serve();
|
|
17
|
+
return niwa;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TO = { kind: "agent" as const, id: "rin" };
|
|
21
|
+
|
|
22
|
+
test("a text with a lone carriage return or a line break does not break the list", async () => {
|
|
23
|
+
const home = await tempHome();
|
|
24
|
+
const niwa = await serve(home.path);
|
|
25
|
+
try {
|
|
26
|
+
const from = { kind: "owner" as const };
|
|
27
|
+
for (const text of ["a\rb", "first\nsecond", "\r\nsecond line", "plain"]) {
|
|
28
|
+
await niwa.call(messageSend(), { from, to: TO, body: { type: "update", text } });
|
|
29
|
+
}
|
|
30
|
+
const listed = await niwa.call(messageListRef, {});
|
|
31
|
+
expect(listed.items.map((item) => item.description)).toEqual(["update: a", "update: first", "update:", "update: plain"]);
|
|
32
|
+
} finally {
|
|
33
|
+
await niwa.close();
|
|
34
|
+
await home.cleanup();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// What the actions say about a message that cannot be taken.
|
|
2
|
+
//
|
|
3
|
+
// Only the public API is used: the entries of core and messages, and testing.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { createNiwa, tempHome } from "@niwa-labs/testing";
|
|
8
|
+
import type { Host } from "@niwa-labs/testing";
|
|
9
|
+
|
|
10
|
+
import { messageReceive, messageSend, messages } from "../src/index.ts";
|
|
11
|
+
|
|
12
|
+
/** A host with the messages module alone. */
|
|
13
|
+
async function serve(home: string): Promise<Host> {
|
|
14
|
+
const niwa = createNiwa({ home });
|
|
15
|
+
niwa.use(messages({ bind: () => undefined }));
|
|
16
|
+
await niwa.serve();
|
|
17
|
+
return niwa;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TO = { kind: "agent" as const, id: "rin" };
|
|
21
|
+
|
|
22
|
+
test("a message without a known sender is refused as not sent", async () => {
|
|
23
|
+
const home = await tempHome();
|
|
24
|
+
const niwa = await serve(home.path);
|
|
25
|
+
try {
|
|
26
|
+
for (const action of [messageSend(), messageReceive()]) {
|
|
27
|
+
const refused = await niwa.call(action, { to: TO, body: { type: "update", text: "hello" } }).then(
|
|
28
|
+
() => undefined,
|
|
29
|
+
(error: unknown) => error,
|
|
30
|
+
);
|
|
31
|
+
expect(refused).toBeInstanceOf(Error);
|
|
32
|
+
expect((refused as { effectPossible?: boolean }).effectPossible).toBe(false);
|
|
33
|
+
}
|
|
34
|
+
} finally {
|
|
35
|
+
await niwa.close();
|
|
36
|
+
await home.cleanup();
|
|
37
|
+
}
|
|
38
|
+
});
|