Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions dev/test/cli/cli_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {
DatabaseSessionService,
getSessionServiceFromUri,
InMemorySessionService,
LogLevel,
setLogLevel,
Expand Down Expand Up @@ -47,18 +48,29 @@ vi.mock('../../src/version', () => ({
}));

vi.mock('@google/adk', async (importOriginal) => {
const actual = await importOriginal();
const actual = await importOriginal<typeof import('@google/adk')>();
return {
...(actual as object),
...actual,
setLogLevel: vi.fn(),
getSessionServiceFromUri: vi.fn(actual.getSessionServiceFromUri),
};
});

// Stands in for a developer's shell exporting DATABASE_URL. It has to be a
// plain assignment made before the first `vi.stubEnv`, because the automatic
// unstub between tests restores each variable to the value it had at that
// point: a stub installed later would be erased rather than inherited.
process.env.DATABASE_URL = 'postgresql://ambient:pass@localhost:5432/ambient';

describe('CLI Entrypoint', () => {
let program: ReturnType<typeof createProgram>;

beforeEach(() => {
vi.clearAllMocks();
// Every web/api_server/run case resolves a session service, so an ambient
// DATABASE_URL would silently swap the in-memory service they assume for a
// database-backed one.
vi.stubEnv('DATABASE_URL', undefined);
program = createProgram();
program.exitOverride();
});
Expand Down Expand Up @@ -223,6 +235,15 @@ describe('CLI Entrypoint', () => {
const args = vi.mocked(runAgent).mock.calls[0][0];
expect(args.sessionService).toBeInstanceOf(DatabaseSessionService);
});

// The only case that leaves DATABASE_URL unstubbed, so the ambient value
// set at module scope is what reaches the CLI unless `beforeEach` scrubs
// it.
it('should ignore an ambient DATABASE_URL', async () => {
await parse(['web']);

expect(getSessionServiceFromUri).toHaveBeenCalledWith('memory://');
});
});

describe('command: create', () => {
Expand Down
Loading