Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions e2e/helpers/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function launchApp(homeDir: string) {
);
const executablePath = requireFromApp('electron');

const app = await electron.launch({
const electronApp = await electron.launch({
executablePath,
args: [connectAppDir, '--insecure'],
env: {
Expand All @@ -51,12 +51,16 @@ export async function launchApp(homeDir: string) {
});

try {
const page = await app.firstWindow();
const page = await electronApp.firstWindow();
await page.waitForLoadState('domcontentloaded');

return { app, page, [Symbol.asyncDispose]: async () => app.close() };
return {
electronApp,
page,
[Symbol.asyncDispose]: async () => electronApp.close(),
};
} catch (err) {
await app.close();
await electronApp.close();
throw err;
}
}
Expand Down Expand Up @@ -104,7 +108,7 @@ export const test = base.extend<{
await login(launchedApp.page);
}
await use({
electronApp: launchedApp.app,
electronApp: launchedApp.electronApp,
page: launchedApp.page,
appConfigPath,
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/scripts/open-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ await login(launched.page);
info('Teleport Connect opened and authenticated');
info('close the app window or press Ctrl+C to exit');

await new Promise<void>(resolve => launched.app.once('close', resolve));
await new Promise<void>(resolve => launched.electronApp.once('close', resolve));
info('Teleport Connect closed');
55 changes: 55 additions & 0 deletions e2e/tests/connect/firstLaunch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
Comment thread
ravicious marked this conversation as resolved.
* Teleport
* Copyright (C) 2026 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';

import {
expect,
initializeDataDir,
launchApp,
test,
withDefaultAppConfig,
} from '@gravitational/e2e/helpers/connect';

test('first launch shows usage data dialog', async () => {
await using temp = await fs.mkdtempDisposable(
path.join(os.tmpdir(), 'connect-e2e-first-launch-')
);
// Set usageReporting.enabled to undefined so it's not stored in the config, which causes the
// usage data dialog to appear on launch.
await initializeDataDir(
temp.path,
withDefaultAppConfig({ 'usageReporting.enabled': undefined })
);

await using app = await launchApp(temp.path);
const { page } = app;

const usageDataDialog = page.getByText('Anonymous usage data');
await expect(usageDataDialog).toBeVisible();
await page.getByRole('button', { name: 'Decline', exact: true }).click();

// Assert the dialog is dismissed – without this, the "Connect a Cluster" check below would pass
// even if clicking Decline failed, since that screen is already rendered under the modal.
await expect(usageDataDialog).not.toBeVisible();

// After dismissing the dialog, the app should show the default "Connect a Cluster" screen.
await expect(page.getByText('Connect a Cluster')).toBeVisible();
Comment thread
ravicious marked this conversation as resolved.
});
258 changes: 258 additions & 0 deletions e2e/tests/connect/stateRestoration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/**
* Teleport
* Copyright (C) 2026 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';

import {
expect,
initializeDataDir,
launchApp,
login,
test,
withDefaultAppConfig,
} from '@gravitational/e2e/helpers/connect';

// These tests manage the app lifecycle manually (multiple launches/closes), so they do not use the
// `app` fixture.
test.describe('state restoration from disk', () => {
let tempPath: string;

test.beforeEach(async () => {
tempPath = await fs.mkdtemp(path.join(os.tmpdir(), 'connect-e2e-state-'));
await initializeDataDir(tempPath, withDefaultAppConfig({}));
});

test.afterEach(async () => {
await fs.rm(tempPath, { recursive: true, force: true });
});

test('relaunch restores tabs', async () => {
// Login and create extra tabs.
{
await using app = await launchApp(tempPath);
const { page } = app;
await login(page);

// Open a terminal tab so there are 2 tabs (cluster + terminal).
await page.getByTitle('Additional Actions').click();
await page.getByText('Open new terminal').click();
await expect(
page.getByRole('textbox', { name: 'Terminal input' })
).toBeVisible();
Comment thread
ravicious marked this conversation as resolved.
}

// Relaunch – the app should offer to restore the terminal tab.
{
await using app = await launchApp(tempPath);
const { page } = app;
await expect(page.getByText('Reopen previous session')).toBeVisible();
await page.getByRole('button', { name: 'Reopen' }).click();
await expect(page.getByText('Reopen previous session')).not.toBeVisible();

// Both tabs should be restored.
await expect(
page.locator('[role="tab"][data-doc-kind="doc.cluster"]')
).toBeVisible();
await expect(
page.locator('[role="tab"][data-doc-kind="doc.terminal_shell"]')
).toBeVisible();
Comment thread
ravicious marked this conversation as resolved.
}
});

test('missing app_state.json does not crash the app', async () => {
// Login to create state files on disk.
{
await using app = await launchApp(tempPath);
await login(app.page);
Comment thread
ravicious marked this conversation as resolved.
}

// Remove app_state.json (keep tsh dir) – the app should not crash.
const appStatePath = path.join(tempPath, 'userData', 'app_state.json');
await fs.rm(appStatePath);

{
await using app = await launchApp(tempPath);

// Without app_state.json, the app has no saved rootClusterUri, so no workspace is activated
// and the cluster connect panel prompts the user to pick one.
await expect(
app.page.getByText('Log in to a cluster to use Teleport Connect.')
).toBeVisible();
}
});

test('missing tsh home directory does not crash the app', async () => {
// Login to create state files on disk.
{
await using app = await launchApp(tempPath);
await login(app.page);
}

// Remove the tsh home directory (keep app_state.json) – the app should not crash.
const tshHomePath = path.join(tempPath, 'home', '.tsh');
await fs.rm(tshHomePath, { recursive: true });

{
await using app = await launchApp(tempPath);
const { page } = app;

// With no tsh dir, no cluster can be connected.
await expect(page.getByText('Connect a Cluster')).toBeVisible();
// app_state.json still references the old cluster, so setActiveWorkspace should show an error.
await expect(
page.getByText('Could not set cluster as active')
).toBeVisible();
}
});

test('logout clears previous tabs', async () => {
await using app = await launchApp(tempPath);
const { page } = app;
await login(page);

// Open a terminal tab.
await page.getByTitle('Additional Actions').click();
await page.getByText('Open new terminal').click();
await expect(
page.getByRole('textbox', { name: 'Terminal input' })
).toBeVisible();

// Logout.
await page.getByTitle(/Open Profiles/).click();
await page.getByTitle(/Log out/).click();
await expect(
page.getByText('Are you sure you want to log out?')
).toBeVisible();
await page.getByRole('button', { name: 'Log Out', exact: true }).click();
await expect(page.getByText('Connect a Cluster')).toBeVisible();

// Login to the same cluster again.
await login(page);

// No restore dialog should appear – logout clears previous tabs.
// Only the default cluster tab should be open.
const clusterTab = page.locator(
'[role="tab"][data-doc-kind="doc.cluster"]'
);
await expect(clusterTab).toHaveCount(1);
await expect(
page.locator('[role="tab"][data-doc-kind="doc.terminal_shell"]')
Comment thread
ravicious marked this conversation as resolved.
).toHaveCount(0);
await expect(page.getByText('Reopen previous session')).not.toBeVisible();
Comment thread
ravicious marked this conversation as resolved.
});

test('identical workspace shape does not trigger restore dialog', async () => {
// Login, then replace the default cluster tab with a new one (same shape).
{
await using app = await launchApp(tempPath);
const { page } = app;
await login(page);

const clusterTab = page.locator(
'[role="tab"][data-doc-kind="doc.cluster"]'
);
await expect(clusterTab).toBeVisible();

// Close the existing cluster tab.
await clusterTab.locator('.close').click();
// Open a new cluster tab via the "+" button.
await page.getByTitle(/New Tab/).click();
await expect(clusterTab).toBeVisible();
}

// Relaunch – no restore dialog since the workspace has the same shape.
{
await using app = await launchApp(tempPath);
const { page } = app;

const clusterTab = page.locator(
'[role="tab"][data-doc-kind="doc.cluster"]'
);
await expect(clusterTab).toHaveCount(1);
await expect(page.getByText('Reopen previous session')).not.toBeVisible();
}
});

test('window remembers size and position after restart', async () => {
// Launch the app and resize the window.
let targetBounds: { x: number; y: number; width: number; height: number };
{
await using app = await launchApp(tempPath);
const { page } = app;
await expect(page.getByText('Connect a Cluster')).toBeVisible();

// Pick bounds relative to the primary display's work area so the test works on any screen
// size and on multi-monitor setups where the primary display may be offset from (0, 0).
// WindowsManager.getWindowState restores saved bounds only when they fit entirely within
// a display.
const requestedBounds = await app.electronApp.evaluate(({ screen }) => {
const wa = screen.getPrimaryDisplay().workArea;
return {
x: wa.x + Math.floor(wa.width * 0.1),
y: wa.y + Math.floor(wa.height * 0.1),
width: Math.floor(wa.width * 0.6),
height: Math.floor(wa.height * 0.6),
Comment thread
ravicious marked this conversation as resolved.
};
});

// Capture the initial bounds before resizing so we can verify setBounds() had an effect.
const initialBounds = await app.electronApp.evaluate(
({ BrowserWindow }) => {
const win = BrowserWindow.getAllWindows()[0];
return win.getNormalBounds();
}
);

await app.electronApp.evaluate(({ BrowserWindow }, bounds) => {
const win = BrowserWindow.getAllWindows()[0];
win.setBounds(bounds);
}, requestedBounds);

// Read back the accepted bounds after the window manager has applied them. The WM may adjust
// the requested rectangle (e.g. on Wayland or tiling layouts), and the app persists
// getNormalBounds(), not the requested values.
targetBounds = await app.electronApp.evaluate(({ BrowserWindow }) => {
const win = BrowserWindow.getAllWindows()[0];
return win.getNormalBounds();
});
Comment thread
ravicious marked this conversation as resolved.

// Verify the window actually moved. On environments where setBounds() is a no-op (e.g.
// Wayland, tiling WMs), the relaunch assertion would pass trivially without this guard.
expect(targetBounds).not.toEqual(initialBounds);
}

// Relaunch – the window should restore to the same size & position.
{
await using app = await launchApp(tempPath);

// Wait for the app to fully initialize before reading bounds and closing, otherwise the
// app may close before the renderer acks the initial cluster store message, causing a
// "Failed to receive message acknowledgement from the renderer" error dialog.
await expect(app.page.getByText('Connect a Cluster')).toBeVisible();

const bounds = await app.electronApp.evaluate(({ BrowserWindow }) => {
const win = BrowserWindow.getAllWindows()[0];
return win.getNormalBounds();
});

expect(bounds).toEqual(targetBounds);
Comment thread
ravicious marked this conversation as resolved.
}
});
});
Loading
Loading