Skip to content

Refactor Cosmos DB Shell integration: split modules, centralize constants, add tests#3077

Open
mkrueger wants to merge 4 commits into
mainfrom
dev/mkrueger/cosmos_shell_cleanup
Open

Refactor Cosmos DB Shell integration: split modules, centralize constants, add tests#3077
mkrueger wants to merge 4 commits into
mainfrom
dev/mkrueger/cosmos_shell_cleanup

Conversation

@mkrueger

Copy link
Copy Markdown
Member

Summary

Cleanup pass on the Cosmos DB Shell integration addressing three of the items called out in the code-quality review:

  1. Split the monolithic CosmosDBShellExtension.ts into focused sibling modules under src/cosmosDBShell/.
  2. Centralized duplicated magic strings and numbers into a single constants.ts (terminal name, MCP port + server name, setting keys, command id) and unified terminal.name lookups onto terminal.creationOptions.name.
  3. Added unit-test coverage for the previously-untested pure logic (72 new tests, lifting the cosmos-shell suite from 4 → 76 tests).

No behavior, telemetry, or user-facing string changes.

Commits

  • b1107e6c — Split CosmosDBShellExtension.ts into focused modules.
  • 2265cc47 — Centralize Cosmos DB Shell magic strings and constants.
  • b5f0cb29 — Add unit tests for Cosmos DB Shell core modules.

New test files

  • src/cosmosDBShell/shellCommand.test.ts (12) — isCosmosDBShellPathFound() quote stripping + fs failure paths, quoteArg() escaping.
  • src/cosmosDBShell/nodeCredentials.test.ts (13) — getNodeAuthKind() priority order, credential getters.
  • src/cosmosDBShell/terminalReuse.test.ts (21) — canReuseTerminalForNode() per-auth-mode rules, buildInteractiveConnectCommand() flag composition + quoting.
  • src/cosmosDBShell/shellSupportCache.test.ts (10) — cache hit/miss/invalidate, per-command keying, SemVer parsing incl. pre-release tokens, ANSI-disabled non-zero-exit fallback.
  • src/cosmosDBShell/install/dotNetSdk.test.ts (16) — compareDotNetVersions(), list-sdks parsing, hasRequiredDotNetSdk() floor checks.

Validation

  • npm run vitest — 76/76 cosmos-shell tests pass.
  • npm run prettier-fix — clean.
  • npm run lint — oxlint 0 errors, eslint 0 errors.

mkrueger added 3 commits May 28, 2026 12:19
Extract subsystems from the 1295-line CosmosDBShellExtension.ts into

sibling modules so each file has a single responsibility:

- shellCommand.ts: command resolution, path validation, watchForEarlyExit, quoteArg, COSMOS_DB_SHELL_TERMINAL_NAME

- shellSupportCache.ts: cached '--version' probing (isCosmosDBShellInstalled, getDetectedCosmosDBShellVersion, invalidateCosmosDBShellSupportCache)

- nodeCredentials.ts: AuthKind, credential accessors, getNodeAuthKind, getCosmosDBShellToken

- terminalReuse.ts: ShellTerminalState, terminalStates map, canReuse/findReusable, buildInteractiveConnectCommand

- install/dotNetSdk.ts: SDK constants, compareDotNetVersions, hasRequiredDotNetSdk, tryInstallDotNetSdkViaExtension

- install/installPrompts.ts: install/repair prompts; takes a launchShell callback to avoid a circular import

- mcpProvider.ts: registerMcpServer, port probing, resolveMcpServer, config-change wiring

- languageServer.ts: registerCosmosDBShellLanguageServer

CosmosDBShellExtension.ts (314 lines) keeps the extension class, launchCosmosDBShell, connectCosmosDBShell, and re-exports the public surface consumed by extension.ts.

Carried in passing: void-prefixed two fire-and-forget setContext calls; replaced repeated 'Cosmos DB Shell' literal with COSMOS_DB_SHELL_TERMINAL_NAME; replaced repeated 6128 with DEFAULT_MCP_PORT; commented the useMcp = mcpEnabled && !foundTerminal rationale; removed a redundant duplicate authKind assignment in connectCosmosDBShell.

No behavior, telemetry, or user-facing string changes.
Extracts duplicated string and number literals (terminal name, MCP port/server name, setting keys, command id) into a single src/cosmosDBShell/constants.ts module and updates CosmosDBShellExtension, shellCommand, mcpProvider, and install/installPrompts to consume them.

Also unifies the three remaining terminal.name lookups in CosmosDBShellExtension onto terminal.creationOptions.name for consistency with mcpProvider.

No behavior, telemetry, or user-facing string changes.
Adds 72 new tests across 5 files covering the previously-untested pure logic:

- shellCommand: isCosmosDBShellPathFound() (quote stripping, fs failures, directory paths) and quoteArg() escaping.

- nodeCredentials: getNodeAuthKind() priority order (emulator > accountKey > entraId > managedIdentity > none) and the three credential getters.

- terminalReuse: buildTerminalStateForNode(), canReuseTerminalForNode() reuse rules per auth mode (endpoint/tenant matching for accountKey/entraId, always-true for emulator/managedIdentity/none), and buildInteractiveConnectCommand() flag composition.

- shellSupportCache: cache hit/miss/invalidate behavior, per-command cache keying, version parsing including pre-release tokens, and the ANSI-disabled non-zero exit fallback.

- install/dotNetSdk: compareDotNetVersions() (numeric components, missing parts, suffix handling), getInstalledDotNetSdkVersions() parsing, and hasRequiredDotNetSdk() against MIN_DOTNET_SDK_VERSION.

Tests stub the transitively-loaded modules (azureSessionHelper, vscode-azext-utils) that perform CJS require('vscode') calls bypassing the vitest alias.
@mkrueger mkrueger requested a review from a team as a code owner May 28, 2026 10:54
Comment thread src/cosmosDBShell/shellCommand.ts Fixed
@github-actions

github-actions Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

🎉 Build Summary

🔗 Source

📦 Package Information

🧪 Test Results

  • Unit Tests: ✅ success
  • Integration Tests: ✅ success

✅ Build Status

All checks completed successfully!

quoteArg() previously escaped embedded double quotes but not backslashes. An input
containing both (e.g. `foo\"bar`) would round-trip to `"foo\"bar"`, where the shell
parses the `\"` as an escaped quote and the literal `"` then terminates the argument
early.

Escape backslashes first, then quotes, and include backslash in the trigger character
class so values containing only backslashes (e.g. Windows paths) are wrapped + escaped
consistently.

Adds tests for backslash, backslash+quote, and Windows-path inputs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants