|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { BashEnv } from "../../BashEnv.js"; |
| 3 | + |
| 4 | +describe("base64", () => { |
| 5 | + describe("encoding", () => { |
| 6 | + it("should encode string from stdin", async () => { |
| 7 | + const env = new BashEnv(); |
| 8 | + const result = await env.exec('echo -n "hello" | base64'); |
| 9 | + expect(result.stdout).toBe("aGVsbG8=\n"); |
| 10 | + expect(result.stderr).toBe(""); |
| 11 | + expect(result.exitCode).toBe(0); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should encode string with newline", async () => { |
| 15 | + const env = new BashEnv(); |
| 16 | + const result = await env.exec('echo "hello" | base64'); |
| 17 | + expect(result.stdout).toBe("aGVsbG8K\n"); |
| 18 | + expect(result.stderr).toBe(""); |
| 19 | + expect(result.exitCode).toBe(0); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should encode file contents", async () => { |
| 23 | + const env = new BashEnv({ |
| 24 | + files: { "/test.txt": "hello" }, |
| 25 | + }); |
| 26 | + const result = await env.exec("base64 /test.txt"); |
| 27 | + expect(result.stdout).toBe("aGVsbG8=\n"); |
| 28 | + expect(result.stderr).toBe(""); |
| 29 | + expect(result.exitCode).toBe(0); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should wrap lines at 76 characters by default", async () => { |
| 33 | + const env = new BashEnv(); |
| 34 | + // Long input that will produce > 76 chars of base64 |
| 35 | + const result = await env.exec( |
| 36 | + 'echo -n "This is a very long string that will definitely produce more than 76 characters of base64 output" | base64', |
| 37 | + ); |
| 38 | + const lines = result.stdout.split("\n").filter((l) => l); |
| 39 | + expect(lines[0].length).toBeLessThanOrEqual(76); |
| 40 | + expect(result.exitCode).toBe(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should not wrap with -w 0", async () => { |
| 44 | + const env = new BashEnv(); |
| 45 | + const result = await env.exec( |
| 46 | + 'echo -n "This is a very long string that will definitely produce more than 76 characters of base64 output" | base64 -w 0', |
| 47 | + ); |
| 48 | + // Should be single line (no newline wrapping, just trailing newline) |
| 49 | + expect(result.stdout).not.toContain("\n\n"); |
| 50 | + expect(result.exitCode).toBe(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should wrap at custom width", async () => { |
| 54 | + const env = new BashEnv(); |
| 55 | + const result = await env.exec( |
| 56 | + 'echo -n "hello world test" | base64 -w 10', |
| 57 | + ); |
| 58 | + const lines = result.stdout.split("\n").filter((l) => l); |
| 59 | + for (const line of lines.slice(0, -1)) { |
| 60 | + expect(line.length).toBe(10); |
| 61 | + } |
| 62 | + expect(result.exitCode).toBe(0); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("decoding", () => { |
| 67 | + it("should decode base64 with -d", async () => { |
| 68 | + const env = new BashEnv(); |
| 69 | + const result = await env.exec('echo "aGVsbG8=" | base64 -d'); |
| 70 | + expect(result.stdout).toBe("hello"); |
| 71 | + expect(result.stderr).toBe(""); |
| 72 | + expect(result.exitCode).toBe(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should decode base64 with --decode", async () => { |
| 76 | + const env = new BashEnv(); |
| 77 | + const result = await env.exec('echo "aGVsbG8=" | base64 --decode'); |
| 78 | + expect(result.stdout).toBe("hello"); |
| 79 | + expect(result.stderr).toBe(""); |
| 80 | + expect(result.exitCode).toBe(0); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should decode base64 from file", async () => { |
| 84 | + const env = new BashEnv({ |
| 85 | + files: { "/encoded.txt": "aGVsbG8gd29ybGQ=" }, |
| 86 | + }); |
| 87 | + const result = await env.exec("base64 -d /encoded.txt"); |
| 88 | + expect(result.stdout).toBe("hello world"); |
| 89 | + expect(result.stderr).toBe(""); |
| 90 | + expect(result.exitCode).toBe(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should ignore whitespace when decoding", async () => { |
| 94 | + const env = new BashEnv(); |
| 95 | + const result = await env.exec('echo "aGVs\nbG8=" | base64 -d'); |
| 96 | + expect(result.stdout).toBe("hello"); |
| 97 | + expect(result.exitCode).toBe(0); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should roundtrip encode then decode", async () => { |
| 101 | + const env = new BashEnv(); |
| 102 | + const result = await env.exec( |
| 103 | + 'echo -n "test data 123" | base64 | base64 -d', |
| 104 | + ); |
| 105 | + expect(result.stdout).toBe("test data 123"); |
| 106 | + expect(result.exitCode).toBe(0); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe("error handling", () => { |
| 111 | + it("should error on missing file", async () => { |
| 112 | + const env = new BashEnv(); |
| 113 | + const result = await env.exec("base64 /nonexistent.txt"); |
| 114 | + expect(result.stdout).toBe(""); |
| 115 | + expect(result.stderr).toBe( |
| 116 | + "base64: /nonexistent.txt: No such file or directory\n", |
| 117 | + ); |
| 118 | + expect(result.exitCode).toBe(1); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should error on unknown option", async () => { |
| 122 | + const env = new BashEnv(); |
| 123 | + const result = await env.exec("base64 --unknown"); |
| 124 | + expect(result.stderr).toContain("unrecognized option"); |
| 125 | + expect(result.exitCode).toBe(1); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should error on unknown short option", async () => { |
| 129 | + const env = new BashEnv(); |
| 130 | + const result = await env.exec("base64 -z"); |
| 131 | + expect(result.stderr).toContain("invalid option"); |
| 132 | + expect(result.exitCode).toBe(1); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe("help", () => { |
| 137 | + it("should show help with --help", async () => { |
| 138 | + const env = new BashEnv(); |
| 139 | + const result = await env.exec("base64 --help"); |
| 140 | + expect(result.stdout).toContain("base64"); |
| 141 | + expect(result.stdout).toContain("decode"); |
| 142 | + expect(result.exitCode).toBe(0); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe("stdin placeholder", () => { |
| 147 | + it("should read from stdin with -", async () => { |
| 148 | + const env = new BashEnv(); |
| 149 | + const result = await env.exec('echo -n "test" | base64 -'); |
| 150 | + expect(result.stdout).toBe("dGVzdA==\n"); |
| 151 | + expect(result.exitCode).toBe(0); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments