mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* feat(terminal): add Directory Picker context chip * fix(terminal): bind directory reads to the exact SSH session * fix(terminal): follow manually changed working directories * fix(terminal): isolate cwd updates and gate local platform support * docs(terminal): research Directory Picker cross-platform follow-up * fix(terminal): confirm OSC 7 navigation and reset picker loading * fix(terminal): preserve active shell OSC 7 during directory refresh * test(terminal): retain cross-session OSC 7 submission guard --------- Co-authored-by: Joseph Hau <hexart@gmail.com>
162 lines
8.6 KiB
TypeScript
162 lines
8.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { createRequire } from 'node:module';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { createTerminalDirectoryIO } from './terminalDirectory';
|
|
import { createTerminalDirectoryController } from '../../application/state/terminalDirectoryController';
|
|
import { terminalCwdStore } from '../../application/state/terminalCwdStore';
|
|
import {
|
|
observeTerminalDirectory, registerTerminalDirectory, refreshObservedTerminalDirectory, scheduleObservedTerminalDirectoryRefresh,
|
|
} from '../../application/state/terminalDirectoryRegistry';
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
test('directory picker changes a real POSIX PTY cwd with quoted names and directory symlinks', {
|
|
skip: process.platform === 'win32', timeout: 20000,
|
|
}, async t => {
|
|
const tempDirBridge = require('../../electron/bridges/tempDirBridge.cjs');
|
|
const { readLocalSessionCwd } = require('../../electron/bridges/localSessionCwd.cjs');
|
|
const pty = require('node-pty') as typeof import('node-pty');
|
|
tempDirBridge.ensureTempDir();
|
|
const fixture = fs.mkdtempSync(tempDirBridge.getTempFilePath('directory-picker-'));
|
|
const name = "space ' $() `literal` 中文";
|
|
const target = path.join(fixture, name);
|
|
fs.mkdirSync(target);
|
|
fs.symlinkSync(target, path.join(fixture, 'link'));
|
|
let ready = false;
|
|
const shell = pty.spawn('/bin/bash', ['--noprofile', '--norc', '-i'], {
|
|
cwd: fixture, env: { ...process.env, PS1: '__DIRECTORY_READY__ ', PROMPT_COMMAND: '' },
|
|
cols: 100, rows: 24,
|
|
});
|
|
shell.onData(data => { if (data.includes('__DIRECTORY_READY__ ')) ready = true; });
|
|
const waitReady = async () => {
|
|
const deadline = Date.now() + 4000;
|
|
while (!ready && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 20));
|
|
assert.ok(ready, 'shell reached its prompt');
|
|
};
|
|
t.after(() => { shell.kill(); fs.rmSync(fixture, { recursive: true, force: true }); });
|
|
await waitReady();
|
|
let published = '';
|
|
const io = createTerminalDirectoryIO({
|
|
getSessionPwd: async () => readLocalSessionCwd({ pty: shell }),
|
|
realpathLocal: async value => fs.promises.realpath(value),
|
|
writeToSession: (id, data) => { assert.equal(id, 'real-shell'); ready = false; shell.write(data); },
|
|
} as unknown as NetcattyBridge, 'real-shell', { local: true, hostname: '', username: '' });
|
|
const controller = createTerminalDirectoryController({
|
|
sessionId: 'real-shell', io, isReady: () => ready, isCurrent: () => true,
|
|
publish: cwd => { published = cwd; },
|
|
});
|
|
t.after(() => controller.dispose());
|
|
await controller.change(target, new AbortController().signal, () => {});
|
|
assert.equal(await io.cwd(), fs.realpathSync(target));
|
|
assert.equal(published, fs.realpathSync(target));
|
|
await waitReady();
|
|
await controller.change(path.join(fixture, 'link'), new AbortController().signal, () => {});
|
|
assert.equal(await io.cwd(), fs.realpathSync(target));
|
|
});
|
|
|
|
test('a real nested Bash OSC 7 cwd survives outer PTY probes after output settles', {
|
|
skip: process.platform !== 'linux', timeout: 20000,
|
|
}, async t => {
|
|
const tempDirBridge = require('../../electron/bridges/tempDirBridge.cjs');
|
|
const { readLocalSessionCwd } = require('../../electron/bridges/localSessionCwd.cjs');
|
|
const pty = require('node-pty') as typeof import('node-pty');
|
|
tempDirBridge.ensureTempDir();
|
|
const fixture = fs.mkdtempSync(tempDirBridge.getTempFilePath('nested-directory-shell-'));
|
|
const id = 'nested-directory-shell';
|
|
const shell = pty.spawn('/bin/bash', ['--noprofile', '--norc', '-i'], {
|
|
cwd: fixture, env: { ...process.env, PS1: '__OUTER_READY__ ', PROMPT_COMMAND: '' }, cols: 100, rows: 24,
|
|
});
|
|
let output = '';
|
|
let reports = 0;
|
|
const published: string[] = [];
|
|
const subscription = shell.onData(data => {
|
|
output += data;
|
|
scheduleObservedTerminalDirectoryRefresh(id);
|
|
// Parse actual bytes from the child shell, including split OSC chunks.
|
|
const paths = output.split('\x1b]7;file://localhost').slice(1)
|
|
.filter(chunk => chunk.includes('\x07')).map(chunk => chunk.split('\x07')[0]);
|
|
if (paths.length > reports) {
|
|
reports = paths.length;
|
|
terminalCwdStore.setCwd(id, paths.at(-1)!, 'osc7');
|
|
}
|
|
});
|
|
const controller = createTerminalDirectoryController({
|
|
sessionId: id, isCurrent: () => true, isReady: () => true,
|
|
io: { cwd: async () => {
|
|
const result = await readLocalSessionCwd({ pty: shell });
|
|
assert.ok(result.success); return result.cwd;
|
|
}, list: async () => ({ entries: [], home: null }), realpath: async value => value, write() {} },
|
|
publish: cwd => { published.push(cwd); terminalCwdStore.setCwd(id, cwd, 'backend-strict'); },
|
|
});
|
|
const unregister = registerTerminalDirectory(id, controller);
|
|
const stopObserving = observeTerminalDirectory(id);
|
|
t.after(() => { stopObserving(); unregister(); subscription.dispose(); shell.kill(); fs.rmSync(fixture, { recursive: true, force: true }); });
|
|
const waitFor = async (condition: () => boolean) => {
|
|
const deadline = Date.now() + 5000;
|
|
while (!condition() && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 25));
|
|
assert.ok(condition(), 'nested shell reached the expected prompt/cwd');
|
|
};
|
|
await waitFor(() => output.includes('__OUTER_READY__ '));
|
|
shell.write('PS1="__CHILD_READY__ " PROMPT_COMMAND=\'printf "\\033]7;file://localhost%s\\007" "$PWD"\' bash --noprofile --norc -i\r');
|
|
await waitFor(() => reports > 0);
|
|
shell.write('cd /\r');
|
|
await waitFor(() => terminalCwdStore.getCwd(id) === '/');
|
|
// Demonstrate the original lookup really observes the outer shell, then
|
|
// let the real 150 ms debounce expire before checking the picker/tracker.
|
|
assert.equal((await readLocalSessionCwd({ pty: shell })).cwd, fs.realpathSync(fixture));
|
|
await new Promise(resolve => setTimeout(resolve, 350));
|
|
assert.equal(terminalCwdStore.getCwd(id), '/');
|
|
refreshObservedTerminalDirectory(id);
|
|
assert.equal(await controller.refresh(), '/');
|
|
assert.equal(terminalCwdStore.getSource(id), 'osc7');
|
|
assert.ok(!published.includes(fs.realpathSync(fixture)), 'outer cwd never overwrote the child report');
|
|
});
|
|
|
|
test('manual cd and delayed shell functions update the observed cwd without shell integration', {
|
|
skip: process.platform === 'win32', timeout: 20000,
|
|
}, async t => {
|
|
const tempDirBridge = require('../../electron/bridges/tempDirBridge.cjs');
|
|
const { readLocalSessionCwd } = require('../../electron/bridges/localSessionCwd.cjs');
|
|
const pty = require('node-pty') as typeof import('node-pty');
|
|
tempDirBridge.ensureTempDir();
|
|
const fixture = fs.mkdtempSync(tempDirBridge.getTempFilePath('manual-directory-cd-'));
|
|
const first = path.join(fixture, 'first'); const second = path.join(fixture, 'second');
|
|
fs.mkdirSync(first); fs.mkdirSync(second);
|
|
const id = 'manual-cd-shell';
|
|
// An intentionally non-standard PS1: no trusted prompt/completion callback
|
|
// or OSC directory reporting is available to drive chip updates.
|
|
const shell = pty.spawn('/bin/bash', ['--noprofile', '--norc', '-i'], {
|
|
cwd: fixture, env: { ...process.env, PS1: 'custom input: ', PROMPT_COMMAND: '' }, cols: 100, rows: 24,
|
|
});
|
|
let output = ''; let published = '';
|
|
const subscription = shell.onData(data => {
|
|
output += data;
|
|
scheduleObservedTerminalDirectoryRefresh(id);
|
|
});
|
|
const controller = createTerminalDirectoryController({
|
|
sessionId: id, isCurrent: () => true, isReady: () => false,
|
|
io: { cwd: async () => {
|
|
const result = await readLocalSessionCwd({ pty: shell });
|
|
assert.ok(result.success); return result.cwd;
|
|
}, list: async () => ({ entries: [], home: null }), realpath: async value => value,
|
|
write() { assert.fail('manual navigation must not use the picker write action'); } },
|
|
publish: cwd => { published = cwd; },
|
|
});
|
|
const unregister = registerTerminalDirectory(id, controller);
|
|
const stopObserving = observeTerminalDirectory(id);
|
|
t.after(() => { stopObserving(); unregister(); subscription.dispose(); shell.kill(); fs.rmSync(fixture, { recursive: true, force: true }); });
|
|
const waitFor = async (condition: () => boolean) => {
|
|
const deadline = Date.now() + 5000;
|
|
while (!condition() && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 25));
|
|
assert.ok(condition(), 'cwd followed actual shell output');
|
|
};
|
|
await waitFor(() => output.includes('custom input: '));
|
|
shell.write(`cd -- '${first}'\r`);
|
|
await waitFor(() => published === fs.realpathSync(first));
|
|
// The directory changes well after a fixed post-Enter timer would fire.
|
|
shell.write(`go_later() { sleep 0.6; cd -- '${second}'; }; go_later\r`);
|
|
await waitFor(() => published === fs.realpathSync(second));
|
|
});
|