@yaebal/test 0.1.0 → 0.2.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.
- package/README.md +333 -160
- package/lib/api.d.ts +83 -0
- package/lib/api.d.ts.map +1 -0
- package/lib/api.js +186 -0
- package/lib/api.js.map +1 -0
- package/lib/api.test.d.ts +2 -0
- package/lib/api.test.d.ts.map +1 -0
- package/lib/api.test.js +131 -0
- package/lib/api.test.js.map +1 -0
- package/lib/bot-messages.d.ts +42 -0
- package/lib/bot-messages.d.ts.map +1 -0
- package/lib/bot-messages.js +72 -0
- package/lib/bot-messages.js.map +1 -0
- package/lib/chat-actor.d.ts +45 -0
- package/lib/chat-actor.d.ts.map +1 -0
- package/lib/chat-actor.js +72 -0
- package/lib/chat-actor.js.map +1 -0
- package/lib/clock.d.ts +22 -0
- package/lib/clock.d.ts.map +1 -0
- package/lib/clock.js +72 -0
- package/lib/clock.js.map +1 -0
- package/lib/clock.test.d.ts +2 -0
- package/lib/clock.test.d.ts.map +1 -0
- package/lib/clock.test.js +69 -0
- package/lib/clock.test.js.map +1 -0
- package/lib/env.d.ts +100 -0
- package/lib/env.d.ts.map +1 -0
- package/lib/env.js +164 -0
- package/lib/env.js.map +1 -0
- package/lib/env.test.d.ts +2 -0
- package/lib/env.test.d.ts.map +1 -0
- package/lib/env.test.js +302 -0
- package/lib/env.test.js.map +1 -0
- package/lib/fetch.d.ts +3 -0
- package/lib/fetch.d.ts.map +1 -0
- package/lib/fetch.js +12 -0
- package/lib/fetch.js.map +1 -0
- package/lib/index.d.ts +19 -205
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +19 -391
- package/lib/index.js.map +1 -1
- package/lib/internal.d.ts +25 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +19 -0
- package/lib/internal.js.map +1 -0
- package/lib/keyboard.d.ts +15 -0
- package/lib/keyboard.d.ts.map +1 -0
- package/lib/keyboard.js +25 -0
- package/lib/keyboard.js.map +1 -0
- package/lib/keyboard.test.d.ts +2 -0
- package/lib/keyboard.test.d.ts.map +1 -0
- package/lib/keyboard.test.js +31 -0
- package/lib/keyboard.test.js.map +1 -0
- package/lib/normalize.d.ts +10 -0
- package/lib/normalize.d.ts.map +1 -0
- package/lib/normalize.js +27 -0
- package/lib/normalize.js.map +1 -0
- package/lib/reactions.d.ts +3 -0
- package/lib/reactions.d.ts.map +1 -0
- package/lib/reactions.js +17 -0
- package/lib/reactions.js.map +1 -0
- package/lib/updates.d.ts +126 -0
- package/lib/updates.d.ts.map +1 -0
- package/lib/updates.js +200 -0
- package/lib/updates.js.map +1 -0
- package/lib/updates.test.d.ts +2 -0
- package/lib/updates.test.d.ts.map +1 -0
- package/lib/updates.test.js +72 -0
- package/lib/updates.test.js.map +1 -0
- package/lib/user-actor.d.ts +188 -0
- package/lib/user-actor.d.ts.map +1 -0
- package/lib/user-actor.js +465 -0
- package/lib/user-actor.js.map +1 -0
- package/lib/webhook.d.ts +18 -0
- package/lib/webhook.d.ts.map +1 -0
- package/lib/webhook.js +25 -0
- package/lib/webhook.js.map +1 -0
- package/lib/webhook.test.d.ts +2 -0
- package/lib/webhook.test.d.ts.map +1 -0
- package/lib/webhook.test.js +36 -0
- package/lib/webhook.test.js.map +1 -0
- package/package.json +7 -5
- package/src/api.test.ts +180 -0
- package/src/api.ts +289 -0
- package/src/bot-messages.ts +117 -0
- package/src/chat-actor.ts +101 -0
- package/src/clock.test.ts +80 -0
- package/src/clock.ts +118 -0
- package/src/env.test.ts +370 -0
- package/src/env.ts +235 -0
- package/src/fetch.ts +11 -0
- package/src/index.ts +79 -630
- package/src/internal.ts +43 -0
- package/src/keyboard.test.ts +35 -0
- package/src/keyboard.ts +38 -0
- package/src/normalize.ts +34 -0
- package/src/reactions.ts +18 -0
- package/src/updates.test.ts +107 -0
- package/src/updates.ts +354 -0
- package/src/user-actor.ts +702 -0
- package/src/webhook.test.ts +54 -0
- package/src/webhook.ts +48 -0
- package/lib/index.test.d.ts +0 -2
- package/lib/index.test.d.ts.map +0 -1
- package/lib/index.test.js +0 -213
- package/lib/index.test.js.map +0 -1
- package/src/index.test.ts +0 -320
package/src/env.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import type { Update } from "@yaebal/core";
|
|
2
|
+
import { type Api, type Composer, Context, type NextFn } from "@yaebal/core";
|
|
3
|
+
import type { RecordedCall } from "./api.js";
|
|
4
|
+
import { type MockApi, type MockResult, mockApi } from "./api.js";
|
|
5
|
+
import {
|
|
6
|
+
attachBotMessageTracking,
|
|
7
|
+
type BotMessage,
|
|
8
|
+
type BotMessageTracker,
|
|
9
|
+
type LastBotMessageQuery,
|
|
10
|
+
} from "./bot-messages.js";
|
|
11
|
+
import { ChatActor, type ChatType, type CreateChatOptions } from "./chat-actor.js";
|
|
12
|
+
import { installTestClock, type TestClock } from "./clock.js";
|
|
13
|
+
import type { ActorHost } from "./internal.js";
|
|
14
|
+
import { type BuildUserOptions, buildUser, createUpdate, detectUpdateType } from "./updates.js";
|
|
15
|
+
import { UserActor } from "./user-actor.js";
|
|
16
|
+
|
|
17
|
+
export type { ApiErrorSentinel, MockResult, OnApiOptions, RecordedCall } from "./api.js";
|
|
18
|
+
export { apiError, isApiErrorSentinel, TestApiError } from "./api.js";
|
|
19
|
+
export type { BotMessage, LastBotMessageQuery } from "./bot-messages.js";
|
|
20
|
+
export {
|
|
21
|
+
ChatActor,
|
|
22
|
+
type ChatMembership,
|
|
23
|
+
type ChatType,
|
|
24
|
+
type CreateChatOptions,
|
|
25
|
+
} from "./chat-actor.js";
|
|
26
|
+
export { installTestClock, type TestClock } from "./clock.js";
|
|
27
|
+
export {
|
|
28
|
+
type MediaOptions,
|
|
29
|
+
type MessageOptions,
|
|
30
|
+
UserActor,
|
|
31
|
+
UserInChatScope,
|
|
32
|
+
UserOnMessageScope,
|
|
33
|
+
} from "./user-actor.js";
|
|
34
|
+
|
|
35
|
+
/** a satellite plugin's canned test fixtures — pass to {@link createTestEnv} via `options.packs`. explicit (you always list which packs apply), matching yaebal's "no implicit plugin wiring" rule. */
|
|
36
|
+
export interface TestPack<C extends Context = Context> {
|
|
37
|
+
readonly name: string;
|
|
38
|
+
setup(env: TestEnv<C>): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TestEnvOptions<C extends Context = Context> {
|
|
42
|
+
/** seed permanent per-method replies — same as calling `env.onApi(method, result)` for each entry. */
|
|
43
|
+
results?: Record<string, MockResult>;
|
|
44
|
+
/** throw instead of returning `{}` when a method has no builtin default and no override. default `false`. */
|
|
45
|
+
strictApi?: boolean;
|
|
46
|
+
/** throw if an actor-driven update falls through the whole bot with no handler consuming it. default `false`. */
|
|
47
|
+
strictDispatch?: boolean;
|
|
48
|
+
/** satellite-plugin test packs to apply (see {@link TestPack}). */
|
|
49
|
+
packs?: TestPack<C>[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* the actor-driven test environment: wraps a `Composer`/`Bot`, intercepts every outgoing api
|
|
54
|
+
* call (no real HTTP), and hands out {@link UserActor}/{@link ChatActor} actors that dispatch
|
|
55
|
+
* real updates through it — the way real Telegram users would.
|
|
56
|
+
*/
|
|
57
|
+
export class TestEnv<C extends Context = Context> implements ActorHost {
|
|
58
|
+
readonly api: Api;
|
|
59
|
+
readonly apiCalls: RecordedCall[];
|
|
60
|
+
readonly hooks: MockApi["hooks"];
|
|
61
|
+
readonly users: UserActor[] = [];
|
|
62
|
+
readonly chats: ChatActor[] = [];
|
|
63
|
+
|
|
64
|
+
private readonly bot: Composer<C>;
|
|
65
|
+
private readonly mock: MockApi;
|
|
66
|
+
private readonly botMessages: BotMessageTracker;
|
|
67
|
+
private readonly strictDispatch: boolean;
|
|
68
|
+
private readonly postDispatchHooks: Array<(update: Update) => void | Promise<void>> = [];
|
|
69
|
+
private clock: TestClock | undefined;
|
|
70
|
+
private messageIdCounter = 0;
|
|
71
|
+
|
|
72
|
+
constructor(bot: Composer<C>, options: TestEnvOptions<C> = {}) {
|
|
73
|
+
this.bot = bot;
|
|
74
|
+
this.strictDispatch = options.strictDispatch ?? false;
|
|
75
|
+
|
|
76
|
+
this.mock = mockApi({
|
|
77
|
+
results: options.results,
|
|
78
|
+
strictApi: options.strictApi,
|
|
79
|
+
now: () => this.now(),
|
|
80
|
+
});
|
|
81
|
+
this.api = this.mock.api;
|
|
82
|
+
this.apiCalls = this.mock.calls;
|
|
83
|
+
this.hooks = this.mock.hooks;
|
|
84
|
+
this.botMessages = attachBotMessageTracking(this.api, (chatId) => this.resolveChatType(chatId));
|
|
85
|
+
|
|
86
|
+
for (const pack of options.packs ?? []) pack.setup(this);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** the clock's current time if {@link advanceTime} has installed one, else the real `Date.now()`. */
|
|
90
|
+
now(): number {
|
|
91
|
+
return this.clock ? this.clock.now() : Date.now();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
nextMessageId(): number {
|
|
95
|
+
return ++this.messageIdCounter;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private resolveChatType(chatId: number): ChatType {
|
|
99
|
+
for (const chat of this.chats) if (chat.id === chatId) return chat.type;
|
|
100
|
+
for (const user of this.users) if (user.pmChat.id === chatId) return "private";
|
|
101
|
+
|
|
102
|
+
return "private";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** create a user actor. auto-allocates an id unless one is given. */
|
|
106
|
+
createUser(options: BuildUserOptions = {}): UserActor {
|
|
107
|
+
const user = new UserActor(this, buildUser(options));
|
|
108
|
+
this.users.push(user);
|
|
109
|
+
return user;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** create a chat actor (group/supergroup/channel/private). */
|
|
113
|
+
createChat(options: CreateChatOptions): ChatActor {
|
|
114
|
+
const chat = new ChatActor(this, options);
|
|
115
|
+
this.chats.push(chat);
|
|
116
|
+
return chat;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* dispatch a raw {@link Update} through the bot — the escape hatch beneath every actor
|
|
121
|
+
* method, for update shapes the actors don't cover (business connections, exotic service
|
|
122
|
+
* messages, whatever ships in the next Bot API release before the actors catch up).
|
|
123
|
+
*/
|
|
124
|
+
async dispatch(update: Update): Promise<void> {
|
|
125
|
+
const ctx = new Context({
|
|
126
|
+
api: this.api,
|
|
127
|
+
update,
|
|
128
|
+
updateType: detectUpdateType(update),
|
|
129
|
+
}) as unknown as C;
|
|
130
|
+
|
|
131
|
+
let reachedEnd = false;
|
|
132
|
+
const sentinel: NextFn = async () => {
|
|
133
|
+
reachedEnd = true;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
await this.bot.toMiddleware()(ctx, sentinel);
|
|
137
|
+
|
|
138
|
+
if (this.strictDispatch && reachedEnd) {
|
|
139
|
+
throw new Error(
|
|
140
|
+
`TestEnv: no handler consumed a "${detectUpdateType(update)}" update (strictDispatch is on)`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const hook of this.postDispatchHooks) await hook(update);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** alias for {@link dispatch} — ships an arbitrary update payload, puregram-style naming. */
|
|
148
|
+
inject(update: Update): Promise<void> {
|
|
149
|
+
return this.dispatch(update);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** run `fn` after every actor-driven (or `dispatch`ed) update finishes. hooks compose in registration order. */
|
|
153
|
+
onPostDispatch(fn: (update: Update) => void | Promise<void>): void {
|
|
154
|
+
this.postDispatchHooks.push(fn);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** override a method's reply. permanent unless `opts.times` is given. */
|
|
158
|
+
onApi(...args: Parameters<MockApi["onApi"]>): void {
|
|
159
|
+
this.mock.onApi(...args);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** drop a method's overrides (or every method's, if none given). */
|
|
163
|
+
offApi(method?: string): void {
|
|
164
|
+
this.mock.offApi(method);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** the most recent recorded call, optionally filtered to a method. */
|
|
168
|
+
lastApiCall(method?: string): RecordedCall | undefined {
|
|
169
|
+
return this.mock.lastCall(method);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** every recorded call to a given method, in call order. */
|
|
173
|
+
callsTo(method: string): RecordedCall[] {
|
|
174
|
+
return this.mock.callsTo(method);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** empty `apiCalls` and drop tracked bot messages — useful between logical phases of a test. */
|
|
178
|
+
clearApiCalls(): void {
|
|
179
|
+
this.mock.reset();
|
|
180
|
+
this.botMessages.clear();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** the bot's most recent `send*`/`forwardMessage`/`copyMessage`, optionally filtered; kept in sync with later edits. */
|
|
184
|
+
lastBotMessage(query?: LastBotMessageQuery): BotMessage | undefined {
|
|
185
|
+
return this.botMessages.lastBotMessage(query);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** look up a specific bot message by `(chat_id, message_id)`. */
|
|
189
|
+
botMessage(chatId: number, messageId: number): BotMessage | undefined {
|
|
190
|
+
return this.botMessages.botMessage(chatId, messageId);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* arm the virtual clock (a no-op if one is already installed) — call this *before* triggering
|
|
195
|
+
* the code that schedules the timer you plan to fast-forward with {@link advanceTime}, the
|
|
196
|
+
* same way you'd call `vi.useFakeTimers()` before the code under test runs. a timer scheduled
|
|
197
|
+
* against the real clock before this call is invisible to `advanceTime` — it was never handed
|
|
198
|
+
* to the virtual scheduler.
|
|
199
|
+
*/
|
|
200
|
+
useFakeTimers(): void {
|
|
201
|
+
this.clock ??= installTestClock();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** advance the (auto-armed) virtual clock by `ms`, firing due timers. see {@link installTestClock}. */
|
|
205
|
+
async advanceTime(ms: number): Promise<void> {
|
|
206
|
+
this.useFakeTimers();
|
|
207
|
+
await (this.clock as TestClock).advance(ms);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** did the bot answer `answerPreCheckoutQuery` for `preCheckoutQueryId` with `ok: true`? used by `sendSuccessfulPayment`. */
|
|
211
|
+
answeredPreCheckoutQuery(preCheckoutQueryId: string): boolean {
|
|
212
|
+
return this.callsTo("answerPreCheckoutQuery").some(
|
|
213
|
+
(call) =>
|
|
214
|
+
!call.error &&
|
|
215
|
+
call.params?.pre_checkout_query_id === preCheckoutQueryId &&
|
|
216
|
+
call.params?.ok === true,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** restore the virtual clock (if one was installed) to real timers. call in `afterEach`/teardown. */
|
|
221
|
+
shutdown(): void {
|
|
222
|
+
this.clock?.restore();
|
|
223
|
+
this.clock = undefined;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** create a {@link TestEnv} wrapping `bot` — the main entry point of `@yaebal/test`. */
|
|
228
|
+
export function createTestEnv<C extends Context = Context>(
|
|
229
|
+
bot: Composer<C>,
|
|
230
|
+
options?: TestEnvOptions<C>,
|
|
231
|
+
): TestEnv<C> {
|
|
232
|
+
return new TestEnv(bot, options);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export { createUpdate };
|
package/src/fetch.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** stub `globalThis.fetch` for the duration of `fn`, restoring the original afterwards (even on throw). */
|
|
2
|
+
export async function withFetch<T>(handler: typeof fetch, fn: () => T | Promise<T>): Promise<T> {
|
|
3
|
+
const realFetch = globalThis.fetch;
|
|
4
|
+
globalThis.fetch = handler;
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
return await fn();
|
|
8
|
+
} finally {
|
|
9
|
+
globalThis.fetch = realFetch;
|
|
10
|
+
}
|
|
11
|
+
}
|