|
| 1 | +import test from "ava"; |
| 2 | +import { Bash } from "../wrapper.js"; |
| 3 | + |
| 4 | +// ============================================================================ |
| 5 | +// VFS — readFile / writeFile |
| 6 | +// ============================================================================ |
| 7 | + |
| 8 | +test("writeFile + readFile roundtrip", (t) => { |
| 9 | + const bash = new Bash(); |
| 10 | + bash.writeFile("/tmp/hello.txt", "Hello, VFS!"); |
| 11 | + t.is(bash.readFile("/tmp/hello.txt"), "Hello, VFS!"); |
| 12 | +}); |
| 13 | + |
| 14 | +test("writeFile overwrites existing content", (t) => { |
| 15 | + const bash = new Bash(); |
| 16 | + bash.writeFile("/tmp/over.txt", "first"); |
| 17 | + bash.writeFile("/tmp/over.txt", "second"); |
| 18 | + t.is(bash.readFile("/tmp/over.txt"), "second"); |
| 19 | +}); |
| 20 | + |
| 21 | +test("readFile throws on missing file", (t) => { |
| 22 | + const bash = new Bash(); |
| 23 | + t.throws(() => bash.readFile("/nonexistent/file.txt")); |
| 24 | +}); |
| 25 | + |
| 26 | +test("writeFile preserves binary-like content", (t) => { |
| 27 | + const bash = new Bash(); |
| 28 | + const content = "line1\nline2\n\ttabbed\n"; |
| 29 | + bash.writeFile("/tmp/multi.txt", content); |
| 30 | + t.is(bash.readFile("/tmp/multi.txt"), content); |
| 31 | +}); |
| 32 | + |
| 33 | +test("writeFile with empty content", (t) => { |
| 34 | + const bash = new Bash(); |
| 35 | + bash.writeFile("/tmp/empty.txt", ""); |
| 36 | + t.is(bash.readFile("/tmp/empty.txt"), ""); |
| 37 | +}); |
| 38 | + |
| 39 | +// ============================================================================ |
| 40 | +// VFS — mkdir |
| 41 | +// ============================================================================ |
| 42 | + |
| 43 | +test("mkdir creates directory", (t) => { |
| 44 | + const bash = new Bash(); |
| 45 | + bash.mkdir("/tmp/newdir"); |
| 46 | + t.true(bash.exists("/tmp/newdir")); |
| 47 | +}); |
| 48 | + |
| 49 | +test("mkdir recursive creates parent chain", (t) => { |
| 50 | + const bash = new Bash(); |
| 51 | + bash.mkdir("/a/b/c/d", true); |
| 52 | + t.true(bash.exists("/a/b/c/d")); |
| 53 | + t.true(bash.exists("/a/b/c")); |
| 54 | + t.true(bash.exists("/a/b")); |
| 55 | +}); |
| 56 | + |
| 57 | +test("mkdir non-recursive fails without parent", (t) => { |
| 58 | + const bash = new Bash(); |
| 59 | + t.throws(() => bash.mkdir("/x/y/z")); |
| 60 | +}); |
| 61 | + |
| 62 | +// ============================================================================ |
| 63 | +// VFS — exists |
| 64 | +// ============================================================================ |
| 65 | + |
| 66 | +test("exists returns false for missing path", (t) => { |
| 67 | + const bash = new Bash(); |
| 68 | + t.false(bash.exists("/does/not/exist")); |
| 69 | +}); |
| 70 | + |
| 71 | +test("exists returns true for file", (t) => { |
| 72 | + const bash = new Bash(); |
| 73 | + bash.writeFile("/tmp/e.txt", "x"); |
| 74 | + t.true(bash.exists("/tmp/e.txt")); |
| 75 | +}); |
| 76 | + |
| 77 | +test("exists returns true for directory", (t) => { |
| 78 | + const bash = new Bash(); |
| 79 | + bash.mkdir("/tmp/edir"); |
| 80 | + t.true(bash.exists("/tmp/edir")); |
| 81 | +}); |
| 82 | + |
| 83 | +// ============================================================================ |
| 84 | +// VFS — remove |
| 85 | +// ============================================================================ |
| 86 | + |
| 87 | +test("remove deletes a file", (t) => { |
| 88 | + const bash = new Bash(); |
| 89 | + bash.writeFile("/tmp/rm.txt", "bye"); |
| 90 | + t.true(bash.exists("/tmp/rm.txt")); |
| 91 | + bash.remove("/tmp/rm.txt"); |
| 92 | + t.false(bash.exists("/tmp/rm.txt")); |
| 93 | +}); |
| 94 | + |
| 95 | +test("remove recursive deletes directory tree", (t) => { |
| 96 | + const bash = new Bash(); |
| 97 | + bash.mkdir("/tmp/tree/sub", true); |
| 98 | + bash.writeFile("/tmp/tree/sub/f.txt", "data"); |
| 99 | + bash.remove("/tmp/tree", true); |
| 100 | + t.false(bash.exists("/tmp/tree")); |
| 101 | +}); |
| 102 | + |
| 103 | +test("remove throws on missing path", (t) => { |
| 104 | + const bash = new Bash(); |
| 105 | + t.throws(() => bash.remove("/no/such/file")); |
| 106 | +}); |
| 107 | + |
| 108 | +// ============================================================================ |
| 109 | +// VFS ↔ bash interop |
| 110 | +// ============================================================================ |
| 111 | + |
| 112 | +test("bash executeSync sees VFS-written files", (t) => { |
| 113 | + const bash = new Bash(); |
| 114 | + bash.writeFile("/tmp/from-vfs.txt", "vfs-content"); |
| 115 | + const r = bash.executeSync("cat /tmp/from-vfs.txt"); |
| 116 | + t.is(r.stdout, "vfs-content"); |
| 117 | +}); |
| 118 | + |
| 119 | +test("readFile sees bash-created files", (t) => { |
| 120 | + const bash = new Bash(); |
| 121 | + bash.executeSync("echo bash-content > /tmp/from-bash.txt"); |
| 122 | + t.is(bash.readFile("/tmp/from-bash.txt"), "bash-content\n"); |
| 123 | +}); |
| 124 | + |
| 125 | +test("VFS mkdir makes directory visible to bash ls", (t) => { |
| 126 | + const bash = new Bash(); |
| 127 | + bash.mkdir("/project/src/lib", true); |
| 128 | + bash.writeFile("/project/src/lib/mod.rs", "// rust"); |
| 129 | + const r = bash.executeSync("ls /project/src/lib/"); |
| 130 | + t.is(r.stdout.trim(), "mod.rs"); |
| 131 | +}); |
| 132 | + |
| 133 | +test("bash mkdir makes directory visible to VFS exists", (t) => { |
| 134 | + const bash = new Bash(); |
| 135 | + bash.executeSync("mkdir -p /project/pkg"); |
| 136 | + t.true(bash.exists("/project/pkg")); |
| 137 | +}); |
| 138 | + |
| 139 | +test("reset clears VFS state", (t) => { |
| 140 | + const bash = new Bash(); |
| 141 | + bash.writeFile("/tmp/persist.txt", "data"); |
| 142 | + t.true(bash.exists("/tmp/persist.txt")); |
| 143 | + bash.reset(); |
| 144 | + t.false(bash.exists("/tmp/persist.txt")); |
| 145 | +}); |
0 commit comments