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
26 changes: 25 additions & 1 deletion scripts/e2e-terminal-stop.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
onRunStart,
onRunResult,
printInfo,
resetTerminalSession,
showInitialPrompt,
startRun,
stopRun,
Expand All @@ -20,13 +21,14 @@ function setupTerminalHarness({
onRun,
} = {}) {
const writes = [];
const clearCalls = [];
const runStateChanges = [];
const runPreparationChanges = [];
const stopCalls = [];
const runCalls = [];
const stdinMessages = [];
const fakeTerm = {
clear() {},
clear() { clearCalls.push(true); },
write(text) { writes.push(text); },
};

Expand All @@ -46,6 +48,7 @@ function setupTerminalHarness({

return {
writes,
clearCalls,
runStateChanges,
runPreparationChanges,
stopCalls,
Expand Down Expand Up @@ -87,6 +90,27 @@ test('e2e: initial terminal prompt is only written once', () => {
assert.equal(output.match(/browser\.cpp/g)?.length, 1);
});

test('e2e: resetting a terminal session clears output and redraws one prompt', () => {
const ctx = setupTerminalHarness();
showInitialPrompt();
printInfo('stale output');

resetTerminalSession({ name: 'project', entries: [] });

assert.equal(ctx.clearCalls.length, 1);
assert.equal(ctx.writes.at(-1).match(/browser\.cpp/g)?.length, 1);
assert.match(ctx.writes.at(-1), /browser\.cpp.*:~\$ /);
});

test('e2e: resetting before compiler readiness does not print a prompt', () => {
const ctx = setupTerminalHarness();

resetTerminalSession();

assert.equal(ctx.clearCalls.length, 1);
assert.equal(ctx.writes.length, 0);
});

test('e2e: clearing during startup does not reveal the initial prompt early', () => {
const ctx = setupTerminalHarness();

Expand Down
13 changes: 13 additions & 0 deletions src/ui/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,19 @@ export function clearTerminal() {
writePrompt();
}

/**
* Start a clean terminal session for a workspace transition.
* Unlike clearTerminal(), this also resets the workspace prompt context and
* input line before drawing at most one prompt.
*/
export function resetTerminalSession(workspace = null) {
inputBuffer = '';
historyIdx = -1;
setWorkspace(workspace);
term?.clear();
if (initialPromptShown) writePrompt();
}

/** Write the first shell prompt after compiler startup has reached a terminal state. */
export function showInitialPrompt() {
if (initialPromptShown) return;
Expand Down
5 changes: 2 additions & 3 deletions src/ui/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ function clearTransientProjectState() {
_runAfterSuccessfulCompile = false;
_runPreparationActive = false;
_lastRunBinaryBytes = null;
_terminalAPI.clearTerminal?.();
_editorAPI.clearDiagnostics?.();
}

Expand All @@ -608,6 +607,7 @@ export function restoreNoWorkspaceSource(source) {
closeAllTabs();
_fsAPI.newFile();
clearWorkspaceMode();
_terminalAPI.resetTerminalSession?.(null);
openTabForFile(UNSAVED_TAB_PATH, source);
markDirty(false);
}
Expand Down Expand Up @@ -1097,15 +1097,14 @@ function showOpenError(err) {
function setWorkspaceMode(workspace) {
_workspace = workspace;
_expandedWorkspaceDirectories.clear();
_terminalAPI.setWorkspace?.(workspace);
_terminalAPI.resetTerminalSession?.(workspace);
updateCompileButtons();
startWorkspaceSyncPolling();
}

function clearWorkspaceMode() {
_workspace = null;
_expandedWorkspaceDirectories.clear();
_terminalAPI.setWorkspace?.(null);
const tree = document.getElementById('file-tree');
if (tree) tree.innerHTML = '';
updateCompileButtons(false);
Expand Down
Loading