From 1d2756ecd8f17ef43e2e55bcde912e5ed0018c6e Mon Sep 17 00:00:00 2001 From: theia Date: Thu, 9 Jan 2025 16:53:52 -0500 Subject: [PATCH] added randomness and started discover --- tests/crud.ts | 47 +++++++++++++++++++++----------------------- tests/discover.ts | 32 ++++++++++++++++++++++++++++++ tests/index.ts | 1 + tests/location.ts | 15 +++++++------- tests/synchronize.ts | 39 ++++++++++++++++-------------------- tests/utils.ts | 22 +++++++++++++++++++++ 6 files changed, 102 insertions(+), 54 deletions(-) create mode 100644 tests/discover.ts create mode 100644 tests/utils.ts diff --git a/tests/crud.ts b/tests/crud.ts index 1e7d909..46529f2 100644 --- a/tests/crud.ts +++ b/tests/crud.ts @@ -10,6 +10,7 @@ import { GraffitiErrorPatchTestFailed, GraffitiErrorPatchError, } from "../src/index"; +import { randomPutObject, randomString } from "./utils"; export const graffitiCRUDTests = ( useGraffiti: GraffitiFactory, @@ -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); @@ -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 @@ -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, @@ -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); }); @@ -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( @@ -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[] = [ { @@ -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(), ); diff --git a/tests/discover.ts b/tests/discover.ts new file mode 100644 index 0000000..8c2fcc1 --- /dev/null +++ b/tests/discover.ts @@ -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); + }); + }); +}; diff --git a/tests/index.ts b/tests/index.ts index eeb5b5d..8be5ab9 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -1,3 +1,4 @@ export * from "./location"; export * from "./crud"; export * from "./synchronize"; +export * from "./discover"; diff --git a/tests/location.ts b/tests/location.ts index 8c3684b..dcf61ae 100644 --- a/tests/location.ts +++ b/tests/location.ts @@ -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); @@ -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); diff --git a/tests/synchronize.ts b/tests/synchronize.ts index 874f350..7fea4fe 100644 --- a/tests/synchronize.ts +++ b/tests/synchronize.ts @@ -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, @@ -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(); @@ -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( @@ -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]; @@ -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]; @@ -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, @@ -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(); @@ -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! diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 0000000..667180b --- /dev/null +++ b/tests/utils.ts @@ -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()], + }; +}