Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Jul 1, 2024
1 parent 9f64375 commit 8dc4e01
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/api/src/bsky-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ export class BskyAgent extends AtpAgent {
await updateHiddenPost(this, postUri, 'unhide')
}

async bskyAppDismissNudge(nudge: string) {
async bskyAppDismissNudge(nudge: string, isDismissed = true) {
await updatePreferences(this, (prefs: AppBskyActorDefs.Preferences) => {
let bskyAppStatePref = prefs.findLast(
(pref) =>
Expand All @@ -1053,11 +1053,20 @@ export class BskyAgent extends AtpAgent {
)

bskyAppStatePref = bskyAppStatePref || {}
if (!Array.isArray(bskyAppStatePref.dismissedNudges)) {
bskyAppStatePref.dismissedNudges = [nudge]
if (isDismissed) {
if (!Array.isArray(bskyAppStatePref.dismissedNudges)) {
bskyAppStatePref.dismissedNudges = [nudge]
} else {
if (!bskyAppStatePref.dismissedNudges.includes(nudge)) {
bskyAppStatePref.dismissedNudges.push(nudge)
}
}
} else {
if (!bskyAppStatePref.dismissedNudges.includes(nudge)) {
bskyAppStatePref.dismissedNudges.push(nudge)
if (!Array.isArray(bskyAppStatePref.dismissedNudges)) {
bskyAppStatePref.dismissedNudges = []
} else {
bskyAppStatePref.dismissedNudges =
bskyAppStatePref.dismissedNudges.filter((v) => v !== nudge)
}
}

Expand Down
135 changes: 135 additions & 0 deletions packages/api/tests/bsky-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setAdultContentEnabled(true)
Expand Down Expand Up @@ -313,6 +316,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setAdultContentEnabled(false)
Expand Down Expand Up @@ -350,6 +356,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setContentLabelPref('misinfo', 'hide')
Expand Down Expand Up @@ -387,6 +396,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setContentLabelPref('spam', 'ignore')
Expand Down Expand Up @@ -428,6 +440,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.addSavedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -472,6 +487,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.addPinnedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -516,6 +534,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.removePinnedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -560,6 +581,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.removeSavedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -604,6 +628,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.addPinnedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -648,6 +675,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.addPinnedFeed('at://bob.com/app.bsky.feed.generator/fake2')
Expand Down Expand Up @@ -698,6 +728,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.removeSavedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -742,6 +775,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setPersonalDetails({ birthDate: '2023-09-11T18:05:42.556Z' })
Expand Down Expand Up @@ -786,6 +822,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setFeedViewPrefs('home', { hideReplies: true })
Expand Down Expand Up @@ -830,6 +869,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setFeedViewPrefs('home', { hideReplies: false })
Expand Down Expand Up @@ -874,6 +916,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setFeedViewPrefs('other', { hideReplies: true })
Expand Down Expand Up @@ -925,6 +970,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setThreadViewPrefs({ sort: 'random' })
Expand Down Expand Up @@ -976,6 +1024,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setThreadViewPrefs({ sort: 'oldest' })
Expand Down Expand Up @@ -1027,6 +1078,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: [],
},
})

await agent.setInterestsPref({ tags: ['foo', 'bar'] })
Expand Down Expand Up @@ -1078,6 +1132,9 @@ describe('agent', () => {
interests: {
tags: ['foo', 'bar'],
},
bskyAppState: {
dismissedNudges: [],
},
})
})

Expand Down Expand Up @@ -1195,6 +1252,14 @@ describe('agent', () => {
sort: 'newest',
prioritizeFollowedUsers: false,
},
{
$type: 'app.bsky.actor.defs#bskyAppStatePref',
dismissedNudges: ['one'],
},
{
$type: 'app.bsky.actor.defs#bskyAppStatePref',
dismissedNudges: ['two'],
},
],
})
await expect(agent.getPreferences()).resolves.toStrictEqual({
Expand Down Expand Up @@ -1247,6 +1312,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two'],
},
})

await agent.setAdultContentEnabled(false)
Expand Down Expand Up @@ -1300,6 +1368,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two'],
},
})

await agent.setContentLabelPref('porn', 'ignore')
Expand Down Expand Up @@ -1354,6 +1425,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two'],
},
})

await agent.removeLabeler('did:plc:other')
Expand Down Expand Up @@ -1404,6 +1478,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two'],
},
})

await agent.addPinnedFeed('at://bob.com/app.bsky.feed.generator/fake')
Expand Down Expand Up @@ -1454,6 +1531,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two'],
},
})

await agent.setPersonalDetails({ birthDate: '2023-09-11T18:05:42.556Z' })
Expand Down Expand Up @@ -1504,6 +1584,9 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two'],
},
})

await agent.setFeedViewPrefs('home', {
Expand All @@ -1518,6 +1601,7 @@ describe('agent', () => {
prioritizeFollowedUsers: true,
})
await agent.setPersonalDetails({ birthDate: '2023-09-11T18:05:42.556Z' })
await agent.bskyAppDismissNudge('three')
await expect(agent.getPreferences()).resolves.toStrictEqual({
feeds: {
pinned: ['at://bob.com/app.bsky.feed.generator/fake'],
Expand Down Expand Up @@ -1565,11 +1649,18 @@ describe('agent', () => {
interests: {
tags: [],
},
bskyAppState: {
dismissedNudges: ['two', 'three'],
},
})

const res = await agent.app.bsky.actor.getPreferences()
expect(res.data.preferences.sort(byType)).toStrictEqual(
[
{
$type: 'app.bsky.actor.defs#bskyAppStatePref',
dismissedNudges: ['two', 'three'],
},
{
$type: 'app.bsky.actor.defs#adultContentPref',
enabled: false,
Expand Down Expand Up @@ -2693,6 +2784,50 @@ describe('agent', () => {
})
})

describe('dismissed nudges', () => {
let agent: BskyAgent

beforeAll(async () => {
agent = new BskyAgent({ service: network.pds.url })
await agent.createAccount({
handle: 'user11.test',
email: '[email protected]',
password: 'password',
})
})

it('dismissNudge (true)', async () => {
await agent.bskyAppDismissNudge('first')
await expect(agent.getPreferences()).resolves.toHaveProperty(
'bskyAppState.dismissedNudges',
['first'],
)
await agent.bskyAppDismissNudge('first')
await expect(agent.getPreferences()).resolves.toHaveProperty(
'bskyAppState.dismissedNudges',
['first'],
)
await agent.bskyAppDismissNudge('second')
await expect(agent.getPreferences()).resolves.toHaveProperty(
'bskyAppState.dismissedNudges',
['first', 'second'],
)
})

it('dismissNudge (false)', async () => {
await agent.bskyAppDismissNudge('first', false)
await expect(agent.getPreferences()).resolves.toHaveProperty(
'bskyAppState.dismissedNudges',
['second'],
)
await agent.bskyAppDismissNudge('first', false)
await expect(agent.getPreferences()).resolves.toHaveProperty(
'bskyAppState.dismissedNudges',
['second'],
)
})
})

// end
})
})
Expand Down
Loading

0 comments on commit 8dc4e01

Please sign in to comment.