feat: phase 1 of python scripting
This commit is contained in:
@@ -235,6 +235,24 @@ describe('Panel', () => {
|
||||
expect(screen.getByRole('tab', { name: 'Output' })).toHaveAttribute('aria-selected', 'true');
|
||||
});
|
||||
|
||||
it('renders output entries when output tab is active', () => {
|
||||
useAppStore.setState({
|
||||
panelActiveTab: 'output',
|
||||
panelOutputEntries: [
|
||||
{
|
||||
id: 'output-1',
|
||||
message: 'hello from script',
|
||||
createdAt: '2026-02-22T00:00:00.000Z',
|
||||
kind: 'stdout',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
render(<Panel />);
|
||||
|
||||
expect(screen.getByText('hello from script')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders grouped tasks as expandable parent rows with child task names in tasks tab', async () => {
|
||||
useAppStore.setState({
|
||||
tasks: [
|
||||
|
||||
23
tests/renderer/components/ScriptsView.styles.test.ts
Normal file
23
tests/renderer/components/ScriptsView.styles.test.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
|
||||
describe('ScriptsView styles', () => {
|
||||
const cssPath = path.resolve(
|
||||
__dirname,
|
||||
'../../../src/renderer/components/ScriptsView/ScriptsView.css'
|
||||
);
|
||||
|
||||
it('uses full editor area layout for the scripts container', () => {
|
||||
const css = fs.readFileSync(cssPath, 'utf8');
|
||||
|
||||
expect(css).toMatch(/\.scripts-view\s*\{[^}]*flex:\s*1;[^}]*min-height:\s*0;[^}]*\}/s);
|
||||
});
|
||||
|
||||
it('keeps editor and textarea stretched to fill available space', () => {
|
||||
const css = fs.readFileSync(cssPath, 'utf8');
|
||||
|
||||
expect(css).toMatch(/\.scripts-editor\s*\{[^}]*flex:\s*1;[^}]*min-height:\s*0;[^}]*\}/s);
|
||||
expect(css).toMatch(/\.scripts-textarea\s*\{[^}]*flex:\s*1;[^}]*min-height:\s*0;[^}]*\}/s);
|
||||
});
|
||||
});
|
||||
80
tests/renderer/components/ScriptsView.test.tsx
Normal file
80
tests/renderer/components/ScriptsView.test.tsx
Normal file
@@ -0,0 +1,80 @@
|
||||
import React from 'react';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { ScriptsView } from '../../../src/renderer/components/ScriptsView/ScriptsView';
|
||||
import { useAppStore } from '../../../src/renderer/store';
|
||||
|
||||
const executeMock = vi.fn();
|
||||
|
||||
vi.mock('../../../src/renderer/python/runtimeManagerInstance', () => ({
|
||||
getPythonRuntimeManager: () => ({
|
||||
execute: executeMock,
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('ScriptsView', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
executeMock.mockResolvedValue({ result: '2', stdout: 'hello\n' });
|
||||
|
||||
(window as any).electronAPI = {
|
||||
...(window as any).electronAPI,
|
||||
scripts: {
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
get: vi.fn().mockResolvedValue({
|
||||
id: 'script-1',
|
||||
projectId: 'default',
|
||||
slug: 'hello-script',
|
||||
title: 'Hello Script',
|
||||
kind: 'utility',
|
||||
entrypoint: 'render',
|
||||
enabled: true,
|
||||
version: 1,
|
||||
filePath: '/tmp/hello-script.py',
|
||||
content: 'print("hello")',
|
||||
createdAt: '2026-02-22T00:00:00.000Z',
|
||||
updatedAt: '2026-02-22T00:00:00.000Z',
|
||||
}),
|
||||
getAll: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
useAppStore.setState({
|
||||
panelVisible: false,
|
||||
panelActiveTab: 'tasks',
|
||||
panelOutputEntries: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('loads scripts and allows editing content', async () => {
|
||||
render(<ScriptsView scriptId="script-1" />);
|
||||
|
||||
const textarea = screen.getByLabelText('Script Content') as HTMLTextAreaElement;
|
||||
await vi.waitFor(() => {
|
||||
expect(textarea.value).toContain('print("hello")');
|
||||
});
|
||||
|
||||
fireEvent.change(textarea, { target: { value: 'print("updated")' } });
|
||||
expect(textarea.value).toContain('updated');
|
||||
});
|
||||
|
||||
it('runs selected script and writes output into panel output log', async () => {
|
||||
render(<ScriptsView scriptId="script-1" />);
|
||||
|
||||
await screen.findByLabelText('Script Content');
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Run Script' }));
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(executeMock).toHaveBeenCalledWith('print("hello")');
|
||||
});
|
||||
|
||||
const state = useAppStore.getState();
|
||||
expect(state.panelVisible).toBe(true);
|
||||
expect(state.panelActiveTab).toBe('output');
|
||||
expect(state.panelOutputEntries.length).toBeGreaterThan(0);
|
||||
expect(state.panelOutputEntries[state.panelOutputEntries.length - 1].message).toContain('hello');
|
||||
});
|
||||
});
|
||||
147
tests/renderer/components/SidebarScripts.test.tsx
Normal file
147
tests/renderer/components/SidebarScripts.test.tsx
Normal file
@@ -0,0 +1,147 @@
|
||||
import React from 'react';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { Sidebar } from '../../../src/renderer/components/Sidebar/Sidebar';
|
||||
import { useAppStore } from '../../../src/renderer/store';
|
||||
|
||||
describe('Sidebar scripts list behavior', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
(window as any).electronAPI = {
|
||||
...(window as any).electronAPI,
|
||||
scripts: {
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
get: vi.fn(),
|
||||
getAll: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: 'script-1',
|
||||
projectId: 'default',
|
||||
slug: 'hello-script',
|
||||
title: 'Hello Script',
|
||||
kind: 'utility',
|
||||
entrypoint: 'render',
|
||||
enabled: true,
|
||||
version: 1,
|
||||
filePath: '/tmp/hello-script.py',
|
||||
content: 'print("hello")',
|
||||
createdAt: '2026-02-22T00:00:00.000Z',
|
||||
updatedAt: '2026-02-22T00:00:00.000Z',
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
|
||||
useAppStore.setState({
|
||||
activeView: 'scripts',
|
||||
sidebarVisible: true,
|
||||
tabs: [],
|
||||
activeTabId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('opens a transient script tab on single click', async () => {
|
||||
render(<Sidebar />);
|
||||
|
||||
const scriptRow = await screen.findByRole('button', { name: 'Hello Script' });
|
||||
fireEvent.click(scriptRow);
|
||||
|
||||
expect(useAppStore.getState().tabs).toEqual([
|
||||
{
|
||||
type: 'scripts',
|
||||
id: 'script-1',
|
||||
isTransient: true,
|
||||
},
|
||||
]);
|
||||
expect(useAppStore.getState().activeTabId).toBe('script-1');
|
||||
});
|
||||
|
||||
it('renders scripts section title and create button', async () => {
|
||||
render(<Sidebar />);
|
||||
|
||||
expect(screen.getByText('SCRIPTS')).toBeInTheDocument();
|
||||
expect(await screen.findByRole('button', { name: 'New Script' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows loading state while scripts are being fetched', () => {
|
||||
(window as any).electronAPI.scripts.getAll = vi.fn().mockImplementation(
|
||||
() => new Promise(() => {}),
|
||||
);
|
||||
|
||||
render(<Sidebar />);
|
||||
|
||||
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows empty state with create action when no scripts exist', async () => {
|
||||
(window as any).electronAPI.scripts.getAll = vi.fn().mockResolvedValue([]);
|
||||
|
||||
render(<Sidebar />);
|
||||
|
||||
expect(await screen.findByText('No scripts yet')).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Create a script' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('creates a new script from the create button and opens it pinned', async () => {
|
||||
const createMock = vi.fn().mockResolvedValue({
|
||||
id: 'script-new',
|
||||
projectId: 'default',
|
||||
slug: 'new-script',
|
||||
title: 'New Script',
|
||||
kind: 'utility',
|
||||
entrypoint: 'render',
|
||||
enabled: true,
|
||||
version: 1,
|
||||
filePath: '/tmp/new-script.py',
|
||||
content: 'print("new script")',
|
||||
createdAt: '2026-02-22T00:00:00.000Z',
|
||||
updatedAt: '2026-02-22T00:00:00.000Z',
|
||||
});
|
||||
|
||||
(window as any).electronAPI.scripts.create = createMock;
|
||||
|
||||
render(<Sidebar />);
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: 'New Script' }));
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(createMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
title: 'New Script',
|
||||
kind: 'utility',
|
||||
entrypoint: 'render',
|
||||
enabled: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(useAppStore.getState().tabs).toEqual([
|
||||
{
|
||||
type: 'scripts',
|
||||
id: 'script-new',
|
||||
isTransient: false,
|
||||
},
|
||||
]);
|
||||
expect(useAppStore.getState().activeTabId).toBe('script-new');
|
||||
});
|
||||
});
|
||||
|
||||
it('opens a pinned script tab on double click', async () => {
|
||||
render(<Sidebar />);
|
||||
|
||||
const scriptRow = await screen.findByRole('button', { name: 'Hello Script' });
|
||||
fireEvent.doubleClick(scriptRow);
|
||||
|
||||
expect(useAppStore.getState().tabs).toEqual([
|
||||
{
|
||||
type: 'scripts',
|
||||
id: 'script-1',
|
||||
isTransient: false,
|
||||
},
|
||||
]);
|
||||
expect(useAppStore.getState().activeTabId).toBe('script-1');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user