fs.test.js 25 KB

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