11/**
22 * @vitest -environment node
33 */
4- import { describe , expect , it } from 'vitest'
4+ import { beforeEach , describe , expect , it , vi } from 'vitest'
55import {
66 getTargetedLayoutChangeSet ,
77 getTargetedLayoutImpact ,
88} from '@/lib/workflows/autolayout/change-set'
99import type { BlockState , WorkflowState } from '@/stores/workflows/workflow/types'
1010
11+ const { mockGetBlock } = vi . hoisted ( ( ) => ( {
12+ mockGetBlock : vi . fn ( ) ,
13+ } ) )
14+
15+ vi . mock ( '@/blocks' , ( ) => ( {
16+ getBlock : mockGetBlock ,
17+ } ) )
18+
19+ const JIRA_TEST_BLOCK_CONFIG = {
20+ category : 'tools' ,
21+ subBlocks : [
22+ { id : 'operation' , type : 'dropdown' } ,
23+ { id : 'domain' , type : 'short-input' } ,
24+ { id : 'credential' , type : 'oauth-input' , mode : 'basic' } ,
25+ { id : 'issueKey' , type : 'short-input' , condition : { field : 'operation' , value : 'read' } } ,
26+ { id : 'projectId' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
27+ { id : 'summary' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
28+ { id : 'description' , type : 'long-input' , condition : { field : 'operation' , value : 'write' } } ,
29+ { id : 'priority' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
30+ { id : 'labels' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
31+ { id : 'issueType' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
32+ { id : 'parentIssue' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
33+ { id : 'assignee' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
34+ { id : 'reporter' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
35+ { id : 'environment' , type : 'long-input' , condition : { field : 'operation' , value : 'write' } } ,
36+ { id : 'components' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
37+ { id : 'fixVersions' , type : 'short-input' , condition : { field : 'operation' , value : 'write' } } ,
38+ ] ,
39+ } as const
40+
1141function createBlock (
1242 id : string ,
1343 overrides : Partial < BlockState > = { } ,
@@ -39,7 +69,44 @@ function createWorkflowState({
3969 }
4070}
4171
72+ function createJiraBlock (
73+ id : string ,
74+ operation : 'read' | 'write' ,
75+ overrides : Partial < BlockState > = { }
76+ ) : BlockState {
77+ return createBlock ( id , {
78+ type : 'jira' ,
79+ position : { x : 100 , y : 100 } ,
80+ height : 100 ,
81+ layout : { measuredWidth : 250 , measuredHeight : 100 } ,
82+ subBlocks : {
83+ operation : {
84+ id : 'operation' ,
85+ type : 'dropdown' ,
86+ value : operation ,
87+ } ,
88+ domain : {
89+ id : 'domain' ,
90+ type : 'short-input' ,
91+ value : 'company.atlassian.net' ,
92+ } ,
93+ credential : {
94+ id : 'credential' ,
95+ type : 'oauth-input' ,
96+ value : 'credential-1' ,
97+ } ,
98+ } ,
99+ ...overrides ,
100+ } )
101+ }
102+
42103describe ( 'getTargetedLayoutChangeSet' , ( ) => {
104+ beforeEach ( ( ) => {
105+ mockGetBlock . mockImplementation ( ( type : string ) =>
106+ type === 'jira' ? JIRA_TEST_BLOCK_CONFIG : undefined
107+ )
108+ } )
109+
43110 it ( 'does not relayout newly added blocks that already have valid positions' , ( ) => {
44111 const before = createWorkflowState ( {
45112 blocks : {
@@ -77,7 +144,15 @@ describe('getTargetedLayoutChangeSet', () => {
77144 it ( 'keeps subblock-only edits anchored' , ( ) => {
78145 const before = createWorkflowState ( {
79146 blocks : {
80- start : createBlock ( 'start' ) ,
147+ start : createBlock ( 'start' , {
148+ subBlocks : {
149+ prompt : {
150+ id : 'prompt' ,
151+ type : 'long-input' ,
152+ value : 'old value' ,
153+ } ,
154+ } ,
155+ } ) ,
81156 } ,
82157 } )
83158
@@ -98,10 +173,40 @@ describe('getTargetedLayoutChangeSet', () => {
98173 expect ( getTargetedLayoutChangeSet ( { before, after } ) ) . toEqual ( [ ] )
99174 } )
100175
176+ it ( 'reopens edited blocks when an operation change increases their visible height' , ( ) => {
177+ const before = createWorkflowState ( {
178+ blocks : {
179+ jira : createJiraBlock ( 'jira' , 'read' ) ,
180+ } ,
181+ } )
182+
183+ const after = createWorkflowState ( {
184+ blocks : {
185+ jira : createJiraBlock ( 'jira' , 'write' ) ,
186+ } ,
187+ } )
188+
189+ expect ( getTargetedLayoutChangeSet ( { before, after } ) ) . toEqual ( [ ] )
190+ expect ( getTargetedLayoutImpact ( { before, after } ) ) . toEqual ( {
191+ layoutBlockIds : [ ] ,
192+ resizedBlockIds : [ 'jira' ] ,
193+ shiftSourceBlockIds : [ ] ,
194+ } )
195+ } )
196+
101197 it ( 'does not relayout a pre-existing block legitimately placed at the origin' , ( ) => {
102198 const before = createWorkflowState ( {
103199 blocks : {
104- start : createBlock ( 'start' , { position : { x : 0 , y : 0 } } ) ,
200+ start : createBlock ( 'start' , {
201+ position : { x : 0 , y : 0 } ,
202+ subBlocks : {
203+ prompt : {
204+ id : 'prompt' ,
205+ type : 'long-input' ,
206+ value : 'old value' ,
207+ } ,
208+ } ,
209+ } ) ,
105210 } ,
106211 } )
107212
@@ -167,6 +272,7 @@ describe('getTargetedLayoutChangeSet', () => {
167272
168273 expect ( getTargetedLayoutImpact ( { before, after } ) ) . toEqual ( {
169274 layoutBlockIds : [ 'function1' ] ,
275+ resizedBlockIds : [ ] ,
170276 shiftSourceBlockIds : [ ] ,
171277 } )
172278 } )
@@ -231,6 +337,7 @@ describe('getTargetedLayoutChangeSet', () => {
231337
232338 expect ( getTargetedLayoutImpact ( { before, after } ) ) . toEqual ( {
233339 layoutBlockIds : [ ] ,
340+ resizedBlockIds : [ ] ,
234341 shiftSourceBlockIds : [ 'source' ] ,
235342 } )
236343 } )
@@ -279,6 +386,7 @@ describe('getTargetedLayoutChangeSet', () => {
279386
280387 expect ( getTargetedLayoutImpact ( { before, after } ) ) . toEqual ( {
281388 layoutBlockIds : [ ] ,
389+ resizedBlockIds : [ ] ,
282390 shiftSourceBlockIds : [ 'a-b' ] ,
283391 } )
284392 } )
@@ -327,6 +435,7 @@ describe('getTargetedLayoutChangeSet', () => {
327435
328436 expect ( getTargetedLayoutImpact ( { before, after } ) ) . toEqual ( {
329437 layoutBlockIds : [ 'inserted' ] ,
438+ resizedBlockIds : [ ] ,
330439 shiftSourceBlockIds : [ 'inserted' ] ,
331440 } )
332441 } )
0 commit comments