|
1 | 1 | import { describe, it, expect, beforeEach } from 'vitest'; |
2 | | -import { mountR2Storage } from './r2'; |
| 2 | +import { ensureRcloneConfig } from './r2'; |
3 | 3 | import { |
4 | 4 | createMockEnv, |
5 | 5 | createMockEnvWithR2, |
6 | | - createMockProcess, |
| 6 | + createMockExecResult, |
7 | 7 | createMockSandbox, |
8 | 8 | suppressConsole, |
9 | 9 | } from '../test-utils'; |
10 | 10 |
|
11 | | -describe('mountR2Storage', () => { |
| 11 | +describe('ensureRcloneConfig', () => { |
12 | 12 | beforeEach(() => { |
13 | 13 | suppressConsole(); |
14 | 14 | }); |
15 | 15 |
|
16 | 16 | describe('credential validation', () => { |
17 | 17 | it('returns false when R2_ACCESS_KEY_ID is missing', async () => { |
18 | 18 | const { sandbox } = createMockSandbox(); |
19 | | - const env = createMockEnv({ |
20 | | - R2_SECRET_ACCESS_KEY: 'secret', |
21 | | - CF_ACCOUNT_ID: 'account123', |
22 | | - }); |
23 | | - |
24 | | - const result = await mountR2Storage(sandbox, env); |
25 | | - |
26 | | - expect(result).toBe(false); |
| 19 | + const env = createMockEnv({ R2_SECRET_ACCESS_KEY: 'secret', CF_ACCOUNT_ID: 'acct' }); |
| 20 | + expect(await ensureRcloneConfig(sandbox, env)).toBe(false); |
27 | 21 | }); |
28 | 22 |
|
29 | 23 | it('returns false when R2_SECRET_ACCESS_KEY is missing', async () => { |
30 | 24 | const { sandbox } = createMockSandbox(); |
31 | | - const env = createMockEnv({ |
32 | | - R2_ACCESS_KEY_ID: 'key123', |
33 | | - CF_ACCOUNT_ID: 'account123', |
34 | | - }); |
35 | | - |
36 | | - const result = await mountR2Storage(sandbox, env); |
37 | | - |
38 | | - expect(result).toBe(false); |
| 25 | + const env = createMockEnv({ R2_ACCESS_KEY_ID: 'key', CF_ACCOUNT_ID: 'acct' }); |
| 26 | + expect(await ensureRcloneConfig(sandbox, env)).toBe(false); |
39 | 27 | }); |
40 | 28 |
|
41 | 29 | it('returns false when CF_ACCOUNT_ID is missing', async () => { |
42 | 30 | const { sandbox } = createMockSandbox(); |
43 | | - const env = createMockEnv({ |
44 | | - R2_ACCESS_KEY_ID: 'key123', |
45 | | - R2_SECRET_ACCESS_KEY: 'secret', |
46 | | - }); |
47 | | - |
48 | | - const result = await mountR2Storage(sandbox, env); |
49 | | - |
50 | | - expect(result).toBe(false); |
| 31 | + const env = createMockEnv({ R2_ACCESS_KEY_ID: 'key', R2_SECRET_ACCESS_KEY: 'secret' }); |
| 32 | + expect(await ensureRcloneConfig(sandbox, env)).toBe(false); |
51 | 33 | }); |
52 | 34 |
|
53 | 35 | it('returns false when all R2 credentials are missing', async () => { |
54 | 36 | const { sandbox } = createMockSandbox(); |
55 | 37 | const env = createMockEnv(); |
56 | | - |
57 | | - const result = await mountR2Storage(sandbox, env); |
58 | | - |
59 | | - expect(result).toBe(false); |
60 | | - expect(console.log).toHaveBeenCalledWith( |
61 | | - expect.stringContaining('R2 storage not configured'), |
62 | | - ); |
| 38 | + expect(await ensureRcloneConfig(sandbox, env)).toBe(false); |
63 | 39 | }); |
64 | 40 | }); |
65 | 41 |
|
66 | | - describe('mounting behavior', () => { |
67 | | - it('mounts R2 bucket when credentials provided and not already mounted', async () => { |
68 | | - const { sandbox, mountBucketMock } = createMockSandbox({ mounted: false }); |
69 | | - const env = createMockEnvWithR2({ |
70 | | - R2_ACCESS_KEY_ID: 'key123', |
71 | | - R2_SECRET_ACCESS_KEY: 'secret', |
72 | | - CF_ACCOUNT_ID: 'account123', |
73 | | - }); |
74 | | - |
75 | | - const result = await mountR2Storage(sandbox, env); |
76 | | - |
77 | | - expect(result).toBe(true); |
78 | | - expect(mountBucketMock).toHaveBeenCalledWith('moltbot-data', '/data/moltbot', { |
79 | | - endpoint: 'https://account123.r2.cloudflarestorage.com', |
80 | | - credentials: { |
81 | | - accessKeyId: 'key123', |
82 | | - secretAccessKey: 'secret', |
83 | | - }, |
84 | | - }); |
85 | | - }); |
86 | | - |
87 | | - it('uses custom bucket name from R2_BUCKET_NAME env var', async () => { |
88 | | - const { sandbox, mountBucketMock } = createMockSandbox({ mounted: false }); |
89 | | - const env = createMockEnvWithR2({ |
90 | | - R2_ACCESS_KEY_ID: 'key123', |
91 | | - R2_SECRET_ACCESS_KEY: 'secret', |
92 | | - CF_ACCOUNT_ID: 'account123', |
93 | | - R2_BUCKET_NAME: 'moltbot-e2e-test123', |
94 | | - }); |
95 | | - |
96 | | - const result = await mountR2Storage(sandbox, env); |
97 | | - |
98 | | - expect(result).toBe(true); |
99 | | - expect(mountBucketMock).toHaveBeenCalledWith( |
100 | | - 'moltbot-e2e-test123', |
101 | | - '/data/moltbot', |
102 | | - expect.any(Object), |
103 | | - ); |
104 | | - }); |
105 | | - |
106 | | - it('returns true immediately when bucket is already mounted', async () => { |
107 | | - const { sandbox, mountBucketMock } = createMockSandbox({ mounted: true }); |
108 | | - const env = createMockEnvWithR2(); |
109 | | - |
110 | | - const result = await mountR2Storage(sandbox, env); |
111 | | - |
112 | | - expect(result).toBe(true); |
113 | | - expect(mountBucketMock).not.toHaveBeenCalled(); |
114 | | - expect(console.log).toHaveBeenCalledWith('R2 bucket already mounted at', '/data/moltbot'); |
115 | | - }); |
116 | | - |
117 | | - it('logs success message when mounted successfully', async () => { |
118 | | - const { sandbox } = createMockSandbox({ mounted: false }); |
119 | | - const env = createMockEnvWithR2(); |
120 | | - |
121 | | - await mountR2Storage(sandbox, env); |
122 | | - |
123 | | - expect(console.log).toHaveBeenCalledWith( |
124 | | - 'R2 bucket mounted successfully - moltbot data will persist across sessions', |
125 | | - ); |
126 | | - }); |
127 | | - }); |
128 | | - |
129 | | - describe('error handling', () => { |
130 | | - it('returns false when mountBucket throws and mount check fails', async () => { |
131 | | - const { sandbox, mountBucketMock, startProcessMock } = createMockSandbox({ mounted: false }); |
132 | | - mountBucketMock.mockRejectedValue(new Error('Mount failed')); |
133 | | - startProcessMock |
134 | | - .mockResolvedValueOnce(createMockProcess('')) |
135 | | - .mockResolvedValueOnce(createMockProcess('')); |
136 | | - |
| 42 | + describe('configuration behavior', () => { |
| 43 | + it('skips setup if already configured (flag file exists)', async () => { |
| 44 | + const { sandbox, execMock, writeFileMock } = createMockSandbox(); |
| 45 | + execMock.mockResolvedValue(createMockExecResult('yes')); |
137 | 46 | const env = createMockEnvWithR2(); |
138 | 47 |
|
139 | | - const result = await mountR2Storage(sandbox, env); |
140 | | - |
141 | | - expect(result).toBe(false); |
142 | | - expect(console.error).toHaveBeenCalledWith('Failed to mount R2 bucket:', expect.any(Error)); |
| 48 | + expect(await ensureRcloneConfig(sandbox, env)).toBe(true); |
| 49 | + expect(writeFileMock).not.toHaveBeenCalled(); |
143 | 50 | }); |
144 | 51 |
|
145 | | - it('returns true if mount fails but check shows it is actually mounted', async () => { |
146 | | - const { sandbox, mountBucketMock, startProcessMock } = createMockSandbox(); |
147 | | - startProcessMock |
148 | | - .mockResolvedValueOnce(createMockProcess('not-mounted\n')) |
149 | | - .mockResolvedValueOnce(createMockProcess('mounted\n')); |
150 | | - |
151 | | - mountBucketMock.mockRejectedValue(new Error('Transient error')); |
| 52 | + it('writes rclone config and sets flag when not configured', async () => { |
| 53 | + const { sandbox, execMock, writeFileMock } = createMockSandbox(); |
| 54 | + execMock |
| 55 | + .mockResolvedValueOnce(createMockExecResult('no')) // flag check |
| 56 | + .mockResolvedValueOnce(createMockExecResult()) // mkdir |
| 57 | + .mockResolvedValueOnce(createMockExecResult()); // touch flag |
152 | 58 |
|
153 | | - const env = createMockEnvWithR2(); |
| 59 | + const env = createMockEnvWithR2({ |
| 60 | + R2_ACCESS_KEY_ID: 'mykey', |
| 61 | + R2_SECRET_ACCESS_KEY: 'mysecret', |
| 62 | + CF_ACCOUNT_ID: 'myaccount', |
| 63 | + }); |
154 | 64 |
|
155 | | - const result = await mountR2Storage(sandbox, env); |
| 65 | + expect(await ensureRcloneConfig(sandbox, env)).toBe(true); |
156 | 66 |
|
157 | | - expect(result).toBe(true); |
158 | | - expect(console.log).toHaveBeenCalledWith('R2 bucket is mounted despite error'); |
| 67 | + const writtenConfig = writeFileMock.mock.calls[0][1]; |
| 68 | + expect(writtenConfig).toContain('access_key_id = mykey'); |
| 69 | + expect(writtenConfig).toContain('secret_access_key = mysecret'); |
| 70 | + expect(writtenConfig).toContain('endpoint = https://myaccount.r2.cloudflarestorage.com'); |
159 | 71 | }); |
160 | 72 | }); |
161 | 73 | }); |
0 commit comments