2023-09-24 01:49:47 +09:00
|
|
|
import MisskeyEntity from '@/misskey/entity'
|
|
|
|
import MisskeyNotificationType from '@/misskey/notification'
|
|
|
|
import Misskey from '@/misskey'
|
|
|
|
import MegalodonNotificationType from '@/notification'
|
2023-09-25 08:49:57 +09:00
|
|
|
import axios, { AxiosHeaders, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
2023-09-24 01:49:47 +09:00
|
|
|
|
|
|
|
jest.mock('axios')
|
|
|
|
|
|
|
|
const user: MisskeyEntity.User = {
|
|
|
|
id: '1',
|
|
|
|
name: 'test_user',
|
|
|
|
username: 'TestUser',
|
|
|
|
host: 'misskey.io',
|
|
|
|
avatarUrl: 'https://example.com/icon.png',
|
|
|
|
avatarColor: '#000000',
|
|
|
|
emojis: []
|
|
|
|
}
|
|
|
|
|
|
|
|
const note: MisskeyEntity.Note = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: '1',
|
|
|
|
user: user,
|
|
|
|
text: 'hogehoge',
|
|
|
|
cw: null,
|
|
|
|
visibility: 'public',
|
|
|
|
renoteCount: 0,
|
|
|
|
repliesCount: 0,
|
|
|
|
reactions: {},
|
2023-09-25 08:49:57 +09:00
|
|
|
reactionEmojis: {},
|
2023-09-24 01:49:47 +09:00
|
|
|
emojis: [],
|
|
|
|
fileIds: [],
|
|
|
|
files: [],
|
|
|
|
replyId: null,
|
|
|
|
renoteId: null
|
|
|
|
}
|
|
|
|
|
|
|
|
const follow: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.Follow
|
|
|
|
}
|
|
|
|
|
|
|
|
const mention: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.Mention,
|
|
|
|
note: note
|
|
|
|
}
|
|
|
|
|
|
|
|
const reply: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.Reply,
|
|
|
|
note: note
|
|
|
|
}
|
|
|
|
|
|
|
|
const renote: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.Renote,
|
|
|
|
note: note
|
|
|
|
}
|
|
|
|
|
|
|
|
const quote: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.Quote,
|
|
|
|
note: note
|
|
|
|
}
|
|
|
|
|
|
|
|
const reaction: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.Reaction,
|
|
|
|
note: note,
|
|
|
|
reaction: '♥'
|
|
|
|
}
|
|
|
|
|
|
|
|
const pollVote: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
2023-09-25 08:49:57 +09:00
|
|
|
type: MisskeyNotificationType.PollVote,
|
2023-09-24 01:49:47 +09:00
|
|
|
note: note
|
|
|
|
}
|
|
|
|
|
|
|
|
const receiveFollowRequest: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.ReceiveFollowRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
const followRequestAccepted: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.FollowRequestAccepted
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupInvited: MisskeyEntity.Notification = {
|
|
|
|
id: '1',
|
|
|
|
createdAt: '2021-02-01T01:49:29',
|
|
|
|
userId: user.id,
|
|
|
|
user: user,
|
|
|
|
type: MisskeyNotificationType.GroupInvited
|
|
|
|
}
|
|
|
|
|
|
|
|
;(axios.CancelToken.source as any).mockImplementation(() => {
|
|
|
|
return {
|
|
|
|
token: {
|
|
|
|
throwIfRequested: () => {},
|
|
|
|
promise: {
|
|
|
|
then: () => {},
|
|
|
|
catch: () => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getNotifications', () => {
|
|
|
|
const client = new Misskey('http://localhost', 'sample token')
|
|
|
|
const cases: Array<{ event: MisskeyEntity.Notification; expected: Entity.NotificationType; title: string }> = [
|
|
|
|
{
|
|
|
|
event: follow,
|
|
|
|
expected: MegalodonNotificationType.Follow,
|
|
|
|
title: 'follow'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: mention,
|
|
|
|
expected: MegalodonNotificationType.Mention,
|
|
|
|
title: 'mention'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: reply,
|
|
|
|
expected: MegalodonNotificationType.Mention,
|
|
|
|
title: 'reply'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: renote,
|
|
|
|
expected: MegalodonNotificationType.Reblog,
|
|
|
|
title: 'renote'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: quote,
|
|
|
|
expected: MegalodonNotificationType.Reblog,
|
|
|
|
title: 'quote'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: reaction,
|
2023-09-25 08:49:57 +09:00
|
|
|
expected: MegalodonNotificationType.EmojiReaction,
|
2023-09-24 01:49:47 +09:00
|
|
|
title: 'reaction'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: pollVote,
|
2023-09-25 08:49:57 +09:00
|
|
|
expected: MegalodonNotificationType.PollVote,
|
2023-09-24 01:49:47 +09:00
|
|
|
title: 'pollVote'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: receiveFollowRequest,
|
|
|
|
expected: MegalodonNotificationType.FollowRequest,
|
|
|
|
title: 'receiveFollowRequest'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: followRequestAccepted,
|
|
|
|
expected: MegalodonNotificationType.Follow,
|
|
|
|
title: 'followRequestAccepted'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
cases.forEach(c => {
|
|
|
|
it(`should be ${c.title} event`, async () => {
|
2023-09-25 08:49:57 +09:00
|
|
|
const config: InternalAxiosRequestConfig<any> = {
|
|
|
|
headers: new AxiosHeaders()
|
|
|
|
}
|
2023-09-24 01:49:47 +09:00
|
|
|
const mockResponse: AxiosResponse<Array<MisskeyEntity.Notification>> = {
|
|
|
|
data: [c.event],
|
|
|
|
status: 200,
|
|
|
|
statusText: '200OK',
|
|
|
|
headers: {},
|
2023-09-25 08:49:57 +09:00
|
|
|
config: config
|
2023-09-24 01:49:47 +09:00
|
|
|
}
|
|
|
|
;(axios.post as any).mockResolvedValue(mockResponse)
|
|
|
|
const res = await client.getNotifications()
|
|
|
|
expect(res.data[0].type).toEqual(c.expected)
|
|
|
|
})
|
|
|
|
})
|
2023-09-25 08:49:57 +09:00
|
|
|
it('groupInvited event should be ignored', async () => {
|
|
|
|
const config: InternalAxiosRequestConfig<any> = {
|
|
|
|
headers: new AxiosHeaders()
|
|
|
|
}
|
|
|
|
const mockResponse: AxiosResponse<Array<MisskeyEntity.Notification>> = {
|
|
|
|
data: [groupInvited],
|
|
|
|
status: 200,
|
|
|
|
statusText: '200OK',
|
|
|
|
headers: {},
|
|
|
|
config: config
|
|
|
|
}
|
|
|
|
;(axios.post as any).mockResolvedValue(mockResponse)
|
|
|
|
const res = await client.getNotifications()
|
|
|
|
expect(res.data).toEqual([])
|
|
|
|
})
|
2023-09-24 01:49:47 +09:00
|
|
|
})
|