@niwa-labs/mcp 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 +2 -2
- package/package.json +13 -4
- package/src/bind.ts +37 -0
- package/src/index.ts +387 -0
- package/src/tools.ts +325 -0
- package/test/fake-host.ts +136 -0
- package/test/lifecycle.test.ts +77 -0
- package/test/tools.test.ts +296 -0
- package/test/watch.test.ts +169 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// The MCP tool filter: a session that was given some actions sees only those.
|
|
2
|
+
//
|
|
3
|
+
// The filter lives next to the catalog, so it is checked there: the catalog of a description
|
|
4
|
+
// with a visible list holds only the tools that list names, and the actions it holds are the
|
|
5
|
+
// ones those tools call. The observation actions of the core are actions like any other, so
|
|
6
|
+
// they are filtered by their action name.
|
|
7
|
+
|
|
8
|
+
import { expect, test } from "bun:test";
|
|
9
|
+
|
|
10
|
+
import { actionName } from "@niwa-labs/core";
|
|
11
|
+
import type { Description } from "@niwa-labs/core";
|
|
12
|
+
|
|
13
|
+
import { AjvJsonSchemaValidator } from "@modelcontextprotocol/sdk/validation/ajv-provider.js";
|
|
14
|
+
|
|
15
|
+
import { catalogOf, withAssignedTasks, withOpenQuestions } from "../src/tools.ts";
|
|
16
|
+
import type { Tool } from "../src/tools.ts";
|
|
17
|
+
|
|
18
|
+
/** Does a client that validates a call against the schema accept this value? */
|
|
19
|
+
function accepts(schema: unknown, value: unknown): boolean {
|
|
20
|
+
return new AjvJsonSchemaValidator().getValidator(schema as Record<string, unknown>)(value).valid;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const FINISH: Tool = {
|
|
24
|
+
name: "task_finish",
|
|
25
|
+
description: "Finish a task.",
|
|
26
|
+
inputSchema: { type: "object", properties: { task: { type: "string" }, result: {} }, required: ["task", "result"] },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const DESCRIPTION: Description = {
|
|
30
|
+
actions: [
|
|
31
|
+
{
|
|
32
|
+
name: actionName("session.send"),
|
|
33
|
+
description: "Sends a message into a session.",
|
|
34
|
+
recovery: "do-not-repeat",
|
|
35
|
+
effect: "write",
|
|
36
|
+
input: { type: "object", properties: { message: { type: "string" } } },
|
|
37
|
+
output: { type: "object", properties: {} },
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: actionName("task.create"),
|
|
41
|
+
description: "Creates a task.",
|
|
42
|
+
recovery: "repeat",
|
|
43
|
+
effect: "record",
|
|
44
|
+
input: { type: "object", properties: { title: { type: "string" } } },
|
|
45
|
+
output: { type: "object", properties: {} },
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: actionName("niwa.trace"),
|
|
49
|
+
description: "Everything that grew from a record.",
|
|
50
|
+
recovery: "repeat",
|
|
51
|
+
effect: "read",
|
|
52
|
+
input: { type: "object", properties: { id: { type: "string" } } },
|
|
53
|
+
output: { type: "object", properties: {} },
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
types: [],
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
test("without a visible list the catalog holds every action", () => {
|
|
60
|
+
const catalog = catalogOf(DESCRIPTION);
|
|
61
|
+
expect(catalog.tools.map((tool) => tool.name)).toEqual([
|
|
62
|
+
"session_send",
|
|
63
|
+
"task_create",
|
|
64
|
+
"niwa_trace",
|
|
65
|
+
]);
|
|
66
|
+
expect(catalog.actions.get("session_send")).toBe(actionName("session.send"));
|
|
67
|
+
expect(catalog.actions.get("task_create")).toBe(actionName("task.create"));
|
|
68
|
+
expect(catalog.actions.get("niwa_trace")).toBe(actionName("niwa.trace"));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("a visible list keeps only the actions it names, by their action name", () => {
|
|
72
|
+
const catalog = catalogOf(DESCRIPTION, ["session.send"]);
|
|
73
|
+
expect(catalog.tools.map((tool) => tool.name)).toEqual(["session_send"]);
|
|
74
|
+
expect(catalog.actions.get("session_send")).toBe(actionName("session.send"));
|
|
75
|
+
expect(catalog.actions.has("task_create")).toBe(false);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("an observation action is kept only when it is named, by its action name", () => {
|
|
79
|
+
const catalog = catalogOf(DESCRIPTION, ["niwa.trace"]);
|
|
80
|
+
expect(catalog.tools.map((tool) => tool.name)).toEqual(["niwa_trace"]);
|
|
81
|
+
expect(catalog.actions.get("niwa_trace")).toBe(actionName("niwa.trace"));
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("an empty visible list leaves nothing, and an unknown name keeps nothing", () => {
|
|
85
|
+
expect(catalogOf(DESCRIPTION, []).tools).toEqual([]);
|
|
86
|
+
expect(catalogOf(DESCRIPTION, ["no.such.action"]).tools).toEqual([]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a tool name is not an action name: the filter names the actions", () => {
|
|
90
|
+
// The list names actions; the tool name of an action is what the tool is called.
|
|
91
|
+
const catalog = catalogOf(DESCRIPTION, ["task_create"]);
|
|
92
|
+
expect(catalog.tools).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("ans-1: references inside an answer schema keep pointing at that schema once it is under `value`", () => {
|
|
96
|
+
const tool = {
|
|
97
|
+
name: "submit_answer",
|
|
98
|
+
description: "",
|
|
99
|
+
inputSchema: { type: "object", properties: { question: { type: "string" }, value: {} } },
|
|
100
|
+
};
|
|
101
|
+
const tree = {
|
|
102
|
+
type: "object",
|
|
103
|
+
properties: { kids: { type: "array", items: { $ref: "#" } }, thing: { $ref: "#/$defs/Thing" } },
|
|
104
|
+
$defs: { Thing: { type: "object", properties: { x: { type: "number" } } } },
|
|
105
|
+
};
|
|
106
|
+
const one = withOpenQuestions(tool, [{ request: "req_1", schema: tree }]).inputSchema as Record<string, any>;
|
|
107
|
+
expect(one.properties.value.$defs).toBeUndefined();
|
|
108
|
+
expect(one.properties.value.properties.kids.items).toEqual({ $ref: "#/properties/value" });
|
|
109
|
+
expect(one.properties.value.properties.thing).toEqual({ $ref: "#/$defs/Thing" });
|
|
110
|
+
expect(Object.keys(one.$defs)).toEqual(["Thing"]);
|
|
111
|
+
|
|
112
|
+
// Two schemas with a definition of the same name do not meet in the root.
|
|
113
|
+
const two = withOpenQuestions(tool, [
|
|
114
|
+
{ request: "req_1", schema: tree },
|
|
115
|
+
{ request: "req_2", schema: tree },
|
|
116
|
+
]).inputSchema as Record<string, any>;
|
|
117
|
+
expect(Object.keys(two.$defs)).toEqual(["q0_Thing", "q1_Thing"]);
|
|
118
|
+
expect(two.properties.value.anyOf[1].properties.kids.items).toEqual({ $ref: "#/properties/value/anyOf/1" });
|
|
119
|
+
expect(two.properties.value.anyOf[1].properties.thing).toEqual({ $ref: "#/$defs/q1_Thing" });
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("the hints of a tool come from what its action declares, and an unknown one is left out", () => {
|
|
123
|
+
const base = { description: "x", recovery: "repeat" as const, input: { type: "object" }, output: { type: "object" } };
|
|
124
|
+
const catalog = catalogOf({
|
|
125
|
+
actions: [
|
|
126
|
+
{ ...base, name: actionName("a.read"), effect: "read", world: "niwa" },
|
|
127
|
+
{ ...base, name: actionName("a.note"), effect: "record" },
|
|
128
|
+
{ ...base, name: actionName("a.comment"), effect: "write", world: "external", recovery: "repeat-with-key" },
|
|
129
|
+
{ ...base, name: actionName("a.merge"), effect: "irreversible", world: "external", recovery: "do-not-repeat" },
|
|
130
|
+
{ ...base, name: actionName("a.put"), effect: "write", world: "local", idempotent: true, destructive: true },
|
|
131
|
+
],
|
|
132
|
+
types: [],
|
|
133
|
+
});
|
|
134
|
+
const hints = Object.fromEntries(catalog.tools.map((tool) => [tool.name, tool.annotations]));
|
|
135
|
+
expect(hints).toEqual({
|
|
136
|
+
a_read: { readOnlyHint: true, openWorldHint: false },
|
|
137
|
+
a_note: { readOnlyHint: false },
|
|
138
|
+
// A retry with the same key is one operation; two calls are two comments: no idempotent hint.
|
|
139
|
+
a_comment: { readOnlyHint: false, openWorldHint: true },
|
|
140
|
+
a_merge: { readOnlyHint: false, openWorldHint: true, destructiveHint: true },
|
|
141
|
+
a_put: { readOnlyHint: false, openWorldHint: false, destructiveHint: true, idempotentHint: true },
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("an action the session does not see cannot take the name of one it sees", () => {
|
|
146
|
+
const base = { description: "x", recovery: "repeat" as const, effect: "read" as const, input: { type: "object" }, output: { type: "object" } };
|
|
147
|
+
const description: Description = {
|
|
148
|
+
actions: [
|
|
149
|
+
{ ...base, name: actionName("hello") },
|
|
150
|
+
{ ...base, name: actionName("a.b") },
|
|
151
|
+
{ ...base, name: actionName("a-b") },
|
|
152
|
+
],
|
|
153
|
+
types: [],
|
|
154
|
+
};
|
|
155
|
+
expect(catalogOf(description, ["hello"]).tools.map((tool) => tool.name)).toEqual(["hello"]);
|
|
156
|
+
// When both that make one name are seen, the clash is still reported.
|
|
157
|
+
expect(() => catalogOf(description, ["a.b", "a-b"])).toThrow(/already taken/);
|
|
158
|
+
expect(() => catalogOf(description)).toThrow(/already taken/);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("a result schema keeps its meaning once it is embedded, whichever dialect writes its references", () => {
|
|
162
|
+
// Draft 7: `definitions` and a pointer into the schema itself; and the same with the markers of its own document.
|
|
163
|
+
const draft7 = {
|
|
164
|
+
type: "object",
|
|
165
|
+
properties: {
|
|
166
|
+
label: { $ref: "#/definitions/Label" },
|
|
167
|
+
other: { $ref: "#/properties/label" },
|
|
168
|
+
kind: { default: { $ref: "#/nowhere" }, enum: ["a", "b"] },
|
|
169
|
+
},
|
|
170
|
+
required: ["label"],
|
|
171
|
+
definitions: { Label: { type: "string", minLength: 1 } },
|
|
172
|
+
};
|
|
173
|
+
const named = { ...draft7, $schema: "http://json-schema.org/draft-07/schema#", $id: "https://example.test/result" };
|
|
174
|
+
const modern = {
|
|
175
|
+
type: "object",
|
|
176
|
+
properties: { label: { $ref: "#/$defs/Label" }, children: { type: "array", items: { $ref: "#" } } },
|
|
177
|
+
required: ["label"],
|
|
178
|
+
$defs: { Label: { type: "string", minLength: 1 } },
|
|
179
|
+
};
|
|
180
|
+
const samples: unknown[] = [
|
|
181
|
+
{ label: "x" },
|
|
182
|
+
{ label: "" },
|
|
183
|
+
{ label: 1 },
|
|
184
|
+
{ label: "x", other: "y" },
|
|
185
|
+
{ label: "x", other: 1 },
|
|
186
|
+
{ label: "x", kind: "c" },
|
|
187
|
+
{ label: "x", children: [{ label: "y" }] },
|
|
188
|
+
{ label: "x", children: [{ label: 3 }] },
|
|
189
|
+
{},
|
|
190
|
+
];
|
|
191
|
+
for (const schema of [draft7, named, modern]) {
|
|
192
|
+
const one = withAssignedTasks(FINISH, [{ task: "task-1", description: "d", result: schema }]);
|
|
193
|
+
const two = withAssignedTasks(FINISH, [
|
|
194
|
+
{ task: "task-1", description: "d", result: schema },
|
|
195
|
+
{ task: "task-2", description: "e", result: modern },
|
|
196
|
+
]);
|
|
197
|
+
for (const sample of samples) {
|
|
198
|
+
expect(accepts(one.inputSchema, { task: "task-1", result: sample })).toBe(accepts(schema, sample));
|
|
199
|
+
expect(accepts(two.inputSchema, { task: "task-1", result: sample })).toBe(accepts(schema, sample));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("a value is held to the schema of the id it is given with", () => {
|
|
205
|
+
const tool: Tool = {
|
|
206
|
+
name: "submit_answer",
|
|
207
|
+
description: "",
|
|
208
|
+
inputSchema: { type: "object", properties: { question: { type: "string" }, value: {} }, required: ["question", "value"] },
|
|
209
|
+
};
|
|
210
|
+
const shown = withOpenQuestions(tool, [
|
|
211
|
+
{ request: "req_text", schema: { type: "string" } },
|
|
212
|
+
{ request: "req_count", schema: { type: "integer" } },
|
|
213
|
+
{ request: "req_flag", schema: { type: "boolean" } },
|
|
214
|
+
]).inputSchema;
|
|
215
|
+
const given = (question: string, value: unknown) => accepts(shown, { question, value });
|
|
216
|
+
expect(given("req_text", "yes")).toBe(true);
|
|
217
|
+
expect(given("req_count", 12)).toBe(true);
|
|
218
|
+
expect(given("req_flag", false)).toBe(true);
|
|
219
|
+
// The value of another question is not an answer to this one.
|
|
220
|
+
expect(given("req_text", 12)).toBe(false);
|
|
221
|
+
expect(given("req_count", "yes")).toBe(false);
|
|
222
|
+
expect(given("req_flag", 12)).toBe(false);
|
|
223
|
+
// The id is still one of the open ones, and both fields are still required.
|
|
224
|
+
expect(given("req_other", "yes")).toBe(false);
|
|
225
|
+
expect(accepts(shown, { question: "req_text" })).toBe(false);
|
|
226
|
+
// No combinator at the root, which some model APIs refuse.
|
|
227
|
+
for (const keyword of ["anyOf", "oneOf", "allOf"]) expect(Object.keys(shown)).not.toContain(keyword);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("the schemas of several choices keep their own references and definitions", () => {
|
|
231
|
+
const tool: Tool = {
|
|
232
|
+
name: "task_finish",
|
|
233
|
+
description: "",
|
|
234
|
+
inputSchema: { type: "object", properties: { task: { type: "string" }, result: {} }, required: ["task", "result"] },
|
|
235
|
+
};
|
|
236
|
+
const tree = {
|
|
237
|
+
type: "object",
|
|
238
|
+
properties: { kids: { type: "array", items: { $ref: "#" } }, thing: { $ref: "#/$defs/Thing" } },
|
|
239
|
+
$defs: { Thing: { type: "object", properties: { x: { type: "number" } }, required: ["x"] } },
|
|
240
|
+
};
|
|
241
|
+
const loose = { type: "object", properties: { label: { type: "string" } }, required: ["label"] };
|
|
242
|
+
const shown = withAssignedTasks(tool, [
|
|
243
|
+
{ task: "task-1", description: "a", result: tree },
|
|
244
|
+
{ task: "task-2", description: "b", result: loose },
|
|
245
|
+
]).inputSchema;
|
|
246
|
+
expect(accepts(shown, { task: "task-1", result: { kids: [{ thing: { x: 1 } }] } })).toBe(true);
|
|
247
|
+
expect(accepts(shown, { task: "task-1", result: { kids: [{ thing: { x: "no" } }] } })).toBe(false);
|
|
248
|
+
expect(accepts(shown, { task: "task-1", result: { label: "x" } })).toBe(true);
|
|
249
|
+
expect(accepts(shown, { task: "task-2", result: { label: "x" } })).toBe(true);
|
|
250
|
+
expect(accepts(shown, { task: "task-2", result: { label: 1 } })).toBe(false);
|
|
251
|
+
expect(accepts(shown, { task: "task-2", result: {} })).toBe(false);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("a task that declared no result schema can be finished next to one that did", () => {
|
|
255
|
+
const shown = withAssignedTasks(FINISH, [
|
|
256
|
+
{ task: "task-1", description: "count", result: { type: "object", properties: { n: { type: "integer" } }, required: ["n"] } },
|
|
257
|
+
{ task: "task-2", description: "say anything" },
|
|
258
|
+
]);
|
|
259
|
+
expect(accepts(shown.inputSchema, { task: "task-1", result: { n: 3 } })).toBe(true);
|
|
260
|
+
expect(accepts(shown.inputSchema, { task: "task-1", result: "done" })).toBe(false);
|
|
261
|
+
expect(accepts(shown.inputSchema, { task: "task-2", result: "done" })).toBe(true);
|
|
262
|
+
expect(accepts(shown.inputSchema, { task: "task-2", result: { n: "x" } })).toBe(true);
|
|
263
|
+
expect(shown.description).toContain("task-2 (say anything)");
|
|
264
|
+
|
|
265
|
+
// Alone, such a task takes any result.
|
|
266
|
+
const alone = withAssignedTasks(FINISH, [{ task: "task-2", description: "say anything" }]);
|
|
267
|
+
expect(accepts(alone.inputSchema, { task: "task-2", result: [1, "x"] })).toBe(true);
|
|
268
|
+
expect(accepts(alone.inputSchema, { task: "task-1", result: 1 })).toBe(false);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test("an action that does not take an object takes its input as one argument", () => {
|
|
272
|
+
const base = { description: "x", recovery: "repeat" as const, effect: "read" as const, output: { type: "object" } };
|
|
273
|
+
const catalog = catalogOf({
|
|
274
|
+
actions: [
|
|
275
|
+
{ ...base, name: actionName("a.text"), input: { type: "string", minLength: 2 } },
|
|
276
|
+
{ ...base, name: actionName("a.list"), input: { type: "array", items: { $ref: "#/$defs/Item" }, $defs: { Item: { type: "integer" } } } },
|
|
277
|
+
{ ...base, name: actionName("a.record"), input: { type: "object", properties: { n: { type: "integer" } } } },
|
|
278
|
+
{ ...base, name: actionName("a.either"), input: { anyOf: [{ type: "object" }, { type: "object" }] } },
|
|
279
|
+
],
|
|
280
|
+
types: [],
|
|
281
|
+
});
|
|
282
|
+
const tool = (name: string) => catalog.tools.find((one) => one.name === name)!;
|
|
283
|
+
expect([...catalog.wrapped]).toEqual(["a_text", "a_list"]);
|
|
284
|
+
for (const one of catalog.tools) expect(one.inputSchema.type).toBe("object");
|
|
285
|
+
|
|
286
|
+
// What the tool takes is what the action takes, in `input`.
|
|
287
|
+
expect(accepts(tool("a_text").inputSchema, { input: "ok" })).toBe(true);
|
|
288
|
+
expect(accepts(tool("a_text").inputSchema, { input: "x" })).toBe(false);
|
|
289
|
+
expect(accepts(tool("a_text").inputSchema, {})).toBe(false);
|
|
290
|
+
expect(accepts(tool("a_list").inputSchema, { input: [1, 2] })).toBe(true);
|
|
291
|
+
expect(accepts(tool("a_list").inputSchema, { input: ["a"] })).toBe(false);
|
|
292
|
+
expect(tool("a_text").description).toContain('"input"');
|
|
293
|
+
// An object is taken as it is written, and one that names no type is still an object.
|
|
294
|
+
expect(tool("a_record").inputSchema).toEqual({ type: "object", properties: { n: { type: "integer" } } });
|
|
295
|
+
expect(tool("a_either").inputSchema).toMatchObject({ type: "object", anyOf: expect.any(Array) });
|
|
296
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// What the server tells its client about changes of its tools, and what it follows to know.
|
|
2
|
+
|
|
3
|
+
import { afterEach, expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
6
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
7
|
+
import { ToolListChangedNotificationSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
8
|
+
|
|
9
|
+
import { createMcpServer } from "../src/index.ts";
|
|
10
|
+
import type { McpServerOptions } from "../src/index.ts";
|
|
11
|
+
import { declared, FakeHost, until } from "./fake-host.ts";
|
|
12
|
+
|
|
13
|
+
const open: Array<() => Promise<void>> = [];
|
|
14
|
+
|
|
15
|
+
afterEach(async () => {
|
|
16
|
+
for (const close of open.splice(0)) await close().catch(() => {});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
async function started(host: FakeHost, options: Partial<McpServerOptions> = {}) {
|
|
20
|
+
const mcp = createMcpServer({ home: "home", connect: host.connect, refreshMs: 40, ...options });
|
|
21
|
+
const [clientSide, serverSide] = InMemoryTransport.createLinkedPair();
|
|
22
|
+
const client = new Client({ name: "check", version: "0" });
|
|
23
|
+
let changed = 0;
|
|
24
|
+
client.setNotificationHandler(ToolListChangedNotificationSchema, () => {
|
|
25
|
+
changed += 1;
|
|
26
|
+
});
|
|
27
|
+
await Promise.all([mcp.connect(serverSide), client.connect(clientSide)]);
|
|
28
|
+
open.push(async () => {
|
|
29
|
+
await client.close();
|
|
30
|
+
await mcp.close();
|
|
31
|
+
});
|
|
32
|
+
const names = async (): Promise<string[]> => (await client.listTools()).tools.map((tool) => tool.name).sort();
|
|
33
|
+
return { mcp, client, names, changed: () => changed };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const settle = (ms = 150): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
|
37
|
+
|
|
38
|
+
test("a connection with no mark is told of a workflow registered after it connected", async () => {
|
|
39
|
+
const host = new FakeHost();
|
|
40
|
+
host.actions = [declared("a.one")];
|
|
41
|
+
const { names, changed } = await started(host, { refreshMs: 600_000 });
|
|
42
|
+
expect(await names()).toEqual(["a_one"]);
|
|
43
|
+
await until(() => host.subscriptions.length === 1, "the watch to start");
|
|
44
|
+
|
|
45
|
+
host.actions = [declared("a.one"), declared("flow.wf-abc123")];
|
|
46
|
+
host.commit("workflow.registered");
|
|
47
|
+
await until(() => changed() === 1, "the notification");
|
|
48
|
+
expect(await names()).toEqual(["a_one", "flow_wf_abc123"]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("a promoted built-in workflow, a new task and a record of no interest are told apart", async () => {
|
|
52
|
+
const host = new FakeHost();
|
|
53
|
+
host.actions = [declared("a.one")];
|
|
54
|
+
const { names, changed } = await started(host, { refreshMs: 600_000 });
|
|
55
|
+
await names();
|
|
56
|
+
await until(() => host.subscriptions.length === 1, "the watch to start");
|
|
57
|
+
// The feed is asked for these records only; the rest never reaches the server.
|
|
58
|
+
expect(host.subscriptions[0]?.types).toEqual(
|
|
59
|
+
expect.arrayContaining(["workflow.registered", "workflow.rechecked", "workflow.builtin.promoted", "task.created", "task.done"]),
|
|
60
|
+
);
|
|
61
|
+
expect(host.subscriptions[0]?.types).not.toContain("request.created");
|
|
62
|
+
|
|
63
|
+
host.actions = [{ ...declared("a.one"), description: "The promoted version." }];
|
|
64
|
+
host.commit("workflow.builtin.promoted");
|
|
65
|
+
await until(() => changed() === 1, "the notification for the promotion");
|
|
66
|
+
await names();
|
|
67
|
+
|
|
68
|
+
// Nothing about the tools moved: a record that is followed changes nothing for the client.
|
|
69
|
+
host.commit("task.created");
|
|
70
|
+
await settle();
|
|
71
|
+
expect(changed()).toBe(1);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("the questions of a session are followed only when the server serves a session", async () => {
|
|
75
|
+
const marked = new FakeHost();
|
|
76
|
+
marked.actions = [declared("a.one")];
|
|
77
|
+
const first = await started(marked, { mark: "mark-1", refreshMs: 600_000 });
|
|
78
|
+
await first.names();
|
|
79
|
+
await until(() => marked.subscriptions.length === 1, "the watch to start");
|
|
80
|
+
expect(marked.subscriptions[0]?.types).toEqual(expect.arrayContaining(["request.created", "session.marked", "workflow.registered"]));
|
|
81
|
+
|
|
82
|
+
const plain = new FakeHost();
|
|
83
|
+
plain.actions = [declared("a.one")];
|
|
84
|
+
const second = await started(plain, { refreshMs: 600_000 });
|
|
85
|
+
await second.names();
|
|
86
|
+
await until(() => plain.subscriptions.length === 1, "the watch to start");
|
|
87
|
+
expect(plain.subscriptions[0]?.types).not.toContain("request.created");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("the watch starts from the end of the journal without reading its history", async () => {
|
|
91
|
+
const host = new FakeHost();
|
|
92
|
+
host.actions = [declared("a.one")];
|
|
93
|
+
host.commit("workflow.registered");
|
|
94
|
+
host.commit("task.created");
|
|
95
|
+
host.commit("task.done");
|
|
96
|
+
const { names } = await started(host, { refreshMs: 600_000 });
|
|
97
|
+
await names();
|
|
98
|
+
await until(() => host.subscriptions.length === 1, "the watch to start");
|
|
99
|
+
expect(host.subscriptions[0]?.from).toBe(3);
|
|
100
|
+
expect(host.calls.filter((call) => call.action === "niwa.records.read")).toEqual([
|
|
101
|
+
{ action: "niwa.records.read", input: expect.objectContaining({ tail: 1 }) },
|
|
102
|
+
]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a change that lands after the first list and before the watch is not lost", async () => {
|
|
106
|
+
const host = new FakeHost();
|
|
107
|
+
host.actions = [declared("a.one")];
|
|
108
|
+
let release: () => void = () => undefined;
|
|
109
|
+
host.readGate = new Promise((resolve) => {
|
|
110
|
+
release = resolve;
|
|
111
|
+
});
|
|
112
|
+
const { client, changed } = await started(host, { refreshMs: 600_000 });
|
|
113
|
+
await client.listTools();
|
|
114
|
+
// Registered, and recorded, before the watch reads where the journal ends: the feed starts after it.
|
|
115
|
+
host.actions = [declared("a.one"), declared("flow.wf-late")];
|
|
116
|
+
host.commit("workflow.registered");
|
|
117
|
+
release();
|
|
118
|
+
await until(() => changed() === 1, "the notification");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a session that may not read the journal is told of a change by looking again", async () => {
|
|
122
|
+
const host = new FakeHost();
|
|
123
|
+
host.readMode = "denied";
|
|
124
|
+
host.actions = [declared("a.one")];
|
|
125
|
+
const { names, changed } = await started(host, { mark: "mark-1" });
|
|
126
|
+
expect(await names()).toEqual(["a_one"]);
|
|
127
|
+
await settle();
|
|
128
|
+
expect(host.subscriptions).toEqual([]);
|
|
129
|
+
// Looking again alone tells nothing while nothing changed.
|
|
130
|
+
expect(changed()).toBe(0);
|
|
131
|
+
|
|
132
|
+
host.actions = [declared("a.one"), declared("flow.wf-abc123")];
|
|
133
|
+
await until(() => changed() === 1, "the notification");
|
|
134
|
+
expect(await names()).toEqual(["a_one", "flow_wf_abc123"]);
|
|
135
|
+
await settle();
|
|
136
|
+
expect(changed()).toBe(1);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("a server that ended stops looking at its tools", async () => {
|
|
140
|
+
const host = new FakeHost();
|
|
141
|
+
host.readMode = "denied";
|
|
142
|
+
host.actions = [declared("a.one")];
|
|
143
|
+
const { mcp, names } = await started(host);
|
|
144
|
+
await names();
|
|
145
|
+
await until(() => host.described > 2, "a few looks");
|
|
146
|
+
await mcp.close();
|
|
147
|
+
const after = host.described;
|
|
148
|
+
await settle(200);
|
|
149
|
+
expect(host.described).toBe(after);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("a session that looks again looks less often while its tools stay as they are, and at once more often after a change", async () => {
|
|
153
|
+
const host = new FakeHost();
|
|
154
|
+
host.readMode = "denied";
|
|
155
|
+
host.actions = [declared("a.one")];
|
|
156
|
+
const { names, changed } = await started(host, { refreshMs: 20 });
|
|
157
|
+
await names();
|
|
158
|
+
await settle(500);
|
|
159
|
+
// A look every 20 ms would be 25 looks; the spacing doubles up to ten times the interval.
|
|
160
|
+
expect(host.described).toBeLessThan(12);
|
|
161
|
+
|
|
162
|
+
host.actions = [declared("a.one"), declared("a.two")];
|
|
163
|
+
await until(() => changed() === 1, "the notification");
|
|
164
|
+
await names();
|
|
165
|
+
// After the change the spacing is back to the interval.
|
|
166
|
+
const after = host.described;
|
|
167
|
+
await settle(130);
|
|
168
|
+
expect(host.described - after).toBeGreaterThanOrEqual(2);
|
|
169
|
+
});
|