agent-ecosystem/test/renderer/store/editorSlice.test.ts
Илия 945d5b237c
feat: project editor with drag & drop, search, Quick Open (#11)
* fix: add retry logic to sendInboxMessage for concurrent writes

On Windows, parallel writes to the same inbox file cause race conditions
where atomicWrite verification fails (another process overwrites between
write and verify). Added retry loop (8 attempts) matching the existing
pattern in addTaskComment. Bumps teamctl version to 11.

Fixes CI failure: test (windows-latest) "parallel messages to same inbox"

* fix: enhance CLI installer and session management

- Updated the postinstall script in package.json to handle rebuild failures gracefully.
- Added clearContext option in team launch requests to allow starting fresh sessions without resuming previous context.
- Improved CLI installer logging by integrating raw output chunks for better terminal rendering.
- Refactored components to utilize TerminalLogPanel for displaying installation logs, enhancing user experience during CLI installation.
- Updated various services and hooks to support the new clearContext feature and raw logging.

* fix: update MemberBadge and LaunchTeamDialog components for improved functionality

- Modified MemberBadge to display 'lead' for team leads instead of the full name.
- Refactored LaunchTeamDialog to simplify model selection logic and replace the Select component with a custom button-based interface for better user experience.
- Enhanced KanbanTaskCard to include meta actions for task management, improving the layout and functionality for manual review tasks.

* feat: auto-publish releases with stable download links

- Change releaseType from draft to release for auto-publishing
- Add upload-stable-links job to create version-agnostic asset copies
- Update README with direct download URLs per platform
- Add Requirements section to Installation
- Remove downloads/platform badges
- Add docs/RELEASE.md with versioning and release guide
- Move community docs to .github/

* improvemtns

* improvement

* fix: handle Windows spawn EINVAL on non-ASCII paths and add helper utilities

* improvements

* fix: enhance child process environment handling for Windows

- Added a helper function to build the child process environment with the correct HOME directory, addressing issues with non-ASCII usernames on Windows.
- Updated CLI installer methods to utilize the new environment setup for improved compatibility and error handling.

* refactor: replace execFile and spawn with execCli and spawnCli in CLI and TeamProvisioning services

- Updated CliInstallerService and TeamProvisioningService to use execCli and spawnCli for improved error handling and compatibility, particularly on Windows.
- Enhanced child process utility functions to better manage non-ASCII paths and provide consistent behavior across different platforms.
- Adjusted tests to mock new child process utilities and verify correct usage in service methods.

* fix

* fix windows

* feat: add download badges with direct links per platform

* refactor: move download buttons to Installation section

* refactor: move Docker files to docker/ and CHANGELOG to docs/

* refactor: move vite.standalone.config to docker/, remove .nvmrc

* refactor: merge tsconfig.test.json into tsconfig.json

* refactor: remove .editorconfig, .gitattributes, merge knip.json into package.json

* fix: adjust macOS download badge sizing

* feat: implement in-app project editor with CodeMirror integration

- Added architectural plan and iteration plan for the in-app project editor.
- Introduced new components for the editor, including CodeEditorOverlay, FileTreePanel, and EditorTabsPanel.
- Established state management using Zustand for editor state persistence.
- Implemented IPC channels for file operations and editor functionality.
- Enhanced TeamDetailView with a button to open the editor overlay.
- Conducted reuse analysis for existing components to optimize codebase integration.

* feat: enhance in-app project editor with architecture documentation and service updates

- Added detailed architecture and component hierarchy documentation for the in-app project editor.
- Introduced `ProjectFileService` to manage file operations with improved path validation.
- Updated `electron.vite.config.ts` to set `UV_THREADPOOL_SIZE` for better performance on Windows.
- Deferred non-critical startup tasks in `index.ts` to avoid thread pool contention.
- Enhanced `CliInstallerService` with timeout handling for status gathering to prevent UI hangs.
- Added tests for `CliInstallerService` to ensure proper timeout behavior.

* feat: enhance project editor with file management, Git integration, and UI improvements

- Introduced `EditorFileWatcher` for live file change detection and `GitStatusService` for displaying Git status in the file tree.
- Added context menu for file operations (create, delete) and implemented multi-tab support for the editor.
- Enhanced user experience with keyboard shortcuts, search functionality, and breadcrumb navigation.
- Updated IPC channels for file operations and integrated conflict detection during file saves.
- Improved performance with file watcher optimizations and virtualized file tree rendering.

* feat: enhance project editor with autosave, improved file management, and performance optimizations

- Implemented draft autosave functionality to prevent data loss during crashes, with recovery options for unsaved changes.
- Updated file management services to support better path validation and conflict detection.
- Enhanced performance with optimized file watcher and caching strategies for project scanning.
- Improved user experience with confirmation dialogs for unsaved changes and refined keyboard shortcuts.
- Documented testing strategies and rollback plans for iterative development.

* feat: integrate simple-git for enhanced Git status tracking and improve file watcher performance

- Replaced direct Git command usage with `simple-git` for more reliable status tracking, including support for renamed files and conflict detection.
- Updated IPC channels to reflect changes in Git status retrieval method.
- Enhanced file watcher initialization on Windows to prevent UV thread pool saturation by starting watchers sequentially.
- Improved application startup by staggering context system initialization and notification listeners to optimize performance.

* feat: optimize IPC initialization and context management for improved app performance

- Deferred IPC-heavy initialization to occur after the first paint to prevent app freezing on Windows.
- Staggered notification listener setup to avoid saturating the UV thread pool during startup.
- Updated context system initialization to be lazy, ensuring local context is always ready without upfront costs.
- Enhanced data fetching sequence to reduce simultaneous IPC calls, improving overall responsiveness.

* feat: enhance project editor with new error boundary and file handling improvements

- Introduced `EditorErrorBoundary` component to catch runtime errors in CodeMirror, providing a fallback UI to prevent crashes.
- Updated file handling logic to utilize `isbinaryfile` for more reliable binary detection, replacing manual null-byte scans.
- Enhanced `openFile` method to prevent duplicate tabs for already opened files.
- Improved documentation for new components and updated file lists to reflect recent changes.
- Optimized file watcher and path validation processes for better performance and reliability.

* feat: enhance TeamConfigReader with improved file handling and concurrency

- Introduced `mapLimit` function to manage concurrent processing of team directories, optimizing performance.
- Added `readFileHead` function to read the beginning of large configuration files efficiently.
- Implemented `extractQuotedString` to safely extract values from JSON strings in configuration headers.
- Enhanced error handling and validation for team configuration files, ensuring robust processing of team data.
- Updated logic to handle large configuration files differently, improving overall reliability and performance.

* feat: enhance team management with improved session and project path history handling

- Introduced constants for maximum session and project path history limits to optimize memory usage.
- Updated `TeamConfigReader` and `TeamProvisioningService` to limit session and project path history to defined maximums.
- Enhanced `TeamMembersMetaStore` to handle large meta files more efficiently by checking file size before processing.
- Refactored state management in `teamSlice` to include optimized lookups for team summaries by name and session ID.

* feat: enhance GlobalTaskDetailDialog and TaskDetailDialog with loading state management

- Added a loading state to the TaskDetailDialog to display a loading indicator while fetching team data.
- Updated GlobalTaskDetailDialog to pass the loading state to TaskDetailDialog.
- Modified the selectTeam function in teamSlice to accept options for skipping project auto-selection, improving team data handling.

* feat: optimize team display name resolution and enhance file change handling

- Introduced a caching mechanism for team display names to reduce redundant API calls and improve performance.
- Updated file change event handling to debounce cache invalidation, preventing unnecessary rescans during rapid file changes.
- Enhanced GlobalTaskDetailDialog and TaskDetailDialog to manage loading states and improve team data fetching logic.
- Refactored teamSlice to streamline team selection and data loading processes.

* fix: improve team selection logic and prevent duplicate fetches

- Enhanced GlobalTaskDetailDialog to handle loading states more effectively, preventing unnecessary re-fetching of team data.
- Updated selectTeam function in teamSlice to guard against duplicate in-flight fetches for the same team, improving performance and user experience.
- Refactored dependencies in useEffect to ensure proper data loading behavior.

* feat: enhance team data fetching with performance logging and timeout handling

- Added performance logging to the handleGetData function, tracking the duration of team data retrieval and logging warnings for slow responses.
- Implemented a timeout mechanism in the selectTeam function to prevent long-running fetch operations, improving user experience.
- Enhanced the getTeamData method with detailed timing metrics for each data loading step, allowing for better performance analysis and debugging.

* feat: implement timeout handling and logging for team data fetching

- Added a timeout mechanism to the getBranch calls in TeamDataService to prevent hangs on Windows setups, improving reliability during team data retrieval.
- Introduced performance logging in TeamDetailView and teamSlice to track the start and completion of team selection processes, enhancing debugging capabilities.
- Updated error handling to provide clearer warnings during team provisioning and selection, improving user experience.

* feat: enhance MarkdownViewer and task dialogs with loading state management and performance logging

- Introduced character limits for Markdown content in MarkdownViewer to prevent UI freezes with large content.
- Added state management for raw content display in MarkdownViewer, allowing users to expand and view large markdown files.
- Implemented performance logging in GlobalTaskDetailDialog and TaskDetailDialog to track loading states and improve debugging.
- Updated loading state handling in TaskDetailDialog to ensure accurate representation of loading conditions.

* feat: enhance TaskCommentsSection with improved comment rendering and visibility management

- Added state management for visible comments, allowing users to see a limited number of comments for better performance.
- Implemented logic to cap the number of rendered comments, preventing UI freezes with large comment lists.
- Introduced sorting for comments based on creation date to display the most recent comments first.
- Updated the UI to inform users when only a subset of comments is being displayed, enhancing user experience.

* feat: enhance MarkdownViewer with improved character limits and syntax highlighting management

- Updated character limits for Markdown content to prevent UI freezes with large inputs.
- Introduced logic to disable syntax highlighting for medium/large content and show raw previews for very large content.
- Enhanced responsiveness of the MarkdownViewer by managing rendering based on content size.

* refactor: remove console warnings from team-related components for cleaner logging

- Eliminated console warnings in TeamDetailView, GlobalTaskDetailDialog, and TaskDetailDialog to streamline logging and reduce clutter during team data operations.
- Updated the selectTeam function in teamSlice to remove unnecessary logging, enhancing performance and readability.

* feat: add project editor with drag & drop file management

- Backend: ProjectFileService with file CRUD, search, git status, file watcher
- IPC: 12 editor channels with security validation and path containment
- Store: editorSlice with multi-tab management, draft persistence, conflict detection
- UI: CodeMirror 6 editor, file tree with DnD, search-in-files, context menus
- Move: fs.rename with EXDEV fallback, full path remapping across all caches
- Tests: comprehensive coverage for services, IPC handlers, store, and utilities

* fix: rename closeTab/setActiveTab to closeEditorTab/setActiveEditorTab

Resolve naming collision between editorSlice and tabSlice.
Both slices defined closeTab and setActiveTab, and since editorSlice
was spread last in the store composition, it silently overwrote the
tabSlice methods, breaking tab management.

* fix: editor improvements — isDir bug, scroll-to-line, Quick Open, a11y

- Fix isDir heuristic: use backend-provided isDirectory instead of
  filename-based guessing (breaks for Makefile, .github, etc.)
- Add scroll-to-line on search result click via editorPendingGoToLine
- Add Cmd+Shift+W shortcut for toggling line wrap
- Rewrite Quick Open to fetch all project files from backend API
  instead of flattening the loaded tree (limited to expanded dirs)
- Fix fd leak in atomicWrite: close file handle in finally block
- Add a11y: role=dialog/alert, aria-modal, aria-label on modals
- Add type=button on error state buttons

---------

Co-authored-by: Алексей <aleksei@example.com>
2026-03-01 07:56:59 +02:00

852 lines
31 KiB
TypeScript

/**
* Tests for editorSlice — openEditor, closeEditor, expandDirectory, collapseDirectory.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createTestStore } from './storeTestUtils';
import type { TestStore } from './storeTestUtils';
import type { FileTreeEntry, ReadDirResult } from '../../../src/shared/types/editor';
// =============================================================================
// Mock API
// =============================================================================
const mockEditorAPI = {
open: vi.fn(),
close: vi.fn(),
readDir: vi.fn(),
readFile: vi.fn(),
writeFile: vi.fn(),
createFile: vi.fn(),
createDir: vi.fn(),
deleteFile: vi.fn(),
moveFile: vi.fn(),
};
vi.mock('@renderer/api', () => ({
api: {
editor: {
open: (...args: unknown[]) => mockEditorAPI.open(...args),
close: (...args: unknown[]) => mockEditorAPI.close(...args),
readDir: (...args: unknown[]) => mockEditorAPI.readDir(...args),
readFile: (...args: unknown[]) => mockEditorAPI.readFile(...args),
writeFile: (...args: unknown[]) => mockEditorAPI.writeFile(...args),
createFile: (...args: unknown[]) => mockEditorAPI.createFile(...args),
createDir: (...args: unknown[]) => mockEditorAPI.createDir(...args),
deleteFile: (...args: unknown[]) => mockEditorAPI.deleteFile(...args),
moveFile: (...args: unknown[]) => mockEditorAPI.moveFile(...args),
},
// Provide stubs for other API domains if needed
getProjects: vi.fn(),
getSessions: vi.fn(),
},
}));
const mockBridge = {
getContent: vi.fn(),
getAllModifiedContent: vi.fn(),
destroy: vi.fn(),
deleteState: vi.fn(),
remapState: vi.fn(),
};
vi.mock('@renderer/utils/editorBridge', () => ({
editorBridge: {
getContent: (...args: unknown[]) => mockBridge.getContent(...args),
getAllModifiedContent: (...args: unknown[]) => mockBridge.getAllModifiedContent(...args),
destroy: (...args: unknown[]) => mockBridge.destroy(...args),
deleteState: (...args: unknown[]) => mockBridge.deleteState(...args),
remapState: (...args: unknown[]) => mockBridge.remapState(...args),
register: vi.fn(),
unregister: vi.fn(),
isRegistered: false,
updateView: vi.fn(),
getView: vi.fn(),
},
}));
vi.mock('@renderer/utils/codemirrorLanguages', () => ({
getLanguageFromFileName: (name: string) => {
const ext = name.split('.').pop()?.toLowerCase();
const map: Record<string, string> = {
ts: 'TypeScript',
tsx: 'TSX',
js: 'JavaScript',
json: 'JSON',
md: 'Markdown',
py: 'Python',
};
return map[ext ?? ''] ?? 'Plain Text';
},
getSyncLanguageExtension: vi.fn(),
getAsyncLanguageDesc: vi.fn(),
}));
vi.mock('@shared/utils/logger', () => ({
createLogger: () => ({
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
}),
}));
// =============================================================================
// Helpers
// =============================================================================
const PROJECT_PATH = '/Users/test/my-project';
function makeEntry(name: string, type: 'file' | 'directory', absPath?: string): FileTreeEntry {
return {
name,
path: absPath ?? `${PROJECT_PATH}/${name}`,
type,
};
}
function makeDirResult(entries: FileTreeEntry[], truncated = false): ReadDirResult {
return { entries, truncated };
}
// =============================================================================
// Tests
// =============================================================================
describe('editorSlice', () => {
let store: TestStore;
beforeEach(() => {
vi.resetAllMocks();
store = createTestStore();
});
describe('initial state', () => {
it('has null/empty defaults', () => {
const state = store.getState();
expect(state.editorProjectPath).toBeNull();
expect(state.editorFileTree).toBeNull();
expect(state.editorFileTreeLoading).toBe(false);
expect(state.editorFileTreeError).toBeNull();
expect(state.editorExpandedDirs).toEqual({});
});
});
describe('openEditor', () => {
it('sets loading state, calls API, and stores file tree', async () => {
const entries = [makeEntry('src', 'directory'), makeEntry('README.md', 'file')];
mockEditorAPI.open.mockResolvedValue(undefined);
mockEditorAPI.readDir.mockResolvedValue(makeDirResult(entries));
await store.getState().openEditor(PROJECT_PATH);
const state = store.getState();
expect(state.editorProjectPath).toBe(PROJECT_PATH);
expect(state.editorFileTree).toEqual(entries);
expect(state.editorFileTreeLoading).toBe(false);
expect(state.editorFileTreeError).toBeNull();
expect(mockEditorAPI.open).toHaveBeenCalledWith(PROJECT_PATH);
expect(mockEditorAPI.readDir).toHaveBeenCalledWith(PROJECT_PATH);
});
it('sets error state on API failure', async () => {
mockEditorAPI.open.mockRejectedValue(new Error('Permission denied'));
await store.getState().openEditor(PROJECT_PATH);
const state = store.getState();
expect(state.editorFileTreeLoading).toBe(false);
expect(state.editorFileTreeError).toBe('Permission denied');
expect(state.editorFileTree).toBeNull();
});
it('resets expanded dirs on new open', async () => {
// Set some expanded dirs manually
store.setState({ editorExpandedDirs: { '/old/path': true } });
mockEditorAPI.open.mockResolvedValue(undefined);
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
await store.getState().openEditor(PROJECT_PATH);
expect(store.getState().editorExpandedDirs).toEqual({});
});
});
describe('closeEditor', () => {
it('resets all editor state', async () => {
// Setup non-default state
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [makeEntry('file.ts', 'file')],
editorFileTreeLoading: true,
editorFileTreeError: 'some error',
editorExpandedDirs: { '/path': true },
});
mockEditorAPI.close.mockResolvedValue(undefined);
store.getState().closeEditor();
const state = store.getState();
expect(state.editorProjectPath).toBeNull();
expect(state.editorFileTree).toBeNull();
expect(state.editorFileTreeLoading).toBe(false);
expect(state.editorFileTreeError).toBeNull();
expect(state.editorExpandedDirs).toEqual({});
});
it('still resets local state even if IPC close fails', async () => {
store.setState({ editorProjectPath: PROJECT_PATH });
mockEditorAPI.close.mockRejectedValue(new Error('IPC error'));
store.getState().closeEditor();
// Local state reset immediately (fire-and-forget IPC)
expect(store.getState().editorProjectPath).toBeNull();
});
});
describe('expandDirectory', () => {
it('marks directory expanded immediately, then merges children', async () => {
const srcDir = makeEntry('src', 'directory');
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [srcDir, makeEntry('README.md', 'file')],
});
const children = [makeEntry('index.ts', 'file', `${PROJECT_PATH}/src/index.ts`)];
mockEditorAPI.readDir.mockResolvedValue(makeDirResult(children));
const expandPromise = store.getState().expandDirectory(srcDir.path);
// Immediately expanded (optimistic UI)
expect(store.getState().editorExpandedDirs[srcDir.path]).toBe(true);
await expandPromise;
// Children merged into tree
const tree = store.getState().editorFileTree!;
const srcNode = tree.find((e) => e.name === 'src');
expect(srcNode?.children).toEqual(children);
});
it('reverts expansion on error', async () => {
const srcDir = makeEntry('src', 'directory');
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [srcDir],
editorExpandedDirs: {},
});
mockEditorAPI.readDir.mockRejectedValue(new Error('Network error'));
await store.getState().expandDirectory(srcDir.path);
// Expansion reverted
expect(store.getState().editorExpandedDirs[srcDir.path]).toBeUndefined();
});
});
describe('collapseDirectory', () => {
it('removes directory from expandedDirs', () => {
const dirPath = PROJECT_PATH + '/src';
store.setState({ editorExpandedDirs: { [dirPath]: true, '/other': true } });
store.getState().collapseDirectory(dirPath);
expect(store.getState().editorExpandedDirs).toEqual({ '/other': true });
});
it('no-op when directory not expanded', () => {
store.setState({ editorExpandedDirs: { '/other': true } });
store.getState().collapseDirectory('/not-expanded');
expect(store.getState().editorExpandedDirs).toEqual({ '/other': true });
});
});
// ═══════════════════════════════════════════════════════
// Group 2: Tab management
// ═══════════════════════════════════════════════════════
describe('openFile', () => {
it('creates a tab and activates it', () => {
store.getState().openFile('/project/src/index.ts');
const state = store.getState();
expect(state.editorOpenTabs).toHaveLength(1);
expect(state.editorOpenTabs[0].filePath).toBe('/project/src/index.ts');
expect(state.editorOpenTabs[0].fileName).toBe('index.ts');
expect(state.editorOpenTabs[0].language).toBe('TypeScript');
expect(state.editorActiveTabId).toBe('/project/src/index.ts');
});
it('activates existing tab instead of creating duplicate', () => {
store.getState().openFile('/project/src/index.ts');
store.getState().openFile('/project/src/app.tsx');
store.getState().openFile('/project/src/index.ts');
const state = store.getState();
expect(state.editorOpenTabs).toHaveLength(2);
expect(state.editorActiveTabId).toBe('/project/src/index.ts');
});
it('detects language from file extension', () => {
store.getState().openFile('/project/data.json');
expect(store.getState().editorOpenTabs[0].language).toBe('JSON');
});
it('uses "Plain Text" for unknown extensions', () => {
store.getState().openFile('/project/Dockerfile');
expect(store.getState().editorOpenTabs[0].language).toBe('Plain Text');
});
});
describe('closeEditorTab', () => {
it('removes tab and activates adjacent', () => {
store.getState().openFile('/project/a.ts');
store.getState().openFile('/project/b.ts');
store.getState().openFile('/project/c.ts');
// Active is c.ts, close it
store.getState().closeEditorTab('/project/c.ts');
const state = store.getState();
expect(state.editorOpenTabs).toHaveLength(2);
expect(state.editorActiveTabId).toBe('/project/b.ts');
});
it('activates first remaining tab when first is closed', () => {
store.getState().openFile('/project/a.ts');
store.getState().openFile('/project/b.ts');
store.getState().setActiveEditorTab('/project/a.ts');
store.getState().closeEditorTab('/project/a.ts');
expect(store.getState().editorActiveTabId).toBe('/project/b.ts');
});
it('sets null when last tab is closed', () => {
store.getState().openFile('/project/a.ts');
store.getState().closeEditorTab('/project/a.ts');
expect(store.getState().editorActiveTabId).toBeNull();
expect(store.getState().editorOpenTabs).toHaveLength(0);
});
it('cleans up dirty and error state for closed tab', () => {
store.getState().openFile('/project/a.ts');
store.setState({
editorModifiedFiles: { '/project/a.ts': true },
editorSaveError: { '/project/a.ts': 'Save failed' },
});
store.getState().closeEditorTab('/project/a.ts');
expect(store.getState().editorModifiedFiles).toEqual({});
expect(store.getState().editorSaveError).toEqual({});
});
it('does not change activeTabId when closing non-active tab', () => {
store.getState().openFile('/project/a.ts');
store.getState().openFile('/project/b.ts');
// b.ts is active
store.getState().closeEditorTab('/project/a.ts');
expect(store.getState().editorActiveTabId).toBe('/project/b.ts');
expect(store.getState().editorOpenTabs).toHaveLength(1);
});
});
describe('setActiveEditorTab', () => {
it('changes the active tab', () => {
store.getState().openFile('/project/a.ts');
store.getState().openFile('/project/b.ts');
// b.ts is active
store.getState().setActiveEditorTab('/project/a.ts');
expect(store.getState().editorActiveTabId).toBe('/project/a.ts');
});
});
// ═══════════════════════════════════════════════════════
// Group 3: Dirty/Save
// ═══════════════════════════════════════════════════════
describe('markFileModified', () => {
it('sets dirty flag', () => {
store.getState().markFileModified('/project/a.ts');
expect(store.getState().editorModifiedFiles['/project/a.ts']).toBe(true);
});
it('is idempotent', () => {
store.getState().markFileModified('/project/a.ts');
const first = store.getState().editorModifiedFiles;
store.getState().markFileModified('/project/a.ts');
const second = store.getState().editorModifiedFiles;
// Same reference (no unnecessary update)
expect(first).toBe(second);
});
});
describe('markFileSaved', () => {
it('removes dirty flag', () => {
store.setState({ editorModifiedFiles: { '/project/a.ts': true, '/project/b.ts': true } });
store.getState().markFileSaved('/project/a.ts');
expect(store.getState().editorModifiedFiles).toEqual({ '/project/b.ts': true });
});
});
describe('hasUnsavedChanges', () => {
it('returns false when no modified files', () => {
expect(store.getState().hasUnsavedChanges()).toBe(false);
});
it('returns true when modified files exist', () => {
store.setState({ editorModifiedFiles: { '/project/a.ts': true } });
expect(store.getState().hasUnsavedChanges()).toBe(true);
});
});
describe('saveFile', () => {
it('saves file via API and clears dirty flag', async () => {
const filePath = '/project/src/index.ts';
mockBridge.getContent.mockReturnValue('new content');
mockEditorAPI.writeFile.mockResolvedValue({ mtimeMs: Date.now(), size: 11 });
store.setState({ editorModifiedFiles: { [filePath]: true } });
await store.getState().saveFile(filePath);
expect(mockBridge.getContent).toHaveBeenCalledWith(filePath);
expect(mockEditorAPI.writeFile).toHaveBeenCalledWith(filePath, 'new content', undefined);
expect(store.getState().editorModifiedFiles[filePath]).toBeUndefined();
expect(store.getState().editorSaving[filePath]).toBeUndefined();
});
it('sets saving flag during save', async () => {
const filePath = '/project/src/index.ts';
let savingDuringCall = false;
mockBridge.getContent.mockReturnValue('content');
mockEditorAPI.writeFile.mockImplementation(async () => {
savingDuringCall = !!store.getState().editorSaving[filePath];
return { mtimeMs: Date.now(), size: 7 };
});
await store.getState().saveFile(filePath);
expect(savingDuringCall).toBe(true);
expect(store.getState().editorSaving[filePath]).toBeUndefined();
});
it('does nothing when bridge has no content', async () => {
mockBridge.getContent.mockReturnValue(null);
await store.getState().saveFile('/project/src/index.ts');
expect(mockEditorAPI.writeFile).not.toHaveBeenCalled();
});
it('sets error on save failure', async () => {
const filePath = '/project/src/index.ts';
mockBridge.getContent.mockReturnValue('content');
mockEditorAPI.writeFile.mockRejectedValue(new Error('Permission denied'));
store.setState({ editorModifiedFiles: { [filePath]: true } });
await store.getState().saveFile(filePath);
expect(store.getState().editorSaveError[filePath]).toBe('Permission denied');
// Dirty flag preserved on error
expect(store.getState().editorModifiedFiles[filePath]).toBe(true);
expect(store.getState().editorSaving[filePath]).toBeUndefined();
});
});
describe('saveAllFiles', () => {
it('saves all modified files', async () => {
const files = new Map([
['/project/a.ts', 'content a'],
['/project/b.ts', 'content b'],
]);
mockBridge.getAllModifiedContent.mockReturnValue(files);
mockEditorAPI.writeFile.mockResolvedValue({ mtimeMs: Date.now(), size: 10 });
store.setState({
editorModifiedFiles: { '/project/a.ts': true, '/project/b.ts': true },
});
await store.getState().saveAllFiles();
expect(mockEditorAPI.writeFile).toHaveBeenCalledTimes(2);
expect(store.getState().editorModifiedFiles).toEqual({});
});
it('handles partial failures', async () => {
const files = new Map([
['/project/a.ts', 'content a'],
['/project/b.ts', 'content b'],
]);
mockBridge.getAllModifiedContent.mockReturnValue(files);
mockEditorAPI.writeFile
.mockResolvedValueOnce({ mtimeMs: Date.now(), size: 10 })
.mockRejectedValueOnce(new Error('Disk full'));
store.setState({
editorModifiedFiles: { '/project/a.ts': true, '/project/b.ts': true },
});
await store.getState().saveAllFiles();
// a.ts saved, b.ts still dirty
expect(store.getState().editorModifiedFiles['/project/a.ts']).toBeUndefined();
expect(store.getState().editorModifiedFiles['/project/b.ts']).toBe(true);
expect(store.getState().editorSaveError['/project/b.ts']).toBe('Disk full');
});
});
describe('discardChanges', () => {
it('clears dirty flag and error for the file', () => {
store.setState({
editorModifiedFiles: { '/project/a.ts': true, '/project/b.ts': true },
editorSaveError: { '/project/a.ts': 'Error' },
});
store.getState().discardChanges('/project/a.ts');
expect(store.getState().editorModifiedFiles).toEqual({ '/project/b.ts': true });
expect(store.getState().editorSaveError).toEqual({});
});
});
describe('closeEditor resets all state including Group 2+3', () => {
it('resets tabs, dirty, saving, errors', () => {
store.setState({
editorProjectPath: PROJECT_PATH,
editorOpenTabs: [
{ id: '/a.ts', filePath: '/a.ts', fileName: 'a.ts', language: 'TypeScript' },
],
editorActiveTabId: '/a.ts',
editorModifiedFiles: { '/a.ts': true },
editorSaving: { '/a.ts': true },
editorSaveError: { '/a.ts': 'Error' },
});
mockEditorAPI.close.mockResolvedValue(undefined);
store.getState().closeEditor();
const state = store.getState();
expect(state.editorOpenTabs).toEqual([]);
expect(state.editorActiveTabId).toBeNull();
expect(state.editorModifiedFiles).toEqual({});
expect(state.editorSaving).toEqual({});
expect(state.editorSaveError).toEqual({});
expect(mockBridge.destroy).toHaveBeenCalled();
});
});
// ═══════════════════════════════════════════════════════
// Tab disambiguation
// ═══════════════════════════════════════════════════════
describe('openFile with disambiguation', () => {
it('adds disambiguation labels when 2 files share the same name', () => {
store.getState().openFile('/project/src/main/index.ts');
store.getState().openFile('/project/src/renderer/index.ts');
const tabs = store.getState().editorOpenTabs;
expect(tabs).toHaveLength(2);
expect(tabs[0].disambiguatedLabel).toBe('(main)');
expect(tabs[1].disambiguatedLabel).toBe('(renderer)');
});
it('no labels when names are unique', () => {
store.getState().openFile('/project/src/app.ts');
store.getState().openFile('/project/src/index.ts');
const tabs = store.getState().editorOpenTabs;
expect(tabs[0].disambiguatedLabel).toBeUndefined();
expect(tabs[1].disambiguatedLabel).toBeUndefined();
});
});
describe('closeEditorTab clears disambiguation when names become unique', () => {
it('removes label after closing duplicate', () => {
store.getState().openFile('/project/src/main/index.ts');
store.getState().openFile('/project/src/renderer/index.ts');
// Both have labels
expect(store.getState().editorOpenTabs[0].disambiguatedLabel).toBe('(main)');
// Close one
store.getState().closeEditorTab('/project/src/main/index.ts');
// Remaining should lose its label
const tabs = store.getState().editorOpenTabs;
expect(tabs).toHaveLength(1);
expect(tabs[0].disambiguatedLabel).toBeUndefined();
});
});
describe('closeEditorTab calls editorBridge.deleteState', () => {
it('clears cached state for the closed tab', () => {
store.getState().openFile('/project/a.ts');
store.getState().closeEditorTab('/project/a.ts');
expect(mockBridge.deleteState).toHaveBeenCalledWith('/project/a.ts');
});
});
// ═══════════════════════════════════════════════════════
// Group 4: File operations
// ═══════════════════════════════════════════════════════
describe('createFileInTree', () => {
it('creates file, refreshes tree, and returns path', async () => {
const createdPath = '/project/src/new-file.ts';
mockEditorAPI.createFile.mockResolvedValue({ filePath: createdPath, mtimeMs: 123 });
mockEditorAPI.readDir.mockResolvedValue(
makeDirResult([makeEntry('new-file.ts', 'file', createdPath)])
);
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [makeEntry('src', 'directory')],
});
const result = await store.getState().createFileInTree('/project/src', 'new-file.ts');
expect(result).toBe(createdPath);
expect(mockEditorAPI.createFile).toHaveBeenCalledWith('/project/src', 'new-file.ts');
expect(store.getState().editorCreating).toBe(false);
expect(store.getState().editorCreateError).toBeNull();
});
it('sets error on failure', async () => {
mockEditorAPI.createFile.mockRejectedValue(new Error('File already exists'));
const result = await store.getState().createFileInTree('/project/src', 'existing.ts');
expect(result).toBeNull();
expect(store.getState().editorCreating).toBe(false);
expect(store.getState().editorCreateError).toBe('File already exists');
});
});
describe('createDirInTree', () => {
it('creates directory, refreshes tree, and returns path', async () => {
const createdPath = '/project/src/new-dir';
mockEditorAPI.createDir.mockResolvedValue({ dirPath: createdPath });
mockEditorAPI.readDir.mockResolvedValue(
makeDirResult([makeEntry('new-dir', 'directory', createdPath)])
);
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [makeEntry('src', 'directory')],
});
const result = await store.getState().createDirInTree('/project/src', 'new-dir');
expect(result).toBe(createdPath);
expect(mockEditorAPI.createDir).toHaveBeenCalledWith('/project/src', 'new-dir');
});
});
describe('deleteFileFromTree', () => {
it('deletes file and closes its tab if open', async () => {
mockEditorAPI.deleteFile.mockResolvedValue({ deletedPath: '/project/src/old.ts' });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.getState().openFile('/project/src/old.ts');
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [makeEntry('src', 'directory')],
});
const result = await store.getState().deleteFileFromTree('/project/src/old.ts');
expect(result).toBe(true);
expect(mockEditorAPI.deleteFile).toHaveBeenCalledWith('/project/src/old.ts');
// Tab should be closed
expect(store.getState().editorOpenTabs).toHaveLength(0);
});
it('returns false on failure', async () => {
mockEditorAPI.deleteFile.mockRejectedValue(new Error('Permission denied'));
const result = await store.getState().deleteFileFromTree('/project/src/file.ts');
expect(result).toBe(false);
});
it('closes tabs for files inside deleted directory', async () => {
mockEditorAPI.deleteFile.mockResolvedValue({ deletedPath: '/project/src' });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.getState().openFile('/project/src/a.ts');
store.getState().openFile('/project/src/b.ts');
store.getState().openFile('/project/other.ts');
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [makeEntry('src', 'directory'), makeEntry('other.ts', 'file')],
});
await store.getState().deleteFileFromTree('/project/src');
// Only other.ts should remain
expect(store.getState().editorOpenTabs).toHaveLength(1);
expect(store.getState().editorOpenTabs[0].filePath).toBe('/project/other.ts');
});
});
// ═══════════════════════════════════════════════════════
// moveFileInTree
// ═══════════════════════════════════════════════════════
describe('moveFileInTree', () => {
const SRC_DIR = PROJECT_PATH + '/src';
const LIB_DIR = PROJECT_PATH + '/lib';
it('moves file, updates tabs, and returns true', async () => {
const oldPath = SRC_DIR + '/utils.ts';
const newPath = LIB_DIR + '/utils.ts';
mockEditorAPI.moveFile.mockResolvedValue({ newPath, isDirectory: false });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.getState().openFile(oldPath);
store.setState({
editorProjectPath: PROJECT_PATH,
editorFileTree: [makeEntry('src', 'directory'), makeEntry('lib', 'directory')],
});
const result = await store.getState().moveFileInTree(oldPath, LIB_DIR);
expect(result).toBe(true);
expect(mockEditorAPI.moveFile).toHaveBeenCalledWith(oldPath, LIB_DIR);
// Tab should be remapped to new path
const tabs = store.getState().editorOpenTabs;
expect(tabs).toHaveLength(1);
expect(tabs[0].filePath).toBe(newPath);
expect(tabs[0].id).toBe(newPath);
expect(tabs[0].fileName).toBe('utils.ts');
});
it('remaps activeTabId when moved file is active', async () => {
const oldPath = SRC_DIR + '/index.ts';
const newPath = LIB_DIR + '/index.ts';
mockEditorAPI.moveFile.mockResolvedValue({ newPath, isDirectory: false });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.getState().openFile(oldPath);
store.setState({ editorProjectPath: PROJECT_PATH });
await store.getState().moveFileInTree(oldPath, LIB_DIR);
expect(store.getState().editorActiveTabId).toBe(newPath);
});
it('remaps modifiedFiles and fileMtimes', async () => {
const oldPath = SRC_DIR + '/dirty.ts';
const newPath = LIB_DIR + '/dirty.ts';
mockEditorAPI.moveFile.mockResolvedValue({ newPath, isDirectory: false });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.setState({
editorProjectPath: PROJECT_PATH,
editorModifiedFiles: { [oldPath]: true },
editorFileMtimes: { [oldPath]: 123456 },
});
await store.getState().moveFileInTree(oldPath, LIB_DIR);
expect(store.getState().editorModifiedFiles[newPath]).toBe(true);
expect(store.getState().editorModifiedFiles[oldPath]).toBeUndefined();
expect(store.getState().editorFileMtimes[newPath]).toBe(123456);
expect(store.getState().editorFileMtimes[oldPath]).toBeUndefined();
});
it('handles directory move (prefix remapping of nested tabs)', async () => {
const oldDir = SRC_DIR + '/components';
const newDir = LIB_DIR + '/components';
const oldFilePath = oldDir + '/Button.tsx';
const newFilePath = newDir + '/Button.tsx';
mockEditorAPI.moveFile.mockResolvedValue({ newPath: newDir, isDirectory: true });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.getState().openFile(oldFilePath);
store.setState({
editorProjectPath: PROJECT_PATH,
editorModifiedFiles: { [oldFilePath]: true },
editorExpandedDirs: { [oldDir]: true },
});
await store.getState().moveFileInTree(oldDir, LIB_DIR);
// Tab should be remapped
const tabs = store.getState().editorOpenTabs;
expect(tabs).toHaveLength(1);
expect(tabs[0].filePath).toBe(newFilePath);
// Modified files remapped
expect(store.getState().editorModifiedFiles[newFilePath]).toBe(true);
expect(store.getState().editorModifiedFiles[oldFilePath]).toBeUndefined();
// Expanded dirs remapped
expect(store.getState().editorExpandedDirs[newDir]).toBe(true);
expect(store.getState().editorExpandedDirs[oldDir]).toBeUndefined();
});
it('blocks during save', async () => {
const filePath = SRC_DIR + '/saving.ts';
store.setState({
editorProjectPath: PROJECT_PATH,
editorSaving: { [filePath]: true },
});
const result = await store.getState().moveFileInTree(filePath, LIB_DIR);
expect(result).toBe(false);
expect(mockEditorAPI.moveFile).not.toHaveBeenCalled();
});
it('returns false on API error', async () => {
const filePath = SRC_DIR + '/index.ts';
mockEditorAPI.moveFile.mockRejectedValue(new Error('Permission denied'));
store.setState({ editorProjectPath: PROJECT_PATH });
const result = await store.getState().moveFileInTree(filePath, LIB_DIR);
expect(result).toBe(false);
});
it('calls editorBridge.remapState for affected files', async () => {
const oldPath = SRC_DIR + '/bridge.ts';
const newPath = LIB_DIR + '/bridge.ts';
mockEditorAPI.moveFile.mockResolvedValue({ newPath, isDirectory: false });
mockEditorAPI.readDir.mockResolvedValue(makeDirResult([]));
store.getState().openFile(oldPath);
store.setState({ editorProjectPath: PROJECT_PATH });
await store.getState().moveFileInTree(oldPath, LIB_DIR);
expect(mockBridge.remapState).toHaveBeenCalledWith(oldPath, newPath);
});
});
});