@@ -125,3 +125,67 @@ func TestUploadParseDelete(t *testing.T) {
125125 t .Error ("expected document to be removed from files map after delete" )
126126 }
127127}
128+
129+ // TestCleanupSession uploads two documents, parses them, then calls
130+ // CleanupSession and verifies all documents and parse jobs are cleaned up.
131+ // This also verifies that cleanup continues past individual errors
132+ // (best-effort) rather than stopping on the first failure.
133+ func TestCleanupSession (t * testing.T ) {
134+ s := initTestServer (t )
135+ ctx := context .Background ()
136+
137+ // Upload two documents.
138+ var docIDs []string
139+ for range 2 {
140+ result , _ , err := s .UploadDocument (ctx , & mcp.CallToolRequest {}, & UploadDocumentInput {
141+ URL : "file://testdata/sixt_DE_de.pdf" ,
142+ })
143+ if err != nil {
144+ t .Fatalf ("UploadDocument error: %v" , err )
145+ }
146+ if result .IsError {
147+ t .Fatalf ("UploadDocument tool error: %v" , result .Content )
148+ }
149+ upload := unmarshalToolResult [UploadDocumentOutput ](t , result )
150+ docIDs = append (docIDs , upload .DocumentId )
151+ }
152+
153+ // Parse both documents synchronously.
154+ for _ , docID := range docIDs {
155+ result , _ , err := s .ParseDocument (ctx , & mcp.CallToolRequest {}, & ParseDocumentInput {
156+ DocumentId : docID ,
157+ Sync : true ,
158+ })
159+ if err != nil {
160+ t .Fatalf ("ParseDocument error: %v" , err )
161+ }
162+ if result .IsError {
163+ t .Fatalf ("ParseDocument tool error: %v" , result .Content )
164+ }
165+ }
166+
167+ // Verify both documents are in the files map with parse jobs.
168+ for _ , docID := range docIDs {
169+ info , ok := files .Load (docID )
170+ if ! ok {
171+ t .Fatalf ("expected document %s in files map before cleanup" , docID )
172+ }
173+ if len (info .ParseJobs ) == 0 {
174+ t .Fatalf ("expected parse jobs for document %s before cleanup" , docID )
175+ }
176+ }
177+
178+ // Run cleanup.
179+ s .CleanupSession (ctx )
180+
181+ // Verify the files map is empty — all documents should have been
182+ // processed regardless of individual errors.
183+ var remaining []string
184+ files .Range (func (key string , _ * FileInfo ) bool {
185+ remaining = append (remaining , key )
186+ return true
187+ })
188+ if len (remaining ) > 0 {
189+ t .Errorf ("expected files map to be empty after CleanupSession, still has: %v" , remaining )
190+ }
191+ }
0 commit comments