refs #1021 Add test for toot parser

This commit is contained in:
AkiraFukushima 2019-12-11 22:48:38 +09:00
parent f2ce160075
commit fe766aaf3f
2 changed files with 49 additions and 21 deletions

View File

@ -3,14 +3,14 @@ import { findLink, findTag, findAccount } from '@/utils/tootParser'
describe('findLink', () => {
describe('Pleroma', () => {
const doc = (new JSDOM(`<html><head></head><body>
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p>
I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streaming update of Pleroma. But it contains a bug, so it is resolved in version 2.4.1. <br /><a href="https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1" id="link">https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1</a><br /><a href="https://pleroma.io/tag/whalebird">#Whalebird</a>
</p>
</div>
</body>
</html>`)).window.document
</html>`).window.document
const target = doc.getElementById('link')
it('should find', () => {
@ -22,14 +22,14 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
describe('findTag', () => {
describe('Pleroma', () => {
const doc = (new JSDOM(`<html><head></head><body>
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p>
I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streaming update of Pleroma. But it contains a bug, so it is resolved in version 2.4.1. <br /><a href="https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1">https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1</a><br /><a id="tag" href="https://pleroma.io/tag/whalebird">#Whalebird</a>
</p>
</div>
</body>
</html>`)).window.document
</html>`).window.document
const target = doc.getElementById('tag')
it('should find', () => {
const res = findTag(target)
@ -38,14 +38,14 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
})
describe('Mastodon', () => {
const doc = (new JSDOM(`<html><head></head><body>
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p>
I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streaming update of Pleroma. But it contains a bug, so it is resolved in version 2.4.1. <br /><a href="https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1">https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1</a><br /><a id="tag" class="hashtag" href="https://pleroma.io/tag/whalebird">#<span>Whalebird</span></a>
</p>
</div>
</body>
</html>`)).window.document
</html>`).window.document
const target = doc.getElementById('tag')
it('should find', () => {
const res = findTag(target)
@ -57,12 +57,12 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
describe('findAccount', () => {
describe('in Pleroma', () => {
describe('from Mastodon', () => {
const doc = (new JSDOM(`<html><head></head><body>
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p><span><a href="https://social.mikutter.hachune.net/@h3_poteto">@<span id="user">h3_poteto</span></a></span> hogehoge</p>
</div>
</body>
</html>`)).window.document
</html>`).window.document
const target = doc.getElementById('user')
it('should find', () => {
const res = findAccount(target)
@ -72,12 +72,12 @@ describe('findAccount', () => {
})
describe('from Pleroma', () => {
const doc = (new JSDOM(`<html><head></head><body>
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p><span><a href="https://pleroma.io/users/h3poteto">@<span id="user">h3_poteto</span></a></span> hogehoge</p>
</div>
</body>
</html>`)).window.document
</html>`).window.document
const target = doc.getElementById('user')
it('should find', () => {
const res = findAccount(target)
@ -85,5 +85,33 @@ describe('findAccount', () => {
expect(res.acct).toEqual('@h3poteto@pleroma.io')
})
})
describe('toot link in Mastodon', () => {
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p><span><a id="status" href="https://https://fedibird.com/@h3poteto/103040884240752891">https://fedibird.com/@h3poteto/103040884240752891</a></span> hogehoge</p>
</div>
</body>
</html>`).window.document
const target = doc.getElementById('status')
it('should not find', () => {
const res = findAccount(target)
expect(res).toBeNull()
})
})
describe('toot link in Pleroma', () => {
const doc = new JSDOM(`<html><head></head><body>
<div class="toot">
<p><span><a id="status" href="https://pleroma.io/notice/9pqtJ78TcXAytY51Wa">https://pleroma.io/notice/9pqtJ78TcXAytY51Wa</a></span> hogehoge</p>
</div>
</body>
</html>`).window.document
const target = doc.getElementById('status')
it('should not find', () => {
const res = findAccount(target)
expect(res).toBeNull()
})
})
})
})

View File

@ -1,4 +1,4 @@
export function findLink (target, parentClass = 'toot') {
export function findLink(target, parentClass = 'toot') {
if (target.localName === 'a') {
return target.href
}
@ -11,7 +11,7 @@ export function findLink (target, parentClass = 'toot') {
return findLink(target.parentNode, parentClass)
}
export function findTag (target, parentClass = 'toot') {
export function findTag(target, parentClass = 'toot') {
if (target.getAttribute('class') && target.getAttribute('class').includes('hashtag')) {
return parseTag(target.href)
}
@ -29,14 +29,14 @@ export function findTag (target, parentClass = 'toot') {
return findTag(target.parentNode, parentClass)
}
export function parseTag (tagURL) {
export function parseTag(tagURL) {
const res = tagURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(tag|tags)\/(.+)/)
return res[3]
}
export function findAccount (target, parentClass = 'toot') {
export function findAccount(target, parentClass = 'toot') {
if (target.getAttribute('class') && target.getAttribute('class').includes('u-url')) {
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+/)) {
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+$/)) {
return parsePleromaAccount(target.href)
} else {
return parseMastodonAccount(target.href)
@ -44,11 +44,11 @@ export function findAccount (target, parentClass = 'toot') {
}
// In Pleroma, link does not have class.
// So we have to check URL.
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/@[a-zA-Z0-9-_.]+/)) {
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/@[a-zA-Z0-9-_.]+$/)) {
return parseMastodonAccount(target.href)
}
// Toot URL of Pleroma does not contain @.
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+/)) {
if (target.href && target.href.match(/^https:\/\/[a-zA-Z0-9-.]+\/users\/[a-zA-Z0-9-_.]+$/)) {
return parsePleromaAccount(target.href)
}
if (target.parentNode === undefined || target.parentNode === null) {
@ -60,8 +60,8 @@ export function findAccount (target, parentClass = 'toot') {
return findAccount(target.parentNode, parentClass)
}
export function parseMastodonAccount (accountURL) {
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(@[a-zA-Z0-9-_.]+)/)
export function parseMastodonAccount(accountURL) {
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/(@[a-zA-Z0-9-_.]+)$/)
const domainName = res[1]
const accountName = res[2]
return {
@ -71,8 +71,8 @@ export function parseMastodonAccount (accountURL) {
}
}
export function parsePleromaAccount (accountURL) {
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/users\/([a-zA-Z0-9-_.]+)/)
export function parsePleromaAccount(accountURL) {
const res = accountURL.match(/^https:\/\/([a-zA-Z0-9-.]+)\/users\/([a-zA-Z0-9-_.]+)$/)
const domainName = res[1]
const accountName = res[2]
return {