feat: more phase 1 implementation - proper parity now

This commit is contained in:
2026-02-22 22:39:26 +01:00
parent 3ec8819d6d
commit 7cb47e0aa5
13 changed files with 694 additions and 56 deletions

View File

@@ -100,9 +100,9 @@ describe('ScriptEngine', () => {
content: 'def render(context):\n return {"html": "<h1>Hi</h1>"}',
});
expect(created.slug).toBe('render-hero');
expect(created.slug).toBe('render_hero');
expect(mockScripts.has(created.id)).toBe(true);
expect(mockFiles.get('/mock/userData/projects/default/scripts/render-hero.py')).toContain('def render');
expect(mockFiles.get('/mock/userData/projects/default/scripts/render_hero.py')).toContain('def render');
});
it('updates script metadata and file content', async () => {
@@ -117,8 +117,29 @@ describe('ScriptEngine', () => {
content: 'def render(context):\n return {"html": "<h1>Banner</h1>"}',
});
expect(updated?.slug).toBe('render-hero-banner');
expect(mockFiles.get('/mock/userData/projects/default/scripts/render-hero-banner.py')).toContain('Banner');
expect(updated?.slug).toBe('render_hero_banner');
expect(mockFiles.get('/mock/userData/projects/default/scripts/render_hero_banner.py')).toContain('Banner');
});
it('appends underscore numeric suffix for duplicate slugs', async () => {
const first = await scriptEngine.createScript({
title: 'Render Hero',
kind: 'macro',
content: 'def render(context):\n return {"html": "<h1>Hi</h1>"}',
});
vi.mocked((await import('uuid')).v4)
.mockReturnValueOnce('mock-script-id-2');
const second = await scriptEngine.createScript({
title: 'Render Hero',
kind: 'macro',
content: 'def render(context):\n return {"html": "<h1>Again</h1>"}',
});
expect(first.slug).toBe('render_hero');
expect(second.slug).toBe('render_hero_2');
expect(mockFiles.get('/mock/userData/projects/default/scripts/render_hero_2.py')).toContain('Again');
});
it('deletes script metadata and source file', async () => {
@@ -132,6 +153,6 @@ describe('ScriptEngine', () => {
expect(deleted).toBe(true);
expect(mockScripts.has(created.id)).toBe(false);
expect(mockFiles.has('/mock/userData/projects/default/scripts/delete-me.py')).toBe(false);
expect(mockFiles.has('/mock/userData/projects/default/scripts/delete_me.py')).toBe(false);
});
});

View File

@@ -18,6 +18,6 @@ describe('ScriptsView styles', () => {
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);
expect(css).toMatch(/\.scripts-monaco\s*\{[^}]*flex:\s*1;[^}]*min-height:\s*0;[^}]*\}/s);
});
});

View File

@@ -5,6 +5,24 @@ import { ScriptsView } from '../../../src/renderer/components/ScriptsView/Script
import { useAppStore } from '../../../src/renderer/store';
const executeMock = vi.fn();
const monacoPropsSpy = vi.fn();
vi.mock('@monaco-editor/react', () => ({
default: (props: {
value?: string;
onChange?: (value?: string) => void;
language?: string;
}) => {
monacoPropsSpy(props);
return (
<textarea
aria-label="Script Content"
value={props.value || ''}
onChange={(event) => props.onChange?.(event.target.value)}
/>
);
},
}));
vi.mock('../../../src/renderer/python/runtimeManagerInstance', () => ({
getPythonRuntimeManager: () => ({
@@ -59,6 +77,89 @@ describe('ScriptsView', () => {
fireEvent.change(textarea, { target: { value: 'print("updated")' } });
expect(textarea.value).toContain('updated');
expect(monacoPropsSpy).toHaveBeenCalledWith(
expect.objectContaining({
language: 'python',
}),
);
});
it('shows metadata fields and footer timestamps', async () => {
render(<ScriptsView scriptId="script-1" />);
const titleInput = await screen.findByLabelText('Title') as HTMLInputElement;
const slugInput = screen.getByLabelText('Slug') as HTMLInputElement;
const kindSelect = screen.getByLabelText('Kind') as HTMLSelectElement;
const entrypointInput = screen.getByLabelText('Entrypoint') as HTMLInputElement;
const enabledInput = screen.getByLabelText('Enabled') as HTMLInputElement;
await vi.waitFor(() => {
expect(titleInput.value).toBe('Hello Script');
expect(slugInput.value).toBe('hello_script');
});
expect(kindSelect.value).toBe('utility');
expect(entrypointInput.value).toBe('render');
expect(enabledInput.checked).toBe(true);
expect(screen.getByText(/Created:/)).toBeInTheDocument();
expect(screen.getByText(/Updated:/)).toBeInTheDocument();
});
it('saves renamed script metadata and content', async () => {
const updateMock = vi.fn().mockResolvedValue({
id: 'script-1',
projectId: 'default',
slug: 'my_helper_function',
title: 'My Helper Function',
kind: 'utility',
entrypoint: 'render',
enabled: true,
version: 2,
filePath: '/tmp/hello-script.py',
content: 'print("renamed")',
createdAt: '2026-02-22T00:00:00.000Z',
updatedAt: '2026-02-22T00:01:00.000Z',
});
(window as any).electronAPI.scripts.update = updateMock;
render(<ScriptsView scriptId="script-1" />);
const titleInput = await screen.findByLabelText('Title');
const kindSelect = screen.getByLabelText('Kind');
const entrypointInput = screen.getByLabelText('Entrypoint');
const enabledInput = screen.getByLabelText('Enabled');
const textarea = screen.getByLabelText('Script Content');
await vi.waitFor(() => {
expect((titleInput as HTMLInputElement).value).toBe('Hello Script');
});
fireEvent.change(kindSelect, { target: { value: 'transform' } });
fireEvent.click(enabledInput);
fireEvent.change(textarea, { target: { value: 'print("renamed")' } });
await vi.waitFor(() => {
expect((kindSelect as HTMLSelectElement).value).toBe('transform');
expect((enabledInput as HTMLInputElement).checked).toBe(false);
});
fireEvent.click(screen.getByRole('button', { name: 'Save Script' }));
await vi.waitFor(() => {
expect(updateMock).toHaveBeenCalledWith(
'script-1',
expect.objectContaining({
title: 'Hello Script',
slug: 'hello_script',
kind: 'transform',
entrypoint: 'render',
enabled: false,
content: 'print("hello")',
}),
);
});
});
it('runs selected script and writes output into panel output log', async () => {
@@ -77,4 +178,23 @@ describe('ScriptsView', () => {
expect(state.panelOutputEntries.length).toBeGreaterThan(0);
expect(state.panelOutputEntries[state.panelOutputEntries.length - 1].message).toContain('hello');
});
it('deletes script from editor action', async () => {
const deleteMock = vi.fn().mockResolvedValue(true);
(window as any).electronAPI.scripts.delete = deleteMock;
useAppStore.setState({
tabs: [{ type: 'scripts', id: 'script-1', isTransient: false }],
activeTabId: 'script-1',
});
render(<ScriptsView scriptId="script-1" />);
fireEvent.click(await screen.findByRole('button', { name: 'Delete Script' }));
await vi.waitFor(() => {
expect(deleteMock).toHaveBeenCalledWith('script-1');
expect(useAppStore.getState().tabs).toEqual([]);
});
});
});

View File

@@ -8,6 +8,21 @@ describe('Sidebar scripts list behavior', () => {
beforeEach(() => {
vi.clearAllMocks();
const listeners = new Map<string, Set<(event: Event) => void>>();
(window as any).addEventListener = vi.fn((type: string, listener: (event: Event) => void) => {
if (!listeners.has(type)) {
listeners.set(type, new Set());
}
listeners.get(type)?.add(listener);
});
(window as any).removeEventListener = vi.fn((type: string, listener: (event: Event) => void) => {
listeners.get(type)?.delete(listener);
});
(window as any).dispatchEvent = vi.fn((event: Event) => {
listeners.get(event.type)?.forEach((listener) => listener(event));
return true;
});
(window as any).electronAPI = {
...(window as any).electronAPI,
scripts: {
@@ -43,9 +58,11 @@ describe('Sidebar scripts list behavior', () => {
});
it('opens a transient script tab on single click', async () => {
render(<Sidebar />);
const { container } = render(<Sidebar />);
const scriptRow = await screen.findByRole('button', { name: 'Hello Script' });
expect(scriptRow).toHaveClass('chat-list-item');
expect(container.querySelector('.chat-item-date')).not.toBeNull();
fireEvent.click(scriptRow);
expect(useAppStore.getState().tabs).toEqual([
@@ -144,4 +161,70 @@ describe('Sidebar scripts list behavior', () => {
]);
expect(useAppStore.getState().activeTabId).toBe('script-1');
});
it('deletes a script from sidebar action', async () => {
const deleteMock = vi.fn().mockResolvedValue(true);
(window as any).electronAPI.scripts.delete = deleteMock;
useAppStore.setState({
tabs: [{ type: 'scripts', id: 'script-1', isTransient: false }],
activeTabId: 'script-1',
});
render(<Sidebar />);
const deleteButton = await screen.findByTitle('Delete script');
fireEvent.click(deleteButton);
await vi.waitFor(() => {
expect(deleteMock).toHaveBeenCalledWith('script-1');
expect(useAppStore.getState().tabs).toEqual([]);
});
});
it('refreshes scripts list when scripts-changed event is emitted', async () => {
const getAllMock = vi
.fn()
.mockResolvedValueOnce([
{
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',
},
])
.mockResolvedValueOnce([
{
id: 'script-1',
projectId: 'default',
slug: 'renamed_script',
title: 'Renamed Script',
kind: 'utility',
entrypoint: 'render',
enabled: true,
version: 2,
filePath: '/tmp/hello-script.py',
content: 'print("hello")',
createdAt: '2026-02-22T00:00:00.000Z',
updatedAt: '2026-02-22T00:01:00.000Z',
},
]);
(window as any).electronAPI.scripts.getAll = getAllMock;
render(<Sidebar />);
await screen.findByRole('button', { name: 'Hello Script' });
window.dispatchEvent(new CustomEvent('bds:scripts-changed'));
expect(await screen.findByRole('button', { name: 'Renamed Script' })).toBeInTheDocument();
});
});