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');
|
||||
});
|
||||
});
|
||||
@@ -87,7 +87,20 @@ describe('activityBehavior', () => {
|
||||
});
|
||||
|
||||
it('supports all expected activity ids', () => {
|
||||
const ids: ActivityId[] = ['posts', 'pages', 'media', 'tags', 'chat', 'import', 'git', 'settings'];
|
||||
expect(ids).toHaveLength(8);
|
||||
const ids: ActivityId[] = ['posts', 'pages', 'media', 'scripts', 'tags', 'chat', 'import', 'git', 'settings'];
|
||||
expect(ids).toHaveLength(9);
|
||||
});
|
||||
|
||||
it('returns posts-style sidebar actions for scripts', () => {
|
||||
const hiddenSidebarSnapshot = createSnapshot({ activeView: 'posts', sidebarVisible: false });
|
||||
expect(getActivityClickActions(hiddenSidebarSnapshot, 'scripts')).toEqual([
|
||||
{ type: 'setActiveView', view: 'scripts' },
|
||||
{ type: 'toggleSidebar' },
|
||||
]);
|
||||
|
||||
const visibleSidebarSnapshot = createSnapshot({ activeView: 'posts', sidebarVisible: true });
|
||||
expect(getActivityClickActions(visibleSidebarSnapshot, 'scripts')).toEqual([
|
||||
{ type: 'setActiveView', view: 'scripts' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,6 +20,7 @@ describe('editorRouting', () => {
|
||||
'git-diff': 'git-diff',
|
||||
documentation: 'documentation',
|
||||
'site-validation': 'site-validation',
|
||||
scripts: 'scripts',
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ describe('sidebarViewRegistry', () => {
|
||||
'posts',
|
||||
'pages',
|
||||
'media',
|
||||
'scripts',
|
||||
'settings',
|
||||
'tags',
|
||||
'chat',
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
getGitDiffCommitTabSpec,
|
||||
getGitDiffFileTabSpec,
|
||||
getImportTabSpec,
|
||||
getScriptTabSpec,
|
||||
parseGitDiffTabId,
|
||||
openChatTab,
|
||||
getSingletonToolTabSpec,
|
||||
@@ -12,6 +13,7 @@ import {
|
||||
openGitDiffCommitTab,
|
||||
openGitDiffFileTab,
|
||||
openImportTab,
|
||||
openScriptTab,
|
||||
openSingletonToolTab,
|
||||
} from '../../../src/renderer/navigation/tabPolicy';
|
||||
|
||||
@@ -20,6 +22,7 @@ describe('tabPolicy', () => {
|
||||
expect(getSingletonToolTabSpec('settings')).toEqual({ type: 'settings', id: 'settings', isTransient: false });
|
||||
expect(getSingletonToolTabSpec('tags')).toEqual({ type: 'tags', id: 'tags', isTransient: false });
|
||||
expect(getSingletonToolTabSpec('style')).toEqual({ type: 'style', id: 'style', isTransient: false });
|
||||
expect(getSingletonToolTabSpec('scripts')).toEqual({ type: 'scripts', id: 'scripts', isTransient: false });
|
||||
expect(getSingletonToolTabSpec('menu-editor')).toEqual({ type: 'menu-editor', id: 'menu-editor', isTransient: false });
|
||||
expect(getSingletonToolTabSpec('documentation')).toEqual({ type: 'documentation', id: 'documentation', isTransient: false });
|
||||
expect(getSingletonToolTabSpec('metadata-diff')).toEqual({ type: 'metadata-diff', id: 'metadata-diff', isTransient: false });
|
||||
@@ -93,6 +96,35 @@ describe('tabPolicy', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('provides canonical script tab spec for preview and pin intents', () => {
|
||||
expect(getScriptTabSpec('script-1', 'preview')).toEqual({
|
||||
type: 'scripts',
|
||||
id: 'script-1',
|
||||
isTransient: true,
|
||||
});
|
||||
|
||||
expect(getScriptTabSpec('script-1', 'pin')).toEqual({
|
||||
type: 'scripts',
|
||||
id: 'script-1',
|
||||
isTransient: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('opens script tabs from shared policy', () => {
|
||||
const opened: Array<{ type: string; id: string; isTransient: boolean }> = [];
|
||||
const openTab = (tab: { type: string; id: string; isTransient: boolean }) => {
|
||||
opened.push(tab);
|
||||
};
|
||||
|
||||
openScriptTab(openTab, 'script-preview', 'preview');
|
||||
openScriptTab(openTab, 'script-pin', 'pin');
|
||||
|
||||
expect(opened).toEqual([
|
||||
{ type: 'scripts', id: 'script-preview', isTransient: true },
|
||||
{ type: 'scripts', id: 'script-pin', isTransient: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it('builds and parses git-diff file and commit tab specs', () => {
|
||||
expect(getGitDiffFileTabSpec('posts/first.md', 'preview')).toEqual({
|
||||
type: 'git-diff',
|
||||
|
||||
@@ -22,4 +22,10 @@ describe('vite renderer chunking', () => {
|
||||
const resolved = typeof viteConfig === 'function' ? viteConfig({ command: 'build', mode: 'production', isSsrBuild: false, isPreview: false }) : viteConfig;
|
||||
expect(resolved.build?.chunkSizeWarningLimit).toBe(8000);
|
||||
});
|
||||
|
||||
it('excludes pyodide from optimizeDeps pre-bundling', () => {
|
||||
const resolved = typeof viteConfig === 'function' ? viteConfig({ command: 'serve', mode: 'development', isSsrBuild: false, isPreview: false }) : viteConfig;
|
||||
const excluded = resolved.optimizeDeps?.exclude ?? [];
|
||||
expect(excluded).toContain('pyodide');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user