@acid-cat/cli 0.19.0 → 0.21.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.
Files changed (42) hide show
  1. package/README.md +32 -2
  2. package/conformance/fixture.acidcat.json +96 -0
  3. package/dist/conform-LH9KgQ9-.js +5045 -0
  4. package/dist/index.d.ts +164 -4
  5. package/dist/index.js +96 -4
  6. package/dist/main.d.ts +1 -2
  7. package/dist/main.js +1090 -7
  8. package/package.json +4 -3
  9. package/dist/catalog.d.ts +0 -12
  10. package/dist/catalog.js +0 -149
  11. package/dist/conform/check.d.ts +0 -47
  12. package/dist/conform/check.js +0 -178
  13. package/dist/conform/cli.d.ts +0 -5
  14. package/dist/conform/cli.js +0 -721
  15. package/dist/conform/http.d.ts +0 -6
  16. package/dist/conform/http.js +0 -297
  17. package/dist/conform/index.d.ts +0 -69
  18. package/dist/conform/index.js +0 -80
  19. package/dist/conform/mcp-stdio.d.ts +0 -7
  20. package/dist/conform/mcp-stdio.js +0 -291
  21. package/dist/conform/mcp.d.ts +0 -47
  22. package/dist/conform/mcp.js +0 -754
  23. package/dist/conform/poll.d.ts +0 -13
  24. package/dist/conform/poll.js +0 -295
  25. package/dist/conform/properties.d.ts +0 -12
  26. package/dist/conform/properties.js +0 -151
  27. package/dist/conform/serve.d.ts +0 -12
  28. package/dist/conform/serve.js +0 -96
  29. package/dist/conform/server-card.d.ts +0 -5
  30. package/dist/conform/server-card.js +0 -142
  31. package/dist/conform/sse.d.ts +0 -9
  32. package/dist/conform/sse.js +0 -53
  33. package/dist/conform/webhook.d.ts +0 -30
  34. package/dist/conform/webhook.js +0 -481
  35. package/dist/conform/webmcp.d.ts +0 -53
  36. package/dist/conform/webmcp.js +0 -232
  37. package/dist/json/catalog.schema.json.js +0 -170
  38. package/dist/json/fixture.acidcat.json.js +0 -396
  39. package/dist/json/package.json.js +0 -3
  40. package/dist/json/server-card.schema.json.js +0 -281
  41. package/dist/json.d.ts +0 -11
  42. package/dist/json.js +0 -13
@@ -1,13 +0,0 @@
1
- import { Checks } from "./check.js";
2
- import type { Channel } from "./mcp.js";
3
- /**
4
- * The poll settings of the deployment that conform tests. Each one is optional. With a setting, conform also tests the
5
- * rule that depends on it. The lease time of the deployment must be longer than the deadline: a lease must last for a
6
- * whole check.
7
- */
8
- export interface PollSettings {
9
- /** The number of occurrences that a lease keeps. With it, conform tests the occurrences of a cursor older than the buffer. It must be 97 or less. */
10
- readonly bufferLimit?: number | undefined;
11
- }
12
- export declare function checkPoll(checks: Checks, channel: Channel, settings: PollSettings): Promise<void>;
13
- export declare function checkPollLease(checks: Checks, channel: Channel, leaseMs: number): Promise<void>;
@@ -1,295 +0,0 @@
1
- import { isArrayOf, isObject, memberOf } from "../json.js";
2
- import { expect, expectEqual, expectOccurrence, until } from "./check.js";
3
- import { eventRequests } from "./mcp.js";
4
- import { randomUUID } from "node:crypto";
5
- // src/conform/poll.ts
6
- const RULE = "spec/surfaces/mcp.md#poll";
7
- const MAXIMUM_TICKS = 100;
8
- const MAXIMUM_EVENTS = 100;
9
- function pollRequests(channel) {
10
- const { errorOf, watching, signalKey } = eventRequests(channel);
11
- const polledOf = (message, name) => {
12
- const result = memberOf(message, "result");
13
- expect(isObject(result), `events/poll must give a result, got ${JSON.stringify(message)}`);
14
- expectEqual(result.resultType, "complete", "resultType");
15
- const { events, cursor, truncated, hasMore, nextPollMs } = result;
16
- if (!isArrayOf(events, isObject)) throw new Error(`events must be a list of occurrences, got ${JSON.stringify(events)}`);
17
- for (const each of events) expect(!("truncated" in each), `an item of events must have no truncated member, got ${JSON.stringify(each)}`);
18
- expect(typeof cursor === "string", `cursor must be a string, got ${JSON.stringify(cursor)}`);
19
- expect(typeof truncated === "boolean", `truncated must be a boolean, got ${JSON.stringify(truncated)}`);
20
- expect(typeof hasMore === "boolean", `hasMore must be a boolean, got ${JSON.stringify(hasMore)}`);
21
- expect(typeof nextPollMs === "number" && Number.isSafeInteger(nextPollMs) && nextPollMs >= 0, `nextPollMs must be an integer of 0 or more, got ${JSON.stringify(nextPollMs)}`);
22
- return {
23
- events: events.map((each) => expectOccurrence(each, name)),
24
- cursor,
25
- truncated,
26
- hasMore
27
- };
28
- };
29
- const poll = async (name, input, extra, signal) => polledOf(await channel.request("events/poll", {
30
- name,
31
- arguments: input,
32
- ...extra
33
- }, signal), name);
34
- const pollUntil = (what, name, input, extra, done, signal) => until(what, () => poll(name, input, extra, signal), done, signal);
35
- const pollError = async (name, input, extra, signal) => errorOf(await channel.request("events/poll", {
36
- name,
37
- arguments: input,
38
- ...extra
39
- }, signal));
40
- const dataOf = (polled) => polled.events.map((each) => each.data);
41
- const watched = (key, ...values) => values.map((value) => ({
42
- key,
43
- value
44
- }));
45
- const ticks = (from, to) => Array.from({ length: to - from + 1 }, (_, index) => ({ n: from + index }));
46
- const hasKey = (key) => (keys) => keys.includes(key);
47
- const lacksKey = (key) => (keys) => !keys.includes(key);
48
- const expectStart = (polled, truncated) => {
49
- expectEqual(polled.events, [], "the occurrences of the poll that starts the lease");
50
- expectEqual(polled.truncated, truncated, "truncated");
51
- expectEqual(polled.hasMore, false, "hasMore");
52
- };
53
- const startWatch = async (key, signal) => {
54
- const started = await poll("fixture.watch", { key }, {}, signal);
55
- expectStart(started, false);
56
- await until("fixture.watching gives the key of the lease", () => watching(signal), hasKey(key), signal);
57
- return started.cursor;
58
- };
59
- const signalAll = async (key, values, signal) => {
60
- for (const value of values) expectEqual(await signalKey(key, value, signal), 1, "the subscriptions that fixture.signal reached");
61
- };
62
- return {
63
- errorOf,
64
- watching,
65
- polledOf,
66
- poll,
67
- pollUntil,
68
- pollError,
69
- dataOf,
70
- watched,
71
- ticks,
72
- hasKey,
73
- lacksKey,
74
- expectStart,
75
- startWatch,
76
- signalAll
77
- };
78
- }
79
- async function checkPoll(checks, channel, settings) {
80
- const { errorOf, watching, polledOf, poll, pollUntil, pollError, dataOf, watched, ticks, hasKey, expectStart, startWatch, signalAll } = pollRequests(channel);
81
- await checks.run("events/poll with no cursor and no lease starts a lease with no occurrences, whose subscription starts now; a cursor of the lease gives the occurrences after it, in order; and no cursor gives the newest position", RULE, async (signal) => {
82
- const key = randomUUID();
83
- const start = await startWatch(key, signal);
84
- await signalAll(key, ["a", "b"], signal);
85
- const both = await pollUntil("the lease has both occurrences", "fixture.watch", { key }, { cursor: start }, (each) => each.events.length >= 2, signal);
86
- expectEqual(dataOf(both), watched(key, "a", "b"), "the data of the occurrences after the start");
87
- expectEqual([both.truncated, both.hasMore], [false, false], "truncated and hasMore");
88
- const newest = await poll("fixture.watch", { key }, {}, signal);
89
- expectEqual([newest.events, newest.truncated], [[], false], "the occurrences and truncated of a poll with no cursor");
90
- await signalAll(key, ["c"], signal);
91
- const after = await pollUntil("the lease has the next occurrence", "fixture.watch", { key }, { cursor: newest.cursor }, (each) => each.events.length > 0, signal);
92
- expectEqual(dataOf(after), watched(key, "c"), "the data of the occurrences after the newest position");
93
- const next = await poll("fixture.watch", { key }, { cursor: both.cursor }, signal);
94
- expectEqual(dataOf(next), watched(key, "c"), "the data of the occurrences after the cursor of the last result");
95
- });
96
- await checks.run("maxEvents limits the occurrences of a result, and hasMore tells that the buffer has more", RULE, async (signal) => {
97
- const key = randomUUID();
98
- const start = await startWatch(key, signal);
99
- await signalAll(key, [
100
- "1",
101
- "2",
102
- "3"
103
- ], signal);
104
- const first = await pollUntil("the lease has three occurrences", "fixture.watch", { key }, {
105
- cursor: start,
106
- maxEvents: 2
107
- }, (each) => each.hasMore, signal);
108
- expectEqual(dataOf(first), watched(key, "1", "2"), "the data of the first result");
109
- const second = await poll("fixture.watch", { key }, {
110
- cursor: first.cursor,
111
- maxEvents: 2
112
- }, signal);
113
- expectEqual(dataOf(second), watched(key, "3"), "the data of the second result");
114
- expectEqual(second.hasMore, false, "hasMore of the second result");
115
- });
116
- await checks.run("a maxEvents that is not an integer of 1 or more is the error -32602", RULE, async (signal) => {
117
- for (const maxEvents of [
118
- 0,
119
- -1,
120
- 1.5,
121
- "2"
122
- ]) {
123
- const error = await pollError("fixture.watch", { key: randomUUID() }, { maxEvents }, signal);
124
- expectEqual(error.code, -32602, `the JSON-RPC error code for maxEvents ${JSON.stringify(maxEvents)}`);
125
- }
126
- });
127
- await checks.run("a cursor that is not of the lease, while the lease exists, gives no occurrences, the newest position and truncated: true", RULE, async (signal) => {
128
- const key = randomUUID();
129
- const start = await startWatch(key, signal);
130
- const other = await poll("fixture.watch", { key: randomUUID() }, {}, signal);
131
- await signalAll(key, ["before"], signal);
132
- await pollUntil("the lease has the occurrence", "fixture.watch", { key }, { cursor: start }, (each) => each.events.length > 0, signal);
133
- const foreign = await poll("fixture.watch", { key }, { cursor: other.cursor }, signal);
134
- expectEqual([foreign.events, foreign.truncated], [[], true], "the occurrences and truncated");
135
- await signalAll(key, ["after"], signal);
136
- const after = await pollUntil("the lease has the next occurrence", "fixture.watch", { key }, { cursor: foreign.cursor }, (each) => each.events.length > 0, signal);
137
- expectEqual(dataOf(after), watched(key, "after"), "the data of the occurrences after the newest position");
138
- });
139
- await checks.run("a cursor that is not of a lease, with no lease, starts a lease that starts now, with truncated: true", RULE, async (signal) => {
140
- const other = await poll("fixture.watch", { key: randomUUID() }, {}, signal);
141
- const key = randomUUID();
142
- const started = await poll("fixture.watch", { key }, { cursor: other.cursor }, signal);
143
- expectStart(started, true);
144
- await until("fixture.watching gives the key of the new lease", () => watching(signal), hasKey(key), signal);
145
- await signalAll(key, ["now"], signal);
146
- const polled = await pollUntil("the lease has the occurrence", "fixture.watch", { key }, { cursor: started.cursor }, (each) => each.events.length > 0, signal);
147
- expectEqual(dataOf(polled), watched(key, "now"), "the data of the occurrences");
148
- });
149
- await checks.run("a cursor of push delivery has no cursor of the provider: the new lease starts now, with truncated: true", RULE, async (signal) => {
150
- const input = {
151
- count: 3,
152
- key: randomUUID()
153
- };
154
- const started = await poll("fixture.ticks", input, { cursor: "1" }, signal);
155
- expectStart(started, true);
156
- const polled = await pollUntil("the lease has the last tick", "fixture.ticks", input, { cursor: started.cursor }, (each) => each.events.some((event) => memberOf(event.data, "n") === 3), signal);
157
- expectEqual(dataOf(polled), ticks(1, 3), "the data of the occurrences, from n = 1");
158
- });
159
- await checks.run("a cursor of another lease with a cursor of the provider starts a lease that resumes after it", RULE, async (signal) => {
160
- const first = {
161
- count: 3,
162
- key: randomUUID()
163
- };
164
- const start = await poll("fixture.ticks", first, {}, signal);
165
- expectStart(start, false);
166
- const one = await pollUntil("the first lease has the first tick", "fixture.ticks", first, {
167
- cursor: start.cursor,
168
- maxEvents: 1
169
- }, (each) => each.events.length > 0, signal);
170
- expectEqual(dataOf(one), ticks(1, 1), "the data of the first occurrence");
171
- const second = {
172
- count: 4,
173
- key: randomUUID()
174
- };
175
- const resumed = await poll("fixture.ticks", second, { cursor: one.cursor }, signal);
176
- expectStart(resumed, false);
177
- const polled = await pollUntil("the new lease has the last tick", "fixture.ticks", second, { cursor: resumed.cursor }, (each) => each.events.some((event) => memberOf(event.data, "n") === 4), signal);
178
- expectEqual(dataOf(polled), ticks(2, 4), "the data of the occurrences after the cursor of the provider");
179
- expectEqual(polled.truncated, false, "truncated");
180
- });
181
- await checks.run("an occurrence after a gap gives a result with truncated: true, and its item has no truncated", RULE, async (signal) => {
182
- const input = {
183
- count: 3,
184
- gap: 2,
185
- key: randomUUID()
186
- };
187
- const start = await poll("fixture.ticks", input, {}, signal);
188
- expectStart(start, false);
189
- const before = await pollUntil("the lease has the first tick", "fixture.ticks", input, {
190
- cursor: start.cursor,
191
- maxEvents: 1
192
- }, (each) => each.events.length > 0, signal);
193
- expectEqual([dataOf(before), before.truncated], [ticks(1, 1), false], "the data and truncated of the result before the gap");
194
- const after = await pollUntil("the lease has the last tick", "fixture.ticks", input, { cursor: before.cursor }, (each) => each.events.some((event) => memberOf(event.data, "n") === 3), signal);
195
- expectEqual([dataOf(after), after.truncated], [ticks(2, 3), true], "the data and truncated of the result with the gap");
196
- });
197
- if (settings.bufferLimit !== void 0) {
198
- const limit = settings.bufferLimit;
199
- await checks.run("a cursor older than the buffer gives the occurrences in the buffer, and truncated: true", RULE, async (signal) => {
200
- expect(Number.isSafeInteger(limit) && limit >= 1 && limit + 3 <= MAXIMUM_TICKS, `conform tests the buffer only when it keeps 1 to 97 occurrences, got ${limit}`);
201
- const total = limit + 3;
202
- const input = {
203
- count: total,
204
- key: randomUUID()
205
- };
206
- const start = await poll("fixture.ticks", input, {}, signal);
207
- expectStart(start, false);
208
- const polled = await pollUntil("the lease has the last tick", "fixture.ticks", input, {
209
- cursor: start.cursor,
210
- maxEvents: MAXIMUM_EVENTS
211
- }, (each) => each.events.some((event) => memberOf(event.data, "n") === total), signal);
212
- expectEqual(dataOf(polled), ticks(4, total), "the data of the occurrences in the buffer");
213
- expectEqual([polled.truncated, polled.hasMore], [true, false], "truncated and hasMore");
214
- });
215
- }
216
- await checks.run("a failure before the first occurrence is a JSON-RPC error, and starts no lease", RULE, async (signal) => {
217
- for (const [name, input, code] of [
218
- [
219
- "fixture.secret",
220
- {},
221
- -32023
222
- ],
223
- [
224
- "fixture.echo",
225
- { text: "x" },
226
- -32023
227
- ],
228
- [
229
- "fixture.ticks",
230
- { count: 101 },
231
- -32602
232
- ]
233
- ]) for (const attempt of ["first", "second"]) {
234
- const error = await pollError(name, input, {}, signal);
235
- expectEqual(error.code, code, `the JSON-RPC error code of the ${attempt} poll of ${name}`);
236
- if (code === -32023) expectEqual(memberOf(error.data, "kind"), "event", `data.kind of the ${attempt} poll of ${name}`);
237
- }
238
- });
239
- await checks.run("after a defect, the next poll is the error -32603 and ends the lease, and a later poll starts a new lease", RULE, async (signal) => {
240
- const start = async () => {
241
- const message = await channel.request("events/poll", {
242
- name: "fixture.broken",
243
- arguments: {}
244
- }, signal);
245
- const polled = memberOf(message, "error") === void 0 ? polledOf(message, "fixture.broken") : await poll("fixture.broken", {}, {}, signal);
246
- expectStart(polled, false);
247
- return polled.cursor;
248
- };
249
- const failure = async (cursor) => {
250
- const message = await until("a poll gets the error", async () => {
251
- const each = await channel.request("events/poll", {
252
- name: "fixture.broken",
253
- arguments: {},
254
- cursor
255
- }, signal);
256
- if (memberOf(each, "error") === void 0) expect(dataOf(polledOf(each, "fixture.broken")).every((data) => memberOf(data, "n") === 1), "a result before the error has only the occurrence n = 1");
257
- return each;
258
- }, (each) => memberOf(each, "error") !== void 0, signal);
259
- const error = errorOf(message);
260
- expectEqual(error.code, -32603, "the JSON-RPC error code");
261
- expect(!/\n\s+at /.test(String(error.message)), "the message must not contain a stack trace");
262
- };
263
- await failure(await start());
264
- await failure(await start());
265
- });
266
- }
267
- async function checkPollLease(checks, channel, leaseMs) {
268
- const { watching, poll, lacksKey, hasKey, expectStart } = pollRequests(channel);
269
- const keepUntilWatched = async (key, cursor, signal) => {
270
- const lease = {
271
- cursor,
272
- renewedAt: Date.now()
273
- };
274
- await until("fixture.watching gives the key of the lease, while each poll renews the lease", async () => {
275
- lease.renewedAt = Date.now();
276
- lease.cursor = (await poll("fixture.watch", { key }, { cursor: lease.cursor }, signal)).cursor;
277
- return watching(signal);
278
- }, hasKey(key), signal);
279
- return lease;
280
- };
281
- await checks.run("a lease that gets no poll for the lease time ends, and cancels its subscription", RULE, async (signal) => {
282
- expect(Number.isSafeInteger(leaseMs) && leaseMs >= 1 && leaseMs < checks.deadline, `conform tests the end of a lease only when the lease time is shorter than the deadline of ${checks.deadline} ms, got ${leaseMs}`);
283
- const key = randomUUID();
284
- const started = await poll("fixture.watch", { key }, {}, signal);
285
- expectStart(started, false);
286
- const lease = await keepUntilWatched(key, started.cursor, signal);
287
- await until("the lease ends, and the key leaves fixture.watching", () => watching(signal), lacksKey(key), signal);
288
- const elapsed = Date.now() - lease.renewedAt;
289
- expect(elapsed >= leaseMs, `the lease ended ${elapsed} ms after the last poll, before the lease time of ${leaseMs} ms`);
290
- const again = await poll("fixture.watch", { key }, { cursor: lease.cursor }, signal);
291
- expectStart(again, true);
292
- await keepUntilWatched(key, again.cursor, signal);
293
- });
294
- }
295
- export { checkPoll, checkPollLease };
@@ -1,12 +0,0 @@
1
- import type { Outcome } from "@acid-cat/spec-ts";
2
- import type { Client } from "@modelcontextprotocol/client";
3
- import type { JsonObject } from "../json.js";
4
- import { Checks } from "./check.js";
5
- export type Invoke = (id: string, input: JsonObject, signal?: AbortSignal) => Promise<Outcome<unknown, unknown>>;
6
- export declare function httpInvoke(base: string): Invoke;
7
- export declare function mcpInvoke(url: string, signal: AbortSignal): Promise<{
8
- readonly invoke: Invoke;
9
- close(): Promise<void>;
10
- }>;
11
- export declare function clientInvoke(client: Client): Invoke;
12
- export declare function checkProperties(surface: string, invoke: Invoke, deadline: number, signal?: AbortSignal): Promise<Checks>;
@@ -1,151 +0,0 @@
1
- import { isStringArray, memberOf, parseJson, readJson } from "../json.js";
2
- import { Checks, expect, isOutcome, until } from "./check.js";
3
- import { mcpClient } from "./mcp.js";
4
- import { randomUUID } from "node:crypto";
5
- import { SdkError } from "@modelcontextprotocol/client";
6
- import fc from "fast-check";
7
- // src/conform/properties.ts
8
- const RULE = "spec/invocation.md#cancellation-and-concurrency";
9
- function httpInvoke(base) {
10
- return async (id, input, signal) => {
11
- const response = await fetch(`${base}/capabilities/${id}`, {
12
- method: "POST",
13
- headers: { "Content-Type": "application/json" },
14
- body: JSON.stringify(input),
15
- ...signal ? { signal } : {}
16
- });
17
- const outcome = await readJson(response);
18
- if (!isOutcome(outcome)) throw new Error(`POST /capabilities/${id} gave ${JSON.stringify(outcome)}, which is not an outcome`);
19
- return outcome;
20
- };
21
- }
22
- async function mcpInvoke(url, signal) {
23
- const client = await mcpClient(url, signal);
24
- return {
25
- invoke: clientInvoke(client),
26
- close: () => client.close()
27
- };
28
- }
29
- function clientInvoke(client) {
30
- return async (id, input, signal) => {
31
- try {
32
- const result = await client.callTool({
33
- name: id,
34
- arguments: input
35
- }, signal ? { signal } : void 0);
36
- const first = result.content[0];
37
- const body = parseJson(first?.type === "text" ? first.text : "null");
38
- if (result.isError !== true) return {
39
- ok: true,
40
- value: result.structuredContent ?? body
41
- };
42
- if (!isOutcome(body)) throw new Error(`the error result of ${id} gave ${JSON.stringify(body)}, which is not an outcome`);
43
- return body;
44
- } catch (error) {
45
- if (signal?.aborted && error instanceof SdkError && error.message === String(signal.reason)) throw signal.reason;
46
- if (memberOf(error, "code") === -32602) return {
47
- kind: "unknown-capability",
48
- message: String(error)
49
- };
50
- throw error;
51
- }
52
- };
53
- }
54
- const operation = fc.oneof(fc.string({ maxLength: 40 }).map((text) => ({
55
- kind: "echo",
56
- text
57
- })), fc.constant({ kind: "refuse" }), fc.constant({ kind: "crash" }), fc.constant({ kind: "invalid" }));
58
- function agrees(operation, outcome) {
59
- switch (operation.kind) {
60
- case "echo": return "ok" in outcome && outcome.ok && JSON.stringify(outcome.value) === JSON.stringify({ text: operation.text });
61
- case "refuse": return "ok" in outcome && !outcome.ok && memberOf(outcome.error, "_tag") === "Refused";
62
- case "crash": return "kind" in outcome && outcome.kind === "internal";
63
- case "invalid": return "kind" in outcome && outcome.kind === "invalid-input";
64
- }
65
- }
66
- const callFor = (invoke, operation, signal) => {
67
- switch (operation.kind) {
68
- case "echo": return invoke("fixture.echo", { text: operation.text }, signal);
69
- case "refuse": return invoke("fixture.refuse", {}, signal);
70
- case "crash": return invoke("fixture.crash", {}, signal);
71
- case "invalid": return invoke("fixture.echo", { text: 0 }, signal);
72
- }
73
- };
74
- const settle = (call) => call.then((outcome) => ({ outcome }), (rejected) => ({ rejected }));
75
- const describe = (settled) => "outcome" in settled ? JSON.stringify(settled.outcome) : `the rejection ${String(settled.rejected)}`;
76
- const isCancellation = (settled, reason) => "outcome" in settled ? "kind" in settled.outcome && settled.outcome.kind === "cancelled" : settled.rejected === reason;
77
- async function heldKeys(invoke, signal) {
78
- const outcome = await invoke("fixture.held", {}, signal);
79
- expect("ok" in outcome && outcome.ok, `fixture.held gave ${JSON.stringify(outcome)}`);
80
- const keys = memberOf(outcome.value, "keys");
81
- expect(isStringArray(keys), `fixture.held gave ${JSON.stringify(outcome)}, with no list of keys`);
82
- return keys;
83
- }
84
- async function cancelSome(invoke, flags, signal, deadline) {
85
- const calls = flags.map((cancelled) => {
86
- const key = randomUUID();
87
- const controller = new AbortController();
88
- const reason = /* @__PURE__ */ new Error(`acid-cat conform cancels the call that holds ${key}`);
89
- return {
90
- key,
91
- cancelled,
92
- cancel: () => controller.abort(reason),
93
- reason,
94
- settled: settle(invoke("fixture.hold", { key }, AbortSignal.any([controller.signal, signal])))
95
- };
96
- });
97
- const cancelled = calls.filter((call) => call.cancelled);
98
- const kept = calls.filter((call) => !call.cancelled);
99
- const read = () => heldKeys(invoke, signal);
100
- try {
101
- await until("fixture.held gives the key of each call", read, (held) => calls.every((call) => held.includes(call.key)), signal);
102
- for (const call of cancelled) call.cancel();
103
- for (const call of cancelled) {
104
- const settled = await call.settled;
105
- expect(isCancellation(settled, call.reason), `a cancelled call gave ${describe(settled)}`);
106
- }
107
- const held = await until("the cancelled calls stop holding", read, (held) => cancelled.every((call) => !held.includes(call.key)), signal);
108
- expect(kept.every((call) => held.includes(call.key)), "a call that was not cancelled stopped holding");
109
- for (const call of kept) {
110
- await invoke("fixture.release", { key: call.key }, signal);
111
- const settled = await call.settled;
112
- const own = {
113
- ok: true,
114
- value: { key: call.key }
115
- };
116
- expect("outcome" in settled && JSON.stringify(settled.outcome) === JSON.stringify(own), `a call that was not cancelled gave ${describe(settled)}`);
117
- }
118
- } finally {
119
- const cleanup = AbortSignal.timeout(deadline);
120
- await Promise.all(calls.map((call) => invoke("fixture.release", { key: call.key }, cleanup).catch(() => void 0)));
121
- }
122
- }
123
- const cancellations = fc.array(fc.boolean(), {
124
- minLength: 2,
125
- maxLength: 5
126
- }).filter((flags) => flags.includes(true) && flags.includes(false));
127
- async function checkProperties(surface, invoke, deadline, signal) {
128
- const checks = new Checks(`${surface} properties`, deadline, signal);
129
- await checks.runEach("concurrent calls each get exactly their own outcome", RULE, async (each) => {
130
- await fc.assert(fc.asyncProperty(fc.array(operation, {
131
- minLength: 1,
132
- maxLength: 12
133
- }), (operations) => each(async (signal) => {
134
- (await Promise.all(operations.map(async (op) => ({
135
- op,
136
- outcome: await callFor(invoke, op, signal)
137
- })))).forEach(({ op, outcome }) => expect(agrees(op, outcome), `${op.kind} gave ${JSON.stringify(outcome)}`));
138
- })), {
139
- numRuns: 25,
140
- includeErrorInReport: true
141
- });
142
- });
143
- await checks.runEach("cancelling a call affects only that call", RULE, async (each) => {
144
- await fc.assert(fc.asyncProperty(cancellations, (flags) => each((signal) => cancelSome(invoke, flags, signal, deadline))), {
145
- numRuns: 5,
146
- includeErrorInReport: true
147
- });
148
- });
149
- return checks;
150
- }
151
- export { checkProperties, clientInvoke, httpInvoke, mcpInvoke };
@@ -1,12 +0,0 @@
1
- export interface Served {
2
- /** The base URL, for example `http://127.0.0.1:40123`. */
3
- readonly url: string;
4
- close(): Promise<void>;
5
- }
6
- export type WebHandler = (request: Request) => Promise<Response> | Response;
7
- /** Handlers by path prefix, for example `{ "/mcp": mcp, "/api": http }`. The longest prefix that matches gets the request. */
8
- export interface Routes {
9
- readonly [prefix: string]: WebHandler;
10
- }
11
- /** Serve one handler for all paths, or handlers by path prefix, on `127.0.0.1` and a free port. */
12
- export declare function serve(handler: WebHandler | Routes): Promise<Served>;
@@ -1,96 +0,0 @@
1
- import { createServer } from "node:http";
2
- // src/conform/serve.ts
3
- /** Serve one handler for all paths, or handlers by path prefix, on `127.0.0.1` and a free port. */
4
- function serve(handler) {
5
- const routes = typeof handler === "function" ? { "/": handler } : handler;
6
- const prefixes = Object.keys(routes).sort((a, b) => b.length - a.length);
7
- const server = createServer({ keepAliveTimeout: 0 }, (incoming, outgoing) => {
8
- const closeIfIdle = () => server.listening || server.closeIdleConnections();
9
- incoming.on("end", closeIfIdle);
10
- outgoing.on("finish", closeIfIdle);
11
- answer(incoming, outgoing, prefixes, routes);
12
- });
13
- return new Promise((resolve) => {
14
- server.listen(0, "127.0.0.1", () => {
15
- const address = server.address();
16
- resolve({
17
- url: `http://127.0.0.1:${typeof address === "object" && address !== null ? address.port : 0}`,
18
- close: () => new Promise((done) => server.close(() => done()))
19
- });
20
- });
21
- });
22
- }
23
- async function answer(incoming, outgoing, prefixes, routes) {
24
- const url = new URL(incoming.url ?? "/", `http://${incoming.headers.host ?? "127.0.0.1"}`);
25
- const prefix = prefixes.find((candidate) => candidate === "/" || url.pathname === candidate || url.pathname.startsWith(`${candidate}/`));
26
- const handler = prefix === void 0 ? void 0 : routes[prefix];
27
- if (handler === void 0) {
28
- outgoing.writeHead(404).end();
29
- return;
30
- }
31
- const abort = new AbortController();
32
- outgoing.on("close", () => {
33
- if (!outgoing.writableFinished) abort.abort();
34
- });
35
- const headers = new Headers();
36
- for (const [name, value] of Object.entries(incoming.headers)) if (Array.isArray(value)) value.forEach((item) => headers.append(name, item));
37
- else if (value !== void 0) headers.set(name, value);
38
- const hasBody = incoming.method !== "GET" && incoming.method !== "HEAD";
39
- const init = {
40
- method: incoming.method ?? "GET",
41
- headers,
42
- signal: abort.signal
43
- };
44
- const withBody = () => ({
45
- ...init,
46
- body: bodyOf(incoming),
47
- duplex: "half"
48
- });
49
- const request = new Request(url, hasBody ? withBody() : init);
50
- try {
51
- const response = await handler(request);
52
- outgoing.writeHead(response.status, Object.fromEntries(response.headers));
53
- if (response.body === null) {
54
- outgoing.end();
55
- return;
56
- }
57
- outgoing.flushHeaders();
58
- const reader = response.body.getReader();
59
- abort.signal.addEventListener("abort", () => void reader.cancel().catch(() => void 0), { once: true });
60
- for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) outgoing.write(chunk.value);
61
- outgoing.end();
62
- } catch {
63
- if (!outgoing.headersSent) outgoing.writeHead(500);
64
- outgoing.end();
65
- }
66
- }
67
- /** A request that the handler does not read stays unread, so that Node.js discards it after the response. */
68
- function bodyOf(incoming) {
69
- let open = true;
70
- let reading = false;
71
- const close = (end) => {
72
- if (open) end();
73
- open = false;
74
- };
75
- return new ReadableStream({
76
- pull(controller) {
77
- if (!reading) {
78
- reading = true;
79
- incoming.on("data", (chunk) => {
80
- if (!open) return;
81
- if (!(chunk instanceof Uint8Array)) return close(() => controller.error(/* @__PURE__ */ new TypeError("a chunk of the request body is not bytes")));
82
- controller.enqueue(chunk);
83
- incoming.pause();
84
- });
85
- incoming.on("end", () => close(() => controller.close()));
86
- incoming.on("error", (error) => close(() => controller.error(error)));
87
- }
88
- incoming.resume();
89
- },
90
- cancel() {
91
- open = false;
92
- incoming.resume();
93
- }
94
- }, { highWaterMark: 0 });
95
- }
96
- export { serve };
@@ -1,5 +0,0 @@
1
- import type { JsonObject } from "../json.js";
2
- import type { Checks } from "./check.js";
3
- import type { Channel } from "./mcp.js";
4
- export declare function signingKeysOf(card: JsonObject): readonly JsonObject[];
5
- export declare function checkServerCard(checks: Checks, url: string, channel: Channel): Promise<JsonObject | undefined>;