@capybara-hq/testing 0.1.0-rc.40 → 0.1.0-rc.41
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.
|
@@ -46,7 +46,8 @@ createServer((conn) => {
|
|
|
46
46
|
conn.on("error", () => {});
|
|
47
47
|
}).listen(socket);
|
|
48
48
|
`;
|
|
49
|
-
|
|
49
|
+
/** The service id the conformance starts (a provider receives ids: `<extension>.<name>`). */
|
|
50
|
+
const SERVICE = "conformance.echo";
|
|
50
51
|
/** What the kernel sends with every `ifRunning` ensure: the name, nothing to run. */
|
|
51
52
|
const nameOnly = (name) => ({
|
|
52
53
|
name,
|
|
@@ -14,6 +14,11 @@ export interface FakeDialInMachineOptions {
|
|
|
14
14
|
maxFrameBytes?: number;
|
|
15
15
|
/** Random per program run (default: a fresh one). */
|
|
16
16
|
programRunId?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Offer binary `stream-data` in the hello (`transport.binaryStreamData`); once the kernel's
|
|
19
|
+
* ready agrees, stream data goes both ways as binary messages (default false: JSON only).
|
|
20
|
+
*/
|
|
21
|
+
binaryStreamData?: boolean;
|
|
17
22
|
}
|
|
18
23
|
type RequestHandler = (body: unknown) => unknown | Promise<unknown>;
|
|
19
24
|
/**
|
|
@@ -25,6 +30,8 @@ type RequestHandler = (body: unknown) => unknown | Promise<unknown>;
|
|
|
25
30
|
export declare class FakeMachineStream {
|
|
26
31
|
readonly streamId: string;
|
|
27
32
|
private readonly sendFrame;
|
|
33
|
+
/** Sends one message of this stream (JSON or binary, as the connection agreed). */
|
|
34
|
+
private readonly sendData;
|
|
28
35
|
/** The messages received from the kernel, in order. */
|
|
29
36
|
readonly received: Uint8Array[];
|
|
30
37
|
/** Grant the kernel one message back for each message received (default true). */
|
|
@@ -43,7 +50,9 @@ export declare class FakeMachineStream {
|
|
|
43
50
|
private readonly countWaiters;
|
|
44
51
|
private messageHandler;
|
|
45
52
|
/** @internal */
|
|
46
|
-
constructor(streamId: string, sendFrame: (frame: unknown) => Promise<void
|
|
53
|
+
constructor(streamId: string, sendFrame: (frame: unknown) => Promise<void>,
|
|
54
|
+
/** Sends one message of this stream (JSON or binary, as the connection agreed). */
|
|
55
|
+
sendData: (data: Uint8Array) => Promise<void>);
|
|
47
56
|
/** Called for each message received (after it is recorded in {@link received}). */
|
|
48
57
|
onMessage(fn: (m: Uint8Array) => void): void;
|
|
49
58
|
/** Resolves once at least `n` messages were received. */
|
|
@@ -61,6 +70,8 @@ export declare class FakeMachineStream {
|
|
|
61
70
|
messages?: number;
|
|
62
71
|
reason?: string;
|
|
63
72
|
}): void;
|
|
73
|
+
/** @internal one message from the kernel (a JSON or a binary `stream-data`). */
|
|
74
|
+
onData(m: Uint8Array): void;
|
|
64
75
|
/** @internal the machine answered `stream-opened` ok: queued messages may go out. */
|
|
65
76
|
activate(): void;
|
|
66
77
|
/** @internal the stream ended (closed, or the socket went away). */
|
|
@@ -89,6 +100,10 @@ export declare class FakeDialInMachine {
|
|
|
89
100
|
readonly connectionId: string;
|
|
90
101
|
/** The connection's effective frame capacity (from `ready`): at most the announced one. */
|
|
91
102
|
readonly maxFrameBytes: number;
|
|
103
|
+
/** Whether hello and ready agreed on binary `stream-data`. */
|
|
104
|
+
readonly binaryStreamData: boolean;
|
|
105
|
+
/** Binary `stream-data` messages received from the kernel. */
|
|
106
|
+
binaryReceived: number;
|
|
92
107
|
/** The bodies of the `req` frames received, in order. */
|
|
93
108
|
readonly requests: unknown[];
|
|
94
109
|
/** The ids of the `cancel` frames received, in order. */
|
|
@@ -97,7 +112,10 @@ export declare class FakeDialInMachine {
|
|
|
97
112
|
readonly stops: ("drain" | "stop")[];
|
|
98
113
|
/** Socket errors after `ready` (kept, so an error never goes unhandled), in order. */
|
|
99
114
|
readonly errors: Error[];
|
|
100
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Kernel messages it could not take, in order: text that was not JSON, and binary messages
|
|
117
|
+
* (hex) that were not an agreed `stream-data` (the kernel never sends either).
|
|
118
|
+
*/
|
|
101
119
|
readonly invalid: string[];
|
|
102
120
|
private handler;
|
|
103
121
|
private streamHandler;
|
|
@@ -127,6 +145,11 @@ export declare class FakeDialInMachine {
|
|
|
127
145
|
/** Closes the socket (a normal close) and resolves when it is closed. */
|
|
128
146
|
close(): Promise<void>;
|
|
129
147
|
private sendFrame;
|
|
148
|
+
/**
|
|
149
|
+
* A binary message: a `stream-data` of a connection that agreed on binary (anything else is
|
|
150
|
+
* kept in {@link invalid} as hex).
|
|
151
|
+
*/
|
|
152
|
+
private onBinary;
|
|
130
153
|
private onMessage;
|
|
131
154
|
private openStream;
|
|
132
155
|
}
|
|
@@ -2,6 +2,13 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { WebSocket } from "ws";
|
|
3
3
|
/** The protocol of the `fake-dial-in` fixture provider (`permissions.dialIn.protocol`). */
|
|
4
4
|
export const FAKE_DIAL_IN_PROTOCOL = "fake-dial-in/1";
|
|
5
|
+
/** The tag of a binary `stream-data` message (the SDK's `DIAL_IN_STREAM_DATA_TAG`). */
|
|
6
|
+
const STREAM_DATA_TAG = 1;
|
|
7
|
+
/** A binary `stream-data` message: tag, id length, id (ASCII), the bytes (the SDK's encoding). */
|
|
8
|
+
function encodeStreamData(streamId, data) {
|
|
9
|
+
const id = Buffer.from(streamId, "ascii");
|
|
10
|
+
return Buffer.concat([Buffer.from([STREAM_DATA_TAG, id.length]), id, data]);
|
|
11
|
+
}
|
|
5
12
|
/**
|
|
6
13
|
* The credit each side of a dial-in stream starts with for the other (the SDK's
|
|
7
14
|
* `DIAL_IN_STREAM_INITIAL_CREDIT`, repeated so this module stays a plain test helper).
|
|
@@ -16,6 +23,7 @@ const INITIAL_CREDIT = 8;
|
|
|
16
23
|
export class FakeMachineStream {
|
|
17
24
|
streamId;
|
|
18
25
|
sendFrame;
|
|
26
|
+
sendData;
|
|
19
27
|
/** The messages received from the kernel, in order. */
|
|
20
28
|
received = [];
|
|
21
29
|
/** Grant the kernel one message back for each message received (default true). */
|
|
@@ -32,9 +40,12 @@ export class FakeMachineStream {
|
|
|
32
40
|
countWaiters = [];
|
|
33
41
|
messageHandler;
|
|
34
42
|
/** @internal */
|
|
35
|
-
constructor(streamId, sendFrame
|
|
43
|
+
constructor(streamId, sendFrame,
|
|
44
|
+
/** Sends one message of this stream (JSON or binary, as the connection agreed). */
|
|
45
|
+
sendData) {
|
|
36
46
|
this.streamId = streamId;
|
|
37
47
|
this.sendFrame = sendFrame;
|
|
48
|
+
this.sendData = sendData;
|
|
38
49
|
this.closed = new Promise((resolve) => {
|
|
39
50
|
this.resolveClosed = resolve;
|
|
40
51
|
});
|
|
@@ -54,7 +65,7 @@ export class FakeMachineStream {
|
|
|
54
65
|
if (this.isClosed)
|
|
55
66
|
return Promise.reject(new Error("stream closed"));
|
|
56
67
|
return new Promise((resolve, reject) => {
|
|
57
|
-
this.waiting.push({ data:
|
|
68
|
+
this.waiting.push({ data: new Uint8Array(message), resolve, reject });
|
|
58
69
|
this.flush();
|
|
59
70
|
});
|
|
60
71
|
}
|
|
@@ -76,17 +87,7 @@ export class FakeMachineStream {
|
|
|
76
87
|
if (this.isClosed)
|
|
77
88
|
return;
|
|
78
89
|
if (frame.type === "stream-data" && typeof frame.data === "string") {
|
|
79
|
-
|
|
80
|
-
this.received.push(m);
|
|
81
|
-
for (const w of this.countWaiters.splice(0)) {
|
|
82
|
-
if (this.received.length >= w.n)
|
|
83
|
-
w.resolve();
|
|
84
|
-
else
|
|
85
|
-
this.countWaiters.push(w);
|
|
86
|
-
}
|
|
87
|
-
if (this.autoCredit)
|
|
88
|
-
void this.grant(1).catch(() => { });
|
|
89
|
-
this.messageHandler?.(m);
|
|
90
|
+
this.onData(new Uint8Array(Buffer.from(frame.data, "base64")));
|
|
90
91
|
}
|
|
91
92
|
else if (frame.type === "stream-credit" && typeof frame.messages === "number") {
|
|
92
93
|
this.credit += frame.messages;
|
|
@@ -96,6 +97,21 @@ export class FakeMachineStream {
|
|
|
96
97
|
this.end(frame.reason ?? "closed");
|
|
97
98
|
}
|
|
98
99
|
}
|
|
100
|
+
/** @internal one message from the kernel (a JSON or a binary `stream-data`). */
|
|
101
|
+
onData(m) {
|
|
102
|
+
if (this.isClosed)
|
|
103
|
+
return;
|
|
104
|
+
this.received.push(m);
|
|
105
|
+
for (const w of this.countWaiters.splice(0)) {
|
|
106
|
+
if (this.received.length >= w.n)
|
|
107
|
+
w.resolve();
|
|
108
|
+
else
|
|
109
|
+
this.countWaiters.push(w);
|
|
110
|
+
}
|
|
111
|
+
if (this.autoCredit)
|
|
112
|
+
void this.grant(1).catch(() => { });
|
|
113
|
+
this.messageHandler?.(m);
|
|
114
|
+
}
|
|
99
115
|
/** @internal the machine answered `stream-opened` ok: queued messages may go out. */
|
|
100
116
|
activate() {
|
|
101
117
|
this.opened = true;
|
|
@@ -114,7 +130,7 @@ export class FakeMachineStream {
|
|
|
114
130
|
while (this.opened && this.credit > 0 && this.waiting.length > 0 && !this.isClosed) {
|
|
115
131
|
const w = this.waiting.shift();
|
|
116
132
|
this.credit--;
|
|
117
|
-
this.
|
|
133
|
+
this.sendData(w.data).then(w.resolve, w.reject);
|
|
118
134
|
}
|
|
119
135
|
}
|
|
120
136
|
}
|
|
@@ -132,6 +148,10 @@ export class FakeDialInMachine {
|
|
|
132
148
|
connectionId;
|
|
133
149
|
/** The connection's effective frame capacity (from `ready`): at most the announced one. */
|
|
134
150
|
maxFrameBytes;
|
|
151
|
+
/** Whether hello and ready agreed on binary `stream-data`. */
|
|
152
|
+
binaryStreamData;
|
|
153
|
+
/** Binary `stream-data` messages received from the kernel. */
|
|
154
|
+
binaryReceived = 0;
|
|
135
155
|
/** The bodies of the `req` frames received, in order. */
|
|
136
156
|
requests = [];
|
|
137
157
|
/** The ids of the `cancel` frames received, in order. */
|
|
@@ -140,7 +160,10 @@ export class FakeDialInMachine {
|
|
|
140
160
|
stops = [];
|
|
141
161
|
/** Socket errors after `ready` (kept, so an error never goes unhandled), in order. */
|
|
142
162
|
errors = [];
|
|
143
|
-
/**
|
|
163
|
+
/**
|
|
164
|
+
* Kernel messages it could not take, in order: text that was not JSON, and binary messages
|
|
165
|
+
* (hex) that were not an agreed `stream-data` (the kernel never sends either).
|
|
166
|
+
*/
|
|
144
167
|
invalid = [];
|
|
145
168
|
handler = () => ({});
|
|
146
169
|
streamHandler;
|
|
@@ -156,10 +179,21 @@ export class FakeDialInMachine {
|
|
|
156
179
|
this.ws = ws;
|
|
157
180
|
this.connectionId = ready.connectionId;
|
|
158
181
|
this.maxFrameBytes = ready.maxFrameBytes;
|
|
182
|
+
this.binaryStreamData = ready.binaryStreamData;
|
|
159
183
|
this.closed = closed;
|
|
160
184
|
// the connect promise's listener is gone once settled: an error later must not go unhandled
|
|
161
185
|
ws.on("error", (e) => this.errors.push(e));
|
|
162
|
-
ws.on("message", (data) =>
|
|
186
|
+
ws.on("message", (data, isBinary) => {
|
|
187
|
+
const buf = Array.isArray(data)
|
|
188
|
+
? Buffer.concat(data)
|
|
189
|
+
: Buffer.isBuffer(data)
|
|
190
|
+
? data
|
|
191
|
+
: Buffer.from(data);
|
|
192
|
+
if (isBinary)
|
|
193
|
+
this.onBinary(buf);
|
|
194
|
+
else
|
|
195
|
+
this.onMessage(buf.toString());
|
|
196
|
+
});
|
|
163
197
|
void closed.then(() => {
|
|
164
198
|
for (const st of this.streams.values())
|
|
165
199
|
st.end("socket closed");
|
|
@@ -197,7 +231,7 @@ export class FakeDialInMachine {
|
|
|
197
231
|
protocol,
|
|
198
232
|
programRunId: opts.programRunId ?? randomUUID(),
|
|
199
233
|
program: { version: "0.0.0-fake", os: "linux", arch: "x64" },
|
|
200
|
-
transport: { maxFrameBytes },
|
|
234
|
+
transport: { maxFrameBytes, ...(opts.binaryStreamData && { binaryStreamData: true }) },
|
|
201
235
|
body: opts.hello,
|
|
202
236
|
}));
|
|
203
237
|
ws.once("message", (data) => {
|
|
@@ -217,7 +251,11 @@ export class FakeDialInMachine {
|
|
|
217
251
|
reject(new Error(`expected a ready frame, got ${data.toString()}`));
|
|
218
252
|
return;
|
|
219
253
|
}
|
|
220
|
-
resolve(new FakeDialInMachine(ws, {
|
|
254
|
+
resolve(new FakeDialInMachine(ws, {
|
|
255
|
+
connectionId: frame.connectionId,
|
|
256
|
+
maxFrameBytes: frame.maxFrameBytes,
|
|
257
|
+
binaryStreamData: opts.binaryStreamData === true && frame.binaryStreamData === true,
|
|
258
|
+
}, closed));
|
|
221
259
|
});
|
|
222
260
|
});
|
|
223
261
|
});
|
|
@@ -246,6 +284,24 @@ export class FakeDialInMachine {
|
|
|
246
284
|
sendFrame(frame) {
|
|
247
285
|
return new Promise((resolve, reject) => this.ws.send(JSON.stringify(frame), (e) => (e ? reject(e) : resolve())));
|
|
248
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* A binary message: a `stream-data` of a connection that agreed on binary (anything else is
|
|
289
|
+
* kept in {@link invalid} as hex).
|
|
290
|
+
*/
|
|
291
|
+
onBinary(buf) {
|
|
292
|
+
const len = buf[1] ?? 0;
|
|
293
|
+
// binary the connection did not agree on is the kernel's mistake: kept, never delivered
|
|
294
|
+
if (!this.binaryStreamData ||
|
|
295
|
+
buf.length < 2 ||
|
|
296
|
+
buf[0] !== STREAM_DATA_TAG ||
|
|
297
|
+
buf.length < 2 + len) {
|
|
298
|
+
this.invalid.push(buf.toString("hex"));
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
this.binaryReceived++;
|
|
302
|
+
const streamId = buf.subarray(2, 2 + len).toString("ascii");
|
|
303
|
+
this.streams.get(streamId)?.onData(new Uint8Array(buf.subarray(2 + len)));
|
|
304
|
+
}
|
|
249
305
|
onMessage(text) {
|
|
250
306
|
let frame;
|
|
251
307
|
try {
|
|
@@ -292,7 +348,13 @@ export class FakeDialInMachine {
|
|
|
292
348
|
}
|
|
293
349
|
}
|
|
294
350
|
openStream(streamId, body) {
|
|
295
|
-
const st = new FakeMachineStream(streamId, (f) => this.sendFrame(f))
|
|
351
|
+
const st = new FakeMachineStream(streamId, (f) => this.sendFrame(f), (data) => this.binaryStreamData
|
|
352
|
+
? new Promise((resolve, reject) => this.ws.send(encodeStreamData(streamId, data), (e) => (e ? reject(e) : resolve())))
|
|
353
|
+
: this.sendFrame({
|
|
354
|
+
type: "stream-data",
|
|
355
|
+
streamId,
|
|
356
|
+
data: Buffer.from(data).toString("base64"),
|
|
357
|
+
}));
|
|
296
358
|
void st.closed.then(() => this.streams.delete(streamId));
|
|
297
359
|
let error;
|
|
298
360
|
if (this.streamHandler === undefined)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/testing",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.41",
|
|
4
4
|
"description": "Test helpers for Capybara: test containers, dual-replica harness and fake third-party servers",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -47,18 +47,18 @@
|
|
|
47
47
|
"tsx": "^4.23.15",
|
|
48
48
|
"ws": "^8.22.0",
|
|
49
49
|
"zod": "^4.6.7",
|
|
50
|
-
"@capybara-hq/kernel": "^0.1.0-rc.
|
|
50
|
+
"@capybara-hq/kernel": "^0.1.0-rc.90"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/pg": "^8.23.1",
|
|
54
54
|
"@types/ws": "^8.18.2",
|
|
55
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
55
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.72",
|
|
56
56
|
"@capybara-hq/plugin-client": "^0.1.0-rc.9"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"vitest": "^5.0.5",
|
|
60
|
-
"@capybara-hq/plugin-
|
|
61
|
-
"@capybara-hq/plugin-
|
|
60
|
+
"@capybara-hq/plugin-tools": "^0.1.0-rc.25",
|
|
61
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.72"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|
|
64
64
|
"@capybara-hq/plugin-tools": {
|