| 985 | } |
| 986 | |
| 987 | func TestFileSystemIntegration(t *testing.T) { |
| 988 | vm := otto.New() |
| 989 | |
| 990 | // Create a temporary directory for testing |
| 991 | tmpDir, err := os.MkdirTemp("", "js_test_integration_*") |
| 992 | if err != nil { |
| 993 | t.Fatalf("failed to create temp dir: %v", err) |
| 994 | } |
| 995 | defer os.RemoveAll(tmpDir) |
| 996 | |
| 997 | t.Run("write, append, then read file", func(t *testing.T) { |
| 998 | testDir := filepath.Join(tmpDir, "nested", "sub", "directories") |
| 999 | testFile := filepath.Join(testDir, "roundtrip.txt") |
| 1000 | initialContent := "Round-trip test content\nLine 2\nLine 3\n" |
| 1001 | appendedContent := "Appended content\n" |
| 1002 | expectedContent := initialContent + appendedContent |
| 1003 | |
| 1004 | // Create subdirectories |
| 1005 | argDir, _ := vm.ToValue(testDir) |
| 1006 | mkdirCall := otto.FunctionCall{ |
| 1007 | ArgumentList: []otto.Value{argDir}, |
| 1008 | } |
| 1009 | mkdirResult := mkdirAll(mkdirCall) |
| 1010 | |
| 1011 | if !mkdirResult.IsNull() { |
| 1012 | t.Error("mkdirAll failed") |
| 1013 | } |
| 1014 | |
| 1015 | // Write file |
| 1016 | argFile, _ := vm.ToValue(testFile) |
| 1017 | argInitial, _ := vm.ToValue(initialContent) |
| 1018 | writeCall := otto.FunctionCall{ |
| 1019 | ArgumentList: []otto.Value{argFile, argInitial}, |
| 1020 | } |
| 1021 | |
| 1022 | writeResult := writeFile(writeCall) |
| 1023 | if !writeResult.IsNull() { |
| 1024 | t.Fatal("write failed") |
| 1025 | } |
| 1026 | |
| 1027 | // Append content |
| 1028 | argAppend, _ := vm.ToValue(appendedContent) |
| 1029 | appendCall := otto.FunctionCall{ |
| 1030 | ArgumentList: []otto.Value{argFile, argAppend}, |
| 1031 | } |
| 1032 | |
| 1033 | appendResult := appendFile(appendCall) |
| 1034 | if !appendResult.IsNull() { |
| 1035 | t.Fatal("append failed") |
| 1036 | } |
| 1037 | |
| 1038 | // Read file back |
| 1039 | readCall := otto.FunctionCall{ |
| 1040 | ArgumentList: []otto.Value{argFile}, |
| 1041 | } |
| 1042 | |
| 1043 | readResult := readFile(readCall) |
| 1044 | if readResult.IsUndefined() { |