mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
- persist drafts, panel views, and active sessions per terminal/workspace scope - restore scoped AI session selection on reconnect and cold mount - prefer unsent drafts over implicit history fallback - avoid redundant active session map rewrites during scoped cleanup Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
177 lines
4.9 KiB
TypeScript
177 lines
4.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import type {
|
|
AIPanelView,
|
|
AISession,
|
|
} from "../../infrastructure/ai/types.ts";
|
|
import {
|
|
applyHistorySessionSelection,
|
|
normalizePanelView,
|
|
resolveDisplayedPanelView,
|
|
resolveDisplayedSession,
|
|
shouldRetargetSessionForScope,
|
|
} from "./aiPanelViewState.ts";
|
|
|
|
function createSession(id: string): AISession {
|
|
return {
|
|
id,
|
|
title: `Session ${id}`,
|
|
messages: [],
|
|
createdAt: 1,
|
|
updatedAt: 1,
|
|
agentId: "catty",
|
|
scope: {
|
|
type: "terminal",
|
|
targetId: "terminal-1",
|
|
},
|
|
};
|
|
}
|
|
|
|
test("draft view never falls back to most recent history", () => {
|
|
const panelView: AIPanelView = { mode: "draft" };
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.equal(resolveDisplayedSession(panelView, sessions), null);
|
|
});
|
|
|
|
test("session view returns the selected session", () => {
|
|
const selectedSession = createSession("session-2");
|
|
const panelView: AIPanelView = { mode: "session", sessionId: selectedSession.id };
|
|
const sessions = [createSession("session-1"), selectedSession];
|
|
|
|
assert.equal(resolveDisplayedSession(panelView, sessions), selectedSession);
|
|
});
|
|
|
|
test("missing session target resolves to null instead of history fallback", () => {
|
|
const panelView: AIPanelView = { mode: "session", sessionId: "missing-session" };
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.equal(resolveDisplayedSession(panelView, sessions), null);
|
|
});
|
|
|
|
test("missing session target normalizes back to draft view", () => {
|
|
const panelView: AIPanelView = { mode: "session", sessionId: "missing-session" };
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.deepEqual(normalizePanelView(panelView, sessions), { mode: "draft" });
|
|
});
|
|
|
|
test("missing explicit panel view resumes the most recent matching history when no draft exists", () => {
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.deepEqual(
|
|
resolveDisplayedPanelView(undefined, false, sessions),
|
|
{ mode: "session", sessionId: "session-2" },
|
|
);
|
|
});
|
|
|
|
test("missing explicit panel view restores the persisted active session instead of the newest", () => {
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.deepEqual(
|
|
resolveDisplayedPanelView(undefined, false, sessions, "session-1"),
|
|
{ mode: "session", sessionId: "session-1" },
|
|
);
|
|
});
|
|
|
|
test("persisted session id that no longer exists in history falls back to newest", () => {
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.deepEqual(
|
|
resolveDisplayedPanelView(undefined, false, sessions, "deleted-session"),
|
|
{ mode: "session", sessionId: "session-2" },
|
|
);
|
|
});
|
|
|
|
test("null persisted session id falls back to newest history entry", () => {
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.deepEqual(
|
|
resolveDisplayedPanelView(undefined, false, sessions, null),
|
|
{ mode: "session", sessionId: "session-2" },
|
|
);
|
|
});
|
|
|
|
test("missing explicit panel view prefers the draft when unsent input exists", () => {
|
|
const sessions = [createSession("session-2"), createSession("session-1")];
|
|
|
|
assert.deepEqual(
|
|
resolveDisplayedPanelView(undefined, true, sessions),
|
|
{ mode: "draft" },
|
|
);
|
|
});
|
|
|
|
test("draft state is used when there is no implicit history to resume", () => {
|
|
assert.deepEqual(
|
|
resolveDisplayedPanelView(undefined, true, []),
|
|
{ mode: "draft" },
|
|
);
|
|
});
|
|
|
|
test("restorable terminal history should retarget to the current scope", () => {
|
|
const session: AISession = {
|
|
...createSession("session-2"),
|
|
scope: {
|
|
type: "terminal",
|
|
targetId: "old-terminal",
|
|
hostIds: ["host-1"],
|
|
},
|
|
};
|
|
|
|
assert.equal(
|
|
shouldRetargetSessionForScope(
|
|
session,
|
|
"terminal",
|
|
"new-terminal",
|
|
["host-1"],
|
|
new Set<string>(),
|
|
),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("session owned by another active terminal should not retarget", () => {
|
|
const session: AISession = {
|
|
...createSession("session-2"),
|
|
scope: {
|
|
type: "terminal",
|
|
targetId: "other-active-terminal",
|
|
hostIds: ["host-1"],
|
|
},
|
|
};
|
|
|
|
assert.equal(
|
|
shouldRetargetSessionForScope(
|
|
session,
|
|
"terminal",
|
|
"new-terminal",
|
|
["host-1"],
|
|
new Set<string>(["other-active-terminal"]),
|
|
),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("history selection switches to the chosen session without touching draft state", () => {
|
|
const calls: string[] = [];
|
|
|
|
applyHistorySessionSelection("session-2", {
|
|
showSessionView: (sessionId) => {
|
|
calls.push(`view:${sessionId}`);
|
|
},
|
|
setActiveSessionId: (sessionId) => {
|
|
calls.push(`active:${sessionId}`);
|
|
},
|
|
closeHistory: () => {
|
|
calls.push("close-history");
|
|
},
|
|
});
|
|
|
|
assert.deepEqual(calls, [
|
|
"view:session-2",
|
|
"active:session-2",
|
|
"close-history",
|
|
]);
|
|
});
|