@capybara-hq/testing 0.1.0-rc.2 → 0.1.0-rc.20
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/containers.d.ts +7 -2
- package/dist/containers.js +7 -3
- package/dist/dual-replica.d.ts +11 -2
- package/dist/dual-replica.js +48 -0
- package/dist/fake-channel.d.ts +31 -2
- package/dist/fake-channel.js +29 -2
- package/dist/fake-e2b.d.ts +34 -0
- package/dist/fake-e2b.js +95 -1
- package/dist/fake-git-server.d.ts +32 -4
- package/dist/fake-git-server.js +87 -19
- package/dist/fake-oauth-server.d.ts +16 -2
- package/dist/fake-oauth-server.js +21 -1
- package/dist/fake-telegram.d.ts +165 -0
- package/dist/fake-telegram.js +569 -0
- package/dist/fixtures.d.ts +21 -4
- package/dist/fixtures.js +30 -4
- package/dist/index.d.ts +7 -5
- package/dist/index.js +4 -2
- package/dist/kernel-helpers.d.ts +40 -16
- package/dist/kernel-helpers.js +44 -13
- package/dist/model-conformance.d.ts +5 -0
- package/dist/model-conformance.js +33 -1
- package/dist/replica-runner.js +16 -8
- package/dist/sandbox-native-conformance.d.ts +31 -0
- package/dist/sandbox-native-conformance.js +153 -0
- package/fixtures/extensions/fake-client/.capybara-plugin/plugin.json +11 -0
- package/fixtures/extensions/fake-client/client/styles/fake.css +11 -0
- package/fixtures/extensions/fake-client/package.json +12 -0
- package/fixtures/extensions/fake-client/src/client.tsx +51 -0
- package/fixtures/extensions/fake-client/src/index.ts +43 -0
- package/fixtures/extensions/fake-client/themes/fake.json +11 -0
- package/fixtures/extensions/fake-client-broken/.capybara-plugin/plugin.json +7 -0
- package/fixtures/extensions/fake-client-broken/package.json +12 -0
- package/fixtures/extensions/fake-client-broken/src/client.tsx +22 -0
- package/fixtures/extensions/fake-client-broken/src/index.ts +27 -0
- package/fixtures/extensions/fake-config-form/.capybara-plugin/plugin.json +11 -0
- package/fixtures/extensions/fake-config-form/package.json +12 -0
- package/fixtures/extensions/fake-config-form/src/index.ts +205 -0
- package/fixtures/extensions/fake-context/.capybara-plugin/plugin.json +7 -0
- package/fixtures/extensions/fake-context/package.json +12 -0
- package/fixtures/extensions/fake-context/src/index.ts +81 -0
- package/fixtures/extensions/fake-dep-provider/src/index.ts +1 -1
- package/fixtures/extensions/fake-dep-requirer/src/index.ts +1 -1
- package/fixtures/extensions/fake-embed/src/index.ts +26 -26
- package/fixtures/extensions/fake-im/.capybara-plugin/plugin.json +7 -1
- package/fixtures/extensions/fake-im/guides/fake-im-app.md +32 -0
- package/fixtures/extensions/fake-im/guides/fake-im-app.zh.md +32 -0
- package/fixtures/extensions/fake-im/src/channel.ts +3 -1
- package/fixtures/extensions/fake-im/src/checks.ts +94 -0
- package/fixtures/extensions/fake-im/src/index.ts +36 -1
- package/fixtures/extensions/fake-lifecycle/.capybara-plugin/plugin.json +7 -0
- package/fixtures/extensions/fake-lifecycle/migrations/0001_init.sql +16 -0
- package/fixtures/extensions/fake-lifecycle/package.json +12 -0
- package/fixtures/extensions/fake-lifecycle/src/index.ts +72 -0
- package/fixtures/extensions/fake-storage-user/src/index.ts +1 -1
- package/fixtures/extensions/fake-tools-ui/skills/fake-notes/SKILL.md +1 -1
- package/fixtures/extensions/fake-tools-ui/src/index.ts +62 -3
- package/fixtures/extensions/fake-web/.capybara-plugin/plugin.json +7 -0
- package/fixtures/extensions/fake-web/package.json +12 -0
- package/fixtures/extensions/fake-web/src/index.ts +22 -0
- package/fixtures/extensions/fake-web/src/web.ts +183 -0
- package/fixtures/extensions/fake-web-2/.capybara-plugin/plugin.json +7 -0
- package/fixtures/extensions/fake-web-2/package.json +12 -0
- package/fixtures/extensions/fake-web-2/src/index.ts +16 -0
- package/fixtures/extensions/fake-web-2/src/web.ts +183 -0
- package/fixtures/plugins/fake-theme/.capybara-plugin/plugin.json +7 -0
- package/fixtures/plugins/fake-theme/fonts/FakeSans.woff2 +1 -0
- package/fixtures/plugins/fake-theme/logo.svg +1 -0
- package/fixtures/plugins/fake-theme/package.json +8 -0
- package/fixtures/plugins/fake-theme/themes/neon-grid.json +13 -0
- package/fixtures/plugins/fake-theme/themes/paper.json +12 -0
- package/package.json +6 -5
package/dist/fake-git-server.js
CHANGED
|
@@ -4,7 +4,7 @@ import { appendFile, mkdtemp, readFile, rm } from "node:fs/promises";
|
|
|
4
4
|
import { createServer } from "node:http";
|
|
5
5
|
import { tmpdir } from "node:os";
|
|
6
6
|
import { join, posix } from "node:path";
|
|
7
|
-
const REPO_RE = /^[A-Za-z0-9][A-Za-z0-9._-]
|
|
7
|
+
const REPO_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*(\/[A-Za-z0-9][A-Za-z0-9._-]*)?$/;
|
|
8
8
|
/** Git runs with no user or system config, so a developer's settings never change the repos. */
|
|
9
9
|
function gitEnv(extra = {}) {
|
|
10
10
|
return {
|
|
@@ -56,7 +56,7 @@ function git(args, o = {}) {
|
|
|
56
56
|
}
|
|
57
57
|
});
|
|
58
58
|
}
|
|
59
|
-
/** Starts `git http-backend` as a CGI script for one request (smart HTTP,
|
|
59
|
+
/** Starts `git http-backend` as a CGI script for one request (smart HTTP, push enabled). */
|
|
60
60
|
function spawnCgi(root, req) {
|
|
61
61
|
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
62
62
|
const env = {
|
|
@@ -77,7 +77,9 @@ function spawnCgi(root, req) {
|
|
|
77
77
|
const enc = req.headers["content-encoding"];
|
|
78
78
|
if (typeof enc === "string")
|
|
79
79
|
env.HTTP_CONTENT_ENCODING = enc;
|
|
80
|
-
const p = spawn("git", ["http-backend"], {
|
|
80
|
+
const p = spawn("git", ["-c", "http.receivepack=true", "http-backend"], {
|
|
81
|
+
env: gitEnv(env),
|
|
82
|
+
});
|
|
81
83
|
// http-backend may exit without reading the whole body (a GET, or a request it refuses): the
|
|
82
84
|
// write then fails with EPIPE. Its exit status and output decide the response, not the pipe.
|
|
83
85
|
p.stdin.on("error", () => { });
|
|
@@ -129,8 +131,10 @@ function serveCgi(root, req, res) {
|
|
|
129
131
|
res.writeHead(h.status, h.headers);
|
|
130
132
|
res.write(head.subarray(h.bodyStart));
|
|
131
133
|
});
|
|
134
|
+
// A child that could not be spawned emits 'error' and then 'close': only the first writes a head
|
|
135
|
+
// (a second writeHead throws ERR_HTTP_HEADERS_SENT, an uncaught exception).
|
|
132
136
|
p.on("close", () => {
|
|
133
|
-
if (!headDone)
|
|
137
|
+
if (!headDone && !res.headersSent)
|
|
134
138
|
res.writeHead(502);
|
|
135
139
|
res.end();
|
|
136
140
|
});
|
|
@@ -167,8 +171,8 @@ const fastImportPath = (path) => /^"|[\n"\\]/.test(path)
|
|
|
167
171
|
? `"${path.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n")}"`
|
|
168
172
|
: path;
|
|
169
173
|
/**
|
|
170
|
-
* A fake git host: bare repos in a temp dir, served
|
|
171
|
-
* (smart HTTP, as CGI) over a loopback http server. Repos allow fetching any reachable commit by sha
|
|
174
|
+
* A fake git host: bare repos in a temp dir, served by the system `git http-backend`
|
|
175
|
+
* (smart HTTP, as CGI, pushes included) over a loopback http server. Repos allow fetching any reachable commit by sha
|
|
172
176
|
* (`uploadpack.allowReachableSHA1InWant`, as GitHub does). Needs `git` on PATH.
|
|
173
177
|
*
|
|
174
178
|
* Spawning git is what these tests spend their time on, and it slows down most under load, so the
|
|
@@ -176,26 +180,66 @@ const fastImportPath = (path) => /^"|[\n"\\]/.test(path)
|
|
|
176
180
|
* `git fast-import`, and a ref advertisement (`GET …/info/refs`) is answered by http-backend once per
|
|
177
181
|
* repo state, then repeated until the next commit to that repo.
|
|
178
182
|
*/
|
|
179
|
-
export async function startFakeGitServer() {
|
|
183
|
+
export async function startFakeGitServer(options = {}) {
|
|
180
184
|
const root = await mkdtemp(join(tmpdir(), "capybara-fake-git-"));
|
|
181
185
|
const requests = [];
|
|
182
186
|
let redirect = null;
|
|
187
|
+
let nextFail;
|
|
188
|
+
let nextRedirect;
|
|
189
|
+
const expectedBasic = options.requireBasic === undefined
|
|
190
|
+
? undefined
|
|
191
|
+
: `Basic ${Buffer.from(`${options.requireBasic.username}:${options.requireBasic.password}`).toString("base64")}`;
|
|
183
192
|
/** Ref advertisements by repo, then by request (path, query and `Git-Protocol`). */
|
|
184
193
|
const advertised = new Map();
|
|
185
194
|
/** Bumped by every commit to a repo: an answer read before the commit is not kept. */
|
|
186
195
|
const generation = new Map();
|
|
187
|
-
const repoOf = (pathname) => /^\/(
|
|
196
|
+
const repoOf = (pathname) => /^\/(.+)\.git\/info\/refs$/.exec(pathname)?.[1];
|
|
197
|
+
const pushedRepo = (pathname) => /^\/(.+)\.git\/git-receive-pack$/.exec(pathname)?.[1];
|
|
188
198
|
const server = createServer((req, res) => {
|
|
189
|
-
const
|
|
190
|
-
|
|
199
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
200
|
+
const authorized = expectedBasic === undefined || req.headers.authorization === expectedBasic;
|
|
201
|
+
const r = {
|
|
202
|
+
method: req.method ?? "GET",
|
|
203
|
+
path: req.url ?? "/",
|
|
204
|
+
authorized,
|
|
205
|
+
headerNames: Object.keys(req.headers),
|
|
206
|
+
};
|
|
207
|
+
if (options.recordAuthorization !== false && typeof req.headers.authorization === "string") {
|
|
191
208
|
r.authorization = req.headers.authorization;
|
|
209
|
+
}
|
|
210
|
+
const service = url.searchParams.get("service") ?? /\/(git-(?:upload|receive)-pack)$/.exec(url.pathname)?.[1];
|
|
211
|
+
if (service !== undefined && service !== null)
|
|
212
|
+
r.service = service;
|
|
192
213
|
requests.push(r);
|
|
214
|
+
if (nextFail !== undefined) {
|
|
215
|
+
const f = nextFail;
|
|
216
|
+
nextFail = undefined;
|
|
217
|
+
res.writeHead(f.status, { "content-type": "text/plain" }).end(f.body);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (nextRedirect !== undefined) {
|
|
221
|
+
const l = nextRedirect;
|
|
222
|
+
nextRedirect = undefined;
|
|
223
|
+
res.writeHead(302, { location: l }).end();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (!authorized) {
|
|
227
|
+
res.writeHead(401, { "www-authenticate": 'Basic realm="fake"' }).end();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
193
230
|
if (redirect !== null) {
|
|
194
231
|
res.writeHead(302, { location: redirect }).end();
|
|
195
232
|
return;
|
|
196
233
|
}
|
|
197
|
-
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
198
234
|
const repo = req.method === "GET" ? repoOf(url.pathname) : undefined;
|
|
235
|
+
const pushed = pushedRepo(url.pathname);
|
|
236
|
+
if (pushed !== undefined) {
|
|
237
|
+
// a push moves refs: advertisements read before it are stale
|
|
238
|
+
res.on("close", () => {
|
|
239
|
+
generation.set(pushed, (generation.get(pushed) ?? 0) + 1);
|
|
240
|
+
advertised.delete(pushed);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
199
243
|
if (repo === undefined) {
|
|
200
244
|
serveCgi(root, req, res);
|
|
201
245
|
return;
|
|
@@ -223,6 +267,13 @@ export async function startFakeGitServer() {
|
|
|
223
267
|
});
|
|
224
268
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
225
269
|
const port = server.address().port;
|
|
270
|
+
const stopServer = async () => {
|
|
271
|
+
await new Promise((resolve) => {
|
|
272
|
+
server.closeAllConnections();
|
|
273
|
+
server.close(() => resolve());
|
|
274
|
+
});
|
|
275
|
+
await rm(root, { recursive: true, force: true });
|
|
276
|
+
};
|
|
226
277
|
const repoDir = async (repo) => {
|
|
227
278
|
if (!REPO_RE.test(repo))
|
|
228
279
|
throw new Error(`invalid fake repo name: ${repo}`);
|
|
@@ -305,19 +356,36 @@ export async function startFakeGitServer() {
|
|
|
305
356
|
advertised.delete(repo);
|
|
306
357
|
return sha;
|
|
307
358
|
};
|
|
308
|
-
|
|
359
|
+
const fake = {
|
|
309
360
|
url: (repo) => `http://127.0.0.1:${port}/${repo}.git`,
|
|
310
361
|
commit,
|
|
311
362
|
setRedirect: (l) => {
|
|
312
363
|
redirect = l;
|
|
313
364
|
},
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
});
|
|
320
|
-
await rm(root, { recursive: true, force: true });
|
|
365
|
+
failNext: (status, body = "") => {
|
|
366
|
+
nextFail = { status, body };
|
|
367
|
+
},
|
|
368
|
+
redirectNext: (l) => {
|
|
369
|
+
nextRedirect = l;
|
|
321
370
|
},
|
|
371
|
+
refOf: async (repo, ref) => {
|
|
372
|
+
const gitDir = await repoDir(repo);
|
|
373
|
+
return git(["rev-parse", "--verify", "--quiet", ref], { env: { GIT_DIR: gitDir } }).then((sha) => sha || undefined, () => undefined);
|
|
374
|
+
},
|
|
375
|
+
requests,
|
|
376
|
+
stop: stopServer,
|
|
322
377
|
};
|
|
378
|
+
try {
|
|
379
|
+
for (const repo of options.repos ?? []) {
|
|
380
|
+
if (options.seed === false)
|
|
381
|
+
await repoDir(repo);
|
|
382
|
+
else
|
|
383
|
+
await commit(repo, { "README.md": "seed\n" });
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
await stopServer();
|
|
388
|
+
throw e;
|
|
389
|
+
}
|
|
390
|
+
return fake;
|
|
323
391
|
}
|
|
@@ -19,6 +19,15 @@ export interface FakeOAuthServerOptions {
|
|
|
19
19
|
scopesSupported?: string[];
|
|
20
20
|
/** 元数据里的 `code_challenge_methods_supported`;null = 不声明。默认 `["S256"]`。 */
|
|
21
21
|
pkceMethods?: string[] | null;
|
|
22
|
+
/**
|
|
23
|
+
* How the token endpoint answers. `"oauth"` (default): RFC 6749 (`token_type: "Bearer"`, errors
|
|
24
|
+
* as 400 / 401 `{ error }`). `"slack"`: like Slack's `oauth.v2.user.access`: success is
|
|
25
|
+
* `{ ok: true, token_type: "user", ... }`, every error is HTTP 200 `{ ok: false, error }` with
|
|
26
|
+
* Slack's codes where they differ (`invalid_code`, `invalid_refresh_token`, `invalid_client_id`).
|
|
27
|
+
*/
|
|
28
|
+
dialect?: "oauth" | "slack";
|
|
29
|
+
/** The `token_type` of successful token responses (default: `Bearer`; `user` with dialect slack). */
|
|
30
|
+
tokenType?: string;
|
|
22
31
|
}
|
|
23
32
|
/** 令牌端点收到的一次请求(不含任何密钥)。 */
|
|
24
33
|
export interface FakeTokenRequest {
|
|
@@ -28,8 +37,10 @@ export interface FakeTokenRequest {
|
|
|
28
37
|
scope: string | undefined;
|
|
29
38
|
/** 客户端认证方式:Basic 头、表单里的 client_secret、或只有 client_id。 */
|
|
30
39
|
clientAuth: "basic" | "post" | "none";
|
|
31
|
-
/** 本次响应的 HTTP
|
|
40
|
+
/** 本次响应的 HTTP 状态(dialect slack: errors are 200 too). */
|
|
32
41
|
status: number;
|
|
42
|
+
/** The error code answered, if any (as sent: Slack's code with dialect slack). */
|
|
43
|
+
error?: string;
|
|
33
44
|
}
|
|
34
45
|
export interface FakeOAuthServer {
|
|
35
46
|
/** issuer(`http://127.0.0.1:<port>`),也是各端点的基址。 */
|
|
@@ -45,7 +56,10 @@ export interface FakeOAuthServer {
|
|
|
45
56
|
issuedSecrets(): string[];
|
|
46
57
|
/** 作废全部 access token(refresh token 仍有效):资源服务随之返回 401。 */
|
|
47
58
|
revokeAccessTokens(): void;
|
|
48
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* 作废全部 refresh token:之后的刷新返回 `invalid_grant`(dialect slack: HTTP 200
|
|
61
|
+
* `{ ok: false, error: "invalid_refresh_token" }`)。
|
|
62
|
+
*/
|
|
49
63
|
revokeRefreshTokens(): void;
|
|
50
64
|
/** 下一次授权请求以该 OAuth 错误回跳。 */
|
|
51
65
|
denyNext(error: string, description?: string): void;
|
|
@@ -19,6 +19,16 @@ const json = (res, status, body) => {
|
|
|
19
19
|
*/
|
|
20
20
|
export async function startFakeOAuthServer(opts = {}) {
|
|
21
21
|
let dcr = opts.dcr ?? true;
|
|
22
|
+
const slack = opts.dialect === "slack";
|
|
23
|
+
const tokenType = opts.tokenType ?? (slack ? "user" : "Bearer");
|
|
24
|
+
/** RFC 6749 error code -> Slack's code for the same failure (dialect slack). */
|
|
25
|
+
const slackCode = (error, grantType) => {
|
|
26
|
+
if (error === "invalid_grant")
|
|
27
|
+
return grantType === "refresh_token" ? "invalid_refresh_token" : "invalid_code";
|
|
28
|
+
if (error === "invalid_client")
|
|
29
|
+
return "invalid_client_id";
|
|
30
|
+
return error;
|
|
31
|
+
};
|
|
22
32
|
const ttl = opts.accessTokenTtl ?? 3600;
|
|
23
33
|
const clients = new Map((opts.clients ?? []).map((c) => [
|
|
24
34
|
c.clientId,
|
|
@@ -53,7 +63,11 @@ export async function startFakeOAuthServer(opts = {}) {
|
|
|
53
63
|
const accessToken = rand("at_");
|
|
54
64
|
access.set(accessToken, { expiresAt: ttl > 0 ? Date.now() + ttl * 1000 : null });
|
|
55
65
|
secrets.add(accessToken);
|
|
56
|
-
const body = {
|
|
66
|
+
const body = {
|
|
67
|
+
...(slack && { ok: true }),
|
|
68
|
+
access_token: accessToken,
|
|
69
|
+
token_type: tokenType,
|
|
70
|
+
};
|
|
57
71
|
if (ttl > 0)
|
|
58
72
|
body.expires_in = ttl;
|
|
59
73
|
if (scope !== undefined)
|
|
@@ -108,7 +122,13 @@ export async function startFakeOAuthServer(opts = {}) {
|
|
|
108
122
|
return json(res, tokenFailures.status, tokenFailures.body ?? { message: "fake failure" });
|
|
109
123
|
}
|
|
110
124
|
const fail = (status, error) => {
|
|
125
|
+
if (slack) {
|
|
126
|
+
entry.status = 200;
|
|
127
|
+
entry.error = slackCode(error, grantType);
|
|
128
|
+
return json(res, 200, { ok: false, error: entry.error });
|
|
129
|
+
}
|
|
111
130
|
entry.status = status;
|
|
131
|
+
entry.error = error;
|
|
112
132
|
json(res, status, { error, error_description: `fake: ${error}` });
|
|
113
133
|
};
|
|
114
134
|
const client = authenticate(req, form);
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A programmable fake Telegram Bot API (for the Telegram channel plugin's tests).
|
|
3
|
+
*
|
|
4
|
+
* - **Routing**: `GET|POST /bot<token>/<method>` (method names are case-insensitive, like Telegram);
|
|
5
|
+
* parameters come from the query string and a JSON or form-urlencoded body. Every answer is the
|
|
6
|
+
* Bot API envelope: `{ ok: true, result }` or `{ ok: false, error_code, description, parameters? }`.
|
|
7
|
+
* An unknown token → 401 `Unauthorized`; an unknown method → 404 `Not Found`. Point a client at
|
|
8
|
+
* `apiUrl` (it appends `/bot<token>/<method>`).
|
|
9
|
+
* - **Methods**: `getMe`, `getWebhookInfo` (the url given to {@link FakeTelegramServer.setWebhook}),
|
|
10
|
+
* `getUpdates`, `sendMessage`, `editMessageText`, `answerCallbackQuery`.
|
|
11
|
+
* - **Long polling**: {@link FakeTelegramServer.push} queues an update per bot and assigns its
|
|
12
|
+
* `update_id`. `getUpdates` with `offset` confirms (drops) the lower ids; with `timeout` it waits
|
|
13
|
+
* until a push or the timeout; `allowed_updates` is remembered per bot like Telegram and updates of
|
|
14
|
+
* other types are dropped. While a webhook is set `getUpdates` answers 409; a second concurrent
|
|
15
|
+
* `getUpdates` for the same token ends the earlier one with 409 "Conflict: terminated by other
|
|
16
|
+
* getUpdates request", as Telegram does.
|
|
17
|
+
* - **Several bots**: {@link FakeTelegramOptions.bots}; each token has its own queue, offset, webhook
|
|
18
|
+
* and pollers.
|
|
19
|
+
* - **Failures**: {@link FakeTelegramServer.failNext} answers a method with an error envelope
|
|
20
|
+
* (`parameters.retry_after` / `migrate_to_chat_id`), {@link FakeTelegramServer.delay} holds it.
|
|
21
|
+
*
|
|
22
|
+
* Records: `messages` (sendMessage), `edits` (successful editMessageText), `calls` (every call with
|
|
23
|
+
* a known token), `callbackAnswers` (answerCallbackQuery ids).
|
|
24
|
+
*/
|
|
25
|
+
export interface FakeTelegramBot {
|
|
26
|
+
/** `<id>:<secret>`, as BotFather issues it. */
|
|
27
|
+
token: string;
|
|
28
|
+
id: number;
|
|
29
|
+
username: string;
|
|
30
|
+
firstName?: string;
|
|
31
|
+
/** getMe `can_read_all_group_messages` (privacy mode off); default false. */
|
|
32
|
+
canReadAllGroupMessages?: boolean;
|
|
33
|
+
/** getMe `has_topics_enabled` (forum topics in private chats); default false. */
|
|
34
|
+
hasTopicsEnabled?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/** One Bot API call with a known token (parameters merged from query string and body). */
|
|
37
|
+
export interface FakeTelegramCall {
|
|
38
|
+
method: string;
|
|
39
|
+
token: string;
|
|
40
|
+
params: Record<string, unknown>;
|
|
41
|
+
at: number;
|
|
42
|
+
}
|
|
43
|
+
/** A message the bot sent (`sendMessage`) or an edit of one (`editMessageText`). */
|
|
44
|
+
export interface FakeTelegramMessage {
|
|
45
|
+
chatId: number;
|
|
46
|
+
messageId: number;
|
|
47
|
+
text: string;
|
|
48
|
+
parseMode?: string;
|
|
49
|
+
replyMarkup?: unknown;
|
|
50
|
+
messageThreadId?: number;
|
|
51
|
+
replyToMessageId?: number;
|
|
52
|
+
at: number;
|
|
53
|
+
}
|
|
54
|
+
/** An injected error answer: `status` is the HTTP status, `errorCode` defaults to it. */
|
|
55
|
+
export interface FakeTelegramFailure {
|
|
56
|
+
status: number;
|
|
57
|
+
errorCode?: number;
|
|
58
|
+
description: string;
|
|
59
|
+
retryAfter?: number;
|
|
60
|
+
migrateToChatId?: number;
|
|
61
|
+
}
|
|
62
|
+
export interface FakeTelegramOptions {
|
|
63
|
+
/** The bots this fake serves; default one bot (`123456:fake-telegram-token`, `capybara_bot`). */
|
|
64
|
+
bots?: FakeTelegramBot[];
|
|
65
|
+
}
|
|
66
|
+
export interface FakeTelegramServer {
|
|
67
|
+
url: string;
|
|
68
|
+
/** Same as `url`: the client appends `/bot<token>/<method>`. */
|
|
69
|
+
apiUrl: string;
|
|
70
|
+
/** The first bot (the default for every `token?` argument). */
|
|
71
|
+
bot: FakeTelegramBot;
|
|
72
|
+
bots: FakeTelegramBot[];
|
|
73
|
+
/** sendMessage results, in order. */
|
|
74
|
+
messages: FakeTelegramMessage[];
|
|
75
|
+
/** Successful editMessageText calls, in order (markup-only edits are recorded too). */
|
|
76
|
+
edits: FakeTelegramMessage[];
|
|
77
|
+
calls: FakeTelegramCall[];
|
|
78
|
+
/** answerCallbackQuery `callback_query_id`s, in order. */
|
|
79
|
+
callbackAnswers: string[];
|
|
80
|
+
/** Queues an update for the bot, assigns its `update_id` and returns it; wakes a long poll. */
|
|
81
|
+
push(update: Record<string, unknown>, token?: string): number;
|
|
82
|
+
/** The highest offset passed to getUpdates (0 before any). */
|
|
83
|
+
confirmedOffset(token?: string): number;
|
|
84
|
+
/** Updates pushed and not yet confirmed. */
|
|
85
|
+
pending(token?: string): number;
|
|
86
|
+
/** In-flight getUpdates requests. */
|
|
87
|
+
activePollers(token?: string): number;
|
|
88
|
+
/**
|
|
89
|
+
* The peak number of overlapping getUpdates requests: a request that arrives while another is in
|
|
90
|
+
* flight counts as concurrent with it, although it ends the earlier one with 409.
|
|
91
|
+
*/
|
|
92
|
+
maxConcurrentPollers(token?: string): number;
|
|
93
|
+
/**
|
|
94
|
+
* The client TCP ports of the in-flight getUpdates requests; dual-replica tests map them to the
|
|
95
|
+
* replica process holding the poll.
|
|
96
|
+
*/
|
|
97
|
+
pollerPeerPorts(token?: string): number[];
|
|
98
|
+
/** Sets (or clears with null) the webhook getWebhookInfo reports; while set, getUpdates is 409. */
|
|
99
|
+
setWebhook(url: string | null, token?: string): void;
|
|
100
|
+
/** The next `count` (default 1) calls of `method` answer with `failure`. */
|
|
101
|
+
failNext(method: string, failure: FakeTelegramFailure, count?: number): void;
|
|
102
|
+
/** Every later call of `method` waits `ms` before answering (0 cancels). */
|
|
103
|
+
delay(method: string, ms: number): void;
|
|
104
|
+
close(): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
export type TgUser = {
|
|
107
|
+
id: number;
|
|
108
|
+
first_name: string;
|
|
109
|
+
last_name?: string;
|
|
110
|
+
username?: string;
|
|
111
|
+
is_bot?: boolean;
|
|
112
|
+
};
|
|
113
|
+
export type TgChat = {
|
|
114
|
+
id: number;
|
|
115
|
+
type: "private" | "group" | "supergroup";
|
|
116
|
+
title?: string;
|
|
117
|
+
is_forum?: boolean;
|
|
118
|
+
};
|
|
119
|
+
/** Starts the fake Telegram Bot API (listens on a random 127.0.0.1 port). */
|
|
120
|
+
export declare function startFakeTelegram(opts?: FakeTelegramOptions): Promise<FakeTelegramServer>;
|
|
121
|
+
/** An update with a private (direct) message from `from`. */
|
|
122
|
+
export declare function telegramPrivateMessage(from: TgUser, text: string, o?: {
|
|
123
|
+
messageId?: number;
|
|
124
|
+
date?: number;
|
|
125
|
+
}): Record<string, unknown>;
|
|
126
|
+
/**
|
|
127
|
+
* An update with a group message. `mention` (with or without `@`) adds a `mention` entity at its
|
|
128
|
+
* UTF-16 offset, prefixing `@<mention> ` when the text lacks it; `replyTo` sets `reply_to_message`
|
|
129
|
+
* (from the bot when `fromBot` is given); `messageThreadId` sets the thread, with
|
|
130
|
+
* `is_topic_message` defaulting to the chat's `is_forum`; `caption` turns the message into a photo
|
|
131
|
+
* whose caption carries the text and entities.
|
|
132
|
+
*/
|
|
133
|
+
export declare function telegramGroupMessage(chat: TgChat, from: TgUser, text: string, o?: {
|
|
134
|
+
messageId?: number;
|
|
135
|
+
mention?: string;
|
|
136
|
+
replyTo?: {
|
|
137
|
+
messageId: number;
|
|
138
|
+
fromBot?: FakeTelegramBot;
|
|
139
|
+
text?: string;
|
|
140
|
+
};
|
|
141
|
+
messageThreadId?: number;
|
|
142
|
+
isTopicMessage?: boolean;
|
|
143
|
+
caption?: boolean;
|
|
144
|
+
}): Record<string, unknown>;
|
|
145
|
+
/** An update with a callback query: `from` pressed an inline button with `data` on a bot message. */
|
|
146
|
+
export declare function telegramCallback(from: TgUser, message: {
|
|
147
|
+
chatId: number;
|
|
148
|
+
messageId: number;
|
|
149
|
+
chatType?: string;
|
|
150
|
+
messageThreadId?: number;
|
|
151
|
+
}, data: string, o?: {
|
|
152
|
+
id?: string;
|
|
153
|
+
}): Record<string, unknown>;
|
|
154
|
+
/** Finds an inline keyboard button in a `reply_markup` (object or JSON string). */
|
|
155
|
+
export declare function findTelegramButton(replyMarkup: unknown, pred: (b: {
|
|
156
|
+
text: string;
|
|
157
|
+
callback_data?: string;
|
|
158
|
+
url?: string;
|
|
159
|
+
}) => boolean): {
|
|
160
|
+
text: string;
|
|
161
|
+
callback_data?: string;
|
|
162
|
+
url?: string;
|
|
163
|
+
} | undefined;
|
|
164
|
+
/** The headers Telegram sends to a webhook set with `secret_token`. */
|
|
165
|
+
export declare function telegramWebhookHeaders(secret: string): Record<string, string>;
|