|
| 1 | +// |
| 2 | +// WelcomeViewModelTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +@testable import TablePro |
| 7 | +import TableProPluginKit |
| 8 | +import XCTest |
| 9 | + |
| 10 | +@MainActor |
| 11 | +final class WelcomeViewModelTests: XCTestCase { |
| 12 | + private var suiteName: String! |
| 13 | + private var defaults: UserDefaults! |
| 14 | + private var syncSuiteName: String! |
| 15 | + private var syncDefaults: UserDefaults! |
| 16 | + private var connectionFileURL: URL! |
| 17 | + private var groupStorage: GroupStorage! |
| 18 | + private var connectionStorage: ConnectionStorage! |
| 19 | + private var viewModel: WelcomeViewModel! |
| 20 | + |
| 21 | + override func setUp() { |
| 22 | + super.setUp() |
| 23 | + let unique = UUID().uuidString |
| 24 | + suiteName = "com.TablePro.tests.WelcomeViewModel.\(unique)" |
| 25 | + syncSuiteName = "com.TablePro.tests.WelcomeViewModel.sync.\(unique)" |
| 26 | + guard let defaults = UserDefaults(suiteName: suiteName), |
| 27 | + let syncDefaults = UserDefaults(suiteName: syncSuiteName) else { |
| 28 | + XCTFail("Could not create isolated UserDefaults suites") |
| 29 | + return |
| 30 | + } |
| 31 | + self.defaults = defaults |
| 32 | + self.syncDefaults = syncDefaults |
| 33 | + let tracker = SyncChangeTracker(metadataStorage: SyncMetadataStorage(userDefaults: syncDefaults)) |
| 34 | + connectionFileURL = FileManager.default.temporaryDirectory |
| 35 | + .appendingPathComponent("tablepro-tests") |
| 36 | + .appendingPathComponent("welcome-connections_\(unique).json") |
| 37 | + try? FileManager.default.createDirectory( |
| 38 | + at: connectionFileURL.deletingLastPathComponent(), |
| 39 | + withIntermediateDirectories: true |
| 40 | + ) |
| 41 | + connectionStorage = ConnectionStorage( |
| 42 | + fileURL: connectionFileURL, |
| 43 | + userDefaults: defaults, |
| 44 | + syncTracker: tracker |
| 45 | + ) |
| 46 | + groupStorage = GroupStorage( |
| 47 | + userDefaults: defaults, |
| 48 | + syncTracker: tracker, |
| 49 | + connectionStorage: self.connectionStorage |
| 50 | + ) |
| 51 | + viewModel = WelcomeViewModel(services: makeServices()) |
| 52 | + } |
| 53 | + |
| 54 | + override func tearDown() { |
| 55 | + defaults.removePersistentDomain(forName: suiteName) |
| 56 | + syncDefaults.removePersistentDomain(forName: syncSuiteName) |
| 57 | + try? FileManager.default.removeItem(at: connectionFileURL) |
| 58 | + viewModel = nil |
| 59 | + groupStorage = nil |
| 60 | + connectionStorage = nil |
| 61 | + defaults = nil |
| 62 | + syncDefaults = nil |
| 63 | + suiteName = nil |
| 64 | + syncSuiteName = nil |
| 65 | + connectionFileURL = nil |
| 66 | + super.tearDown() |
| 67 | + } |
| 68 | + |
| 69 | + private func makeServices() -> AppServices { |
| 70 | + let live = AppServices.live |
| 71 | + return AppServices( |
| 72 | + appEvents: live.appEvents, |
| 73 | + appSettings: live.appSettings, |
| 74 | + appSettingsStorage: live.appSettingsStorage, |
| 75 | + connectionStorage: connectionStorage, |
| 76 | + databaseManager: live.databaseManager, |
| 77 | + pluginManager: live.pluginManager, |
| 78 | + schemaService: live.schemaService, |
| 79 | + schemaProviderRegistry: live.schemaProviderRegistry, |
| 80 | + sqlFavoriteManager: live.sqlFavoriteManager, |
| 81 | + favoriteTablesStorage: live.favoriteTablesStorage, |
| 82 | + aiChatStorage: live.aiChatStorage, |
| 83 | + aiKeyStorage: live.aiKeyStorage, |
| 84 | + groupStorage: groupStorage, |
| 85 | + tagStorage: live.tagStorage, |
| 86 | + sshProfileStorage: live.sshProfileStorage, |
| 87 | + licenseManager: live.licenseManager, |
| 88 | + conflictResolver: live.conflictResolver, |
| 89 | + syncMetadataStorage: live.syncMetadataStorage, |
| 90 | + favoritesExpansionState: live.favoritesExpansionState, |
| 91 | + linkedFolderWatcher: live.linkedFolderWatcher, |
| 92 | + queryHistoryManager: live.queryHistoryManager, |
| 93 | + dateFormattingService: live.dateFormattingService, |
| 94 | + copilotService: live.copilotService, |
| 95 | + mcpServerManager: live.mcpServerManager, |
| 96 | + syncTracker: live.syncTracker, |
| 97 | + themeEngine: live.themeEngine |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + private func groupIds(in nodes: [ConnectionGroupTreeNode]) -> [UUID] { |
| 102 | + nodes.flatMap { node -> [UUID] in |
| 103 | + guard case .group(let group, let children) = node else { return [] } |
| 104 | + return [group.id] + groupIds(in: children) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + func testCreateGroupShowsImmediatelyInTree() throws { |
| 109 | + XCTAssertTrue(groupIds(in: viewModel.treeItems).isEmpty) |
| 110 | + |
| 111 | + viewModel.createGroup(name: "Production", color: .red, parentId: nil) |
| 112 | + |
| 113 | + let created = try XCTUnwrap(groupStorage.loadGroups().first { $0.name == "Production" }) |
| 114 | + XCTAssertTrue(groupIds(in: viewModel.treeItems).contains(created.id)) |
| 115 | + XCTAssertTrue(viewModel.expandedGroupIds.contains(created.id)) |
| 116 | + } |
| 117 | + |
| 118 | + func testCreateSubgroupExpandsParentAndChild() throws { |
| 119 | + viewModel.createGroup(name: "Parent", color: .none, parentId: nil) |
| 120 | + let parentId = try XCTUnwrap(groupStorage.loadGroups().first { $0.name == "Parent" }?.id) |
| 121 | + |
| 122 | + viewModel.createGroup(name: "Child", color: .none, parentId: parentId) |
| 123 | + let childId = try XCTUnwrap(groupStorage.loadGroups().first { $0.name == "Child" }?.id) |
| 124 | + |
| 125 | + XCTAssertTrue(groupIds(in: viewModel.treeItems).contains(parentId)) |
| 126 | + XCTAssertTrue(groupIds(in: viewModel.treeItems).contains(childId)) |
| 127 | + XCTAssertTrue(viewModel.expandedGroupIds.contains(parentId)) |
| 128 | + XCTAssertTrue(viewModel.expandedGroupIds.contains(childId)) |
| 129 | + } |
| 130 | + |
| 131 | + func testCreateDuplicateNameDoesNotAddSecondNode() { |
| 132 | + viewModel.createGroup(name: "Staging", color: .orange, parentId: nil) |
| 133 | + viewModel.createGroup(name: "staging", color: .blue, parentId: nil) |
| 134 | + |
| 135 | + let stagingNodes = groupIds(in: viewModel.treeItems).filter { id in |
| 136 | + viewModel.groups.first { $0.id == id }?.name.lowercased() == "staging" |
| 137 | + } |
| 138 | + XCTAssertEqual(stagingNodes.count, 1) |
| 139 | + } |
| 140 | +} |
0 commit comments