@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 400 lines 14.5 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Effect, Layer, Predicate } from 'effect';
3 import {
4 detectPlatform,
5 UnsupportedPlatformError,
6 type PlatformArch,
7 } from 'src/effects/detect-platform';
8 import { NodeOs } from 'src/services/node-os';
9
10 /**
11 * Mock NodeOs service with custom platform and architecture
12 */
13 const createMockNodeOs = <P extends NodeJS.Platform>(platform: P, arch: string) =>
14 Layer.succeed(NodeOs, {
15 homedir: '/mock/home',
16 tmpdir: '/tmp',
17 platform: platform as NodeJS.Platform,
18 arch,
19 });
20
21 describe('detect-platform.ts', () => {
22 describe('detectPlatform', () => {
23 describe('Supported Combinations', () => {
24 it.effect('should detect darwin + x64', () =>
25 Effect.gen(function* () {
26 const result = yield* detectPlatform;
27
28 expect(result).toEqual<PlatformArch>({
29 platform: 'darwin',
30 arch: 'x64',
31 });
32 }).pipe(Effect.provide(createMockNodeOs('darwin', 'x64')))
33 );
34
35 it.effect('should detect darwin + arm64 (mapped to aarch64)', () =>
36 Effect.gen(function* () {
37 const result = yield* detectPlatform;
38
39 expect(result).toEqual<PlatformArch>({
40 platform: 'darwin',
41 arch: 'aarch64',
42 });
43 }).pipe(Effect.provide(createMockNodeOs('darwin', 'arm64')))
44 );
45
46 it.effect('should detect darwin + aarch64', () =>
47 Effect.gen(function* () {
48 const result = yield* detectPlatform;
49
50 expect(result).toEqual<PlatformArch>({
51 platform: 'darwin',
52 arch: 'aarch64',
53 });
54 }).pipe(Effect.provide(createMockNodeOs('darwin', 'aarch64')))
55 );
56
57 it.effect('should detect linux + x64', () =>
58 Effect.gen(function* () {
59 const result = yield* detectPlatform;
60
61 expect(result).toEqual<PlatformArch>({
62 platform: 'linux',
63 arch: 'x64',
64 });
65 }).pipe(Effect.provide(createMockNodeOs('linux', 'x64')))
66 );
67
68 it.effect('should detect linux + arm64 (mapped to aarch64)', () =>
69 Effect.gen(function* () {
70 const result = yield* detectPlatform;
71
72 expect(result).toEqual<PlatformArch>({
73 platform: 'linux',
74 arch: 'aarch64',
75 });
76 }).pipe(Effect.provide(createMockNodeOs('linux', 'arm64')))
77 );
78
79 it.effect('should detect linux + aarch64', () =>
80 Effect.gen(function* () {
81 const result = yield* detectPlatform;
82
83 expect(result).toEqual<PlatformArch>({
84 platform: 'linux',
85 arch: 'aarch64',
86 });
87 }).pipe(Effect.provide(createMockNodeOs('linux', 'aarch64')))
88 );
89 });
90
91 it.effect('maps Windows x64 to the Windows release asset platform', () =>
92 Effect.gen(function* () {
93 expect(yield* detectPlatform).toEqual({ platform: 'windows', arch: 'x64' });
94 }).pipe(Effect.provide(createMockNodeOs('win32', 'x64')))
95 );
96
97 it.effect('rejects Windows ARM64 without a published binary', () =>
98 Effect.gen(function* () {
99 const error = yield* Effect.flip(detectPlatform);
100 expect(error).toBeInstanceOf(UnsupportedPlatformError);
101 expect(error.arch).toBe('arm64');
102 }).pipe(Effect.provide(createMockNodeOs('win32', 'arm64')))
103 );
104
105 describe('Unsupported Platforms', () => {
106 const unsupportedPlatforms = [
107 'freebsd',
108 'openbsd',
109 'sunos',
110 'aix',
111 'android',
112 'cygwin',
113 'netbsd',
114 ] as const;
115
116 unsupportedPlatforms.forEach(platform => {
117 it.effect(`should fail for unsupported platform: ${platform}`, () =>
118 Effect.gen(function* () {
119 const result = yield* Effect.flip(detectPlatform);
120
121 expect(result).toBeInstanceOf(UnsupportedPlatformError);
122 expect(result.platform).toBe(platform);
123 expect(result.arch).toBe('x64');
124 }).pipe(Effect.provide(createMockNodeOs(platform, 'x64')))
125 );
126 });
127 });
128
129 describe('Unsupported Architectures', () => {
130 const unsupportedArchitectures = [
131 'ia32',
132 'mips',
133 'mipsel',
134 'ppc',
135 'ppc64',
136 's390',
137 's390x',
138 'loong64',
139 'riscv64',
140 ];
141
142 unsupportedArchitectures.forEach(arch => {
143 it.effect(`should fail for unsupported architecture: ${arch} on darwin`, () =>
144 Effect.gen(function* () {
145 const result = yield* Effect.flip(detectPlatform);
146
147 expect(result).toBeInstanceOf(UnsupportedPlatformError);
148 expect(result.platform).toBe('darwin');
149 expect(result.arch).toBe(arch);
150 }).pipe(Effect.provide(createMockNodeOs('darwin', arch)))
151 );
152
153 it.effect(`should fail for unsupported architecture: ${arch} on linux`, () =>
154 Effect.gen(function* () {
155 const result = yield* Effect.flip(detectPlatform);
156
157 expect(result).toBeInstanceOf(UnsupportedPlatformError);
158 expect(result.platform).toBe('linux');
159 expect(result.arch).toBe(arch);
160 }).pipe(Effect.provide(createMockNodeOs('linux', arch)))
161 );
162 });
163 });
164
165 describe('Double Unsupported (Platform + Architecture)', () => {
166 it.effect('should fail with both unsupported platform and architecture', () =>
167 Effect.gen(function* () {
168 const result = yield* Effect.flip(detectPlatform);
169
170 expect(result).toBeInstanceOf(UnsupportedPlatformError);
171 expect(result.platform).toBe('win32');
172 expect(result.arch).toBe('ia32');
173 }).pipe(Effect.provide(createMockNodeOs('win32', 'ia32')))
174 );
175
176 it.effect('should fail with freebsd + mips combination', () =>
177 Effect.gen(function* () {
178 const result = yield* Effect.flip(detectPlatform);
179
180 expect(result).toBeInstanceOf(UnsupportedPlatformError);
181 expect(result.platform).toBe('freebsd');
182 expect(result.arch).toBe('mips');
183 }).pipe(Effect.provide(createMockNodeOs('freebsd', 'mips')))
184 );
185 });
186
187 describe('Edge Cases', () => {
188 it.effect('should handle empty platform string', () =>
189 Effect.gen(function* () {
190 const result = yield* Effect.flip(detectPlatform);
191
192 expect(result).toBeInstanceOf(UnsupportedPlatformError);
193 expect(result.platform).toBe('');
194 expect(result.arch).toBe('x64');
195 }).pipe(Effect.provide(createMockNodeOs('' as NodeJS.Platform, 'x64')))
196 );
197
198 it.effect('should handle empty architecture string', () =>
199 Effect.gen(function* () {
200 const result = yield* Effect.flip(detectPlatform);
201
202 expect(result).toBeInstanceOf(UnsupportedPlatformError);
203 expect(result.platform).toBe('darwin');
204 expect(result.arch).toBe('');
205 }).pipe(Effect.provide(createMockNodeOs('darwin', '')))
206 );
207
208 it.effect('should handle both empty platform and architecture', () =>
209 Effect.gen(function* () {
210 const result = yield* Effect.flip(detectPlatform);
211
212 expect(result).toBeInstanceOf(UnsupportedPlatformError);
213 expect(result.platform).toBe('');
214 expect(result.arch).toBe('');
215 }).pipe(Effect.provide(createMockNodeOs('' as NodeJS.Platform, '')))
216 );
217
218 it.effect('should handle null-like values as strings', () =>
219 Effect.gen(function* () {
220 const result = yield* Effect.flip(detectPlatform);
221
222 expect(result).toBeInstanceOf(UnsupportedPlatformError);
223 expect(result.platform).toBe('null');
224 expect(result.arch).toBe('undefined');
225 }).pipe(Effect.provide(createMockNodeOs('null' as NodeJS.Platform, 'undefined')))
226 );
227
228 it.effect('should handle very long platform name', () => {
229 const longPlatform = 'a'.repeat(1000);
230 return Effect.gen(function* () {
231 const result = yield* Effect.flip(detectPlatform);
232
233 expect(result).toBeInstanceOf(UnsupportedPlatformError);
234 expect(result.platform).toBe(longPlatform);
235 expect(result.arch).toBe('x64');
236 }).pipe(Effect.provide(createMockNodeOs(longPlatform as NodeJS.Platform, 'x64')));
237 });
238
239 it.effect('should handle very long architecture name', () => {
240 const longArch = 'z'.repeat(1000);
241 return Effect.gen(function* () {
242 const result = yield* Effect.flip(detectPlatform);
243
244 expect(result).toBeInstanceOf(UnsupportedPlatformError);
245 expect(result.platform).toBe('darwin');
246 expect(result.arch).toBe(longArch);
247 }).pipe(Effect.provide(createMockNodeOs('darwin', longArch)));
248 });
249 });
250
251 describe('Case Sensitivity', () => {
252 it.effect('should not match Darwin (uppercase D)', () =>
253 Effect.gen(function* () {
254 const result = yield* Effect.flip(detectPlatform);
255
256 expect(result).toBeInstanceOf(UnsupportedPlatformError);
257 expect(result.platform).toBe('Darwin');
258 expect(result.arch).toBe('x64');
259 }).pipe(Effect.provide(createMockNodeOs('Darwin' as NodeJS.Platform, 'x64')))
260 );
261
262 it.effect('should not match LINUX (uppercase)', () =>
263 Effect.gen(function* () {
264 const result = yield* Effect.flip(detectPlatform);
265
266 expect(result).toBeInstanceOf(UnsupportedPlatformError);
267 expect(result.platform).toBe('LINUX');
268 expect(result.arch).toBe('x64');
269 }).pipe(Effect.provide(createMockNodeOs('LINUX' as NodeJS.Platform, 'x64')))
270 );
271
272 it.effect('should not match X64 (uppercase)', () =>
273 Effect.gen(function* () {
274 const result = yield* Effect.flip(detectPlatform);
275
276 expect(result).toBeInstanceOf(UnsupportedPlatformError);
277 expect(result.platform).toBe('darwin');
278 expect(result.arch).toBe('X64');
279 }).pipe(Effect.provide(createMockNodeOs('darwin', 'X64')))
280 );
281
282 it.effect('should not match ARM64 (uppercase)', () =>
283 Effect.gen(function* () {
284 const result = yield* Effect.flip(detectPlatform);
285
286 expect(result).toBeInstanceOf(UnsupportedPlatformError);
287 expect(result.platform).toBe('linux');
288 expect(result.arch).toBe('ARM64');
289 }).pipe(Effect.provide(createMockNodeOs('linux', 'ARM64')))
290 );
291 });
292
293 describe('Whitespace Handling', () => {
294 it.effect('should not match platform with leading whitespace', () =>
295 Effect.gen(function* () {
296 const result = yield* Effect.flip(detectPlatform);
297
298 expect(result).toBeInstanceOf(UnsupportedPlatformError);
299 expect(result.platform).toBe(' darwin');
300 expect(result.arch).toBe('x64');
301 }).pipe(Effect.provide(createMockNodeOs(' darwin' as NodeJS.Platform, 'x64')))
302 );
303
304 it.effect('should not match platform with trailing whitespace', () =>
305 Effect.gen(function* () {
306 const result = yield* Effect.flip(detectPlatform);
307
308 expect(result).toBeInstanceOf(UnsupportedPlatformError);
309 expect(result.platform).toBe('linux ');
310 expect(result.arch).toBe('x64');
311 }).pipe(Effect.provide(createMockNodeOs('linux ' as NodeJS.Platform, 'x64')))
312 );
313
314 it.effect('should not match architecture with leading whitespace', () =>
315 Effect.gen(function* () {
316 const result = yield* Effect.flip(detectPlatform);
317
318 expect(result).toBeInstanceOf(UnsupportedPlatformError);
319 expect(result.platform).toBe('darwin');
320 expect(result.arch).toBe(' x64');
321 }).pipe(Effect.provide(createMockNodeOs('darwin', ' x64')))
322 );
323
324 it.effect('should not match architecture with trailing whitespace', () =>
325 Effect.gen(function* () {
326 const result = yield* Effect.flip(detectPlatform);
327
328 expect(result).toBeInstanceOf(UnsupportedPlatformError);
329 expect(result.platform).toBe('darwin');
330 expect(result.arch).toBe('arm64 ');
331 }).pipe(Effect.provide(createMockNodeOs('darwin', 'arm64 ')))
332 );
333
334 it.effect('should not match with internal whitespace in platform', () =>
335 Effect.gen(function* () {
336 const result = yield* Effect.flip(detectPlatform);
337
338 expect(result).toBeInstanceOf(UnsupportedPlatformError);
339 expect(result.platform).toBe('dar win');
340 expect(result.arch).toBe('x64');
341 }).pipe(Effect.provide(createMockNodeOs('dar win' as NodeJS.Platform, 'x64')))
342 );
343
344 it.effect('should not match with internal whitespace in architecture', () =>
345 Effect.gen(function* () {
346 const result = yield* Effect.flip(detectPlatform);
347
348 expect(result).toBeInstanceOf(UnsupportedPlatformError);
349 expect(result.platform).toBe('darwin');
350 expect(result.arch).toBe('x 64');
351 }).pipe(Effect.provide(createMockNodeOs('darwin', 'x 64')))
352 );
353 });
354 });
355
356 describe('UnsupportedPlatformError', () => {
357 it('should be a proper Error instance', () => {
358 const error = new UnsupportedPlatformError({ platform: 'test', arch: 'test' });
359 expect(error).toBeInstanceOf(Error);
360 expect(error).toBeInstanceOf(UnsupportedPlatformError);
361 expect(Predicate.isTagged(error, 'UnsupportedPlatformError')).toBe(true);
362 });
363
364 it('should contain platform and arch information', () => {
365 const error = new UnsupportedPlatformError({ platform: 'testPlatform', arch: 'testArch' });
366 expect(error.platform).toBe('testPlatform');
367 expect(error.arch).toBe('testArch');
368 });
369
370 it('should maintain properties for empty values', () => {
371 const error = new UnsupportedPlatformError({ platform: '', arch: '' });
372 expect(error.platform).toBe('');
373 expect(error.arch).toBe('');
374 });
375 });
376
377 describe('Type Tests', () => {
378 it.effect('should return correct PlatformArch type for supported combinations', () =>
379 Effect.gen(function* () {
380 // This test verifies TypeScript types at runtime
381 const testType = (
382 platform: string,
383 arch: string
384 ): Effect.Effect<PlatformArch, UnsupportedPlatformError> =>
385 detectPlatform.pipe(Effect.provide(createMockNodeOs(platform as NodeJS.Platform, arch)));
386
387 // These calls should compile without TypeScript errors
388 expect(yield* testType('darwin', 'x64')).toEqual({ platform: 'darwin', arch: 'x64' });
389 expect(yield* testType('darwin', 'arm64')).toEqual({
390 platform: 'darwin',
391 arch: 'aarch64',
392 });
393 expect(yield* testType('linux', 'aarch64')).toEqual({
394 platform: 'linux',
395 arch: 'aarch64',
396 });
397 })
398 );
399 });
400 });