@capybara-hq/testing 0.1.0-rc.40 → 0.1.0-rc.42

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
- const SERVICE = "conformance-echo";
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,12 +112,20 @@ 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
- /** Kernel frames that were not JSON, in order (the kernel never sends one). */
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;
104
122
  /** Leave every `stream-open` unanswered (a machine that never opens streams). */
105
123
  ignoreStreamOpens: boolean;
124
+ /**
125
+ * While set, each `stream-open` is answered only once this promise resolves (a machine slow to
126
+ * open its service): a test holds the kernel's stream open here.
127
+ */
128
+ streamOpenGate: Promise<void> | undefined;
106
129
  /** The ids of the `stream-open` frames received, in order. */
107
130
  readonly streamOpens: string[];
108
131
  /** The ids of the `stream-close` frames received, in order (also of streams never opened). */
@@ -127,6 +150,11 @@ export declare class FakeDialInMachine {
127
150
  /** Closes the socket (a normal close) and resolves when it is closed. */
128
151
  close(): Promise<void>;
129
152
  private sendFrame;
153
+ /**
154
+ * A binary message: a `stream-data` of a connection that agreed on binary (anything else is
155
+ * kept in {@link invalid} as hex).
156
+ */
157
+ private onBinary;
130
158
  private onMessage;
131
159
  private openStream;
132
160
  }
@@ -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: Buffer.from(message).toString("base64"), resolve, reject });
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
- const m = new Uint8Array(Buffer.from(frame.data, "base64"));
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.sendFrame({ type: "stream-data", streamId: this.streamId, data: w.data }).then(w.resolve, w.reject);
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,12 +160,20 @@ 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
- /** Kernel frames that were not JSON, in order (the kernel never sends one). */
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;
147
170
  /** Leave every `stream-open` unanswered (a machine that never opens streams). */
148
171
  ignoreStreamOpens = false;
172
+ /**
173
+ * While set, each `stream-open` is answered only once this promise resolves (a machine slow to
174
+ * open its service): a test holds the kernel's stream open here.
175
+ */
176
+ streamOpenGate;
149
177
  /** The ids of the `stream-open` frames received, in order. */
150
178
  streamOpens = [];
151
179
  /** The ids of the `stream-close` frames received, in order (also of streams never opened). */
@@ -156,10 +184,21 @@ export class FakeDialInMachine {
156
184
  this.ws = ws;
157
185
  this.connectionId = ready.connectionId;
158
186
  this.maxFrameBytes = ready.maxFrameBytes;
187
+ this.binaryStreamData = ready.binaryStreamData;
159
188
  this.closed = closed;
160
189
  // the connect promise's listener is gone once settled: an error later must not go unhandled
161
190
  ws.on("error", (e) => this.errors.push(e));
162
- ws.on("message", (data) => this.onMessage(data.toString()));
191
+ ws.on("message", (data, isBinary) => {
192
+ const buf = Array.isArray(data)
193
+ ? Buffer.concat(data)
194
+ : Buffer.isBuffer(data)
195
+ ? data
196
+ : Buffer.from(data);
197
+ if (isBinary)
198
+ this.onBinary(buf);
199
+ else
200
+ this.onMessage(buf.toString());
201
+ });
163
202
  void closed.then(() => {
164
203
  for (const st of this.streams.values())
165
204
  st.end("socket closed");
@@ -197,7 +236,7 @@ export class FakeDialInMachine {
197
236
  protocol,
198
237
  programRunId: opts.programRunId ?? randomUUID(),
199
238
  program: { version: "0.0.0-fake", os: "linux", arch: "x64" },
200
- transport: { maxFrameBytes },
239
+ transport: { maxFrameBytes, ...(opts.binaryStreamData && { binaryStreamData: true }) },
201
240
  body: opts.hello,
202
241
  }));
203
242
  ws.once("message", (data) => {
@@ -217,7 +256,11 @@ export class FakeDialInMachine {
217
256
  reject(new Error(`expected a ready frame, got ${data.toString()}`));
218
257
  return;
219
258
  }
220
- resolve(new FakeDialInMachine(ws, { connectionId: frame.connectionId, maxFrameBytes: frame.maxFrameBytes }, closed));
259
+ resolve(new FakeDialInMachine(ws, {
260
+ connectionId: frame.connectionId,
261
+ maxFrameBytes: frame.maxFrameBytes,
262
+ binaryStreamData: opts.binaryStreamData === true && frame.binaryStreamData === true,
263
+ }, closed));
221
264
  });
222
265
  });
223
266
  });
@@ -246,6 +289,24 @@ export class FakeDialInMachine {
246
289
  sendFrame(frame) {
247
290
  return new Promise((resolve, reject) => this.ws.send(JSON.stringify(frame), (e) => (e ? reject(e) : resolve())));
248
291
  }
292
+ /**
293
+ * A binary message: a `stream-data` of a connection that agreed on binary (anything else is
294
+ * kept in {@link invalid} as hex).
295
+ */
296
+ onBinary(buf) {
297
+ const len = buf[1] ?? 0;
298
+ // binary the connection did not agree on is the kernel's mistake: kept, never delivered
299
+ if (!this.binaryStreamData ||
300
+ buf.length < 2 ||
301
+ buf[0] !== STREAM_DATA_TAG ||
302
+ buf.length < 2 + len) {
303
+ this.invalid.push(buf.toString("hex"));
304
+ return;
305
+ }
306
+ this.binaryReceived++;
307
+ const streamId = buf.subarray(2, 2 + len).toString("ascii");
308
+ this.streams.get(streamId)?.onData(new Uint8Array(buf.subarray(2 + len)));
309
+ }
249
310
  onMessage(text) {
250
311
  let frame;
251
312
  try {
@@ -279,8 +340,15 @@ export class FakeDialInMachine {
279
340
  }
280
341
  else if (frame.type === "stream-open" && frame.streamId !== undefined) {
281
342
  this.streamOpens.push(frame.streamId);
282
- if (!this.ignoreStreamOpens)
283
- this.openStream(frame.streamId, frame.body);
343
+ if (!this.ignoreStreamOpens) {
344
+ const streamId = frame.streamId;
345
+ const body = frame.body;
346
+ const gate = this.streamOpenGate;
347
+ if (gate === undefined)
348
+ this.openStream(streamId, body);
349
+ else
350
+ void gate.then(() => this.openStream(streamId, body));
351
+ }
284
352
  }
285
353
  else if (frame.type.startsWith("stream-") && frame.streamId !== undefined) {
286
354
  if (frame.type === "stream-close")
@@ -292,7 +360,13 @@ export class FakeDialInMachine {
292
360
  }
293
361
  }
294
362
  openStream(streamId, body) {
295
- const st = new FakeMachineStream(streamId, (f) => this.sendFrame(f));
363
+ const st = new FakeMachineStream(streamId, (f) => this.sendFrame(f), (data) => this.binaryStreamData
364
+ ? new Promise((resolve, reject) => this.ws.send(encodeStreamData(streamId, data), (e) => (e ? reject(e) : resolve())))
365
+ : this.sendFrame({
366
+ type: "stream-data",
367
+ streamId,
368
+ data: Buffer.from(data).toString("base64"),
369
+ }));
296
370
  void st.closed.then(() => this.streams.delete(streamId));
297
371
  let error;
298
372
  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.40",
3
+ "version": "0.1.0-rc.42",
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.86"
50
+ "@capybara-hq/kernel": "^0.1.0-rc.92"
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.70",
56
- "@capybara-hq/plugin-client": "^0.1.0-rc.9"
55
+ "@capybara-hq/plugin-client": "^0.1.0-rc.9",
56
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.75"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "vitest": "^5.0.5",
60
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.70",
61
- "@capybara-hq/plugin-tools": "^0.1.0-rc.24"
60
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.75",
61
+ "@capybara-hq/plugin-tools": "^0.1.0-rc.25"
62
62
  },
63
63
  "peerDependenciesMeta": {
64
64
  "@capybara-hq/plugin-tools": {