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 fs from 'node:fs'; |
| 2 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | import * as os from 'node:os'; |
| 4 | import * as path from 'node:path'; |
| 5 | import { describe, expect, it, vi } from 'vitest'; |
| 6 | import { platform } from '../../src/platform/node'; |
| 7 | |
| 8 | vi.mock('node:fs', async importOriginal => { |
| 9 | const actual = await importOriginal<typeof import('node:fs')>(); |
| 10 | return { |
| 11 | ...actual, |
| 12 | closeSync: vi.fn(actual.closeSync), |
| 13 | writeFileSync: vi.fn(actual.writeFileSync), |
| 14 | }; |
| 15 | }); |
| 16 | |
| 17 | const invertAsciiCase = (value: string): string => |
| 18 | [...value] |
| 19 | .map(character => { |
| 20 | const upper = character.toUpperCase(); |
| 21 | return character === upper ? character.toLowerCase() : upper; |
| 22 | }) |
| 23 | .join(''); |
| 24 | |
| 25 | describe('node platform filesystem case detection', () => { |
| 26 | it('detects the target filesystem for existing and not-yet-created paths', () => { |
| 27 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-case-sensitivity-')); |
| 28 | try { |
| 29 | const probeDirectory = path.join(root, 'CaseProbe'); |
| 30 | mkdirSync(probeDirectory); |
| 31 | const existingFile = path.join(probeDirectory, 'config.json'); |
| 32 | writeFileSync(existingFile, '{}'); |
| 33 | |
| 34 | const caseVariantExists = existsSync(path.join(root, invertAsciiCase('CaseProbe'))); |
| 35 | const expected = !caseVariantExists; |
| 36 | |
| 37 | expect(platform.isFileSystemCaseSensitive(existingFile)).toBe(expected); |
| 38 | expect( |
| 39 | platform.isFileSystemCaseSensitive( |
| 40 | path.join(probeDirectory, 'not-created-yet', 'config.json') |
| 41 | ) |
| 42 | ).toBe(expected); |
| 43 | } finally { |
| 44 | rmSync(root, { recursive: true, force: true }); |
| 45 | } |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | describe('node platform exclusive writes', () => { |
| 50 | it('removes the file it created when the write fails', () => { |
| 51 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-exclusive-write-')); |
| 52 | try { |
| 53 | const filePath = path.join(root, 'report.pdf'); |
| 54 | const actual = vi.mocked(fs.writeFileSync).getMockImplementation()!; |
| 55 | // A disk that fills up mid-write: some bytes land, then the write fails. |
| 56 | vi.mocked(fs.writeFileSync).mockImplementationOnce((file, data, options) => { |
| 57 | actual(file, (data as Uint8Array).subarray(0, 1), options); |
| 58 | throw Object.assign(new Error('no space left on device'), { code: 'ENOSPC' }); |
| 59 | }); |
| 60 | |
| 61 | expect(() => platform.writeFileExclusiveSync(filePath, new Uint8Array([1, 2]))).toThrow( |
| 62 | 'no space left on device' |
| 63 | ); |
| 64 | expect(existsSync(filePath)).toBe(false); |
| 65 | } finally { |
| 66 | rmSync(root, { recursive: true, force: true }); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | it('removes the file it created when closing it fails', () => { |
| 71 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-exclusive-write-')); |
| 72 | try { |
| 73 | const filePath = path.join(root, 'report.pdf'); |
| 74 | const actual = vi.mocked(fs.closeSync).getMockImplementation()!; |
| 75 | // Some filesystems report a deferred write error only on close. |
| 76 | vi.mocked(fs.closeSync).mockImplementationOnce(fd => { |
| 77 | actual(fd); |
| 78 | throw Object.assign(new Error('disk quota exceeded'), { code: 'EDQUOT' }); |
| 79 | }); |
| 80 | |
| 81 | expect(() => platform.writeFileExclusiveSync(filePath, new Uint8Array([1, 2]))).toThrow( |
| 82 | 'disk quota exceeded' |
| 83 | ); |
| 84 | expect(existsSync(filePath)).toBe(false); |
| 85 | } finally { |
| 86 | rmSync(root, { recursive: true, force: true }); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | it('leaves an existing file untouched', () => { |
| 91 | const root = mkdtempSync(path.join(os.tmpdir(), 'composio-exclusive-write-')); |
| 92 | try { |
| 93 | const filePath = path.join(root, 'report.pdf'); |
| 94 | writeFileSync(filePath, 'original'); |
| 95 | |
| 96 | expect(() => platform.writeFileExclusiveSync(filePath, new Uint8Array([1]))).toThrow(); |
| 97 | expect(readFileSync(filePath, 'utf8')).toBe('original'); |
| 98 | } finally { |
| 99 | rmSync(root, { recursive: true, force: true }); |
| 100 | } |
| 101 | }); |
| 102 | }); |