Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 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 { Clock, Effect, Fiber, FileSystem, Layer, Option, Path, PlatformError } from 'effect'; |
| 5 | import { TestClock } from 'effect/testing'; |
| 6 | import { AtomicReplaceError, atomicReplaceFile } from 'src/utils/atomic-replace'; |
| 7 | |
| 8 | const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer); |
| 9 | |
| 10 | describe('atomic replace', () => { |
| 11 | layer(TestPlatform)(it => { |
| 12 | it.effect('replaces an existing file without modifying its open inode', () => |
| 13 | Effect.gen(function* () { |
| 14 | const fs = yield* FileSystem.FileSystem; |
| 15 | const path = yield* Path.Path; |
| 16 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 17 | const sourcePath = path.join(directory, 'source'); |
| 18 | const targetPath = path.join(directory, 'target'); |
| 19 | yield* fs.writeFileString(sourcePath, 'new contents'); |
| 20 | yield* fs.writeFileString(targetPath, 'old contents'); |
| 21 | |
| 22 | const before = yield* fs.stat(targetPath); |
| 23 | const oldTarget = yield* fs.open(targetPath); |
| 24 | yield* Effect.scoped(atomicReplaceFile({ sourcePath, targetPath })); |
| 25 | const after = yield* fs.stat(targetPath); |
| 26 | const openContents = yield* oldTarget.readAlloc('old contents'.length); |
| 27 | |
| 28 | expect(yield* fs.readFileString(targetPath)).toBe('new contents'); |
| 29 | expect(after.dev).toBe(before.dev); |
| 30 | expect(Option.getOrThrow(after.ino)).not.toBe(Option.getOrThrow(before.ino)); |
| 31 | expect(new TextDecoder().decode(Option.getOrThrow(openContents))).toBe('old contents'); |
| 32 | expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']); |
| 33 | }) |
| 34 | ); |
| 35 | |
| 36 | it.effect('creates an executable target when mode is provided', () => |
| 37 | Effect.gen(function* () { |
| 38 | const fs = yield* FileSystem.FileSystem; |
| 39 | const path = yield* Path.Path; |
| 40 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 41 | const sourcePath = path.join(directory, 'source'); |
| 42 | const targetPath = path.join(directory, 'target'); |
| 43 | yield* fs.writeFileString(sourcePath, 'binary'); |
| 44 | |
| 45 | yield* Effect.scoped(atomicReplaceFile({ sourcePath, targetPath, mode: 0o755 })); |
| 46 | |
| 47 | expect(yield* fs.readFileString(targetPath)).toBe('binary'); |
| 48 | expect((yield* fs.stat(targetPath)).mode & 0o777).toBe(0o755); |
| 49 | expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']); |
| 50 | }) |
| 51 | ); |
| 52 | |
| 53 | it.effect('keeps the old executable beside the new one and replaces a previous backup', () => |
| 54 | Effect.gen(function* () { |
| 55 | const fs = yield* FileSystem.FileSystem; |
| 56 | const path = yield* Path.Path; |
| 57 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 58 | const sourcePath = path.join(directory, 'source'); |
| 59 | const targetPath = path.join(directory, 'composio.exe'); |
| 60 | const backupPath = `${targetPath}.old`; |
| 61 | yield* fs.writeFileString(sourcePath, 'new binary'); |
| 62 | yield* fs.writeFileString(targetPath, 'old binary'); |
| 63 | yield* fs.writeFileString(backupPath, 'previous backup'); |
| 64 | |
| 65 | yield* Effect.scoped(atomicReplaceFile({ sourcePath, targetPath, backupPath })); |
| 66 | |
| 67 | expect(yield* fs.readFileString(targetPath)).toBe('new binary'); |
| 68 | expect(yield* fs.readFileString(backupPath)).toBe('old binary'); |
| 69 | expect((yield* fs.readDirectory(directory)).sort()).toEqual([ |
| 70 | 'composio.exe', |
| 71 | 'composio.exe.old', |
| 72 | 'source', |
| 73 | ]); |
| 74 | }) |
| 75 | ); |
| 76 | |
| 77 | it.effect.each([ |
| 78 | { |
| 79 | name: 'waits for a briefly busy executable before replacing it', |
| 80 | reason: 'Busy', |
| 81 | failures: 2, |
| 82 | attempts: 3, |
| 83 | }, |
| 84 | { |
| 85 | name: 'fails after bounded retries when an executable stays busy', |
| 86 | reason: 'Busy', |
| 87 | failures: Infinity, |
| 88 | attempts: 11, |
| 89 | }, |
| 90 | { |
| 91 | name: 'does not retry a permission failure when moving an executable', |
| 92 | reason: 'PermissionDenied', |
| 93 | failures: Infinity, |
| 94 | attempts: 1, |
| 95 | }, |
| 96 | ] as const)('$name', scenario => |
| 97 | Effect.gen(function* () { |
| 98 | const fs = yield* FileSystem.FileSystem; |
| 99 | const path = yield* Path.Path; |
| 100 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 101 | const sourcePath = path.join(directory, 'source'); |
| 102 | const targetPath = path.join(directory, 'composio.exe'); |
| 103 | const backupPath = `${targetPath}.old`; |
| 104 | yield* fs.writeFileString(sourcePath, 'new binary'); |
| 105 | yield* fs.writeFileString(targetPath, 'old binary'); |
| 106 | const lockedError = PlatformError.systemError({ |
| 107 | _tag: scenario.reason, |
| 108 | module: 'FileSystem', |
| 109 | method: 'rename', |
| 110 | pathOrDescriptor: targetPath, |
| 111 | }); |
| 112 | const attemptTimes: number[] = []; |
| 113 | const lockedFs = FileSystem.make({ |
| 114 | ...fs, |
| 115 | rename: (from, to) => |
| 116 | Effect.gen(function* () { |
| 117 | if (from === targetPath && to === backupPath) { |
| 118 | attemptTimes.push(yield* Clock.currentTimeMillis); |
| 119 | if (attemptTimes.length <= scenario.failures) { |
| 120 | return yield* Effect.fail(lockedError); |
| 121 | } |
| 122 | } |
| 123 | return yield* fs.rename(from, to); |
| 124 | }), |
| 125 | }); |
| 126 | const fiber = yield* Effect.scoped( |
| 127 | atomicReplaceFile({ sourcePath, targetPath, backupPath }) |
| 128 | ).pipe( |
| 129 | Effect.provideService(FileSystem.FileSystem, lockedFs), |
| 130 | Effect.result, |
| 131 | Effect.forkChild |
| 132 | ); |
| 133 | yield* Effect.yieldNow.pipe( |
| 134 | Effect.andThen(TestClock.adjust('3 seconds')), |
| 135 | Effect.repeat({ times: 100, while: () => fiber.pollUnsafe() === undefined }) |
| 136 | ); |
| 137 | const result = yield* Fiber.join(fiber); |
| 138 | |
| 139 | expect(attemptTimes).toHaveLength(scenario.attempts); |
| 140 | const firstAttempt = attemptTimes[0]; |
| 141 | expect(attemptTimes.map(time => time - firstAttempt)).toEqual( |
| 142 | Array.from({ length: scenario.attempts }, (_, index) => index * 200) |
| 143 | ); |
| 144 | if (scenario.failures === 2) { |
| 145 | expect(yield* fs.readFileString(targetPath)).toBe('new binary'); |
| 146 | expect(yield* fs.readFileString(backupPath)).toBe('old binary'); |
| 147 | } else { |
| 148 | const error = yield* Effect.flip(Effect.fromResult(result)); |
| 149 | expect(error).toBeInstanceOf(AtomicReplaceError); |
| 150 | expect(error.cause).toBe(lockedError); |
| 151 | expect(yield* fs.readFileString(targetPath)).toBe('old binary'); |
| 152 | expect(yield* fs.exists(backupPath)).toBe(false); |
| 153 | } |
| 154 | expect((yield* fs.readDirectory(directory)).sort()).toEqual( |
| 155 | scenario.failures === 2 |
| 156 | ? ['composio.exe', 'composio.exe.old', 'source'] |
| 157 | : ['composio.exe', 'source'] |
| 158 | ); |
| 159 | }) |
| 160 | ); |
| 161 | |
| 162 | it.effect('restores the old executable when placing its staged replacement fails', () => |
| 163 | Effect.gen(function* () { |
| 164 | const fs = yield* FileSystem.FileSystem; |
| 165 | const path = yield* Path.Path; |
| 166 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 167 | const sourcePath = path.join(directory, 'source'); |
| 168 | const targetPath = path.join(directory, 'composio.exe'); |
| 169 | const backupPath = `${targetPath}.old`; |
| 170 | yield* fs.writeFileString(sourcePath, 'new binary'); |
| 171 | yield* fs.writeFileString(targetPath, 'old binary'); |
| 172 | const failingFs = FileSystem.make({ |
| 173 | ...fs, |
| 174 | rename: (from, to) => { |
| 175 | return from.includes('.composio-atomic-replace-') && to === targetPath |
| 176 | ? fs.rename(path.join(directory, 'missing'), to) |
| 177 | : fs.rename(from, to); |
| 178 | }, |
| 179 | }); |
| 180 | |
| 181 | const error = yield* Effect.flip( |
| 182 | Effect.scoped(atomicReplaceFile({ sourcePath, targetPath, backupPath })).pipe( |
| 183 | Effect.provideService(FileSystem.FileSystem, failingFs) |
| 184 | ) |
| 185 | ); |
| 186 | |
| 187 | expect(error).toBeInstanceOf(AtomicReplaceError); |
| 188 | expect(yield* fs.readFileString(targetPath)).toBe('old binary'); |
| 189 | expect(yield* fs.exists(backupPath)).toBe(false); |
| 190 | }) |
| 191 | ); |
| 192 | |
| 193 | it.effect('preserves the target and cleans staging when the source is missing', () => |
| 194 | Effect.gen(function* () { |
| 195 | const fs = yield* FileSystem.FileSystem; |
| 196 | const path = yield* Path.Path; |
| 197 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 198 | const sourcePath = path.join(directory, 'missing'); |
| 199 | const targetPath = path.join(directory, 'target'); |
| 200 | yield* fs.writeFileString(targetPath, 'old contents'); |
| 201 | |
| 202 | const error = yield* Effect.flip( |
| 203 | Effect.scoped(atomicReplaceFile({ sourcePath, targetPath })) |
| 204 | ); |
| 205 | |
| 206 | expect(error).toBeInstanceOf(AtomicReplaceError); |
| 207 | expect(error.targetPath).toBe(targetPath); |
| 208 | expect(error.cause).toBeDefined(); |
| 209 | expect(yield* fs.readFileString(targetPath)).toBe('old contents'); |
| 210 | expect(yield* fs.readDirectory(directory)).toEqual(['target']); |
| 211 | }) |
| 212 | ); |
| 213 | }); |
| 214 | }); |