mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-24 17:32:18 +00:00
* feat(ai): global AI session manager with cross-scope rebind Scope-agnostic view over every AI chat regardless of panel scope (issue #4098 phases 4-5), hosted as a scoped/global toggle inside the existing session history drawer. - Global list: title, agent, resolved scope (workspace title / terminal label / global / dangling), state badge (active / idle / dissociated), message count, relative time; title-substring search, agent filter, newest-first ordering and a render batch guard. Titles only, never message bodies. - Per-row actions: open/switch (exact-scope chats only; other-scope rows offer rebind instead of faking activation), rebind to the current panel (issue #4019 path) or to any open workspace/terminal via an explicit target picker, confirm-gated per-record delete, and single-chat export (exportSessionsBackup now takes an optional session-id subset; import merges by id). - App-wide open terminals are threaded to every AI panel so the manager resolves scopes and rebind targets across tabs. - Deletion safety: no scopeKey is passed, stale pointers of any panel are repaired by the #4125 validation effect; deleting the last chat cannot resurrect on reboot (intentionally-deleted ids + migration marker). No cleanup/eviction semantics are touched (#4015 R2). * fix(ai): stop active turns before scope rebind Rebinding a chat while a turn streams into it corrupts streaming state (the stream targets the old scope while the active map/panel pointer move underneath it). Both rebind paths — the global session manager and the /rebind <n> panel command — now await stopStreamingForSession first, chosen over rejecting the rebind because it cleanly awaits termination (stopAgentTurn + waitForActiveTurn) and preserves the user's rebind intent. stopStreamingForSession moves above the /rebind handler to avoid a TDZ reference; the guard lives in AIChatSidePanelHelpers so both call sites share it. Also passes the panel scope host ids to the manager for the 'This panel' rebind target. * fix(ai): derive session manager rebind targets from all live terminals The global AI session manager built its app-wide terminal list from contextsByTabId, which only covers tabs whose AI panel context is mounted - a live terminal without one was missing as a rebind target. useTerminalAiContexts now also exposes buildAllTerminalAiInfos over the full terminal session store (same buildAITerminalSessionInfo per session, so infos merge seamlessly where a context exists) and threads it to AIChatPanelsHost as appTerminalSessions. * fix(ai): preserve host ids on panel rebind and reset stale agent filters - 'This panel' rebind now carries the viewing panel's scope host ids (currentScopeHostIds) onto the rewritten scope, so the destination host association is not orphaned; explicit picker targets already forwarded their hostIds. - The agent filter no longer survives its agent: when the filtered agent's last chat is deleted the filter resets to 'all' instead of leaving a permanently empty list with a blank select; the select's built-in 'all' option stays the manual clear affordance. * fix(ai): clear source workspace views when rebinding a chat * fix(ai): demote implicit source views when rebinding restored chats * fix(ai): stabilize appTerminalInfos identity and polish global session manager UI - Retain appTerminalInfos array identity across structurally equal terminal updates so unrelated tab state churn does not re-render every mounted AIChatSidePanel. - Replace native <select> elements in GlobalAIChatSessionManager with the shared Radix Select component for consistent dark/light theme rendering. - Wrap the disabled cross-scope open button in a <span> inside TooltipTrigger (and add type="button" to row action buttons) so hover tooltips fire reliably. * fix(ai): carry member host IDs on explicit workspace rebind targets * fix(ai): remove redundant title beside history drawer scope switcher * fix(ai): guard optional appTerminalSessions in AIChatPanelsHost * fix(ai): clear persisted source workspace anchors when rebinding chats * fix(ai): exclude non-exact workspace matches from implicit auto-resume --------- Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
614 lines
22 KiB
TypeScript
614 lines
22 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
activateDraftView,
|
|
bumpDraftMutationVersionState,
|
|
bumpDraftUploadGenerationState,
|
|
clearScopeDraftState,
|
|
clearSessionViewsForSession,
|
|
demoteImplicitSourceViewsForSession,
|
|
createEmptyDraft,
|
|
ensureDraftForScopeState,
|
|
getDraftMutationVersionState,
|
|
getDraftUploadGenerationState,
|
|
pruneStaleSessionPanelViews,
|
|
pruneTerminalScopeState,
|
|
pruneTerminalTransientState,
|
|
resolvePanelView,
|
|
selectDraftForAgentSwitch,
|
|
setDraftView,
|
|
setSessionView,
|
|
updateDraftForScope,
|
|
draftsByScopeEqualIgnoringComposerText,
|
|
draftsByScopeEqualIgnoringAllComposerText,
|
|
} from "./aiDraftState.ts";
|
|
|
|
test("draftsByScopeEqualIgnoringComposerText ignores typing in the active scope", () => {
|
|
const base = createEmptyDraft("catty");
|
|
const prev = { "terminal:1": { ...base, text: "" } };
|
|
const next = { "terminal:1": { ...base, text: "你好", updatedAt: base.updatedAt + 1 } };
|
|
assert.equal(draftsByScopeEqualIgnoringComposerText(prev, next, "terminal:1"), true);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringComposerText(
|
|
prev,
|
|
{ "terminal:1": { ...base, text: "你好", attachments: [{ id: "a" } as never] } },
|
|
"terminal:1",
|
|
),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringComposerText(
|
|
prev,
|
|
{ "terminal:1": prev["terminal:1"], "workspace:2": base },
|
|
"terminal:1",
|
|
),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringComposerText(
|
|
prev,
|
|
{
|
|
"terminal:1": prev["terminal:1"],
|
|
"workspace:2": { ...base, attachments: [{ id: "a" } as never] },
|
|
},
|
|
"terminal:1",
|
|
),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("first composer draft is treated as text-only identity churn", () => {
|
|
const empty = createEmptyDraft("catty");
|
|
const created = { ...empty, text: "你" };
|
|
assert.equal(draftsByScopeEqualIgnoringAllComposerText({}, { "terminal:1": empty }), false);
|
|
assert.equal(draftsByScopeEqualIgnoringAllComposerText({}, { "terminal:1": created }), true);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringComposerText({}, { "terminal:1": created }, "terminal:1"),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringAllComposerText(
|
|
{ "terminal:1": empty },
|
|
{ "terminal:1": created },
|
|
),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringAllComposerText(
|
|
{ "terminal:1": created },
|
|
{ "terminal:1": empty },
|
|
),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringAllComposerText(
|
|
{},
|
|
{ "terminal:1": { ...created, attachments: [{ id: "a" } as never] } },
|
|
),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("draftsByScopeEqualIgnoringAllComposerText ignores typing in every scope", () => {
|
|
const base = createEmptyDraft("catty");
|
|
const prev = {
|
|
"terminal:1": { ...base, text: "a" },
|
|
"terminal:2": { ...base, text: "b", attachments: [] },
|
|
};
|
|
const next = {
|
|
"terminal:1": { ...prev["terminal:1"], text: "aa", updatedAt: base.updatedAt + 1 },
|
|
"terminal:2": { ...prev["terminal:2"], text: "bb", updatedAt: base.updatedAt + 2 },
|
|
};
|
|
assert.equal(draftsByScopeEqualIgnoringAllComposerText(prev, next), true);
|
|
assert.equal(
|
|
draftsByScopeEqualIgnoringAllComposerText(prev, {
|
|
...next,
|
|
"terminal:2": { ...next["terminal:2"], attachments: [{ id: "a" } as never] },
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("updateDraftForScope keeps the original map when the updater is a no-op", () => {
|
|
const draft = createEmptyDraft("catty");
|
|
const prev = { "terminal:1": draft };
|
|
const next = updateDraftForScope(prev, "terminal:1", "catty", (current) => current);
|
|
assert.equal(next, prev);
|
|
});
|
|
|
|
test("createEmptyDraft seeds selected agent and empty inputs", () => {
|
|
const draft = createEmptyDraft("agent-alpha");
|
|
|
|
assert.equal(draft.agentId, "agent-alpha");
|
|
assert.equal(draft.text, "");
|
|
assert.deepEqual(draft.attachments, []);
|
|
assert.deepEqual(draft.selectedUserSkillSlugs, []);
|
|
assert.equal(typeof draft.updatedAt, "number");
|
|
});
|
|
|
|
test("resolvePanelView defaults to draft when no explicit view exists", () => {
|
|
assert.deepEqual(resolvePanelView({}, "terminal:123"), { mode: "draft" });
|
|
});
|
|
|
|
test("setDraftView records draft mode", () => {
|
|
assert.deepEqual(setDraftView({}, "terminal:123"), {
|
|
"terminal:123": { mode: "draft" },
|
|
});
|
|
});
|
|
|
|
test("activateDraftView clears the terminal scope's active session owner", () => {
|
|
const activeSessionIdMap = {
|
|
"terminal:123": "session-123",
|
|
"workspace:abc": "session-workspace",
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:123": { mode: "session", sessionId: "session-123" },
|
|
"workspace:abc": { mode: "session", sessionId: "session-workspace" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = activateDraftView(
|
|
activeSessionIdMap,
|
|
panelViewByScope,
|
|
"terminal:123",
|
|
);
|
|
|
|
assert.deepEqual(next.activeSessionIdMap, {
|
|
"workspace:abc": "session-workspace",
|
|
});
|
|
assert.deepEqual(next.panelViewByScope, {
|
|
"terminal:123": { mode: "draft" },
|
|
"workspace:abc": panelViewByScope["workspace:abc"],
|
|
});
|
|
});
|
|
|
|
test("activateDraftView is a no-op when the scope already has explicit draft view", () => {
|
|
const activeSessionIdMap = {
|
|
"workspace:abc": "session-workspace",
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:123": { mode: "draft" },
|
|
"workspace:abc": { mode: "session", sessionId: "session-workspace" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = activateDraftView(
|
|
activeSessionIdMap,
|
|
panelViewByScope,
|
|
"terminal:123",
|
|
);
|
|
|
|
assert.equal(next.activeSessionIdMap, activeSessionIdMap);
|
|
assert.equal(next.panelViewByScope, panelViewByScope);
|
|
});
|
|
|
|
test("setSessionView records target session id", () => {
|
|
assert.deepEqual(setSessionView({}, "workspace:abc", "session-123"), {
|
|
"workspace:abc": { mode: "session", sessionId: "session-123" },
|
|
});
|
|
});
|
|
|
|
test("pruneStaleSessionPanelViews resets session views that no longer exist", () => {
|
|
const panelViewByScope = {
|
|
"terminal:1": { mode: "session", sessionId: "deleted-session" },
|
|
"workspace:2": { mode: "session", sessionId: "session-keep" },
|
|
"terminal:3": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = pruneStaleSessionPanelViews(
|
|
panelViewByScope,
|
|
new Set(["session-keep"]),
|
|
);
|
|
|
|
assert.deepEqual(next, {
|
|
"terminal:1": { mode: "draft" },
|
|
"workspace:2": { mode: "session", sessionId: "session-keep" },
|
|
"terminal:3": { mode: "draft" },
|
|
});
|
|
});
|
|
|
|
test("pruneStaleSessionPanelViews returns the original ref when nothing is stale", () => {
|
|
const panelViewByScope = {
|
|
"terminal:1": { mode: "session", sessionId: "session-keep" },
|
|
"terminal:2": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = pruneStaleSessionPanelViews(
|
|
panelViewByScope,
|
|
new Set(["session-keep"]),
|
|
);
|
|
|
|
assert.equal(next, panelViewByScope);
|
|
});
|
|
|
|
test("clearSessionViewsForSession clears the rebound chat's source views and keeps the destination + unrelated scopes", () => {
|
|
// Mirrors rebindSessionScope: source A shows chat-1, destination B is set to
|
|
// chat-1, and unrelated scopes keep their own chats/drafts.
|
|
const panelViewByScope = {
|
|
"workspace:A": { mode: "session", sessionId: "chat-1" },
|
|
"workspace:A-alias": { mode: "session", sessionId: "chat-1" },
|
|
"workspace:B": { mode: "session", sessionId: "prev-b" },
|
|
"workspace:C": { mode: "session", sessionId: "chat-2" },
|
|
"workspace:D": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = setSessionView(
|
|
clearSessionViewsForSession(panelViewByScope, "chat-1", "workspace:B"),
|
|
"workspace:B",
|
|
"chat-1",
|
|
);
|
|
|
|
assert.deepEqual(next, {
|
|
"workspace:A": { mode: "draft" },
|
|
"workspace:A-alias": { mode: "draft" },
|
|
"workspace:B": { mode: "session", sessionId: "chat-1" },
|
|
"workspace:C": { mode: "session", sessionId: "chat-2" },
|
|
"workspace:D": { mode: "draft" },
|
|
});
|
|
// The destination's prior view must not be cleared by the purge (setSessionView
|
|
// overwrites it), and unrelated scopes must keep their exact view objects.
|
|
assert.equal(next["workspace:C"], panelViewByScope["workspace:C"]);
|
|
assert.equal(next["workspace:D"], panelViewByScope["workspace:D"]);
|
|
});
|
|
|
|
test("clearSessionViewsForSession returns the original ref when no other scope points at the chat", () => {
|
|
const panelViewByScope = {
|
|
"workspace:B": { mode: "session", sessionId: "chat-2" },
|
|
"workspace:C": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = clearSessionViewsForSession(panelViewByScope, "chat-1", "workspace:B");
|
|
|
|
assert.equal(next, panelViewByScope);
|
|
});
|
|
|
|
// Mirrors rebindSessionScope's single setPanelViewByScope updater: explicit
|
|
// source views are purged, implicit source scopes from the pre-rebind active
|
|
// map are demoted to draft, and the destination view is (re)set to the chat.
|
|
function applyRebindViewUpdates(
|
|
panelViewByScope: Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>,
|
|
activeSessionIdMap: Record<string, string | null>,
|
|
sessionId: string,
|
|
keepScopeKey: string,
|
|
) {
|
|
return setSessionView(
|
|
demoteImplicitSourceViewsForSession(
|
|
clearSessionViewsForSession(panelViewByScope, sessionId, keepScopeKey),
|
|
activeSessionIdMap,
|
|
sessionId,
|
|
keepScopeKey,
|
|
),
|
|
keepScopeKey,
|
|
sessionId,
|
|
);
|
|
}
|
|
|
|
test("round 4 RESTORED: rebind of a restored chat demotes implicit source scopes when panel views are empty", () => {
|
|
// Post-restart (issue #4125): the active-session map is restored from the
|
|
// IndexedDB meta store, but panelViewByScope is memory-only and starts EMPTY.
|
|
// chat-1 is A's only session; rebinding it to B has no explicit source view
|
|
// for the round-3 purge to demote. A's implicit view must be written to draft.
|
|
const activeSessionIdMap = {
|
|
"workspace:A": "chat-1",
|
|
"workspace:C": "chat-2",
|
|
};
|
|
|
|
const next = applyRebindViewUpdates({}, activeSessionIdMap, "chat-1", "workspace:B");
|
|
|
|
assert.deepEqual(next, {
|
|
// A had no explicit view but was an implicit source via the restored map:
|
|
// it now has an explicit draft view that blocks the sessions[0] fallback.
|
|
"workspace:A": { mode: "draft" },
|
|
// Destination still points at the rebound chat.
|
|
"workspace:B": { mode: "session", sessionId: "chat-1" },
|
|
});
|
|
// Unrelated scope C (its active pointer named chat-2, not chat-1) is not a
|
|
// source scope and must not gain any view.
|
|
assert.equal(next["workspace:C"], undefined);
|
|
});
|
|
|
|
test("round 4: rebind keeps explicit source views demoted and leaves unrelated + target scopes intact", () => {
|
|
const activeSessionIdMap = {
|
|
"workspace:A": "chat-1",
|
|
"workspace:A-alias": "chat-1",
|
|
"workspace:C": "chat-2",
|
|
};
|
|
const panelViewByScope = {
|
|
"workspace:A": { mode: "session", sessionId: "chat-1" },
|
|
"workspace:A-alias": { mode: "session", sessionId: "chat-1" },
|
|
"workspace:B": { mode: "session", sessionId: "prev-b" },
|
|
"workspace:C": { mode: "session", sessionId: "chat-2" },
|
|
"workspace:D": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = applyRebindViewUpdates(panelViewByScope, activeSessionIdMap, "chat-1", "workspace:B");
|
|
|
|
assert.deepEqual(next, {
|
|
"workspace:A": { mode: "draft" },
|
|
"workspace:A-alias": { mode: "draft" },
|
|
"workspace:B": { mode: "session", sessionId: "chat-1" },
|
|
"workspace:C": { mode: "session", sessionId: "chat-2" },
|
|
"workspace:D": { mode: "draft" },
|
|
});
|
|
// Unrelated scopes keep their exact objects; the destination is overwritten
|
|
// by setSessionView, not by the purge.
|
|
assert.equal(next["workspace:C"], panelViewByScope["workspace:C"]);
|
|
assert.equal(next["workspace:D"], panelViewByScope["workspace:D"]);
|
|
});
|
|
|
|
test("round 4: implicit source keys come from the PRE-rebind active map (post-clear would miss them)", () => {
|
|
// rebindActiveSessionMap drops A's pointer to chat-1 before views are
|
|
// written. Feeding the post-clear map to the demotion would miss A entirely,
|
|
// so the source keys MUST be captured before rebindActiveSessionMap runs.
|
|
const preRebind = { "workspace:A": "chat-1", "workspace:B": "chat-1" };
|
|
const postClear = { "workspace:B": "chat-1" };
|
|
|
|
const fromPreRebind = demoteImplicitSourceViewsForSession(
|
|
{}, preRebind, "chat-1", "workspace:B",
|
|
);
|
|
assert.deepEqual(fromPreRebind, { "workspace:A": { mode: "draft" } });
|
|
|
|
const fromPostClear = demoteImplicitSourceViewsForSession(
|
|
{}, postClear, "chat-1", "workspace:B",
|
|
);
|
|
assert.deepEqual(fromPostClear, {});
|
|
});
|
|
|
|
test("clearScopeDraftState removes both the draft and current panel view", () => {
|
|
const draftsByScope = {
|
|
"terminal:1": createEmptyDraft("agent-alpha"),
|
|
"workspace:2": createEmptyDraft("agent-beta"),
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:1": { mode: "session", sessionId: "session-123" },
|
|
"workspace:2": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = clearScopeDraftState(draftsByScope, panelViewByScope, "terminal:1");
|
|
|
|
assert.deepEqual(next.draftsByScope, {
|
|
"workspace:2": draftsByScope["workspace:2"],
|
|
});
|
|
assert.deepEqual(next.panelViewByScope, {
|
|
"workspace:2": panelViewByScope["workspace:2"],
|
|
});
|
|
});
|
|
|
|
test("clearScopeDraftState is a no-op when the scope is already cleared", () => {
|
|
const draftsByScope = {
|
|
"workspace:2": createEmptyDraft("agent-beta"),
|
|
};
|
|
const panelViewByScope = {
|
|
"workspace:2": { mode: "draft" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = clearScopeDraftState(draftsByScope, panelViewByScope, "terminal:closed");
|
|
|
|
assert.equal(next.draftsByScope, draftsByScope);
|
|
assert.equal(next.panelViewByScope, panelViewByScope);
|
|
});
|
|
|
|
test("updateDraftForScope creates a draft on first write and keeps other scopes untouched", () => {
|
|
const draftsByScope = {
|
|
"workspace:2": createEmptyDraft("agent-beta"),
|
|
};
|
|
|
|
const next = updateDraftForScope(
|
|
draftsByScope,
|
|
"terminal:1",
|
|
"agent-alpha",
|
|
(draft) => ({
|
|
...draft,
|
|
text: "hello world",
|
|
}),
|
|
);
|
|
|
|
assert.equal(next["terminal:1"].agentId, "agent-alpha");
|
|
assert.equal(next["terminal:1"].text, "hello world");
|
|
assert.equal(next["workspace:2"], draftsByScope["workspace:2"]);
|
|
});
|
|
|
|
test("ensureDraftForScopeState adds the missing scope without dropping siblings", () => {
|
|
const draftsByScope = {
|
|
"workspace:2": createEmptyDraft("agent-beta"),
|
|
};
|
|
|
|
const next = ensureDraftForScopeState(
|
|
draftsByScope,
|
|
"terminal:1",
|
|
"agent-alpha",
|
|
);
|
|
|
|
assert.equal(next["terminal:1"].agentId, "agent-alpha");
|
|
assert.equal(next["terminal:1"].text, "");
|
|
assert.equal(next["workspace:2"], draftsByScope["workspace:2"]);
|
|
});
|
|
|
|
test("ensureDraftForScopeState returns the original ref when the scope already exists", () => {
|
|
const draftsByScope = {
|
|
"terminal:1": createEmptyDraft("agent-alpha"),
|
|
};
|
|
|
|
const next = ensureDraftForScopeState(
|
|
draftsByScope,
|
|
"terminal:1",
|
|
"agent-beta",
|
|
);
|
|
|
|
assert.equal(next, draftsByScope);
|
|
});
|
|
|
|
test("selectDraftForAgentSwitch preserves hidden draft content when leaving a populated chat session", () => {
|
|
const currentDraft = {
|
|
...createEmptyDraft("agent-alpha"),
|
|
text: "keep me only if I was already drafting",
|
|
attachments: [{ id: "file-1", filename: "note.txt", dataUrl: "", base64Data: "", mediaType: "text/plain" }],
|
|
selectedUserSkillSlugs: ["skill-a"],
|
|
};
|
|
|
|
const next = selectDraftForAgentSwitch(currentDraft, "agent-beta", true);
|
|
|
|
assert.equal(next.agentId, "agent-beta");
|
|
assert.equal(next.text, "keep me only if I was already drafting");
|
|
assert.deepEqual(next.attachments, currentDraft.attachments);
|
|
assert.deepEqual(next.selectedUserSkillSlugs, ["skill-a"]);
|
|
});
|
|
|
|
test("selectDraftForAgentSwitch resets to an empty draft when leaving a populated chat session without pending draft content", () => {
|
|
const currentDraft = createEmptyDraft("agent-alpha");
|
|
|
|
const next = selectDraftForAgentSwitch(currentDraft, "agent-beta", true);
|
|
|
|
assert.equal(next.agentId, "agent-beta");
|
|
assert.equal(next.text, "");
|
|
assert.deepEqual(next.attachments, []);
|
|
assert.deepEqual(next.selectedUserSkillSlugs, []);
|
|
});
|
|
|
|
test("selectDraftForAgentSwitch preserves an existing draft while only changing agent", () => {
|
|
const currentDraft = {
|
|
...createEmptyDraft("agent-alpha"),
|
|
text: "unfinished prompt",
|
|
selectedUserSkillSlugs: ["skill-a"],
|
|
};
|
|
|
|
const next = selectDraftForAgentSwitch(currentDraft, "agent-beta", false);
|
|
|
|
assert.equal(next.agentId, "agent-beta");
|
|
assert.equal(next.text, "unfinished prompt");
|
|
assert.deepEqual(next.selectedUserSkillSlugs, ["skill-a"]);
|
|
});
|
|
|
|
test("draft mutation version increments on every mutation for the same scope", () => {
|
|
const scopeKey = "terminal:1";
|
|
const initialVersion = getDraftMutationVersionState({}, scopeKey);
|
|
const nextVersions = bumpDraftMutationVersionState({}, scopeKey);
|
|
const finalVersions = bumpDraftMutationVersionState(nextVersions, scopeKey);
|
|
|
|
assert.equal(initialVersion, 0);
|
|
assert.equal(getDraftMutationVersionState(nextVersions, scopeKey), 1);
|
|
assert.equal(getDraftMutationVersionState(finalVersions, scopeKey), 2);
|
|
});
|
|
|
|
test("draft upload generation only increments when the draft lifecycle rolls over", () => {
|
|
const scopeKey = "terminal:1";
|
|
const initialGeneration = getDraftUploadGenerationState({}, scopeKey);
|
|
const nextGenerations = bumpDraftUploadGenerationState({}, scopeKey);
|
|
const finalGenerations = bumpDraftUploadGenerationState(nextGenerations, scopeKey);
|
|
|
|
assert.equal(initialGeneration, 0);
|
|
assert.equal(getDraftUploadGenerationState(nextGenerations, scopeKey), 1);
|
|
assert.equal(getDraftUploadGenerationState(finalGenerations, scopeKey), 2);
|
|
});
|
|
|
|
test("pruneTerminalScopeState removes closed terminal drafts and views only", () => {
|
|
const draftsByScope = {
|
|
"terminal:closed": createEmptyDraft("agent-alpha"),
|
|
"terminal:open": createEmptyDraft("agent-beta"),
|
|
"workspace:keep": createEmptyDraft("agent-gamma"),
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:closed": { mode: "draft" },
|
|
"terminal:open": { mode: "session", sessionId: "session-open" },
|
|
"workspace:keep": { mode: "session", sessionId: "session-workspace" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = pruneTerminalScopeState(
|
|
draftsByScope,
|
|
panelViewByScope,
|
|
new Set(["open"]),
|
|
);
|
|
|
|
assert.deepEqual(next.draftsByScope, {
|
|
"terminal:open": draftsByScope["terminal:open"],
|
|
"workspace:keep": draftsByScope["workspace:keep"],
|
|
});
|
|
assert.deepEqual(next.panelViewByScope, {
|
|
"terminal:open": panelViewByScope["terminal:open"],
|
|
"workspace:keep": panelViewByScope["workspace:keep"],
|
|
});
|
|
});
|
|
|
|
test("pruneTerminalScopeState returns original refs when nothing is pruned", () => {
|
|
const draftsByScope = {
|
|
"terminal:open": createEmptyDraft("agent-alpha"),
|
|
"workspace:keep": createEmptyDraft("agent-beta"),
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:open": { mode: "draft" },
|
|
"workspace:keep": { mode: "session", sessionId: "session-1" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = pruneTerminalScopeState(
|
|
draftsByScope,
|
|
panelViewByScope,
|
|
new Set(["open"]),
|
|
);
|
|
|
|
assert.equal(next.draftsByScope, draftsByScope);
|
|
assert.equal(next.panelViewByScope, panelViewByScope);
|
|
});
|
|
|
|
test("pruneTerminalTransientState clears closed terminal active session, draft, and view state only", () => {
|
|
const activeSessionIdMap = {
|
|
"terminal:closed": "session-closed",
|
|
"terminal:open": "session-open",
|
|
"workspace:keep": "session-workspace",
|
|
};
|
|
const draftsByScope = {
|
|
"terminal:closed": createEmptyDraft("agent-alpha"),
|
|
"terminal:open": createEmptyDraft("agent-beta"),
|
|
"workspace:keep": createEmptyDraft("agent-gamma"),
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:closed": { mode: "draft" },
|
|
"terminal:open": { mode: "session", sessionId: "session-open" },
|
|
"workspace:keep": { mode: "session", sessionId: "session-workspace" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = pruneTerminalTransientState(
|
|
activeSessionIdMap,
|
|
draftsByScope,
|
|
panelViewByScope,
|
|
new Set(["open"]),
|
|
);
|
|
|
|
assert.deepEqual(next.activeSessionIdMap, {
|
|
"terminal:open": "session-open",
|
|
"workspace:keep": "session-workspace",
|
|
});
|
|
assert.deepEqual(next.draftsByScope, {
|
|
"terminal:open": draftsByScope["terminal:open"],
|
|
"workspace:keep": draftsByScope["workspace:keep"],
|
|
});
|
|
assert.deepEqual(next.panelViewByScope, {
|
|
"terminal:open": panelViewByScope["terminal:open"],
|
|
"workspace:keep": panelViewByScope["workspace:keep"],
|
|
});
|
|
});
|
|
|
|
test("pruneTerminalTransientState returns original refs when no terminal scopes close", () => {
|
|
const activeSessionIdMap = {
|
|
"terminal:open": "session-open",
|
|
"workspace:keep": "session-workspace",
|
|
};
|
|
const draftsByScope = {
|
|
"terminal:open": createEmptyDraft("agent-alpha"),
|
|
"workspace:keep": createEmptyDraft("agent-beta"),
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:open": { mode: "draft" },
|
|
"workspace:keep": { mode: "session", sessionId: "session-workspace" },
|
|
} satisfies Record<string, { mode: "draft" } | { mode: "session"; sessionId: string }>;
|
|
|
|
const next = pruneTerminalTransientState(
|
|
activeSessionIdMap,
|
|
draftsByScope,
|
|
panelViewByScope,
|
|
new Set(["open"]),
|
|
);
|
|
|
|
assert.equal(next.activeSessionIdMap, activeSessionIdMap);
|
|
assert.equal(next.draftsByScope, draftsByScope);
|
|
assert.equal(next.panelViewByScope, panelViewByScope);
|
|
});
|