mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-17 21:10:48 +00:00
Phase 2 of #4098: every remaining positional fallback in the workspace AI scope is replaced with id-based resolution. - Workspace gains anchorChatId: pinned by the AI side panel whenever a chat becomes active in the workspace scope, persisted through the session restore snapshot, and validated lazily by consumers. - resolveDisplayedPanelView: the auto-resume fallback now resolves the anchored chat first, then the most recently updated chat (updatedAt), else draft; sessions[0] is gone. - retargetWorkspaceActiveChatAfterMemberLoss: when the focused member did not survive, the retarget lands on the most-recently-active survivor by chat recency, not the first positional member. - plugin keybinding context: focused member, then the terminal member owning the anchored chat, then the most-recently-active member; a single-member workspace uses its only member. - New domain/aiSessionRecency helper shared by all survivor rankings. - /rebind and session_recover semantics (#4019) are unchanged.
181 lines
4.9 KiB
TypeScript
181 lines
4.9 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import {
|
|
buildTerminalPluginContributionContext,
|
|
resolveActivePluginKeybindingContext,
|
|
} from './pluginContributionContexts.ts';
|
|
import type { TerminalSession, Workspace } from '../../types.ts';
|
|
|
|
const sessions: TerminalSession[] = [
|
|
{
|
|
id: 'session-1',
|
|
hostId: 'host-1',
|
|
hostLabel: 'Host 1',
|
|
username: 'root',
|
|
hostname: 'example.com',
|
|
status: 'connected',
|
|
protocol: 'ssh',
|
|
workspaceId: 'workspace-1',
|
|
},
|
|
{
|
|
id: 'session-2',
|
|
hostId: 'local-2',
|
|
hostLabel: 'Local',
|
|
username: '',
|
|
hostname: 'localhost',
|
|
status: 'disconnected',
|
|
protocol: 'local',
|
|
},
|
|
];
|
|
|
|
const workspaces: Workspace[] = [{
|
|
id: 'workspace-1',
|
|
title: 'Workspace',
|
|
root: { id: 'pane-1', type: 'pane', sessionId: 'session-1' },
|
|
}];
|
|
|
|
test('builds a resource-bearing terminal command context', () => {
|
|
assert.deepEqual(buildTerminalPluginContributionContext({
|
|
surface: 'terminal/context',
|
|
sessionId: 'session-1',
|
|
status: 'connected',
|
|
hostId: 'host-1',
|
|
hostProtocol: 'ssh',
|
|
workspaceId: 'workspace-1',
|
|
hasSelection: true,
|
|
alternateScreen: false,
|
|
reconnectable: false,
|
|
}), {
|
|
'netcatty.surface': 'terminal/context',
|
|
'terminal.sessionId': 'session-1',
|
|
'terminal.status': 'connected',
|
|
'host.id': 'host-1',
|
|
'host.protocol': 'ssh',
|
|
'workspace.id': 'workspace-1',
|
|
'terminal.hasSelection': true,
|
|
'terminal.alternateScreen': false,
|
|
'terminal.reconnectable': false,
|
|
});
|
|
});
|
|
|
|
test('resolves the focused workspace session for global keybindings', () => {
|
|
assert.deepEqual(resolveActivePluginKeybindingContext({
|
|
activeTabId: 'workspace-1',
|
|
sessions,
|
|
workspaces,
|
|
}), {
|
|
'netcatty.surface': 'keybinding',
|
|
'terminal.sessionId': 'session-1',
|
|
'terminal.status': 'connected',
|
|
'host.id': 'host-1',
|
|
'host.protocol': 'ssh',
|
|
'workspace.id': 'workspace-1',
|
|
'netcatty.activeTabId': 'workspace-1',
|
|
});
|
|
});
|
|
|
|
test('uses the anchor chat owner when focus is missing (#4098 phase 2)', () => {
|
|
const splitWorkspace: Workspace = {
|
|
id: 'workspace-1',
|
|
title: 'Workspace',
|
|
anchorChatId: 'chat-2',
|
|
root: {
|
|
id: 'split-1',
|
|
type: 'split',
|
|
direction: 'vertical',
|
|
children: [
|
|
{ id: 'pane-1', type: 'pane', sessionId: 'session-1' },
|
|
{ id: 'pane-2', type: 'pane', sessionId: 'session-3' },
|
|
],
|
|
sizes: [1, 1],
|
|
},
|
|
};
|
|
const chatOwnerSessions: TerminalSession[] = [
|
|
...sessions,
|
|
{
|
|
id: 'session-3',
|
|
hostId: 'host-3',
|
|
hostLabel: 'Host 3',
|
|
username: 'root',
|
|
hostname: 'example.org',
|
|
status: 'connected',
|
|
protocol: 'ssh',
|
|
workspaceId: 'workspace-1',
|
|
},
|
|
];
|
|
|
|
assert.equal(
|
|
resolveActivePluginKeybindingContext({
|
|
activeTabId: 'workspace-1',
|
|
sessions: chatOwnerSessions,
|
|
workspaces: [splitWorkspace],
|
|
aiSessions: [
|
|
{ id: 'chat-2', scope: { type: 'terminal', targetId: 'session-3' }, updatedAt: 2 },
|
|
{ id: 'chat-1', scope: { type: 'terminal', targetId: 'session-1' }, updatedAt: 1 },
|
|
],
|
|
})['terminal.sessionId'],
|
|
// anchorChatId's chat is terminal-scoped to session-3, which wins even
|
|
// though session-1's chat is older and session-1 is the first member.
|
|
'session-3',
|
|
);
|
|
});
|
|
|
|
test('falls back to the most-recently-active member when there is no anchor (#4098 phase 2)', () => {
|
|
const splitWorkspace: Workspace = {
|
|
id: 'workspace-1',
|
|
title: 'Workspace',
|
|
root: {
|
|
id: 'split-1',
|
|
type: 'split',
|
|
direction: 'vertical',
|
|
children: [
|
|
{ id: 'pane-1', type: 'pane', sessionId: 'session-1' },
|
|
{ id: 'pane-2', type: 'pane', sessionId: 'session-3' },
|
|
],
|
|
sizes: [1, 1],
|
|
},
|
|
};
|
|
const chatOwnerSessions: TerminalSession[] = [
|
|
...sessions,
|
|
{
|
|
id: 'session-3',
|
|
hostId: 'host-3',
|
|
hostLabel: 'Host 3',
|
|
username: 'root',
|
|
hostname: 'example.org',
|
|
status: 'connected',
|
|
protocol: 'ssh',
|
|
workspaceId: 'workspace-1',
|
|
},
|
|
];
|
|
|
|
assert.equal(
|
|
resolveActivePluginKeybindingContext({
|
|
activeTabId: 'workspace-1',
|
|
sessions: chatOwnerSessions,
|
|
workspaces: [splitWorkspace],
|
|
aiSessions: [
|
|
{ id: 'chat-1', scope: { type: 'terminal', targetId: 'session-1' }, updatedAt: 1 },
|
|
{ id: 'chat-2', scope: { type: 'terminal', targetId: 'session-3' }, updatedAt: 9 },
|
|
],
|
|
})['terminal.sessionId'],
|
|
'session-3',
|
|
);
|
|
});
|
|
|
|
test('resolves standalone sessions and fails closed for non-terminal tabs', () => {
|
|
assert.equal(
|
|
resolveActivePluginKeybindingContext({ activeTabId: 'session-2', sessions, workspaces })['terminal.sessionId'],
|
|
'session-2',
|
|
);
|
|
assert.deepEqual(resolveActivePluginKeybindingContext({
|
|
activeTabId: 'vault',
|
|
sessions,
|
|
workspaces,
|
|
}), {
|
|
'netcatty.surface': 'keybinding',
|
|
'netcatty.activeTabId': 'vault',
|
|
});
|
|
});
|