Skip to content

Commit

Permalink
feat: adapt tests, bypass query_* and calls from within the drp
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLewDev committed Jan 3, 2025
1 parent baa1572 commit c8d698e
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 102 deletions.
2 changes: 1 addition & 1 deletion examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function paint_pixel(pixel: HTMLDivElement) {
random_int(256),
];
canvasDRP.paint([x, y], painting);
const [r, g, b] = canvasDRP.getPixel(x, y).color();
const [r, g, b] = canvasDRP.query_pixel(x, y).color();
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/canvas/src/objects/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Canvas implements DRP {
this.canvas[offset[0]][offset[1]].paint(rgb);
}

getPixel(x: number, y: number): Pixel {
query_pixel(x: number, y: number): Pixel {
return this.canvas[x][y];
}

Expand Down
2 changes: 1 addition & 1 deletion examples/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const render = () => {
element_objectPeers.innerHTML = `[${objectPeers.join(", ")}]`;

if (!chatDRP) return;
const chat = chatDRP.getMessages();
const chat = chatDRP.query_messages();
const element_chat = <HTMLDivElement>document.getElementById("chat");
element_chat.innerHTML = "";

Expand Down
2 changes: 1 addition & 1 deletion examples/chat/src/objects/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Chat implements DRP {
this.messages.add(`(${timestamp}, ${message}, ${peerId})`);
}

getMessages(): Set<string> {
query_messages(): Set<string> {
return this.messages;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/grid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const render = () => {
: `Your frens in GRID: [${objectPeers.map((peer) => `<strong style="color: ${getColorForPeerId(peer)};">${formatPeerId(peer)}</strong>`).join(", ")}]`;

if (!gridDRP) return;
const users = gridDRP.getUsers();
const users = gridDRP.query_users();
const element_grid = <HTMLDivElement>document.getElementById("grid");
element_grid.innerHTML = "";

Expand Down Expand Up @@ -119,7 +119,7 @@ const render = () => {

for (const userColorString of users) {
const [id, color] = userColorString.split(":");
const position = gridDRP.getUserPosition(userColorString);
const position = gridDRP.query_userPosition(userColorString);

if (position) {
const div = document.createElement("div");
Expand Down
4 changes: 2 additions & 2 deletions examples/grid/src/objects/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export class Grid implements DRP {
}
}

getUsers(): string[] {
query_users(): string[] {
return [...this.positions.keys()];
}

getUserPosition(
query_userPosition(
userColorString: string,
): { x: number; y: number } | undefined {
const position = this.positions.get(userColorString);
Expand Down
12 changes: 6 additions & 6 deletions packages/blueprints/src/ACL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ACL implements IACL, DRP {
}

grant(senderId: string, peerId: string, publicKey: string): void {
if (!this.isAdmin(senderId)) {
if (!this.query_isAdmin(senderId)) {
throw new Error("Only admin nodes can grant permissions.");
}
this._grant(peerId, publicKey);
Expand All @@ -45,26 +45,26 @@ export class ACL implements IACL, DRP {
}

revoke(senderId: string, peerId: string): void {
if (!this.isAdmin(senderId)) {
if (!this.query_isAdmin(senderId)) {
throw new Error("Only admin nodes can revoke permissions.");
}
if (this.isAdmin(peerId)) {
if (this.query_isAdmin(peerId)) {
throw new Error(
"Cannot revoke permissions from a node with admin privileges.",
);
}
this._revoke(peerId);
}

isAdmin(peerId: string): boolean {
query_isAdmin(peerId: string): boolean {
return this._admins.has(peerId);
}

isWriter(peerId: string): boolean {
query_isWriter(peerId: string): boolean {
return this._writers.has(peerId);
}

getPeerKey(peerId: string): string | undefined {
query_getPeerKey(peerId: string): string | undefined {
return this._writers.get(peerId);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/blueprints/src/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export class AddWinsSet<T> implements DRP {
this._remove(value);
}

queryContains(value: T): boolean {
query_contains(value: T): boolean {
return this.state.get(value) === true;
}

getValues(): T[] {
query_getValues(): T[] {
return Array.from(this.state.entries())
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
Expand Down
8 changes: 4 additions & 4 deletions packages/blueprints/src/AddWinsSetWithACL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AddWinsSetWithACL<T> implements DRP {
}

add(sender: string, value: T): void {
if (this.acl && !this.acl.isWriter(sender)) {
if (this.acl && !this.acl.query_isWriter(sender)) {
throw new Error("Only writers can add values.");
}
this._add(value);
Expand All @@ -35,17 +35,17 @@ export class AddWinsSetWithACL<T> implements DRP {
}

remove(sender: string, value: T): void {
if (this.acl && !this.acl.isWriter(sender)) {
if (this.acl && !this.acl.query_isWriter(sender)) {
throw new Error("Only writers can remove values.");
}
this._remove(value);
}

queryContains(value: T): boolean {
query_contains(value: T): boolean {
return this.state.get(value) === true;
}

getValues(): T[] {
query_getValues(): T[] {
return Array.from(this.state.entries())
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
Expand Down
4 changes: 2 additions & 2 deletions packages/blueprints/src/PseudoRandomWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export class PseudoRandomWinsSet<T> implements DRP {
this._remove(value);
}

queryContains(value: T): boolean {
query_contains(value: T): boolean {
return this.state.get(value) === true;
}

getValues(): T[] {
query_getValues(): T[] {
return Array.from(this.state.entries())
.filter(([_, exists]) => exists)
.map(([value, _]) => value);
Expand Down
12 changes: 6 additions & 6 deletions packages/blueprints/tests/AddWinsSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ describe("HashGraph for AddWinSet tests", () => {

test("Test: Add", () => {
drp.add(1);
let set = drp.getValues();
let set = drp.query_getValues();
expect(set).toEqual([1]);

drp.add(2);
set = drp.getValues();
set = drp.query_getValues();
expect(set).toEqual([1, 2]);
});

test("Test: Add and Remove", () => {
drp.add(1);
let set = drp.getValues();
let set = drp.query_getValues();
expect(set).toEqual([1]);

drp.add(2);
set = drp.getValues();
set = drp.query_getValues();
expect(set).toEqual([1, 2]);

drp.remove(1);
set = drp.getValues();
expect(drp.queryContains(1)).toBe(false);
set = drp.query_getValues();
expect(drp.query_contains(1)).toBe(false);
expect(set).toEqual([2]);
});
});
10 changes: 5 additions & 5 deletions packages/blueprints/tests/AddWinsSetWithACL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ describe("AccessControl tests with RevokeWins resolution", () => {
});

test("Admin nodes should have admin privileges", () => {
expect(drp.acl.isAdmin("peer1")).toBe(true);
expect(drp.acl.query_isAdmin("peer1")).toBe(true);
});

test("Admin nodes should have write permissions", () => {
expect(drp.acl.isWriter("peer1")).toBe(true);
expect(drp.acl.query_isWriter("peer1")).toBe(true);
});

test("Grant write permissions to a new writer", () => {
drp.acl.grant("peer1", "peer3", "publicKey3");

expect(drp.acl.isWriter("peer3")).toBe(true);
expect(drp.acl.query_isWriter("peer3")).toBe(true);
});

test("Revoke write permissions from a writer", () => {
drp.acl.grant("peer1", "peer3", "publicKey3");
drp.acl.revoke("peer1", "peer3");

expect(drp.acl.isWriter("peer3")).toBe(false);
expect(drp.acl.query_isWriter("peer3")).toBe(false);
});

test("Cannot revoke admin permissions", () => {
expect(() => {
drp.acl.revoke("peer1", "peer1");
}).toThrow("Cannot revoke permissions from a node with admin privileges.");

expect(drp.acl.isWriter("peer1")).toBe(true);
expect(drp.acl.query_isWriter("peer1")).toBe(true);
});

test("Resolve conflicts with RevokeWins", () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/blueprints/tests/PseudoRandomWinsSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ describe("HashGraph for PseudoRandomWinsSet tests", () => {

test("Test: Add", () => {
drp.add(1);
let set = drp.getValues();
let set = drp.query_getValues();
expect(set).toEqual([1]);

drp.add(2);
set = drp.getValues();
set = drp.query_getValues();
expect(set).toEqual([1, 2]);
});

test("Test: Add and Remove", () => {
drp.add(1);
let set = drp.getValues();
let set = drp.query_getValues();
expect(set).toEqual([1]);

drp.add(2);
set = drp.getValues();
set = drp.query_getValues();
expect(set).toEqual([1, 2]);

drp.remove(1);
set = drp.getValues();
expect(drp.queryContains(1)).toBe(false);
set = drp.query_getValues();
expect(drp.query_contains(1)).toBe(false);
expect(set).toEqual([2]);
});
});
2 changes: 1 addition & 1 deletion packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export async function verifyIncomingVertices(

const signature = uint8ArrayFromString(vertex.signature, "base64");

const publicKey = acl.getPeerKey(vertex.peerId);
const publicKey = acl.query_getPeerKey(vertex.peerId);
if (!publicKey) {
return null;
}
Expand Down
Loading

0 comments on commit c8d698e

Please sign in to comment.