-
Notifications
You must be signed in to change notification settings - Fork 96
feat(vscode): Add draft workflow system for Designer V2 #8811
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
Open
Elaina-Lee
wants to merge
6
commits into
main
Choose a base branch
from
elaina/vscode-v2-draft
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,369
−43
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
225 changes: 225 additions & 0 deletions
225
apps/vs-code-designer/src/app/utils/codeless/__test__/draftManager.test.ts
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,225 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import * as path from 'path'; | ||
|
|
||
| vi.mock('fs', () => ({ | ||
| existsSync: vi.fn(), | ||
| readFileSync: vi.fn(), | ||
| writeFileSync: vi.fn(), | ||
| unlinkSync: vi.fn(), | ||
| mkdirSync: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../../../../constants', () => ({ | ||
| draftWorkflowFileName: 'workflow.draft.json', | ||
| draftConnectionsFileName: 'connections.draft.json', | ||
| draftParametersFileName: 'parameters.draft.json', | ||
| })); | ||
|
|
||
| import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync } from 'fs'; | ||
| import { | ||
| getDraftWorkflowPath, | ||
| getDraftConnectionsPath, | ||
| getDraftParametersPath, | ||
| hasDraft, | ||
| saveDraft, | ||
| loadDraft, | ||
| discardDraft, | ||
| } from '../draftManager'; | ||
|
|
||
| const workflowFilePath = path.join('/project', 'myWorkflow', 'workflow.json'); | ||
| const workflowDir = path.dirname(workflowFilePath); | ||
|
|
||
| describe('draftManager', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('path helpers', () => { | ||
| it('getDraftWorkflowPath returns workflow.draft.json in the same directory', () => { | ||
| expect(getDraftWorkflowPath(workflowFilePath)).toBe(path.join(workflowDir, 'workflow.draft.json')); | ||
| }); | ||
|
|
||
| it('getDraftConnectionsPath returns connections.draft.json in the same directory', () => { | ||
| expect(getDraftConnectionsPath(workflowFilePath)).toBe(path.join(workflowDir, 'connections.draft.json')); | ||
| }); | ||
|
|
||
| it('getDraftParametersPath returns parameters.draft.json in the same directory', () => { | ||
| expect(getDraftParametersPath(workflowFilePath)).toBe(path.join(workflowDir, 'parameters.draft.json')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('hasDraft', () => { | ||
| it('returns true when draft workflow file exists', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true); | ||
| expect(hasDraft(workflowFilePath)).toBe(true); | ||
| expect(existsSync).toHaveBeenCalledWith(path.join(workflowDir, 'workflow.draft.json')); | ||
| }); | ||
|
|
||
| it('returns false when draft workflow file does not exist', () => { | ||
| vi.mocked(existsSync).mockReturnValue(false); | ||
| expect(hasDraft(workflowFilePath)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('saveDraft', () => { | ||
| const definition = { triggers: {}, actions: { action1: { type: 'Http' } } }; | ||
| const connectionReferences = { conn1: { api: { id: '/providers/test' } } }; | ||
| const parameters = { param1: { type: 'String', value: 'hello' } }; | ||
|
|
||
| it('creates directory and writes definition file', () => { | ||
| saveDraft(workflowFilePath, { definition }); | ||
|
|
||
| expect(mkdirSync).toHaveBeenCalledWith(workflowDir, { recursive: true }); | ||
| expect(writeFileSync).toHaveBeenCalledWith( | ||
| path.join(workflowDir, 'workflow.draft.json'), | ||
| JSON.stringify(definition, null, 4), | ||
| 'utf8' | ||
| ); | ||
| }); | ||
|
|
||
| it('writes connections file when connectionReferences provided', () => { | ||
| saveDraft(workflowFilePath, { definition, connectionReferences }); | ||
|
|
||
| expect(writeFileSync).toHaveBeenCalledWith( | ||
| path.join(workflowDir, 'connections.draft.json'), | ||
| JSON.stringify(connectionReferences, null, 4), | ||
| 'utf8' | ||
| ); | ||
| }); | ||
|
|
||
| it('writes parameters file when parameters provided', () => { | ||
| saveDraft(workflowFilePath, { definition, parameters }); | ||
|
|
||
| expect(writeFileSync).toHaveBeenCalledWith( | ||
| path.join(workflowDir, 'parameters.draft.json'), | ||
| JSON.stringify(parameters, null, 4), | ||
| 'utf8' | ||
| ); | ||
| }); | ||
|
|
||
| it('does not write connections file when connectionReferences is undefined', () => { | ||
| saveDraft(workflowFilePath, { definition }); | ||
|
|
||
| const writeCallPaths = vi.mocked(writeFileSync).mock.calls.map((call) => call[0]); | ||
| expect(writeCallPaths).not.toContain(path.join(workflowDir, 'connections.draft.json')); | ||
| }); | ||
|
|
||
| it('does not write parameters file when parameters is undefined', () => { | ||
| saveDraft(workflowFilePath, { definition }); | ||
|
|
||
| const writeCallPaths = vi.mocked(writeFileSync).mock.calls.map((call) => call[0]); | ||
| expect(writeCallPaths).not.toContain(path.join(workflowDir, 'parameters.draft.json')); | ||
| }); | ||
|
|
||
| it('writes all three files when all data provided', () => { | ||
| saveDraft(workflowFilePath, { definition, connectionReferences, parameters }); | ||
|
|
||
| expect(writeFileSync).toHaveBeenCalledTimes(3); | ||
| }); | ||
| }); | ||
|
|
||
| describe('loadDraft', () => { | ||
| const definition = { triggers: {}, actions: {} }; | ||
| const connections = { conn1: { api: { id: '/test' } } }; | ||
| const parameters = { param1: { type: 'String' } }; | ||
|
|
||
| it('returns hasDraft false when no draft file exists', () => { | ||
| vi.mocked(existsSync).mockReturnValue(false); | ||
|
|
||
| const result = loadDraft(workflowFilePath); | ||
|
|
||
| expect(result).toEqual({ hasDraft: false }); | ||
| }); | ||
|
|
||
| it('loads only workflow definition when only draft workflow exists', () => { | ||
| vi.mocked(existsSync).mockImplementation((p: any) => { | ||
| return p === path.join(workflowDir, 'workflow.draft.json'); | ||
| }); | ||
| vi.mocked(readFileSync).mockReturnValue(JSON.stringify(definition)); | ||
|
|
||
| const result = loadDraft(workflowFilePath); | ||
|
|
||
| expect(result.hasDraft).toBe(true); | ||
| expect(result.draftWorkflow).toEqual(definition); | ||
| expect(result.draftConnections).toBeUndefined(); | ||
| expect(result.draftParameters).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('loads all draft artifacts when all files exist', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true); | ||
| vi.mocked(readFileSync).mockImplementation((p: any) => { | ||
| if (String(p).includes('workflow.draft.json')) { | ||
| return JSON.stringify(definition); | ||
| } | ||
| if (String(p).includes('connections.draft.json')) { | ||
| return JSON.stringify(connections); | ||
| } | ||
| if (String(p).includes('parameters.draft.json')) { | ||
| return JSON.stringify(parameters); | ||
| } | ||
| return '{}'; | ||
| }); | ||
|
|
||
| const result = loadDraft(workflowFilePath); | ||
|
|
||
| expect(result.hasDraft).toBe(true); | ||
| expect(result.draftWorkflow).toEqual(definition); | ||
| expect(result.draftConnections).toEqual(connections); | ||
| expect(result.draftParameters).toEqual(parameters); | ||
| }); | ||
|
|
||
| it('loads workflow and connections without parameters', () => { | ||
| vi.mocked(existsSync).mockImplementation((p: any) => { | ||
| return !String(p).includes('parameters.draft.json'); | ||
| }); | ||
| vi.mocked(readFileSync).mockImplementation((p: any) => { | ||
| if (String(p).includes('workflow.draft.json')) { | ||
| return JSON.stringify(definition); | ||
| } | ||
| if (String(p).includes('connections.draft.json')) { | ||
| return JSON.stringify(connections); | ||
| } | ||
| return '{}'; | ||
| }); | ||
|
|
||
| const result = loadDraft(workflowFilePath); | ||
|
|
||
| expect(result.hasDraft).toBe(true); | ||
| expect(result.draftWorkflow).toEqual(definition); | ||
| expect(result.draftConnections).toEqual(connections); | ||
| expect(result.draftParameters).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('discardDraft', () => { | ||
| it('deletes all existing draft files', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true); | ||
|
|
||
| discardDraft(workflowFilePath); | ||
|
|
||
| expect(unlinkSync).toHaveBeenCalledTimes(3); | ||
| expect(unlinkSync).toHaveBeenCalledWith(path.join(workflowDir, 'workflow.draft.json')); | ||
| expect(unlinkSync).toHaveBeenCalledWith(path.join(workflowDir, 'connections.draft.json')); | ||
| expect(unlinkSync).toHaveBeenCalledWith(path.join(workflowDir, 'parameters.draft.json')); | ||
| }); | ||
|
|
||
| it('only deletes files that exist', () => { | ||
| vi.mocked(existsSync).mockImplementation((p: any) => { | ||
| return String(p).includes('workflow.draft.json'); | ||
| }); | ||
|
|
||
| discardDraft(workflowFilePath); | ||
|
|
||
| expect(unlinkSync).toHaveBeenCalledTimes(1); | ||
| expect(unlinkSync).toHaveBeenCalledWith(path.join(workflowDir, 'workflow.draft.json')); | ||
| }); | ||
|
|
||
| it('does nothing when no draft files exist', () => { | ||
| vi.mocked(existsSync).mockReturnValue(false); | ||
|
|
||
| discardDraft(workflowFilePath); | ||
|
|
||
| expect(unlinkSync).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
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.