fs.test.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. naughtyStrings = [
  2. "文件.txt", // Chinese characters
  3. "файл.txt", // Cyrillic characters
  4. "ファイル.txt", // Japanese characters
  5. "파일.txt", // Korean characters
  6. "ملف.txt", // Arabic characters
  7. "फ़ाइल.txt", // Hindi characters
  8. "archivo.txt", // Spanish characters
  9. "fichier.txt", // French characters
  10. "αρχείο.txt", // Greek characters
  11. "datei.txt", // German characters
  12. "fil.txt", // Swedish characters
  13. "קובץ.txt", // Hebrew characters
  14. "文件名.txt", // Chinese characters
  15. "файлы.txt", // Russian characters
  16. "फ़ाइलें.txt", // Hindi characters
  17. "📄_emoji.txt", // Emoji
  18. "file name with spaces.txt",
  19. "file-name-with-dashes.txt",
  20. "file_name_with_underscores.txt",
  21. "file.name.with.periods.txt",
  22. "file,name,with,commas.txt",
  23. "file;name;with;semicolons.txt",
  24. "file(name)with(parentheses).txt",
  25. "file[name]with[brackets].txt",
  26. "file{name}with{braces}.txt",
  27. "file!name!with!exclamations!.txt",
  28. "file@name@with@ats.txt",
  29. "file#name#with#hashes#.txt",
  30. "file$name$with$dollars$.txt",
  31. "file%name%with%percentages%.txt",
  32. "file^name^with^carats^.txt",
  33. "file&name&with&amps&.txt",
  34. "file*name*with*asterisks*.txt",
  35. "file_name_with_long_name_exceeding_255_characters_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.txt",
  36. "file👍name👍with👍thumbs👍up.txt",
  37. "file😂name😂with😂emojis😂.txt",
  38. "file🌍name🌍with🌍globe🌍emojis🌍.txt",
  39. "file🔥name🔥with🔥fire🔥emoji🔥.txt",
  40. "file🎉name🎉with🎉party🎉popper🎉emoji🎉.txt",
  41. "file💼name💼with💼briefcase💼emoji💼.txt",
  42. "file🍔name🍔with🍔burger🍔emoji🍔.txt",
  43. "file🚀name🚀with🚀rocket🚀emoji🚀.txt",
  44. "file👽name👽with👽alien👽emoji👽.txt",
  45. "file🌈name🌈with🌈rainbow🌈emoji🌈.txt",
  46. "file🍆name🍆with🍆eggplant🍆emoji🍆.txt",
  47. "file🍑name🍑with🍑peach🍑emoji🍑.txt",
  48. "invisible\u200Bname.txt", // Invisible Unicode character (Zero Width Space)
  49. "invisible\u200Cname.txt", // Invisible Unicode character (Zero Width Non-Joiner)
  50. "invisible\u200Dname.txt", // Invisible Unicode character (Zero Width Joiner)
  51. "invisible\uFEFFname.txt", // Invisible Unicode character (Zero Width No-Break Space)
  52. "invisible\u180Ename.txt", // Invisible Unicode character (Mongolian Vowel Separator)
  53. "hash#tag.txt",
  54. "percent%20encoded.txt",
  55. "plus+sign.txt",
  56. "ampersand&symbol.txt",
  57. "at@symbol.txt",
  58. "parentheses(1).txt",
  59. "brackets[1].txt",
  60. "curly{braces}.txt",
  61. "angle<tags>.txt",
  62. "exclamation!point.txt",
  63. "question?mark.txt",
  64. "colon:separated.txt",
  65. "semicolon;separated.txt",
  66. "single'quote.txt",
  67. "double\"quote.txt",
  68. "backtick`char.txt",
  69. "tilde~sign.txt",
  70. "underscore_character.txt",
  71. "hyphen-character.txt",
  72. "equal=sign.txt",
  73. "plus+sign.txt",
  74. "asterisk*char.txt",
  75. "caret^char.txt",
  76. "percent%sign.txt",
  77. "dollar$sign.txt",
  78. "pound#sign.txt",
  79. "at@sign.txt",
  80. "exclamation!mark.txt",
  81. "question?mark.txt",
  82. "backslash\\char.txt",
  83. "pipe|char.txt",
  84. "colon:char.txt",
  85. "semicolon;char.txt",
  86. "quote'char.txt",
  87. "double\"quote.txt",
  88. "backtick`char.txt",
  89. "braces{char}.txt",
  90. "brackets[char].txt",
  91. "parentheses(char).txt",
  92. "angle<brackets>.txt",
  93. "ellipsis….txt",
  94. "accentué.txt",
  95. "ümlaut.txt",
  96. "tildeñ.txt",
  97. "çedilla.txt",
  98. "špecial.txt",
  99. "russianЯ.txt",
  100. "chinese中文.txt",
  101. "arabicعربى.txt",
  102. "hebrewעברית.txt",
  103. "japanese日本語.txt",
  104. "korean한국어.txt",
  105. "vietnameseTiếng Việt.txt",
  106. ]
  107. window.fsTests = [
  108. testFSWrite = async ()=>{
  109. try {
  110. let randName = puter.randName();
  111. const result = await puter.fs.write(randName, 'testValue');
  112. assert(result.uid, "Failed to write to file");
  113. pass("testFSWrite passed");
  114. // delete the file
  115. try {
  116. await puter.fs.delete(randName);
  117. } catch (error) {
  118. throw("testFSWrite failed to delete file:", error);
  119. }
  120. } catch (error) {
  121. if(puter.debugMode)
  122. console.log(error);
  123. throw("testFSWrite failed:", error);
  124. }
  125. },
  126. testFSRead = async ()=>{
  127. try {
  128. let randName = puter.randName();
  129. await puter.fs.write(randName, 'testValue');
  130. const result = await (await puter.fs.read(randName)).text();
  131. assert(result === 'testValue', "Failed to read from file");
  132. pass("testFSRead passed");
  133. // delete the file
  134. try {
  135. await puter.fs.delete(randName);
  136. } catch (error) {
  137. fail("testFSRead failed to delete file:", error);
  138. }
  139. } catch (error) {
  140. fail("testFSRead failed:", error);
  141. }
  142. },
  143. testFSWriteWithoutData = async ()=>{
  144. try {
  145. let randName = puter.randName();
  146. const result = await puter.fs.write(randName);
  147. assert(result.uid, "Failed to write to file");
  148. pass("testFSWriteWithoutData passed");
  149. if(randName !== result.name) {
  150. fail(`testFSWriteWithoutData failed: Names do not match ${randName} ${result.name}`);
  151. }
  152. // delete the file
  153. try {
  154. await puter.fs.delete(randName);
  155. } catch (error) {
  156. fail("testFSWriteWithoutData failed to delete file:", error);
  157. }
  158. } catch (error) {
  159. fail("testFSWriteWithoutData failed:", error);
  160. }
  161. },
  162. testFSReadWithoutData = async ()=>{
  163. try {
  164. let randName = puter.randName();
  165. await puter.fs.write(randName);
  166. const result = await (await puter.fs.read(randName)).text();
  167. assert(result === '', "Failed to read from file");
  168. pass("testFSReadWithoutData passed");
  169. // delete the file
  170. try {
  171. await puter.fs.delete(randName);
  172. } catch (error) {
  173. fail("testFSReadWithoutData failed to delete file:", error);
  174. }
  175. } catch (error) {
  176. fail("testFSReadWithoutData failed:", error);
  177. }
  178. },
  179. testFSWriteToExistingFile = async ()=>{
  180. try {
  181. let randName = puter.randName();
  182. await puter.fs.write(randName, 'testValue');
  183. const result = await puter.fs.write(randName, 'updatedValue');
  184. assert(result.uid, "Failed to write to file");
  185. pass("testFSWriteToExistingFile passed");
  186. // delete the file
  187. try {
  188. await puter.fs.delete(randName);
  189. } catch (error) {
  190. fail("testFSWriteToExistingFile failed to delete file:", error);
  191. }
  192. } catch (error) {
  193. fail("testFSWriteToExistingFile failed:", error);
  194. }
  195. },
  196. testFSWriteToExistingFileWithoutOverwriteAndDedupe = async ()=>{
  197. try {
  198. let randName = puter.randName();
  199. await puter.fs.write(randName, 'testValue');
  200. const result = await puter.fs.write(randName, 'updatedValue', { overwrite: false, dedupeName: false });
  201. assert(!result.uid, "Failed to write to file");
  202. fail("testFSWriteToExistingFileWithoutOverwriteAndDedupe failed");
  203. // delete the file
  204. try {
  205. await puter.fs.delete(randName);
  206. } catch (error) {
  207. fail("testFSWriteToExistingFileWithoutOverwriteAndDedupe failed to delete file:", error);
  208. }
  209. } catch (error) {
  210. pass("testFSWriteToExistingFileWithoutOverwriteAndDedupe passed");
  211. }
  212. },
  213. testFSWriteToExistingFileWithoutOverwriteButWithDedupe = async ()=>{
  214. try {
  215. let randName = puter.randName();
  216. await puter.fs.write(randName, 'testValue');
  217. const result = await puter.fs.write(randName, 'updatedValue', { overwrite: false, dedupeName: true });
  218. assert(result.uid, "Failed to write to file");
  219. pass("testFSWriteToExistingFileWithoutOverwriteButWithDedupe passed");
  220. // delete the file
  221. try {
  222. await puter.fs.delete(randName);
  223. } catch (error) {
  224. fail("testFSWriteToExistingFileWithoutOverwriteButWithDedupe failed to delete file:", error);
  225. }
  226. } catch (error) {
  227. fail("testFSWriteToExistingFileWithoutOverwriteButWithDedupe failed:", error);
  228. }
  229. },
  230. testFSWriteToExistingFileWithOverwriteButWithoutDedupe = async ()=>{
  231. try {
  232. let randName = puter.randName();
  233. await puter.fs.write(randName, 'testValue');
  234. const result = await puter.fs.write(randName, 'updatedValue', { overwrite: true, dedupeName: false });
  235. assert(result.uid, "Failed to write to file");
  236. pass("testFSWriteToExistingFileWithOverwriteButWithoutDedupe passed");
  237. // delete the file
  238. try {
  239. await puter.fs.delete(randName);
  240. } catch (error) {
  241. fail("testFSWriteToExistingFileWithOverwriteButWithoutDedupe failed to delete file:", error);
  242. }
  243. } catch (error) {
  244. fail("testFSWriteToExistingFileWithOverwriteButWithoutDedupe failed:", error);
  245. }
  246. },
  247. // create a directory
  248. testFSCreateDir = async ()=>{
  249. try {
  250. let randName = puter.randName();
  251. const result = await puter.fs.mkdir(randName);
  252. assert(result.uid, "Failed to create directory");
  253. pass("testFSCreateDir passed");
  254. } catch (error) {
  255. fail("testFSCreateDir failed:", error);
  256. }
  257. },
  258. // write a number of files to a directory and list them
  259. testFSReadDir = async ()=>{
  260. try {
  261. let randName = puter.randName();
  262. await puter.fs.mkdir(randName);
  263. await puter.fs.write(randName + '/file1', 'testValue');
  264. await puter.fs.write(randName + '/file2', 'testValue');
  265. await puter.fs.write(randName + '/file3', 'testValue');
  266. const result = await puter.fs.readdir(randName);
  267. assert(result.length === 3, "Failed to read directory");
  268. pass("testFSReadDir passed");
  269. } catch (error) {
  270. fail("testFSReadDir failed:", error);
  271. }
  272. },
  273. // create a file then delete it
  274. testFSDelete = async ()=>{
  275. try {
  276. let randName = puter.randName();
  277. await puter.fs.write(randName, 'testValue');
  278. const result = await puter.fs.delete(randName);
  279. assert(!result.uid, "Failed to delete file");
  280. pass("testFSDelete passed");
  281. } catch (error) {
  282. fail("testFSDelete failed:", error);
  283. }
  284. },
  285. // create a directory, write a number of files to it, then delete it
  286. testFSDeleteDir = async ()=>{
  287. try {
  288. let randName = puter.randName();
  289. await puter.fs.mkdir(randName);
  290. await puter.fs.write(randName + '/file1', 'testValue');
  291. await puter.fs.write(randName + '/file2', 'testValue');
  292. await puter.fs.write(randName + '/file3', 'testValue');
  293. const result = await puter.fs.delete(randName);
  294. assert(!result.uid, "Failed to delete directory");
  295. pass("testFSDeleteDir passed");
  296. } catch (error) {
  297. fail("testFSDeleteDir failed:", error);
  298. }
  299. },
  300. // attempt to delete a non-existent file
  301. testFSDeleteNonExistentFile = async ()=>{
  302. try {
  303. let randName = puter.randName();
  304. const result = await puter.fs.delete(randName);
  305. assert(!result.uid, "Failed to delete non-existent file");
  306. pass("testFSDeleteNonExistentFile passed");
  307. } catch (error) {
  308. if(error.code !== "subject_does_not_exist")
  309. fail("testFSDeleteNonExistentFile failed:", error);
  310. else
  311. pass("testFSDeleteNonExistentFile passed");
  312. }
  313. },
  314. // attempt to access a non-existent file
  315. testFSReadNonExistentFile = async ()=>{
  316. try {
  317. let randName = puter.randName();
  318. const result = await puter.fs.read(randName);
  319. fail("testFSReadNonExistentFile failed");
  320. } catch (error) {
  321. if(error.code !== "subject_does_not_exist")
  322. fail("testFSReadNonExistentFile failed:", error);
  323. else
  324. pass("testFSReadNonExistentFile passed");
  325. }
  326. },
  327. testFSWriteWithSpecialCharacters = async ()=>{
  328. let randName
  329. try {
  330. randName = 'testFileWithSpecialCharacte rs!@#$%^&*()_+{}|:"<>?`~'
  331. const result = await puter.fs.write(randName, 'testValue', { specialCharacters: true });
  332. assert(result.uid, "Failed to write to file");
  333. pass("testFSWriteWithSpecialCharacters passed");
  334. } catch (error) {
  335. fail("testFSWriteWithSpecialCharacters failed:", error);
  336. }
  337. // delete the file
  338. try {
  339. await puter.fs.delete(randName);
  340. } catch (error) {
  341. fail("testFSWriteWithSpecialCharacters failed to delete file:", error);
  342. }
  343. },
  344. testFSReadWithSpecialCharacters = async ()=>{
  345. try {
  346. let randName = 'testFileWithSpecialCharacte rs!@#$%^&*()_+{}|:"<>?`~'
  347. await puter.fs.write(randName, 'testValue');
  348. const result = await (await puter.fs.read(randName)).text();
  349. assert(result === 'testValue', "Failed to read from file");
  350. pass("testFSReadWithSpecialCharacters passed");
  351. } catch (error) {
  352. fail("testFSReadWithSpecialCharacters failed:", error);
  353. }
  354. },
  355. testFSWriteLargeFile = async ()=>{
  356. try {
  357. let randName = puter.randName();
  358. const result = await puter.fs.write(randName, 'testValue'.repeat(100000));
  359. assert(result.uid, "Failed to write to file");
  360. pass("testFSWriteLargeFile passed");
  361. } catch (error) {
  362. fail("testFSWriteLargeFile failed:", error);
  363. }
  364. },
  365. testFSReadLargeFile = async ()=>{
  366. try {
  367. let randName = puter.randName();
  368. await puter.fs.write(randName, 'testValue'.repeat(100000));
  369. const result = await (await puter.fs.read(randName)).text();
  370. assert(result === 'testValue'.repeat(100000), "Failed to read from file");
  371. pass("testFSReadLargeFile passed");
  372. } catch (error) {
  373. fail("testFSReadLargeFile failed:", error);
  374. }
  375. },
  376. testFSRenameFile = async ()=>{
  377. try {
  378. let randName = puter.randName();
  379. let randName2 = puter.randName();
  380. await puter.fs.write(randName, 'testValue');
  381. const result = await puter.fs.rename(randName, randName2);
  382. assert(result.name, "Failed to rename file");
  383. pass("testFSRenameFile passed");
  384. // check that the old file is gone
  385. try {
  386. await puter.fs.read(randName);
  387. fail("testFSRenameFile failed to delete old file");
  388. } catch (error) {
  389. if(error.code !== "subject_does_not_exist")
  390. fail("testFSRenameFile failed to delete old file:", error);
  391. else
  392. pass("testFSRenameFile passed");
  393. }
  394. } catch (error) {
  395. fail("testFSRenameFile failed:", error);
  396. }
  397. },
  398. testFSMoveFile = async ()=>{
  399. try {
  400. let randName = puter.randName();
  401. let randName2 = puter.randName();
  402. await puter.fs.write(randName, 'testValue');
  403. await puter.fs.mkdir(randName2);
  404. let result = await puter.fs.move(randName, randName2);
  405. assert(result.moved, "Failed to move file");
  406. // check that the old file is gone
  407. try {
  408. await puter.fs.read(randName);
  409. fail("testFSMoveFile failed to delete old file");
  410. } catch (error) {
  411. if(error.code !== "subject_does_not_exist")
  412. fail("testFSMoveFile failed to delete old file:", error);
  413. else
  414. pass("testFSMoveFile passed");
  415. }
  416. } catch (error) {
  417. fail("testFSMoveFile failed:", error);
  418. }
  419. },
  420. testFSCopyFile = async ()=>{
  421. try {
  422. let randName = puter.randName();
  423. let randName2 = puter.randName();
  424. await puter.fs.write(randName, 'testValue');
  425. await puter.fs.mkdir(randName2);
  426. let result = await puter.fs.copy(randName, randName2);
  427. assert(Array.isArray(result) && result[0].uid, "Failed to copy file");
  428. // check that the old file is still there
  429. try {
  430. await puter.fs.read(randName);
  431. pass("testFSCopyFile passed");
  432. } catch (error) {
  433. fail("testFSCopyFile failed to keep old file:", error);
  434. }
  435. } catch (error) {
  436. fail("testFSCopyFile failed:", error);
  437. }
  438. },
  439. // copy a file to a directory with newName option
  440. testFSCopyFileWithNewName = async ()=>{
  441. try {
  442. let randName = puter.randName();
  443. let randName2 = puter.randName();
  444. await puter.fs.write(randName, 'testValue');
  445. await puter.fs.mkdir(randName2);
  446. let result = await puter.fs.copy(randName, randName2, { newName: 'newName' });
  447. assert(Array.isArray(result) && result[0].uid, "Failed to copy file");
  448. // check file name
  449. assert(result[0].name === 'newName', "Failed to copy file with new name");
  450. // check that the old file is still there
  451. try {
  452. await puter.fs.read(randName);
  453. pass("testFSCopyFileWithNewName passed");
  454. } catch (error) {
  455. fail("testFSCopyFileWithNewName failed to keep old file:", error);
  456. }
  457. } catch (error) {
  458. fail("testFSCopyFileWithNewName failed:", error);
  459. }
  460. },
  461. testFSStat = async ()=>{
  462. try {
  463. let randName = puter.randName();
  464. await puter.fs.write(randName, 'testValue');
  465. let result = await puter.fs.stat(randName);
  466. assert(result.uid, "Failed to stat file");
  467. pass("testFSStat passed");
  468. } catch (error) {
  469. fail("testFSStat failed:", error);
  470. }
  471. },
  472. testFSStatDir = async ()=>{
  473. try {
  474. let randName = puter.randName();
  475. await puter.fs.mkdir(randName);
  476. let result = await puter.fs.stat(randName);
  477. assert(result.uid, "Failed to stat directory");
  478. pass("testFSStatDir passed");
  479. } catch (error) {
  480. fail("testFSStatDir failed:", error);
  481. }
  482. },
  483. testFSStatNonExistent = async ()=>{
  484. try {
  485. let randName = puter.randName();
  486. let result = await puter.fs.stat(randName);
  487. fail("testFSStatNonExistent failed");
  488. } catch (error) {
  489. if(error.code !== "subject_does_not_exist")
  490. fail("testFSStatNonExistent failed:", error);
  491. else
  492. pass("testFSStatNonExistent passed");
  493. }
  494. },
  495. // create a directory, write a number of files to it, then delete it
  496. testFSDeleteDirWithFiles = async ()=>{
  497. try {
  498. let randName = puter.randName();
  499. await puter.fs.mkdir(randName);
  500. await puter.fs.write(randName + '/file1', 'testValue');
  501. await puter.fs.write(randName + '/file2', 'testValue');
  502. await puter.fs.write(randName + '/file3', 'testValue');
  503. const result = await puter.fs.delete(randName, { recursive: true });
  504. assert(!result.uid, "Failed to delete directory");
  505. pass("testFSDeleteDirWithFiles passed");
  506. } catch (error) {
  507. fail("testFSDeleteDirWithFiles failed:", error);
  508. }
  509. },
  510. // check if stat on a directory returns name, path, is_dir
  511. testFSStatDirReturnsAttrs = async ()=>{
  512. try {
  513. let randName = puter.randName();
  514. await puter.fs.mkdir(randName);
  515. let result = await puter.fs.stat(randName);
  516. assert(result.name && typeof result.name === 'string', "Failed to stat directory (name)");
  517. assert(result.path && typeof result.path === 'string', "Failed to stat directory (path)");
  518. assert(result.immutable !== undefined, "Failed to stat directory (immutable)");
  519. assert(result.metadata !== undefined, "Failed to stat directory (metadata)");
  520. assert(result.modified !== undefined, "Failed to stat directory (modified)");
  521. assert(result.created !== undefined, "Failed to stat directory (created)");
  522. assert(result.accessed !== undefined, "Failed to stat directory (accessed)");
  523. assert(result.size !== undefined, "Failed to stat directory (size)");
  524. assert(result.layout !== undefined, "Failed to stat directory (layout)");
  525. assert(result.owner !== undefined && typeof result.owner === 'object', "Failed to stat directory (owner)");
  526. assert(result.dirname !== undefined && typeof result.dirname === 'string', "Failed to stat directory (dirname)");
  527. assert(result.parent_id !== undefined && typeof result.parent_id === 'string', "Failed to stat directory (parent_id)");
  528. // todo this will fail for now until is_dir is turned into boolean
  529. assert(result.is_dir !== undefined && typeof result.is_dir === 'boolean' && result.is_dir === true, "Failed to stat directory (is_dir)");
  530. assert(result.is_empty !== undefined && typeof result.is_empty === 'boolean', "Failed to stat directory (is_empty)");
  531. pass("testFSStatDirReturnsAttrs passed");
  532. } catch (error) {
  533. throw("testFSStatDirReturnsAttrs failed:", error);
  534. }
  535. },
  536. // test read() with the object returned by write()
  537. testFSReadWithWriteResult = async ()=>{
  538. try {
  539. let randName = puter.randName();
  540. let writeResult = await puter.fs.write(randName, 'testValue');
  541. let result = await (await puter.fs.read(writeResult)).text();
  542. assert(result === 'testValue', "Failed to read from file");
  543. pass("testFSReadWithWriteResult passed");
  544. // delete the file
  545. try {
  546. await puter.fs.delete(randName);
  547. } catch (error) {
  548. fail("testFSReadWithWriteResult failed to delete file:", error);
  549. }
  550. } catch (error) {
  551. fail("testFSReadWithWriteResult failed:", error);
  552. }
  553. },
  554. // test stat() with the object returned by write()
  555. testFSStatWithWriteResult = async ()=>{
  556. try {
  557. let randName = puter.randName();
  558. let writeResult = await puter.fs.write(randName, 'testValue');
  559. let result = await puter.fs.stat(writeResult);
  560. assert(result.uid, "Failed to stat file");
  561. pass("testFSStatWithWriteResult passed");
  562. // delete the file
  563. try {
  564. await puter.fs.delete(randName);
  565. } catch (error) {
  566. fail("testFSStatWithWriteResult failed to delete file:", error);
  567. }
  568. } catch (error) {
  569. fail("testFSStatWithWriteResult failed:", error);
  570. }
  571. },
  572. // test creating files with names from naughtyStrings
  573. testFSWriteWithNaughtyStrings = async ()=>{
  574. try {
  575. let randName = puter.randName();
  576. for(let i = 0; i < naughtyStrings.length; i++) {
  577. let filename = randName + naughtyStrings[i];
  578. let result = await puter.fs.write(filename, 'testValue');
  579. assert(result.uid, "Failed to write to file");
  580. // check name
  581. assert(result.name === filename, "Failed to write to file with naughty name: " + filename);
  582. // delete the file
  583. try {
  584. await puter.fs.delete(filename);
  585. } catch (error) {
  586. fail("testFSWriteWithNaughtyStrings failed to delete file: " + filename, error);
  587. }
  588. }
  589. pass("testFSWriteWithNaughtyStrings passed");
  590. } catch (error) {
  591. console.log(error);
  592. fail("testFSWriteWithNaughtyStrings failed:", error);
  593. }
  594. },
  595. ];