Add tests for PrinterRequestManager , remove singleton implementation in favor of view constructor arguments

This commit is contained in:
Madeline 2023-03-20 19:58:40 -04:00
parent ff551c9a5f
commit 6341b0464d
9 changed files with 98 additions and 40 deletions

View File

@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
E124B9D929941A4D00C0D2D2 /* PrinterConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E124B9D829941A4D00C0D2D2 /* PrinterConfigView.swift */; }; E124B9D929941A4D00C0D2D2 /* PrinterConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E124B9D829941A4D00C0D2D2 /* PrinterConfigView.swift */; };
E16378B229A43CE1002F05E9 /* SoyuzScratchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */; }; E16378B229A43CE1002F05E9 /* SoyuzScratchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */; };
E16378B429A491E6002F05E9 /* PrinterRequestsManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B329A491E6002F05E9 /* PrinterRequestsManagerTests.swift */; }; E16378B429A491E6002F05E9 /* PrinterRequestManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16378B329A491E6002F05E9 /* PrinterRequestManagerTests.swift */; };
E180B5E92992CD9100425DB0 /* SoyuzApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B5E82992CD9100425DB0 /* SoyuzApp.swift */; }; E180B5E92992CD9100425DB0 /* SoyuzApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E180B5E82992CD9100425DB0 /* SoyuzApp.swift */; };
E180B5ED2992CD9200425DB0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E180B5EC2992CD9200425DB0 /* Assets.xcassets */; }; E180B5ED2992CD9200425DB0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E180B5EC2992CD9200425DB0 /* Assets.xcassets */; };
E180B5F02992CD9200425DB0 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E180B5EF2992CD9200425DB0 /* Preview Assets.xcassets */; }; E180B5F02992CD9200425DB0 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E180B5EF2992CD9200425DB0 /* Preview Assets.xcassets */; };
@ -46,7 +46,7 @@
E124B9D72993FE5500C0D2D2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; }; E124B9D72993FE5500C0D2D2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
E124B9D829941A4D00C0D2D2 /* PrinterConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterConfigView.swift; sourceTree = "<group>"; }; E124B9D829941A4D00C0D2D2 /* PrinterConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterConfigView.swift; sourceTree = "<group>"; };
E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoyuzScratchTests.swift; sourceTree = "<group>"; }; E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoyuzScratchTests.swift; sourceTree = "<group>"; };
E16378B329A491E6002F05E9 /* PrinterRequestsManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterRequestsManagerTests.swift; sourceTree = "<group>"; }; E16378B329A491E6002F05E9 /* PrinterRequestManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrinterRequestManagerTests.swift; sourceTree = "<group>"; };
E180B5E52992CD9100425DB0 /* Soyuz.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Soyuz.app; sourceTree = BUILT_PRODUCTS_DIR; }; E180B5E52992CD9100425DB0 /* Soyuz.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Soyuz.app; sourceTree = BUILT_PRODUCTS_DIR; };
E180B5E82992CD9100425DB0 /* SoyuzApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoyuzApp.swift; sourceTree = "<group>"; }; E180B5E82992CD9100425DB0 /* SoyuzApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoyuzApp.swift; sourceTree = "<group>"; };
E180B5EC2992CD9200425DB0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; }; E180B5EC2992CD9200425DB0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -143,7 +143,7 @@
children = ( children = (
E180B5FF2992CD9300425DB0 /* SoyuzTests.swift */, E180B5FF2992CD9300425DB0 /* SoyuzTests.swift */,
E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */, E16378B129A43CE1002F05E9 /* SoyuzScratchTests.swift */,
E16378B329A491E6002F05E9 /* PrinterRequestsManagerTests.swift */, E16378B329A491E6002F05E9 /* PrinterRequestManagerTests.swift */,
); );
path = SoyuzTests; path = SoyuzTests;
sourceTree = "<group>"; sourceTree = "<group>";
@ -309,7 +309,7 @@
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
E180B6002992CD9300425DB0 /* SoyuzTests.swift in Sources */, E180B6002992CD9300425DB0 /* SoyuzTests.swift in Sources */,
E16378B429A491E6002F05E9 /* PrinterRequestsManagerTests.swift in Sources */, E16378B429A491E6002F05E9 /* PrinterRequestManagerTests.swift in Sources */,
E16378B229A43CE1002F05E9 /* SoyuzScratchTests.swift in Sources */, E16378B229A43CE1002F05E9 /* SoyuzScratchTests.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;

View File

@ -2,6 +2,6 @@
<Workspace <Workspace
version = "1.0"> version = "1.0">
<FileRef <FileRef
location = "self:/Volumes/SNAP/Users/averyadapace/Code/MacOS/KlipperMon/Soyuz.xcodeproj"> location = "self:">
</FileRef> </FileRef>
</Workspace> </Workspace>

View File

@ -9,6 +9,11 @@
<key>orderHint</key> <key>orderHint</key>
<integer>0</integer> <integer>0</integer>
</dict> </dict>
<key>Soyuz.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict> </dict>
</dict> </dict>
</plist> </plist>

View File

@ -9,7 +9,7 @@ import SwiftUI
import Network import Network
struct PrinterConfigView: View { struct PrinterConfigView: View {
@ObservedObject var printerManager = PrinterRequestManager.shared @ObservedObject var printerManager: PrinterRequestManager
var body: some View { var body: some View {
VStack { VStack {
@ -52,8 +52,10 @@ struct PrinterConfigView: View {
} }
struct PrinterConfigView_Previews: PreviewProvider { struct PrinterConfigView_Previews: PreviewProvider {
@State static var printerManager = PrinterRequestManager()
static var previews: some View { static var previews: some View {
PrinterConfigView() PrinterConfigView(printerManager: printerManager)
} }
} }

View File

@ -7,6 +7,7 @@
import Foundation import Foundation
import Network import Network
import AppKit
import Starscream import Starscream
// MARK: Bonjour Protocol // MARK: Bonjour Protocol
@ -17,13 +18,22 @@ protocol NetworkDiscoveryEngine {
func setStateUpdateHandler(_ handler: @escaping ((NWBrowser.State) -> Void)) func setStateUpdateHandler(_ handler: @escaping ((NWBrowser.State) -> Void))
} }
//protocol NDEResult {
// var endpoint: NWEndpoint { get }
// var interface: [NWInterface] { get }
// var metadata: NWBrowser.Result.Metadata { get }
//}
extension NWBrowser: NetworkDiscoveryEngine { extension NWBrowser: NetworkDiscoveryEngine {
//class NDEngine: NetworkDiscoveryEngine,
func startScan(queue: DispatchQueue) { func startScan(queue: DispatchQueue) {
start(queue: queue) start(queue: queue)
} }
func setBrowseResultsChangedHandler(_ handler: @escaping ((Set<NWBrowser.Result>, Set<NWBrowser.Result.Change>) -> Void)) { func setBrowseResultsChangedHandler(_ handler: @escaping ((Set<NWBrowser.Result>, Set<NWBrowser.Result.Change>) -> Void)) {
self.browseResultsChangedHandler = handler self.browseResultsChangedHandler = handler
} }
func setStateUpdateHandler(_ handler: @escaping ((State) -> Void)) { func setStateUpdateHandler(_ handler: @escaping ((State) -> Void)) {
self.stateUpdateHandler = handler self.stateUpdateHandler = handler
} }
@ -50,8 +60,6 @@ struct JsonRpcRequest: Codable {
class PrinterRequestManager: ObservableObject, WebSocketDelegate { class PrinterRequestManager: ObservableObject, WebSocketDelegate {
let WEBSOCKET_TIMEOUT_INTERVAL: TimeInterval = 60.0 let WEBSOCKET_TIMEOUT_INTERVAL: TimeInterval = 60.0
static let shared = PrinterRequestManager()
// Debug stuff // Debug stuff
let startDate = Date() let startDate = Date()
let startDateString: String let startDateString: String
@ -96,8 +104,6 @@ class PrinterRequestManager: ObservableObject, WebSocketDelegate {
var socket: WebSocket? var socket: WebSocket?
var lastPingDate = Date() var lastPingDate = Date()
// TODO: Set this up to actually reconnect
// Parse a JSON-RPC query-response message // Parse a JSON-RPC query-response message
func parse_response(_ response: jsonRpcResponse) { func parse_response(_ response: jsonRpcResponse) {
state = response.result.status.print_stats?.state ?? "" state = response.result.status.print_stats?.state ?? ""
@ -124,7 +130,7 @@ class PrinterRequestManager: ObservableObject, WebSocketDelegate {
} }
} }
private init(browser: NetworkDiscoveryEngine = NWBrowser(for: .bonjourWithTXTRecord(type: "_moonraker._tcp", domain: "local."), using: .tcp)) { init(browser: NetworkDiscoveryEngine = NWBrowser(for: .bonjourWithTXTRecord(type: "_moonraker._tcp", domain: "local."), using: .tcp)) {
state = "" state = ""
progress = 0.0 progress = 0.0
extruderTemperature = 0.0 extruderTemperature = 0.0
@ -167,6 +173,38 @@ class PrinterRequestManager: ObservableObject, WebSocketDelegate {
}) })
// Start up the bonjour browser, get results and process them in the update handler // Start up the bonjour browser, get results and process them in the update handler
nwBrowser.startScan(queue: DispatchQueue.main) nwBrowser.startScan(queue: DispatchQueue.main)
// Screen sleep functionality
// NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(screenDidSleep(_:)), name: NSWorkspace.screensDidSleepNotification, object: nil)
// NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(screenDidWake(_:)), name: NSWorkspace.screensDidWakeNotification, object: nil)
let center = NSWorkspace.shared.notificationCenter;
let mainQueue = OperationQueue.main
center.addObserver(forName: NSWorkspace.screensDidWakeNotification, object: nil, queue: mainQueue) { notification in
self.screenChangedSleepState(notification)
}
center.addObserver(forName: NSWorkspace.screensDidSleepNotification, object: nil, queue: mainQueue) { notification in
self.screenChangedSleepState(notification)
}
}
func screenChangedSleepState(_ notification: Notification) {
switch(notification.name) {
case NSWorkspace.screensDidSleepNotification:
socket?.disconnect()
case NSWorkspace.screensDidWakeNotification:
self.openWebsocket()
default:
return
}
}
func screenDidWake(_ notification: Notification) {
print("Screen woke: \(notification.name)")
if socket != nil {
self.openWebsocket()
}
} }
// Called from the UI with an endpoint. // Called from the UI with an endpoint.

View File

@ -13,6 +13,8 @@ struct SoyuzApp: App {
@State var currentIcon = "move.3d" @State var currentIcon = "move.3d"
@ObservedObject var printerManager = PrinterRequestManager()
var body: some Scene { var body: some Scene {
// WindowGroup(id: "floating-stats") { // WindowGroup(id: "floating-stats") {
// KlipperMonMenuBarExtraView(currentMenuBarIcon: $currentIcon) // KlipperMonMenuBarExtraView(currentMenuBarIcon: $currentIcon)
@ -20,13 +22,13 @@ struct SoyuzApp: App {
// } // }
WindowGroup("Configuration", id: "soyuz_cfg", content: { WindowGroup("Configuration", id: "soyuz_cfg", content: {
PrinterConfigView() PrinterConfigView(printerManager: printerManager)
//.frame(minWidth: 300, maxWidth: 600, minHeight: 60, maxHeight: 100) //.frame(minWidth: 300, maxWidth: 600, minHeight: 60, maxHeight: 100)
}) })
.windowResizability(.contentSize) .windowResizability(.contentSize)
MenuBarExtra("Soyuz", systemImage: currentIcon) { MenuBarExtra("Soyuz", systemImage: currentIcon) {
SoyuzMenuBarExtraView(currentMenuBarIcon: $currentIcon) SoyuzMenuBarExtraView(printerManager: printerManager, currentMenuBarIcon: $currentIcon)
.padding([.top, .leading, .trailing], 8) .padding([.top, .leading, .trailing], 8)
.padding([.bottom], 6) .padding([.bottom], 6)
} }

View File

@ -15,7 +15,7 @@ struct SoyuzMenuBarExtraView: View {
@Environment(\.openWindow) var openWindow @Environment(\.openWindow) var openWindow
@ObservedObject var printerManager = PrinterRequestManager.shared @ObservedObject var printerManager: PrinterRequestManager
@State var printPercentage: Double = 0 @State var printPercentage: Double = 0
@ -100,8 +100,10 @@ struct SoyuzMenuBarExtraView: View {
struct KlipperMonMenuBarExtraView_Previews: PreviewProvider { struct KlipperMonMenuBarExtraView_Previews: PreviewProvider {
@State static var currentMenuBarIcon = "move.3d" @State static var currentMenuBarIcon = "move.3d"
@State static var printerManager = PrinterRequestManager()
static var previews: some View { static var previews: some View {
SoyuzMenuBarExtraView(currentMenuBarIcon: $currentMenuBarIcon) SoyuzMenuBarExtraView(printerManager: printerManager, currentMenuBarIcon: $currentMenuBarIcon)
} }
} }

View File

@ -0,0 +1,33 @@
//
// PrinterRequestsManagerTests.swift
// SoyuzTests
//
// Created by maddiefuzz on 2/21/23.
//
import XCTest
import Network
@testable import Soyuz
class PrinterRequestManagerTests: XCTestCase {
var printerRequestManager: PrinterRequestManager?
var testBonjourListener: NWListener?
override func setUp() {
printerRequestManager = PrinterRequestManager(browser: NWBrowser(for: .bonjour(type: "_http._tcp", domain: "local."), using: .tcp))
// Set up test bonjour server
//let parameters = NWParameters(tls: .none, tcp: NWListener.)
do {
testBonjourListener = try NWListener(using: .tcp, on: .http)
testBonjourListener!.start(queue: DispatchQueue.main)
} catch {
print("Error: \(error)")
}
}
func testBonjourDiscoveredItemsNotNil() {
XCTAssertNotNil(printerRequestManager?.nwBrowserDiscoveredItems)
}
}

View File

@ -1,24 +0,0 @@
//
// PrinterRequestsManagerTests.swift
// SoyuzTests
//
// Created by maddiefuzz on 2/21/23.
//
import XCTest
@testable import Soyuz
class FileHandleMock: FileHandle {
override func write(_ data: Data) {
return
}
}
class PrinterRequestManagerTests: XCTestCase {
var printerRequestsManager: PrinterRequestManager?
override func setUp() {
printerRequestsManager = PrinterRequestManager.shared
}
}