Skip to content

Commit

Permalink
fix(connections): make sure that autoconnect info is provided in conn…
Browse files Browse the repository at this point in the history
…ection hooks COMPASS-8044 (#5978)

* fix(connections): make sure that autoconnect info is accounted for in the connections lists

* chore(e2e): update autoconnect test to check that connection succeeded
  • Loading branch information
gribnoysup authored Jun 27, 2024
1 parent f1cc072 commit fc705a0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function sortedAlphabetically(a: ConnectionInfo, b: ConnectionInfo): number {
export type ConnectionRepository = {
favoriteConnections: ConnectionInfo[];
nonFavoriteConnections: ConnectionInfo[];
autoConnectInfo?: ConnectionInfo;
saveConnection: (info: PartialConnectionInfo) => Promise<ConnectionInfo>;
deleteConnection: (info: ConnectionInfo) => Promise<void>;
getConnectionInfoById: (
Expand Down Expand Up @@ -189,6 +190,7 @@ export function useConnectionRepository(): ConnectionRepository {
getConnectionTitleById,
favoriteConnections,
nonFavoriteConnections,
autoConnectInfo,
saveConnection,
deleteConnection,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ export function useConnectionsWithStatus(): ConnectionInfoWithStatus[] {
// when this code is refactored to use the hadron plugin interface, storage
// should be handled through the plugin activation lifecycle
const connectionsManager = useConnectionsManagerContext();
const { favoriteConnections, nonFavoriteConnections } =
const { favoriteConnections, nonFavoriteConnections, autoConnectInfo } =
useConnectionRepository();
const allConnections = useMemo(() => {
return [...favoriteConnections, ...nonFavoriteConnections];
}, [favoriteConnections, nonFavoriteConnections]);
return favoriteConnections.concat(
nonFavoriteConnections,
autoConnectInfo ? autoConnectInfo : []
);
}, [favoriteConnections, nonFavoriteConnections, autoConnectInfo]);

const [connectionsWithStatus, setConnectionsWithStatus] = useState<
ConnectionInfoWithStatus[]
Expand Down Expand Up @@ -60,7 +63,7 @@ export function useConnectionsWithStatus(): ConnectionInfoWithStatus[] {

useEffect(() => {
updateListRef.current();
}, [favoriteConnections, nonFavoriteConnections]);
}, [favoriteConnections, nonFavoriteConnections, autoConnectInfo]);

useEffect(() => {
const updateOnStatusChange = () => {
Expand Down
22 changes: 20 additions & 2 deletions packages/compass-e2e-tests/tests/auto-connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import type { Compass } from '../helpers/compass';
import {
init,
cleanup,
Expand All @@ -13,6 +14,7 @@ import path from 'path';
import { promises as fs } from 'fs';

const connectionStringSuccess = 'mongodb://127.0.0.1:27091/test';
const connectionStringSuccessTitle = '127.0.0.1:27091';
const connectionStringUnreachable =
'mongodb://127.0.0.1:27091/test?tls=true&serverSelectionTimeoutMS=10';
const connectionStringInvalid = 'http://example.com';
Expand Down Expand Up @@ -81,13 +83,29 @@ describe('Automatically connecting from the command line', function () {
await fs.rmdir(tmpdir, { recursive: true });
});

async function waitForConnectionSuccessAndCheckConnection(
compass: Compass,
expectedTitle = connectionStringSuccessTitle
) {
await compass.browser.waitForConnectionResult('success');
const sidebarTitle = await compass.browser
.$(Selectors.SidebarTitle)
.getText();
expect(sidebarTitle).to.eq(expectedTitle);
const result = await compass.browser.shellEval(
'db.runCommand({ connectionStatus: 1 })',
true
);
expect(result).to.have.property('ok', 1);
}

it('works with a connection string on the command line', async function () {
const compass = await init(this.test?.fullTitle(), {
wrapBinary: positionalArgs([connectionStringSuccess]),
noWaitForConnectionScreen: true,
});
try {
await compass.browser.waitForConnectionResult('success');
await waitForConnectionSuccessAndCheckConnection(compass);
} finally {
await cleanup(compass);
}
Expand All @@ -105,7 +123,7 @@ describe('Automatically connecting from the command line', function () {
noWaitForConnectionScreen: true,
});
try {
await compass.browser.waitForConnectionResult('success');
await waitForConnectionSuccessAndCheckConnection(compass, 'Success');
} finally {
await cleanup(compass);
}
Expand Down

0 comments on commit fc705a0

Please sign in to comment.