Repository navigation
Add browser profile targeting to CLI pane creation #8874
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
Changes from 1 commit
60c59bc
59d1b43
242422d
85e5dd5
03b4d76
8ddeefd
a09135d
99f4726
50c338c
120c9b6
57e0197
21fc8d3
e5c03bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import Foundation | ||
| import Testing | ||
| #if canImport(cmux_DEV) | ||
| @testable import cmux_DEV | ||
| #elseif canImport(cmux) | ||
| @testable import cmux | ||
| #endif | ||
|
|
||
| @MainActor | ||
| @Suite(.serialized) | ||
| struct BrowserProfileSocketTests { | ||
| @Test func browserCreationCommandsHonorExplicitProfilesAndRejectInvalidSelectors() throws { | ||
| let defaults = UserDefaults.standard | ||
| let wasBrowserDisabled = BrowserAvailabilitySettings.isDisabled(defaults: defaults) | ||
| let store = BrowserProfileStore.shared | ||
| let previousLastUsedProfileID = store.effectiveLastUsedProfileID | ||
| let suffix = UUID().uuidString | ||
| let target = try #require(store.createProfile(named: "Issue 2720 Target \(suffix)")) | ||
| let fallback = try #require(store.createProfile(named: "Issue 2720 Fallback \(suffix)")) | ||
| let ambiguousName = "Issue 2720 Shared \(suffix)" | ||
| let ambiguousFirst = try #require(store.createProfile(named: ambiguousName)) | ||
| let ambiguousSecond = try #require(store.createProfile(named: ambiguousName.lowercased())) | ||
| let createdProfileIDs = [target.id, fallback.id, ambiguousFirst.id, ambiguousSecond.id] | ||
|
|
||
| BrowserAvailabilitySettings.setDisabled(false, defaults: defaults) | ||
| defer { | ||
| TerminalController.shared.setActiveTabManager(nil) | ||
| for profileID in createdProfileIDs { | ||
| _ = store.deleteProfile(id: profileID) | ||
| } | ||
| store.noteUsed(previousLastUsedProfileID) | ||
| BrowserAvailabilitySettings.setDisabled(wasBrowserDisabled, defaults: defaults) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| store.noteUsed(fallback.id) | ||
| let openManager = TabManager() | ||
| defer { openManager.tabs.forEach { $0.teardownAllPanels() } } | ||
| let openWorkspace = try #require(openManager.selectedWorkspace) | ||
| let openSourceID = try #require(openWorkspace.focusedPanelId) | ||
| TerminalController.shared.setActiveTabManager(openManager) | ||
|
|
||
| let openResponse = try call( | ||
| method: "browser.open_split", | ||
| params: [ | ||
| "workspace_id": openWorkspace.id.uuidString, | ||
| "surface_id": openSourceID.uuidString, | ||
| "url": "about:blank", | ||
| "profile": target.displayName.lowercased(), | ||
| "focus": false, | ||
| ] | ||
| ) | ||
| let openResult = try successfulResult(openResponse) | ||
| let openedSurfaceID = try #require( | ||
| (openResult["surface_id"] as? String).flatMap(UUID.init(uuidString:)) | ||
| ) | ||
| let openedPanel = try #require(openWorkspace.panels[openedSurfaceID] as? BrowserPanel) | ||
| #expect(openedPanel.profileID == target.id) | ||
|
|
||
| store.noteUsed(fallback.id) | ||
| let paneManager = TabManager() | ||
| defer { paneManager.tabs.forEach { $0.teardownAllPanels() } } | ||
| let paneWorkspace = try #require(paneManager.selectedWorkspace) | ||
| let paneSourceID = try #require(paneWorkspace.focusedPanelId) | ||
| TerminalController.shared.setActiveTabManager(paneManager) | ||
|
|
||
| let paneResponse = try call( | ||
| method: "pane.create", | ||
| params: [ | ||
| "workspace_id": paneWorkspace.id.uuidString, | ||
| "surface_id": paneSourceID.uuidString, | ||
| "direction": "right", | ||
| "type": "browser", | ||
| "url": "about:blank", | ||
| "profile": target.id.uuidString, | ||
| "focus": false, | ||
| ] | ||
| ) | ||
| let paneResult = try successfulResult(paneResponse) | ||
| let paneSurfaceID = try #require( | ||
| (paneResult["surface_id"] as? String).flatMap(UUID.init(uuidString:)) | ||
| ) | ||
| let panePanel = try #require(paneWorkspace.panels[paneSurfaceID] as? BrowserPanel) | ||
| #expect(panePanel.profileID == target.id) | ||
|
|
||
| store.noteUsed(fallback.id) | ||
| let fallbackManager = TabManager() | ||
| defer { fallbackManager.tabs.forEach { $0.teardownAllPanels() } } | ||
| let fallbackWorkspace = try #require(fallbackManager.selectedWorkspace) | ||
| let fallbackSourceID = try #require(fallbackWorkspace.focusedPanelId) | ||
| TerminalController.shared.setActiveTabManager(fallbackManager) | ||
|
|
||
| let fallbackResponse = try call( | ||
| method: "browser.open_split", | ||
| params: [ | ||
| "workspace_id": fallbackWorkspace.id.uuidString, | ||
| "surface_id": fallbackSourceID.uuidString, | ||
| "url": "about:blank", | ||
| "focus": false, | ||
| ] | ||
| ) | ||
| let fallbackResult = try successfulResult(fallbackResponse) | ||
| let fallbackSurfaceID = try #require( | ||
| (fallbackResult["surface_id"] as? String).flatMap(UUID.init(uuidString:)) | ||
| ) | ||
| let fallbackPanel = try #require(fallbackWorkspace.panels[fallbackSurfaceID] as? BrowserPanel) | ||
| #expect(fallbackPanel.profileID == fallback.id) | ||
|
|
||
| let unknownSelector = "Issue 2720 Missing \(suffix)" | ||
| let unknownResponse = try call( | ||
| method: "browser.open_split", | ||
| params: [ | ||
| "workspace_id": fallbackWorkspace.id.uuidString, | ||
| "surface_id": fallbackSourceID.uuidString, | ||
| "profile": unknownSelector, | ||
| ] | ||
| ) | ||
| let unknownError = try errorPayload(unknownResponse) | ||
| #expect(unknownError["code"] as? String == "invalid_params") | ||
| #expect((unknownError["message"] as? String)?.contains(unknownSelector) == true) | ||
| #expect((unknownError["data"] as? [String: Any])?["profile"] as? String == unknownSelector) | ||
|
|
||
| let ambiguousResponse = try call( | ||
| method: "pane.create", | ||
| params: [ | ||
| "workspace_id": fallbackWorkspace.id.uuidString, | ||
| "surface_id": fallbackSourceID.uuidString, | ||
| "direction": "right", | ||
| "type": "browser", | ||
| "profile": ambiguousName.uppercased(), | ||
| ] | ||
| ) | ||
| let ambiguousError = try errorPayload(ambiguousResponse) | ||
| let ambiguousMessage = try #require(ambiguousError["message"] as? String) | ||
| let ambiguousCandidates = try #require( | ||
| (ambiguousError["data"] as? [String: Any])?["candidates"] as? [[String: Any]] | ||
| ) | ||
| #expect(ambiguousError["code"] as? String == "invalid_params") | ||
| #expect(ambiguousMessage.contains(ambiguousFirst.id.uuidString)) | ||
| #expect(ambiguousMessage.contains(ambiguousSecond.id.uuidString)) | ||
| #expect(Set(ambiguousCandidates.compactMap { $0["id"] as? String }) == [ | ||
| ambiguousFirst.id.uuidString, | ||
| ambiguousSecond.id.uuidString, | ||
| ]) | ||
| } | ||
|
|
||
| private func call(method: String, params: [String: Any]) throws -> [String: Any] { | ||
| let request: [String: Any] = [ | ||
| "jsonrpc": "2.0", | ||
| "id": UUID().uuidString, | ||
| "method": method, | ||
| "params": params, | ||
| ] | ||
| let data = try JSONSerialization.data(withJSONObject: request) | ||
| let line = try #require(String(data: data, encoding: .utf8)) | ||
| let response = TerminalController.shared.handleSocketLine(line) | ||
| return try #require( | ||
| JSONSerialization.jsonObject(with: Data(response.utf8)) as? [String: Any] | ||
| ) | ||
| } | ||
|
|
||
| private func successfulResult(_ response: [String: Any]) throws -> [String: Any] { | ||
| #expect(response["ok"] as? Bool == true) | ||
| return try #require(response["result"] as? [String: Any]) | ||
| } | ||
|
|
||
| private func errorPayload(_ response: [String: Any]) throws -> [String: Any] { | ||
| #expect(response["ok"] as? Bool == false) | ||
| return try #require(response["error"] as? [String: Any]) | ||
| } | ||
| } | ||
|
Comment on lines
+13
to
+350
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
All five scenarios — name-based open_split, UUID-based pane.create, no-profile fallback, unknown selector, and ambiguous selector — share a single
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping this as one serialized behavior flow is intentional: it shares one BrowserProfileStore lifecycle and verifies the complete socket contract (name, UUID, fallback, unknown, ambiguous) without multiplying mutation of persisted global profile state. Each scenario has localized #expect/#require diagnostics, while repository edge cases remain split into isolated unit tests. — Claude Code
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point — the shared profile lifecycle and the layered "each subsequent scenario builds on the previous state" structure are real constraints that would complicate splitting. The distinction between socket-level behavioral flow tests and isolated unit tests is a reasonable boundary to draw here. The short-circuit risk from |
||
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.
The assertion
== .ambiguous([first, second])depends on the implementation returning candidates in insertion/creation order. IfresolveProfileSelectionbuilds its candidate list from aSet,Dictionary, or any other unordered structure, this test will fail spuriously even when the result is semantically correct. The downstream socket test (BrowserProfileSocketTests) correctly avoids this by usingSet-based comparison. Consider extracting IDs and comparing as aSethere too, or pinning the order expectation to whatever the production implementation guarantees (e.g. by name).Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
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.
Fixed in 242422d: the test now compares the ambiguous candidate UUIDs as a Set, so it asserts membership without inventing an ordering contract.
— Claude Code