Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 2 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 3 | import { describe, expect, layer } from '@effect/vitest'; |
| 4 | import { Deferred, Effect, Fiber, FileSystem, Layer, Path, PlatformError } from 'effect'; |
| 5 | |
| 6 | type WriteFileOptions = Parameters<FileSystem.FileSystem['writeFileString']>[2]; |
| 7 | import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write'; |
| 8 | |
| 9 | const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer); |
| 10 | |
| 11 | const failChmod = (fs: FileSystem.FileSystem, onAttempt: () => void): FileSystem.FileSystem => |
| 12 | new Proxy(fs, { |
| 13 | get(target, property, receiver) { |
| 14 | if (property !== 'chmod') { |
| 15 | return Reflect.get(target, property, receiver); |
| 16 | } |
| 17 | |
| 18 | return (path: string) => { |
| 19 | onAttempt(); |
| 20 | return Effect.fail( |
| 21 | PlatformError.systemError({ |
| 22 | _tag: 'PermissionDenied', |
| 23 | module: 'FileSystem', |
| 24 | method: 'chmod', |
| 25 | pathOrDescriptor: path, |
| 26 | }) |
| 27 | ); |
| 28 | }; |
| 29 | }, |
| 30 | }); |
| 31 | |
| 32 | const collideWithTmpWrites = ( |
| 33 | fs: FileSystem.FileSystem, |
| 34 | maxCollisions: number, |
| 35 | onCollision: (path: string) => void |
| 36 | ): FileSystem.FileSystem => { |
| 37 | let collisions = 0; |
| 38 | |
| 39 | return new Proxy(fs, { |
| 40 | get(target, property, receiver) { |
| 41 | if (property !== 'writeFileString') { |
| 42 | return Reflect.get(target, property, receiver); |
| 43 | } |
| 44 | |
| 45 | return (path: string, contents: string, options?: WriteFileOptions) => { |
| 46 | if (collisions >= maxCollisions || !path.includes('.composio-tmp.')) { |
| 47 | return target.writeFileString(path, contents, options); |
| 48 | } |
| 49 | |
| 50 | collisions += 1; |
| 51 | onCollision(path); |
| 52 | return target |
| 53 | .writeFileString(path, 'pre-existing contents') |
| 54 | .pipe(Effect.andThen(target.writeFileString(path, contents, options))); |
| 55 | }; |
| 56 | }, |
| 57 | }); |
| 58 | }; |
| 59 | |
| 60 | const delayAfterTmpWrite = ( |
| 61 | fs: FileSystem.FileSystem, |
| 62 | onStaged: Effect.Effect<void>, |
| 63 | release: Effect.Effect<void> |
| 64 | ): FileSystem.FileSystem => |
| 65 | new Proxy(fs, { |
| 66 | get(target, property, receiver) { |
| 67 | if (property !== 'writeFileString') { |
| 68 | return Reflect.get(target, property, receiver); |
| 69 | } |
| 70 | |
| 71 | return (path: string, contents: string, options?: WriteFileOptions) => |
| 72 | target.writeFileString(path, contents, options).pipe( |
| 73 | Effect.tap(() => onStaged), |
| 74 | Effect.andThen(release) |
| 75 | ); |
| 76 | }, |
| 77 | }); |
| 78 | |
| 79 | const failAfterTmpWrite = (fs: FileSystem.FileSystem): FileSystem.FileSystem => |
| 80 | new Proxy(fs, { |
| 81 | get(target, property, receiver) { |
| 82 | if (property !== 'writeFileString') { |
| 83 | return Reflect.get(target, property, receiver); |
| 84 | } |
| 85 | |
| 86 | return (path: string, contents: string, options?: WriteFileOptions) => { |
| 87 | if (!path.includes('.composio-tmp.')) { |
| 88 | return target.writeFileString(path, contents, options); |
| 89 | } |
| 90 | |
| 91 | return target.writeFileString(path, contents, options).pipe( |
| 92 | Effect.andThen( |
| 93 | Effect.fail( |
| 94 | PlatformError.systemError({ |
| 95 | _tag: 'PermissionDenied', |
| 96 | module: 'FileSystem', |
| 97 | method: 'writeFileString', |
| 98 | pathOrDescriptor: path, |
| 99 | }) |
| 100 | ) |
| 101 | ) |
| 102 | ); |
| 103 | }; |
| 104 | }, |
| 105 | }); |
| 106 | |
| 107 | describe('atomicWritePrivateFileString', () => { |
| 108 | layer(TestPlatform)(it => { |
| 109 | it.effect('retries an exclusive random staging path without touching a collision', () => |
| 110 | Effect.gen(function* () { |
| 111 | const fs = yield* FileSystem.FileSystem; |
| 112 | const path = yield* Path.Path; |
| 113 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 114 | const target = path.join(directory, 'credentials.json'); |
| 115 | let collisionPath: string | undefined; |
| 116 | const collidingFs = collideWithTmpWrites(fs, 1, collidedPath => { |
| 117 | collisionPath = collidedPath; |
| 118 | }); |
| 119 | |
| 120 | yield* atomicWritePrivateFileString({ |
| 121 | fs: collidingFs, |
| 122 | target, |
| 123 | contents: '{"api_key":"valid"}\n', |
| 124 | }); |
| 125 | |
| 126 | expect(collisionPath).toBeDefined(); |
| 127 | expect(yield* fs.readFileString(collisionPath!, 'utf8')).toBe('pre-existing contents'); |
| 128 | expect(yield* fs.readFileString(target, 'utf8')).toBe('{"api_key":"valid"}\n'); |
| 129 | expect((yield* fs.stat(target)).mode & 0o777).toBe(0o600); |
| 130 | }) |
| 131 | ); |
| 132 | |
| 133 | it.effect('stops after bounded collisions without deleting foreign files', () => |
| 134 | Effect.gen(function* () { |
| 135 | const fs = yield* FileSystem.FileSystem; |
| 136 | const path = yield* Path.Path; |
| 137 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 138 | const target = path.join(directory, 'credentials.json'); |
| 139 | const collisionPaths: string[] = []; |
| 140 | const collidingFs = collideWithTmpWrites(fs, Number.POSITIVE_INFINITY, collisionPath => { |
| 141 | collisionPaths.push(collisionPath); |
| 142 | }); |
| 143 | |
| 144 | const error = yield* atomicWritePrivateFileString({ |
| 145 | fs: collidingFs, |
| 146 | target, |
| 147 | contents: '{"api_key":"valid"}\n', |
| 148 | }).pipe(Effect.flip); |
| 149 | |
| 150 | expect(error).toMatchObject({ _tag: 'PlatformError', reason: { _tag: 'AlreadyExists' } }); |
| 151 | expect(collisionPaths).toHaveLength(5); |
| 152 | expect(new Set(collisionPaths)).toHaveLength(5); |
| 153 | expect(yield* fs.exists(target)).toBe(false); |
| 154 | expect( |
| 155 | yield* Effect.forEach(collisionPaths, collisionPath => |
| 156 | fs.readFileString(collisionPath, 'utf8') |
| 157 | ) |
| 158 | ).toEqual(Array.from({ length: 5 }, () => 'pre-existing contents')); |
| 159 | }) |
| 160 | ); |
| 161 | |
| 162 | it.effect('cleans staging before honoring interruption', () => |
| 163 | Effect.gen(function* () { |
| 164 | const fs = yield* FileSystem.FileSystem; |
| 165 | const path = yield* Path.Path; |
| 166 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 167 | const target = path.join(directory, 'credentials.json'); |
| 168 | yield* fs.writeFileString(target, '{"api_key":"old"}\n'); |
| 169 | yield* fs.chmod(target, 0o600); |
| 170 | const staged = yield* Deferred.make<void>(); |
| 171 | const release = yield* Deferred.make<void>(); |
| 172 | const delayedFs = delayAfterTmpWrite( |
| 173 | fs, |
| 174 | Deferred.succeed(staged, undefined), |
| 175 | Deferred.await(release) |
| 176 | ); |
| 177 | const fiber = yield* atomicWritePrivateFileString({ |
| 178 | fs: delayedFs, |
| 179 | target, |
| 180 | contents: '{"api_key":"valid"}\n', |
| 181 | }).pipe(Effect.forkChild); |
| 182 | |
| 183 | yield* Deferred.await(staged); |
| 184 | const interruptFiber = yield* Fiber.interrupt(fiber).pipe(Effect.forkChild); |
| 185 | yield* Effect.yieldNow; |
| 186 | yield* Deferred.succeed(release, undefined); |
| 187 | yield* Fiber.join(interruptFiber); |
| 188 | |
| 189 | expect(yield* fs.readFileString(target, 'utf8')).toBe('{"api_key":"old"}\n'); |
| 190 | expect((yield* fs.stat(target)).mode & 0o777).toBe(0o600); |
| 191 | expect( |
| 192 | (yield* fs.readDirectory(directory)).filter(name => name.includes('.composio-tmp.')) |
| 193 | ).toEqual([]); |
| 194 | }) |
| 195 | ); |
| 196 | |
| 197 | it.effect('cleans a partially written staging file on non-collision failure', () => |
| 198 | Effect.gen(function* () { |
| 199 | const fs = yield* FileSystem.FileSystem; |
| 200 | const path = yield* Path.Path; |
| 201 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 202 | const target = path.join(directory, 'credentials.json'); |
| 203 | yield* fs.writeFileString(target, '{"api_key":"old"}\n'); |
| 204 | yield* fs.chmod(target, 0o600); |
| 205 | |
| 206 | const error = yield* atomicWritePrivateFileString({ |
| 207 | fs: failAfterTmpWrite(fs), |
| 208 | target, |
| 209 | contents: '{"api_key":"new"}\n', |
| 210 | }).pipe(Effect.flip); |
| 211 | |
| 212 | expect(error).toMatchObject({ |
| 213 | _tag: 'PlatformError', |
| 214 | reason: { _tag: 'PermissionDenied' }, |
| 215 | }); |
| 216 | expect(yield* fs.readFileString(target, 'utf8')).toBe('{"api_key":"old"}\n'); |
| 217 | expect( |
| 218 | (yield* fs.readDirectory(directory)).filter(name => name.includes('.composio-tmp.')) |
| 219 | ).toEqual([]); |
| 220 | }) |
| 221 | ); |
| 222 | }); |
| 223 | }); |
| 224 | |
| 225 | describe('ensurePrivateFileMode', () => { |
| 226 | layer(TestPlatform)(it => { |
| 227 | it.effect('keeps valid contents readable when chmod fails', () => |
| 228 | Effect.gen(function* () { |
| 229 | const fs = yield* FileSystem.FileSystem; |
| 230 | const path = yield* Path.Path; |
| 231 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 232 | const target = path.join(directory, 'credentials.json'); |
| 233 | const contents = '{"api_key":"valid"}\n'; |
| 234 | yield* fs.writeFileString(target, contents); |
| 235 | yield* fs.chmod(target, 0o644); |
| 236 | |
| 237 | let chmodAttempts = 0; |
| 238 | const failingFs = failChmod(fs, () => { |
| 239 | chmodAttempts += 1; |
| 240 | }); |
| 241 | const readContents = yield* ensurePrivateFileMode({ fs: failingFs, target }).pipe( |
| 242 | Effect.andThen(failingFs.readFileString(target, 'utf8')) |
| 243 | ); |
| 244 | |
| 245 | expect(chmodAttempts).toBe(1); |
| 246 | expect(readContents).toBe(contents); |
| 247 | expect((yield* fs.stat(target)).mode & 0o777).toBe(0o644); |
| 248 | }) |
| 249 | ); |
| 250 | |
| 251 | it.effect('does not call chmod for an already-private file', () => |
| 252 | Effect.gen(function* () { |
| 253 | const fs = yield* FileSystem.FileSystem; |
| 254 | const path = yield* Path.Path; |
| 255 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 256 | const target = path.join(directory, 'credentials.json'); |
| 257 | yield* fs.writeFileString(target, '{"api_key":"valid"}\n'); |
| 258 | yield* fs.chmod(target, 0o600); |
| 259 | |
| 260 | let chmodAttempts = 0; |
| 261 | const failingFs = failChmod(fs, () => { |
| 262 | chmodAttempts += 1; |
| 263 | }); |
| 264 | yield* ensurePrivateFileMode({ fs: failingFs, target }); |
| 265 | |
| 266 | expect(chmodAttempts).toBe(0); |
| 267 | }) |
| 268 | ); |
| 269 | }); |
| 270 | }); |