Skip to content

Commit d691998

Browse files
committed
tapsend: add unit test for allocation piece order
Before we refactor the ordering, we add a unit test to make sure we don't change the behavior.
1 parent 7575331 commit d691998

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

tapsend/allocation_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,3 +1230,139 @@ func TestAllocationsFromTemplate(t *testing.T) {
12301230
})
12311231
}
12321232
}
1233+
1234+
// TestSortPiecesWithProofs tests that we can sort pieces of assets with their
1235+
// proofs. The sorting is done first by asset ID, then by the amount of the
1236+
// proofs in descending order and then by script key.
1237+
func TestSortPiecesWithProofs(t *testing.T) {
1238+
key1 := asset.NewScriptKey(test.ParsePubKey(
1239+
t, "03a15fd6e1fded33270ae01183dfc8f8edd1274644b7d014ac5ab576f"+
1240+
"bf8328b05",
1241+
))
1242+
key2 := asset.NewScriptKey(test.ParsePubKey(
1243+
t, "029191ec924fb3c6bbd0d264d0b3cf97fcb2fc1eb5737184e7e17e35c"+
1244+
"6609ee853",
1245+
))
1246+
tests := []struct {
1247+
name string
1248+
input []*piece
1249+
expected []*piece
1250+
}{{
1251+
name: "sort by asset ID and proofs by amount",
1252+
input: []*piece{{
1253+
assetID: asset.ID{0x02},
1254+
proofs: []*proof.Proof{{
1255+
Asset: asset.Asset{
1256+
Amount: 50,
1257+
ScriptKey: key1,
1258+
},
1259+
}, {
1260+
Asset: asset.Asset{
1261+
Amount: 300,
1262+
ScriptKey: key2,
1263+
},
1264+
}, {
1265+
Asset: asset.Asset{
1266+
Amount: 100,
1267+
ScriptKey: key2,
1268+
},
1269+
}},
1270+
}, {
1271+
assetID: asset.ID{0x01},
1272+
proofs: []*proof.Proof{{
1273+
Asset: asset.Asset{
1274+
Amount: 200,
1275+
ScriptKey: key1,
1276+
},
1277+
}, {
1278+
Asset: asset.Asset{
1279+
Amount: 150,
1280+
ScriptKey: key2,
1281+
},
1282+
}},
1283+
}},
1284+
expected: []*piece{{
1285+
assetID: asset.ID{0x01},
1286+
proofs: []*proof.Proof{{
1287+
Asset: asset.Asset{
1288+
Amount: 200,
1289+
ScriptKey: key1,
1290+
},
1291+
}, {
1292+
Asset: asset.Asset{
1293+
Amount: 150,
1294+
ScriptKey: key2,
1295+
},
1296+
}},
1297+
}, {
1298+
assetID: asset.ID{0x02},
1299+
proofs: []*proof.Proof{{
1300+
Asset: asset.Asset{
1301+
Amount: 300,
1302+
ScriptKey: key2,
1303+
},
1304+
}, {
1305+
Asset: asset.Asset{
1306+
Amount: 100,
1307+
ScriptKey: key2,
1308+
},
1309+
}, {
1310+
Asset: asset.Asset{
1311+
Amount: 50,
1312+
ScriptKey: key1,
1313+
},
1314+
}},
1315+
}},
1316+
}, {
1317+
name: "script keys after amount",
1318+
input: []*piece{{
1319+
assetID: asset.ID{0x01},
1320+
proofs: []*proof.Proof{{
1321+
Asset: asset.Asset{
1322+
Amount: 50,
1323+
ScriptKey: key1,
1324+
},
1325+
}, {
1326+
Asset: asset.Asset{
1327+
Amount: 50,
1328+
ScriptKey: key2,
1329+
},
1330+
}, {
1331+
Asset: asset.Asset{
1332+
Amount: 50,
1333+
ScriptKey: key2,
1334+
},
1335+
}},
1336+
}},
1337+
expected: []*piece{{
1338+
assetID: asset.ID{0x01},
1339+
proofs: []*proof.Proof{{
1340+
Asset: asset.Asset{
1341+
Amount: 50,
1342+
ScriptKey: key2,
1343+
},
1344+
}, {
1345+
Asset: asset.Asset{
1346+
Amount: 50,
1347+
ScriptKey: key2,
1348+
},
1349+
}, {
1350+
Asset: asset.Asset{
1351+
Amount: 50,
1352+
ScriptKey: key1,
1353+
},
1354+
}},
1355+
}},
1356+
}, {
1357+
name: "empty input",
1358+
input: []*piece{},
1359+
expected: []*piece{},
1360+
}}
1361+
1362+
for _, tt := range tests {
1363+
t.Run(tt.name, func(t *testing.T) {
1364+
sortPiecesWithProofs(tt.input)
1365+
require.Equal(t, tt.expected, tt.input)
1366+
})
1367+
}
1368+
}

0 commit comments

Comments
 (0)