@@ -267,39 +267,52 @@ func TestStreamNewReplyHandler(t *testing.T) {
267267}
268268
269269func TestStreamGetRepliesHandler (t * testing.T ) {
270- owner := "owner-1"
271270 rootId := "root-1"
272271 parentId := "parent-1"
273272
274273 t .Run ("invalid payload" , func (t * testing.T ) {
275- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {}, stubStreamer {} )
274+ h := StreamGetRepliesHandler (stubReplyRepo {})
276275 _ , err := h ([]byte ("{" ), nil )
277276 if err == nil {
278277 t .Fatal ("expected error" )
279278 }
280279 })
281280
282- t .Run ("empty parent id" , func (t * testing.T ) {
283- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {}, stubStreamer {})
281+ t .Run ("empty parent id defaults to root" , func (t * testing.T ) {
282+ // Top-level replies on a thread carry no parent_id from the
283+ // client — the handler must fall back to root_id so the repo
284+ // lookup runs against the first tier of replies hanging off
285+ // the root tweet.
286+ var seenRoot , seenParent string
287+ h := StreamGetRepliesHandler (
288+ stubReplyRepo {getRepliesTreeFn : func (rootID , parentIdArg string , _ * uint64 , _ * string ) ([]domain.ReplyNode , string , error ) {
289+ seenRoot = rootID
290+ seenParent = parentIdArg
291+ return nil , "" , nil
292+ }},
293+ )
284294 _ , err := h (marshal (t , event.GetAllRepliesEvent {RootId : rootId }), nil )
285- if err == nil || err . Error () != "empty parent id" {
295+ if err != nil {
286296 t .Fatalf ("unexpected err: %v" , err )
287297 }
298+ if seenRoot != rootId || seenParent != rootId {
299+ t .Fatalf ("expected rootId %q and parentId %q from root fallback, got root=%q parent=%q" , rootId , rootId , seenRoot , seenParent )
300+ }
288301 })
289302
290303 t .Run ("empty root id" , func (t * testing.T ) {
291- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {}, stubStreamer {} )
304+ h := StreamGetRepliesHandler (stubReplyRepo {})
292305 _ , err := h (marshal (t , event.GetAllRepliesEvent {ParentId : parentId }), nil )
293306 if err == nil || err .Error () != "empty root id" {
294307 t .Fatalf ("unexpected err: %v" , err )
295308 }
296309 })
297310
298- t .Run ("own tweet replies" , func (t * testing.T ) {
311+ t .Run ("serves replies from local repo " , func (t * testing.T ) {
299312 replies := []domain.ReplyNode {{Reply : domain.Tweet {Id : "r1" , Text : "reply" }}}
300313 h := StreamGetRepliesHandler (stubReplyRepo {getRepliesTreeFn : func (rootID , parentIdArg string , limit * uint64 , cursor * string ) ([]domain.ReplyNode , string , error ) {
301314 return replies , "end" , nil
302- }}, stubReplyUserRepo {}, stubStreamer { nodeInfo : warpnet. NodeInfo { OwnerId : parentId }} )
315+ }})
303316 resp , err := h (marshal (t , event.GetAllRepliesEvent {RootId : rootId , ParentId : parentId }), nil )
304317 if err != nil {
305318 t .Fatalf ("unexpected err: %v" , err )
@@ -308,65 +321,19 @@ func TestStreamGetRepliesHandler(t *testing.T) {
308321 if len (r .Replies ) != 1 {
309322 t .Fatalf ("expected 1 reply, got %d" , len (r .Replies ))
310323 }
311- })
312-
313- t .Run ("parent user not found fallback" , func (t * testing.T ) {
314- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {getFn : func (userId string ) (domain.User , error ) {
315- return domain.User {}, database .ErrUserNotFound
316- }}, stubStreamer {nodeInfo : warpnet.NodeInfo {OwnerId : owner }})
317- resp , err := h (marshal (t , event.GetAllRepliesEvent {RootId : rootId , ParentId : parentId }), nil )
318- if err != nil {
319- t .Fatalf ("unexpected err: %v" , err )
320- }
321- _ = resp .(event.RepliesResponse )
322- })
323-
324- t .Run ("stream node offline fallback" , func (t * testing.T ) {
325- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {}, stubStreamer {
326- nodeInfo : warpnet.NodeInfo {OwnerId : owner },
327- genericStreamFn : func (nodeId string , path stream.WarpRoute , data any ) ([]byte , error ) {
328- return nil , warpnet .ErrNodeIsOffline
329- },
330- })
331- resp , err := h (marshal (t , event.GetAllRepliesEvent {RootId : rootId , ParentId : parentId }), nil )
332- if err != nil {
333- t .Fatalf ("unexpected err: %v" , err )
324+ if r .Cursor != "end" {
325+ t .Fatalf ("expected cursor 'end', got %q" , r .Cursor )
334326 }
335- _ = resp .(event.RepliesResponse )
336327 })
337328
338- t .Run ("stream error" , func (t * testing.T ) {
339- streamErr := errors .New ("broken" )
340- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {}, stubStreamer {
341- nodeInfo : warpnet.NodeInfo {OwnerId : owner },
342- genericStreamFn : func (nodeId string , path stream.WarpRoute , data any ) ([]byte , error ) {
343- return nil , streamErr
344- },
345- })
329+ t .Run ("propagates repo error" , func (t * testing.T ) {
330+ boom := errors .New ("db down" )
331+ h := StreamGetRepliesHandler (stubReplyRepo {getRepliesTreeFn : func (string , string , * uint64 , * string ) ([]domain.ReplyNode , string , error ) {
332+ return nil , "" , boom
333+ }})
346334 _ , err := h (marshal (t , event.GetAllRepliesEvent {RootId : rootId , ParentId : parentId }), nil )
347- if ! errors .Is (err , streamErr ) {
348- t .Fatalf ("expected stream error: %v" , err )
349- }
350- })
351-
352- t .Run ("remote successful response" , func (t * testing.T ) {
353- remoteResp , _ := json .Marshal (event.RepliesResponse {
354- Cursor : "end" ,
355- Replies : []domain.ReplyNode {{Reply : domain.Tweet {Id : "r1" , Text : "remote reply" , RootId : rootId , ParentId : & parentId }}},
356- })
357- h := StreamGetRepliesHandler (stubReplyRepo {}, stubReplyUserRepo {}, stubStreamer {
358- nodeInfo : warpnet.NodeInfo {OwnerId : owner },
359- genericStreamFn : func (nodeId string , path stream.WarpRoute , data any ) ([]byte , error ) {
360- return remoteResp , nil
361- },
362- })
363- resp , err := h (marshal (t , event.GetAllRepliesEvent {RootId : rootId , ParentId : parentId }), nil )
364- if err != nil {
365- t .Fatalf ("unexpected err: %v" , err )
366- }
367- r := resp .(event.RepliesResponse )
368- if len (r .Replies ) != 1 {
369- t .Fatalf ("expected 1 reply: %v" , r )
335+ if ! errors .Is (err , boom ) {
336+ t .Fatalf ("expected db error, got %v" , err )
370337 }
371338 })
372339}
0 commit comments