-
Notifications
You must be signed in to change notification settings - Fork 91
feat(plcopen): PLCopen XML import parser + import/export UI (NODE-111, NODE-112) #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b4c8ca5
feat(plcopen): port PLCopen XML import parser and wire import/export …
dcoutinho1328 d2a83f8
fix(plcopen): register confirm-plcopen-import in ALL_MODAL_TYPES
dcoutinho1328 2ef0feb
fix(plcopen): sort imports to satisfy simple-import-sort lint rule
dcoutinho1328 57144eb
Merge remote-tracking branch 'origin/development' into feature/plcope…
dcoutinho1328 627e5dc
style: reformat with prettier after merging development
dcoutinho1328 205a7e1
Merge remote-tracking branch 'origin/development' into feature/plcope…
dcoutinho1328 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /** | ||
| * `getPlcopenImportFilePath` / `getPlcopenExportSavePath` — focused tests. | ||
| * | ||
| * Electron's `dialog` is mocked (no real native dialog in Jest); the | ||
| * filesystem is real (per-test temp dir) so read/write failure paths | ||
| * are exercised against actual I/O rather than stubbed error shapes. | ||
| */ | ||
|
|
||
| import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs' | ||
| import { tmpdir } from 'os' | ||
| import { join } from 'path' | ||
|
|
||
| const showOpenDialogMock = jest.fn() | ||
| const showSaveDialogMock = jest.fn() | ||
|
|
||
| jest.mock('electron', () => ({ | ||
| BrowserWindow: class {}, | ||
| dialog: { | ||
| showOpenDialog: (...args: unknown[]) => showOpenDialogMock(...args), | ||
| showSaveDialog: (...args: unknown[]) => showSaveDialogMock(...args), | ||
| }, | ||
| })) | ||
|
|
||
| import { getPlcopenExportSavePath, getPlcopenImportFilePath } from '../path-picker' | ||
|
|
||
| describe('getPlcopenImportFilePath', () => { | ||
| let dir: string | ||
|
|
||
| beforeEach(() => { | ||
| dir = mkdtempSync(join(tmpdir(), 'plcopen-import-')) | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| it('reads and returns the selected file content', async () => { | ||
| const filePath = join(dir, 'program.xml') | ||
| writeFileSync(filePath, '<project/>') | ||
| showOpenDialogMock.mockResolvedValue({ canceled: false, filePaths: [filePath] }) | ||
|
|
||
| const result = await getPlcopenImportFilePath({} as never) | ||
|
|
||
| expect(showOpenDialogMock).toHaveBeenCalledWith( | ||
| {}, | ||
| expect.objectContaining({ | ||
| title: 'Select a PLCopen XML file to import', | ||
| properties: ['openFile'], | ||
| filters: [{ name: 'PLCopen XML', extensions: ['xml'] }], | ||
| }), | ||
| ) | ||
| expect(result).toEqual({ success: true, content: '<project/>' }) | ||
| }) | ||
|
|
||
| it('returns a canceled error when the user dismisses the dialog', async () => { | ||
| showOpenDialogMock.mockResolvedValue({ canceled: true, filePaths: [] }) | ||
|
|
||
| const result = await getPlcopenImportFilePath({} as never) | ||
|
|
||
| expect(result).toEqual({ | ||
| success: false, | ||
| error: { title: 'Operation canceled', description: 'Operation canceled by the user.' }, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns a read error when the selected file cannot be read', async () => { | ||
| const missingPath = join(dir, 'missing.xml') | ||
| showOpenDialogMock.mockResolvedValue({ canceled: false, filePaths: [missingPath] }) | ||
|
|
||
| const result = await getPlcopenImportFilePath({} as never) | ||
|
|
||
| expect(result).toEqual({ | ||
| success: false, | ||
| error: { title: 'Error reading file', description: 'Failed to read the selected PLCopen XML file.' }, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getPlcopenExportSavePath', () => { | ||
| let dir: string | ||
|
|
||
| beforeEach(() => { | ||
| dir = mkdtempSync(join(tmpdir(), 'plcopen-export-')) | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| it('writes the XML content to the chosen path', async () => { | ||
| const filePath = join(dir, 'exported.xml') | ||
| showSaveDialogMock.mockResolvedValue({ canceled: false, filePath }) | ||
|
|
||
| const result = await getPlcopenExportSavePath({} as never, 'exported.xml', '<project/>') | ||
|
|
||
| expect(showSaveDialogMock).toHaveBeenCalledWith( | ||
| {}, | ||
| expect.objectContaining({ | ||
| title: 'Export PLCopen XML', | ||
| defaultPath: 'exported.xml', | ||
| filters: [{ name: 'PLCopen XML', extensions: ['xml'] }], | ||
| }), | ||
| ) | ||
| expect(result).toEqual({ success: true }) | ||
| expect(readFileSync(filePath, 'utf-8')).toBe('<project/>') | ||
| }) | ||
|
|
||
| it('returns a canceled error when the user dismisses the dialog', async () => { | ||
| showSaveDialogMock.mockResolvedValue({ canceled: true, filePath: undefined }) | ||
|
|
||
| const result = await getPlcopenExportSavePath({} as never, 'exported.xml', '<project/>') | ||
|
|
||
| expect(result).toEqual({ | ||
| success: false, | ||
| error: { title: 'Operation canceled', description: 'Operation canceled by the user.' }, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns a write error when the target path cannot be written', async () => { | ||
| const badPath = join(dir, 'nonexistent-subdir', 'exported.xml') | ||
| showSaveDialogMock.mockResolvedValue({ canceled: false, filePath: badPath }) | ||
|
|
||
| const result = await getPlcopenExportSavePath({} as never, 'exported.xml', '<project/>') | ||
|
|
||
| expect(result).toEqual({ | ||
| success: false, | ||
| error: { title: 'Error writing file', description: 'Failed to write the PLCopen XML file.' }, | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/frontend/components/_organisms/modals/__tests__/confirm-plcopen-import-modal.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { fireEvent, render, screen } from '@testing-library/react' | ||
|
|
||
| const mockProjectPort = { id: 'fake-project-port' } | ||
| vi.mock('../../../../../middleware/shared/providers', () => ({ | ||
| useProject: () => mockProjectPort, | ||
| })) | ||
|
|
||
| const mockExecuteImportPlcopen = vi.fn() | ||
| vi.mock('../../../../services/import-actions', () => ({ | ||
| executeImportPlcopen: (...args: unknown[]) => mockExecuteImportPlcopen(...args), | ||
| })) | ||
|
|
||
| const closeModal = vi.fn() | ||
| const onOpenChange = vi.fn() | ||
| vi.mock('../../../../store', () => ({ | ||
| useOpenPLCStore: () => ({ | ||
| modalActions: { onOpenChange, closeModal }, | ||
| }), | ||
| })) | ||
|
|
||
| import { ConfirmPlcopenImportModal } from '../confirm-plcopen-import-modal' | ||
|
|
||
| describe('ConfirmPlcopenImportModal', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockExecuteImportPlcopen.mockResolvedValue({ success: true }) | ||
| }) | ||
|
|
||
| it('renders the overwrite warning copy when open', () => { | ||
| render(<ConfirmPlcopenImportModal isOpen />) | ||
| expect(screen.getByText('Import PLCopen XML?')).toBeTruthy() | ||
| expect( | ||
| screen.getByText( | ||
| 'Importing a PLCopen XML file will overwrite the entire currently open project. This cannot be undone.', | ||
| ), | ||
| ).toBeTruthy() | ||
| }) | ||
|
|
||
| it('renders nothing visible when closed', () => { | ||
| render(<ConfirmPlcopenImportModal isOpen={false} />) | ||
| expect(screen.queryByText('Import PLCopen XML?')).toBeNull() | ||
| }) | ||
|
|
||
| it('calls executeImportPlcopen with the project port and closes the modal on confirm', async () => { | ||
| render(<ConfirmPlcopenImportModal isOpen />) | ||
|
|
||
| fireEvent.click(screen.getByText('Import PLCopen XML')) | ||
|
|
||
| // Flush the async handler. | ||
| await Promise.resolve() | ||
| await Promise.resolve() | ||
|
|
||
| expect(mockExecuteImportPlcopen).toHaveBeenCalledWith(mockProjectPort) | ||
| expect(closeModal).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('closes without importing on cancel', () => { | ||
| render(<ConfirmPlcopenImportModal isOpen />) | ||
|
|
||
| fireEvent.click(screen.getByText('Cancel')) | ||
|
|
||
| expect(mockExecuteImportPlcopen).not.toHaveBeenCalled() | ||
| expect(closeModal).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
70 changes: 70 additions & 0 deletions
70
src/frontend/components/_organisms/modals/confirm-plcopen-import-modal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * Confirmation modal for File → "Import PLCopen XML". | ||
| * | ||
| * Distinct from `confirm-delete-project` in that it carries no data | ||
| * payload — it acts on whatever project is currently open (an in-place | ||
| * content overwrite), not a specific record picked from a list. | ||
| */ | ||
|
|
||
| import { useProject } from '../../../../middleware/shared/providers' | ||
| import { WarningIcon } from '../../../assets/icons/interface/Warning' | ||
| import { executeImportPlcopen } from '../../../services/import-actions' | ||
| import { useOpenPLCStore } from '../../../store' | ||
| import { Modal, ModalContent } from '../../_molecules/modal' | ||
|
|
||
| type ConfirmPlcopenImportModalProps = { | ||
| isOpen: boolean | ||
| } | ||
|
|
||
| const ConfirmPlcopenImportModal = ({ isOpen, ...rest }: ConfirmPlcopenImportModalProps) => { | ||
| const projectPort = useProject() | ||
| const { | ||
| modalActions: { onOpenChange, closeModal }, | ||
| } = useOpenPLCStore() | ||
|
|
||
| const handleConfirm = async () => { | ||
| await executeImportPlcopen(projectPort) | ||
| closeModal() | ||
| } | ||
|
|
||
| return ( | ||
| <Modal | ||
| open={isOpen} | ||
| onOpenChange={(open) => { | ||
| if (!open) closeModal() | ||
| onOpenChange('confirm-plcopen-import', open) | ||
| }} | ||
| {...rest} | ||
| > | ||
| <ModalContent className='flex max-h-96 w-[340px] select-none flex-col items-center justify-evenly rounded-lg'> | ||
| <div className='flex select-none flex-col items-center gap-5'> | ||
| <WarningIcon className='mt-2 h-[73px] w-[73px]' /> | ||
| <div className='flex flex-col gap-2'> | ||
| <p className='text-m w-full text-center font-bold text-gray-600 dark:text-neutral-100'> | ||
| Import PLCopen XML? | ||
| </p> | ||
| <p className='w-full text-center text-xs text-gray-500 dark:text-neutral-400'> | ||
| Importing a PLCopen XML file will overwrite the entire currently open project. This cannot be undone. | ||
| </p> | ||
| </div> | ||
| <div className='flex w-[220px] flex-col gap-1 space-y-2 text-sm'> | ||
| <button | ||
| onClick={() => void handleConfirm()} | ||
| className='w-full rounded-lg bg-brand px-4 py-2 text-center font-medium text-white' | ||
| > | ||
| Import PLCopen XML | ||
| </button> | ||
| <button | ||
| onClick={() => closeModal()} | ||
| className='w-full rounded-md bg-neutral-100 px-4 py-2 font-medium dark:bg-neutral-850 dark:text-neutral-100' | ||
| > | ||
| Cancel | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </ModalContent> | ||
| </Modal> | ||
| ) | ||
| } | ||
|
|
||
| export { ConfirmPlcopenImportModal } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.