Sleeping in an area surrounded by enemies, even in the relative safety of a locked room or a tiny hut spell, does not help characters recover hit dice. The stress of the situation makes it too difficult to get any rest.
Once every 24-hour period, a character may take a long rest. During this 8-hour downtime, a character sleeps or performs light activity: reading, talking, eating, or standing watch for no more than 2 hours. If the rest is interrupted by a period of strenuous activity the characters must begin the rest again to gain any benefit from it.
While long rests do not automatically heal your character, they can spend Hit Dice to heal at the end of a long rest, just as with a short rest.
At the end of a long rest a character regains spent abilities and spell slots. They also recover hit dice depending on the conditions they are resting in: