|
7 | 7 | // and deduplication logic in QueryTabManager. |
8 | 8 | // |
9 | 9 |
|
| 10 | +import AppKit |
10 | 11 | import Foundation |
11 | 12 | @testable import TablePro |
12 | 13 | import Testing |
@@ -190,3 +191,74 @@ struct PersistedTabSourceFileURLTests { |
190 | 191 | #expect(decoded.sourceFileURL == nil) |
191 | 192 | } |
192 | 193 | } |
| 194 | + |
| 195 | +// MARK: - WindowLifecycleMonitor Source File Tracking Tests |
| 196 | + |
| 197 | +@Suite("WindowLifecycleMonitor source file tracking") |
| 198 | +@MainActor |
| 199 | +struct WindowLifecycleMonitorSourceFileTests { |
| 200 | + @Test("window(forSourceFile:) returns nil for unregistered URL") |
| 201 | + func unregisteredURLReturnsNil() { |
| 202 | + let url = URL(fileURLWithPath: "/tmp/unknown.sql") |
| 203 | + #expect(WindowLifecycleMonitor.shared.window(forSourceFile: url) == nil) |
| 204 | + } |
| 205 | + |
| 206 | + @Test("registerSourceFile and window(forSourceFile:) round-trip when window is alive") |
| 207 | + func registerAndFindSourceFile() { |
| 208 | + let url = URL(fileURLWithPath: "/tmp/registered.sql") |
| 209 | + let windowId = UUID() |
| 210 | + let window = NSWindow() |
| 211 | + |
| 212 | + WindowLifecycleMonitor.shared.register( |
| 213 | + window: window, |
| 214 | + connectionId: UUID(), |
| 215 | + windowId: windowId |
| 216 | + ) |
| 217 | + WindowLifecycleMonitor.shared.registerSourceFile(url, windowId: windowId) |
| 218 | + |
| 219 | + #expect(WindowLifecycleMonitor.shared.window(forSourceFile: url) === window) |
| 220 | + |
| 221 | + WindowLifecycleMonitor.shared.unregisterSourceFile(url) |
| 222 | + WindowLifecycleMonitor.shared.unregisterWindow(for: windowId) |
| 223 | + } |
| 224 | + |
| 225 | + @Test("unregisterSourceFiles(for:) removes all files for a window") |
| 226 | + func unregisterAllFilesForWindow() { |
| 227 | + let url1 = URL(fileURLWithPath: "/tmp/file1.sql") |
| 228 | + let url2 = URL(fileURLWithPath: "/tmp/file2.sql") |
| 229 | + let windowId = UUID() |
| 230 | + let window = NSWindow() |
| 231 | + |
| 232 | + WindowLifecycleMonitor.shared.register( |
| 233 | + window: window, |
| 234 | + connectionId: UUID(), |
| 235 | + windowId: windowId |
| 236 | + ) |
| 237 | + WindowLifecycleMonitor.shared.registerSourceFile(url1, windowId: windowId) |
| 238 | + WindowLifecycleMonitor.shared.registerSourceFile(url2, windowId: windowId) |
| 239 | + |
| 240 | + WindowLifecycleMonitor.shared.unregisterSourceFiles(for: windowId) |
| 241 | + |
| 242 | + #expect(WindowLifecycleMonitor.shared.window(forSourceFile: url1) == nil) |
| 243 | + #expect(WindowLifecycleMonitor.shared.window(forSourceFile: url2) == nil) |
| 244 | + |
| 245 | + WindowLifecycleMonitor.shared.unregisterWindow(for: windowId) |
| 246 | + } |
| 247 | + |
| 248 | + @Test("window(forSourceFile:) returns nil after window is unregistered") |
| 249 | + func returnsNilAfterWindowUnregistered() { |
| 250 | + let url = URL(fileURLWithPath: "/tmp/closed.sql") |
| 251 | + let windowId = UUID() |
| 252 | + let window = NSWindow() |
| 253 | + |
| 254 | + WindowLifecycleMonitor.shared.register( |
| 255 | + window: window, |
| 256 | + connectionId: UUID(), |
| 257 | + windowId: windowId |
| 258 | + ) |
| 259 | + WindowLifecycleMonitor.shared.registerSourceFile(url, windowId: windowId) |
| 260 | + WindowLifecycleMonitor.shared.unregisterWindow(for: windowId) |
| 261 | + |
| 262 | + #expect(WindowLifecycleMonitor.shared.window(forSourceFile: url) == nil) |
| 263 | + } |
| 264 | +} |
0 commit comments