@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, 06:00 UTC

next
ts 185 lines 6.65 KB
Raw
1 import { RuleTester } from 'oxlint/plugins-dev';
2
3 import { noOrDieInServiceConstructionRule } from './no-ordie-in-service-construction.ts';
4
5 const tester = new RuleTester({ languageOptions: { parserOptions: { lang: 'ts' } } });
6 const imports = `
7 import * as Context from "effect/Context";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10 `;
11 const constructionOrDie = { messageId: 'constructionOrDie', data: { member: 'orDie' } };
12 const constructionOptions = `{
13 make: Effect.gen(function* () {
14 const token = yield* load.pipe(Effect.orDie);
15 return { token };
16 }),
17 }`;
18 const serviceCall = (options: string) =>
19 `Context.Service<Client, ClientShape>()("Client", ${options})`;
20
21 tester.run('anti-slop-effect/no-ordie-in-service-construction', noOrDieInServiceConstructionRule, {
22 valid: [
23 `import { Layer } from "effect";
24 function build(Layer: OtherLibrary) { return live.pipe(Layer.orDie); }`,
25 `${imports}
26 const ordinary = define(({ make: Effect.gen(function* () {
27 return yield* load.pipe(Effect.orDie);
28 }) } satisfies Options));
29 class NotService extends (define(({ make: Effect.gen(function* () {
30 return yield* load.pipe(Effect.orDie);
31 }) } as Options)) as Constructor) {}`,
32 `${imports}
33 class Store extends (Context.Service<Store, StoreShape>()("Store", ({
34 make: Effect.gen(function* () {
35 return { find: () => Effect.gen(function* () {
36 return yield* load.pipe(Effect.orDie);
37 }) };
38 }),
39 } satisfies Options)) as Constructor) {}`,
40 `import { Context } from "other-library";
41 import { Effect } from "effect";
42 class OtherService extends (Context.Service<OtherService, OtherShape>()("Other", ({
43 make: Effect.gen(function* () { return yield* load.pipe(Effect.orDie); }),
44 } satisfies Options)) as Constructor) {}`,
45 // orDie inside an operation of the returned service.
46 `${imports}
47 export class Store extends Context.Service<Store, StoreShape>()("@app/Store", {
48 make: Effect.gen(function* () {
49 const sql = yield* Sql;
50 return {
51 find: (id: string) => Effect.gen(function* () {
52 return yield* sql.find(id).pipe(Effect.orDie);
53 }),
54 list: Effect.orDie(sql.list),
55 };
56 }),
57 }) {
58 static readonly layer = Layer.effect(this, this.make);
59 }`,
60 // orDie inside an Effect.fn operation and in an effect make defines without running it.
61 `${imports}
62 export class Store extends Context.Service<Store, StoreShape>()("@app/Store", {
63 make: Effect.gen(function* () {
64 const find = Effect.fn("Store.find")(function* (id: string) {
65 return yield* lookup(id).pipe(Effect.orDie);
66 });
67 const unwrap = Effect.fnUntraced(function* (id: string) { return id; }, Effect.orDie);
68 const collect = Stream.runCollect(entries).pipe(Effect.orDie);
69 return { find, unwrap, collect };
70 }),
71 }) {
72 static readonly layer = Layer.effect(this, this.make);
73 }`,
74 // orDie outside service construction.
75 `${imports}
76 const program = Effect.gen(function* () {
77 yield* risky.pipe(Effect.orDie);
78 });`,
79 `${imports}
80 class Store extends Context.Service<Store, StoreShape>()("Store", {
81 make: Effect.gen(function* () {
82 return {
83 nested: { later: Effect.gen(function* () { return yield* load.pipe(Effect.orDie); }) },
84 [key]: load.pipe(Effect.orDie),
85 };
86 }),
87 }) {}`,
88 {
89 filename: 'store.test.ts',
90 code: `${imports}
91 const layer = Store.layer.pipe(Layer.orDie);`,
92 },
93 // Not Effect's Layer.
94 `
95 import * as Layer from "other-library";
96 const layer = live.pipe(Layer.orDie);`,
97 ],
98 invalid: [
99 ...[
100 serviceCall(`(${constructionOptions})`),
101 serviceCall(`${constructionOptions} as Options`),
102 serviceCall(`<Options>${constructionOptions}`),
103 serviceCall(`${constructionOptions} satisfies Options`),
104 `(${serviceCall(constructionOptions)})`,
105 `(${serviceCall(constructionOptions)} as Constructor)`,
106 `(<Constructor>${serviceCall(constructionOptions)})`,
107 `(${serviceCall(constructionOptions)} satisfies Constructor)`,
108 `(((${serviceCall(`((${constructionOptions} as Options) satisfies Options)`)} as Constructor) satisfies Constructor))`,
109 ].map(extendsExpression => ({
110 code: `${imports} class Client extends ${extendsExpression} {}`,
111 errors: [constructionOrDie],
112 })),
113 {
114 code: `${imports}
115 class Client extends Context.Service<Client, ClientShape>()("Client", {
116 make: Effect.gen(function* () {
117 return {
118 token: yield* load.pipe(Effect.orDie),
119 nested: { token: yield* load.pipe(Effect.orDie) },
120 [yield* key.pipe(Effect.orDie)]: true,
121 };
122 }),
123 }) {}`,
124 errors: [constructionOrDie, constructionOrDie, constructionOrDie],
125 },
126 {
127 code: `import { Context as Ctx, Effect as Fx, Layer as L } from "effect";
128 class Client extends Ctx.Service<Client, {}>()("Client") {
129 static make = Fx.gen(function* () {
130 yield* load.pipe(Fx.orDie);
131 return {};
132 });
133 }
134 const layer = live.pipe(L.orDie);`,
135 errors: [constructionOrDie, { messageId: 'layerOrDie' }],
136 },
137 {
138 code: `${imports}
139 export class Client extends Context.Service<Client, ClientShape>()("@app/Client", {
140 make: Effect.gen(function* () {
141 const token = yield* Config.string("TOKEN").pipe(Effect.orDie);
142 return { token };
143 }),
144 }) {
145 static readonly layer = Layer.effect(this, this.make);
146 }`,
147 errors: [constructionOrDie],
148 },
149 {
150 code: `${imports}
151 export class Client extends Context.Service<Client, ClientShape>()("@app/Client", {
152 make: Effect.gen(function* () {
153 return {};
154 }).pipe(Effect.orDieWith((error) => error)),
155 }) {
156 static readonly layer = Layer.effect(this, this.make);
157 }`,
158 errors: [{ messageId: 'constructionOrDie', data: { member: 'orDieWith' } }],
159 },
160 {
161 code: `${imports}
162 export class Database extends Context.Service<Database, DatabaseShape>()("@app/Database") {
163 static readonly make = (file: string) => Effect.gen(function* () {
164 yield* migrate(file).pipe(Effect.orDie);
165 return {};
166 });
167 static readonly layer = (file: string) => Layer.effect(this, this.make(file));
168 }`,
169 errors: [constructionOrDie],
170 },
171 {
172 code: `${imports}
173 export const live = Layer.effect(Store, Effect.gen(function* () {
174 const key = yield* Effect.suspend(() => loadKey.pipe(Effect.orDie));
175 return { key };
176 }));`,
177 errors: [constructionOrDie],
178 },
179 {
180 code: `${imports}
181 const logLayer = layerJsonl(path).pipe(Layer.orDie);`,
182 errors: [{ messageId: 'layerOrDie' }],
183 },
184 ],
185 });