1+ /**
2+ * Document service tests
3+ */
4+
5+ import { describe , it , expect , beforeEach , afterEach } from 'vitest' ;
6+ import { DocumentService } from '../document-service.js' ;
7+ import type { DevlogDocument } from '../../types/index.js' ;
8+
9+ // Mock data for testing
10+ const mockFile = {
11+ originalName : 'test-document.txt' ,
12+ mimeType : 'text/plain' ,
13+ size : 1024 ,
14+ content : Buffer . from ( 'This is a test document content' , 'utf-8' ) ,
15+ } ;
16+
17+ const mockDevlogId = 1 ;
18+
19+ describe ( 'DocumentService' , ( ) => {
20+ // Note: Database tests are skipped due to enum column compatibility issues with SQLite
21+ // These tests focus on the business logic and type detection functionality
22+
23+ describe ( 'Document Type Detection' , ( ) => {
24+ it ( 'should detect text documents correctly' , ( ) => {
25+ const service = DocumentService . getInstance ( ) ;
26+
27+ // Access private method through any to test it
28+ const detectType = ( service as any ) . determineDocumentType . bind ( service ) ;
29+
30+ expect ( detectType ( 'text/plain' , '.txt' ) ) . toBe ( 'text' ) ;
31+ expect ( detectType ( 'text/markdown' , '.md' ) ) . toBe ( 'markdown' ) ;
32+ expect ( detectType ( 'application/json' , '.json' ) ) . toBe ( 'json' ) ;
33+ expect ( detectType ( 'text/csv' , '.csv' ) ) . toBe ( 'csv' ) ;
34+ } ) ;
35+
36+ it ( 'should detect code documents correctly' , ( ) => {
37+ const service = DocumentService . getInstance ( ) ;
38+ const detectType = ( service as any ) . determineDocumentType . bind ( service ) ;
39+
40+ expect ( detectType ( 'text/plain' , '.js' ) ) . toBe ( 'code' ) ;
41+ expect ( detectType ( 'text/plain' , '.ts' ) ) . toBe ( 'code' ) ;
42+ expect ( detectType ( 'text/plain' , '.py' ) ) . toBe ( 'code' ) ;
43+ expect ( detectType ( 'text/plain' , '.java' ) ) . toBe ( 'code' ) ;
44+ } ) ;
45+
46+ it ( 'should detect images correctly' , ( ) => {
47+ const service = DocumentService . getInstance ( ) ;
48+ const detectType = ( service as any ) . determineDocumentType . bind ( service ) ;
49+
50+ expect ( detectType ( 'image/png' , '.png' ) ) . toBe ( 'image' ) ;
51+ expect ( detectType ( 'image/jpeg' , '.jpg' ) ) . toBe ( 'image' ) ;
52+ expect ( detectType ( 'image/gif' , '.gif' ) ) . toBe ( 'image' ) ;
53+ } ) ;
54+
55+ it ( 'should detect PDFs correctly' , ( ) => {
56+ const service = DocumentService . getInstance ( ) ;
57+ const detectType = ( service as any ) . determineDocumentType . bind ( service ) ;
58+
59+ expect ( detectType ( 'application/pdf' , '.pdf' ) ) . toBe ( 'pdf' ) ;
60+ } ) ;
61+
62+ it ( 'should default to other for unknown types' , ( ) => {
63+ const service = DocumentService . getInstance ( ) ;
64+ const detectType = ( service as any ) . determineDocumentType . bind ( service ) ;
65+
66+ expect ( detectType ( 'application/unknown' , '.xyz' ) ) . toBe ( 'other' ) ;
67+ } ) ;
68+ } ) ;
69+
70+ describe ( 'Text Content Extraction' , ( ) => {
71+ it ( 'should identify text-based types correctly' , ( ) => {
72+ const service = DocumentService . getInstance ( ) ;
73+ const isTextBased = ( service as any ) . isTextBasedType . bind ( service ) ;
74+
75+ expect ( isTextBased ( 'text' ) ) . toBe ( true ) ;
76+ expect ( isTextBased ( 'markdown' ) ) . toBe ( true ) ;
77+ expect ( isTextBased ( 'code' ) ) . toBe ( true ) ;
78+ expect ( isTextBased ( 'json' ) ) . toBe ( true ) ;
79+ expect ( isTextBased ( 'csv' ) ) . toBe ( true ) ;
80+ expect ( isTextBased ( 'log' ) ) . toBe ( true ) ;
81+ expect ( isTextBased ( 'config' ) ) . toBe ( true ) ;
82+
83+ expect ( isTextBased ( 'image' ) ) . toBe ( false ) ;
84+ expect ( isTextBased ( 'pdf' ) ) . toBe ( false ) ;
85+ expect ( isTextBased ( 'other' ) ) . toBe ( false ) ;
86+ } ) ;
87+
88+ it ( 'should extract text content from strings and buffers' , ( ) => {
89+ const service = DocumentService . getInstance ( ) ;
90+ const extractText = ( service as any ) . extractTextContent . bind ( service ) ;
91+
92+ const textContent = 'Hello, World!' ;
93+ const bufferContent = Buffer . from ( textContent , 'utf-8' ) ;
94+
95+ expect ( extractText ( textContent , 'text' ) ) . toBe ( textContent ) ;
96+ expect ( extractText ( bufferContent , 'text' ) ) . toBe ( textContent ) ;
97+ expect ( extractText ( bufferContent , 'image' ) ) . toBe ( '' ) ;
98+ } ) ;
99+ } ) ;
100+
101+ // Note: More comprehensive integration tests would require a test database
102+ // These tests focus on the business logic and type detection functionality
103+ } ) ;
0 commit comments