import Foundation import Testing @testable import VirtualCore @MainActor struct ExternalVirtualMachineTests { // MARK: - Store @Test func rememberedBundleIsAvailableAcrossLaunches() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeExternalBundle(named: "External") fixture.makeStore().add(bundleURL) #expect(fixture.availablePaths(in: fixture.makeStore()) == [fixture.canonicalPath(bundleURL)]) } @Test func addingTheSameBundleTwiceKeepsOneReference() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeExternalBundle(named: "External") let store = fixture.makeStore() store.add(bundleURL) store.add(bundleURL) #expect(store.references.count == 1) #expect(store.contains(bundleURL)) } @Test func followsBundleThatWasRenamed() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeExternalBundle(named: "External") let store = fixture.makeStore() store.add(bundleURL) let renamedURL = fixture.externalURL.appendingPathComponent("Renamed.vbvm") try FileManager.default.moveItem(at: bundleURL, to: renamedURL) #expect(fixture.availablePaths(in: store) == [fixture.canonicalPath(renamedURL)]) #expect(store.contains(renamedURL)) #expect(!store.contains(bundleURL)) } @Test func forgetsBundleThatNoLongerExists() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeExternalBundle(named: "External") let store = fixture.makeStore() store.add(bundleURL) try FileManager.default.removeItem(at: bundleURL) #expect(fixture.availablePaths(in: store).isEmpty) #expect(store.references.isEmpty) } @Test func forgetsBundleThatWasMovedIntoTheLibrary() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeExternalBundle(named: "External") let store = fixture.makeStore() store.add(bundleURL) try FileManager.default.moveItem(at: bundleURL, to: fixture.libraryURL.appendingPathComponent("External.vbvm")) #expect(fixture.availablePaths(in: store).isEmpty) #expect(store.references.isEmpty) } @Test func remembersBundleWhileItsVolumeIsNotMounted() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let volumePath = "/Volumes/\(UUID().uuidString)" let reference = try PropertyListDecoder().decode( ExternalVirtualMachineStore.Reference.self, from: PropertyListEncoder().encode(["path": "\(volumePath)/External.vbvm", "volumePath": volumePath]) ) fixture.defaults.set(try PropertyListEncoder().encode([reference]), forKey: ExternalVirtualMachineStore.defaultsKey) let store = fixture.makeStore() #expect(fixture.availablePaths(in: store).isEmpty) #expect(store.references == [reference]) #expect(store.hasReferences(inVolumeAt: URL(fileURLWithPath: volumePath))) #expect(!store.hasReferences(inVolumeAt: URL(fileURLWithPath: "/Volumes/\(UUID().uuidString)"))) } @Test func removedBundleIsNoLongerAvailable() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeExternalBundle(named: "External") let store = fixture.makeStore() store.add(bundleURL) store.remove(bundleURL) #expect(fixture.availablePaths(in: fixture.makeStore()).isEmpty) #expect(FileManager.default.fileExists(atPath: bundleURL.path)) } // MARK: - Library @Test func externalVirtualMachineShowsUpInTheLibraryWithoutBeingCopied() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let libraryBundleURL = try fixture.makeBundle(at: fixture.libraryURL.appendingPathComponent("Internal.vbvm")) let externalBundleURL = try fixture.makeExternalBundle(named: "External") let library = fixture.makeLibrary() #expect(library.virtualMachines.map(\.name) == ["Internal"]) let added = try #require(library.addExternalVirtualMachine(at: externalBundleURL)) #expect(Set(library.virtualMachines.map(\.name)) == ["Internal", "External"]) #expect(library.isExternal(added)) #expect(fixture.canonicalPath(added.bundleURL) == fixture.canonicalPath(externalBundleURL)) #expect(try FileManager.default.contentsOfDirectory(atPath: fixture.libraryURL.path) == [libraryBundleURL.lastPathComponent]) /// A library created later, like on the next launch, shows it as well. #expect(Set(fixture.makeLibrary().virtualMachines.map(\.name)) == ["Internal", "External"]) } @Test func externalVirtualMachineMakesAnEmptyLibraryLoaded() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let externalBundleURL = try fixture.makeExternalBundle(named: "External") let library = fixture.makeLibrary() #expect(library.state.id == .empty) library.addExternalVirtualMachine(at: externalBundleURL) #expect(library.state.id == .loaded) } @Test func virtualMachineInTheLibraryDirectoryIsNotExternal() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let bundleURL = try fixture.makeBundle(at: fixture.libraryURL.appendingPathComponent("Internal.vbvm")) let library = fixture.makeLibrary() let vm = try #require(library.addExternalVirtualMachine(at: bundleURL)) #expect(!library.isExternal(vm)) #expect(library.virtualMachines.count == 1) #expect(fixture.makeStore().references.isEmpty) #expect(throws: (any Error).self) { try library.removeExternalVirtualMachine(vm) } } @Test func removingExternalVirtualMachineLeavesItsFilesUntouched() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let externalBundleURL = try fixture.makeExternalBundle(named: "External") let library = fixture.makeLibrary() let vm = try #require(library.addExternalVirtualMachine(at: externalBundleURL)) try library.removeExternalVirtualMachine(vm) #expect(library.virtualMachines.isEmpty) #expect(FileManager.default.fileExists(atPath: externalBundleURL.path)) #expect(fixture.makeLibrary().virtualMachines.isEmpty) } @Test func externalVirtualMachineIsOnlyOpenedWhenTheUserChoseNotToSeeItInTheLibrary() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let externalBundleURL = try fixture.makeExternalBundle(named: "External") let container = fixture.makeSettingsContainer() container.settings.showExternalVirtualMachinesInLibrary = false let library = VMLibraryController(settingsContainer: container) #expect(!library.showsExternalVirtualMachines) #expect(library.addExternalVirtualMachine(at: externalBundleURL) == nil) #expect(library.virtualMachines.isEmpty) #expect(fixture.makeStore().references.isEmpty) } @Test func externalVirtualMachinesAreHiddenButRememberedWhileTheUserChoosesNotToSeeThem() throws { let fixture = try Fixture() defer { fixture.cleanUp() } try fixture.makeBundle(at: fixture.libraryURL.appendingPathComponent("Internal.vbvm")) let externalBundleURL = try fixture.makeExternalBundle(named: "External") let container = fixture.makeSettingsContainer() let library = VMLibraryController(settingsContainer: container) library.addExternalVirtualMachine(at: externalBundleURL) container.settings.showExternalVirtualMachinesInLibrary = false #expect(library.virtualMachines.map(\.name) == ["Internal"]) #expect(fixture.makeStore().contains(externalBundleURL)) container.settings.showExternalVirtualMachinesInLibrary = true #expect(Set(library.virtualMachines.map(\.name)) == ["Internal", "External"]) } @Test func externalVirtualMachinesShowUpInTheLibraryUnlessTheUserChoosesOtherwise() throws { let fixture = try Fixture() defer { fixture.cleanUp() } #expect(try VBSettings(with: fixture.defaults).showExternalVirtualMachinesInLibrary) var settings = try VBSettings(with: fixture.defaults) settings.showExternalVirtualMachinesInLibrary = false try settings.write(to: fixture.defaults) #expect(try !VBSettings(with: fixture.defaults).showExternalVirtualMachinesInLibrary) } @Test func renamedExternalVirtualMachineStaysInTheLibrary() throws { let fixture = try Fixture() defer { fixture.cleanUp() } let externalBundleURL = try fixture.makeExternalBundle(named: "External") let library = fixture.makeLibrary() let vm = try #require(library.addExternalVirtualMachine(at: externalBundleURL)) try library.rename(vm, to: "Renamed") #expect(library.virtualMachines.map(\.name) == ["Renamed"]) #expect(FileManager.default.fileExists(atPath: fixture.externalURL.appendingPathComponent("Renamed.vbvm").path)) #expect(fixture.makeLibrary().virtualMachines.map(\.name) == ["Renamed"]) } @Test func duplicateOfExternalVirtualMachineShowsUpNextToIt() async throws { let fixture = try Fixture() defer { fixture.cleanUp() } let externalBundleURL = try fixture.makeExternalBundle(named: "External") let library = fixture.makeLibrary() let vm = try #require(library.addExternalVirtualMachine(at: externalBundleURL)) let copy = try await library.duplicate(vm) #expect(fixture.canonicalPath(copy.bundleURL.deletingLastPathComponent()) == fixture.canonicalPath(fixture.externalURL)) #expect(Set(library.virtualMachines.map(\.name)) == ["External", "Copy of External"]) #expect(try FileManager.default.contentsOfDirectory(atPath: fixture.libraryURL.path).isEmpty) } } /// A library directory and a directory outside of it, with their own settings. @MainActor private struct Fixture { let rootURL: URL let libraryURL: URL let externalURL: URL let suiteName: String let defaults: UserDefaults init() throws { rootURL = FileManager.default.temporaryDirectory.appendingPathComponent("ExternalVirtualMachineTests-\(UUID().uuidString)", isDirectory: true) libraryURL = rootURL.appendingPathComponent("Library", isDirectory: true) externalURL = rootURL.appendingPathComponent("External", isDirectory: true) suiteName = "ExternalVirtualMachineTests.\(UUID().uuidString)" defaults = try #require(UserDefaults(suiteName: suiteName)) try FileManager.default.createDirectory(at: libraryURL, withIntermediateDirectories: true) try FileManager.default.createDirectory(at: externalURL, withIntermediateDirectories: true) } func cleanUp() { defaults.removePersistentDomain(forName: suiteName) try? FileManager.default.removeItem(at: rootURL) } @discardableResult func makeBundle(at url: URL) throws -> URL { var vm = try VBVirtualMachine(bundleURL: url, isNewInstall: true) /// The library only loads bundles that have their metadata on disk. try vm.saveMetadata() return vm.bundleURL } func makeExternalBundle(named name: String) throws -> URL { try makeBundle(at: externalURL.appendingPathComponent(name).appendingPathExtension(VBVirtualMachine.bundleExtension)) } func makeStore() -> ExternalVirtualMachineStore { ExternalVirtualMachineStore(defaults: defaults) } func makeSettingsContainer() -> VBSettingsContainer { let container = VBSettingsContainer(with: defaults) container.settings.libraryURL = libraryURL return container } func makeLibrary() -> VMLibraryController { VMLibraryController(settingsContainer: makeSettingsContainer()) } func canonicalPath(_ url: URL) -> String { ExternalVirtualMachineStore.canonicalPath(for: url) } func availablePaths(in store: ExternalVirtualMachineStore) -> [String] { store.resolveAvailableBundleURLs(excludingContentsOf: libraryURL).map(canonicalPath) } }