2025-05-13 18:39:57 +00:00
import { expect , tap } from '@push.rocks/tapbundle' ;
2024-04-25 10:49:07 +02:00
import * as qenv from '@push.rocks/qenv' ;
2024-04-27 12:47:49 +02:00
import * as smartrequest from '@push.rocks/smartrequest' ;
import * as smartfile from '@push.rocks/smartfile' ;
2024-03-30 12:42:44 +01:00
2024-04-25 10:49:07 +02:00
const testQenv = new qenv . Qenv ( './' , './.nogit/' ) ;
import * as smartai from '../ts/index.js' ;
let testSmartai : smartai.SmartAi ;
2025-10-03 14:21:25 +00:00
tap . test ( 'OpenAI Document: should create a smartai instance with OpenAI provider' , async ( ) = > {
2024-04-25 10:49:07 +02:00
testSmartai = new smartai . SmartAi ( {
openaiToken : await testQenv . getEnvVarOnDemand ( 'OPENAI_TOKEN' ) ,
} ) ;
2024-04-27 12:47:49 +02:00
await testSmartai . start ( ) ;
} ) ;
2025-10-03 14:21:25 +00:00
tap . test ( 'OpenAI Document: should document a pdf' , async ( ) = > {
2024-04-27 12:47:49 +02:00
const pdfUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf' ;
2025-08-01 18:25:46 +00:00
const pdfResponse = await smartrequest . SmartRequest . create ( )
. url ( pdfUrl )
. get ( ) ;
2024-04-27 12:47:49 +02:00
const result = await testSmartai . openaiProvider . document ( {
2025-02-03 15:16:58 +01:00
systemMessage : 'Classify the document. Only the following answers are allowed: "invoice", "bank account statement", "contract", "other". The answer should only contain the keyword for machine use.' ,
2024-04-27 12:47:49 +02:00
userMessage : "Classify the document." ,
messageHistory : [ ] ,
2025-08-01 18:25:46 +00:00
pdfDocuments : [ Buffer . from ( await pdfResponse . arrayBuffer ( ) ) ] ,
2024-04-27 12:47:49 +02:00
} ) ;
console . log ( result ) ;
2025-10-03 14:21:25 +00:00
expect ( result . message ) . toBeTruthy ( ) ;
2024-04-27 12:47:49 +02:00
} ) ;
2025-10-03 14:21:25 +00:00
tap . test ( 'OpenAI Document: should recognize companies in a pdf' , async ( ) = > {
2024-04-27 12:47:49 +02:00
const pdfBuffer = await smartfile . fs . toBuffer ( './.nogit/demo_without_textlayer.pdf' ) ;
const result = await testSmartai . openaiProvider . document ( {
systemMessage : `
summarize the document .
answer in JSON format , adhering to the following schema :
\ ` \` \` typescript
type TAnswer = {
entitySender : {
type : 'official state entity' | 'company' | 'person' ;
name : string ;
address : string ;
city : string ;
country : string ;
2025-02-25 19:15:32 +00:00
EU : boolean ; // whether the entity is within EU
2024-04-27 12:47:49 +02:00
} ;
entityReceiver : {
type : 'official state entity' | 'company' | 'person' ;
name : string ;
address : string ;
city : string ;
country : string ;
2025-02-25 19:15:32 +00:00
EU : boolean ; // whether the entity is within EU
2024-04-27 12:47:49 +02:00
} ;
date : string ; // the date of the document as YYYY-MM-DD
title : string ; // a short title, suitable for a filename
}
\ ` \` \`
` ,
userMessage : "Classify the document." ,
messageHistory : [ ] ,
pdfDocuments : [ pdfBuffer ] ,
} ) ;
console . log ( result ) ;
2025-10-03 14:21:25 +00:00
expect ( result . message ) . toBeTruthy ( ) ;
2025-02-25 19:15:32 +00:00
} ) ;
2025-10-03 14:21:25 +00:00
tap . test ( 'OpenAI Document: should stop the smartai instance' , async ( ) = > {
2024-04-27 12:47:49 +02:00
await testSmartai . stop ( ) ;
} ) ;
2025-10-03 14:21:25 +00:00
export default tap . start ( ) ;