Skip to content

Commit 15d9f2d

Browse files
committed
perf(tests): stop copilot handler tests from loading the executor, block, and trigger registries
The router test stubs every handler implementation module so the handler map binds without loading the executor and block registry; the 90s timeout goes. Handler, VFS, payload, and request tests mock the execution-side leaves and catalog registries they never exercise. Source edges that dragged in barrels for a single symbol now import the defining module: the Mothership block icon from @sim/emcn/icons, contract schemas from their contract file, and autolayout constants from new @sim/workflow-renderer/dimensions and /note-content subpaths. lib/copilot: 141 files, per-file collect+run 74.2s -> 37.4s, wall 19.1s -> 15.0s router.test.ts: 15.1s -> 0.45s
1 parent aa19e68 commit 15d9f2d

22 files changed

Lines changed: 130 additions & 18 deletions

File tree

‎apps/sim/blocks/blocks/mothership.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Blimp } from '@sim/emcn'
1+
import { Blimp } from '@sim/emcn/icons'
22
import type { BlockConfig } from '@/blocks/types'
33
import type { ToolResponse } from '@/tools/types'
44

‎apps/sim/hooks/queries/environment.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
removeWorkspaceEnvironmentContract,
77
savePersonalEnvironmentContract,
88
upsertWorkspaceEnvironmentContract,
9-
} from '@/lib/api/contracts'
9+
} from '@/lib/api/contracts/environment'
1010
import type { WorkspaceEnvironmentData } from '@/lib/environment/api'
1111
import { fetchPersonalEnvironment, fetchWorkspaceEnvironment } from '@/lib/environment/api'
1212
import { invalidateSelectorQueries } from '@/hooks/queries/utils/selector-keys'

‎apps/sim/lib/copilot/chat/payload.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ vi.mock('@/tools/registry', () => ({
5959
},
6060
}))
6161

62+
/** Denied-operation projection walks the block map only for blocks the mocked tool list never names. */
63+
vi.mock('@/blocks/registry-maps', () => ({ BLOCK_REGISTRY: {}, BLOCK_META_REGISTRY: {} }))
64+
6265
vi.mock('@/tools/utils', () => ({
6366
getLatestVersionTools: vi.fn((input) => input),
6467
stripVersionSuffix: vi.fn((toolId: string) => toolId),

‎apps/sim/lib/copilot/chat/process-contents-log-projection.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import {
2121
permissionGroupScopeMockFns,
2222
resetPermissionGroupScopeMock,
2323
workflowAuthzMockFns,
24+
workflowsUtilsMock,
2425
} from '@sim/testing'
2526
import { beforeEach, describe, expect, it, vi } from 'vitest'
2627
import type { ChatContext } from '@/stores/panel'
2728

2829
vi.mock('@/lib/permission-groups/config-scope.server', () => permissionGroupScopeMock)
2930

31+
/** Folder listing is untouched by `@log` mentions; the real module drags in the block and trigger registries. */
32+
vi.mock('@/lib/workflows/utils', () => workflowsUtilsMock)
33+
3034
import { processContextsServer } from '@/lib/copilot/chat/process-contents'
3135
import { DEFAULT_PERMISSION_GROUP_CONFIG } from '@/lib/permission-groups/fields'
3236

‎apps/sim/lib/copilot/request/go/stream.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import {
1111
MothershipStreamV1ToolPhase,
1212
} from '@/lib/copilot/generated/mothership-stream-v1'
1313

14+
/** Table side effects are not exercised here, and the real module loads the table application layer. */
15+
vi.mock('@/lib/copilot/request/tools/tables', () => ({
16+
maybeWriteOutputToTable: vi.fn(async (_toolName, _params, result) => result),
17+
maybeWriteReadCsvToTable: vi.fn(async (_toolName, _params, result) => result),
18+
}))
19+
1420
vi.mock('@/lib/copilot/request/session', async () => {
1521
const actual = await vi.importActual<typeof import('@/lib/copilot/request/session')>(
1622
'@/lib/copilot/request/session'

‎apps/sim/lib/copilot/request/handlers/handlers.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ vi.mock('@/lib/copilot/async-runs/repository', () => ({
6565
claimWorkflowToolExecution,
6666
}))
6767

68+
/** Table side effects are not exercised here, and the real module loads the table application layer. */
69+
vi.mock('@/lib/copilot/request/tools/tables', () => ({
70+
maybeWriteOutputToTable: vi.fn(async (_toolName, _params, result) => result),
71+
maybeWriteReadCsvToTable: vi.fn(async (_toolName, _params, result) => result),
72+
}))
73+
6874
vi.mock('@/lib/copilot/request/tools/client', () => ({
6975
waitForClientToolCompletion,
7076
waitForToolCompletion,

‎apps/sim/lib/copilot/request/lifecycle/resume-leg-context.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { describe, expect, it, vi } from 'vitest'
22
import { MothershipStreamV1CompletionStatus } from '@/lib/copilot/generated/mothership-stream-v1'
33
import { createStreamingContext } from '@/lib/copilot/request/context/request-context'
4+
5+
/** Table side effects are not exercised here, and the real module loads the table application layer. */
6+
vi.mock('@/lib/copilot/request/tools/tables', () => ({
7+
maybeWriteOutputToTable: vi.fn(async (_toolName, _params, result) => result),
8+
maybeWriteReadCsvToTable: vi.fn(async (_toolName, _params, result) => result),
9+
}))
10+
411
import { makeResumeLegContext, mergeResumeLegOutputs } from '@/lib/copilot/request/lifecycle/run'
512

613
// Guards the makeResumeLegContext / mergeResumeLegOutputs contract: the two MUST

‎apps/sim/lib/copilot/server/agent-url.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { db } from '@sim/db'
22
import { settings, user } from '@sim/db/schema'
33
import { eq } from 'drizzle-orm'
4-
import { type MothershipEnvironment, mothershipEnvironmentSchema } from '@/lib/api/contracts'
4+
import { type MothershipEnvironment, mothershipEnvironmentSchema } from '@/lib/api/contracts/user'
55
import { SIM_AGENT_API_URL, SIM_AGENT_API_URL_DEFAULT } from '@/lib/copilot/constants'
66
import { env } from '@/lib/core/config/env'
77

‎apps/sim/lib/copilot/tool-executor/router.test.ts‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,50 @@
22
* @vitest-environment node
33
*/
44

5-
import { describe, expect, it } from 'vitest'
5+
import { describe, expect, it, vi } from 'vitest'
6+
7+
/**
8+
* The handler map is a wiring table from tool id to implementation. Only its
9+
* shape is asserted here, so every implementation module it imports is stubbed
10+
* — loading them for real reaches the block registry, the executor, and most
11+
* of `lib/`. Every export resolves to a mock function, which is all the table
12+
* needs to bind.
13+
*/
14+
const { stubHandlerModule } = vi.hoisted(() => ({
15+
stubHandlerModule: () =>
16+
new Proxy(
17+
{},
18+
{
19+
get: (_target, name) => (typeof name === 'string' && name !== 'then' ? vi.fn() : undefined),
20+
has: (_target, name) => typeof name === 'string' && name !== 'then',
21+
}
22+
),
23+
}))
24+
25+
vi.mock('@/lib/copilot/tools/handlers/deployment/custom-block', stubHandlerModule)
26+
vi.mock('@/lib/copilot/tools/handlers/deployment/deploy', stubHandlerModule)
27+
vi.mock('@/lib/copilot/tools/handlers/deployment/manage', stubHandlerModule)
28+
vi.mock('@/lib/copilot/tools/handlers/function-execute', stubHandlerModule)
29+
vi.mock('@/lib/copilot/tools/handlers/integration-tools', stubHandlerModule)
30+
vi.mock('@/lib/copilot/tools/handlers/management/connect-slack-bot', stubHandlerModule)
31+
vi.mock('@/lib/copilot/tools/handlers/management/manage-credential', stubHandlerModule)
32+
vi.mock('@/lib/copilot/tools/handlers/management/manage-custom-tool', stubHandlerModule)
33+
vi.mock('@/lib/copilot/tools/handlers/management/manage-mcp-tool', stubHandlerModule)
34+
vi.mock('@/lib/copilot/tools/handlers/management/manage-sandbox', stubHandlerModule)
35+
vi.mock('@/lib/copilot/tools/handlers/management/manage-skill', stubHandlerModule)
36+
vi.mock('@/lib/copilot/tools/handlers/materialize-file', stubHandlerModule)
37+
vi.mock('@/lib/copilot/tools/handlers/oauth', stubHandlerModule)
38+
vi.mock('@/lib/copilot/tools/handlers/resources', stubHandlerModule)
39+
vi.mock('@/lib/copilot/tools/handlers/restore-resource', stubHandlerModule)
40+
vi.mock('@/lib/copilot/tools/handlers/run-code', stubHandlerModule)
41+
vi.mock('@/lib/copilot/tools/handlers/vfs', stubHandlerModule)
42+
vi.mock('@/lib/copilot/tools/handlers/vfs-mutate', stubHandlerModule)
43+
vi.mock('@/lib/copilot/tools/handlers/workflow/mutations', stubHandlerModule)
44+
vi.mock('@/lib/copilot/tools/handlers/workflow/queries', stubHandlerModule)
45+
46+
/** Server-router tools are appended to the map from their own registry, which this test does not cover. */
47+
vi.mock('@/lib/copilot/tools/server/router', () => ({ getRegisteredServerToolNames: () => [] }))
48+
649
import { hasHandler } from '@/lib/copilot/tool-executor/executor'
750
import { ensureHandlersRegistered } from '@/lib/copilot/tool-executor/register-handlers'
851
import {
@@ -21,11 +64,9 @@ describe('workflow-run cancellation tool routing', () => {
2164
expect(toolRequiresApproval('cancel_workflow_run')).toBe(true)
2265
})
2366

24-
// Registration loads the whole handler map on first use, which is most of
25-
// `lib/` — well past the default 10s under a fully parallel run.
2667
it('registers the Sim cancellation handler', async () => {
2768
await ensureHandlersRegistered()
2869

2970
expect(hasHandler('cancel_workflow_run')).toBe(true)
30-
}, 90_000)
71+
})
3172
})

‎apps/sim/lib/copilot/tools/handlers/materialize-file.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ vi.mock('@/lib/copilot/vfs/path-utils', () => ({
108108
}))
109109

110110
vi.mock('@/lib/workflows/operations/import-export', () => ({ parseWorkflowJson: vi.fn() }))
111+
/** Only the import size cap is read from `import-workflow`; its orchestration dependency is the whole deploy graph. */
112+
vi.mock('@/lib/workflows/orchestration', () => ({
113+
performCreateWorkflow: vi.fn(),
114+
performCreateWorkflowTransition: vi.fn(),
115+
}))
111116
vi.mock('@/lib/workflows/persistence/utils', () => ({ saveWorkflowToNormalizedTables: vi.fn() }))
112117
vi.mock('@/lib/workflows/utils', () => ({ deduplicateWorkflowName: vi.fn() }))
113118
vi.mock('@/app/api/v1/admin/types', () => ({ extractWorkflowMetadata: vi.fn() }))

0 commit comments

Comments
 (0)