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. console.log(error);
  122. throw("testFSWrite failed:", error);
  123. }
  124. },
  125. testFSRead = async ()=>{
  126. try {
  127. let randName = puter.randName();
  128. await puter.fs.write(randName, 'testValue');
  129. const result = await (await puter.fs.read(randName)).text();
  130. assert(result === 'testValue', "Failed to read from file");
  131. pass("testFSRead passed");
  132. // delete the file
  133. try {
  134. await puter.fs.delete(randName);
  135. } catch (error) {
  136. fail("testFSRead failed to delete file:", error);
  137. }
  138. } catch (error) {
  139. fail("testFSRead failed:", error);
  140. }
  141. },
  142. testFSWriteWithoutData = async ()=>{
  143. try {
  144. let randName = puter.randName();
  145. const result = await puter.fs.write(randName);
  146. assert(result.uid, "Failed to write to file");
  147. pass("testFSWriteWithoutData passed");
  148. if(randName !== result.name) {
  149. fail(`testFSWriteWithoutData failed: Names do not match ${randName} ${result.name}`);
  150. }
  151. // delete the file
  152. try {
  153. await puter.fs.delete(randName);
  154. } catch (error) {
  155. fail("testFSWriteWithoutData failed to delete file:", error);
  156. }
  157. } catch (error) {
  158. fail("testFSWriteWithoutData failed:", error);
  159. }
  160. },
  161. testFSReadWithoutData = async ()=>{
  162. try {
  163. let randName = puter.randName();
  164. await puter.fs.write(randName);
  165. const result = await (await puter.fs.read(randName)).text();
  166. assert(result === '', "Failed to read from file");
  167. pass("testFSReadWithoutData passed");
  168. // delete the file
  169. try {
  170. await puter.fs.delete(randName);
  171. } catch (error) {
  172. fail("testFSReadWithoutData failed to delete file:", error);
  173. }
  174. } catch (error) {
  175. fail("testFSReadWithoutData failed:", error);
  176. }
  177. },
  178. testFSWriteToExistingFile = async ()=>{
  179. try {
  180. let randName = puter.randName();
  181. await puter.fs.write(randName, 'testValue');
  182. const result = await puter.fs.write(randName, 'updatedValue');
  183. assert(result.uid, "Failed to write to file");
  184. pass("testFSWriteToExistingFile passed");
  185. // delete the file
  186. try {
  187. await puter.fs.delete(randName);
  188. } catch (error) {
  189. fail("testFSWriteToExistingFile failed to delete file:", error);
  190. }
  191. } catch (error) {
  192. fail("testFSWriteToExistingFile failed:", error);
  193. }
  194. },
  195. testFSWriteToExistingFileWithoutOverwriteAndDedupe = async ()=>{
  196. try {
  197. let randName = puter.randName();
  198. await puter.fs.write(randName, 'testValue');
  199. const result = await puter.fs.write(randName, 'updatedValue', { overwrite: false, dedupeName: false });
  200. assert(!result.uid, "Failed to write to file");
  201. fail("testFSWriteToExistingFileWithoutOverwriteAndDedupe failed");
  202. // delete the file
  203. try {
  204. await puter.fs.delete(randName);
  205. } catch (error) {
  206. fail("testFSWriteToExistingFileWithoutOverwriteAndDedupe failed to delete file:", error);
  207. }
  208. } catch (error) {
  209. pass("testFSWriteToExistingFileWithoutOverwriteAndDedupe passed");
  210. }
  211. },
  212. testFSWriteToExistingFileWithoutOverwriteButWithDedupe = async ()=>{
  213. try {
  214. let randName = puter.randName();
  215. await puter.fs.write(randName, 'testValue');
  216. const result = await puter.fs.write(randName, 'updatedValue', { overwrite: false, dedupeName: true });
  217. assert(result.uid, "Failed to write to file");
  218. pass("testFSWriteToExistingFileWithoutOverwriteButWithDedupe passed");
  219. // delete the file
  220. try {
  221. await puter.fs.delete(randName);
  222. } catch (error) {
  223. fail("testFSWriteToExistingFileWithoutOverwriteButWithDedupe failed to delete file:", error);
  224. }
  225. } catch (error) {
  226. fail("testFSWriteToExistingFileWithoutOverwriteButWithDedupe failed:", error);
  227. }
  228. },
  229. testFSWriteToExistingFileWithOverwriteButWithoutDedupe = async ()=>{
  230. try {
  231. let randName = puter.randName();
  232. await puter.fs.write(randName, 'testValue');
  233. const result = await puter.fs.write(randName, 'updatedValue', { overwrite: true, dedupeName: false });
  234. assert(result.uid, "Failed to write to file");
  235. pass("testFSWriteToExistingFileWithOverwriteButWithoutDedupe passed");
  236. // delete the file
  237. try {
  238. await puter.fs.delete(randName);
  239. } catch (error) {
  240. fail("testFSWriteToExistingFileWithOverwriteButWithoutDedupe failed to delete file:", error);
  241. }
  242. } catch (error) {
  243. fail("testFSWriteToExistingFileWithOverwriteButWithoutDedupe failed:", error);
  244. }
  245. },
  246. // create a directory
  247. testFSCreateDir = async ()=>{
  248. try {
  249. let randName = puter.randName();
  250. const result = await puter.fs.mkdir(randName);
  251. assert(result.uid, "Failed to create directory");
  252. pass("testFSCreateDir passed");
  253. } catch (error) {
  254. fail("testFSCreateDir failed:", error);
  255. }
  256. },
  257. // write a number of files to a directory and list them
  258. testFSReadDir = async ()=>{
  259. try {
  260. let randName = puter.randName();
  261. await puter.fs.mkdir(randName);
  262. await puter.fs.write(randName + '/file1', 'testValue');
  263. await puter.fs.write(randName + '/file2', 'testValue');
  264. await puter.fs.write(randName + '/file3', 'testValue');
  265. const result = await puter.fs.readdir(randName);
  266. assert(result.length === 3, "Failed to read directory");
  267. pass("testFSReadDir passed");
  268. } catch (error) {
  269. fail("testFSReadDir failed:", error);
  270. }
  271. },
  272. // create a file then delete it
  273. testFSDelete = async ()=>{
  274. try {
  275. let randName = puter.randName();
  276. await puter.fs.write(randName, 'testValue');
  277. const result = await puter.fs.delete(randName);
  278. assert(!result.uid, "Failed to delete file");
  279. pass("testFSDelete passed");
  280. } catch (error) {
  281. fail("testFSDelete failed:", error);
  282. }
  283. },
  284. // create a directory, write a number of files to it, then delete it
  285. testFSDeleteDir = async ()=>{
  286. try {
  287. let randName = puter.randName();
  288. await puter.fs.mkdir(randName);
  289. await puter.fs.write(randName + '/file1', 'testValue');
  290. await puter.fs.write(randName + '/file2', 'testValue');
  291. await puter.fs.write(randName + '/file3', 'testValue');
  292. const result = await puter.fs.delete(randName);
  293. assert(!result.uid, "Failed to delete directory");
  294. pass("testFSDeleteDir passed");
  295. } catch (error) {
  296. fail("testFSDeleteDir failed:", error);
  297. }
  298. },
  299. // attempt to delete a non-existent file
  300. testFSDeleteNonExistentFile = async ()=>{
  301. try {
  302. let randName = puter.randName();
  303. const result = await puter.fs.delete(randName);
  304. assert(!result.uid, "Failed to delete non-existent file");
  305. pass("testFSDeleteNonExistentFile passed");
  306. } catch (error) {
  307. if(error.code !== "subject_does_not_exist")
  308. fail("testFSDeleteNonExistentFile failed:", error);
  309. else
  310. pass("testFSDeleteNonExistentFile passed");
  311. }
  312. },
  313. // attempt to access a non-existent file
  314. testFSReadNonExistentFile = async ()=>{
  315. try {
  316. let randName = puter.randName();
  317. const result = await puter.fs.read(randName);
  318. fail("testFSReadNonExistentFile failed");
  319. } catch (error) {
  320. if(error.code !== "subject_does_not_exist")
  321. fail("testFSReadNonExistentFile failed:", error);
  322. else
  323. pass("testFSReadNonExistentFile passed");
  324. }
  325. },
  326. testFSWriteWithSpecialCharacters = async ()=>{
  327. let randName
  328. try {
  329. randName = 'testFileWithSpecialCharacte rs!@#$%^&*()_+{}|:"<>?`~'
  330. const result = await puter.fs.write(randName, 'testValue', { specialCharacters: true });
  331. assert(result.uid, "Failed to write to file");
  332. pass("testFSWriteWithSpecialCharacters passed");
  333. } catch (error) {
  334. fail("testFSWriteWithSpecialCharacters failed:", error);
  335. }
  336. // delete the file
  337. try {
  338. await puter.fs.delete(randName);
  339. } catch (error) {
  340. fail("testFSWriteWithSpecialCharacters failed to delete file:", error);
  341. }
  342. },
  343. testFSReadWithSpecialCharacters = async ()=>{
  344. try {
  345. let randName = 'testFileWithSpecialCharacte rs!@#$%^&*()_+{}|:"<>?`~'
  346. await puter.fs.write(randName, 'testValue');
  347. const result = await (await puter.fs.read(randName)).text();
  348. assert(result === 'testValue', "Failed to read from file");
  349. pass("testFSReadWithSpecialCharacters passed");
  350. } catch (error) {
  351. fail("testFSReadWithSpecialCharacters failed:", error);
  352. }
  353. },
  354. testFSWriteLargeFile = async ()=>{
  355. try {
  356. let randName = puter.randName();
  357. const result = await puter.fs.write(randName, 'testValue'.repeat(100000));
  358. assert(result.uid, "Failed to write to file");
  359. pass("testFSWriteLargeFile passed");
  360. } catch (error) {
  361. fail("testFSWriteLargeFile failed:", error);
  362. }
  363. },
  364. testFSReadLargeFile = async ()=>{
  365. try {
  366. let randName = puter.randName();
  367. await puter.fs.write(randName, 'testValue'.repeat(100000));
  368. const result = await (await puter.fs.read(randName)).text();
  369. assert(result === 'testValue'.repeat(100000), "Failed to read from file");
  370. pass("testFSReadLargeFile passed");
  371. } catch (error) {
  372. fail("testFSReadLargeFile failed:", error);
  373. }
  374. },
  375. testFSRenameFile = async ()=>{
  376. try {
  377. let randName = puter.randName();
  378. let randName2 = puter.randName();
  379. await puter.fs.write(randName, 'testValue');
  380. const result = await puter.fs.rename(randName, randName2);
  381. assert(result.name, "Failed to rename file");
  382. pass("testFSRenameFile passed");
  383. // check that the old file is gone
  384. try {
  385. await puter.fs.read(randName);
  386. fail("testFSRenameFile failed to delete old file");
  387. } catch (error) {
  388. if(error.code !== "subject_does_not_exist")
  389. fail("testFSRenameFile failed to delete old file:", error);
  390. else
  391. pass("testFSRenameFile passed");
  392. }
  393. } catch (error) {
  394. fail("testFSRenameFile failed:", error);
  395. }
  396. },
  397. testFSMoveFile = async ()=>{
  398. try {
  399. let randName = puter.randName();
  400. let randName2 = puter.randName();
  401. await puter.fs.write(randName, 'testValue');
  402. await puter.fs.mkdir(randName2);
  403. let result = await puter.fs.move(randName, randName2);
  404. assert(result.moved, "Failed to move file");
  405. // check that the old file is gone
  406. try {
  407. await puter.fs.read(randName);
  408. fail("testFSMoveFile failed to delete old file");
  409. } catch (error) {
  410. if(error.code !== "subject_does_not_exist")
  411. fail("testFSMoveFile failed to delete old file:", error);
  412. else
  413. pass("testFSMoveFile passed");
  414. }
  415. } catch (error) {
  416. fail("testFSMoveFile failed:", error);
  417. }
  418. },
  419. testFSCopyFile = async ()=>{
  420. try {
  421. let randName = puter.randName();
  422. let randName2 = puter.randName();
  423. await puter.fs.write(randName, 'testValue');
  424. await puter.fs.mkdir(randName2);
  425. let result = await puter.fs.copy(randName, randName2);
  426. assert(Array.isArray(result) && result[0].uid, "Failed to copy file");
  427. // check that the old file is still there
  428. try {
  429. await puter.fs.read(randName);
  430. pass("testFSCopyFile passed");
  431. } catch (error) {
  432. fail("testFSCopyFile failed to keep old file:", error);
  433. }
  434. } catch (error) {
  435. fail("testFSCopyFile failed:", error);
  436. }
  437. },
  438. // copy a file to a directory with newName option
  439. testFSCopyFileWithNewName = async ()=>{
  440. try {
  441. let randName = puter.randName();
  442. let randName2 = puter.randName();
  443. await puter.fs.write(randName, 'testValue');
  444. await puter.fs.mkdir(randName2);
  445. let result = await puter.fs.copy(randName, randName2, { newName: 'newName' });
  446. assert(Array.isArray(result) && result[0].uid, "Failed to copy file");
  447. // check file name
  448. assert(result[0].name === 'newName', "Failed to copy file with new name");
  449. // check that the old file is still there
  450. try {
  451. await puter.fs.read(randName);
  452. pass("testFSCopyFileWithNewName passed");
  453. } catch (error) {
  454. fail("testFSCopyFileWithNewName failed to keep old file:", error);
  455. }
  456. } catch (error) {
  457. fail("testFSCopyFileWithNewName failed:", error);
  458. }
  459. },
  460. testFSStat = async ()=>{
  461. try {
  462. let randName = puter.randName();
  463. await puter.fs.write(randName, 'testValue');
  464. let result = await puter.fs.stat(randName);
  465. assert(result.uid, "Failed to stat file");
  466. pass("testFSStat passed");
  467. } catch (error) {
  468. fail("testFSStat failed:", error);
  469. }
  470. },
  471. testFSStatDir = async ()=>{
  472. try {
  473. let randName = puter.randName();
  474. await puter.fs.mkdir(randName);
  475. let result = await puter.fs.stat(randName);
  476. assert(result.uid, "Failed to stat directory");
  477. pass("testFSStatDir passed");
  478. } catch (error) {
  479. fail("testFSStatDir failed:", error);
  480. }
  481. },
  482. testFSStatNonExistent = async ()=>{
  483. try {
  484. let randName = puter.randName();
  485. let result = await puter.fs.stat(randName);
  486. fail("testFSStatNonExistent failed");
  487. } catch (error) {
  488. if(error.code !== "subject_does_not_exist")
  489. fail("testFSStatNonExistent failed:", error);
  490. else
  491. pass("testFSStatNonExistent passed");
  492. }
  493. },
  494. // create a directory, write a number of files to it, then delete it
  495. testFSDeleteDirWithFiles = async ()=>{
  496. try {
  497. let randName = puter.randName();
  498. await puter.fs.mkdir(randName);
  499. await puter.fs.write(randName + '/file1', 'testValue');
  500. await puter.fs.write(randName + '/file2', 'testValue');
  501. await puter.fs.write(randName + '/file3', 'testValue');
  502. const result = await puter.fs.delete(randName, { recursive: true });
  503. assert(!result.uid, "Failed to delete directory");
  504. pass("testFSDeleteDirWithFiles passed");
  505. } catch (error) {
  506. fail("testFSDeleteDirWithFiles failed:", error);
  507. }
  508. },
  509. // check if stat on a directory returns name, path, is_dir
  510. testFSStatDirReturnsAttrs = async ()=>{
  511. try {
  512. let randName = puter.randName();
  513. await puter.fs.mkdir(randName);
  514. let result = await puter.fs.stat(randName);
  515. assert(result.name && typeof result.name === 'string', "Failed to stat directory (name)");
  516. assert(result.path && typeof result.path === 'string', "Failed to stat directory (path)");
  517. assert(result.immutable !== undefined, "Failed to stat directory (immutable)");
  518. assert(result.metadata !== undefined, "Failed to stat directory (metadata)");
  519. assert(result.modified !== undefined, "Failed to stat directory (modified)");
  520. assert(result.created !== undefined, "Failed to stat directory (created)");
  521. assert(result.accessed !== undefined, "Failed to stat directory (accessed)");
  522. assert(result.size !== undefined, "Failed to stat directory (size)");
  523. assert(result.layout !== undefined, "Failed to stat directory (layout)");
  524. assert(result.owner !== undefined && typeof result.owner === 'object', "Failed to stat directory (owner)");
  525. assert(result.dirname !== undefined && typeof result.dirname === 'string', "Failed to stat directory (dirname)");
  526. assert(result.parent_id !== undefined && typeof result.parent_id === 'string', "Failed to stat directory (parent_id)");
  527. // todo this will fail for now until is_dir is turned into boolean
  528. assert(result.is_dir !== undefined && typeof result.is_dir === 'boolean' && result.is_dir === true, "Failed to stat directory (is_dir)");
  529. assert(result.is_empty !== undefined && typeof result.is_empty === 'boolean', "Failed to stat directory (is_empty)");
  530. pass("testFSStatDirReturnsAttrs passed");
  531. } catch (error) {
  532. throw("testFSStatDirReturnsAttrs failed:", error);
  533. }
  534. },
  535. // test read() with the object returned by write()
  536. testFSReadWithWriteResult = async ()=>{
  537. try {
  538. let randName = puter.randName();
  539. let writeResult = await puter.fs.write(randName, 'testValue');
  540. let result = await (await puter.fs.read(writeResult)).text();
  541. assert(result === 'testValue', "Failed to read from file");
  542. pass("testFSReadWithWriteResult passed");
  543. // delete the file
  544. try {
  545. await puter.fs.delete(randName);
  546. } catch (error) {
  547. fail("testFSReadWithWriteResult failed to delete file:", error);
  548. }
  549. } catch (error) {
  550. fail("testFSReadWithWriteResult failed:", error);
  551. }
  552. },
  553. // test stat() with the object returned by write()
  554. testFSStatWithWriteResult = async ()=>{
  555. try {
  556. let randName = puter.randName();
  557. let writeResult = await puter.fs.write(randName, 'testValue');
  558. let result = await puter.fs.stat(writeResult);
  559. assert(result.uid, "Failed to stat file");
  560. pass("testFSStatWithWriteResult passed");
  561. // delete the file
  562. try {
  563. await puter.fs.delete(randName);
  564. } catch (error) {
  565. fail("testFSStatWithWriteResult failed to delete file:", error);
  566. }
  567. } catch (error) {
  568. fail("testFSStatWithWriteResult failed:", error);
  569. }
  570. },
  571. // test creating files with names from naughtyStrings
  572. testFSWriteWithNaughtyStrings = async ()=>{
  573. try {
  574. let randName = puter.randName();
  575. for(let i = 0; i < naughtyStrings.length; i++) {
  576. let filename = randName + naughtyStrings[i];
  577. console.log(filename);
  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. ];