| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package status_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	"github.com/stretchr/testify/suite" | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/processing/status" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type topoSortTestSuite struct { | 
					
						
							|  |  |  | 	suite.Suite | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | func statusIDs(apiStatuses []*gtsmodel.Status) []string { | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	ids := make([]string, 0, len(apiStatuses)) | 
					
						
							|  |  |  | 	for _, apiStatus := range apiStatuses { | 
					
						
							|  |  |  | 		ids = append(ids, apiStatus.ID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ids | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestBranched() { | 
					
						
							|  |  |  | 	// https://commons.wikimedia.org/wiki/File:Sorted_binary_tree_ALL_RGB.svg | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	f := >smodel.Status{ID: "F"} | 
					
						
							|  |  |  | 	b := >smodel.Status{ID: "B", InReplyToID: f.ID} | 
					
						
							|  |  |  | 	a := >smodel.Status{ID: "A", InReplyToID: b.ID} | 
					
						
							|  |  |  | 	d := >smodel.Status{ID: "D", InReplyToID: b.ID} | 
					
						
							|  |  |  | 	c := >smodel.Status{ID: "C", InReplyToID: d.ID} | 
					
						
							|  |  |  | 	e := >smodel.Status{ID: "E", InReplyToID: d.ID} | 
					
						
							|  |  |  | 	g := >smodel.Status{ID: "G", InReplyToID: f.ID} | 
					
						
							|  |  |  | 	i := >smodel.Status{ID: "I", InReplyToID: g.ID} | 
					
						
							|  |  |  | 	h := >smodel.Status{ID: "H", InReplyToID: i.ID} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expected := statusIDs([]*gtsmodel.Status{f, b, a, d, c, e, g, i, h}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{a, b, c, d, e, f, g, h, i} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestBranchedWithSelfReplyChain() { | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	targetAccount := >smodel.Account{ID: "1"} | 
					
						
							|  |  |  | 	otherAccount := >smodel.Account{ID: "2"} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	f := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:      "F", | 
					
						
							|  |  |  | 		Account: targetAccount, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	b := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:                 "B", | 
					
						
							|  |  |  | 		Account:            targetAccount, | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 		AccountID:          targetAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        f.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: f.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	d := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:                 "D", | 
					
						
							|  |  |  | 		Account:            targetAccount, | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 		AccountID:          targetAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        b.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: b.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	e := >smodel.Status{ | 
					
						
							|  |  |  | 		ID:                 "E", | 
					
						
							|  |  |  | 		Account:            targetAccount, | 
					
						
							|  |  |  | 		AccountID:          targetAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        d.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: d.Account.ID, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	c := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:                 "C", | 
					
						
							|  |  |  | 		Account:            otherAccount, | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 		AccountID:          otherAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        d.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: d.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	a := >smodel.Status{ | 
					
						
							|  |  |  | 		ID:                 "A", | 
					
						
							|  |  |  | 		Account:            otherAccount, | 
					
						
							|  |  |  | 		AccountID:          otherAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        b.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: b.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	g := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:                 "G", | 
					
						
							|  |  |  | 		Account:            otherAccount, | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 		AccountID:          otherAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        f.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: f.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	i := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:                 "I", | 
					
						
							|  |  |  | 		Account:            targetAccount, | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 		AccountID:          targetAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        g.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: g.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	h := >smodel.Status{ | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 		ID:                 "H", | 
					
						
							|  |  |  | 		Account:            otherAccount, | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 		AccountID:          otherAccount.ID, | 
					
						
							|  |  |  | 		InReplyToID:        i.ID, | 
					
						
							|  |  |  | 		InReplyToAccountID: i.Account.ID, | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	expected := statusIDs([]*gtsmodel.Status{f, b, d, e, c, a, g, i, h}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{a, b, c, d, e, f, g, h, i} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, targetAccount.ID) | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestDisconnected() { | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	f := >smodel.Status{ID: "F"} | 
					
						
							|  |  |  | 	b := >smodel.Status{ID: "B", InReplyToID: f.ID} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	dID := "D" | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	e := >smodel.Status{ID: "E", InReplyToID: dID} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	expected := statusIDs([]*gtsmodel.Status{e, f, b}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{b, e, f} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestTrivialCycle() { | 
					
						
							|  |  |  | 	xID := "X" | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	x := >smodel.Status{ID: xID, InReplyToID: xID} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	expected := statusIDs([]*gtsmodel.Status{x}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{x} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.ElementsMatch(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestCycle() { | 
					
						
							|  |  |  | 	yID := "Y" | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	x := >smodel.Status{ID: "X", InReplyToID: yID} | 
					
						
							|  |  |  | 	y := >smodel.Status{ID: yID, InReplyToID: x.ID} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	expected := statusIDs([]*gtsmodel.Status{x, y}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{x, y} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.ElementsMatch(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestMixedCycle() { | 
					
						
							|  |  |  | 	yID := "Y" | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	x := >smodel.Status{ID: "X", InReplyToID: yID} | 
					
						
							|  |  |  | 	y := >smodel.Status{ID: yID, InReplyToID: x.ID} | 
					
						
							|  |  |  | 	z := >smodel.Status{ID: "Z"} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	expected := statusIDs([]*gtsmodel.Status{x, y, z}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{x, y, z} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.ElementsMatch(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestEmpty() { | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	expected := statusIDs([]*gtsmodel.Status{}) | 
					
						
							|  |  |  | 	list := []*gtsmodel.Status{} | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *topoSortTestSuite) TestNil() { | 
					
						
							|  |  |  | 	expected := statusIDs(nil) | 
					
						
							| 
									
										
										
										
											2024-07-12 20:36:03 +02:00
										 |  |  | 	var list []*gtsmodel.Status | 
					
						
							| 
									
										
										
										
											2024-01-31 05:40:41 -08:00
										 |  |  | 	status.TopoSort(list, "") | 
					
						
							|  |  |  | 	actual := statusIDs(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Equal(expected, actual) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestTopoSortTestSuite(t *testing.T) { | 
					
						
							|  |  |  | 	suite.Run(t, &topoSortTestSuite{}) | 
					
						
							|  |  |  | } |