170 lines
5.7 KiB
Python
170 lines
5.7 KiB
Python
import json
|
|
import unittest
|
|
|
|
from chatgpt_share_exporter import extract_conversation, render_markdown
|
|
|
|
|
|
def compact_payload(root):
|
|
table = [None]
|
|
|
|
def ref(value):
|
|
table.append(encoded(value))
|
|
return len(table) - 1
|
|
|
|
def encoded(value):
|
|
if isinstance(value, dict):
|
|
return {f"_{ref(key)}": ref(item) for key, item in value.items()}
|
|
if isinstance(value, list):
|
|
return [ref(item) for item in value]
|
|
return value
|
|
|
|
table[0] = encoded(root)
|
|
return table
|
|
|
|
|
|
def page_with_payload(root):
|
|
payload = json.dumps(compact_payload(root))
|
|
stream_argument = json.dumps(payload + "\n")
|
|
return (
|
|
"<html><body><script>"
|
|
f"window.__reactRouterContext.streamController.enqueue({stream_argument});"
|
|
"</script></body></html>"
|
|
)
|
|
|
|
|
|
class ChatGPTShareExporterTests(unittest.TestCase):
|
|
def test_extracts_conversation_from_react_router_stream(self):
|
|
root = {
|
|
"loaderData": {
|
|
"routes/share.$shareId.($action)": {
|
|
"serverResponse": {
|
|
"type": "data",
|
|
"data": sample_conversation(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
conversation = extract_conversation(page_with_payload(root))
|
|
|
|
self.assertEqual(conversation["title"], "Demo Chat")
|
|
self.assertEqual(conversation["conversation_id"], "share-123")
|
|
self.assertEqual(len(conversation["linear_conversation"]), 3)
|
|
|
|
def test_renders_visible_messages_with_clean_metadata(self):
|
|
markdown, count = render_markdown(
|
|
sample_conversation(),
|
|
"https://chatgpt.com/share/share-123",
|
|
timezone_name="UTC",
|
|
)
|
|
|
|
self.assertEqual(count, 2)
|
|
self.assertIn("# Demo Chat", markdown)
|
|
self.assertIn("## 1. User - 1970-01-01T00:16:40+00:00", markdown)
|
|
self.assertIn("| Message ID | user-1 |", markdown)
|
|
self.assertIn("**Attachments**", markdown)
|
|
self.assertIn("scan.jpg", markdown)
|
|
self.assertIn("What does this mean?", markdown)
|
|
self.assertIn("## 2. Assistant - 1970-01-01T00:16:45+00:00", markdown)
|
|
self.assertIn("Here is the answer.", markdown)
|
|
self.assertNotIn("internal note", markdown)
|
|
|
|
def test_can_include_hidden_messages(self):
|
|
markdown, count = render_markdown(
|
|
sample_conversation(),
|
|
"https://chatgpt.com/share/share-123",
|
|
include_hidden=True,
|
|
timezone_name="UTC",
|
|
)
|
|
|
|
self.assertEqual(count, 3)
|
|
self.assertIn("internal note", markdown)
|
|
self.assertIn("## 1. System", markdown)
|
|
|
|
|
|
def sample_conversation():
|
|
return {
|
|
"title": "Demo Chat",
|
|
"conversation_id": "share-123",
|
|
"create_time": 1000,
|
|
"update_time": 1010,
|
|
"default_model_slug": "gpt-test",
|
|
"current_node": "assistant-1",
|
|
"mapping": {},
|
|
"linear_conversation": [
|
|
{
|
|
"id": "system-1",
|
|
"message": {
|
|
"id": "system-1",
|
|
"author": {"role": "system", "metadata": {}},
|
|
"content": {"content_type": "text", "parts": ["internal note"]},
|
|
"status": "finished_successfully",
|
|
"metadata": {"is_visually_hidden_from_conversation": True},
|
|
},
|
|
"children": ["user-1"],
|
|
},
|
|
{
|
|
"id": "user-1",
|
|
"message": {
|
|
"id": "user-1",
|
|
"author": {"role": "user", "metadata": {}},
|
|
"create_time": 1000,
|
|
"content": {
|
|
"content_type": "multimodal_text",
|
|
"parts": [
|
|
{
|
|
"content_type": "image_asset_pointer",
|
|
"asset_pointer": "sediment://file_demo123?shared_conversation_id=share-123",
|
|
"size_bytes": 2048,
|
|
"width": 640,
|
|
"height": 480,
|
|
},
|
|
"What does this mean?",
|
|
],
|
|
},
|
|
"status": "finished_successfully",
|
|
"metadata": {
|
|
"attachments": [
|
|
{
|
|
"id": "file_demo123",
|
|
"name": "scan.jpg",
|
|
"size": 2048,
|
|
"mime_type": "image/jpeg",
|
|
}
|
|
],
|
|
},
|
|
"recipient": "all",
|
|
},
|
|
"parent": "system-1",
|
|
"children": ["assistant-1"],
|
|
},
|
|
{
|
|
"id": "assistant-1",
|
|
"message": {
|
|
"id": "assistant-1",
|
|
"author": {"role": "assistant", "metadata": {}},
|
|
"create_time": 1005,
|
|
"update_time": 1008,
|
|
"content": {
|
|
"content_type": "text",
|
|
"parts": ["Here is the answer."],
|
|
},
|
|
"status": "finished_successfully",
|
|
"end_turn": True,
|
|
"metadata": {
|
|
"resolved_model_slug": "gpt-test",
|
|
"request_id": "request-1",
|
|
},
|
|
"recipient": "all",
|
|
"channel": "final",
|
|
},
|
|
"parent": "user-1",
|
|
"children": [],
|
|
},
|
|
],
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|