bostie.testing¶
bostie.testing
¶
Connection
dataclass
¶
Connection(request: Request, ws: WebSocketResponse, compress: Callable[[bytes], bytes] | None, live: bool = False, close_code: int | None = None, shard: int = 0, received: list[Any] = list[Any]())
A gateway connection, with the close code the client sent if it closed first.
received
class-attribute
instance-attribute
¶
FakeDiscord
¶
FakeDiscord(*, token: str = _FAKE_TOKEN, heartbeat_interval: int = 41250, compress: bool = False, voice: bool = False, voice_heartbeat_interval: float = 13750.0, voice_order: Order = 'state_first')
A local Discord gateway and REST API for tests, served inside async with.
voice=True adds voice: op 4 answers, voice routes, and in voice the voice
gateway and UDP server.
mute, deaf and suppress go into every voice state the fake sends for
the bot. ready_guilds are the guild ids READY lists, each on its own shard.
cdn_requests records the downloads of cdn_file URLs.
channels
instance-attribute
¶
users
instance-attribute
¶
members
instance-attribute
¶
dispatch
async
¶
Send a dispatch event on a shard, kept for RESUME replay; return its seq.
send_op
async
¶
Send a non-dispatch opcode, such as 7 (reconnect) or 9 (invalid session).
close_gateway
async
¶
close_gateway(code: int) -> None
Close the latest gateway connection with code.
drop_gateway
¶
drop_gateway() -> None
Abort the latest gateway connection's TCP stream without a close frame.
next_voice_state_update
async
¶
next_voice_state_update() -> Any
Wait for the next op 4 the bot sent and return its d.
hold_voice_state
¶
hold_voice_state() -> Event
Leave the next op 4 unanswered until the returned event is set.
move_bot
async
¶
move_bot(channel_id: int) -> None
Move the bot to channel_id as a moderator would.
The voice socket of the old channel then closes with 4014.
kick_bot
async
¶
kick_bot() -> None
Disconnect the bot from voice: a voice state without channel, then 4014.
hold_identify
¶
Leave shard's IDENTIFY unanswered until the returned event is set.
hold_hello
¶
hold_hello() -> Event
Leave the next gateway connection without HELLO until the event is set.
interaction
async
¶
Dispatch payloads.interaction(...) and return its first callback.
expect_callback
¶
Accept callbacks for interaction; the future gets the first one.
rate_limit
¶
rate_limit(method: str, route_prefix: str, *, scope: Literal['user', 'shared', 'global'] = 'user', retry_after: float = 0.1) -> None
Answer the next matching request with a 429 of scope.
fail
¶
Answer the next matching request with status and json.
This also stubs routes the fake does not model, which otherwise get a 404.
answer
¶
Answer the next matching request with a 200 that is not JSON.
cdn_file
¶
Serve data at a CDN path like attachments/1/2/a.png; return its URL.
expect_request
¶
A future for the next matching request the bot makes from now on.
Part
dataclass
¶
Recorded
dataclass
¶
Recorded(method: str, path: str, query: dict[str, str], headers: dict[str, str], json: Any, parts: list[Part])
A REST request, with path relative to api_url.
json holds the JSON body, the payload_json part of a multipart body, or
the fields of a form-urlencoded body.
Packet
dataclass
¶
An RTP packet the fake received, opened with its session's AES-GCM key.
VoiceSession
dataclass
¶
VoiceSession(identify: Json, ssrc: int, secret_key: bytes = (lambda: token_bytes(32))(), mode: str | None = None, socket: VoiceSocket | None = None, sent: list[Frame] = list[Frame](), address: Address | None = None, nonces: Iterator[int] = count())
A voice session from its Identify on.
sent keeps every sequenced frame, so a resume can replay what was missed;
address is the bot's UDP address from discovery.
secret_key
class-attribute
instance-attribute
¶
secret_key: bytes = field(default_factory=lambda: secrets.token_bytes(32))
VoiceSocket
dataclass
¶
VoiceSocket(request: Request, ws: WebSocketResponse, session: VoiceSession | None = None, close_code: int | None = None, closing: bool = False)
A voice gateway connection, with the close code the client sent if any.
run
¶
Run async def scenario(fake) against a new FakeDiscord, within 10 s.
settle
async
¶
settle(rounds: int = 5) -> None
Yield to the event loop rounds times so ready callbacks and tasks run.
running
async
¶
running(bot: Bot, fake: FakeDiscord) -> AsyncGenerator[None]
Start bot on fake, wait for READY, and close the bot on exit.
bostie.testing.payloads
¶
snowflake
¶
A unique snowflake created at at (Unix seconds, now by default).
guild
¶
A GUILD_CREATE with channels (one obfuscated), a thread, roles and members.
interaction
¶
An INTERACTION_CREATE payload of type with a fresh id and token.
command
¶
The data of a slash command or autocomplete interaction.
context_menu
¶
The data of a user (2) or message (3) command run on target_id.
option
¶
A command option value; focused marks the one being autocompleted.
resolved
¶
resolved() -> Json
Resolved users with their members, a role, a channel and an attachment.
voice_channel
¶
The guild's voice channel, as GET /channels/{id} returns it.