Skip to content

Commit eeb9271

Browse files
automator: show source type (FAQ/docs/repo) in FAQ assistant Slack replies
Prefix each source line with its type so readers can tell how authoritative it is. Closes #26.
1 parent c9b3647 commit eeb9271

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

automator/src/lambda_function.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,27 @@ def post_faq_assistant_answer(channel, thread_ts, payload):
119119
slack.post_message_to_thread(channel, thread_ts, message)
120120

121121

122+
# Display labels for the FAQ assistant's structured `source` types.
123+
FAQ_SOURCE_LABELS = {'faq': 'FAQ', 'docs': 'docs', 'course-repo': 'repo'}
124+
125+
122126
def format_faq_sources(sources, found_answer=True):
123127
"""Render the FAQ assistant's structured sources as Slack mrkdwn links.
124128
125129
When the assistant found an answer these are its citations; otherwise they are
126-
suggested resources, so the heading changes accordingly.
130+
suggested resources, so the heading changes accordingly. Each line is prefixed
131+
with the source type (FAQ / docs / repo) so the reader can tell how authoritative
132+
it is.
127133
"""
128-
lines = [
129-
f"• <{source['url']}|{source.get('title') or source.get('source') or 'source'}>"
130-
for source in (sources or [])
131-
if source.get('url')
132-
]
134+
lines = []
135+
for source in (sources or []):
136+
if not source.get('url'):
137+
continue
138+
title = source.get('title') or source.get('source') or 'source'
139+
kind = source.get('source')
140+
label = FAQ_SOURCE_LABELS.get(kind, kind)
141+
prefix = f'[{label}] ' if label else ''
142+
lines.append(f"• {prefix}<{source['url']}|{title}>")
133143
if not lines:
134144
return ''
135145
heading = '*Sources:*' if found_answer else '*You can check these for more information:*'

automator/tests/test_automator_lambda.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,5 +1149,44 @@ def test_handle_faq_reaction_posts_answer_to_original_thread(self, mock_call, mo
11491149
)
11501150

11511151

1152+
class TestFormatFaqSources(unittest.TestCase):
1153+
"""format_faq_sources prefixes each line with the source type."""
1154+
1155+
def test_labels_known_source_types(self):
1156+
sources = [
1157+
{'source': 'faq', 'title': 'Can I use TypeScript?', 'url': 'https://x/faq#1'},
1158+
{'source': 'docs', 'title': 'Project', 'url': 'https://x/docs'},
1159+
{'source': 'course-repo', 'title': 'README', 'url': 'https://x/repo'},
1160+
]
1161+
result = lambda_function.format_faq_sources(sources, found_answer=True)
1162+
self.assertIn('*Sources:*', result)
1163+
self.assertIn('• [FAQ] <https://x/faq#1|Can I use TypeScript?>', result)
1164+
self.assertIn('• [docs] <https://x/docs|Project>', result)
1165+
self.assertIn('• [repo] <https://x/repo|README>', result)
1166+
1167+
def test_unknown_source_type_used_verbatim(self):
1168+
sources = [{'source': 'blog', 'title': 'Post', 'url': 'https://x/p'}]
1169+
result = lambda_function.format_faq_sources(sources)
1170+
self.assertIn('• [blog] <https://x/p|Post>', result)
1171+
1172+
def test_missing_source_type_has_no_prefix(self):
1173+
sources = [{'title': 'Post', 'url': 'https://x/p'}]
1174+
result = lambda_function.format_faq_sources(sources)
1175+
self.assertIn('• <https://x/p|Post>', result)
1176+
1177+
def test_not_found_changes_heading(self):
1178+
sources = [{'source': 'faq', 'title': 'T', 'url': 'https://x/f'}]
1179+
result = lambda_function.format_faq_sources(sources, found_answer=False)
1180+
self.assertIn('*You can check these for more information:*', result)
1181+
1182+
def test_sources_without_url_are_skipped(self):
1183+
sources = [{'source': 'faq', 'title': 'No link'}]
1184+
self.assertEqual(lambda_function.format_faq_sources(sources), '')
1185+
1186+
def test_empty_sources(self):
1187+
self.assertEqual(lambda_function.format_faq_sources(None), '')
1188+
self.assertEqual(lambda_function.format_faq_sources([]), '')
1189+
1190+
11521191
if __name__ == '__main__':
11531192
unittest.main()

0 commit comments

Comments
 (0)