mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 15:41:17 +00:00
* feat(system-manager): add container runtime switcher and kubectl pods Keep the existing Docker tab and select docker, podman, nerdctl, or Kubernetes from runtimes detected on the host. Kubernetes replaces the switcher with context and namespace selectors in the same panel. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): address container runtime review findings Move the runtime adapter into the domain layer, keep a context namespace when namespace listing is forbidden, and emit PowerShell or cmd startup commands for Kubernetes shell, logs, and describe. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): cover Windows container popups and stale namespaces Run container shell and logs through the host shell's inspect wrapper on PowerShell and cmd, and reset the namespace when a remembered Kubernetes context is no longer present. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): parse kubectl JSON after banners and scope inspect cache Skip bracketed banners before kubectl JSON, and include the container runtime in image and container inspect cache keys. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): parse Podman image lists emitted as JSON arrays Podman applies {{json .}} to the whole image slice. Accept that array, including Names and byte sizes, and keep Docker's JSON lines. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): parse Podman stats arrays Accept Podman's stats JSON array and its id/cpu_percent fields, and keep Docker's one-stat-per-line output. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): list every Podman image tag Expand each name in a Podman image's Names array into its own row so extra tags stay visible and searchable. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): quote kubectl list and delete for the session shell Namespace, pod, and delete commands follow PowerShell or cmd quoting when the session shell is not POSIX, so apostrophes in a context name survive local PowerShell. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): drop renamed runtime launch commands from shell history Container popups now probe with _nc_rt_err and also cover podman, nerdctl, and kubectl. History sanitizers still required _nc_docker_err, so those launches stayed in persisted history. Confirm-dialog resets also follow the selected runtime. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): sum Podman interface stats and quote kubectl for the executor Podman {{json .}} stats keep per-interface counters on Network, so the details panel now sums RxBytes and TxBytes. Background kubectl runs in powershell.exe on local Windows even when the terminal is cmd, so those commands are quoted for that executor instead of the interactive shell. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): remove a selected image tag instead of the shared id Podman rows that share one image id were all deleted with rmi <id>, which either fails while other tags remain or drops every name. Tagged rows now run rmi repository:tag; dangling images still use the id. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): reject cmd kubectl arguments that contain percent signs Interactive cmd.exe expands %VAR% inside double quotes, so a context such as prod-%USERNAME% would be rewritten before kubectl sees it. Cmd popups now refuse that argument. PowerShell quoting still preserves the percent. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * refactor(system-manager): move runtime selection into an application hook Persisted source, context, and namespace state plus the kubectl discovery effects now live in useContainerRuntimeSelection. DockerManagerTab only renders the switcher and panels. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): offer kubectl shell only for running containers Succeeded and failed pods have no running container, and a completed init container cannot accept exec. Shell targets now come from container statuses that are running; logs still include init containers. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): keep Windows popup commands out of shell history PowerShell and cmd container popups inspect then run, and kubectl popups are typed directly. History filtering only matched the POSIX sh -c wrapper, so those launches were stored as user commands. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): mark Windows popups instead of matching kubectl syntax Filtering kubectl --context logs and describe also dropped commands the user typed. Windows popups now carry __NC_POPUP__, and ordinary kubectl lines stay in history. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): reject cmd arguments that contain delayed-expansion marks cmd.exe with /V:ON expands !VAR! inside double quotes. Contexts such as prod!USER! are refused for cmd popups, same as percent signs. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): hide raw nerdctl and unreachable cluster errors Only offer nerdctl after a short info probe succeeds, or when the failure is a socket permission error that sudo can still recover. Map dead containerd and unreachable kube API errors to short localized messages and keep the CLI text in the log. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): show terminating pods and ephemeral containers A deletion timestamp is Terminating even while the phase is still Running. Ephemeral containers are included in the logs list, and a running one is a shell target. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): keep the runtime select on Kubernetes and avoid covering it Kubernetes uses the same compact source menu as Docker, with context and namespace beside it. Shell popups open in the free part of the window so they no longer sit on top of the pod badges. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): restore the saved engine when Kubernetes disappears A host that was on Podman before Kubernetes keeps Podman if kubectl is gone, instead of falling back to Docker and overwriting that choice. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): avoid the visible system panel when others are parked A hidden parking-host panel can be the first system-manager match and has a zero-size rect. Scan every match and pass the first panel that actually has size so the shell popup still sits beside the open panel. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): keep a runtime choice across probe gaps and show podman ports A failed nerdctl or kubectl probe was saved as the fallback engine, so the previous choice never returned. Reconciliation now restores the stored source when it is detected again, and automatic fallbacks are not written back. Podman ps port mappings and exposed ports are formatted into the same display string the container detail strip already shows. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): show a pod's Evicted reason Pod status.reason is the eviction signal, while the phase stays Failed and the container often reports Error. Use that reason after a container waiting state and before the generic phase so the badge stays Evicted. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): keep an avoided shell popup inside the work area When the source window sits partly off-screen, clamping a slot back onto the display could cover the system panel, and the fallback then used the raw slot origin. Place the popup in the on-screen part of the slot instead. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix(system-manager): left-align the runtime selector row The Kubernetes context, namespace, and runtime selects sat on the right of the header, so they did not line up with the search field. Keep the same padding, let the context select fill the remaining width, and leave the runtime and namespace selects at their natural width. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
306 lines
11 KiB
TypeScript
306 lines
11 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import {
|
|
parseBashHistory,
|
|
parseZshHistory,
|
|
parseFishHistory,
|
|
parseShellHistory,
|
|
mergeRemoteHistory,
|
|
isNetcattyAiHistoryCommand,
|
|
isNetcattyManagedStartupHistoryCommand,
|
|
} from './remoteHistory.ts';
|
|
import { buildDockerExecShellCommand, buildDockerLogsCommand } from './systemManager/dockerShell.ts';
|
|
import {
|
|
buildContainerExecShellCommand,
|
|
buildContainerLogsCommand,
|
|
buildKubectlDescribeCommand,
|
|
buildKubectlExecShellCommand,
|
|
buildKubectlLogsCommand,
|
|
} from './systemManager/containerRuntime.ts';
|
|
import { buildTmuxAttachCommand } from './systemManager/tmuxShell.ts';
|
|
|
|
test('parseBashHistory: plain lines', () => {
|
|
const out = parseBashHistory(['ls -la', 'cd /tmp', 'echo hi'].join('\n'));
|
|
assert.equal(out.length, 3);
|
|
assert.equal(out[0].command, 'ls -la');
|
|
assert.equal(out[0].source, 'bash');
|
|
assert.equal(out[0].timestamp, undefined);
|
|
});
|
|
|
|
test('parseBashHistory: HISTTIMEFORMAT timestamp lines', () => {
|
|
const text = ['#1700000000', 'ls -la', '#1700000100', 'pwd'].join('\n');
|
|
const out = parseBashHistory(text);
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].command, 'ls -la');
|
|
assert.equal(out[0].timestamp, 1700000000000);
|
|
assert.equal(out[1].command, 'pwd');
|
|
assert.equal(out[1].timestamp, 1700000100000);
|
|
});
|
|
|
|
test('parseBashHistory: skips blank lines and trims', () => {
|
|
const out = parseBashHistory('\n ls \n\necho hi\n');
|
|
assert.deepEqual(
|
|
out.map((e) => e.command),
|
|
['ls', 'echo hi'],
|
|
);
|
|
});
|
|
|
|
test('parseBashHistory: groups a multi-line command between timestamp markers', () => {
|
|
// Under HISTTIMEFORMAT + lithist, a multi-line command is stored with
|
|
// embedded newlines; the `#epoch` markers delimit one command from the next.
|
|
const text = [
|
|
'#1700000000',
|
|
'for i in 1 2 3',
|
|
'do',
|
|
' echo $i',
|
|
'done',
|
|
'#1700000100',
|
|
'pwd',
|
|
].join('\n');
|
|
const out = parseBashHistory(text);
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].command, 'for i in 1 2 3\ndo\n echo $i\ndone');
|
|
assert.equal(out[0].timestamp, 1700000000000);
|
|
assert.equal(out[1].command, 'pwd');
|
|
assert.equal(out[1].timestamp, 1700000100000);
|
|
});
|
|
|
|
test('parseZshHistory: plain lines', () => {
|
|
const out = parseZshHistory(['ls', 'cd /tmp'].join('\n'));
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].source, 'zsh');
|
|
assert.equal(out[0].timestamp, undefined);
|
|
});
|
|
|
|
test('parseZshHistory: EXTENDED_HISTORY format', () => {
|
|
const text = [': 1700000000:0;ls -la', ': 1700000100:0;pwd'].join('\n');
|
|
const out = parseZshHistory(text);
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].command, 'ls -la');
|
|
assert.equal(out[0].timestamp, 1700000000000);
|
|
assert.equal(out[1].command, 'pwd');
|
|
});
|
|
|
|
test('parseZshHistory: rejoins backslash line-continuations into one command', () => {
|
|
// zsh escapes each embedded newline with a trailing backslash. The three
|
|
// physical lines below are a single command; `pwd` is a separate record.
|
|
const text = [
|
|
': 1700000000:0;echo a\\',
|
|
'echo b\\',
|
|
'echo c',
|
|
': 1700000100:0;pwd',
|
|
].join('\n');
|
|
const out = parseZshHistory(text);
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].command, 'echo a\necho b\necho c');
|
|
assert.equal(out[0].timestamp, 1700000000000);
|
|
assert.equal(out[1].command, 'pwd');
|
|
});
|
|
|
|
test('parseFishHistory: cmd + when records', () => {
|
|
const text = [
|
|
'- cmd: ls -la',
|
|
' when: 1700000000',
|
|
'- cmd: pwd',
|
|
' when: 1700000100',
|
|
].join('\n');
|
|
const out = parseFishHistory(text);
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].command, 'ls -la');
|
|
assert.equal(out[0].source, 'fish');
|
|
assert.equal(out[0].timestamp, 1700000000000);
|
|
assert.equal(out[1].command, 'pwd');
|
|
assert.equal(out[1].timestamp, 1700000100000);
|
|
});
|
|
|
|
test('parseFishHistory: unescapes \\n and \\\\, ignores paths block', () => {
|
|
const text = [
|
|
'- cmd: echo foo\\nbar',
|
|
' when: 1700000000',
|
|
' paths:',
|
|
' - /tmp',
|
|
'- cmd: grep \\\\d file',
|
|
' when: 1700000100',
|
|
].join('\n');
|
|
const out = parseFishHistory(text);
|
|
assert.equal(out.length, 2);
|
|
assert.equal(out[0].command, 'echo foo\nbar');
|
|
assert.equal(out[1].command, 'grep \\d file');
|
|
});
|
|
|
|
test('parseFishHistory: tolerates a tail-truncated leading remnant', () => {
|
|
// `tail` may cut mid-record: leading when/paths lines with no cmd are ignored
|
|
const text = [
|
|
' when: 1699999999',
|
|
' paths:',
|
|
' - /x',
|
|
'- cmd: whoami',
|
|
' when: 1700000200',
|
|
].join('\n');
|
|
const out = parseFishHistory(text);
|
|
assert.equal(out.length, 1);
|
|
assert.equal(out[0].command, 'whoami');
|
|
assert.equal(out[0].timestamp, 1700000200000);
|
|
});
|
|
|
|
test('parseShellHistory: dispatches by source', () => {
|
|
assert.equal(parseShellHistory('bash', 'ls')[0].source, 'bash');
|
|
assert.equal(parseShellHistory('zsh', 'ls')[0].source, 'zsh');
|
|
assert.equal(parseShellHistory('fish', '- cmd: ls')[0].source, 'fish');
|
|
});
|
|
|
|
test('mergeRemoteHistory: dedupes keeping most recent occurrence', () => {
|
|
const a = parseBashHistory(['ls', 'pwd', 'ls'].join('\n'));
|
|
const b = parseZshHistory([': 1700000000:0;ls', ': 1700000100:0;whoami'].join('\n'));
|
|
const merged = mergeRemoteHistory([a, b]);
|
|
const commands = merged.map((e) => e.command);
|
|
// Newest-first, unique
|
|
assert.deepEqual(commands, ['whoami', 'ls', 'pwd']);
|
|
});
|
|
|
|
test('mergeRemoteHistory: caps to max', () => {
|
|
const entries = Array.from({ length: 50 }, (_, i) => `cmd-${i}`).join('\n');
|
|
const merged = mergeRemoteHistory([parseBashHistory(entries)], 10);
|
|
assert.equal(merged.length, 10);
|
|
// Newest-first means cmd-49 comes first
|
|
assert.equal(merged[0].command, 'cmd-49');
|
|
});
|
|
|
|
test('mergeRemoteHistory: orders by real timestamp, not concatenation order', () => {
|
|
// The zsh command (ts 200) is newer than the fish command (ts 100), even
|
|
// though fish is concatenated last. Newest-first must rank zsh first.
|
|
const zsh = parseZshHistory(': 1700000200:0;zsh-newer');
|
|
const fish = parseFishHistory(['- cmd: fish-older', ' when: 1700000100'].join('\n'));
|
|
const merged = mergeRemoteHistory([zsh, fish]);
|
|
assert.deepEqual(
|
|
merged.map((e) => e.command),
|
|
['zsh-newer', 'fish-older'],
|
|
);
|
|
});
|
|
|
|
test('mergeRemoteHistory: timestamped entries rank above untimestamped ones', () => {
|
|
const bash = parseBashHistory(['plain-a', 'plain-b'].join('\n')); // no timestamps
|
|
const zsh = parseZshHistory(': 1700000000:0;timed'); // carries a timestamp
|
|
const merged = mergeRemoteHistory([bash, zsh]);
|
|
// The timed entry wins; untimestamped entries keep file order (newest last).
|
|
assert.deepEqual(
|
|
merged.map((e) => e.command),
|
|
['timed', 'plain-b', 'plain-a'],
|
|
);
|
|
});
|
|
|
|
test('isNetcattyAiHistoryCommand: detects AI PTY marker lines', () => {
|
|
assert.equal(
|
|
isNetcattyAiHistoryCommand('__NCMCP_abc123=0; ls -la'),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
isNetcattyAiHistoryCommand('/opt/frp/frps.toml__NCMCP_mp56jbh6_3e30833'),
|
|
true,
|
|
);
|
|
assert.equal(isNetcattyAiHistoryCommand('ls -la'), false);
|
|
assert.equal(isNetcattyAiHistoryCommand('grep NCMCP log.txt'), false);
|
|
});
|
|
|
|
test('isNetcattyManagedStartupHistoryCommand: detects Docker and tmux terminal launch commands', () => {
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildDockerExecShellCommand('587abcdef123')), true);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildDockerLogsCommand('587abcdef123')), true);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildContainerLogsCommand('podman', '587abcdef123')), true);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand(buildKubectlLogsCommand('kind-dev', 'default', 'api-0')),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand(
|
|
"printf '\\033[H\\033[2J\\033[3J'; _nc_docker_err=$(docker inspect 587abcdef123 2>&1 >/dev/null);",
|
|
),
|
|
true,
|
|
);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildTmuxAttachCommand('my-session')), true);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildTmuxAttachCommand('my-session', 2)), true);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildContainerLogsCommand('podman', 'abc123', 'powershell')), true);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand(buildContainerExecShellCommand('nerdctl', 'abc123', 'cmd')), true);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand(buildKubectlLogsCommand('kind-kind', 'default', 'api', undefined, 'powershell')),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand(buildKubectlDescribeCommand('kind-kind', 'default', 'api', 'cmd')),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand(buildKubectlExecShellCommand('kind-kind', 'default', 'api', 'app', 'powershell')),
|
|
true,
|
|
);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('podman logs -f abc123'), false);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('kubectl logs api'), false);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('kubectl describe pod api'), false);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand('kubectl --context prod -n default logs -f --tail 200 api'),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
isNetcattyManagedStartupHistoryCommand('kubectl --context prod describe pod api'),
|
|
false,
|
|
);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('docker ps -a'), false);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('docker logs -f 587abcdef123'), false);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('docker exec -it 587abcdef123 bash'), false);
|
|
assert.equal(isNetcattyManagedStartupHistoryCommand('tmux attach -t my-session'), false);
|
|
});
|
|
|
|
test('mergeRemoteHistory: drops Netcatty AI PTY history lines', () => {
|
|
const lists = [
|
|
parseBashHistory(
|
|
['ls -la', '__NCMCP_abc=0; pwd', 'git status'].join('\n'),
|
|
),
|
|
];
|
|
const merged = mergeRemoteHistory(lists);
|
|
assert.deepEqual(
|
|
merged.map((e) => e.command),
|
|
['git status', 'ls -la'],
|
|
);
|
|
});
|
|
|
|
test('mergeRemoteHistory: drops Netcatty managed Docker and tmux startup lines', () => {
|
|
const lists = [
|
|
parseBashHistory(
|
|
[
|
|
'docker ps -a',
|
|
buildDockerLogsCommand('587abcdef123'),
|
|
buildTmuxAttachCommand('my-session'),
|
|
'history',
|
|
].join('\n'),
|
|
),
|
|
];
|
|
const merged = mergeRemoteHistory(lists);
|
|
assert.deepEqual(
|
|
merged.map((e) => e.command),
|
|
['history', 'docker ps -a'],
|
|
);
|
|
});
|
|
|
|
test('mergeRemoteHistory: drops Netcatty managed startup lines from zsh and fish history', () => {
|
|
const zsh = parseZshHistory(
|
|
[
|
|
': 1700000000:0;git status',
|
|
`: 1700000100:0;${buildDockerExecShellCommand('587abcdef123')}`,
|
|
].join('\n'),
|
|
);
|
|
const fish = parseFishHistory(
|
|
[
|
|
'- cmd: docker ps -a',
|
|
' when: 1700000200',
|
|
`- cmd: ${buildTmuxAttachCommand('my-session')}`,
|
|
' when: 1700000300',
|
|
].join('\n'),
|
|
);
|
|
|
|
const merged = mergeRemoteHistory([zsh, fish]);
|
|
assert.deepEqual(
|
|
merged.map((e) => e.command),
|
|
['docker ps -a', 'git status'],
|
|
);
|
|
});
|