11import * as gptscript from "../src/gptscript"
22import {
33 ArgumentSchemaType ,
4- CredentialType , Dataset ,
4+ CredentialType ,
55 getEnv ,
66 PropertyType ,
77 RunEventType ,
@@ -13,7 +13,7 @@ import path from "path"
1313import { fileURLToPath } from "url"
1414import * as fs from "node:fs"
1515import { randomBytes } from "node:crypto"
16- import { tmpdir } from "node:os" ;
16+ import { tmpdir } from "node:os"
1717
1818let gFirst : gptscript . GPTScript
1919let g : gptscript . GPTScript
@@ -908,21 +908,21 @@ describe("gptscript module", () => {
908908 // Add elements
909909 try {
910910 const e1 = await g . addDatasetElement (
911- workspace ,
912- datasetID ,
913- "element1" ,
914- "" ,
915- "this is element 1 contents"
911+ workspace ,
912+ datasetID ,
913+ "element1" ,
914+ "" ,
915+ "this is element 1 contents"
916916 )
917917 expect ( e1 . name ) . toEqual ( "element1" )
918918 expect ( e1 . description ) . toEqual ( "" )
919919
920920 const e2 = await g . addDatasetElement (
921- workspace ,
922- datasetID ,
923- "element2" ,
924- "a description" ,
925- "this is element 2 contents"
921+ workspace ,
922+ datasetID ,
923+ "element2" ,
924+ "a description" ,
925+ "this is element 2 contents"
926926 )
927927 expect ( e2 . name ) . toEqual ( "element2" )
928928 expect ( e2 . description ) . toEqual ( "a description" )
@@ -964,4 +964,113 @@ describe("gptscript module", () => {
964964 throw new Error ( "failed to list datasets: " + e )
965965 }
966966 } , 20000 )
967+
968+ test ( "create and delete workspace" , async ( ) => {
969+ if ( ! process . env . AWS_ACCESS_KEY_ID || ! process . env . AWS_SECRET_ACCESS_KEY ) {
970+ console . log ( "AWS credentials not set, skipping test" )
971+ return
972+ }
973+
974+ const workspaceID = await g . createWorkspace ( "directory" )
975+ expect ( workspaceID ) . toBeDefined ( )
976+ await g . deleteWorkspace ( workspaceID )
977+ } )
978+
979+ test ( "write, read, and delete file" , async ( ) => {
980+ if ( ! process . env . AWS_ACCESS_KEY_ID || ! process . env . AWS_SECRET_ACCESS_KEY ) {
981+ console . log ( "AWS credentials not set, skipping test" )
982+ return
983+ }
984+
985+ const workspaceID = await g . createWorkspace ( "directory" )
986+ expect ( workspaceID ) . toBeDefined ( )
987+
988+ await g . writeFileInWorkspace ( "test.txt" , Buffer . from ( "test" ) , workspaceID )
989+ const content = await g . readFileInWorkspace ( "test.txt" , workspaceID )
990+ expect ( content . toString ( ) ) . toEqual ( "test" )
991+ await g . deleteWorkspace ( workspaceID )
992+ } )
993+
994+ test ( "test complex ls" , async ( ) => {
995+ if ( ! process . env . AWS_ACCESS_KEY_ID || ! process . env . AWS_SECRET_ACCESS_KEY ) {
996+ console . log ( "AWS credentials not set, skipping test" )
997+ return
998+ }
999+
1000+ const workspaceID = await g . createWorkspace ( "directory" )
1001+
1002+ // Write files in the workspace
1003+ await g . writeFileInWorkspace ( "test/test1.txt" , Buffer . from ( "hello1" ) , workspaceID )
1004+ await g . writeFileInWorkspace ( "test1/test2.txt" , Buffer . from ( "hello2" ) , workspaceID )
1005+ await g . writeFileInWorkspace ( "test1/test3.txt" , Buffer . from ( "hello3" ) , workspaceID )
1006+ await g . writeFileInWorkspace ( ".hidden.txt" , Buffer . from ( "hidden" ) , workspaceID )
1007+
1008+ let content = await g . listFilesInWorkspace ( undefined , workspaceID )
1009+ expect ( content . length ) . toEqual ( 4 )
1010+ expect ( content ) . toContain ( "test1/test2.txt" )
1011+ expect ( content ) . toContain ( "test1/test3.txt" )
1012+ expect ( content ) . toContain ( "test/test1.txt" )
1013+ expect ( content ) . toContain ( ".hidden.txt" )
1014+
1015+ content = await g . listFilesInWorkspace ( "test1" , workspaceID )
1016+ expect ( content . length ) . toEqual ( 2 )
1017+ expect ( content ) . toContain ( "test1/test2.txt" )
1018+ expect ( content ) . toContain ( "test1/test3.txt" )
1019+
1020+ await g . removeAll ( "test1" , workspaceID )
1021+
1022+ content = await g . listFilesInWorkspace ( "" , workspaceID )
1023+ expect ( content . length ) . toEqual ( 2 )
1024+ expect ( content ) . toContain ( "test/test1.txt" )
1025+ expect ( content ) . toContain ( ".hidden.txt" )
1026+
1027+ await g . deleteWorkspace ( workspaceID )
1028+ } )
1029+
1030+ test ( "create and delete workspace in s3" , async ( ) => {
1031+ const workspaceID = await g . createWorkspace ( "s3" )
1032+ expect ( workspaceID ) . toBeDefined ( )
1033+ await g . deleteWorkspace ( workspaceID )
1034+ } )
1035+
1036+ test ( "write, read, and delete file in s3" , async ( ) => {
1037+ const workspaceID = await g . createWorkspace ( "s3" )
1038+ expect ( workspaceID ) . toBeDefined ( )
1039+
1040+ await g . writeFileInWorkspace ( "test.txt" , Buffer . from ( "test" ) , workspaceID )
1041+ const content = await g . readFileInWorkspace ( "test.txt" , workspaceID )
1042+ expect ( content . toString ( ) ) . toEqual ( "test" )
1043+ await g . deleteWorkspace ( workspaceID )
1044+ } )
1045+
1046+ test ( "test complex ls in s3" , async ( ) => {
1047+ const workspaceID = await g . createWorkspace ( "s3" )
1048+
1049+ // Write files in the workspace
1050+ await g . writeFileInWorkspace ( "test/test1.txt" , Buffer . from ( "hello1" ) , workspaceID )
1051+ await g . writeFileInWorkspace ( "test1/test2.txt" , Buffer . from ( "hello2" ) , workspaceID )
1052+ await g . writeFileInWorkspace ( "test1/test3.txt" , Buffer . from ( "hello3" ) , workspaceID )
1053+ await g . writeFileInWorkspace ( ".hidden.txt" , Buffer . from ( "hidden" ) , workspaceID )
1054+
1055+ let content = await g . listFilesInWorkspace ( undefined , workspaceID )
1056+ expect ( content . length ) . toEqual ( 4 )
1057+ expect ( content ) . toContain ( "test1/test2.txt" )
1058+ expect ( content ) . toContain ( "test1/test3.txt" )
1059+ expect ( content ) . toContain ( "test/test1.txt" )
1060+ expect ( content ) . toContain ( ".hidden.txt" )
1061+
1062+ content = await g . listFilesInWorkspace ( "test1" , workspaceID )
1063+ expect ( content . length ) . toEqual ( 2 )
1064+ expect ( content ) . toContain ( "test1/test2.txt" )
1065+ expect ( content ) . toContain ( "test1/test3.txt" )
1066+
1067+ await g . removeAll ( "test1" , workspaceID )
1068+
1069+ content = await g . listFilesInWorkspace ( "" , workspaceID )
1070+ expect ( content . length ) . toEqual ( 2 )
1071+ expect ( content ) . toContain ( "test/test1.txt" )
1072+ expect ( content ) . toContain ( ".hidden.txt" )
1073+
1074+ await g . deleteWorkspace ( workspaceID )
1075+ } )
9671076} )
0 commit comments