test: tighten discord gateway opcode assertions

This commit is contained in:
Shakker
2026-05-10 21:39:31 +01:00
parent f2dea8ac5a
commit 8eb14802fd

View File

@@ -21,6 +21,14 @@ function attachOpenSocket(gateway: GatewayPlugin) {
return send;
}
function sentGatewayOpcodes(send: ReturnType<typeof attachOpenSocket>) {
return send.mock.calls.map((call) => {
const [rawPayload] = call;
const payload = JSON.parse(String(rawPayload)) as { op?: unknown };
return payload.op;
});
}
function presenceUpdate(
status: PresenceUpdateStatus.Online | PresenceUpdateStatus.Idle = PresenceUpdateStatus.Online,
): GatewaySendPayload {
@@ -553,17 +561,11 @@ describe("GatewayPlugin", () => {
}
await vi.advanceTimersByTimeAsync(0);
expect(firstSend).toHaveBeenCalledWith(
expect.stringContaining(`"op":${GatewayOpcodes.Identify}`),
);
expect(secondSend).not.toHaveBeenCalledWith(
expect.stringContaining(`"op":${GatewayOpcodes.Identify}`),
);
expect(sentGatewayOpcodes(firstSend)).toContain(GatewayOpcodes.Identify);
expect(sentGatewayOpcodes(secondSend)).not.toContain(GatewayOpcodes.Identify);
await vi.advanceTimersByTimeAsync(5_000);
expect(secondSend).toHaveBeenCalledWith(
expect.stringContaining(`"op":${GatewayOpcodes.Identify}`),
);
expect(sentGatewayOpcodes(secondSend)).toContain(GatewayOpcodes.Identify);
});
it("validates requestGuildMembers before sending", () => {