Skip to content

Commit 31d50b0

Browse files
committed
test(sdk-core): add tests for collection item weights
These were missed from the previous item weights commit.
1 parent e1ced1e commit 31d50b0

2 files changed

Lines changed: 184 additions & 1 deletion

File tree

packages/sdk-core/tests/repository/HypercertOperationsImpl.test.ts

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,90 @@ describe("HypercertOperationsImpl", () => {
986986
}),
987987
);
988988
});
989+
990+
it("should create a collection with weighted items", async () => {
991+
mockAgent.com.atproto.repo.createRecord.mockResolvedValue({
992+
success: true,
993+
data: { uri: "at://did:plc:test/org.hypercerts.collection/weighted", cid: "weighted-cid" },
994+
});
995+
996+
const result = await hypercertOps.createCollection({
997+
title: "Weighted Collection",
998+
items: [
999+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "30" },
1000+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "70" },
1001+
],
1002+
});
1003+
1004+
expect(result.uri).toContain("collection");
1005+
expect(mockAgent.com.atproto.repo.createRecord).toHaveBeenCalledWith(
1006+
expect.objectContaining({
1007+
record: expect.objectContaining({
1008+
title: "Weighted Collection",
1009+
items: [
1010+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "30" },
1011+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "70" },
1012+
],
1013+
}),
1014+
}),
1015+
);
1016+
});
1017+
1018+
it("should create a collection with mixed weighted and unweighted items", async () => {
1019+
mockAgent.com.atproto.repo.createRecord.mockResolvedValue({
1020+
success: true,
1021+
data: { uri: "at://did:plc:test/org.hypercerts.collection/mixed", cid: "mixed-cid" },
1022+
});
1023+
1024+
const result = await hypercertOps.createCollection({
1025+
title: "Mixed Collection",
1026+
items: [
1027+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "50" },
1028+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } }, // No weight
1029+
{ itemIdentifier: { uri: "at://claim3", cid: "cid3" }, itemWeight: "25" },
1030+
],
1031+
});
1032+
1033+
expect(result.uri).toContain("collection");
1034+
expect(mockAgent.com.atproto.repo.createRecord).toHaveBeenCalledWith(
1035+
expect.objectContaining({
1036+
record: expect.objectContaining({
1037+
items: [
1038+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "50" },
1039+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } },
1040+
{ itemIdentifier: { uri: "at://claim3", cid: "cid3" }, itemWeight: "25" },
1041+
],
1042+
}),
1043+
}),
1044+
);
1045+
});
1046+
1047+
it("should create a collection with items without weights", async () => {
1048+
mockAgent.com.atproto.repo.createRecord.mockResolvedValue({
1049+
success: true,
1050+
data: { uri: "at://did:plc:test/org.hypercerts.collection/unweighted", cid: "unweighted-cid" },
1051+
});
1052+
1053+
const result = await hypercertOps.createCollection({
1054+
title: "Unweighted Collection",
1055+
items: [
1056+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" } },
1057+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } },
1058+
],
1059+
});
1060+
1061+
expect(result.uri).toContain("collection");
1062+
expect(mockAgent.com.atproto.repo.createRecord).toHaveBeenCalledWith(
1063+
expect.objectContaining({
1064+
record: expect.objectContaining({
1065+
items: [
1066+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" } },
1067+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } },
1068+
],
1069+
}),
1070+
}),
1071+
);
1072+
});
9891073
});
9901074

9911075
describe("getCollection", () => {
@@ -1093,6 +1177,105 @@ describe("HypercertOperationsImpl", () => {
10931177
}),
10941178
).rejects.toThrow(ValidationError);
10951179
});
1180+
1181+
it("should update collection items with weights", async () => {
1182+
const result = await hypercertOps.updateCollection("at://did:plc:test/org.hypercerts.collection/abc123", {
1183+
items: [
1184+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "40" },
1185+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "60" },
1186+
],
1187+
});
1188+
1189+
expect(result.uri).toBe("at://did:plc:test/org.hypercerts.collection/abc123");
1190+
expect(mockAgent.com.atproto.repo.putRecord).toHaveBeenCalledWith(
1191+
expect.objectContaining({
1192+
record: expect.objectContaining({
1193+
items: [
1194+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "40" },
1195+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "60" },
1196+
],
1197+
}),
1198+
}),
1199+
);
1200+
});
1201+
1202+
it("should update collection adding weights to existing items", async () => {
1203+
// Setup: collection with unweighted items
1204+
mockAgent.com.atproto.repo.getRecord.mockResolvedValue({
1205+
success: true,
1206+
data: {
1207+
uri: "at://did:plc:test/org.hypercerts.collection/abc123",
1208+
cid: "old-cid",
1209+
value: {
1210+
$type: "org.hypercerts.collection",
1211+
title: "Collection",
1212+
items: [
1213+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" } },
1214+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } },
1215+
],
1216+
createdAt: "2024-01-01T00:00:00Z",
1217+
},
1218+
},
1219+
});
1220+
1221+
const result = await hypercertOps.updateCollection("at://did:plc:test/org.hypercerts.collection/abc123", {
1222+
items: [
1223+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "30" },
1224+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "70" },
1225+
],
1226+
});
1227+
1228+
expect(result.uri).toBe("at://did:plc:test/org.hypercerts.collection/abc123");
1229+
expect(mockAgent.com.atproto.repo.putRecord).toHaveBeenCalledWith(
1230+
expect.objectContaining({
1231+
record: expect.objectContaining({
1232+
items: [
1233+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "30" },
1234+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "70" },
1235+
],
1236+
}),
1237+
}),
1238+
);
1239+
});
1240+
1241+
it("should update collection removing weights from items", async () => {
1242+
// Setup: collection with weighted items
1243+
mockAgent.com.atproto.repo.getRecord.mockResolvedValue({
1244+
success: true,
1245+
data: {
1246+
uri: "at://did:plc:test/org.hypercerts.collection/abc123",
1247+
cid: "old-cid",
1248+
value: {
1249+
$type: "org.hypercerts.collection",
1250+
title: "Collection",
1251+
items: [
1252+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" }, itemWeight: "50" },
1253+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" }, itemWeight: "50" },
1254+
],
1255+
createdAt: "2024-01-01T00:00:00Z",
1256+
},
1257+
},
1258+
});
1259+
1260+
const result = await hypercertOps.updateCollection("at://did:plc:test/org.hypercerts.collection/abc123", {
1261+
items: [
1262+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" } },
1263+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } },
1264+
],
1265+
});
1266+
1267+
expect(result.uri).toBe("at://did:plc:test/org.hypercerts.collection/abc123");
1268+
expect(mockAgent.com.atproto.repo.putRecord).toHaveBeenCalledWith(
1269+
expect.objectContaining({
1270+
record: expect.objectContaining({
1271+
items: [
1272+
{ itemIdentifier: { uri: "at://claim1", cid: "cid1" } },
1273+
{ itemIdentifier: { uri: "at://claim2", cid: "cid2" } },
1274+
],
1275+
}),
1276+
}),
1277+
);
1278+
});
10961279
});
10971280

10981281
describe("deleteCollection", () => {

specs/lexicon-sync/v0.10.0-beta.4-v0.10.0-beta.11.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ implemented, validated, and reviewed before proceeding to the next.
2929
- [x] Add usage examples in type documentation
3030
- [x] Verify `CollectionItemInput` helper type is correct (already supports itemWeight)
3131
- [x] Verify CRUD operations support itemWeight (createCollection/updateCollection already pass items through)
32-
- [ ] Add/update tests for collections with weighted items
32+
- [x] Add/update tests for collections with weighted items
3333
- [x] Build and test
3434
- [x] Create changeset (minor - new feature available)
3535

0 commit comments

Comments
 (0)