|
| 1 | +jest.mock('shared/supabase/init') |
| 2 | + |
| 3 | +import {getProfileFeed} from 'api/get-profile-feed' |
| 4 | +import {FeedItem, MAX_FEED_BIO_CHARS} from 'common/feed/feed' |
| 5 | +import * as supabaseInit from 'shared/supabase/init' |
| 6 | + |
| 7 | +const row = (overrides: Record<string, any> = {}) => ({ |
| 8 | + username: 'martin', |
| 9 | + name: 'Martin', |
| 10 | + created_time: '2026-08-05T09:54:00.000Z', |
| 11 | + headline: 'Lazy Hacker', |
| 12 | + city: 'Rome', |
| 13 | + country: 'Italy', |
| 14 | + gender: 'male', |
| 15 | + keywords: ['photography', 'poetry'], |
| 16 | + bio_text: 'Describing is always a tedious task.', |
| 17 | + feed_visibility: 'basic', |
| 18 | + ...overrides, |
| 19 | +}) |
| 20 | + |
| 21 | +describe('getProfileFeed', () => { |
| 22 | + let mockPg: any |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + jest.resetAllMocks() |
| 26 | + mockPg = {any: jest.fn().mockResolvedValue([])} |
| 27 | + ;(supabaseInit.createSupabaseDirectClient as jest.Mock).mockReturnValue(mockPg) |
| 28 | + }) |
| 29 | + |
| 30 | + // Handlers are typed as "the response, or a continuation" — this one never continues. |
| 31 | + const call = async (props: any = {}) => |
| 32 | + (await getProfileFeed(props, undefined as any, {} as any)) as {items: FeedItem[]} |
| 33 | + |
| 34 | + describe('the query', () => { |
| 35 | + it('only ever reads public, syndicatable, listable profiles', async () => { |
| 36 | + await call() |
| 37 | + const [query] = mockPg.any.mock.calls[0] |
| 38 | + |
| 39 | + expect(query).toContain("profiles.visibility = 'public'") |
| 40 | + expect(query).toContain("profiles.feed_visibility <> 'none'") |
| 41 | + expect(query).toContain('profiles.disabled != true') |
| 42 | + expect(query).toContain('profiles.looking_for_matches = true') |
| 43 | + expect(query).toContain('not users.is_banned_from_posting') |
| 44 | + expect(query).toContain("users.data ->> 'userDeleted'") |
| 45 | + }) |
| 46 | + |
| 47 | + it('never selects photos, whatever the level', async () => { |
| 48 | + await call() |
| 49 | + const [query] = mockPg.any.mock.calls[0] |
| 50 | + expect(query).not.toContain('photo_urls') |
| 51 | + expect(query).not.toContain('pinned_url') |
| 52 | + }) |
| 53 | + |
| 54 | + it('filters by country case-insensitively, and not at all when none is given', async () => { |
| 55 | + await call({country: 'italy'}) |
| 56 | + expect(mockPg.any.mock.calls[0][0]).toContain('lower(profiles.country) = lower($(country))') |
| 57 | + expect(mockPg.any.mock.calls[0][1].country).toBe('italy') |
| 58 | + |
| 59 | + await call() |
| 60 | + expect(mockPg.any.mock.calls[1][1].country).toBeNull() |
| 61 | + }) |
| 62 | + |
| 63 | + it('parameterises the country instead of concatenating it', async () => { |
| 64 | + await call({country: "Italy'; drop table profiles; --"}) |
| 65 | + const [query, params] = mockPg.any.mock.calls[0] |
| 66 | + expect(query).not.toContain('drop table') |
| 67 | + expect(params.country).toBe("Italy'; drop table profiles; --") |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('the projection', () => { |
| 72 | + it('sends only name, location, headline, keywords and link for a basic member', async () => { |
| 73 | + mockPg.any.mockResolvedValue([row()]) |
| 74 | + const {items} = await call() |
| 75 | + |
| 76 | + expect(items[0]).toEqual({ |
| 77 | + username: 'martin', |
| 78 | + name: 'Martin', |
| 79 | + createdTime: '2026-08-05T09:54:00.000Z', |
| 80 | + headline: 'Lazy Hacker', |
| 81 | + location: 'Rome, Italy', |
| 82 | + keywords: ['photography', 'poetry'], |
| 83 | + }) |
| 84 | + expect(items[0].bioExcerpt).toBeUndefined() |
| 85 | + expect(items[0].gender).toBeUndefined() |
| 86 | + }) |
| 87 | + |
| 88 | + it('omits keywords rather than sending an empty list', async () => { |
| 89 | + mockPg.any.mockResolvedValue([row({keywords: []})]) |
| 90 | + const {items} = await call() |
| 91 | + expect(items[0].keywords).toBeUndefined() |
| 92 | + }) |
| 93 | + |
| 94 | + it('adds gender and a bio excerpt for a full member', async () => { |
| 95 | + mockPg.any.mockResolvedValue([row({feed_visibility: 'full'})]) |
| 96 | + const {items} = await call() |
| 97 | + |
| 98 | + expect(items[0].gender).toBe('male') |
| 99 | + expect(items[0].keywords).toEqual(['photography', 'poetry']) |
| 100 | + expect(items[0].bioExcerpt).toBe('Describing is always a tedious task.') |
| 101 | + }) |
| 102 | + |
| 103 | + it('truncates a long bio rather than republishing the whole thing', async () => { |
| 104 | + mockPg.any.mockResolvedValue([row({feed_visibility: 'full', bio_text: 'word '.repeat(500)})]) |
| 105 | + const {items} = await call() |
| 106 | + |
| 107 | + expect(items[0].bioExcerpt!.length).toBeLessThanOrEqual(MAX_FEED_BIO_CHARS + 1) |
| 108 | + expect(items[0].bioExcerpt!.endsWith('…')).toBe(true) |
| 109 | + }) |
| 110 | + |
| 111 | + it('falls back to the country alone when no city is set', async () => { |
| 112 | + mockPg.any.mockResolvedValue([row({city: null})]) |
| 113 | + const {items} = await call() |
| 114 | + expect(items[0].location).toBe('Italy') |
| 115 | + }) |
| 116 | + |
| 117 | + it('omits location entirely when neither city nor country is set', async () => { |
| 118 | + mockPg.any.mockResolvedValue([row({city: null, country: null})]) |
| 119 | + const {items} = await call() |
| 120 | + expect(items[0].location).toBeUndefined() |
| 121 | + }) |
| 122 | + }) |
| 123 | +}) |
0 commit comments