- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 335
feat(config): Add window count and workspace pattern support for dyna… #1269
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
      
      
            xand3r40r93
  wants to merge
  2
  commits into
  nikitabobko:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
xand3r40r93:dynamic-gaps
  
      
      
   
  
    
  
  
  
 
  
      
    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.
          
          
  
     Open
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            2 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
          
            153 changes: 153 additions & 0 deletions
          
          153 
        
  Sources/AppBundleTests/config/DynamicConfigValueTests.swift
  
  
      
      
   
        
      
      
    
  
    
      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,153 @@ | ||
| @testable import AppBundle | ||
| import Common | ||
| import TOMLKit | ||
| import XCTest | ||
|  | ||
| final class DynamicConfigValueTests: XCTestCase { | ||
| @MainActor | ||
| func testConstantValue() { | ||
| let value: DynamicConfigValue<Int> = .constant(42) | ||
| let monitor = TestMonitor() | ||
|  | ||
| XCTAssertEqual(value.getValue(for: monitor), 42) | ||
| XCTAssertEqual(value.getValue(for: monitor, windowCount: 1), 42) | ||
| XCTAssertEqual(value.getValue(for: monitor, windowCount: 2), 42) | ||
| } | ||
|  | ||
| @MainActor | ||
| func testPerMonitorWithoutWindows() { | ||
| let mainMonitor = TestMonitor(id: "main") | ||
| let secondaryMonitor = TestMonitor(id: "secondary") | ||
|  | ||
| let mainDesc = MonitorDescription.pattern("main")! | ||
| let value: DynamicConfigValue<Int> = .perMonitor([ | ||
| PerMonitorValue(description: mainDesc, value: 16, windows: nil, workspace: nil), | ||
| ], default: 8) | ||
|  | ||
| // Should match monitor without considering windows | ||
| XCTAssertEqual(value.getValue(for: mainMonitor), 16) | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 1), 16) | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 2), 16) | ||
|  | ||
| // Should use default for non-matching monitor | ||
| XCTAssertEqual(value.getValue(for: secondaryMonitor), 8) | ||
| XCTAssertEqual(value.getValue(for: secondaryMonitor, windowCount: 1), 8) | ||
| } | ||
|  | ||
| @MainActor | ||
| func testPerMonitorWithWindows() { | ||
| let mainMonitor = TestMonitor(id: "main") | ||
| let secondaryMonitor = TestMonitor(id: "secondary") | ||
|  | ||
| let mainDesc = MonitorDescription.pattern("main")! | ||
| let value: DynamicConfigValue<Int> = .perMonitor([ | ||
| PerMonitorValue(description: mainDesc, value: 16, windows: 1, workspace: nil), | ||
| PerMonitorValue(description: mainDesc, value: 32, windows: 2, workspace: nil), | ||
| PerMonitorValue(description: mainDesc, value: 24, windows: nil, workspace: nil), | ||
| ], default: 8) | ||
|  | ||
| // Test exact matches - должны работать только точные совпадения | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 1), 16) | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 2), 32) | ||
|  | ||
| // Test fallback to monitor-only value при отсутствии точного совпадения | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 3), 24) | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 4), 24) | ||
|  | ||
| // Test fallback to default | ||
| XCTAssertEqual(value.getValue(for: secondaryMonitor, windowCount: 1), 8) | ||
| XCTAssertEqual(value.getValue(for: secondaryMonitor, windowCount: 2), 8) | ||
| } | ||
|  | ||
| @MainActor | ||
| func testSingleWindowRule() { | ||
| // Этот тест проверяет случай, когда указано правило только для 1 окна | ||
| let mainMonitor = TestMonitor(id: "main") | ||
|  | ||
| let mainDesc = MonitorDescription.pattern("main")! | ||
| let value: DynamicConfigValue<Int> = .perMonitor([ | ||
| PerMonitorValue(description: mainDesc, value: 720, windows: 1, workspace: nil), | ||
| ], default: 8) | ||
|  | ||
| // Точное совпадение для 1 окна | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 1), 720) | ||
|  | ||
| // Для 2 и более окон должно использоваться значение по умолчанию | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 2), 8) | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 3), 8) | ||
| } | ||
|  | ||
| @MainActor | ||
| func testIgnoreFloatingWindows() { | ||
| // Этот тест проверяет, что плавающие окна игнорируются при подсчете | ||
| // Однако, в тестах мы просто используем значение windowCount | ||
| // Поэтому мы проверяем это косвенно, при помощи isUnitTest и соответствующего кода | ||
|  | ||
| let mainMonitor = TestMonitor(id: "main") | ||
|  | ||
| let mainDesc = MonitorDescription.pattern("main")! | ||
| let value: DynamicConfigValue<Int> = .perMonitor([ | ||
| PerMonitorValue(description: mainDesc, value: 720, windows: 1, workspace: nil), | ||
| PerMonitorValue(description: mainDesc, value: 360, windows: 2, workspace: nil), | ||
| ], default: 8) | ||
|  | ||
| // Тесты работают с переданным windowCount | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 1), 720) | ||
| XCTAssertEqual(value.getValue(for: mainMonitor, windowCount: 2), 360) | ||
| } | ||
|  | ||
| @MainActor | ||
| func testConfigParsing() async throws { | ||
| let config = """ | ||
| [gaps] | ||
| inner.vertical = [ | ||
| { monitor.main = { value = 16, windows = 1 } }, | ||
| { monitor.main = { value = 32, windows = 2 } }, | ||
| { monitor.secondary = 24 }, | ||
| 8 | ||
| ] | ||
| """ | ||
|  | ||
| let (parsed, parseErrors) = parseConfig(config) | ||
| XCTAssertTrue(parseErrors.isEmpty) | ||
|  | ||
| let mainMonitor = TestMonitor(id: "main") | ||
| let secondaryMonitor = TestMonitor(id: "secondary") | ||
|  | ||
| let resolvedMain1 = ResolvedGaps(gaps: parsed.gaps, monitor: mainMonitor, windowCount: 1) | ||
| XCTAssertEqual(resolvedMain1.inner.vertical, 16) | ||
|  | ||
| let resolvedMain2 = ResolvedGaps(gaps: parsed.gaps, monitor: mainMonitor, windowCount: 2) | ||
| XCTAssertEqual(resolvedMain2.inner.vertical, 32) | ||
|  | ||
| let resolvedSecondary = ResolvedGaps(gaps: parsed.gaps, monitor: secondaryMonitor) | ||
| XCTAssertEqual(resolvedSecondary.inner.vertical, 24) | ||
| } | ||
| } | ||
|  | ||
| @MainActor | ||
| private struct TestMonitor: Monitor { | ||
| var id: String | ||
| var name: String | ||
| var monitorAppKitNsScreenScreensId: Int | ||
| var rect: Rect | ||
| var visibleRect: Rect | ||
| var width: CGFloat | ||
| var height: CGFloat | ||
|  | ||
| init(id: String = "test") { | ||
| self.id = id | ||
| self.name = id | ||
| self.monitorAppKitNsScreenScreensId = 1 | ||
| self.rect = Rect(topLeftX: 0, topLeftY: 0, width: 0, height: 0) | ||
| self.visibleRect = Rect(topLeftX: 0, topLeftY: 0, width: 0, height: 0) | ||
| self.width = 0 | ||
| self.height = 0 | ||
| } | ||
| } | ||
|  | ||
| private struct MockBacktrace { | ||
| init() {} | ||
| func appending(_ component: String) -> Self { self } | ||
| static func + (lhs: Self, rhs: String) -> Self { lhs } | ||
| } | ||
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments in Russian, you may want to translate so others can read them :)