The other answer is wrong; we can in fact program the chess engine to favour traps! As you already noted, when the engine thinks it is winning it should simply choose the best move. So the question is how to aim for the sharpest lines for the opponent when it is losing. This is of course subjective, and depends on the kind of opponent we are pitting it against, but there are a few obvious ways to achieve this.
Firstly, we could compute the best play assuming the opponent makes exactly one mistake (as seen from our game tree). Best play assumes that both players make zero mistakes, but we are aiming to trick the opponent to make a mistake, hence we need to know what are the best lines if the opponent does make a mistake. We now have two evaluation scores for each move X, one for the best play and one for the best play with one opponent mistake. Denote these by E0(X) and E1(X) respectively. We can then combine these two scores by some heuristic to obtain an overall score. The safest heuristic is to choose the move X with E1(X)≥0 such that E0(X) is maximum. This heuristic in fact agrees with choosing the best move X if E0(X)>0, since E1(X)≥E0(X). A slightly less safe heuristic is to choose the move with E1(X)>0 such that E0(X) is maximum, which might occasionally choose an inferior move if the best move X has E0(X)=0, in order to try for a win. You can of course design your own heuristic if you want your engine to play more dangerously.
Another way is to incorporate some probabilistic evaluation, by treating opponent play as a random process that occasionally makes mistakes with the distribution of the chosen move depending on the scores of the available moves. I do not think that this is a good general approach, because although it may do well against humans (humans being careless creatures), it is not going to do well against other computer players, as compared to the other approach I gave above.
I also want to emphasize that for this question ("when the game outcome is within their horizon"), any 'hack' such as 'contempt factor' is simply not applicable, because all the terminal nodes in the game tree have known outcomes. Also, many chess engines use a 'contempt factor' instead of the kind of technique I described because it is computationally cheap ('contempt' can be implemented simply as a slightly negative score for a probable forced draw). But if you can already see all the outcomes from a position onwards, it is trivial and costs almost nothing to implement the technique I gave above. The reason it is expensive if the outcome is beyond the search horizon is that, since alpha-beta pruning may prune branches with mistakes, we would frequently need to search substantially more branches to compute E1 correctly.