Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
research-tool social-network tools-for-thought atproto
Something went wrong. Try again.
8.2 kB · 230 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231import { PostgreSqlContainer, StartedPostgreSqlContainer,} from '@testcontainers/postgresql';import postgres from 'postgres';import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';import { DrizzleCollectionQueryRepository } from '../../infrastructure/repositories/DrizzleCollectionQueryRepository';import { DrizzleCardRepository } from '../../infrastructure/repositories/DrizzleCardRepository';import { DrizzleCollectionRepository } from '../../infrastructure/repositories/DrizzleCollectionRepository';import { CuratorId } from '../../domain/value-objects/CuratorId';import { cards } from '../../infrastructure/repositories/schema/card.sql';import { collections, collectionCards,} from '../../infrastructure/repositories/schema/collection.sql';import { libraryMemberships } from '../../infrastructure/repositories/schema/libraryMembership.sql';import { publishedRecords } from '../../infrastructure/repositories/schema/publishedRecord.sql';import { CardBuilder } from '../utils/builders/CardBuilder';import { CollectionBuilder } from '../utils/builders/CollectionBuilder';import { URL } from '../../domain/value-objects/URL';import { createTestSchema } from '../test-utils/createTestSchema';import { CardTypeEnum } from '../../domain/value-objects/CardType';import { CollectionAccessType } from '../../domain/Collection';
/** * Covers the `excludeUrl` anti-join used to keep already-containing * collections out of recommendations. */describe('DrizzleCollectionQueryRepository - excludeUrl filtering', () => { let container: StartedPostgreSqlContainer; let db: PostgresJsDatabase; let queryRepository: DrizzleCollectionQueryRepository; let cardRepository: DrizzleCardRepository; let collectionRepository: DrizzleCollectionRepository;
let curator1: CuratorId; let curator2: CuratorId;
// The URL being recommended for const TARGET_URL = 'https://example.com/target-article'; // A semantically similar URL that seeds the recommendation const SIMILAR_URL = 'https://example.com/similar-article';
beforeAll(async () => { container = await new PostgreSqlContainer('postgres:14').start();
const connectionString = container.getConnectionUri(); process.env.DATABASE_URL = connectionString; const client = postgres(connectionString); db = drizzle(client);
queryRepository = new DrizzleCollectionQueryRepository(db); cardRepository = new DrizzleCardRepository(db); collectionRepository = new DrizzleCollectionRepository(db);
await createTestSchema(db);
curator1 = CuratorId.create('did:plc:curator1').unwrap(); curator2 = CuratorId.create('did:plc:curator2').unwrap(); }, 60000);
afterAll(async () => { await container.stop(); });
beforeEach(async () => { await db.delete(collectionCards); await db.delete(collections); await db.delete(libraryMemberships); await db.delete(cards); await db.delete(publishedRecords); });
/** * Builds two collections for the given author: one holding only the similar * URL, one holding the similar URL *and* the target URL. */ async function seedCollections( curator: CuratorId, accessType: CollectionAccessType, ) { const similarUrl = URL.create(SIMILAR_URL).unwrap(); const targetUrl = URL.create(TARGET_URL).unwrap();
const similarCardA = new CardBuilder() .withCuratorId(curator.value) .withType(CardTypeEnum.URL) .withUrl(similarUrl) .buildOrThrow(); const similarCardB = new CardBuilder() .withCuratorId(curator.value) .withType(CardTypeEnum.URL) .withUrl(similarUrl) .buildOrThrow(); const targetCard = new CardBuilder() .withCuratorId(curator.value) .withType(CardTypeEnum.URL) .withUrl(targetUrl) .buildOrThrow();
await cardRepository.save(similarCardA); await cardRepository.save(similarCardB); await cardRepository.save(targetCard);
const eligible = new CollectionBuilder() .withAuthorId(curator.value) .withName('Eligible') .withAccessType(accessType) .buildOrThrow(); eligible.addCard(similarCardA.cardId, curator);
const alreadyHasTarget = new CollectionBuilder() .withAuthorId(curator.value) .withName('Already Has Target') .withAccessType(accessType) .buildOrThrow(); alreadyHasTarget.addCard(similarCardB.cardId, curator); alreadyHasTarget.addCard(targetCard.cardId, curator);
await collectionRepository.save(eligible); await collectionRepository.save(alreadyHasTarget);
return { eligible, alreadyHasTarget }; }
describe('getCollectionsForUrlsByAuthor', () => { it('returns both collections when no excludeUrl is given', async () => { const { eligible, alreadyHasTarget } = await seedCollections( curator1, CollectionAccessType.CLOSED, );
const result = await queryRepository.getCollectionsForUrlsByAuthor( [SIMILAR_URL], curator1.value, );
const ids = result.map((c) => c.id); expect(ids).toContain(eligible.collectionId.getStringValue()); expect(ids).toContain(alreadyHasTarget.collectionId.getStringValue()); });
it('excludes collections that already contain the target URL', async () => { const { eligible, alreadyHasTarget } = await seedCollections( curator1, CollectionAccessType.CLOSED, );
const result = await queryRepository.getCollectionsForUrlsByAuthor( [SIMILAR_URL], curator1.value, TARGET_URL, );
const ids = result.map((c) => c.id); expect(ids).toContain(eligible.collectionId.getStringValue()); expect(ids).not.toContain(alreadyHasTarget.collectionId.getStringValue()); });
it('still reports matched URLs on the surviving collections', async () => { await seedCollections(curator1, CollectionAccessType.CLOSED);
const result = await queryRepository.getCollectionsForUrlsByAuthor( [SIMILAR_URL], curator1.value, TARGET_URL, );
expect(result).toHaveLength(1); expect(result[0]!.matchedUrls).toEqual([SIMILAR_URL]); }); });
describe('getOpenCollectionsForUrls', () => { it('excludes collections that already contain the target URL', async () => { const { eligible, alreadyHasTarget } = await seedCollections( curator2, CollectionAccessType.OPEN, );
const result = await queryRepository.getOpenCollectionsForUrls( [SIMILAR_URL], curator1.value, // exclude the calling user's own collections TARGET_URL, );
const ids = result.map((c) => c.id); expect(ids).toContain(eligible.collectionId.getStringValue()); expect(ids).not.toContain(alreadyHasTarget.collectionId.getStringValue()); });
it('excludes based on the URL regardless of who added the card', async () => { // curator2 owns an OPEN collection; curator1 contributes the target URL // to it. It should still be excluded. const similarUrl = URL.create(SIMILAR_URL).unwrap(); const targetUrl = URL.create(TARGET_URL).unwrap();
const similarCard = new CardBuilder() .withCuratorId(curator2.value) .withType(CardTypeEnum.URL) .withUrl(similarUrl) .buildOrThrow(); const targetCard = new CardBuilder() .withCuratorId(curator1.value) .withType(CardTypeEnum.URL) .withUrl(targetUrl) .buildOrThrow();
await cardRepository.save(similarCard); await cardRepository.save(targetCard);
const collection = new CollectionBuilder() .withAuthorId(curator2.value) .withName('Open Collection') .withAccessType(CollectionAccessType.OPEN) .buildOrThrow(); collection.addCard(similarCard.cardId, curator2); collection.addCard(targetCard.cardId, curator1); await collectionRepository.save(collection);
const result = await queryRepository.getOpenCollectionsForUrls( [SIMILAR_URL], curator1.value, TARGET_URL, );
expect(result).toHaveLength(0); }); });});