Files
obsidian_ollama/tests/chat-view.test.ts
T

539 lines
20 KiB
TypeScript
Executable File

import { ChatView } from '../src/chat-view';
import { PluginSettings, OllamaMessage, ChatMessage, OllamaTool, ToolCall } from '../src/types';
// Mock Obsidian types
interface MockVault {
getMarkdownFiles: () => any[];
read: () => Promise<string>;
create: () => Promise<any>;
}
interface MockWorkspace {
getLeaf: () => any;
revealLeaf: () => void;
}
interface MockApp {
vault: MockVault;
workspace: MockWorkspace;
}
// Mock Obsidian module - ItemView must set this.app from the leaf
jest.mock('obsidian', () => ({
ItemView: jest.fn().mockImplementation(function (this: any, leaf: any) {
this.app = leaf.app;
}),
WorkspaceLeaf: jest.fn(),
Notice: jest.fn(),
}));
const mockSettings: PluginSettings = {
ollamaUrl: 'http://localhost:11434',
model: 'llama3',
vaultSearchLimit: 3,
maxMessageHistory: 50,
lastIndexTime: 0,
};
describe('ChatView', () => {
let view: ChatView;
let mockLeaf: any;
let mockApp: MockApp;
beforeEach(() => {
mockApp = {
vault: {
getMarkdownFiles: jest.fn().mockReturnValue([]),
read: jest.fn().mockResolvedValue(''),
create: jest.fn().mockResolvedValue(null),
},
workspace: {
getLeaf: jest.fn(),
revealLeaf: jest.fn(),
},
};
mockLeaf = {
view: null,
setViewState: jest.fn(),
app: mockApp,
};
view = new ChatView(mockLeaf as unknown as any, mockSettings);
// Obsidian's contentEl has a createEl helper that standard DOM lacks
// Unlike standard DOM, Obsidian elements can create nested elements with createEl
const contentDiv = document.createElement('div') as any;
// Create a factory function that captures the parent element
const createElementWithCreateEl = function (parent: any) {
return function (tag: string, options?: { cls?: string }) {
const el = document.createElement(tag);
if (options?.cls) {
el.classList.add(...options.cls.split(' '));
}
parent.appendChild(el);
// Add createEl to the new element so it can create nested elements
(el as any).createEl = createElementWithCreateEl(el);
return el;
};
};
contentDiv.createEl = createElementWithCreateEl(contentDiv);
view.contentEl = contentDiv;
});
describe('getViewType', () => {
it('should return the correct view type', () => {
expect(view.getViewType()).toBe('ollama-chat-view');
});
});
describe('getDisplayText', () => {
it('should return the correct display text', () => {
expect(view.getDisplayText()).toBe('Ollama Chat');
});
});
describe('onOpen', () => {
it('should call render and setup event listeners', async () => {
const renderSpy = jest.spyOn(view, 'render');
const setupSpy = jest.spyOn(view, 'setupEventListeners' as any);
await view.onOpen();
expect(renderSpy).toHaveBeenCalled();
expect(setupSpy).toHaveBeenCalled();
});
});
describe('onClose', () => {
it('should clean up resources and remove event listeners', async () => {
view['lastMessageEl'] = document.createElement('div');
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
const cancelSpy = jest.spyOn(view['ollamaClient'], 'cancelStream');
const removeSpy = jest.spyOn(view, 'removeEventListeners' as any);
await view.onClose();
expect(view['lastMessageEl']).toBeNull();
expect(view['sendButton']).toBeNull();
expect(view['inputEl']).toBeNull();
expect(cancelSpy).toHaveBeenCalled();
expect(removeSpy).toHaveBeenCalled();
});
});
describe('render', () => {
it('should render the chat interface', async () => {
await view.render();
expect(view.contentEl.querySelector('.ollama-chat-container')).not.toBeNull();
expect(view.contentEl.querySelector('.ollama-input-container')).not.toBeNull();
});
it('should not duplicate elements on re-render', async () => {
// First render
await view.render();
const firstRenderCount = view.contentEl.querySelectorAll('.ollama-message').length;
// Second render with no changes
await view.render();
const secondRenderCount = view.contentEl.querySelectorAll('.ollama-message').length;
expect(secondRenderCount).toBe(firstRenderCount);
});
it('should only render non-streaming messages', async () => {
view['messages'] = [
{ id: '1', role: 'user', content: 'test', timestamp: Date.now() },
{
id: '2',
role: 'assistant',
content: 'response',
timestamp: Date.now(),
isStreaming: true,
},
];
await view.render();
const messages = view.contentEl.querySelectorAll('.ollama-message');
expect(messages.length).toBe(1);
});
});
describe('handleUserInput', () => {
it('should handle user input and call ollamaClient', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'test' };
})()
);
await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled();
// Verify that messages were added to conversation history
expect((view as any).messages.length).toBeGreaterThan(0);
});
it('should handle empty user input gracefully', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = ' ';
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat');
await (view as any).handleUserInput(' ');
expect(chatSpy).not.toHaveBeenCalled();
});
it('should handle streaming responses and update UI', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'First ' };
yield { role: 'assistant', content: 'chunk ' };
yield { role: 'assistant', content: 'of response' };
})()
);
await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled();
// Should have processed all chunks
const messages = (view as any).messages;
const lastMessage = messages[messages.length - 1];
expect(lastMessage.isStreaming).toBe(false);
});
it('should limit conversation history to maxMessageHistory', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
// Mock a stream that returns quickly
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'response' };
})()
);
// Add enough messages to exceed maxMessageHistory
for (let i = 0; i < 60; i++) {
(view as any).messages.push({
id: `msg-${i}`,
role: 'user',
content: `message ${i}`,
timestamp: Date.now(),
});
}
await (view as any).handleUserInput('test');
// Should be limited to maxMessageHistory
expect((view as any).messages.length).toBeLessThanOrEqual(50);
});
it('should call vaultIndexer.searchVault with user input', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'search query';
const searchSpy = jest.spyOn(view['vaultIndexer'], 'searchVault').mockResolvedValue([]);
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'response' };
})()
);
await (view as any).handleUserInput('search query');
expect(searchSpy).toHaveBeenCalledWith('search query', 3); // Should use DEFAULT_VAULT_SEARCH_LIMIT
expect(chatSpy).toHaveBeenCalled();
});
it('should handle errors during user input gracefully', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
// Mock a stream that throws an error
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
throw new Error('Network error');
})()
);
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith('Error handling user input:', expect.any(Error));
});
it('should handle empty user input gracefully', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = ' ';
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat');
await (view as any).handleUserInput(' ');
expect(chatSpy).not.toHaveBeenCalled();
});
it('should handle streaming responses and update UI', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'First ' };
yield { role: 'assistant', content: 'chunk ' };
yield { role: 'assistant', content: 'of response' };
})()
);
await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled();
// Should have processed all chunks
const messages = (view as any).messages;
const lastMessage = messages[messages.length - 1];
expect(lastMessage.isStreaming).toBe(false);
});
it('should process tool calls with follow-up context', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield {
role: 'assistant',
content: 'test',
tool_calls: [
{
id: 'tool_1',
type: 'function',
function: { name: 'create_file', arguments: '{}' },
},
],
};
})()
);
const followUpSpy = jest
.spyOn(view['ollamaClient'], 'chat')
.mockResolvedValue({ role: 'assistant', content: ' follow-up' });
await (view as any).handleUserInput('test');
expect(followUpSpy).toHaveBeenCalled();
// Verify that tool calls resulted in follow-up messages
expect((view as any).messages.length).toBeGreaterThan(1);
});
it('should limit conversation history to maxMessageHistory', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
// Mock a stream that returns quickly
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'response' };
})()
);
// Add enough messages to exceed maxMessageHistory
for (let i = 0; i < 60; i++) {
(view as any).messages.push({
id: `msg-${i}`,
role: 'user',
content: `message ${i}`,
timestamp: Date.now(),
});
}
await (view as any).handleUserInput('test');
// Should be limited to maxMessageHistory
expect((view as any).messages.length).toBeLessThanOrEqual(50);
});
it('should call vaultIndexer.searchVault with user input', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'search query';
const searchSpy = jest.spyOn(view['vaultIndexer'], 'searchVault').mockResolvedValue([]);
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
yield { role: 'assistant', content: 'response' };
})()
);
await (view as any).handleUserInput('search query');
expect(searchSpy).toHaveBeenCalledWith('search query', 3); // Should use DEFAULT_VAULT_SEARCH_LIMIT
expect(chatSpy).toHaveBeenCalled();
});
it('should handle errors during user input gracefully', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
// Mock a stream that throws an error
const chatSpy = jest.spyOn(view['ollamaClient'], 'streamChat').mockReturnValue(
(async function* () {
throw new Error('Network error');
})()
);
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith('Error handling user input:', expect.any(Error));
});
describe('event handlers', () => {
it('should handle send button click', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const handler = view.getSendButtonClickHandler?.bind(view);
if (!handler) throw new Error('Handler not available');
await handler();
expect(handleSpy).toHaveBeenCalledWith('test');
expect((view['inputEl'] as HTMLTextAreaElement).value).toBe('');
});
it('should handle Enter key press in input', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter' }) as any;
await (view as any).inputKeyDownHandler!(event);
expect(handleSpy).toHaveBeenCalledWith('test');
expect((view['inputEl'] as HTMLTextAreaElement).value).toBe('');
});
it('should not handle Shift+Enter in input', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }) as any;
await (view as any).inputKeyDownHandler!(event);
expect(handleSpy).not.toHaveBeenCalled();
});
it('should handle new chat button click', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
view['newChatButton'] = document.createElement('button');
const clearSpy = jest.spyOn(view as any, 'clearConversation');
const handler = view.getNewChatButtonClickHandler?.bind(view);
if (!handler) throw new Error('Handler not available');
await handler();
expect(clearSpy).toHaveBeenCalled();
});
});
describe('event listeners', () => {
it('should setup event listeners on open', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
await view.onOpen();
expect(view['sendButtonClickHandler']).not.toBeNull();
expect(view['inputKeyDownHandler']).not.toBeNull();
});
it('should remove event listeners on close', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
await view.onOpen();
const removeSpy = jest.spyOn(view, 'removeEventListeners' as any);
await view.onClose();
expect(removeSpy).toHaveBeenCalled();
});
});
});
describe('event handlers', () => {
it('should handle send button click', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const handler = view.getSendButtonClickHandler?.bind(view);
if (!handler) throw new Error('Handler not available');
await handler();
expect(handleSpy).toHaveBeenCalledWith('test');
expect((view['inputEl'] as HTMLTextAreaElement).value).toBe('');
});
it('should handle Enter key press in input', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter' }) as any;
await (view as any).inputKeyDownHandler!(event);
expect(handleSpy).toHaveBeenCalledWith('test');
expect((view['inputEl'] as HTMLTextAreaElement).value).toBe('');
});
it('should not handle Shift+Enter in input', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test';
const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }) as any;
await (view as any).inputKeyDownHandler!(event);
expect(handleSpy).not.toHaveBeenCalled();
});
it('should handle new chat button click', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
view['newChatButton'] = document.createElement('button');
const clearSpy = jest.spyOn(view as any, 'clearConversation');
await (view as any).newChatButtonClickHandler!();
expect(clearSpy).toHaveBeenCalled();
});
});
describe('event listeners', () => {
it('should setup event listeners on open', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
await view.onOpen();
expect(view['sendButtonClickHandler']).not.toBeNull();
expect(view['inputKeyDownHandler']).not.toBeNull();
});
it('should remove event listeners on close', async () => {
view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea');
await view.onOpen();
const removeSpy = jest.spyOn(view, 'removeEventListeners' as any);
await view.onClose();
expect(removeSpy).toHaveBeenCalled();
});
});
});