import { it, expect, describe, assert, beforeAll } from "vitest"; import type { Graffiti, GraffitiSession, JSONSchema, } from "@graffiti-garden/api"; import { GraffitiErrorCursorExpired, GraffitiErrorForbidden, GraffitiErrorInvalidSchema, GraffitiErrorNotFound, } from "@graffiti-garden/api"; import { randomString, nextStreamValue, randomPostObject, continueStream, randomUrl, } from "./utils.js"; export const graffitiDiscoverTests = ( useGraffiti: () => Pick< Graffiti, "discover" | "post" | "get" | "delete" | "continueDiscover" >, useSession1: () => GraffitiSession | Promise, useSession2: () => GraffitiSession | Promise, ) => { describe.concurrent("discover", { timeout: 20000 }, () => { let graffiti: ReturnType; let session: GraffitiSession; let session1: GraffitiSession; let session2: GraffitiSession; beforeAll(async () => { graffiti = useGraffiti(); session1 = await useSession1(); session = session1; session2 = await useSession2(); }); it("discover nothing", async () => { const iterator = graffiti.discover([], {}); expect(await iterator.next()).toHaveProperty("done", true); }); it("discover single", async () => { const object = randomPostObject(); const posted = await graffiti.post<{}>(object, session); const queryChannels = [randomString(), object.channels[0]]; const iterator = graffiti.discover<{}>(queryChannels, {}); const value = await nextStreamValue<{}>(iterator); expect(value.url).toEqual(posted.url); expect(value.value).toEqual(object.value); expect(value.channels).toEqual([object.channels[0]]); expect(value.allowed).toBeUndefined(); expect(value.actor).toEqual(session.actor); const result2 = await iterator.next(); expect(result2.done).toBe(true); }); it("discover wrong channel", async () => { const object = randomPostObject(); await graffiti.post<{}>(object, session); const iterator = graffiti.discover([randomString()], {}); await expect(iterator.next()).resolves.toHaveProperty("done", true); }); it("discover with boolean schemas", async () => { const object = randomPostObject(); const posted = await graffiti.post<{}>(object, session); const matchingIterator = graffiti.discover<{}>(object.channels, true); const value = await nextStreamValue<{}>(matchingIterator); expect(value.url).toEqual(posted.url); const nonmatchingIterator = graffiti.discover(object.channels, false); await expect(nonmatchingIterator.next()).resolves.toHaveProperty( "done", true, ); }); it("discover not allowed", async () => { const object = randomPostObject(); object.allowed = [randomUrl(), randomUrl()]; const posted = await graffiti.post<{}>(object, session1); const iteratorSession1 = graffiti.discover<{}>( object.channels, {}, { session: session1 }, ); const value = await nextStreamValue<{}>(iteratorSession1); expect(value.url).toEqual(posted.url); expect(value.value).toEqual(object.value); expect(value.channels).toEqual(object.channels); expect(value.allowed).toEqual(object.allowed); expect(value.actor).toEqual(session1.actor); const iteratorSession2 = graffiti.discover(object.channels, {}, { session: session2 }); expect(await iteratorSession2.next()).toHaveProperty("done", true); const iteratorNoSession = graffiti.discover(object.channels, {}); expect(await iteratorNoSession.next()).toHaveProperty("done", true); }); it("discover allowed", async () => { const object = randomPostObject(); object.allowed = [randomUrl(), session2.actor, randomUrl()]; const posted = await graffiti.post<{}>(object, session1); const iteratorSession2 = graffiti.discover<{}>( object.channels, {}, { session: session2 }, ); const value = await nextStreamValue<{}>(iteratorSession2); expect(value.url).toEqual(posted.url); expect(value.value).toEqual(object.value); expect(value.allowed).toEqual([session2.actor]); expect(value.channels).toEqual(object.channels); expect(value.actor).toEqual(session1.actor); }); it("discover bad schema", async () => { const iterator = graffiti.discover([], { properties: { value: { //@ts-ignore type: "asdf", }, }, }); await expect(iterator.next()).rejects.toThrow(GraffitiErrorInvalidSchema); }); it("discover for actor", async () => { const object1 = randomPostObject(); const posted1 = await graffiti.post<{}>(object1, session1); const object2 = randomPostObject(); object2.channels = object1.channels; const posted2 = await graffiti.post<{}>(object2, session2); const iterator = graffiti.discover<{}>(object1.channels, { properties: { actor: { const: posted1.actor }, }, }); const value = await nextStreamValue<{}>(iterator); expect(value.url).toEqual(posted1.url); expect(value.url).not.toEqual(posted2.url); expect(value.value).toEqual(object1.value); await expect(iterator.next()).resolves.toHaveProperty("done", true); }); it("discover schema allowed, as and not as owner", async () => { const object = randomPostObject(); object.allowed = [randomUrl(), session2.actor, randomUrl()]; await graffiti.post<{}>(object, session1); const iteratorSession1 = graffiti.discover<{}>( object.channels, { properties: { allowed: { minItems: 3, // Make sure session2.actor is in the allow list not: { items: { not: { const: session2.actor }, }, }, }, }, }, { session: session1 }, ); const value = await nextStreamValue<{}>(iteratorSession1); expect(value.value).toEqual(object.value); await expect(iteratorSession1.next()).resolves.toHaveProperty( "done", true, ); const iteratorSession2BigAllow = graffiti.discover( object.channels, { properties: { allowed: { minItems: 3, }, }, }, { session: session2 }, ); await expect(iteratorSession2BigAllow.next()).resolves.toHaveProperty( "done", true, ); const iteratorSession2PeekOther = graffiti.discover( object.channels, { properties: { allowed: { not: { items: { not: { const: object.allowed[0] }, }, }, }, }, }, { session: session2 }, ); await expect(iteratorSession2PeekOther.next()).resolves.toHaveProperty( "done", true, ); const iteratorSession2SmallAllowPeekSelf = graffiti.discover<{}>( object.channels, { properties: { allowed: { maxItems: 1, not: { items: { not: { const: session2.actor }, }, }, }, }, }, { session: session2 }, ); const value2 = await nextStreamValue<{}>( iteratorSession2SmallAllowPeekSelf, ); expect(value2.value).toEqual(object.value); await expect( iteratorSession2SmallAllowPeekSelf.next(), ).resolves.toHaveProperty("done", true); }); it("discover schema channels, as and not as owner", async () => { const object = randomPostObject(); object.channels = [randomString(), randomString(), randomString()]; await graffiti.post<{}>(object, session1); const iteratorSession1 = graffiti.discover<{}>( [object.channels[0], object.channels[2]], { properties: { channels: { minItems: 3, // Make sure channel 1 is in the allow list not: { items: { not: { const: object.channels[1] }, }, }, }, }, }, { session: session1 }, ); const value = await nextStreamValue<{}>(iteratorSession1); expect(value.value).toEqual(object.value); await expect(iteratorSession1.next()).resolves.toHaveProperty( "done", true, ); const iteratorSession2BigAllow = graffiti.discover( [object.channels[0], object.channels[2]], { properties: { channels: { minItems: 3, }, }, }, { session: session2 }, ); await expect(iteratorSession2BigAllow.next()).resolves.toHaveProperty( "done", true, ); const iteratorSession2PeekOther = graffiti.discover( [object.channels[0], object.channels[2]], { properties: { channels: { not: { items: { not: { const: object.channels[1] }, }, }, }, }, }, { session: session2 }, ); await expect(iteratorSession2PeekOther.next()).resolves.toHaveProperty( "done", true, ); const iteratorSession2SmallAllowPeekSelf = graffiti.discover<{}>( [object.channels[0], object.channels[2]], { properties: { allowed: { maxItems: 2, not: { items: { not: { const: object.channels[2] }, }, }, }, }, }, { session: session2 }, ); const value2 = await nextStreamValue<{}>( iteratorSession2SmallAllowPeekSelf, ); expect(value2.value).toEqual(object.value); await expect( iteratorSession2SmallAllowPeekSelf.next(), ).resolves.toHaveProperty("done", true); }); it("discover query for empty allowed", async () => { const publicO = randomPostObject(); const publicSchema = { not: { required: ["allowed"], }, } satisfies JSONSchema; await graffiti.post<{}>(publicO, session1); const iterator = graffiti.discover<{}>( publicO.channels, publicSchema, { session: session1 }, ); const value = await nextStreamValue<{}>(iterator); expect(value.value).toEqual(publicO.value); expect(value.allowed).toBeUndefined(); await expect(iterator.next()).resolves.toHaveProperty("done", true); const restricted = randomPostObject(); restricted.allowed = []; await graffiti.post<{}>(restricted, session1); const iterator2 = graffiti.discover( restricted.channels, publicSchema, { session: session1 }, ); await expect(iterator2.next()).resolves.toHaveProperty("done", true); }); it("discover query for values", async () => { const object1 = randomPostObject(); object1.value = { test: randomString() }; await graffiti.post<{}>(object1, session); const object2 = randomPostObject(); object2.channels = object1.channels; object2.value = { test: randomString(), something: randomString() }; await graffiti.post<{}>(object2, session); const object3 = randomPostObject(); object3.channels = object1.channels; object3.value = { other: randomString(), something: randomString() }; await graffiti.post<{}>(object3, session); const counts = new Map(); for (const property of ["test", "something", "other"] as const) { let count = 0; for await (const result of graffiti.discover(object1.channels, { properties: { value: { required: [property], }, }, })) { assert(!result.error, "result has error"); assert(!result.tombstone, "result is tombstone"); if (property in result.object.value) { count++; } } counts.set(property, count); } expect(counts.get("test")).toBe(2); expect(counts.get("something")).toBe(2); expect(counts.get("other")).toBe(1); }); it("multi 'device' discover", async () => { const graffiti1 = useGraffiti(); const graffiti2 = useGraffiti(); const prepost = randomPostObject(); const posted = await graffiti1.post<{}>(prepost, session1); expect(await graffiti2.get<{}>(posted, {}, { session: session1 })).toEqual(posted); const iterator1 = graffiti1.discover(prepost.channels, {}, { session: session1 }); const iterator2 = graffiti2.discover(prepost.channels, {}, { session: session1 }); const next1 = await iterator1.next(); const next2 = await iterator2.next(); assert(!next1.done && !next1.value.error); assert(!next2.done && !next2.value.error); expect(next1.value.object).toEqual(posted); expect(next2.value.object).toEqual(posted); const next3 = await iterator1.next(); const next4 = await iterator2.next(); assert(next3.done); assert(next4.done); await graffiti1.delete(posted, session1); const iterator3 = graffiti1.continueDiscover( next3.value.cursor, { session: session1 }, ); const next5 = await iterator3.next(); assert(!next5.done && !next5.value.error); expect(next5.value.tombstone).toBe(true); expect(next5.value.object).toEqual(posted); await expect(iterator3.next()).resolves.toHaveProperty("done", true); // Wait for "labels" to propogate // which could introduce a cache bug await new Promise((resolve) => setTimeout(resolve, 1000)); const iterator4 = graffiti2.continueDiscover( next4.value.cursor, { session: session1 }, ); const next6 = await iterator4.next(); assert(!next6.done && !next6.value.error); expect(next6.value.tombstone).toBe(true); expect(next6.value.object).toEqual(posted); await expect(iterator4.next()).resolves.toHaveProperty("done", true); // A deletion was observed by the discovery stream, // so a subsequent get should fail, even without a refresh await expect(graffiti2.get<{}>(posted, {}, { session: session1 })).rejects.toThrow( GraffitiErrorNotFound, ); }); it("concurrent discovery streams progress independently", async () => { const graffiti1 = useGraffiti(); const graffiti2 = useGraffiti(); const object = randomPostObject(); const posted = await graffiti1.post<{}>(object, session1); const first = graffiti1.discover<{}>(object.channels, {}); const second = graffiti2.discover<{}>(object.channels, {}); try { const [firstResult, secondResult] = await Promise.all([ first.next(), second.next(), ]); assert(!firstResult.done && !firstResult.value.error); assert(!secondResult.done && !secondResult.value.error); expect(firstResult.value.object).toEqual(posted); expect(secondResult.value.object).toEqual(posted); const [firstEnd, secondEnd] = await Promise.all([ first.next(), second.next(), ]); expect(firstEnd.done).toBe(true); expect(secondEnd.done).toBe(true); } finally { await Promise.allSettled([ first.return({ cursor: "" }), second.return({ cursor: "" }), ]); } }); for (const continueType of ["cursor"] as const) { describe(`continue discover with ${continueType}`, () => { it("discover for deleted content", async () => { const object = randomPostObject(); const posted = await graffiti.post<{}>(object, session); const iterator1 = graffiti.discover<{}>(object.channels, {}); const value1 = await nextStreamValue<{}>(iterator1); expect(value1.value).toEqual(object.value); const returnValue = await iterator1.next(); assert(returnValue.done, "value2 is not done"); await graffiti.delete(posted, session); const tombIterator = continueStream<{}>( graffiti, returnValue.value, continueType, ); const value = await tombIterator.next(); assert(!value.done && !value.value.error, "value is done"); assert(value.value.tombstone, "value is not tombstone"); expect(value.value.object).toEqual(posted); const returnValue2 = await tombIterator.next(); assert(returnValue2.done, "value2 is not done"); // Post another object const posted2 = await graffiti.post<{}>(object, session); const doubleContinueIterator = continueStream<{}>( graffiti, returnValue2.value, continueType, ); const value2 = await doubleContinueIterator.next(); assert(!value2.done && !value2.value.error, "value2 is done"); assert(!value2.value.tombstone, "value2 is tombstone"); expect(value2.value.object.url).toEqual(posted2.url); await expect(doubleContinueIterator.next()).resolves.toHaveProperty( "done", true, ); }); it("masks a private deletion notice for an approved recipient", async () => { const object = randomPostObject(); object.allowed = [randomUrl(), session2.actor]; const posted = await graffiti.post<{}>(object, session1); const discovery = graffiti.discover<{}>( [object.channels[0]], {}, { session: session2 }, ); const live = await discovery.next(); assert(!live.done && !live.value.error && !live.value.tombstone); const discoveryEnd = await discovery.next(); assert(discoveryEnd.done); await graffiti.delete(posted, session1); const continuation = graffiti.continueDiscover<{}>( discoveryEnd.value.cursor, { session: session2 }, ); const deletion = await continuation.next(); assert(!deletion.done && !deletion.value.error); assert(deletion.value.tombstone); expect(deletion.value.object).toEqual({ ...posted, channels: [object.channels[0]], allowed: [session2.actor], }); await expect(continuation.next()).resolves.toHaveProperty("done", true); }); it("continue with wrong actor", async () => { const iterator = graffiti.discover<{}>([], {}, { session: session1 }); const result = await iterator.next(); assert(result.done, "iterator is not done"); const continuation = continueStream<{}>( graffiti, result.value, continueType, session2, ); await expect(continuation.next()).rejects.toThrow( GraffitiErrorForbidden, ); }); }); } it("lookup non-existant cursor", async () => { const iterator = graffiti.continueDiscover(randomString()); await expect(iterator.next()).rejects.toThrow(GraffitiErrorCursorExpired); }); it("discover and continueDiscover accept legacy session arguments", async () => { const object = await graffiti.post<{}>( { ...randomPostObject(), allowed: [] }, session, ); const stream = graffiti.discover<{}>(object.channels, {}, session); expect((await nextStreamValue<{}>(stream)).url).toBe(object.url); const end = await stream.next(); assert(end.done); await graffiti.delete(object.url, session); const continuation = graffiti.continueDiscover<{}>(end.value.cursor, session); expect(await continuation.next()).toMatchObject({ done: false, value: { tombstone: true, object: { url: object.url } }, }); await continuation.return({ cursor: "" }); }); it("discovery invalidates a read after another device deletes it", async () => { const writer = useGraffiti(); const reader = useGraffiti(); const posted = await writer.post<{}>(randomPostObject(), session); expect(await reader.get<{}>(posted, {}, { session })).toEqual(posted); await writer.delete(posted, session); for await (const result of reader.discover<{}>(posted.channels, {}, { session })) { assert(!result.error); } // Get should fail even without a refresh, since the discovery stream observed the deletion await expect(reader.get<{}>(posted, {}, { session })).rejects.toThrow( GraffitiErrorNotFound, ); }); it("discovers without refresh and returns cursors that can be refreshed", async () => { const writer = useGraffiti(); const reader = useGraffiti(); const first = await writer.post<{}>(randomPostObject(), session); // A cache miss should still fetch results even without a refresh. const initial = reader.discover<{}>(first.channels, {}, { session, refresh: false }); expect(await nextStreamValue<{}>(initial)).toEqual(first); await expect(initial.next()).resolves.toHaveProperty("done", true); const repeated = reader.discover<{}>(first.channels, {}, { session, refresh: false }); expect(await nextStreamValue<{}>(repeated)).toEqual(first); const end = await repeated.next(); assert(end.done); const unchanged = reader.continueDiscover<{}>(end.value.cursor, { session, refresh: false }); const unchangedEnd = await unchanged.next(); assert(unchangedEnd.done); const second = await writer.post<{}>( { ...randomPostObject(), channels: first.channels }, session, ); await writer.delete(first, session); const added: string[] = []; const deleted: string[] = []; // A cursor obtained without refreshing should still find subsequent // additions and deletions when continued with the default refresh. for await (const result of reader.continueDiscover<{}>(unchangedEnd.value.cursor, { session })) { assert(!result.error); (result.tombstone ? deleted : added).push(result.object.url); } expect(added).toEqual([second.url]); expect(deleted).toEqual([first.url]); }); it("discovery without refresh respects local deletions and deletions learned by another query", async () => { const writer = useGraffiti(); const reader = useGraffiti(); const posted = await writer.post<{}>(randomPostObject(), session); for (const instance of [writer, reader]) { const initial = instance.discover<{}>(posted.channels, {}, { session }); expect(await nextStreamValue<{}>(initial)).toEqual(posted); await expect(initial.next()).resolves.toHaveProperty("done", true); } const otherQuery = reader.discover<{}>( posted.channels, { properties: { actor: { const: session.actor } } }, { session }, ); expect(await nextStreamValue<{}>(otherQuery)).toEqual(posted); const end = await otherQuery.next(); assert(end.done); await writer.delete(posted, session); const continuation = reader.continueDiscover<{}>(end.value.cursor, { session }); expect(await continuation.next()).toMatchObject({ done: false, value: { tombstone: true, object: { url: posted.url } }, }); await expect(continuation.next()).resolves.toHaveProperty("done", true); // Both the local deletion and the deletion observed by another query // should be reflected without refreshing the original query. for (const instance of [writer, reader]) { const saved = instance.discover<{}>(posted.channels, {}, { session, refresh: false }); for await (const result of saved) { assert(!result.error); expect(result.tombstone).toBe(true); } } }); }); };