-
Notifications
You must be signed in to change notification settings - Fork 168
test: add unit tests for ActiveStreamingTool and ReadonlyContext #464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nuthalapativarun
wants to merge
2
commits into
google:main
Choose a base branch
from
nuthalapativarun:test/unit-tests-active-streaming-oauth2-readonly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {describe, expect, it} from 'vitest'; | ||
| import {ActiveStreamingTool} from '../../src/agents/active_streaming_tool.js'; | ||
| import {LiveRequestQueue} from '../../src/agents/live_request_queue.js'; | ||
|
|
||
| describe('ActiveStreamingTool', () => { | ||
| it('should have undefined fields when constructed with no params', () => { | ||
| const tool = new ActiveStreamingTool(); | ||
| expect(tool.task).toBeUndefined(); | ||
| expect(tool.stream).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should set task when constructed with only task param', () => { | ||
| const task = Promise.resolve(); | ||
| const tool = new ActiveStreamingTool({task}); | ||
| expect(tool.task).toBe(task); | ||
| expect(tool.stream).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should set stream when constructed with only stream param', () => { | ||
| const stream = new LiveRequestQueue(); | ||
| const tool = new ActiveStreamingTool({stream}); | ||
| expect(tool.task).toBeUndefined(); | ||
| expect(tool.stream).toBe(stream); | ||
| }); | ||
|
|
||
| it('should set both task and stream when both params are provided', () => { | ||
| const task = Promise.resolve(); | ||
| const stream = new LiveRequestQueue(); | ||
| const tool = new ActiveStreamingTool({task, stream}); | ||
| expect(tool.task).toBe(task); | ||
| expect(tool.stream).toBe(stream); | ||
| }); | ||
|
|
||
| it('should allow fields to be read independently after construction', () => { | ||
| const task = new Promise<void>((resolve) => setTimeout(resolve, 100)); | ||
| const stream = new LiveRequestQueue(); | ||
| const tool = new ActiveStreamingTool({task, stream}); | ||
|
|
||
| const readTask = tool.task; | ||
| const readStream = tool.stream; | ||
| expect(readTask).toBe(task); | ||
| expect(readStream).toBe(stream); | ||
| expect(readStream).toBeInstanceOf(LiveRequestQueue); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2026 |
||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| InvocationContext, | ||
| LlmAgent, | ||
| PluginManager, | ||
| ReadonlyContext, | ||
| Session, | ||
| } from '@google/adk'; | ||
| import {Content} from '@google/genai'; | ||
| import {describe, expect, it} from 'vitest'; | ||
|
|
||
| function makeSession(overrides: Partial<Session> = {}): Session { | ||
| return { | ||
| id: 'session-1', | ||
| appName: 'test-app', | ||
| userId: 'user-1', | ||
| state: {}, | ||
| events: [], | ||
| lastUpdateTime: Date.now(), | ||
| ...overrides, | ||
| } as Session; | ||
| } | ||
|
|
||
| function makeInvocationContext( | ||
| overrides: Partial<{ | ||
| invocationId: string; | ||
| userContent: Content; | ||
| session: Session; | ||
| agentName: string; | ||
| }> = {}, | ||
| ): InvocationContext { | ||
| const agent = new LlmAgent({name: overrides.agentName ?? 'test-agent'}); | ||
| const session = overrides.session ?? makeSession(); | ||
|
|
||
| return new InvocationContext({ | ||
| invocationId: overrides.invocationId ?? 'inv-123', | ||
| agent, | ||
| session, | ||
| userContent: overrides.userContent, | ||
| pluginManager: new PluginManager(), | ||
| }); | ||
| } | ||
|
|
||
| describe('ReadonlyContext', () => { | ||
| it('should construct without errors given a minimal InvocationContext', () => { | ||
| const ic = makeInvocationContext(); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx).toBeInstanceOf(ReadonlyContext); | ||
| }); | ||
|
|
||
| it('invocationId delegates to the underlying InvocationContext', () => { | ||
| const ic = makeInvocationContext({invocationId: 'my-inv-id'}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx.invocationId).toBe('my-inv-id'); | ||
| }); | ||
|
|
||
| it('userId delegates to the underlying session userId', () => { | ||
| const session = makeSession({userId: 'alice'}); | ||
| const ic = makeInvocationContext({session}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx.userId).toBe('alice'); | ||
| }); | ||
|
|
||
| it('sessionId delegates to the underlying session id', () => { | ||
| const session = makeSession({id: 'sess-42'}); | ||
| const ic = makeInvocationContext({session}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx.sessionId).toBe('sess-42'); | ||
| }); | ||
|
|
||
| it('agentName delegates to the underlying agent name', () => { | ||
| const ic = makeInvocationContext({agentName: 'my-agent'}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx.agentName).toBe('my-agent'); | ||
| }); | ||
|
|
||
| it('userContent delegates to the underlying InvocationContext userContent', () => { | ||
| const content: Content = { | ||
| role: 'user', | ||
| parts: [{text: 'hello'}], | ||
| }; | ||
| const ic = makeInvocationContext({userContent: content}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx.userContent).toEqual(content); | ||
| }); | ||
|
|
||
| it('userContent is undefined when not set on InvocationContext', () => { | ||
| const ic = makeInvocationContext(); | ||
| const ctx = new ReadonlyContext(ic); | ||
| expect(ctx.userContent).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('state returns a State wrapping the session state values', () => { | ||
| const session = makeSession({state: {foo: 'bar', count: 42}}); | ||
| const ic = makeInvocationContext({session}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| const state = ctx.state; | ||
| expect(state.get('foo')).toBe('bar'); | ||
| expect(state.get('count')).toBe(42); | ||
| }); | ||
|
|
||
| it('state returns a new State instance on each access', () => { | ||
| const session = makeSession({state: {x: 1}}); | ||
| const ic = makeInvocationContext({session}); | ||
| const ctx = new ReadonlyContext(ic); | ||
| const state1 = ctx.state; | ||
| const state2 = ctx.state; | ||
| // Each call returns a new wrapper around the same underlying data | ||
| expect(state1).not.toBe(state2); | ||
| expect(state1.get('x')).toBe(state2.get('x')); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2026