2025-07-15 10:19:59 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
|
|
|
import { createIdeContextStore } from './ideContext.js';
|
|
|
|
|
|
|
|
|
|
describe('ideContext - Active File', () => {
|
|
|
|
|
let ideContext: ReturnType<typeof createIdeContextStore>;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
// Create a fresh, isolated instance for each test
|
|
|
|
|
ideContext = createIdeContextStore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return undefined initially for active file context', () => {
|
2025-07-21 20:52:02 +00:00
|
|
|
expect(ideContext.getOpenFilesContext()).toBeUndefined();
|
2025-07-15 10:19:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should set and retrieve the active file context', () => {
|
|
|
|
|
const testFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/test/file.ts',
|
|
|
|
|
selectedText: '1234',
|
2025-07-15 10:19:59 -04:00
|
|
|
};
|
|
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(testFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
const activeFile = ideContext.getOpenFilesContext();
|
2025-07-15 10:19:59 -04:00
|
|
|
expect(activeFile).toEqual(testFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update the active file context when called multiple times', () => {
|
|
|
|
|
const firstFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/first.js',
|
|
|
|
|
selectedText: '1234',
|
2025-07-15 10:19:59 -04:00
|
|
|
};
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(firstFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
|
|
|
|
const secondFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/second.py',
|
2025-07-15 10:19:59 -04:00
|
|
|
cursor: { line: 20, character: 30 },
|
|
|
|
|
};
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(secondFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
const activeFile = ideContext.getOpenFilesContext();
|
2025-07-15 10:19:59 -04:00
|
|
|
expect(activeFile).toEqual(secondFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle empty string for file path', () => {
|
|
|
|
|
const testFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '',
|
|
|
|
|
selectedText: '1234',
|
2025-07-15 10:19:59 -04:00
|
|
|
};
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(testFile);
|
|
|
|
|
expect(ideContext.getOpenFilesContext()).toEqual(testFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should notify subscribers when active file context changes', () => {
|
|
|
|
|
const subscriber1 = vi.fn();
|
|
|
|
|
const subscriber2 = vi.fn();
|
|
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.subscribeToOpenFiles(subscriber1);
|
|
|
|
|
ideContext.subscribeToOpenFiles(subscriber2);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
|
|
|
|
const testFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/subscribed.ts',
|
2025-07-15 10:19:59 -04:00
|
|
|
cursor: { line: 15, character: 25 },
|
|
|
|
|
};
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(testFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
|
|
|
|
expect(subscriber1).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(subscriber1).toHaveBeenCalledWith(testFile);
|
|
|
|
|
expect(subscriber2).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(subscriber2).toHaveBeenCalledWith(testFile);
|
|
|
|
|
|
|
|
|
|
// Test with another update
|
|
|
|
|
const newFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/new.js',
|
|
|
|
|
selectedText: '1234',
|
2025-07-15 10:19:59 -04:00
|
|
|
};
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(newFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
|
|
|
|
expect(subscriber1).toHaveBeenCalledTimes(2);
|
|
|
|
|
expect(subscriber1).toHaveBeenCalledWith(newFile);
|
|
|
|
|
expect(subscriber2).toHaveBeenCalledTimes(2);
|
|
|
|
|
expect(subscriber2).toHaveBeenCalledWith(newFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should stop notifying a subscriber after unsubscribe', () => {
|
|
|
|
|
const subscriber1 = vi.fn();
|
|
|
|
|
const subscriber2 = vi.fn();
|
|
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
const unsubscribe1 = ideContext.subscribeToOpenFiles(subscriber1);
|
|
|
|
|
ideContext.subscribeToOpenFiles(subscriber2);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext({
|
|
|
|
|
activeFile: '/path/to/file1.txt',
|
|
|
|
|
selectedText: '1234',
|
2025-07-15 10:19:59 -04:00
|
|
|
});
|
|
|
|
|
expect(subscriber1).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(subscriber2).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
|
|
unsubscribe1();
|
|
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext({
|
|
|
|
|
activeFile: '/path/to/file2.txt',
|
|
|
|
|
selectedText: '1234',
|
2025-07-15 10:19:59 -04:00
|
|
|
});
|
|
|
|
|
expect(subscriber1).toHaveBeenCalledTimes(1); // Should not be called again
|
|
|
|
|
expect(subscriber2).toHaveBeenCalledTimes(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should allow the cursor to be optional', () => {
|
|
|
|
|
const testFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/test/file.ts',
|
2025-07-15 10:19:59 -04:00
|
|
|
};
|
|
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(testFile);
|
2025-07-15 10:19:59 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
const activeFile = ideContext.getOpenFilesContext();
|
2025-07-15 10:19:59 -04:00
|
|
|
expect(activeFile).toEqual(testFile);
|
|
|
|
|
});
|
2025-07-16 17:33:56 -04:00
|
|
|
|
|
|
|
|
it('should clear the active file context', () => {
|
|
|
|
|
const testFile = {
|
2025-07-21 20:52:02 +00:00
|
|
|
activeFile: '/path/to/test/file.ts',
|
|
|
|
|
selectedText: '1234',
|
2025-07-16 17:33:56 -04:00
|
|
|
};
|
|
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.setOpenFilesContext(testFile);
|
2025-07-16 17:33:56 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
expect(ideContext.getOpenFilesContext()).toEqual(testFile);
|
2025-07-16 17:33:56 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
ideContext.clearOpenFilesContext();
|
2025-07-16 17:33:56 -04:00
|
|
|
|
2025-07-21 20:52:02 +00:00
|
|
|
expect(ideContext.getOpenFilesContext()).toBeUndefined();
|
2025-07-16 17:33:56 -04:00
|
|
|
});
|
2025-07-15 10:19:59 -04:00
|
|
|
});
|