@@ -23,6 +23,15 @@ describe('readInputFile', () => {
2323
2424 expect ( content ) . toBe ( 'Hello, world!' )
2525 } )
26+
27+ it ( 'reads a direct Telegram result.json file' , async ( ) => {
28+ const filePath = join ( TEST_DIR , 'result.json' )
29+ await writeFile ( filePath , '{"messages":[]}' )
30+
31+ const content = await readInputFile ( filePath )
32+
33+ expect ( content ) . toBe ( '{"messages":[]}' )
34+ } )
2635 } )
2736
2837 describe ( 'zip files' , ( ) => {
@@ -54,7 +63,21 @@ describe('readInputFile', () => {
5463 expect ( content ) . toBe ( 'Chat via _chat.txt' )
5564 } )
5665
57- it ( 'throws when no .txt file found in zip' , async ( ) => {
66+ it ( 'reads result.json from a Telegram zip archive' , async ( ) => {
67+ const JSZip = await import ( 'jszip' )
68+ const zip = new JSZip . default ( )
69+ zip . file ( 'ChatExport/result.json' , '{"messages":[]}' )
70+
71+ const zipContent = await zip . generateAsync ( { type : 'uint8array' } )
72+ const filePath = join ( TEST_DIR , 'telegram.zip' )
73+ await writeFile ( filePath , zipContent )
74+
75+ const content = await readInputFile ( filePath )
76+
77+ expect ( content ) . toBe ( '{"messages":[]}' )
78+ } )
79+
80+ it ( 'throws when no supported chat file is found in zip' , async ( ) => {
5881 const JSZip = await import ( 'jszip' )
5982 const zip = new JSZip . default ( )
6083 zip . file ( 'readme.md' , 'Not a chat file' )
@@ -78,6 +101,27 @@ describe('readInputFile', () => {
78101 expect ( content ) . toBe ( 'iMessage content' )
79102 } )
80103
104+ it ( 'reads result.json from a Telegram export directory' , async ( ) => {
105+ const subDir = join ( TEST_DIR , 'telegram-export' )
106+ await mkdir ( subDir , { recursive : true } )
107+ await writeFile ( join ( subDir , 'result.json' ) , '{"messages":[]}' )
108+
109+ const content = await readInputFile ( subDir )
110+
111+ expect ( content ) . toBe ( '{"messages":[]}' )
112+ } )
113+
114+ it ( 'prefers Telegram result.json over text files in a directory' , async ( ) => {
115+ const subDir = join ( TEST_DIR , 'telegram-export-with-files' )
116+ await mkdir ( subDir , { recursive : true } )
117+ await writeFile ( join ( subDir , 'result.json' ) , '{"messages":[]}' )
118+ await writeFile ( join ( subDir , 'captions.txt' ) , 'caption attachment' )
119+
120+ const content = await readInputFile ( subDir )
121+
122+ expect ( content ) . toBe ( '{"messages":[]}' )
123+ } )
124+
81125 it ( 'concatenates multiple .txt files from a directory' , async ( ) => {
82126 const subDir = join ( TEST_DIR , 'multi-export' )
83127 await mkdir ( subDir , { recursive : true } )
@@ -90,19 +134,19 @@ describe('readInputFile', () => {
90134 expect ( content ) . toBe ( 'First chat\nSecond chat' )
91135 } )
92136
93- it ( 'throws when directory contains no .txt files' , async ( ) => {
137+ it ( 'throws when directory contains no supported chat files' , async ( ) => {
94138 const subDir = join ( TEST_DIR , 'empty-export' )
95139 await mkdir ( subDir , { recursive : true } )
96140 await writeFile ( join ( subDir , 'readme.md' ) , 'Not a txt file' )
97141
98- await expect ( readInputFile ( subDir ) ) . rejects . toThrow ( 'No .txt files found' )
142+ await expect ( readInputFile ( subDir ) ) . rejects . toThrow ( 'No .txt or result.json files found' )
99143 } )
100144
101145 it ( 'throws when directory is empty' , async ( ) => {
102146 const subDir = join ( TEST_DIR , 'empty-dir' )
103147 await mkdir ( subDir , { recursive : true } )
104148
105- await expect ( readInputFile ( subDir ) ) . rejects . toThrow ( 'No .txt files found' )
149+ await expect ( readInputFile ( subDir ) ) . rejects . toThrow ( 'No .txt or result.json files found' )
106150 } )
107151
108152 it ( 'ignores non-.txt files in directory' , async ( ) => {
0 commit comments