Skip to content

Commit 6cc68ef

Browse files
easonliang28easonLiangWorldedtech
authored andcommitted
fix(activate): harden tab creation serialization and centralize title-bar posts
- openClineInNewTab: extract the unserialized creation body into createTabPanelUnlocked and guard the in-flight slot clear so a settled creation cannot clobber a replacement already stored in the slot. - onDidDispose: clear the tracked tab ref only when the disposing panel is still the tracked one, so a late disposal of a replaced panel cannot clobber the replacement's ref. - MDM lookup failure: log the fallback to the output channel instead of swallowing it silently. - Route the six title-bar button handlers through a shared postActions helper that posts each action in order and logs failures with the handler-specific prefix. - package.json: add the four InTab commands to the command palette, scoped to the active tab panel. - Tests: handler-level regression for openInNewTab + popoutButtonClicked started before the first creation resolves; fresh-creation test for a settled in-flight promise; stale-panel disposal regression; retained panel assertion for disposed tab instances; rightmost-editor column placement assertion; MDM fallback output assertion; %s placeholders for primitive it.each titles. - Stryker directives for the two equivalent setPanel type-literal mutants (setPanel branches only on type === sidebar).
1 parent cbf0edb commit 6cc68ef

3 files changed

Lines changed: 301 additions & 121 deletions

File tree

‎src/activate/__tests__/registerCommands.spec.ts‎

Lines changed: 149 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Mock } from "vitest"
22
import * as vscode from "vscode"
33
import { TelemetryService } from "@roo-code/telemetry"
44

5+
import { ContextProxy } from "../../core/config/ContextProxy"
56
import { ClineProvider } from "../../core/webview/ClineProvider"
67
import { MdmService } from "../../services/mdm/MdmService"
78

@@ -283,20 +284,22 @@ describe("registerCommands handlers", () => {
283284
"zoo-code.historyButtonClickedInTab",
284285
"zoo-code.marketplaceButtonClickedInTab",
285286
]
286-
it.each(inTabNoOpCommands)("$command is a no-op when no tab panel is tracked", async (command) => {
287+
it.each(inTabNoOpCommands)("%s is a no-op when no tab panel is tracked", async (command) => {
287288
await handlers[command]()
288289

289290
expect(ClineProvider.getInstanceForView as Mock).not.toHaveBeenCalled()
290291
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
291292
expect(mockVisibleProvider.postMessageToWebview).not.toHaveBeenCalled()
292293
})
293294

294-
it.each(inTabNoOpCommands)("$command is a no-op when the tab instance is disposed", async (command) => {
295-
setPanel({} as vscode.WebviewPanel, "tab")
295+
it.each(inTabNoOpCommands)("%s is a no-op when the tab instance is disposed", async (command) => {
296+
const disposedPanel = {} as vscode.WebviewPanel
297+
setPanel(disposedPanel, "tab")
296298
;(ClineProvider.getInstanceForView as Mock).mockReturnValue(undefined)
297299

298300
await handlers[command]()
299301

302+
expect(ClineProvider.getInstanceForView as Mock).toHaveBeenCalledWith(disposedPanel)
300303
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
301304
expect(mockVisibleProvider.postMessageToWebview).not.toHaveBeenCalled()
302305
})
@@ -657,6 +660,11 @@ describe("openClineInNewTab", () => {
657660
expect(ctor).toHaveBeenCalledWith(mockContext, mockOutputChannel, "editor", undefined, undefined)
658661
expect(provider).toBe(ctor.mock.instances[0])
659662
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1)
663+
664+
// The fallback is observable in the output channel.
665+
expect(mockOutputChannel.appendLine).toHaveBeenCalledWith(
666+
"[openClineInNewTab] MDM service unavailable, continuing without it: Error: MDM service not initialized",
667+
)
660668
})
661669

662670
it("opens a new group to the right and targets ViewColumn.Two when no editors are visible", async () => {
@@ -708,6 +716,27 @@ describe("openClineInNewTab", () => {
708716
)
709717
})
710718

719+
it("places the tab panel one column right of the rightmost visible editor", async () => {
720+
// openClineInNewTab only reads viewColumn from each editor, so the
721+
// fixtures keep that single field.
722+
;(vscode.window as unknown as { visibleTextEditors: vscode.TextEditor[] }).visibleTextEditors = [
723+
{ viewColumn: 1 } as unknown as vscode.TextEditor,
724+
{ viewColumn: 3 } as unknown as vscode.TextEditor,
725+
]
726+
727+
await openClineInNewTab({ context: mockContext, outputChannel: mockOutputChannel })
728+
729+
// lastCol is 3, so the panel lands on column 4 without opening a new
730+
// editor group.
731+
expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith("workbench.action.newGroupRight")
732+
expect(vscode.window.createWebviewPanel).toHaveBeenCalledWith(
733+
"zoo-code.TabPanelProvider",
734+
"Zoo Code",
735+
4,
736+
expect.objectContaining({ enableScripts: true }),
737+
)
738+
})
739+
711740
it("constructs the tab provider with the 'editor' context and the live MdmService instance", async () => {
712741
const mockMdm = { name: "mock-mdm" }
713742
// MdmService has a private constructor, so pin a sentinel stand-in.
@@ -767,4 +796,121 @@ describe("openClineInNewTab", () => {
767796
expect(second).toBe(constructed)
768797
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1)
769798
})
799+
800+
it("shares one in-flight creation when openInNewTab and popoutButtonClicked start before it resolves", async () => {
801+
// Defer the first creation at ContextProxy.getInstance so both command
802+
// handlers can start while the creation is still in flight.
803+
let resolveContextProxy!: () => void
804+
;(ContextProxy.getInstance as Mock).mockReturnValue(
805+
new Promise<void>((resolve) => {
806+
resolveContextProxy = resolve
807+
}),
808+
)
809+
810+
const commandHandlers: Record<string, (...args: unknown[]) => unknown> = {}
811+
;(vscode.commands.registerCommand as Mock).mockImplementation(
812+
(id: string, cb: (...args: unknown[]) => unknown) => {
813+
commandHandlers[id] = cb
814+
return { dispose: vi.fn() }
815+
},
816+
)
817+
const sidebarProvider = { postMessageToWebview: vi.fn().mockResolvedValue(undefined) }
818+
registerCommands({
819+
context: mockContext,
820+
outputChannel: mockOutputChannel,
821+
provider: sidebarProvider as unknown as ClineProvider,
822+
})
823+
824+
const started = [commandHandlers["zoo-code.openInNewTab"](), commandHandlers["zoo-code.popoutButtonClicked"]()]
825+
826+
// While the shared creation is suspended at ContextProxy.getInstance,
827+
// neither caller has created a panel yet.
828+
expect(vscode.window.createWebviewPanel).not.toHaveBeenCalled()
829+
830+
resolveContextProxy()
831+
const [first, second] = await Promise.all(started)
832+
833+
// Both command entry points await the shared in-flight creation:
834+
// exactly one tab panel is created and both results are the same
835+
// constructed provider.
836+
const ctor = ClineProvider as unknown as Mock
837+
const constructed = ctor.mock.instances[0]
838+
expect(constructed).toBeDefined()
839+
expect(first).toBe(constructed)
840+
expect(second).toBe(constructed)
841+
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1)
842+
})
843+
844+
it("creates a fresh panel for a new call once the previous creation settled and its provider disposed", async () => {
845+
// The first open settles and tracks its panel.
846+
await openClineInNewTab({ context: mockContext, outputChannel: mockOutputChannel })
847+
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1)
848+
849+
// The tracked provider is disposed, so the next open cannot reuse the
850+
// existing tab: the settled (and cleared) in-flight promise must not
851+
// be returned, and a fresh panel is created.
852+
;(ClineProvider.getInstanceForView as Mock).mockReturnValue(undefined)
853+
854+
const secondProvider = await openClineInNewTab({ context: mockContext, outputChannel: mockOutputChannel })
855+
856+
const ctor = ClineProvider as unknown as Mock
857+
const second = ctor.mock.instances[1]
858+
expect(second).toBeDefined()
859+
expect(secondProvider).toBe(second)
860+
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(2)
861+
})
862+
863+
it("keeps the replacement panel tracked when a stale panel's disposal fires late", async () => {
864+
// Capture each created panel so the first panel's (stale) dispose
865+
// handler can fire after the replacement is already tracked.
866+
const createdPanels: { onDidDispose: Mock }[] = []
867+
;(vscode.window.createWebviewPanel as Mock).mockImplementation(() => {
868+
const panel = {
869+
webview: { postMessage: vi.fn() },
870+
onDidChangeViewState: vi.fn(),
871+
onDidDispose: vi.fn(),
872+
}
873+
createdPanels.push(panel)
874+
return panel
875+
})
876+
877+
// First open creates and tracks panel A.
878+
await openClineInNewTab({ context: mockContext, outputChannel: mockOutputChannel })
879+
expect(getPanel()).toBe(createdPanels[0])
880+
881+
// Panel A's provider is disposed before the second open, so the
882+
// second open creates the replacement panel B.
883+
;(ClineProvider.getInstanceForView as Mock).mockReturnValue(undefined)
884+
await openClineInNewTab({ context: mockContext, outputChannel: mockOutputChannel })
885+
expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(2)
886+
expect(getPanel()).toBe(createdPanels[1])
887+
888+
// Panel A's stale dispose handler fires after the replacement is
889+
// tracked; it must not clobber the replacement's ref.
890+
createdPanels[0].onDidDispose.mock.calls[0][0]()
891+
892+
expect(getPanel()).toBe(createdPanels[1])
893+
894+
// Tab-surface commands still reach the provider that owns the
895+
// replacement panel after the stale disposal.
896+
const replacementProvider = { postMessageToWebview: vi.fn().mockResolvedValue(undefined) }
897+
;(ClineProvider.getInstanceForView as Mock).mockReturnValue(replacementProvider)
898+
const commandHandlers: Record<string, (...args: unknown[]) => unknown> = {}
899+
;(vscode.commands.registerCommand as Mock).mockImplementation(
900+
(id: string, cb: (...args: unknown[]) => unknown) => {
901+
commandHandlers[id] = cb
902+
return { dispose: vi.fn() }
903+
},
904+
)
905+
registerCommands({
906+
context: mockContext,
907+
outputChannel: mockOutputChannel,
908+
provider: {} as ClineProvider,
909+
})
910+
await commandHandlers["zoo-code.historyButtonClickedInTab"]()
911+
expect(replacementProvider.postMessageToWebview).toHaveBeenCalledWith({
912+
type: "action",
913+
action: "historyButtonClicked",
914+
})
915+
})
770916
})

0 commit comments

Comments
 (0)