@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 111 lines 5.22 KB
Raw
1 import { RuleTester } from 'oxlint/plugins-dev';
2
3 import { requireCauseWhenNarrowingRule } from './require-cause-when-narrowing.ts';
4
5 const tester = new RuleTester({ languageOptions: { parserOptions: { lang: 'ts' } } });
6 const imports = `
7 import * as Effect from "effect/Effect";
8 `;
9 const missingCause = (operator: string, error: string) => ({
10 messageId: 'missingCause',
11 data: { operator, error },
12 });
13
14 tester.run('anti-slop-effect/require-cause-when-narrowing', requireCauseWhenNarrowingRule, {
15 valid: [
16 // A parameter can shadow an Effect import without referring to Effect.
17 `import { Effect } from "effect";
18 function recover(Effect: OtherLibrary) {
19 return load.pipe(Effect.mapError(() => new Failed()));
20 }`,
21 `${imports}
22 const a = decode(text).pipe(Effect.mapError((cause) => BadRequest.make({ reason: "Invalid JSON", cause })));
23 const b = decode(text).pipe(Effect.mapError((error) => new BadRequest({ reason: error.message })));
24 const c = decode(text).pipe(Effect.mapError(() => BadRequest.make({ reason: "Invalid JSON", ...details })));`,
25 // Capture before narrowing.
26 `${imports}
27 const a = decode(text).pipe(Effect.tapError(Effect.logError), Effect.mapError(() => BadRequest.make({ reason: "Invalid JSON" })));
28 const b = Effect.fn("decode")(function* () {}, Effect.tapCause(Effect.logError), Effect.catch(() => Effect.fail(new Failed())));
29 const c = Effect.mapError(Effect.tapErrorCause(decode(text), Effect.logError), () => new Failed());`,
30 // Specific expected tags are handled on purpose.
31 `${imports}
32 const a = load.pipe(Effect.catchTag("NotFound", () => Effect.fail(new Missing())));
33 const b = load.pipe(Effect.catchTags({ NotFound: () => Effect.fail(new Missing()) }));`,
34 // Recovering with a value, or constructing inside a nested function, is not narrowing.
35 `${imports}
36 const a = load.pipe(Effect.orElse(() => Effect.succeed(new Fallback())));
37 const b = load.pipe(Effect.catch(() => Effect.sync(() => new Fallback())));
38 const c = load.pipe(Effect.catch(() => Effect.succeed(Option.make(1))));`,
39 `${imports}
40 const preserved = load.pipe(Effect.mapError(() => retryable
41 ? new RetryFailed({ cause })
42 : Failed.make({ cause })));
43 const recovered = load.pipe(Effect.catch(() => retryable
44 ? Effect.succeed(new Fallback())
45 : Effect.sync(() => new OtherFallback())));
46 const deferred = load.pipe(Effect.catch(() => () => retryable
47 ? new RetryFailed()
48 : new Failed()));
49 const discarded = load.pipe(Effect.mapError(() => (new Discarded(), Failed.make({ cause }))));
50 const truthy = load.pipe(Effect.mapError(() => new Discarded() && Failed.make({ cause })));`,
51 // Not Effect's Effect.
52 `
53 import * as Effect from "other-library";
54 const a = load.pipe(Effect.mapError(() => new Failed()));`,
55 ],
56 invalid: [
57 {
58 code: `${imports}
59 const conditional = load.pipe(Effect.mapError(() => retryable ? new RetryFailed() : new Failed()));
60 const nested = load.pipe(Effect.mapError(() => retryable
61 ? Failed.make({ cause })
62 : offline ? Failed.make({ cause }) : new Offline()));
63 const logical = load.pipe(Effect.mapError(() => fallback ?? new Missing()));
64 const sequence = load.pipe(Effect.mapError(() => (record(), new Failed())));
65 const failed = load.pipe(Effect.catch(() => Effect.fail(retryable ? new RetryFailed() : new Failed())));`,
66 errors: [
67 missingCause('Effect.mapError', 'RetryFailed'),
68 missingCause('Effect.mapError', 'Offline'),
69 missingCause('Effect.mapError', 'Missing'),
70 missingCause('Effect.mapError', 'Failed'),
71 missingCause('Effect.catch', 'RetryFailed'),
72 ],
73 },
74 {
75 code: `import { Effect as Fx } from "effect";
76 const result = load.pipe(Fx.mapError(() => new Failed()));`,
77 errors: [missingCause('Effect.mapError', 'Failed')],
78 },
79 {
80 code: `${imports}
81 const a = decode(text).pipe(Effect.mapError(() => BadRequest.make({ reason: "Invalid JSON" })));`,
82 errors: [missingCause('Effect.mapError', 'BadRequest')],
83 },
84 {
85 code: `${imports}
86 const a = Effect.mapError(decode(text), (_error) => new McpDialFailed({ message: "bad frame" }));`,
87 errors: [missingCause('Effect.mapError', 'McpDialFailed')],
88 },
89 {
90 code: `${imports}
91 const a = read.pipe(Effect.catch(() => AuthUnavailable.make({ error: "unavailable" })));
92 const b = read.pipe(Effect.catchAll(() => Effect.fail(new Unavailable())));
93 const c = read.pipe(Effect.orElse(() => Effect.gen(function* () {
94 return yield* new Unavailable({ reason: "down" });
95 })));
96 const d = read.pipe(Effect.catchCause(() => { return Effect.fail(Errors.Unavailable.make()); }));`,
97 errors: [
98 missingCause('Effect.catch', 'AuthUnavailable'),
99 missingCause('Effect.catchAll', 'Unavailable'),
100 missingCause('Effect.orElse', 'Unavailable'),
101 missingCause('Effect.catchCause', 'Errors.Unavailable'),
102 ],
103 },
104 {
105 code: `${imports}
106 const a = read.pipe(Effect.tapError(Effect.logError)).pipe(Effect.map(f), Effect.mapError(() => new Failed()));
107 const b = read.pipe(Effect.mapError(() => new Failed()), Effect.tapError(Effect.logError));`,
108 errors: [missingCause('Effect.mapError', 'Failed')],
109 },
110 ],
111 });