diff --git a/core/test/sessions/vertex_ai_session_service_test.ts b/core/test/sessions/vertex_ai_session_service_test.ts index 4a15053c4..2a6afc43f 100644 --- a/core/test/sessions/vertex_ai_session_service_test.ts +++ b/core/test/sessions/vertex_ai_session_service_test.ts @@ -5,8 +5,8 @@ */ import {Sessions} from '@google-cloud/vertexai/build/src/genai/sessions.js'; +import type {Session} from '@google/adk'; import {createEvent, State, VertexAiSessionService} from '@google/adk'; -import {Session} from '@google/adk/sessions/session.js'; import {ApiError} from '@google/genai'; import {beforeEach, describe, expect, it, vi} from 'vitest'; @@ -24,8 +24,8 @@ vi.mock('nodejs-vertexai', () => ({ import { isVertexAiConnectionString, quoteFilterLiteral, -} from '@google/adk/sessions/vertex_ai_session_service.js'; -import {logger} from '@google/adk/utils/logger.js'; +} from '../../src/sessions/vertex_ai_session_service.js'; +import {logger} from '../../src/utils/logger.js'; describe('isVertexAiConnectionString', () => { it('returns true for vertexai://', () => { diff --git a/tests/integration/agents/agent_with_sandbox_executor_test.ts b/tests/integration/agents/agent_with_sandbox_executor_test.ts index 17f952ba0..539e8b75c 100644 --- a/tests/integration/agents/agent_with_sandbox_executor_test.ts +++ b/tests/integration/agents/agent_with_sandbox_executor_test.ts @@ -6,9 +6,9 @@ import {Client} from '@google-cloud/vertexai'; import {AgentEngineSandboxCodeExecutor, LlmAgent} from '@google/adk'; -import {responseProcessor} from '@google/adk/agents/processors/code_execution_request_processor.js'; import {FinishReason} from '@google/genai'; import {describe, expect, it, vi} from 'vitest'; +import {responseProcessor} from '../../../core/src/agents/processors/code_execution_request_processor.js'; import { createRunner, GeminiWithMockResponses, diff --git a/tests/integration/vitest_alias_test.ts b/tests/integration/vitest_alias_test.ts new file mode 100644 index 000000000..ba763f717 --- /dev/null +++ b/tests/integration/vitest_alias_test.ts @@ -0,0 +1,57 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import {describe, expect, it} from 'vitest'; +import {workspaceAliases} from '../../vitest.config.js'; + +const REPO_ROOT = path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + '../..', +); + +/** + * Applies the alias entries the way `@rollup/plugin-alias` does for a `RegExp` + * `find`, so a silent revert to the prefix-matching string form matches nothing + * and fails these tests. + */ +function matchingAliases(specifier: string) { + return workspaceAliases.filter( + (entry) => entry.find instanceof RegExp && entry.find.test(specifier), + ); +} + +describe('workspaceAliases', () => { + it('matches only the bare package specifiers', () => { + expect(matchingAliases('@google/adk')).toHaveLength(1); + expect(matchingAliases('@google/adk-integrations')).toHaveLength(1); + + for (const deepSpecifier of [ + '@google/adk/sessions/session.js', + '@google/adk/agents/processors/code_execution_request_processor.js', + '@google/adk-integrations/foo.js', + '@google/adk-devtools', + ]) { + expect(matchingAliases(deepSpecifier)).toEqual([]); + } + }); + + it('resolves each package to its source root', () => { + expect(matchingAliases('@google/adk')).toEqual([ + { + find: expect.any(RegExp), + replacement: path.resolve(REPO_ROOT, 'core/src'), + }, + ]); + expect(matchingAliases('@google/adk-integrations')).toEqual([ + { + find: expect.any(RegExp), + replacement: path.resolve(REPO_ROOT, 'integrations/src'), + }, + ]); + }); +}); diff --git a/vitest.config.ts b/vitest.config.ts index 1b7eaee63..c4bfba0ec 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -20,6 +20,28 @@ const INTEGRATION_HOOK_TIMEOUT_MS = 120000; */ const INTEGRATION_TEST_TIMEOUT_MS = 60000; +/** + * Maps the workspace package names onto their source trees, shared by every + * test project. + * + * The patterns are anchored regexes rather than plain strings: a string `find` + * matches the package root *and every subpath under it*, so `'@google/adk'` + * also rewrote `@google/adk/sessions/session.js` to + * `core/src/sessions/session.js`. `core/package.json` and + * `integrations/package.json` export only `"."`, so that subpath resolves for + * nobody outside vitest: the alias made the harness accept imports that `tsc` + * (`moduleResolution: nodenext`) and Node both reject. + * + * Import an internal symbol through a relative `../src/...` path instead. + */ +export const workspaceAliases = [ + {find: /^@google\/adk$/, replacement: path.resolve(__dirname, './core/src')}, + { + find: /^@google\/adk-integrations$/, + replacement: path.resolve(__dirname, './integrations/src'), + }, +]; + export default defineConfig({ test: { poolOptions: { @@ -35,13 +57,7 @@ export default defineConfig({ test: { name: 'unit:core', environment: 'node', - alias: { - '@google/adk': path.resolve(__dirname, './core/src'), - '@google/adk-integrations': path.resolve( - __dirname, - './integrations/src', - ), - }, + alias: workspaceAliases, include: ['core/test/**/*_test.ts'], }, }, @@ -49,13 +65,7 @@ export default defineConfig({ test: { name: 'unit:dev', environment: 'node', - alias: { - '@google/adk': path.resolve(__dirname, './core/src'), - '@google/adk-integrations': path.resolve( - __dirname, - './integrations/src', - ), - }, + alias: workspaceAliases, include: ['dev/test/**/*_test.ts'], }, }, @@ -63,13 +73,7 @@ export default defineConfig({ test: { name: 'unit:integrations', environment: 'node', - alias: { - '@google/adk': path.resolve(__dirname, './core/src'), - '@google/adk-integrations': path.resolve( - __dirname, - './integrations/src', - ), - }, + alias: workspaceAliases, include: ['integrations/test/**/*_test.ts'], }, }, @@ -79,13 +83,7 @@ export default defineConfig({ environment: 'node', hookTimeout: INTEGRATION_HOOK_TIMEOUT_MS, testTimeout: INTEGRATION_TEST_TIMEOUT_MS, - alias: { - '@google/adk': path.resolve(__dirname, './core/src'), - '@google/adk-integrations': path.resolve( - __dirname, - './integrations/src', - ), - }, + alias: workspaceAliases, include: ['tests/integration/**/*_test.ts'], }, }, @@ -93,13 +91,7 @@ export default defineConfig({ test: { name: 'e2e', environment: 'node', - alias: { - '@google/adk': path.resolve(__dirname, './core/src'), - '@google/adk-integrations': path.resolve( - __dirname, - './integrations/src', - ), - }, + alias: workspaceAliases, include: ['tests/e2e/**/*_test.ts'], }, }, @@ -107,13 +99,7 @@ export default defineConfig({ test: { name: 'cross-language', environment: 'node', - alias: { - '@google/adk': path.resolve(__dirname, './core/src'), - '@google/adk-integrations': path.resolve( - __dirname, - './integrations/src', - ), - }, + alias: workspaceAliases, include: ['tests/cross_language/**/*_test.ts'], }, },