Skip to content

Commit

Permalink
added randomness and started discover
Browse files Browse the repository at this point in the history
  • Loading branch information
sportdeath committed Jan 9, 2025
1 parent ef97e6a commit 1d2756e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 54 deletions.
47 changes: 22 additions & 25 deletions tests/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GraffitiErrorPatchTestFailed,
GraffitiErrorPatchError,
} from "../src/index";
import { randomPutObject, randomString } from "./utils";

export const graffitiCRUDTests = (
useGraffiti: GraffitiFactory,
Expand All @@ -23,7 +24,7 @@ export const graffitiCRUDTests = (
const value = {
something: "hello, world~ c:",
};
const channels = ["world"];
const channels = [randomString(), randomString()];

// Put the object
const previous = await graffiti.put({ value, channels }, session);
Expand Down Expand Up @@ -210,8 +211,8 @@ export const graffitiCRUDTests = (
const value = {
um: "hi",
};
const allowed = ["asdf"];
const channels = ["helloooo"];
const allowed = [randomString()];
const channels = [randomString()];
const putted = await graffiti.put({ value, allowed, channels }, session1);

// Get it with authenticated session
Expand All @@ -235,8 +236,8 @@ export const graffitiCRUDTests = (
const value = {
um: "hi",
};
const allowed = ["asdf", session2.actor, "1234"];
const channels = ["helloooo"];
const allowed = [randomString(), session2.actor, randomString()];
const channels = [randomString()];
const putted = await graffiti.put(
{
value,
Expand Down Expand Up @@ -337,17 +338,21 @@ export const graffitiCRUDTests = (
const graffiti = useGraffiti();
const session = useSession1();

const channelsBefore = [randomString()];
const channelsAfter = [randomString()];

const putted = await graffiti.put(
{ value: {}, channels: ["helloooo"] },
{ value: {}, channels: channelsBefore },
session,
);

const patch: GraffitiPatch = {
channels: [{ op: "replace", path: "/0", value: "goodbye" }],
channels: [{ op: "replace", path: "/0", value: channelsAfter[0] }],
};
await graffiti.patch(patch, putted, session);
const patched = await graffiti.patch(patch, putted, session);
expect(patched.channels).toEqual(channelsBefore);
const gotten = await graffiti.get(putted, {}, session);
expect(gotten.channels).toEqual(["goodbye"]);
expect(gotten.channels).toEqual(channelsAfter);
await graffiti.delete(putted, session);
});

Expand Down Expand Up @@ -406,13 +411,8 @@ export const graffitiCRUDTests = (
it("invalid patch", async () => {
const graffiti = useGraffiti();
const session = useSession1();
const putted = await graffiti.put(
{
value: {},
channels: [],
},
session,
);
const object = randomPutObject();
const putted = await graffiti.put(object, session);

await expect(
graffiti.patch(
Expand All @@ -431,12 +431,9 @@ export const graffitiCRUDTests = (
it("patch channels to be wrong", async () => {
const graffiti = useGraffiti();
const session = useSession1();
const value = {
original: "value",
};
const channels = ["original-channel"];
const allowed = ["original-allowed"];
const putted = await graffiti.put({ value, channels, allowed }, session);
const object = randomPutObject();
object.allowed = [randomString()];
const putted = await graffiti.put(object, session);

const patches: GraffitiPatch[] = [
{
Expand Down Expand Up @@ -475,9 +472,9 @@ export const graffitiCRUDTests = (
}

const gotten = await graffiti.get(putted, {}, session);
expect(gotten.value).toEqual(value);
expect(gotten.channels).toEqual(channels);
expect(gotten.allowed).toEqual(allowed);
expect(gotten.value).toEqual(object.value);
expect(gotten.channels).toEqual(object.channels);
expect(gotten.allowed).toEqual(object.allowed);
expect(gotten.lastModified.getTime()).toEqual(
putted.lastModified.getTime(),
);
Expand Down
32 changes: 32 additions & 0 deletions tests/discover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { it, expect, describe } from "vitest";
import { type GraffitiFactory, type GraffitiSessionBase } from "../src/index";
import { randomString, randomValue, randomPutObject } from "./utils";

export const graffitiDiscoverTests = (
useGraffiti: GraffitiFactory,
useSession1: () => GraffitiSessionBase,
useSession2: () => GraffitiSessionBase,
) => {
describe("discover", () => {
it("discover single", async () => {
const graffiti = useGraffiti();
const session = useSession1();
const object = randomPutObject();

const putted = await graffiti.put(object, session);

const queryChannels = [randomString(), object.channels[0]];
const iterator = graffiti.discover(queryChannels, {});
const result = (await iterator.next()).value;
if (!result || result.error) throw new Error();
expect(result.value.value).toEqual(object.value);
expect(result.value.channels).toEqual([object.channels[0]]);
expect(result.value.allowed).toBeUndefined();
expect(result.value.actor).toEqual(session.actor);
expect(result.value.tombstone).toBe(false);
expect(result.value.lastModified).toEqual(putted.lastModified);
const result2 = await iterator.next();
expect(result2.done).toBe(true);
});
});
};
1 change: 1 addition & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./location";
export * from "./crud";
export * from "./synchronize";
export * from "./discover";
15 changes: 8 additions & 7 deletions tests/location.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { it, expect, describe } from "vitest";
import { GraffitiErrorInvalidUri, type GraffitiFactory } from "../src/index";
import { randomString } from "./utils";

export const graffitiLocationTests = (useGraffiti: GraffitiFactory) => {
describe("URI and location conversion", () => {
it("location to uri and back", async () => {
const graffiti = useGraffiti();
const location = {
name: "12345",
actor: "https://example.com/actor",
source: "https://example.com/source",
name: randomString(),
actor: randomString(),
source: randomString(),
};
const uri = graffiti.locationToUri(location);
const location2 = graffiti.uriToLocation(uri);
Expand All @@ -18,12 +19,12 @@ export const graffitiLocationTests = (useGraffiti: GraffitiFactory) => {
it("collision resistance", async () => {
const graffiti = useGraffiti();
const location1 = {
name: "12345",
actor: "https://example.com/actor",
source: "https://example.com/source",
name: randomString(),
actor: randomString(),
source: randomString(),
};
for (const prop of ["name", "actor", "source"] as const) {
const location2 = { ...location1, [prop]: "something else" };
const location2 = { ...location1, [prop]: randomString() };
const uri1 = graffiti.locationToUri(location1);
const uri2 = graffiti.locationToUri(location2);
expect(uri1).not.toEqual(uri2);
Expand Down
39 changes: 17 additions & 22 deletions tests/synchronize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { it, expect, describe } from "vitest";
import { type GraffitiFactory, type GraffitiSessionBase } from "../src/index";
import { randomPutObject, randomString } from "./utils";
import { randomInt } from "crypto";

export const graffitiSynchronizeTests = (
useGraffiti: GraffitiFactory,
Expand All @@ -11,16 +13,9 @@ export const graffitiSynchronizeTests = (
const graffiti1 = useGraffiti();
const session = useSession1();

const allChannels = ["channel", "other"];
const channels = [allChannels[0]];
const value = { hello: "world" };
const putted = await graffiti1.put(
{
value,
channels: allChannels,
},
session,
);
const object = randomPutObject();
const channels = object.channels.slice(1);
const putted = await graffiti1.put(object, session);

const graffiti2 = useGraffiti();
const next = graffiti2.synchronize(channels, {}).next();
Expand All @@ -30,7 +25,7 @@ export const graffitiSynchronizeTests = (
if (!result || result.error) {
throw new Error("Error in synchronize");
}
expect(result.value.value).toEqual(value);
expect(result.value.value).toEqual(object.value);
expect(result.value.channels).toEqual(channels);
expect(result.value.tombstone).toBe(false);
expect(result.value.lastModified.getTime()).toEqual(
Expand All @@ -42,9 +37,9 @@ export const graffitiSynchronizeTests = (
const graffiti = useGraffiti();
const session = useSession1();

const beforeChannel = "before";
const afterChannel = "after";
const sharedChannel = "shared";
const beforeChannel = randomString();
const afterChannel = randomString();
const sharedChannel = randomString();

const oldValue = { hello: "world" };
const oldChannels = [beforeChannel, sharedChannel];
Expand Down Expand Up @@ -108,9 +103,9 @@ export const graffitiSynchronizeTests = (
const graffiti = useGraffiti();
const session = useSession1();

const beforeChannel = "before";
const afterChannel = "after";
const sharedChannel = "shared";
const beforeChannel = randomString();
const afterChannel = randomString();
const sharedChannel = randomString();

const oldValue = { hello: "world" };
const oldChannels = [beforeChannel, sharedChannel];
Expand Down Expand Up @@ -189,10 +184,10 @@ export const graffitiSynchronizeTests = (
const graffiti = useGraffiti();
const session = useSession1();

const channels = ["a", "b", "c"];
const channels = [randomString(), randomString(), randomString()];

const oldValue = { hello: "world" };
const oldChannels = ["d", ...channels.slice(1)];
const oldChannels = [randomString(), ...channels.slice(1)];
const putted = await graffiti.put(
{
value: oldValue,
Expand Down Expand Up @@ -221,8 +216,8 @@ export const graffitiSynchronizeTests = (
const session1 = useSession1();
const session2 = useSession2();

const allChannels = ["channel", "other"];
const channels = [allChannels[0]];
const allChannels = [randomString(), randomString(), randomString()];
const channels = allChannels.slice(1);

const creatorNext = graffiti.synchronize(channels, {}, session1).next();
const allowedNext = graffiti.synchronize(channels, {}, session2).next();
Expand All @@ -231,7 +226,7 @@ export const graffitiSynchronizeTests = (
const value = {
hello: "world",
};
const allowed = ["asdf", session2.actor];
const allowed = [randomString(), session2.actor];
await graffiti.put({ value, channels: allChannels, allowed }, session1);

// Expect no session to time out!
Expand Down
22 changes: 22 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { GraffitiPutObject } from "../src";

export function randomString(): string {
const array = new Uint8Array(16);
crypto.getRandomValues(array);
return Array.from(array)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}

export function randomValue() {
return {
[randomString()]: randomString(),
};
}

export function randomPutObject(): GraffitiPutObject<{}> {
return {
value: randomValue(),
channels: [randomString(), randomString()],
};
}

0 comments on commit 1d2756e

Please sign in to comment.