this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

test(firehose): added factories for faker.js

ansxor d8a425aa 3614c7ba

+87 -68
+1
apps/firehose/package.json
··· 4 4 "version": "1.0.0", 5 5 "type": "module", 6 6 "devDependencies": { 7 + "@faker-js/faker": "^10.3.0", 7 8 "@types/bun": "latest" 8 9 }, 9 10 "dependencies": {
+83 -68
apps/firehose/tests/firehose.test.ts
··· 4 4 import { loop } from "../index"; 5 5 import type { JetstreamEvent } from "@atcute/jetstream"; 6 6 import type { CaAnsxorCatnipTrack } from "lexicon/atcute-lexicon"; 7 + import { faker } from "@faker-js/faker"; 7 8 8 9 let db: Awaited<ReturnType<typeof setupTestDb>>; 9 10 let txDb: typeof db; ··· 47 48 }; 48 49 } 49 50 50 - const SAMPLE_DID = "did:plc:ragtjsm2j2vknwkz3zp4oxrd"; 51 - const SAMPLE_RKEY = "3jui7kd54zh2i"; 52 - const SAMPLE_CID = 53 - "bafyreig2fjxi3a743bb54z2at6lsv2brz4f5d3hefma6ludqt5d2k2ofey"; 54 - const SAMPLE_COLLECTION = "ca.ansxor.catnip.track"; 51 + function fakeDid(): `did:${string}:${string}` { 52 + return `did:plc:${faker.string.alphanumeric({ length: 24, casing: "lower" })}`; 53 + } 55 54 56 - const createTrackEvent: JetstreamEvent = { 57 - did: SAMPLE_DID, 58 - time_us: Date.now() * 1000, 59 - kind: "commit", 60 - commit: { 61 - operation: "create", 62 - collection: SAMPLE_COLLECTION, 63 - rkey: SAMPLE_RKEY, 64 - rev: "3jui7kd54zh2i", 65 - cid: SAMPLE_CID, 66 - record: { 67 - $type: "ca.ansxor.catnip.track", 68 - title: "Midnight Echoes", 69 - createdAt: new Date().toISOString(), 70 - audio: { 71 - $type: "blob", 72 - ref: { 73 - $link: "bafyreig2fjxi3a743bb54z2at6lsv2brz4f5d3hefma6ludqt5d2k2ofey", 74 - }, 75 - mimeType: "audio/ogg", 76 - size: 5_000_000, 77 - }, 78 - } satisfies InferOutput<typeof CaAnsxorCatnipTrack.mainSchema>, 79 - }, 80 - }; 55 + function fakeCid(): string { 56 + return `bafyrei${faker.string.alphanumeric({ length: 52, casing: "lower" })}`; 57 + } 81 58 82 - const createTrackWithArtistsEvent: JetstreamEvent = { 83 - did: SAMPLE_DID, 84 - time_us: Date.now() * 1000, 85 - kind: "commit", 86 - commit: { 87 - operation: "create", 88 - collection: SAMPLE_COLLECTION, 89 - rkey: SAMPLE_RKEY, 90 - rev: "3jui7kd54zh2i", 91 - cid: SAMPLE_CID, 92 - record: { 93 - $type: "ca.ansxor.catnip.track", 94 - title: "Midnight Echoes", 95 - createdAt: new Date().toISOString(), 96 - audio: { 97 - $type: "blob", 98 - ref: { 99 - $link: "bafyreig2fjxi3a743bb54z2at6lsv2brz4f5d3hefma6ludqt5d2k2ofey", 100 - }, 101 - mimeType: "audio/ogg", 102 - size: 5_000_000, 103 - }, 104 - artists: [ 105 - { did: SAMPLE_DID, name: "Echo Artist" }, 106 - { name: "Guest Vocalist" }, 107 - ], 108 - } satisfies InferOutput<typeof CaAnsxorCatnipTrack.mainSchema>, 59 + function fakeTid(): string { 60 + return faker.string.alphanumeric(13); 61 + } 62 + 63 + function fakeTrackRecord( 64 + overrides?: Partial<InferOutput<typeof CaAnsxorCatnipTrack.mainSchema>>, 65 + ): InferOutput<typeof CaAnsxorCatnipTrack.mainSchema> { 66 + return { 67 + $type: "ca.ansxor.catnip.track", 68 + title: faker.music.songName(), 69 + createdAt: faker.date.recent().toISOString(), 70 + audio: { 71 + $type: "blob", 72 + ref: { $link: fakeCid() }, 73 + mimeType: "audio/ogg", 74 + size: faker.number.int({ min: 100_000, max: 50_000_000 }), 75 + }, 76 + ...overrides, 77 + }; 78 + } 79 + 80 + function fakeJetstreamCreateEvent( 81 + overrides?: { 82 + did?: `did:${string}:${string}`; 83 + rkey?: string; 84 + cid?: string; 85 + record?: InferOutput<typeof CaAnsxorCatnipTrack.mainSchema>; 109 86 }, 110 - }; 87 + ): JetstreamEvent { 88 + return { 89 + did: overrides?.did ?? fakeDid(), 90 + time_us: Date.now() * 1000, 91 + kind: "commit", 92 + commit: { 93 + operation: "create", 94 + collection: "ca.ansxor.catnip.track", 95 + rkey: overrides?.rkey ?? fakeTid(), 96 + rev: fakeTid(), 97 + cid: overrides?.cid ?? fakeCid(), 98 + record: overrides?.record ?? fakeTrackRecord(), 99 + }, 100 + }; 101 + } 111 102 112 103 test("inserts track from create commit event", async () => { 113 - const subscription = mockSubscription([createTrackEvent]); 104 + const did = fakeDid(); 105 + const rkey = fakeTid(); 106 + const cid = fakeCid(); 107 + const title = faker.music.songName(); 108 + const event = fakeJetstreamCreateEvent({ 109 + did, 110 + rkey, 111 + cid, 112 + record: fakeTrackRecord({ title }), 113 + }); 114 + 115 + const subscription = mockSubscription([event]); 114 116 await loop(subscription, txDb); 115 117 116 118 const tracks = await txDb.query.tracks.findMany(); ··· 118 120 const track = tracks[0]; 119 121 expect(track).not.toBeUndefined(); 120 122 expect(track!.uri).toBe( 121 - `at://${SAMPLE_DID}/${SAMPLE_COLLECTION}/${SAMPLE_RKEY}`, 123 + `at://${did}/ca.ansxor.catnip.track/${rkey}`, 122 124 ); 123 - expect(track!.did).toBe(SAMPLE_DID); 124 - expect(track!.rkey).toBe(SAMPLE_RKEY); 125 - expect(track!.cid).toBe(SAMPLE_CID); 126 - expect(track!.title).toBe("Midnight Echoes"); 125 + expect(track!.did).toBe(did); 126 + expect(track!.rkey).toBe(rkey); 127 + expect(track!.cid).toBe(cid); 128 + expect(track!.title).toBe(title); 127 129 }); 128 130 129 131 test("inserts artists from create commit event", async () => { 130 - const subscription = mockSubscription([createTrackWithArtistsEvent]); 132 + const did = fakeDid(); 133 + const artistName = faker.person.fullName(); 134 + const guestName = faker.person.fullName(); 135 + const event = fakeJetstreamCreateEvent({ 136 + did, 137 + record: fakeTrackRecord({ 138 + artists: [ 139 + { did, name: artistName }, 140 + { name: guestName }, 141 + ], 142 + }), 143 + }); 144 + 145 + const subscription = mockSubscription([event]); 131 146 await loop(subscription, txDb); 132 147 133 148 const artists = await txDb.query.artists.findMany(); 134 149 expect(artists).toHaveLength(2); 135 150 136 - const didArtist = artists.find((a) => a.did === SAMPLE_DID); 151 + const didArtist = artists.find((a) => a.did === did); 137 152 expect(didArtist).not.toBeUndefined(); 138 - expect(didArtist!.name).toBe("Echo Artist"); 153 + expect(didArtist!.name).toBe(artistName); 139 154 140 - const nameOnlyArtist = artists.find((a) => a.name === "Guest Vocalist"); 155 + const nameOnlyArtist = artists.find((a) => a.name === guestName); 141 156 expect(nameOnlyArtist).not.toBeUndefined(); 142 157 expect(nameOnlyArtist!.did).toBeNull(); 143 158
+3
bun.lock
··· 45 45 "lexicon": "workspace:*", 46 46 }, 47 47 "devDependencies": { 48 + "@faker-js/faker": "^10.3.0", 48 49 "@types/bun": "latest", 49 50 }, 50 51 }, ··· 289 290 "@eslint/object-schema": ["@eslint/object-schema@2.1.7", "", {}, "sha512-VtAOaymWVfZcmZbp6E2mympDIHvyjXs/12LqWYjVw6qjrfF+VK+fyG33kChz3nnK+SU5/NeHOqrTEHS8sXO3OA=="], 290 291 291 292 "@eslint/plugin-kit": ["@eslint/plugin-kit@0.4.1", "", { "dependencies": { "@eslint/core": "^0.17.0", "levn": "^0.4.1" } }, "sha512-43/qtrDUokr7LJqoF2c3+RInu/t4zfrpYdoSDfYyhg52rwLV6TnOvdG4fXm7IkSB3wErkcmJS9iEhjVtOSEjjA=="], 293 + 294 + "@faker-js/faker": ["@faker-js/faker@10.3.0", "", {}, "sha512-It0Sne6P3szg7JIi6CgKbvTZoMjxBZhcv91ZrqrNuaZQfB5WoqYYbzCUOq89YR+VY8juY9M1vDWmDDa2TzfXCw=="], 292 295 293 296 "@humanfs/core": ["@humanfs/core@0.19.1", "", {}, "sha512-5DyQ4+1JEUzejeK1JGICcideyfUbGixgS9jNgex5nqkW+cY7WZhxBigmieN5Qnw9ZosSNVC9KQKyb+GUaGyKUA=="], 294 297