Test for web link replacement in the speaker interface

merge-requests/30/head
Bob Mottram 2021-03-01 21:46:44 +00:00
parent 9851a0f1af
commit f0659f2dd2
2 changed files with 26 additions and 2 deletions

View File

@ -38,7 +38,7 @@ def speakerReplaceLinks(sayText: str, translate: {},
"""Replaces any links in the given text with "link to [domain]".
Instead of reading out potentially very long and meaningless links
"""
removeChars = ('.', ',', ';', ':', '?', '!')
removeChars = ('.\n', '. ', ',', ';', '?', '!')
text = sayText
for ch in removeChars:
text = text.replace(ch, ' ')
@ -58,11 +58,13 @@ def speakerReplaceLinks(sayText: str, translate: {},
continue
if '/' in domain:
domain = domain.split('/')[0]
if domain.startswith('www.'):
domain = domain.replace('www.', '')
replacements[domainFull] = '. ' + linkedStr + ' ' + domain + '.'
detectedLinks.append(domainFull)
for replaceStr, newStr in replacements.items():
sayText = sayText.replace(replaceStr, newStr)
return sayText
return sayText.replace('..', '.')
def getSpeakerFromServer(baseDir: str, session,

View File

@ -100,6 +100,7 @@ from mastoapiv1 import getMastoApiV1IdFromNickname
from mastoapiv1 import getNicknameFromMastoApiV1Id
from webapp_post import prepareHtmlPostNickname
from webapp_utils import markdownToHtml
from speaker import speakerReplaceLinks
testServerAliceRunning = False
testServerBobRunning = False
@ -3368,9 +3369,30 @@ def testExtractTextFieldsInPOST():
assert fields['message'] == 'This is a ; test'
def testSpeakerReplaceLinks():
print('testSpeakerReplaceLinks')
text = 'The Tor Project: For Snowflake volunteers: If you use ' + \
'Firefox, Brave, or Chrome, our Snowflake extension turns ' + \
'your browser into a proxy that connects Tor users in ' + \
'censored regions to the Tor network. Note: you should ' + \
'not run more than one snowflake in the same ' + \
'network.https://support.torproject.org/censorship/' + \
'how-to-help-running-snowflake/'
detectedLinks = []
result = speakerReplaceLinks(text, {'Linked': 'Web link'}, detectedLinks)
print(result)
print(str(detectedLinks))
assert len(detectedLinks) == 1
assert detectedLinks[0] == \
'https://support.torproject.org/censorship/' + \
'how-to-help-running-snowflake/'
assert 'Web link support.torproject.org' in result
def runAllTests():
print('Running tests...')
testFunctions()
testSpeakerReplaceLinks()
testExtractTextFieldsInPOST()
testMarkdownToHtml()
testValidHashTag()