2026-03-17 14:42:40 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2026 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
import { act } from 'react';
|
2026-03-17 14:42:40 -07:00
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
|
import { renderHook } from '../../test-utils/render.js';
|
|
|
|
|
import { useLogger } from './useLogger.js';
|
|
|
|
|
import {
|
|
|
|
|
sessionId as globalSessionId,
|
|
|
|
|
Logger,
|
|
|
|
|
type Storage,
|
|
|
|
|
type Config,
|
|
|
|
|
} from '@google/gemini-cli-core';
|
|
|
|
|
import { ConfigContext } from '../contexts/ConfigContext.js';
|
|
|
|
|
import type React from 'react';
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
let deferredInit: { resolve: (val?: unknown) => void };
|
|
|
|
|
|
2026-03-17 14:42:40 -07:00
|
|
|
// Mock Logger
|
|
|
|
|
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
|
|
|
|
|
const actual =
|
|
|
|
|
await importOriginal<typeof import('@google/gemini-cli-core')>();
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
Logger: vi.fn().mockImplementation((id: string) => ({
|
2026-03-20 20:08:29 +00:00
|
|
|
initialize: vi.fn().mockImplementation(
|
|
|
|
|
() =>
|
|
|
|
|
new Promise((resolve) => {
|
|
|
|
|
deferredInit = { resolve };
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-03-17 14:42:40 -07:00
|
|
|
sessionId: id,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('useLogger', () => {
|
|
|
|
|
const mockStorage = {} as Storage;
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
getSessionId: vi.fn().mockReturnValue('active-session-id'),
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should initialize with the global sessionId by default', async () => {
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderHook(() => useLogger(mockStorage));
|
|
|
|
|
|
|
|
|
|
expect(result.current).toBeNull();
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
deferredInit.resolve();
|
|
|
|
|
});
|
2026-03-17 14:42:40 -07:00
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
expect(result.current).not.toBeNull();
|
2026-03-17 14:42:40 -07:00
|
|
|
expect(Logger).toHaveBeenCalledWith(globalSessionId, mockStorage);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should initialize with the active sessionId from ConfigContext when available', async () => {
|
|
|
|
|
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
|
|
|
<ConfigContext.Provider value={mockConfig}>
|
|
|
|
|
{children}
|
|
|
|
|
</ConfigContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
const { result } = await renderHook(() => useLogger(mockStorage), {
|
|
|
|
|
wrapper,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.current).toBeNull();
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
deferredInit.resolve();
|
|
|
|
|
});
|
2026-03-17 14:42:40 -07:00
|
|
|
|
2026-03-20 20:08:29 +00:00
|
|
|
expect(result.current).not.toBeNull();
|
2026-03-17 14:42:40 -07:00
|
|
|
expect(Logger).toHaveBeenCalledWith('active-session-id', mockStorage);
|
|
|
|
|
});
|
|
|
|
|
});
|