Open History to see the last commit
1import { defineRule } from "@oxlint/plugins";
2import * as Option from "effect/Option";
3
4import { getPropertyName, isIdentifier, unwrapExpression } from "../utils.ts";
5
6const TEST_FILE_PATTERN = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
7const EFFECT_RUNTIME_METHODS = new Set([
8 "runCallback",
9 "runCallbackWith",
10 "runFork",
11 "runForkWith",
12 "runPromise",
13 "runPromiseExit",
14 "runPromiseExitWith",
15 "runPromiseWith",
16 "runSync",
17 "runSyncExit",
18 "runSyncExitWith",
19 "runSyncWith",
20]);
21
22// Existing manual runners are tracked as debt. The rule permits no net-new
23// occurrences in these files, while unlisted test files must have zero.
24const LEGACY_BASELINE = new Map<string, number>([
25 ["apps/mobile/src/features/agent-awareness/liveActivityPreferences.test.ts", 1],
26 ["apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts", 2],
27 ["apps/mobile/src/state/use-remote-environment-registry.test.ts", 2],
28 ["apps/server/src/orchestration/commandInvariants.test.ts", 6],
29 ["apps/server/src/orchestration/Layers/CheckpointReactor.test.ts", 42],
30 ["apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts", 5],
31 ["apps/server/src/orchestration/Layers/OrchestrationReactor.test.ts", 4],
32 ["apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts", 70],
33 ["apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts", 31],
34 ["apps/server/src/orchestration/Layers/ThreadDeletionReactor.test.ts", 2],
35 ["apps/server/src/orchestration/projector.test.ts", 20],
36 ["apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts", 4],
37 ["apps/server/src/provider/acp/CursorAcpSupport.test.ts", 1],
38 ["apps/server/src/provider/Layers/ClaudeAdapter.test.ts", 2],
39 ["apps/server/src/provider/Layers/CodexAdapter.test.ts", 1],
40 ["apps/server/src/provider/Layers/CodexSessionRuntime.test.ts", 5],
41 ["apps/server/src/provider/Layers/CursorAdapter.test.ts", 1],
42 ["apps/server/src/provider/Layers/CursorProvider.test.ts", 4],
43 ["apps/server/src/provider/Layers/ProviderService.test.ts", 2],
44 ["apps/server/src/provider/Layers/ProviderSessionReaper.test.ts", 21],
45 ["apps/server/src/relay/AgentAwarenessRelay.test.ts", 4],
46 ["apps/server/src/server.test.ts", 1],
47 ["apps/web/src/cloud/dpop.test.ts", 2],
48 ["apps/web/src/environments/runtime/service.addSavedEnvironment.test.ts", 1],
49 ["oxlint-plugin-t3code/rules/no-manual-effect-runtime-in-tests.test.ts", 7],
50 ["packages/client-runtime/src/relay/managedRelayState.test.ts", 1],
51 ["packages/client-runtime/src/wsTransport.test.ts", 2],
52]);
53
54const baselineFor = (filename: string): number => {
55 const normalized = filename.replaceAll("\\", "/");
56 for (const [suffix, count] of LEGACY_BASELINE) {
57 if (normalized.endsWith(suffix)) return count;
58 }
59 return 0;
60};
61
62const manualRunnerName = (callee: unknown): Option.Option<string> => {
63 const expression = unwrapExpression(callee);
64 if (Option.isNone(expression) || expression.value.type !== "MemberExpression") {
65 return Option.none();
66 }
67
68 const object = unwrapExpression(expression.value.object);
69 const property = getPropertyName(expression.value.property);
70 if (Option.isNone(property)) return Option.none();
71
72 if (isIdentifier(object, "Effect") && EFFECT_RUNTIME_METHODS.has(property.value)) {
73 return Option.some(`Effect.${property.value}`);
74 }
75
76 if (isIdentifier(object, "ManagedRuntime") && property.value === "make") {
77 return Option.some("ManagedRuntime.make");
78 }
79
80 return Option.none();
81};
82
83export default defineRule({
84 meta: {
85 type: "problem",
86 docs: {
87 description:
88 "Disallow manually creating or running Effect runtimes in tests; use @effect/vitest.",
89 },
90 },
91 create(context) {
92 if (!TEST_FILE_PATTERN.test(context.filename)) return {};
93
94 const allowedCount = baselineFor(context.filename);
95 let occurrenceCount = 0;
96
97 return {
98 CallExpression(node) {
99 const runner = manualRunnerName(node.callee);
100 if (Option.isNone(runner)) return;
101
102 occurrenceCount++;
103 if (occurrenceCount <= allowedCount) return;
104
105 context.report({
106 node: node.callee,
107 message: `Do not use ${runner.value} in tests. Use @effect/vitest with it.effect(...) and test layers instead.`,
108 });
109 },
110 };
111 },
112});