|
1 | 1 | import { Bash } from 'just-bash'; |
2 | 2 | import { fs as zenfs, InMemory, configure } from '@zenfs/core'; |
| 3 | +import { ZenFsAdapter } from '../src/ZenFsAdapter'; |
3 | 4 |
|
4 | | -describe('zen-bash: combining just-bash with zen-fs', () => { |
5 | | - describe('using just-bash VirtualFs (default)', () => { |
6 | | - it('executes bash commands with built-in virtual filesystem', async () => { |
7 | | - const bash = new Bash(); |
8 | | - await bash.exec('echo "hello from just-bash" > /tmp/test.txt'); |
9 | | - const result = await bash.exec('cat /tmp/test.txt'); |
10 | | - expect(result.stdout).toBe('hello from just-bash\n'); |
11 | | - }); |
| 5 | +describe('zen-bash: just-bash with zen-fs filesystem', () => { |
| 6 | + let adapter: ZenFsAdapter; |
12 | 7 |
|
13 | | - it('supports complex file operations', async () => { |
14 | | - const bash = new Bash(); |
15 | | - await bash.exec('mkdir -p /tmp/project/src'); |
16 | | - await bash.exec('echo "console.log(42)" > /tmp/project/src/index.js'); |
17 | | - await bash.exec('cp /tmp/project/src/index.js /tmp/project/src/backup.js'); |
18 | | - const result = await bash.exec('cat /tmp/project/src/backup.js'); |
19 | | - expect(result.stdout).toBe('console.log(42)\n'); |
20 | | - }); |
| 8 | + beforeEach(async () => { |
| 9 | + await configure({ mounts: { '/': InMemory } }); |
| 10 | + adapter = new ZenFsAdapter(); |
| 11 | + zenfs.mkdirSync('/tmp', { recursive: true }); |
| 12 | + zenfs.mkdirSync('/home', { recursive: true }); |
21 | 13 | }); |
22 | 14 |
|
23 | | - describe('using zen-fs InMemory backend', () => { |
24 | | - beforeEach(async () => { |
25 | | - await configure({ mounts: { '/': InMemory } }); |
26 | | - }); |
| 15 | + describe('just-bash operating on zen-fs filesystem via adapter', () => { |
| 16 | + it('writes a file via just-bash, reads it via zen-fs', async () => { |
| 17 | + const bash = new Bash({ fs: adapter }); |
27 | 18 |
|
28 | | - it('performs file operations with zen-fs', async () => { |
29 | | - zenfs.writeFileSync('/data.txt', 'zen-fs content'); |
30 | | - const content = zenfs.readFileSync('/data.txt', 'utf-8'); |
31 | | - expect(content).toBe('zen-fs content'); |
32 | | - }); |
| 19 | + await bash.exec('echo "hello from bash" > /tmp/test.txt'); |
33 | 20 |
|
34 | | - it('creates directories and files', async () => { |
35 | | - zenfs.mkdirSync('/project/src', { recursive: true }); |
36 | | - zenfs.writeFileSync('/project/src/main.ts', 'export const x = 1;'); |
37 | | - const files = zenfs.readdirSync('/project/src'); |
38 | | - expect(files).toContain('main.ts'); |
| 21 | + const content = zenfs.readFileSync('/tmp/test.txt', 'utf-8'); |
| 22 | + expect(content).toBe('hello from bash\n'); |
39 | 23 | }); |
40 | | - }); |
41 | 24 |
|
42 | | - describe('interoperability patterns', () => { |
43 | | - beforeEach(async () => { |
44 | | - await configure({ mounts: { '/': InMemory } }); |
| 25 | + it('creates directories via just-bash, verifies via zen-fs', async () => { |
| 26 | + const bash = new Bash({ fs: adapter }); |
| 27 | + |
| 28 | + await bash.exec('mkdir -p /home/user/projects/myapp'); |
| 29 | + await bash.exec('echo "# My App" > /home/user/projects/myapp/README.md'); |
| 30 | + |
| 31 | + expect(zenfs.existsSync('/home/user/projects/myapp')).toBe(true); |
| 32 | + expect(zenfs.existsSync('/home/user/projects/myapp/README.md')).toBe(true); |
| 33 | + expect(zenfs.readFileSync('/home/user/projects/myapp/README.md', 'utf-8')).toBe('# My App\n'); |
45 | 34 | }); |
46 | 35 |
|
47 | | - it('prepares files with zen-fs, processes with just-bash', async () => { |
48 | | - zenfs.mkdirSync('/workspace', { recursive: true }); |
49 | | - zenfs.writeFileSync('/workspace/input.txt', 'line1\nline2\nline3\n'); |
| 36 | + it('appends to a file via just-bash, verifies via zen-fs', async () => { |
| 37 | + const bash = new Bash({ fs: adapter }); |
50 | 38 |
|
51 | | - const bash = new Bash({ |
52 | | - files: { |
53 | | - '/workspace/input.txt': zenfs.readFileSync('/workspace/input.txt', 'utf-8') |
54 | | - } |
55 | | - }); |
| 39 | + await bash.exec('echo "line1" > /tmp/log.txt'); |
| 40 | + await bash.exec('echo "line2" >> /tmp/log.txt'); |
| 41 | + await bash.exec('echo "line3" >> /tmp/log.txt'); |
56 | 42 |
|
57 | | - const result = await bash.exec('wc -l < /workspace/input.txt'); |
58 | | - expect(result.stdout.trim()).toBe('3'); |
| 43 | + const content = zenfs.readFileSync('/tmp/log.txt', 'utf-8'); |
| 44 | + expect(content).toBe('line1\nline2\nline3\n'); |
59 | 45 | }); |
60 | 46 |
|
61 | | - it('uses just-bash for text processing, zen-fs for storage', async () => { |
62 | | - const bash = new Bash({ |
63 | | - files: { '/data/users.json': '[{"name":"Alice"},{"name":"Bob"}]' } |
64 | | - }); |
| 47 | + it('copies files via just-bash, verifies via zen-fs', async () => { |
| 48 | + const bash = new Bash({ fs: adapter }); |
65 | 49 |
|
66 | | - const result = await bash.exec('cat /data/users.json | grep -o \'"name":"[^"]*"\' | wc -l'); |
67 | | - expect(result.stdout.trim()).toBe('2'); |
| 50 | + await bash.exec('echo "original content" > /tmp/original.txt'); |
| 51 | + await bash.exec('cp /tmp/original.txt /tmp/copy.txt'); |
68 | 52 |
|
69 | | - zenfs.mkdirSync('/processed', { recursive: true }); |
70 | | - zenfs.writeFileSync('/processed/count.txt', result.stdout.trim()); |
71 | | - expect(zenfs.readFileSync('/processed/count.txt', 'utf-8')).toBe('2'); |
| 53 | + expect(zenfs.existsSync('/tmp/copy.txt')).toBe(true); |
| 54 | + expect(zenfs.readFileSync('/tmp/copy.txt', 'utf-8')).toBe('original content\n'); |
72 | 55 | }); |
73 | 56 |
|
74 | | - it('combines bash scripting with zen-fs file management', async () => { |
75 | | - zenfs.mkdirSync('/scripts', { recursive: true }); |
76 | | - zenfs.writeFileSync('/scripts/config.env', 'APP_NAME=myapp\nAPP_VERSION=1.0.0'); |
| 57 | + it('moves files via just-bash, verifies via zen-fs', async () => { |
| 58 | + const bash = new Bash({ fs: adapter }); |
77 | 59 |
|
78 | | - const bash = new Bash({ |
79 | | - files: { |
80 | | - '/scripts/config.env': zenfs.readFileSync('/scripts/config.env', 'utf-8') |
81 | | - } |
82 | | - }); |
| 60 | + await bash.exec('echo "movable content" > /tmp/source.txt'); |
| 61 | + await bash.exec('mv /tmp/source.txt /tmp/destination.txt'); |
83 | 62 |
|
84 | | - const nameResult = await bash.exec('grep APP_NAME /scripts/config.env | cut -d= -f2'); |
85 | | - const versionResult = await bash.exec('grep APP_VERSION /scripts/config.env | cut -d= -f2'); |
| 63 | + expect(zenfs.existsSync('/tmp/source.txt')).toBe(false); |
| 64 | + expect(zenfs.existsSync('/tmp/destination.txt')).toBe(true); |
| 65 | + expect(zenfs.readFileSync('/tmp/destination.txt', 'utf-8')).toBe('movable content\n'); |
| 66 | + }); |
86 | 67 |
|
87 | | - expect(nameResult.stdout.trim()).toBe('myapp'); |
88 | | - expect(versionResult.stdout.trim()).toBe('1.0.0'); |
| 68 | + it('removes files via just-bash, verifies via zen-fs', async () => { |
| 69 | + const bash = new Bash({ fs: adapter }); |
89 | 70 |
|
90 | | - zenfs.mkdirSync('/output', { recursive: true }); |
91 | | - zenfs.writeFileSync('/output/app-info.json', JSON.stringify({ |
92 | | - name: nameResult.stdout.trim(), |
93 | | - version: versionResult.stdout.trim() |
94 | | - })); |
| 71 | + await bash.exec('echo "temporary" > /tmp/temp.txt'); |
| 72 | + expect(zenfs.existsSync('/tmp/temp.txt')).toBe(true); |
95 | 73 |
|
96 | | - const appInfo = JSON.parse(zenfs.readFileSync('/output/app-info.json', 'utf-8')); |
97 | | - expect(appInfo.name).toBe('myapp'); |
98 | | - expect(appInfo.version).toBe('1.0.0'); |
| 74 | + await bash.exec('rm /tmp/temp.txt'); |
| 75 | + expect(zenfs.existsSync('/tmp/temp.txt')).toBe(false); |
99 | 76 | }); |
100 | 77 | }); |
101 | 78 |
|
102 | | - describe('parallel usage patterns', () => { |
103 | | - it('maintains separate filesystems for isolation', async () => { |
104 | | - await configure({ mounts: { '/': InMemory } }); |
| 79 | + describe('bidirectional file operations', () => { |
| 80 | + it('zen-fs writes, just-bash reads and processes', async () => { |
| 81 | + zenfs.writeFileSync('/tmp/data.csv', 'name,age\nAlice,30\nBob,25\nCharlie,35'); |
| 82 | + |
| 83 | + const bash = new Bash({ fs: adapter }); |
| 84 | + const result = await bash.exec('cat /tmp/data.csv | grep -c ","'); |
| 85 | + |
| 86 | + expect(result.stdout.trim()).toBe('4'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('just-bash processes, zen-fs stores result', async () => { |
| 90 | + zenfs.writeFileSync('/tmp/numbers.txt', '10\n20\n30\n40\n50\n'); |
| 91 | + |
| 92 | + const bash = new Bash({ fs: adapter }); |
| 93 | + await bash.exec('cat /tmp/numbers.txt | wc -l > /tmp/count.txt'); |
| 94 | + |
| 95 | + const count = zenfs.readFileSync('/tmp/count.txt', 'utf-8'); |
| 96 | + expect(count.trim()).toBe('5'); |
| 97 | + }); |
105 | 98 |
|
106 | | - const bash1 = new Bash({ files: { '/config.txt': 'env=dev' } }); |
107 | | - const bash2 = new Bash({ files: { '/config.txt': 'env=prod' } }); |
| 99 | + it('complex workflow: zen-fs setup, bash transform, zen-fs verify', async () => { |
| 100 | + zenfs.mkdirSync('/workspace/input', { recursive: true }); |
| 101 | + zenfs.mkdirSync('/workspace/output', { recursive: true }); |
| 102 | + zenfs.writeFileSync('/workspace/input/config.env', 'DB_HOST=localhost\nDB_PORT=5432\nDB_NAME=mydb'); |
108 | 103 |
|
109 | | - const result1 = await bash1.exec('cat /config.txt'); |
110 | | - const result2 = await bash2.exec('cat /config.txt'); |
| 104 | + const bash = new Bash({ fs: adapter }); |
111 | 105 |
|
112 | | - expect(result1.stdout).toBe('env=dev'); |
113 | | - expect(result2.stdout).toBe('env=prod'); |
| 106 | + await bash.exec('grep DB_HOST /workspace/input/config.env | cut -d= -f2 > /workspace/output/host.txt'); |
| 107 | + await bash.exec('grep DB_PORT /workspace/input/config.env | cut -d= -f2 > /workspace/output/port.txt'); |
114 | 108 |
|
115 | | - zenfs.writeFileSync('/shared.txt', 'shared data'); |
116 | | - expect(zenfs.readFileSync('/shared.txt', 'utf-8')).toBe('shared data'); |
| 109 | + expect(zenfs.readFileSync('/workspace/output/host.txt', 'utf-8').trim()).toBe('localhost'); |
| 110 | + expect(zenfs.readFileSync('/workspace/output/port.txt', 'utf-8').trim()).toBe('5432'); |
117 | 111 | }); |
118 | 112 | }); |
119 | 113 | }); |
0 commit comments