@capybara-hq/testing 0.1.0-rc.34 → 0.1.0-rc.36
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/dist/fake-dial-in-machine.d.ts +79 -0
- package/dist/fake-dial-in-machine.js +194 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { TestKernel } from "./kernel-helpers.js";
|
|
2
|
+
/** The protocol of the `fake-dial-in` fixture provider (`permissions.dialIn.protocol`). */
|
|
3
|
+
export declare const FAKE_DIAL_IN_PROTOCOL = "fake-dial-in/1";
|
|
4
|
+
export interface FakeDialInMachineOptions {
|
|
5
|
+
/** The kernel's `connectUrl` (a `ws://` URL). */
|
|
6
|
+
url: string;
|
|
7
|
+
/** The machine credential `POST /api/v1/device/token` handed over. */
|
|
8
|
+
credential: string;
|
|
9
|
+
/** The protocol of the upgrade header and the hello (default {@link FAKE_DIAL_IN_PROTOCOL}). */
|
|
10
|
+
protocol?: string;
|
|
11
|
+
/** The hello's `body`: the provider's own. */
|
|
12
|
+
hello: unknown;
|
|
13
|
+
/** The largest frame this machine accepts, announced in the hello (default 1 MiB). */
|
|
14
|
+
maxFrameBytes?: number;
|
|
15
|
+
/** Random per program run (default: a fresh one). */
|
|
16
|
+
programRunId?: string;
|
|
17
|
+
}
|
|
18
|
+
type RequestHandler = (body: unknown) => unknown | Promise<unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* A machine on the dial-in channel for kernel tests: connects to a deployment, sends the hello
|
|
21
|
+
* envelope, waits for the kernel's `ready` and then answers each `req` through the handler set
|
|
22
|
+
* with {@link onRequest} (a `res` with the same `id`). Bodies are opaque to it, like they are to
|
|
23
|
+
* the kernel.
|
|
24
|
+
*/
|
|
25
|
+
export declare class FakeDialInMachine {
|
|
26
|
+
private readonly ws;
|
|
27
|
+
/** Resolves when the socket closes, with the close code and reason the kernel sent. */
|
|
28
|
+
readonly closed: Promise<{
|
|
29
|
+
code: number;
|
|
30
|
+
reason: string;
|
|
31
|
+
}>;
|
|
32
|
+
/** The kernel's id of this connection (from `ready`). */
|
|
33
|
+
readonly connectionId: string;
|
|
34
|
+
/** The connection's effective frame capacity (from `ready`): at most the announced one. */
|
|
35
|
+
readonly maxFrameBytes: number;
|
|
36
|
+
/** The bodies of the `req` frames received, in order. */
|
|
37
|
+
readonly requests: unknown[];
|
|
38
|
+
/** The ids of the `cancel` frames received, in order. */
|
|
39
|
+
readonly cancels: string[];
|
|
40
|
+
/** The reasons of the `stop` frames received, in order. */
|
|
41
|
+
readonly stops: ("drain" | "stop")[];
|
|
42
|
+
/** Socket errors after `ready` (kept, so an error never goes unhandled), in order. */
|
|
43
|
+
readonly errors: Error[];
|
|
44
|
+
/** Kernel frames that were not JSON, in order (the kernel never sends one). */
|
|
45
|
+
readonly invalid: string[];
|
|
46
|
+
private handler;
|
|
47
|
+
private constructor();
|
|
48
|
+
/**
|
|
49
|
+
* Connects and resolves once the kernel answered `ready`. Rejects when the upgrade is refused
|
|
50
|
+
* (the message carries the HTTP status and body) or the socket closes first (the close code).
|
|
51
|
+
*/
|
|
52
|
+
static connect(opts: FakeDialInMachineOptions): Promise<FakeDialInMachine>;
|
|
53
|
+
/** Sets the handler of `req` bodies; its result (or its thrown message) is the `res` body. */
|
|
54
|
+
onRequest(fn: RequestHandler): void;
|
|
55
|
+
/** Sends an unsolicited `event` frame with `event` as its body. */
|
|
56
|
+
send(event: unknown): Promise<void>;
|
|
57
|
+
/** Closes the socket (a normal close) and resolves when it is closed. */
|
|
58
|
+
close(): Promise<void>;
|
|
59
|
+
private sendFrame;
|
|
60
|
+
private onMessage;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Signs a fake machine in over HTTP like `capybara-device login` does: `authorize` (public),
|
|
64
|
+
* `confirm` (the person's session, `session` is its `Cookie` header value) into a new device of
|
|
65
|
+
* `provider` (the plugin name) in `orgId`, then `token`. Returns the machine credential and the
|
|
66
|
+
* new device's id; connect with {@link FakeDialInMachine.connect} at `kernel`'s `connectUrl`.
|
|
67
|
+
*/
|
|
68
|
+
export declare function signInFakeMachine(kernel: TestKernel, opts: {
|
|
69
|
+
orgId: string;
|
|
70
|
+
session: string;
|
|
71
|
+
provider: string;
|
|
72
|
+
protocol: string;
|
|
73
|
+
name?: string;
|
|
74
|
+
}): Promise<{
|
|
75
|
+
credential: string;
|
|
76
|
+
deviceId: string;
|
|
77
|
+
connectUrl: string;
|
|
78
|
+
}>;
|
|
79
|
+
export {};
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { WebSocket } from "ws";
|
|
3
|
+
/** The protocol of the `fake-dial-in` fixture provider (`permissions.dialIn.protocol`). */
|
|
4
|
+
export const FAKE_DIAL_IN_PROTOCOL = "fake-dial-in/1";
|
|
5
|
+
/**
|
|
6
|
+
* A machine on the dial-in channel for kernel tests: connects to a deployment, sends the hello
|
|
7
|
+
* envelope, waits for the kernel's `ready` and then answers each `req` through the handler set
|
|
8
|
+
* with {@link onRequest} (a `res` with the same `id`). Bodies are opaque to it, like they are to
|
|
9
|
+
* the kernel.
|
|
10
|
+
*/
|
|
11
|
+
export class FakeDialInMachine {
|
|
12
|
+
ws;
|
|
13
|
+
/** Resolves when the socket closes, with the close code and reason the kernel sent. */
|
|
14
|
+
closed;
|
|
15
|
+
/** The kernel's id of this connection (from `ready`). */
|
|
16
|
+
connectionId;
|
|
17
|
+
/** The connection's effective frame capacity (from `ready`): at most the announced one. */
|
|
18
|
+
maxFrameBytes;
|
|
19
|
+
/** The bodies of the `req` frames received, in order. */
|
|
20
|
+
requests = [];
|
|
21
|
+
/** The ids of the `cancel` frames received, in order. */
|
|
22
|
+
cancels = [];
|
|
23
|
+
/** The reasons of the `stop` frames received, in order. */
|
|
24
|
+
stops = [];
|
|
25
|
+
/** Socket errors after `ready` (kept, so an error never goes unhandled), in order. */
|
|
26
|
+
errors = [];
|
|
27
|
+
/** Kernel frames that were not JSON, in order (the kernel never sends one). */
|
|
28
|
+
invalid = [];
|
|
29
|
+
handler = () => ({});
|
|
30
|
+
constructor(ws, ready, closed) {
|
|
31
|
+
this.ws = ws;
|
|
32
|
+
this.connectionId = ready.connectionId;
|
|
33
|
+
this.maxFrameBytes = ready.maxFrameBytes;
|
|
34
|
+
this.closed = closed;
|
|
35
|
+
// the connect promise's listener is gone once settled: an error later must not go unhandled
|
|
36
|
+
ws.on("error", (e) => this.errors.push(e));
|
|
37
|
+
ws.on("message", (data) => this.onMessage(data.toString()));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Connects and resolves once the kernel answered `ready`. Rejects when the upgrade is refused
|
|
41
|
+
* (the message carries the HTTP status and body) or the socket closes first (the close code).
|
|
42
|
+
*/
|
|
43
|
+
static connect(opts) {
|
|
44
|
+
const protocol = opts.protocol ?? FAKE_DIAL_IN_PROTOCOL;
|
|
45
|
+
const maxFrameBytes = opts.maxFrameBytes ?? 1024 * 1024;
|
|
46
|
+
const ws = new WebSocket(opts.url, {
|
|
47
|
+
headers: {
|
|
48
|
+
authorization: `Bearer ${opts.credential}`,
|
|
49
|
+
"x-capybara-device-protocol": protocol,
|
|
50
|
+
},
|
|
51
|
+
maxPayload: maxFrameBytes,
|
|
52
|
+
});
|
|
53
|
+
const closed = new Promise((resolve) => {
|
|
54
|
+
ws.once("close", (code, reason) => resolve({ code, reason: reason.toString() }));
|
|
55
|
+
});
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
ws.once("unexpected-response", (_req, res) => {
|
|
58
|
+
const chunks = [];
|
|
59
|
+
res.on("data", (c) => chunks.push(c));
|
|
60
|
+
res.on("end", () => reject(new Error(`upgrade refused: HTTP ${res.statusCode}: ${Buffer.concat(chunks).toString()}`)));
|
|
61
|
+
});
|
|
62
|
+
ws.once("error", reject);
|
|
63
|
+
void closed.then((c) => reject(new Error(`closed before ready: ${c.code} ${c.reason}`.trim())));
|
|
64
|
+
ws.once("open", () => {
|
|
65
|
+
ws.send(JSON.stringify({
|
|
66
|
+
type: "hello",
|
|
67
|
+
protocol,
|
|
68
|
+
programRunId: opts.programRunId ?? randomUUID(),
|
|
69
|
+
program: { version: "0.0.0-fake", os: "linux", arch: "x64" },
|
|
70
|
+
transport: { maxFrameBytes },
|
|
71
|
+
body: opts.hello,
|
|
72
|
+
}));
|
|
73
|
+
ws.once("message", (data) => {
|
|
74
|
+
let frame;
|
|
75
|
+
try {
|
|
76
|
+
frame = JSON.parse(data.toString());
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
ws.terminate();
|
|
80
|
+
reject(new Error("the kernel's first frame is not JSON"));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (frame.type !== "ready" ||
|
|
84
|
+
typeof frame.connectionId !== "string" ||
|
|
85
|
+
typeof frame.maxFrameBytes !== "number") {
|
|
86
|
+
ws.terminate();
|
|
87
|
+
reject(new Error(`expected a ready frame, got ${data.toString()}`));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
resolve(new FakeDialInMachine(ws, { connectionId: frame.connectionId, maxFrameBytes: frame.maxFrameBytes }, closed));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/** Sets the handler of `req` bodies; its result (or its thrown message) is the `res` body. */
|
|
96
|
+
onRequest(fn) {
|
|
97
|
+
this.handler = fn;
|
|
98
|
+
}
|
|
99
|
+
/** Sends an unsolicited `event` frame with `event` as its body. */
|
|
100
|
+
async send(event) {
|
|
101
|
+
await this.sendFrame({ type: "event", body: event });
|
|
102
|
+
}
|
|
103
|
+
/** Closes the socket (a normal close) and resolves when it is closed. */
|
|
104
|
+
async close() {
|
|
105
|
+
if (this.ws.readyState !== WebSocket.CLOSED)
|
|
106
|
+
this.ws.close(1000);
|
|
107
|
+
await this.closed;
|
|
108
|
+
}
|
|
109
|
+
sendFrame(frame) {
|
|
110
|
+
return new Promise((resolve, reject) => this.ws.send(JSON.stringify(frame), (e) => (e ? reject(e) : resolve())));
|
|
111
|
+
}
|
|
112
|
+
onMessage(text) {
|
|
113
|
+
let frame;
|
|
114
|
+
try {
|
|
115
|
+
frame = JSON.parse(text);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
this.invalid.push(text);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (frame.type === "req" && frame.id !== undefined) {
|
|
122
|
+
const id = frame.id;
|
|
123
|
+
this.requests.push(frame.body);
|
|
124
|
+
void (async () => {
|
|
125
|
+
let body;
|
|
126
|
+
try {
|
|
127
|
+
body = await this.handler(frame.body);
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
body = { error: e instanceof Error ? e.message : String(e) };
|
|
131
|
+
}
|
|
132
|
+
// a socket closed meanwhile has nobody to answer
|
|
133
|
+
if (this.ws.readyState === WebSocket.OPEN)
|
|
134
|
+
await this.sendFrame({ type: "res", id, body: body ?? null });
|
|
135
|
+
})();
|
|
136
|
+
}
|
|
137
|
+
else if (frame.type === "cancel" && frame.id !== undefined) {
|
|
138
|
+
this.cancels.push(frame.id);
|
|
139
|
+
}
|
|
140
|
+
else if (frame.type === "stop" && frame.reason !== undefined) {
|
|
141
|
+
this.stops.push(frame.reason);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function expectOk(res, what) {
|
|
146
|
+
if (res.statusCode !== 200) {
|
|
147
|
+
throw new Error(`${what}: expected HTTP 200, got ${res.statusCode}: ${res.body}`);
|
|
148
|
+
}
|
|
149
|
+
return JSON.parse(res.body);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Signs a fake machine in over HTTP like `capybara-device login` does: `authorize` (public),
|
|
153
|
+
* `confirm` (the person's session, `session` is its `Cookie` header value) into a new device of
|
|
154
|
+
* `provider` (the plugin name) in `orgId`, then `token`. Returns the machine credential and the
|
|
155
|
+
* new device's id; connect with {@link FakeDialInMachine.connect} at `kernel`'s `connectUrl`.
|
|
156
|
+
*/
|
|
157
|
+
export async function signInFakeMachine(kernel, opts) {
|
|
158
|
+
const csrf = { "x-capybara-csrf": "1", "content-type": "application/json" };
|
|
159
|
+
const name = opts.name ?? "fake-machine";
|
|
160
|
+
const authorized = expectOk(await kernel.http.inject({
|
|
161
|
+
method: "POST",
|
|
162
|
+
url: "/api/v1/device/authorize",
|
|
163
|
+
headers: csrf,
|
|
164
|
+
payload: {
|
|
165
|
+
name,
|
|
166
|
+
os: "linux",
|
|
167
|
+
arch: "x64",
|
|
168
|
+
programVersion: "0.0.0-fake",
|
|
169
|
+
protocol: opts.protocol,
|
|
170
|
+
},
|
|
171
|
+
}), "device authorize");
|
|
172
|
+
expectOk(await kernel.http.inject({
|
|
173
|
+
method: "POST",
|
|
174
|
+
url: "/api/v1/device/confirm",
|
|
175
|
+
headers: { ...csrf, cookie: opts.session },
|
|
176
|
+
payload: {
|
|
177
|
+
userCode: authorized.userCode,
|
|
178
|
+
orgId: opts.orgId,
|
|
179
|
+
provider: opts.provider,
|
|
180
|
+
name,
|
|
181
|
+
},
|
|
182
|
+
}), "device confirm");
|
|
183
|
+
const grant = expectOk(await kernel.http.inject({
|
|
184
|
+
method: "POST",
|
|
185
|
+
url: "/api/v1/device/token",
|
|
186
|
+
headers: csrf,
|
|
187
|
+
payload: { deviceCode: authorized.deviceCode },
|
|
188
|
+
}), "device token");
|
|
189
|
+
return {
|
|
190
|
+
credential: String(grant.credential),
|
|
191
|
+
deviceId: String(grant.deviceId),
|
|
192
|
+
connectUrl: String(grant.connectUrl),
|
|
193
|
+
};
|
|
194
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export { createFreshDatabase, startPostgresContainer, startTestPostgres, startTe
|
|
|
8
8
|
export { createDualReplicaHarness, type DualReplicaOptions, type ExtraReplicaHandle, type Harness, REPLICA_HEADER, type ReplicaHandle, type ReplicaResponse, replicaOf, } from "./dual-replica.js";
|
|
9
9
|
export { assertExtensionSecretsReachable, extensionDeploymentSecretValues, } from "./extension-config.js";
|
|
10
10
|
export { FAKE_CHANNEL_BAD_SECRET, FAKE_WEBHOOK_SIGNATURE_HEADER, type FakeChannelCheckCall, type FakeChannelFailMatch, type FakeChannelPost, type FakeChannelSent, type FakeChannelSentFile, type FakeChannelServer, type FakeChannelServerOptions, type FakeChannelStream, type FakeChannelUser, type FakeDirectoryBook, type FakeSenderProfile, fakeCardAction, fakeDirectoryMember, fakeDm, fakeGroupMessage, fakeWebhookChallenge, fakeWebhookEvents, signFakeWebhook, startFakeChannelServer, } from "./fake-channel.js";
|
|
11
|
+
export { FAKE_DIAL_IN_PROTOCOL, FakeDialInMachine, type FakeDialInMachineOptions, signInFakeMachine, } from "./fake-dial-in-machine.js";
|
|
11
12
|
export { type FakeE2BCall, type FakeE2BMatch, type FakeE2BOptions, type FakeE2BSandbox, type FakeE2BServer, startFakeE2B, } from "./fake-e2b.js";
|
|
12
13
|
export { type FakeGitCommitOptions, type FakeGitRequest, type FakeGitServer, type FakeGitServerOptions, startFakeGitServer, } from "./fake-git-server.js";
|
|
13
14
|
export { encryptLarkWebhook, type FakeLarkAck, type FakeLarkApi, type FakeLarkButton, type FakeLarkCall, type FakeLarkDepartment, type FakeLarkFailure, type FakeLarkMessage, type FakeLarkOptions, type FakeLarkServer, type FakeLarkUpload, type FakeLarkUser, findLarkButton, larkCardAction, larkMessageEvent, signLarkWebhook, startFakeLark, } from "./fake-lark.js";
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { createFreshDatabase, startPostgresContainer, startTestPostgres, startTe
|
|
|
8
8
|
export { createDualReplicaHarness, REPLICA_HEADER, replicaOf, } from "./dual-replica.js";
|
|
9
9
|
export { assertExtensionSecretsReachable, extensionDeploymentSecretValues, } from "./extension-config.js";
|
|
10
10
|
export { FAKE_CHANNEL_BAD_SECRET, FAKE_WEBHOOK_SIGNATURE_HEADER, fakeCardAction, fakeDirectoryMember, fakeDm, fakeGroupMessage, fakeWebhookChallenge, fakeWebhookEvents, signFakeWebhook, startFakeChannelServer, } from "./fake-channel.js";
|
|
11
|
+
export { FAKE_DIAL_IN_PROTOCOL, FakeDialInMachine, signInFakeMachine, } from "./fake-dial-in-machine.js";
|
|
11
12
|
export { startFakeE2B, } from "./fake-e2b.js";
|
|
12
13
|
export { startFakeGitServer, } from "./fake-git-server.js";
|
|
13
14
|
export { encryptLarkWebhook, findLarkButton, larkCardAction, larkMessageEvent, signLarkWebhook, startFakeLark, } from "./fake-lark.js";
|
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.36",
|
|
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,17 +47,17 @@
|
|
|
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.73"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/pg": "^8.23.1",
|
|
54
54
|
"@types/ws": "^8.18.2",
|
|
55
|
-
"@capybara-hq/plugin-client": "^0.1.0-rc.
|
|
56
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
55
|
+
"@capybara-hq/plugin-client": "^0.1.0-rc.8",
|
|
56
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.57"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"vitest": "^5.0.5",
|
|
60
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
60
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.57",
|
|
61
61
|
"@capybara-hq/plugin-tools": "^0.1.0-rc.20"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|