Jalex23 said:
Every time the officer feels that it is needed for proper assessment.
although i have no personal experience of this scenario i should imagine getting to the point of email or social media passwords anything that has gone before in the inspection has not satisfied CBSA.
So progressively they may start with additional interogation, luggage search, phone messages and if still not satisfied passwords but would guess the latter would be a last resort if the passengers intentions still under suspicion.
Only the OP has any idea why they reached that level of inspection which i doubt based on the volume of inspections happens that often but only CBSA will have that data thus keeping people on their guard.
Standard advice with CBSA answer questions honestly and clearly but do not volunteer additional information that has not been asked for as that can just generate unecessary questions.
Unfortunately these days some people carry their lives around on their smart phones and forget what they have on them so if CBSA so wish thats the first place they may go.