Something went wrong. Try again.
Bluesky app fork with some witchin' additions 💫 forked from jollywhoppers.com/witchsky.app
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import {beforeEach, describe, expect, it, jest} from '@jest/globals'
jest.mock('#/analytics/identifiers', () => ({ getDeviceId: jest.fn(), getSessionId: jest.fn(),}))
import {getDeviceId, getSessionId} from '#/analytics/identifiers'import {app} from '#/lexicons'import {createLexClient} from './lexClient'
const fetchHandler = jest.fn( (_path: `/${string}`, _init: RequestInit): Promise<Response> => Promise.resolve( new Response( JSON.stringify({did: 'did:plc:alice', handle: 'alice.test'}), {headers: {'content-type': 'application/json'}}, ), ),)
function requestHeaders(call: number): Headers { return new Headers(fetchHandler.mock.calls[call][1].headers)}
describe('createLexClient', () => { beforeEach(() => { fetchHandler.mockClear() })
it('sends the current device and session identifiers on every request', async () => { jest.mocked(getDeviceId).mockReturnValue('device-123') jest.mocked(getSessionId).mockReturnValue('session-123') const client = createLexClient( {fetchHandler}, {includeDeviceSessionHeaders: true}, )
await client.call(app.bsky.actor.getProfile, {actor: 'alice.test'}) jest.mocked(getSessionId).mockReturnValue('session-456') await client.call(app.bsky.actor.getProfile, {actor: 'alice.test'})
expect(requestHeaders(0).get('x-atproto-device-id')).toBe('device-123') expect(requestHeaders(0).get('x-atproto-session-id')).toBe('session-123') expect(requestHeaders(1).get('x-atproto-device-id')).toBe('device-123') expect(requestHeaders(1).get('x-atproto-session-id')).toBe('session-456') })
it('omits identifiers unless the destination explicitly opts in', async () => { jest.mocked(getDeviceId).mockReturnValue('device-123') jest.mocked(getSessionId).mockReturnValue('session-123') const client = createLexClient({fetchHandler})
await client.call(app.bsky.actor.getProfile, {actor: 'alice.test'})
expect(requestHeaders(0).get('x-atproto-device-id')).toBeNull() expect(requestHeaders(0).get('x-atproto-session-id')).toBeNull() })
it('omits the device identifier before initialization', async () => { jest.mocked(getDeviceId).mockReturnValue(undefined) jest.mocked(getSessionId).mockReturnValue('session-123') const client = createLexClient( {fetchHandler}, {includeDeviceSessionHeaders: true}, )
await client.call(app.bsky.actor.getProfile, {actor: 'alice.test'})
expect(requestHeaders(0).get('x-atproto-device-id')).toBeNull() expect(requestHeaders(0).get('x-atproto-session-id')).toBe('session-123') })})