Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
test(cli): reset in-process memos between test cases
A memoized tool version or connected-account list lives for the whole process, so within one vitest file the first case that lets the lookup succeed would hand its answer to every later case sharing the key. Expose `clear()` on each memo plus a process-wide `clearInProcessMemos`, and call the latter from the shared vitest setup before every test. Claude-Session: https://claude.ai/code/session_01MTb47vexN35pLGsSZwBmrJ
jkomyno committed
Sep 14, 2026 at 20:55 UTC
2a0d52c315f8cc92b9eb8241a245ed5944cf6e36
3 files changed
+52
-3
ts/packages/cli/src/utils/memoize-in-process.ts
+22
-2
@@ -1,5 +1,23 @@
1
import { Deferred, Effect, Exit } from 'effect';
2
3
+export interface MemoizedInProcess<I, A, E, R> {
4
+ (input: I): Effect.Effect<A, E, R>;
5
+ /** Forgets every cached result of this memo. */
6
+ readonly clear: () => void;
7
+}
8
+
9
+const memos = new Set<{ readonly clear: () => void }>();
10
+
11
+/**
12
+ * Forgets every result memoized by {@link memoizeInProcess} anywhere in the
13
+ * process. A vitest `beforeEach` calls this so a value cached by one test case
14
+ * (a tool version, a connected-account list) cannot leak into the next one;
15
+ * production code has no reason to call it.
16
+ */
17
+export const clearInProcessMemos = (): void => {
18
+ for (const memo of memos) memo.clear();
19
+};
20
+
21
/**
22
* Memoizes an Effect per key for the lifetime of the process, sharing one run
23
* between concurrent callers. A success stays cached; a failure or defect is
@@ -19,7 +37,7 @@ import { Deferred, Effect, Exit } from 'effect';
37
export const memoizeInProcess = <I, A, E, R>(options: {
38
readonly keyOf: (input: I) => string;
39
readonly make: (input: I) => Effect.Effect<A, E, R>;
22
-}): ((input: I) => Effect.Effect<A, E, R>) => {
40
+}): MemoizedInProcess<I, A, E, R> => {
41
const cache = new Map<string, Deferred.Deferred<A, E>>();
42
43
const load = (input: I): Effect.Effect<A, E, R> =>
@@ -49,5 +67,7 @@ export const memoizeInProcess = <I, A, E, R>(options: {
67
})
68
).pipe(Effect.flatMap(cell => Deferred.await(cell)));
69
52
- return load;
70
+ const memo = Object.assign(load, { clear: () => cache.clear() });
71
+ memos.add(memo);
72
+ return memo;
73
};
ts/packages/cli/test/__utils__/vitest.setup.ts
+5
@@ -5,6 +5,7 @@
5
import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
6
import { Effect, FileSystem } from 'effect';
7
import { afterEach, beforeEach, vi } from 'vitest';
8
+import { clearInProcessMemos } from 'src/utils/memoize-in-process';
9
10
// Point every test at a fresh, empty config directory so nothing reads or
11
// writes the developer's real `~/.composio`. An empty directory holds no
@@ -17,6 +18,10 @@ let testConfigDirectory: string | undefined;
18
const runFs = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem>): Promise<A> =>
19
Effect.runPromise(effect.pipe(Effect.provide(BunFileSystem.layer)));
20
21
+// Process-lifetime memos (tool versions, connected-account lists) would
22
+// otherwise carry one case's mocked answer into the next.
23
+beforeEach(clearInProcessMemos);
24
+
25
beforeEach(async () => {
26
testConfigDirectory = await runFs(
27
Effect.flatMap(FileSystem.FileSystem, fs =>
ts/packages/cli/test/src/utils/memoize-in-process.test.ts
+25
-1
@@ -1,6 +1,6 @@
1
import { describe, expect, it } from '@effect/vitest';
2
import { Deferred, Effect, Exit, Fiber } from 'effect';
3
-import { memoizeInProcess } from 'src/utils/memoize-in-process';
3
+import { clearInProcessMemos, memoizeInProcess } from 'src/utils/memoize-in-process';
4
5
describe('memoizeInProcess', () => {
6
it.effect('runs the effect once per key and shares the result', () =>
@@ -114,4 +114,28 @@ describe('memoizeInProcess', () => {
114
expect(calls).toBe(2);
115
})
116
);
117
+
118
+ it.effect('clear() forgets cached results, for every memo in the process', () =>
119
+ Effect.gen(function* () {
120
+ let calls = 0;
121
+ const load = memoizeInProcess({
122
+ keyOf: (key: string) => key,
123
+ make: () => Effect.sync(() => ++calls),
124
+ });
125
+ const other = memoizeInProcess({
126
+ keyOf: (key: string) => key,
127
+ make: () => Effect.sync(() => ++calls),
128
+ });
129
+
130
+ expect(yield* load('k')).toBe(1);
131
+ expect(yield* other('k')).toBe(2);
132
+ load.clear();
133
+ expect(yield* load('k')).toBe(3);
134
+ expect(yield* other('k')).toBe(2);
135
+
136
+ clearInProcessMemos();
137
+ expect(yield* load('k')).toBe(4);
138
+ expect(yield* other('k')).toBe(5);
139
+ })
140
+ );
141
});