Something went wrong. Try again.
cee sky
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160import {Platform} from 'react-native'import {getCalendars} from 'expo-localization'import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals'
import {BSKY_FEED_OWNER_DIDS, DISCOVER_FEED_URI} from '#/lib/constants'import {type UsePreferencesQueryResponse} from '#/state/queries/preferences'import {device} from '#/storage'
jest.mock('#/env', () => ({IS_WEB: false}))jest.mock('#/storage', () => ({device: {get: jest.fn()}}))
import { aggregateUserInterests, createBskyFeedHeaders, createBskyTopicsHeader,} from '../utils'
beforeEach(() => { jest.mocked(device.get).mockReset() jest.mocked(getCalendars).mockClear() jest.replaceProperty(Platform, 'OS', 'ios')})afterEach(() => { jest.restoreAllMocks()})
describe('createBskyFeedHeaders', () => { it.each(['web', 'ios', 'android'] as const)( 'sends the %s platform even without geolocation data', platform => { jest.replaceProperty(Platform, 'OS', platform) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': platform, }) }, )
it.each(BSKY_FEED_OWNER_DIDS)( 'sends service data to feeds owned by %s and encodes the city', did => { jest.mocked(device.get).mockReturnValue({ countryCode: 'JP', regionCode: '13', city: '東京', ip: '2001:db8::1', })
expect( createBskyFeedHeaders(`at://${did}/app.bsky.feed.generator/test`), ).toEqual({ 'x-atproto-bsky-platform': 'ios', 'x-atproto-bsky-geo-country': 'JP', 'x-atproto-bsky-geo-region': '13', 'x-atproto-bsky-geo-city': '%E6%9D%B1%E4%BA%AC', 'x-atproto-bsky-geo-ip': '2001:db8::1', }) expect(device.get).toHaveBeenCalledTimes(1) expect(device.get).toHaveBeenCalledWith(['geolocationServiceResponse']) }, )
it('does not send platform, location or timezone to third-party feeds', () => { expect( createBskyFeedHeaders( 'at://did:example:feed/app.bsky.feed.generator/test', ), ).toEqual({}) expect(device.get).not.toHaveBeenCalled() expect(getCalendars).not.toHaveBeenCalled() })
it('sends the device timezone and reads it on each request', () => { const calendars = jest.mocked(getCalendars) calendars.mockReturnValueOnce([calendar('Asia/Tokyo')]) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': 'ios', 'x-atproto-bsky-timezone': 'Asia/Tokyo', })
calendars.mockReturnValueOnce([calendar('America/Sao_Paulo')]) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': 'ios', 'x-atproto-bsky-timezone': 'America/Sao_Paulo', }) })
it('omits the timezone when the device does not report one', () => { jest.mocked(getCalendars).mockReturnValueOnce([calendar(null)]) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': 'ios', }) })
it('omits missing values and reads updated service data on each request', () => { const get = jest.mocked(device.get) get.mockReturnValueOnce(undefined) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': 'ios', })
get.mockReturnValueOnce({countryCode: 'US', regionCode: undefined}) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': 'ios', 'x-atproto-bsky-geo-country': 'US', })
get.mockReturnValueOnce({countryCode: '', regionCode: '', city: '', ip: ''}) expect(createBskyFeedHeaders(DISCOVER_FEED_URI)).toEqual({ 'x-atproto-bsky-platform': 'ios', }) })})
function calendar(timeZone: string | null) { return {calendar: null, uses24hourClock: null, firstWeekday: null, timeZone}}
function preferences( interests: Pick< UsePreferencesQueryResponse['interests'], 'tags' | 'updatedAt' >,) { return {interests} as UsePreferencesQueryResponse}
describe('aggregateUserInterests', () => { it('appends the update timestamp after a semicolon', () => { expect( createBskyTopicsHeader( aggregateUserInterests( preferences({ tags: ['animals', 'music'], updatedAt: '2026-09-03T17:00:00.000Z', }), ), ), ).toEqual({ 'x-atproto-bsky-topics': 'animals,music;2026-09-03T17:00:00.000Z', }) })
it('supports interests created before update timestamps were recorded', () => { expect(aggregateUserInterests(preferences({tags: ['animals']}))).toBe( 'animals', ) })
it('passes the timestamp when the user has no selected interests', () => { expect( aggregateUserInterests( preferences({ tags: [], updatedAt: '2026-09-03T17:00:00.000Z', }), ), ).toBe(';2026-09-03T17:00:00.000Z') })})