Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157import {Platform} from 'react-native'import {type Client} from '@atproto/lex'
import {DISCOVER_FEED_URI} from '#/lib/constants'import {getContentLanguages} from '#/state/preferences/languages'import {app} from '#/lexicons'import {device} from '#/storage'import {MergeFeedAPI} from './merge'
const post = {} as app.bsky.feed.defs.FeedViewPost
describe('MergeFeedAPI', () => { it('sends platform and topics, and no location, only to owned merged sources', async () => { const get = jest.spyOn(device, 'get').mockReturnValue({ countryCode: 'JP', regionCode: '13', city: '東京', ip: '192.0.2.1', } as never) const call = jest.fn().mockResolvedValue({feed: []}) const thirdPartyFeed = 'at://did:example:feed/app.bsky.feed.generator/test' const api = new MergeFeedAPI({ client: {call} as unknown as Client, feedParams: { mergeFeedEnabled: true, mergeFeedSources: [DISCOVER_FEED_URI, thirdPartyFeed], }, feedTuners: [], userInterests: 'music', }) api.reset() api.following.hasMore = false
try { await api.fetch({cursor: 'started', limit: 10})
expect(call).toHaveBeenCalledTimes(2) expect(call).toHaveBeenCalledWith( app.bsky.feed.getFeed, expect.objectContaining({feed: DISCOVER_FEED_URI}), { headers: { 'x-atproto-bsky-platform': Platform.OS, 'x-atproto-bsky-topics': 'music', 'Accept-Language': getContentLanguages().join(','), }, }, ) expect(call).toHaveBeenCalledWith( app.bsky.feed.getFeed, expect.objectContaining({feed: thirdPartyFeed}), {headers: {'Accept-Language': getContentLanguages().join(',')}}, ) } finally { get.mockRestore() } })
it('drains a terminal following queue without restarting the source', async () => { const api = new MergeFeedAPI({ client: {} as Client, feedParams: {}, feedTuners: [], }) api.reset() api.following.queue = [post, post, post] api.following.hasMore = false
const first = await api.fetch({cursor: 'started', limit: 2}) const second = await api.fetch({cursor: first.cursor, limit: 2})
expect(first.feed).toHaveLength(2) expect(first.cursor).toBeDefined() expect(second.feed).toHaveLength(1) expect(second.cursor).toBeUndefined() })
it('stops when the following source repeats its cursor', async () => { const call = jest .fn() .mockResolvedValueOnce({feed: [], cursor: 'a'}) .mockResolvedValueOnce({feed: [], cursor: 'a'}) const api = new MergeFeedAPI({ client: {call} as unknown as Client, feedParams: {}, feedTuners: [], })
const first = await api.fetch({cursor: undefined, limit: 1}) const second = await api.fetch({cursor: first.cursor, limit: 1})
expect(first.cursor).toBeDefined() expect(second.cursor).toBeUndefined() expect(call).toHaveBeenCalledTimes(2) })
it('stops when the following source returns an empty cursor', async () => { const call = jest.fn().mockResolvedValue({feed: [], cursor: ''}) const api = new MergeFeedAPI({ client: {call} as unknown as Client, feedParams: {}, feedTuners: [], })
const result = await api.fetch({cursor: undefined, limit: 1})
expect(result.cursor).toBeUndefined() expect(call).toHaveBeenCalledTimes(1) })
it('stops when the following source cycles to an earlier cursor', async () => { const call = jest .fn() .mockResolvedValueOnce({feed: [], cursor: 'a'}) .mockResolvedValueOnce({feed: [], cursor: 'b'}) .mockResolvedValueOnce({feed: [], cursor: 'a'}) const api = new MergeFeedAPI({ client: {call} as unknown as Client, feedParams: {}, feedTuners: [], })
const first = await api.fetch({cursor: undefined, limit: 1}) const second = await api.fetch({cursor: first.cursor, limit: 1}) const third = await api.fetch({cursor: second.cursor, limit: 1})
expect(third.cursor).toBeUndefined() expect(call).toHaveBeenCalledTimes(3) })
it('drains a terminal custom-feed queue without restarting the source', async () => { const call = jest.fn() const api = new MergeFeedAPI({ client: {call} as unknown as Client, feedParams: { mergeFeedEnabled: true, mergeFeedSources: [ 'at://did:example:feed/app.bsky.feed.generator/test', ], }, feedTuners: [], }) api.reset() api.following.hasMore = false api.customFeeds[0].queue = [post, post, post] api.customFeeds[0].hasMore = false
const first = await api.fetch({cursor: 'started', limit: 2}) const second = await api.fetch({cursor: first.cursor, limit: 2})
expect(first.feed).toHaveLength(2) expect(second.feed).toHaveLength(1) expect(second.cursor).toBeUndefined() expect(call).not.toHaveBeenCalled() })})