From 31d2da72bb5f683da8e563c9a1a572f21a7c3c16 Mon Sep 17 00:00:00 2001 From: zackverham <96081108+zackverham@users.noreply.github.com> Date: Fri, 13 Feb 2026 13:35:45 -0500 Subject: [PATCH] test(vscode): add tests for last selection loading on initialization Add integration tests verifying that the last selected deployment is correctly loaded when the extension initializes. This addresses issue #2473 as a follow-up to the regression fix in PR #2460. The tests verify: 1. A saved selection in workspace state is retrieved on initialization 2. The full initialization flow works: selection -> content record -> config These tests would catch regressions like #2459 where the last selected deployment was not populating properly on extension UI initialization. Closes #2473 Co-Authored-By: Claude Opus 4.5 --- extensions/vscode/src/state.test.ts | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/extensions/vscode/src/state.test.ts b/extensions/vscode/src/state.test.ts index 111985e2f5..32c1276081 100644 --- a/extensions/vscode/src/state.test.ts +++ b/extensions/vscode/src/state.test.ts @@ -141,6 +141,91 @@ describe("PublisherState", () => { }); }); + // Regression test for https://github.com/posit-dev/publisher/issues/2459 + // Verifies that the last selected deployment is loaded on initialization + // when reopening VS Code with a previously saved selection. + test("last selection is loaded on initialization", () => { + const savedSelection: DeploymentSelectorState = + selectionStateFactory.build(); + + // Initialize with a pre-existing selection in workspace state + // This simulates reopening VS Code after a deployment was previously selected + const { mockWorkspace, mockContext } = mkExtensionContextStateMock({ + [LocalState.LastSelectionState]: savedSelection, + }); + const publisherState = new PublisherState(mockContext); + + // Verify the saved selection is retrieved from workspace state + const currentSelection = publisherState.getSelection(); + expect(mockWorkspace.get).toHaveBeenCalledWith( + LocalState.LastSelectionState, + null, + ); + + // The selection should match the saved state + expect(currentSelection).toEqual({ + projectDir: savedSelection.projectDir, + deploymentName: savedSelection.deploymentName, + deploymentPath: savedSelection.deploymentPath, + }); + }); + + // Regression test for https://github.com/posit-dev/publisher/issues/2459 + // Verifies that after initialization with a saved selection, the content record + // and configuration can be fetched correctly. This tests the full initialization + // flow that was broken in the original bug. + test("saved selection enables fetching content record and config on init", async () => { + const savedSelection: DeploymentSelectorState = + selectionStateFactory.build(); + + // Setup content record that matches the saved selection + const contentRecord: PreContentRecord = preContentRecordFactory.build({ + deploymentPath: savedSelection.deploymentPath, + deploymentName: savedSelection.deploymentName, + projectDir: savedSelection.projectDir, + }); + mockClient.contentRecords.get.mockResolvedValue({ + data: contentRecord, + }); + + // Setup configuration that matches the content record + const config = configurationFactory.build({ + configurationName: contentRecord.configurationName, + projectDir: contentRecord.projectDir, + }); + mockClient.configurations.get.mockResolvedValue({ + data: config, + }); + + // Initialize with a pre-existing selection (simulates VS Code reopen) + const { mockContext } = mkExtensionContextStateMock({ + [LocalState.LastSelectionState]: savedSelection, + }); + const publisherState = new PublisherState(mockContext); + + // Verify selection is available immediately + const selection = publisherState.getSelection(); + expect(selection).toBeDefined(); + expect(selection?.deploymentPath).toEqual(savedSelection.deploymentPath); + + // Fetch the content record for the saved selection + const fetchedContentRecord = + await publisherState.getSelectedContentRecord(); + expect(mockClient.contentRecords.get).toHaveBeenCalledWith( + savedSelection.deploymentName, + savedSelection.projectDir, + ); + expect(fetchedContentRecord).toEqual(contentRecord); + + // Fetch the configuration for the saved selection + const fetchedConfig = await publisherState.getSelectedConfiguration(); + expect(mockClient.configurations.get).toHaveBeenCalledWith( + contentRecord.configurationName, + contentRecord.projectDir, + ); + expect(fetchedConfig).toEqual(config); + }); + describe("getSelectedContentRecord", () => { test("finding records and cache handling", async () => { const initialState: DeploymentSelectorState =