diff --git a/src/state/session/__tests__/oauth-resume-test.ts b/src/state/session/__tests__/oauth-resume-test.ts index a1feb4c68..16d6fb533 100644 --- a/src/state/session/__tests__/oauth-resume-test.ts +++ b/src/state/session/__tests__/oauth-resume-test.ts @@ -1,7 +1,11 @@ import {type OAuthSession, TokenRefreshError} from '@atproto/oauth-client' import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals' -import {oauthResumeSessionWithRetry, withTimeout} from '../oauth-agent' +import {com} from '#/lexicons' +import { + resumeOAuthSessionBundleWithRetry, + withTimeout, +} from '../oauth-agent' import {emitOauthTelemetry} from '../oauth-telemetry' import {type SessionAccount} from '../types' @@ -18,11 +22,6 @@ jest.mock('@atproto/oauth-client-expo', () => { } }) -jest.mock('@atproto/lexicon', () => ({ - ...jest.requireActual('@atproto/lexicon'), - jsonStringToLex: (str: string): unknown => JSON.parse(str), -})) - jest.mock('../moderation', () => ({ configureModerationForAccount: () => Promise.resolve(), })) @@ -82,7 +81,7 @@ const degradedEvents = () => .mock.calls.map(([event]) => event) .filter(event => event.type === 'oauth:sessionResumeDegraded') -describe('oauthResumeSessionWithRetry', () => { +describe('resumeOAuthSessionBundleWithRetry', () => { beforeEach(() => { mockRestore.mockReset() jest.mocked(emitOauthTelemetry).mockClear() @@ -99,7 +98,7 @@ describe('oauthResumeSessionWithRetry', () => { .mockResolvedValueOnce(okSession()) mockRestore.mockResolvedValue(fakeSession(fetchHandler)) - const {account} = await oauthResumeSessionWithRetry(storedAccount, true) + const {account} = await resumeOAuthSessionBundleWithRetry(storedAccount, true) expect(account.handle).toBe('alice.fresh') expect(mockRestore).toHaveBeenCalledTimes(2) @@ -114,7 +113,7 @@ describe('oauthResumeSessionWithRetry', () => { .mockResolvedValueOnce(okSession()) mockRestore.mockResolvedValue(fakeSession(fetchHandler)) - const {account, agent} = await oauthResumeSessionWithRetry( + const {account, bundle} = await resumeOAuthSessionBundleWithRetry( storedAccount, true, ) @@ -128,7 +127,10 @@ describe('oauthResumeSessionWithRetry', () => { }, ]) - const {data} = await agent.com.atproto.server.getSession() + const data = await bundle.pdsClient.call( + com.atproto.server.getSession, + {}, + ) expect(data.handle).toBe('alice.fresh') expect(mockRestore).toHaveBeenCalledTimes(3) }) @@ -137,7 +139,7 @@ describe('oauthResumeSessionWithRetry', () => { jest.useFakeTimers() mockRestore.mockReturnValue(new Promise(() => {})) - const resumed = oauthResumeSessionWithRetry(storedAccount, true) + const resumed = resumeOAuthSessionBundleWithRetry(storedAccount, true) await jest.advanceTimersByTimeAsync(10_000) const {account} = await resumed @@ -157,7 +159,7 @@ describe('oauthResumeSessionWithRetry', () => { async (error, message, errorCategory) => { mockRestore.mockResolvedValue(fakeSession(dpopError(error, message))) - const {account} = await oauthResumeSessionWithRetry(storedAccount, true) + const {account} = await resumeOAuthSessionBundleWithRetry(storedAccount, true) expect(account).toEqual(storedAccount) expect(mockRestore).toHaveBeenCalledTimes(2) @@ -177,7 +179,7 @@ describe('oauthResumeSessionWithRetry', () => { ) await expect( - oauthResumeSessionWithRetry(storedAccount, true), + resumeOAuthSessionBundleWithRetry(storedAccount, true), ).rejects.toThrow(message) expect(mockRestore).toHaveBeenCalledTimes(1) expect(degradedEvents()).toEqual([]) @@ -186,7 +188,7 @@ describe('oauthResumeSessionWithRetry', () => { it('keeps the stored account when the token refresh request fails', async () => { mockRestore.mockRejectedValue(new TypeError('Network request failed')) - const {account} = await oauthResumeSessionWithRetry(storedAccount, true) + const {account} = await resumeOAuthSessionBundleWithRetry(storedAccount, true) expect(account).toEqual(storedAccount) expect(mockRestore).toHaveBeenCalledTimes(2) @@ -202,7 +204,7 @@ describe('oauthResumeSessionWithRetry', () => { ), ) - await expect(oauthResumeSessionWithRetry(storedAccount)).rejects.toThrow( + await expect(resumeOAuthSessionBundleWithRetry(storedAccount)).rejects.toThrow( 'NotEnoughResources', ) expect(mockRestore).toHaveBeenCalledTimes(2) @@ -215,7 +217,7 @@ describe('oauthResumeSessionWithRetry', () => { ) await expect( - oauthResumeSessionWithRetry(storedAccount, true), + resumeOAuthSessionBundleWithRetry(storedAccount, true), ).rejects.toThrow('Refresh token replayed') expect(mockRestore).toHaveBeenCalledTimes(1) expect(degradedEvents()).toEqual([]) @@ -234,7 +236,7 @@ describe('oauthResumeSessionWithRetry', () => { ) await expect( - oauthResumeSessionWithRetry(storedAccount, true), + resumeOAuthSessionBundleWithRetry(storedAccount, true), ).rejects.toThrow('Account has been taken down') expect(mockRestore).toHaveBeenCalledTimes(1) expect(degradedEvents()).toEqual([]) @@ -249,7 +251,7 @@ describe('oauthResumeSessionWithRetry', () => { ) await expect( - oauthResumeSessionWithRetry(storedAccount, true), + resumeOAuthSessionBundleWithRetry(storedAccount, true), ).rejects.toThrow() expect(mockRestore).toHaveBeenCalledTimes(1) expect(degradedEvents()).toEqual([]) @@ -268,7 +270,7 @@ describe('oauthResumeSessionWithRetry', () => { ) await expect( - oauthResumeSessionWithRetry(storedAccount, true), + resumeOAuthSessionBundleWithRetry(storedAccount, true), ).rejects.toThrow('The session was deleted by another process') expect(mockRestore).toHaveBeenCalledTimes(1) expect(degradedEvents()).toEqual([]) diff --git a/src/state/session/__tests__/session-test.ts b/src/state/session/__tests__/session-test.ts index dc3630894..bf0984600 100644 --- a/src/state/session/__tests__/session-test.ts +++ b/src/state/session/__tests__/session-test.ts @@ -65,7 +65,7 @@ describe('session', () => { "accounts": [], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -154,7 +154,7 @@ describe('session', () => { ], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -492,7 +492,7 @@ describe('session', () => { ], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -554,7 +554,7 @@ describe('session', () => { ], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -647,7 +647,7 @@ describe('session', () => { "accounts": [], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -827,7 +827,7 @@ describe('session', () => { ], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -1023,16 +1023,19 @@ describe('session', () => { handle: 'alice.test', isOauthSession: true, } - const agent = new AtpAgent({service: account.service}) let state = run(getInitialState([]), [ - {type: 'switched-to-account', newAgent: agent, newAccount: account}, + { + type: 'switched-to-account', + newBundle: makeBundle(account.service), + newAccount: account, + }, ]) state = run(state, [ {type: 'oauth-session-terminated', accountDid: account.did}, ]) - expect(state.currentAgentState.did).toBeUndefined() + expect(state.currentBundleState.did).toBeUndefined() expect(state.accounts).toEqual([account]) expect(state.needsPersist).toBe(true) }) @@ -1050,11 +1053,17 @@ describe('session', () => { handle: 'bob.test', isOauthSession: true, } - const aliceAgent = new AtpAgent({service: alice.service}) - const bobAgent = new AtpAgent({service: bob.service}) const state = run(getInitialState([]), [ - {type: 'switched-to-account', newAgent: aliceAgent, newAccount: alice}, - {type: 'switched-to-account', newAgent: bobAgent, newAccount: bob}, + { + type: 'switched-to-account', + newBundle: makeBundle(alice.service), + newAccount: alice, + }, + { + type: 'switched-to-account', + newBundle: makeBundle(bob.service), + newAccount: bob, + }, ]) const next = reducer(state, { @@ -1063,7 +1072,7 @@ describe('session', () => { }) expect(next).toBe(state) - expect(next.currentAgentState.did).toBe(bob.did) + expect(next.currentBundleState.did).toBe(bob.did) }) it('bails out of update on identical objects', () => { @@ -1569,7 +1578,7 @@ describe('session', () => { ], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, @@ -1726,7 +1735,7 @@ describe('session', () => { ], "currentBundleState": { "bundle": { - "service": "https://public.api.bsky.app/", + "service": "https://api.blacksky.community/", }, "did": undefined, }, diff --git a/src/state/session/__tests__/strip-appview-proxy-test.ts b/src/state/session/__tests__/strip-appview-proxy-test.ts index 4a8fb2f3c..0fd928c14 100644 --- a/src/state/session/__tests__/strip-appview-proxy-test.ts +++ b/src/state/session/__tests__/strip-appview-proxy-test.ts @@ -1,6 +1,6 @@ import {describe, expect, it} from '@jest/globals' -import {stripAppviewProxyForPdsLocalMethods} from '../agent' +import {stripAppviewProxyForPdsLocalMethods} from '../oauth-agent' const PROXY = 'atproto-proxy' const PROXY_VALUE = 'did:web:api.blacksky.community#bsky_appview'