Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 committed Dec 20, 2024
1 parent 2f9a6c0 commit 0c1d573
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class HashGraph {
throw new Error("Invalid dependency detected.");
}
if (vertex.timestamp > timestamp) {
// Vertex's timestamp must not be less than its dependencies
// Vertex's timestamp must not be less than any of its dependencies' timestamps
throw new Error("Invalid timestamp detected.");
}
}
Expand Down
63 changes: 63 additions & 0 deletions packages/object/tests/hashgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,66 @@ describe("Vertex state tests", () => {
expect(drpStateV8?.state.get("state").get(3)).toBe(undefined);
});
});

describe("Vertex timestamp tests", () => {
let obj1: DRPObject;
let obj2: DRPObject;
let obj3: DRPObject;

beforeEach(async () => {
obj1 = new DRPObject("peer1", new AddWinsSet<number>());
obj2 = new DRPObject("peer1", new AddWinsSet<number>());
obj3 = new DRPObject("peer1", new AddWinsSet<number>());
});

test("Test: Vertex created in the future is invalid", () => {
const drp1 = obj1.drp as AddWinsSet<number>;

drp1.add(1);

expect(() =>
obj1.hashGraph.addVertex(
{
type: "add",
value: 1,
},
obj1.hashGraph.getFrontier(),
"",
Number.POSITIVE_INFINITY,
),
).toThrowError("Invalid timestamp detected.");
});

test("Test: Vertex's timestamp must not be less than any of its dependencies' timestamps", () => {
/*
__ V1:ADD(1) __
/ \
ROOT -- V2:ADD(2) ---- V1:ADD(4) (invalid)
\ /
-- V3:ADD(3) --
*/

const drp1 = obj1.drp as AddWinsSet<number>;
const drp2 = obj2.drp as AddWinsSet<number>;
const drp3 = obj2.drp as AddWinsSet<number>;

drp1.add(1);
drp2.add(2);
drp3.add(3);

obj1.merge(obj2.hashGraph.getAllVertices());
obj1.merge(obj3.hashGraph.getAllVertices());

expect(() =>
obj1.hashGraph.addVertex(
{
type: "add",
value: 1,
},
obj1.hashGraph.getFrontier(),
"",
1,
),
).toThrowError("Invalid timestamp detected.");
});
});

0 comments on commit 0c1d573

Please sign in to comment.