From ad993764dd037aceb99566ff1e6d4d600e93298d Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Tue, 10 Nov 2026 16:41:57 -0500 Subject: [PATCH] test(merge): migrate component and feed tests to the lex clients --- __tests__/lib/feed-manip-community.test.ts | 2 +- __tests__/lib/space-feed-routing.test.ts | 60 +++++++++--------- .../__tests__/AssemblyEmbed.test.tsx | 13 ++-- .../__tests__/CustomFeed-composer.test.tsx | 3 + src/screens/Onboarding/__tests__/util.test.ts | 62 +++++++++---------- 5 files changed, 70 insertions(+), 70 deletions(-) diff --git a/__tests__/lib/feed-manip-community.test.ts b/__tests__/lib/feed-manip-community.test.ts index 6f034c855..97c53f38c 100644 --- a/__tests__/lib/feed-manip-community.test.ts +++ b/__tests__/lib/feed-manip-community.test.ts @@ -100,7 +100,7 @@ describe('FeedTuner with merged community posts', () => { }, } - const slice = new FeedViewPostsSlice({post}) + const slice = new FeedViewPostsSlice({post}, new Map()) expect(slice.items).toHaveLength(1) expect(slice.isQuotePost).toBe(true) diff --git a/__tests__/lib/space-feed-routing.test.ts b/__tests__/lib/space-feed-routing.test.ts index db0d47966..586020d49 100644 --- a/__tests__/lib/space-feed-routing.test.ts +++ b/__tests__/lib/space-feed-routing.test.ts @@ -1,14 +1,7 @@ -// `jsonToLex` walks the body looking for CID links, and the multiformats CID -// helper it uses does not load under jest's module resolution. Nothing here is -// about CID inflation, so it is stubbed to a passthrough. -jest.mock('@atproto/api', () => ({ - ...jest.requireActual('@atproto/api'), - jsonToLex: (value: unknown) => value, -})) - - import {type Client} from '@atproto/lex' + import {CustomFeedAPI} from '#/lib/api/feed/custom' +import {com} from '#/lexicons' import {toPostView, toSpaceFeedPage} from '#/lib/api/space-views' const FEED = 'at://did:plc:community/app.bsky.feed.generator/private' @@ -24,14 +17,15 @@ const config = { space: SPACE, } -type FakeAgent = { +type FakeClient = { did?: string fetchHandler: jest.Mock - app: {bsky: {feed: {getFeed: jest.Mock}}} - com: {atproto: {repo: {getRecord: jest.Mock}}} + getFeed: jest.Mock + agent: {fetchHandler: jest.Mock} + call: jest.Mock } -function client({did}: {did?: string}): FakeAgent { +function client({did}: {did?: string}): FakeClient { const getFeed = jest.fn() const fetchHandler = jest.fn((path: string) => { if (path.includes('community.blacksky.feed.getSpaceFeed')) { @@ -62,29 +56,31 @@ function client({did}: {did?: string}): FakeAgent { return { did, fetchHandler, - app: {bsky: {feed: {getFeed}}}, - com: { - atproto: { - repo: { - getRecord: jest.fn(() => Promise.resolve({data: {value: config}})), - }, - }, - }, + getFeed, + agent: {fetchHandler}, + call: jest.fn((method: unknown, params: unknown) => + method === com.atproto.repo.getRecord + ? Promise.resolve({value: config}) + : getFeed(params), + ), } } -// The feed APIs take a real `AtpAgent`; only the four members above are -// reachable on this path, so the fake is narrowed rather than filled in. -const asAgent = (a: FakeAgent) => a as unknown as AtpAgent +// The feed APIs take a real `Client`; only the members above are reachable on +// this path, so the fake is narrowed rather than filled in. +const asClient = (a: FakeClient) => a as unknown as Client describe('space-backed feed routing', () => { it('reads a space feed through the private endpoint and never the standard one', async () => { const a = client({did: 'did:plc:alice'}) - const api = new CustomFeedAPI({agent: asAgent(a), feedParams: {feed: FEED}}) + const api = new CustomFeedAPI({ + client: asClient(a), + feedParams: {feed: FEED}, + }) const page = await api.fetch({cursor: undefined, limit: 30}) - expect(a.app.bsky.feed.getFeed).not.toHaveBeenCalled() + expect(a.getFeed).not.toHaveBeenCalled() expect(page.feed).toHaveLength(1) // The real space record URI survives: every later interaction (like, // delete, permalink, thread) targets it. @@ -94,17 +90,23 @@ describe('space-backed feed routing', () => { it('peekLatest uses the same endpoint, so polling agrees with pagination', async () => { const a = client({did: 'did:plc:alice'}) - const api = new CustomFeedAPI({agent: asAgent(a), feedParams: {feed: FEED}}) + const api = new CustomFeedAPI({ + client: asClient(a), + feedParams: {feed: FEED}, + }) await api.peekLatest() - expect(a.app.bsky.feed.getFeed).not.toHaveBeenCalled() + expect(a.getFeed).not.toHaveBeenCalled() expect(a.fetchHandler).toHaveBeenCalled() }) it('refuses logged out rather than falling back to the public appview', async () => { const a = client({}) - const api = new CustomFeedAPI({agent: asAgent(a), feedParams: {feed: FEED}}) + const api = new CustomFeedAPI({ + client: asClient(a), + feedParams: {feed: FEED}, + }) await expect(api.fetch({cursor: undefined, limit: 30})).rejects.toThrow( 'FeedSignedInOnly', diff --git a/src/components/Post/Embed/ExternalEmbed/__tests__/AssemblyEmbed.test.tsx b/src/components/Post/Embed/ExternalEmbed/__tests__/AssemblyEmbed.test.tsx index 387cca03b..1cd6822be 100644 --- a/src/components/Post/Embed/ExternalEmbed/__tests__/AssemblyEmbed.test.tsx +++ b/src/components/Post/Embed/ExternalEmbed/__tests__/AssemblyEmbed.test.tsx @@ -28,17 +28,17 @@ jest.mock('#/view/icons/Logo', () => ({Logo: () => null})) type Viewer = {did: string} | undefined let mockViewer: Viewer const mockCreateRecord = jest.fn() -const mockAgent = { - get session() { - return mockViewer +const mockPdsClient = { + get did() { + return mockViewer?.did }, get assertDid() { return mockViewer?.did }, - com: {atproto: {repo: {createRecord: mockCreateRecord}}}, + call: (_method: unknown, input: unknown) => mockCreateRecord(input), } jest.mock('#/state/session', () => ({ - useAgent: () => mockAgent, + usePdsClient: () => mockPdsClient, useSession: () => ({currentAccount: mockViewer}), })) @@ -92,6 +92,7 @@ jest.mock('#/components/StarterPack/StarterPackCard', () => ({ Embed: () => null, })) jest.mock('#/components/SubtleHover', () => ({SubtleHover: () => null})) +jest.mock('#/components/Post/Embed/AtCard', () => ({AtCard: () => null})) jest.mock('#/components/Post/Embed/StandardSiteEmbed', () => ({ StandardSiteEmbed: () => null, })) @@ -560,7 +561,7 @@ describe('AssemblyEmbed', () => { at_cid: 'bafyreistatement', } const voteUri = `at://${VIEWER}/community.blacksky.assembly.vote/3kvote` - mockCreateRecord.mockResolvedValue({data: {uri: voteUri}}) + mockCreateRecord.mockResolvedValue({uri: voteUri}) serve({ participation: reply({ auth: {token: 'participant-token'}, diff --git a/src/screens/CustomFeed/__tests__/CustomFeed-composer.test.tsx b/src/screens/CustomFeed/__tests__/CustomFeed-composer.test.tsx index b2370e606..fca676cab 100644 --- a/src/screens/CustomFeed/__tests__/CustomFeed-composer.test.tsx +++ b/src/screens/CustomFeed/__tests__/CustomFeed-composer.test.tsx @@ -49,6 +49,9 @@ jest.mock('#/state/session', () => ({ })) jest.mock('#/view/com/posts/PostFeed', () => ({PostFeed: () => null})) jest.mock('#/view/com/util/EmptyState', () => ({EmptyState: () => null})) +jest.mock('#/components/PostFeedSkeleton', () => ({ + PostFeedSkeleton: () => null, +})) jest.mock('#/view/com/util/error/ErrorScreen', () => ({ ErrorScreen: () => null, })) diff --git a/src/screens/Onboarding/__tests__/util.test.ts b/src/screens/Onboarding/__tests__/util.test.ts index 072e08e52..a4ecfb5aa 100644 --- a/src/screens/Onboarding/__tests__/util.test.ts +++ b/src/screens/Onboarding/__tests__/util.test.ts @@ -1,8 +1,9 @@ +import {addLabeler} from '@bsky/sdk' import {describe, expect, it, jest} from '@jest/globals' import {generateComputedConfig} from '#/lib/community/configGenerator' import {type RawCommunityConfig} from '#/lib/community/types' -import {saveLabelers} from '#/state/session/agent-config' +import {saveLabelers} from '#/state/session/moderation' import { resolveFollowDids, resolveModerationServiceDids, @@ -10,7 +11,8 @@ import { subscribeToBrandModerationServices, } from '../util' -jest.mock('#/state/session/agent-config', () => ({ +jest.mock('#/state/session/moderation', () => ({ + readLabelers: jest.fn(() => undefined), saveLabelers: jest.fn(), })) @@ -183,51 +185,43 @@ describe('resolveModerationServiceDids', () => { }) describe('subscribeToBrandModerationServices', () => { + function mockClient(addLabelerImpl: (did: string) => void = () => {}) { + const call = jest.fn((_action: unknown, did: string) => { + addLabelerImpl(did) + return Promise.resolve() + }) + return { + call, + client: {did: 'did:plc:user', call} as unknown as Parameters< + typeof subscribeToBrandModerationServices + >[0], + } + } + it('subscribes to each resolved moderation service and caches labelers', async () => { const brand = makeConfig({}, ['did:plc:mod1', 'did:plc:mod1']) - const agent = { - labelers: [] as string[], - addLabeler: jest.fn((did: string) => { - agent.labelers.push(did) - }), - } + const {client, call} = mockClient() - await subscribeToBrandModerationServices( - agent as unknown as Parameters< - typeof subscribeToBrandModerationServices - >[0], - 'did:plc:user', - brand, - ) + await subscribeToBrandModerationServices(client, brand) - expect(agent.addLabeler).toHaveBeenCalledTimes(1) - expect(agent.addLabeler).toHaveBeenCalledWith('did:plc:mod1') + expect(call).toHaveBeenCalledTimes(1) + expect(call).toHaveBeenCalledWith(addLabeler, 'did:plc:mod1') expect(saveLabelers).toHaveBeenCalledWith('did:plc:user', ['did:plc:mod1']) }) it('keeps onboarding best-effort when a moderation service fails', async () => { const brand = makeConfig({}, ['did:plc:good', 'did:plc:bad']) - const agent = { - labelers: [] as string[], - addLabeler: jest.fn((did: string) => { - if (did === 'did:plc:bad') { - throw new Error('failed') - } - agent.labelers.push(did) - }), - } + const {client, call} = mockClient(did => { + if (did === 'did:plc:bad') { + throw new Error('failed') + } + }) await expect( - subscribeToBrandModerationServices( - agent as unknown as Parameters< - typeof subscribeToBrandModerationServices - >[0], - 'did:plc:user', - brand, - ), + subscribeToBrandModerationServices(client, brand), ).resolves.toBeUndefined() - expect(agent.addLabeler).toHaveBeenCalledTimes(2) + expect(call).toHaveBeenCalledTimes(2) expect(saveLabelers).toHaveBeenCalledWith('did:plc:user', ['did:plc:good']) }) }) -- knotmirror 0.0.1