From 788b9797345aa588c6891b9c2f3692f666712261 Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Fri, 1 Sep 2023 16:47:20 -0700 Subject: [PATCH] chore: format specs --- .../test/integration/megalodon.spec.ts | 48 +- .../test/integration/misskey.spec.ts | 359 +++++++------- .../test/unit/misskey/api_client.spec.ts | 466 +++++++++--------- packages/megalodon/test/unit/parser.spec.ts | 276 ++++++----- 4 files changed, 584 insertions(+), 565 deletions(-) diff --git a/packages/megalodon/test/integration/megalodon.spec.ts b/packages/megalodon/test/integration/megalodon.spec.ts index 8964535509..9f7524211c 100644 --- a/packages/megalodon/test/integration/megalodon.spec.ts +++ b/packages/megalodon/test/integration/megalodon.spec.ts @@ -1,27 +1,27 @@ -import { detector } from '../../src/index' +import { detector } from "../../src/index"; -describe('detector', () => { - describe('mastodon', () => { - const url = 'https://fedibird.com' - it('should be mastodon', async () => { - const mastodon = await detector(url) - expect(mastodon).toEqual('mastodon') - }) - }) +describe("detector", () => { + describe("mastodon", () => { + const url = "https://fedibird.com"; + it("should be mastodon", async () => { + const mastodon = await detector(url); + expect(mastodon).toEqual("mastodon"); + }); + }); - describe('pleroma', () => { - const url = 'https://pleroma.soykaf.com' - it('should be pleroma', async () => { - const pleroma = await detector(url) - expect(pleroma).toEqual('pleroma') - }) - }) + describe("pleroma", () => { + const url = "https://pleroma.soykaf.com"; + it("should be pleroma", async () => { + const pleroma = await detector(url); + expect(pleroma).toEqual("pleroma"); + }); + }); - describe('misskey', () => { - const url = 'https://misskey.io' - it('should be misskey', async () => { - const misskey = await detector(url) - expect(misskey).toEqual('misskey') - }) - }) -}) + describe("misskey", () => { + const url = "https://misskey.io"; + it("should be misskey", async () => { + const misskey = await detector(url); + expect(misskey).toEqual("misskey"); + }); + }); +}); diff --git a/packages/megalodon/test/integration/misskey.spec.ts b/packages/megalodon/test/integration/misskey.spec.ts index 0ec1288428..fecde00b57 100644 --- a/packages/megalodon/test/integration/misskey.spec.ts +++ b/packages/megalodon/test/integration/misskey.spec.ts @@ -1,204 +1,207 @@ -import MisskeyEntity from '@/misskey/entity' -import MisskeyNotificationType from '@/misskey/notification' -import Misskey from '@/misskey' -import MegalodonNotificationType from '@/notification' -import axios, { AxiosResponse } from 'axios' +import MisskeyEntity from "@/misskey/entity"; +import MisskeyNotificationType from "@/misskey/notification"; +import Misskey from "@/misskey"; +import MegalodonNotificationType from "@/notification"; +import axios, { AxiosResponse } from "axios"; -jest.mock('axios') +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: [] -} + 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: {}, - emojis: [], - fileIds: [], - files: [], - replyId: null, - renoteId: null -} + id: "1", + createdAt: "2021-02-01T01:49:29", + userId: "1", + user: user, + text: "hogehoge", + cw: null, + visibility: "public", + renoteCount: 0, + repliesCount: 0, + reactions: {}, + 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 -} + 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 -} + 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 -} + 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 -} + 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 -} + 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: '♥' -} + 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, - type: MisskeyNotificationType.PollEnded, - note: note -} + id: "1", + createdAt: "2021-02-01T01:49:29", + userId: user.id, + user: user, + type: MisskeyNotificationType.PollEnded, + note: note, +}; const receiveFollowRequest: MisskeyEntity.Notification = { - id: '1', - createdAt: '2021-02-01T01:49:29', - userId: user.id, - user: user, - type: MisskeyNotificationType.ReceiveFollowRequest -} + 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 -} + 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 -} + 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: () => {}, + }, + }, + }; +}); -;(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, - expected: MegalodonNotificationType.Reaction, - title: 'reaction' - }, - { - event: pollVote, - expected: MegalodonNotificationType.Poll, - title: 'pollVote' - }, - { - event: receiveFollowRequest, - expected: MegalodonNotificationType.FollowRequest, - title: 'receiveFollowRequest' - }, - { - event: followRequestAccepted, - expected: MegalodonNotificationType.Follow, - title: 'followRequestAccepted' - }, - { - event: groupInvited, - expected: MisskeyNotificationType.GroupInvited, - title: 'groupInvited' - } - ] - cases.forEach(c => { - it(`should be ${c.title} event`, async () => { - const mockResponse: AxiosResponse> = { - data: [c.event], - status: 200, - statusText: '200OK', - headers: {}, - config: {} - } - ;(axios.post as any).mockResolvedValue(mockResponse) - const res = await client.getNotifications() - expect(res.data[0].type).toEqual(c.expected) - }) - }) -}) +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, + expected: MegalodonNotificationType.Reaction, + title: "reaction", + }, + { + event: pollVote, + expected: MegalodonNotificationType.Poll, + title: "pollVote", + }, + { + event: receiveFollowRequest, + expected: MegalodonNotificationType.FollowRequest, + title: "receiveFollowRequest", + }, + { + event: followRequestAccepted, + expected: MegalodonNotificationType.Follow, + title: "followRequestAccepted", + }, + { + event: groupInvited, + expected: MisskeyNotificationType.GroupInvited, + title: "groupInvited", + }, + ]; + cases.forEach((c) => { + it(`should be ${c.title} event`, async () => { + const mockResponse: AxiosResponse> = { + data: [c.event], + status: 200, + statusText: "200OK", + headers: {}, + config: {}, + }; + (axios.post as any).mockResolvedValue(mockResponse); + const res = await client.getNotifications(); + expect(res.data[0].type).toEqual(c.expected); + }); + }); +}); diff --git a/packages/megalodon/test/unit/misskey/api_client.spec.ts b/packages/megalodon/test/unit/misskey/api_client.spec.ts index 7cf33b983d..4c5f279c82 100644 --- a/packages/megalodon/test/unit/misskey/api_client.spec.ts +++ b/packages/megalodon/test/unit/misskey/api_client.spec.ts @@ -1,233 +1,245 @@ -import MisskeyAPI from '@/misskey/api_client' -import MegalodonEntity from '@/entity' -import MisskeyEntity from '@/misskey/entity' -import MegalodonNotificationType from '@/notification' -import MisskeyNotificationType from '@/misskey/notification' +import MisskeyAPI from "@/misskey/api_client"; +import MegalodonEntity from "@/entity"; +import MisskeyEntity from "@/misskey/entity"; +import MegalodonNotificationType from "@/notification"; +import MisskeyNotificationType from "@/misskey/notification"; const user: MisskeyEntity.User = { - id: '1', - name: 'test_user', - username: 'TestUser', - host: 'misskey.io', - avatarUrl: 'https://example.com/icon.png', - avatarColor: '#000000', - emojis: [] -} + id: "1", + name: "test_user", + username: "TestUser", + host: "misskey.io", + avatarUrl: "https://example.com/icon.png", + avatarColor: "#000000", + emojis: [], +}; -const converter: MisskeyAPI.Converter = new MisskeyAPI.Converter("https://example.com") +const converter: MisskeyAPI.Converter = new MisskeyAPI.Converter( + "https://example.com", +); -describe('api_client', () => { - describe('notification', () => { - describe('encode', () => { - it('megalodon notification type should be encoded to misskey notification type', () => { - const cases: Array<{ src: MegalodonEntity.NotificationType; dist: MisskeyEntity.NotificationType }> = [ - { - src: MegalodonNotificationType.Follow, - dist: MisskeyNotificationType.Follow - }, - { - src: MegalodonNotificationType.Mention, - dist: MisskeyNotificationType.Reply - }, - { - src: MegalodonNotificationType.Favourite, - dist: MisskeyNotificationType.Reaction - }, - { - src: MegalodonNotificationType.Reaction, - dist: MisskeyNotificationType.Reaction - }, - { - src: MegalodonNotificationType.Reblog, - dist: MisskeyNotificationType.Renote - }, - { - src: MegalodonNotificationType.Poll, - dist: MisskeyNotificationType.PollEnded - }, - { - src: MegalodonNotificationType.FollowRequest, - dist: MisskeyNotificationType.ReceiveFollowRequest - } - ] - cases.forEach(c => { - expect(converter.encodeNotificationType(c.src)).toEqual(c.dist) - }) - }) - }) - describe('decode', () => { - it('misskey notification type should be decoded to megalodon notification type', () => { - const cases: Array<{ src: MisskeyEntity.NotificationType; dist: MegalodonEntity.NotificationType }> = [ - { - src: MisskeyNotificationType.Follow, - dist: MegalodonNotificationType.Follow - }, - { - src: MisskeyNotificationType.Mention, - dist: MegalodonNotificationType.Mention - }, - { - src: MisskeyNotificationType.Reply, - dist: MegalodonNotificationType.Mention - }, - { - src: MisskeyNotificationType.Renote, - dist: MegalodonNotificationType.Reblog - }, - { - src: MisskeyNotificationType.Quote, - dist: MegalodonNotificationType.Reblog - }, - { - src: MisskeyNotificationType.Reaction, - dist: MegalodonNotificationType.Reaction - }, - { - src: MisskeyNotificationType.PollEnded, - dist: MegalodonNotificationType.Poll - }, - { - src: MisskeyNotificationType.ReceiveFollowRequest, - dist: MegalodonNotificationType.FollowRequest - }, - { - src: MisskeyNotificationType.FollowRequestAccepted, - dist: MegalodonNotificationType.Follow - } - ] - cases.forEach(c => { - expect(converter.decodeNotificationType(c.src)).toEqual(c.dist) - }) - }) - }) - }) - describe('reactions', () => { - it('should be mapped', () => { - const misskeyReactions = [ - { - id: '1', - createdAt: '2020-04-21T13:04:13.968Z', - user: { - id: '81u70uwsja', - name: 'h3poteto', - username: 'h3poteto', - host: null, - avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', - avatarColor: 'rgb(146,189,195)', - emojis: [] - }, - type: '❤' - }, - { - id: '2', - createdAt: '2020-04-21T13:04:13.968Z', - user: { - id: '81u70uwsja', - name: 'h3poteto', - username: 'h3poteto', - host: null, - avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', - avatarColor: 'rgb(146,189,195)', - emojis: [] - }, - type: '❤' - }, - { - id: '3', - createdAt: '2020-04-21T13:04:13.968Z', - user: { - id: '81u70uwsja', - name: 'h3poteto', - username: 'h3poteto', - host: null, - avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', - avatarColor: 'rgb(146,189,195)', - emojis: [] - }, - type: '☺' - }, - { - id: '4', - createdAt: '2020-04-21T13:04:13.968Z', - user: { - id: '81u70uwsja', - name: 'h3poteto', - username: 'h3poteto', - host: null, - avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png', - avatarColor: 'rgb(146,189,195)', - emojis: [] - }, - type: '❤' - } - ] +describe("api_client", () => { + describe("notification", () => { + describe("encode", () => { + it("megalodon notification type should be encoded to misskey notification type", () => { + const cases: Array<{ + src: MegalodonEntity.NotificationType; + dist: MisskeyEntity.NotificationType; + }> = [ + { + src: MegalodonNotificationType.Follow, + dist: MisskeyNotificationType.Follow, + }, + { + src: MegalodonNotificationType.Mention, + dist: MisskeyNotificationType.Reply, + }, + { + src: MegalodonNotificationType.Favourite, + dist: MisskeyNotificationType.Reaction, + }, + { + src: MegalodonNotificationType.Reaction, + dist: MisskeyNotificationType.Reaction, + }, + { + src: MegalodonNotificationType.Reblog, + dist: MisskeyNotificationType.Renote, + }, + { + src: MegalodonNotificationType.Poll, + dist: MisskeyNotificationType.PollEnded, + }, + { + src: MegalodonNotificationType.FollowRequest, + dist: MisskeyNotificationType.ReceiveFollowRequest, + }, + ]; + cases.forEach((c) => { + expect(converter.encodeNotificationType(c.src)).toEqual(c.dist); + }); + }); + }); + describe("decode", () => { + it("misskey notification type should be decoded to megalodon notification type", () => { + const cases: Array<{ + src: MisskeyEntity.NotificationType; + dist: MegalodonEntity.NotificationType; + }> = [ + { + src: MisskeyNotificationType.Follow, + dist: MegalodonNotificationType.Follow, + }, + { + src: MisskeyNotificationType.Mention, + dist: MegalodonNotificationType.Mention, + }, + { + src: MisskeyNotificationType.Reply, + dist: MegalodonNotificationType.Mention, + }, + { + src: MisskeyNotificationType.Renote, + dist: MegalodonNotificationType.Reblog, + }, + { + src: MisskeyNotificationType.Quote, + dist: MegalodonNotificationType.Reblog, + }, + { + src: MisskeyNotificationType.Reaction, + dist: MegalodonNotificationType.Reaction, + }, + { + src: MisskeyNotificationType.PollEnded, + dist: MegalodonNotificationType.Poll, + }, + { + src: MisskeyNotificationType.ReceiveFollowRequest, + dist: MegalodonNotificationType.FollowRequest, + }, + { + src: MisskeyNotificationType.FollowRequestAccepted, + dist: MegalodonNotificationType.Follow, + }, + ]; + cases.forEach((c) => { + expect(converter.decodeNotificationType(c.src)).toEqual(c.dist); + }); + }); + }); + }); + describe("reactions", () => { + it("should be mapped", () => { + const misskeyReactions = [ + { + id: "1", + createdAt: "2020-04-21T13:04:13.968Z", + user: { + id: "81u70uwsja", + name: "h3poteto", + username: "h3poteto", + host: null, + avatarUrl: + "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png", + avatarColor: "rgb(146,189,195)", + emojis: [], + }, + type: "❤", + }, + { + id: "2", + createdAt: "2020-04-21T13:04:13.968Z", + user: { + id: "81u70uwsja", + name: "h3poteto", + username: "h3poteto", + host: null, + avatarUrl: + "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png", + avatarColor: "rgb(146,189,195)", + emojis: [], + }, + type: "❤", + }, + { + id: "3", + createdAt: "2020-04-21T13:04:13.968Z", + user: { + id: "81u70uwsja", + name: "h3poteto", + username: "h3poteto", + host: null, + avatarUrl: + "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png", + avatarColor: "rgb(146,189,195)", + emojis: [], + }, + type: "☺", + }, + { + id: "4", + createdAt: "2020-04-21T13:04:13.968Z", + user: { + id: "81u70uwsja", + name: "h3poteto", + username: "h3poteto", + host: null, + avatarUrl: + "https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png", + avatarColor: "rgb(146,189,195)", + emojis: [], + }, + type: "❤", + }, + ]; - const reactions = converter.reactions(misskeyReactions) - expect(reactions).toEqual([ - { - count: 3, - me: false, - name: '❤' - }, - { - count: 1, - me: false, - name: '☺' - } - ]) - }) - }) + const reactions = converter.reactions(misskeyReactions); + expect(reactions).toEqual([ + { + count: 3, + me: false, + name: "❤", + }, + { + count: 1, + me: false, + name: "☺", + }, + ]); + }); + }); - describe('status', () => { - describe('plain content', () => { - it('should be exported plain content and html content', () => { - const plainContent = 'hoge\nfuga\nfuga' - const content = 'hoge
fuga
fuga' - const note: MisskeyEntity.Note = { - id: '1', - createdAt: '2021-02-01T01:49:29', - userId: '1', - user: user, - text: plainContent, - cw: null, - visibility: 'public', - renoteCount: 0, - repliesCount: 0, - reactions: {}, - emojis: [], - fileIds: [], - files: [], - replyId: null, - renoteId: null - } - const megalodonStatus = converter.note(note, user.host || 'misskey.io') - expect(megalodonStatus.plain_content).toEqual(plainContent) - expect(megalodonStatus.content).toEqual(content) - }) - it('html tags should be escaped', () => { - const plainContent = '

hoge\nfuga\nfuga

' - const content = '<p>hoge
fuga
fuga<p>' - const note: MisskeyEntity.Note = { - id: '1', - createdAt: '2021-02-01T01:49:29', - userId: '1', - user: user, - text: plainContent, - cw: null, - visibility: 'public', - renoteCount: 0, - repliesCount: 0, - reactions: {}, - emojis: [], - fileIds: [], - files: [], - replyId: null, - renoteId: null - } - const megalodonStatus = converter.note(note, user.host || 'misskey.io') - expect(megalodonStatus.plain_content).toEqual(plainContent) - expect(megalodonStatus.content).toEqual(content) - }) - }) - }) -}) + describe("status", () => { + describe("plain content", () => { + it("should be exported plain content and html content", () => { + const plainContent = "hoge\nfuga\nfuga"; + const content = "hoge
fuga
fuga"; + const note: MisskeyEntity.Note = { + id: "1", + createdAt: "2021-02-01T01:49:29", + userId: "1", + user: user, + text: plainContent, + cw: null, + visibility: "public", + renoteCount: 0, + repliesCount: 0, + reactions: {}, + emojis: [], + fileIds: [], + files: [], + replyId: null, + renoteId: null, + }; + const megalodonStatus = converter.note(note, user.host || "misskey.io"); + expect(megalodonStatus.plain_content).toEqual(plainContent); + expect(megalodonStatus.content).toEqual(content); + }); + it("html tags should be escaped", () => { + const plainContent = "

hoge\nfuga\nfuga

"; + const content = "<p>hoge
fuga
fuga<p>"; + const note: MisskeyEntity.Note = { + id: "1", + createdAt: "2021-02-01T01:49:29", + userId: "1", + user: user, + text: plainContent, + cw: null, + visibility: "public", + renoteCount: 0, + repliesCount: 0, + reactions: {}, + emojis: [], + fileIds: [], + files: [], + replyId: null, + renoteId: null, + }; + const megalodonStatus = converter.note(note, user.host || "misskey.io"); + expect(megalodonStatus.plain_content).toEqual(plainContent); + expect(megalodonStatus.content).toEqual(content); + }); + }); + }); +}); diff --git a/packages/megalodon/test/unit/parser.spec.ts b/packages/megalodon/test/unit/parser.spec.ts index 5174a647c6..f551b8c6ff 100644 --- a/packages/megalodon/test/unit/parser.spec.ts +++ b/packages/megalodon/test/unit/parser.spec.ts @@ -1,152 +1,156 @@ -import { Parser } from '@/parser' -import Entity from '@/entity' +import { Parser } from "@/parser"; +import Entity from "@/entity"; const account: Entity.Account = { - id: '1', - username: 'h3poteto', - acct: 'h3poteto@pleroma.io', - display_name: 'h3poteto', - locked: false, - created_at: '2019-03-26T21:30:32', - followers_count: 10, - following_count: 10, - statuses_count: 100, - note: 'engineer', - url: 'https://pleroma.io', - avatar: '', - avatar_static: '', - header: '', - header_static: '', - emojis: [], - moved: null, - fields: [], - bot: false -} + id: "1", + username: "h3poteto", + acct: "h3poteto@pleroma.io", + display_name: "h3poteto", + locked: false, + created_at: "2019-03-26T21:30:32", + followers_count: 10, + following_count: 10, + statuses_count: 100, + note: "engineer", + url: "https://pleroma.io", + avatar: "", + avatar_static: "", + header: "", + header_static: "", + emojis: [], + moved: null, + fields: [], + bot: false, +}; const status: Entity.Status = { - id: '1', - uri: 'http://example.com', - url: 'http://example.com', - account: account, - in_reply_to_id: null, - in_reply_to_account_id: null, - reblog: null, - content: 'hoge', - plain_content: 'hoge', - created_at: '2019-03-26T21:40:32', - emojis: [], - replies_count: 0, - reblogs_count: 0, - favourites_count: 0, - reblogged: null, - favourited: null, - muted: null, - sensitive: false, - spoiler_text: '', - visibility: 'public', - media_attachments: [], - mentions: [], - tags: [], - card: null, - poll: null, - application: { - name: 'Web' - } as Entity.Application, - language: null, - pinned: null, - reactions: [], - bookmarked: false, - quote: null -} + id: "1", + uri: "http://example.com", + url: "http://example.com", + account: account, + in_reply_to_id: null, + in_reply_to_account_id: null, + reblog: null, + content: "hoge", + plain_content: "hoge", + created_at: "2019-03-26T21:40:32", + emojis: [], + replies_count: 0, + reblogs_count: 0, + favourites_count: 0, + reblogged: null, + favourited: null, + muted: null, + sensitive: false, + spoiler_text: "", + visibility: "public", + media_attachments: [], + mentions: [], + tags: [], + card: null, + poll: null, + application: { + name: "Web", + } as Entity.Application, + language: null, + pinned: null, + reactions: [], + bookmarked: false, + quote: null, +}; const notification: Entity.Notification = { - id: '1', - account: account, - status: status, - type: 'favourite', - created_at: '2019-04-01T17:01:32' -} + id: "1", + account: account, + status: status, + type: "favourite", + created_at: "2019-04-01T17:01:32", +}; const conversation: Entity.Conversation = { - id: '1', - accounts: [account], - last_status: status, - unread: true -} + id: "1", + accounts: [account], + last_status: status, + unread: true, +}; -describe('Parser', () => { - let parser: Parser +describe("Parser", () => { + let parser: Parser; - beforeEach(() => { - parser = new Parser() - }) + beforeEach(() => { + parser = new Parser(); + }); - describe('parse', () => { - describe('message is heartbeat', () => { - const message: string = ':thump\n' - it('should be called', () => { - const spy = jest.fn() - parser.on('heartbeat', spy) - parser.parse(message) - expect(spy).toHaveBeenLastCalledWith({}) - }) - }) + describe("parse", () => { + describe("message is heartbeat", () => { + const message: string = ":thump\n"; + it("should be called", () => { + const spy = jest.fn(); + parser.on("heartbeat", spy); + parser.parse(message); + expect(spy).toHaveBeenLastCalledWith({}); + }); + }); - describe('message is not json', () => { - describe('event is delete', () => { - const message = `event: delete\ndata: 12asdf34\n\n` - it('should be called', () => { - const spy = jest.fn() - parser.once('delete', spy) - parser.parse(message) - expect(spy).toHaveBeenCalledWith('12asdf34') - }) - }) + describe("message is not json", () => { + describe("event is delete", () => { + const message = `event: delete\ndata: 12asdf34\n\n`; + it("should be called", () => { + const spy = jest.fn(); + parser.once("delete", spy); + parser.parse(message); + expect(spy).toHaveBeenCalledWith("12asdf34"); + }); + }); - describe('event is not delete', () => { - const message = `event: event\ndata: 12asdf34\n\n` - it('should be error', () => { - const error = jest.fn() - const deleted = jest.fn() - parser.once('error', error) - parser.once('delete', deleted) - parser.parse(message) - expect(error).toHaveBeenCalled() - expect(deleted).not.toHaveBeenCalled() - }) - }) - }) + describe("event is not delete", () => { + const message = `event: event\ndata: 12asdf34\n\n`; + it("should be error", () => { + const error = jest.fn(); + const deleted = jest.fn(); + parser.once("error", error); + parser.once("delete", deleted); + parser.parse(message); + expect(error).toHaveBeenCalled(); + expect(deleted).not.toHaveBeenCalled(); + }); + }); + }); - describe('message is json', () => { - describe('event is update', () => { - const message = `event: update\ndata: ${JSON.stringify(status)}\n\n` - it('should be called', () => { - const spy = jest.fn() - parser.once('update', spy) - parser.parse(message) - expect(spy).toHaveBeenCalledWith(status) - }) - }) + describe("message is json", () => { + describe("event is update", () => { + const message = `event: update\ndata: ${JSON.stringify(status)}\n\n`; + it("should be called", () => { + const spy = jest.fn(); + parser.once("update", spy); + parser.parse(message); + expect(spy).toHaveBeenCalledWith(status); + }); + }); - describe('event is notification', () => { - const message = `event: notification\ndata: ${JSON.stringify(notification)}\n\n` - it('should be called', () => { - const spy = jest.fn() - parser.once('notification', spy) - parser.parse(message) - expect(spy).toHaveBeenCalledWith(notification) - }) - }) + describe("event is notification", () => { + const message = `event: notification\ndata: ${JSON.stringify( + notification, + )}\n\n`; + it("should be called", () => { + const spy = jest.fn(); + parser.once("notification", spy); + parser.parse(message); + expect(spy).toHaveBeenCalledWith(notification); + }); + }); - describe('event is conversation', () => { - const message = `event: conversation\ndata: ${JSON.stringify(conversation)}\n\n` - it('should be called', () => { - const spy = jest.fn() - parser.once('conversation', spy) - parser.parse(message) - expect(spy).toHaveBeenCalledWith(conversation) - }) - }) - }) - }) -}) + describe("event is conversation", () => { + const message = `event: conversation\ndata: ${JSON.stringify( + conversation, + )}\n\n`; + it("should be called", () => { + const spy = jest.fn(); + parser.once("conversation", spy); + parser.parse(message); + expect(spy).toHaveBeenCalledWith(conversation); + }); + }); + }); + }); +});