| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- import { describe, expect, it, vi } from "vitest";
- import type { ConversationSnapshot } from "@zcode/shared/zcode-protocol-v4";
- import {
- applyExternalTextInsertRequestToComposer,
- applyComposerRestoreRequestToComposer,
- restorePersistedComposerDraftIntoInput,
- type ComposerRestoreRequest,
- } from "@/v4/ConversationComposer.js";
- import {
- resolveQueuedComposerRestore,
- shouldRestoreQueuedComposerFromAck,
- } from "@/v4/SessionPane.js";
- function makeSnapshot(): ConversationSnapshot {
- return {
- protocolVersion: 1,
- sessionId: "session-1",
- logEpoch: "epoch-1",
- seq: 1,
- revision: 7,
- control: {
- phase: "running",
- sessionEnded: false,
- canStop: true,
- stopState: "stoppable",
- stopTargetKind: "primaryTurn",
- activeWorks: [],
- lastError: null,
- apiRetry: null,
- },
- availability: {
- fork: { allowed: true },
- compact: { allowed: true },
- switchModelConfig: { allowed: true },
- setFollowupMode: { allowed: true },
- queueEdit: { allowed: true },
- sendQueuedNow: { allowed: true },
- pauseGoal: { allowed: false, reasonCode: "noGoalToPause" },
- resumeGoal: { allowed: false, reasonCode: "noGoalToResume" },
- },
- inputRouting: { mode: "enqueue" },
- meta: { title: "Session", titleSource: "generated" },
- config: { provider: "", model: "", thought: "", followupMode: "queue", mode: "build" },
- usage: {
- contextWindow: null,
- cumulative: {
- inputTokens: 0,
- outputTokens: 0,
- cacheReadTokens: 0,
- cacheWriteTokens: 0,
- },
- },
- queue: {
- autoDrain: true,
- items: [
- {
- queueItemId: "q-1",
- kind: "sendText",
- text: "排队原文",
- sourceCommandId: "cmd-q1",
- clientId: "cli",
- attachments: [
- { ref: "ref-b", fileName: "b.png", mime: "image/png", bytes: 2 },
- { ref: "ref-a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
- ],
- delivery: { requested: "queue", admitted: "queue" },
- order: { admissionSeq: 1, queuePosition: 0 },
- steer: { state: "notRequested" },
- dispatch: { state: "queued" },
- admittedAt: 1,
- },
- ],
- },
- pendingInteractions: [],
- pendingCommands: [],
- backgroundWorks: [],
- goal: null,
- plan: null,
- rows: { window: [], totalCount: 0, firstRowId: null },
- };
- }
- describe("v4 queue edit extraction", () => {
- it("Workspace 插件页重挂载时从共享草稿恢复结构化 mention", () => {
- const mention = {
- id: "plugin:document-skills@official",
- category: "plugins" as const,
- label: "文档技能",
- value: "document-skills@official",
- markdown: "[@文档技能](plugin://document-skills@official)",
- data: { pluginId: "document-skills@official" },
- };
- const inputApi = {
- getMarkdown: vi.fn(() => "unused"),
- setEditorStateJson: vi.fn(),
- setMention: vi.fn(),
- setText: vi.fn(),
- };
- const text = `${mention.markdown} 整理文档`;
- expect(
- restorePersistedComposerDraftIntoInput({
- draft: { text, mention, updatedAt: 1 },
- inputApi,
- }),
- ).toBe(text);
- expect(inputApi.setMention).toHaveBeenCalledWith(mention, " 整理文档");
- expect(inputApi.setText).not.toHaveBeenCalled();
- });
- it("插件 Skills 与独立 Skills 的 New 入口从共享草稿恢复 skill node", () => {
- const mention = {
- id: "skill:skill-creator",
- category: "skills" as const,
- label: "skill-creator",
- value: "skill-creator",
- markdown: "[$skill-creator](./.agents/skills/skill-creator/SKILL.md)",
- data: {
- path: ".agents/skills/skill-creator/SKILL.md",
- scope: "workspace" as const,
- },
- };
- const inputApi = {
- getMarkdown: vi.fn(() => "unused"),
- setEditorStateJson: vi.fn(),
- setMention: vi.fn(),
- setText: vi.fn(),
- };
- const text = `${mention.markdown} `;
- expect(
- restorePersistedComposerDraftIntoInput({
- draft: { text, mention },
- inputApi,
- }),
- ).toBe(text);
- expect(inputApi.setMention).toHaveBeenCalledWith(mention, " ");
- expect(inputApi.setText).not.toHaveBeenCalled();
- });
- it("新建技能把 canonical 前缀恢复为结构化 Skill mention 并保留尾空格", () => {
- const mention = {
- id: "skill:skill-creator",
- category: "skills" as const,
- label: "skill-creator",
- value: "skill-creator",
- markdown: "[$skill-creator](./.agents/skills/skill-creator/SKILL.md)",
- data: {
- path: ".agents/skills/skill-creator/SKILL.md",
- scope: "workspace" as const,
- },
- };
- const inputApi = {
- setMention: vi.fn(),
- setText: vi.fn(),
- };
- const updateText = vi.fn();
- const requestFocus = vi.fn();
- const scheduleDraftPersist = vi.fn();
- const text = `${mention.markdown} `;
- expect(
- applyExternalTextInsertRequestToComposer({
- appliedRequestId: null,
- inputApi,
- request: { requestId: 6, text, mention },
- requestFocus,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBe(6);
- expect(inputApi.setMention).toHaveBeenCalledWith(mention, " ");
- expect(inputApi.setText).not.toHaveBeenCalled();
- expect(updateText).toHaveBeenCalledWith(text);
- });
- it("商店试用把 canonical 前缀恢复为结构化 Plugin mention", () => {
- const mention = {
- id: "plugin:demo@mkt",
- category: "plugins" as const,
- label: "Demo",
- value: "demo@mkt",
- markdown: "[@Demo](plugin://demo@mkt)",
- data: {
- pluginId: "demo@mkt",
- icon: "https://cdn.example.com/demo.png",
- },
- };
- const inputApi = {
- setMention: vi.fn(),
- setText: vi.fn(),
- };
- const updateText = vi.fn();
- const requestFocus = vi.fn();
- const scheduleDraftPersist = vi.fn();
- const text = `${mention.markdown} Diagnose this workspace`;
- expect(
- applyExternalTextInsertRequestToComposer({
- appliedRequestId: null,
- inputApi,
- request: { requestId: 7, text, mention },
- requestFocus,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBe(7);
- expect(inputApi.setMention).toHaveBeenCalledWith(mention, " Diagnose this workspace");
- expect(inputApi.setText).not.toHaveBeenCalled();
- expect(updateText).toHaveBeenCalledWith(text);
- expect(scheduleDraftPersist).toHaveBeenCalledTimes(1);
- expect(requestFocus).toHaveBeenCalledTimes(1);
- });
- it("插件安装完成后把唯一 chip 前置到编辑器当前正文而不回放旧 prompt", () => {
- const mention = {
- id: "plugin:document-skills@zcode-plugins-official",
- category: "plugins" as const,
- label: "文档技能",
- value: "document-skills@zcode-plugins-official",
- markdown: "[@文档技能](plugin://document-skills@zcode-plugins-official)",
- data: { pluginId: "document-skills@zcode-plugins-official" },
- };
- const inputApi = {
- getMarkdown: vi.fn(
- () => `${mention.markdown} 用户安装期间编辑后的正文`,
- ),
- prependMentionIfMissing: vi.fn(() => true),
- setMention: vi.fn(),
- setText: vi.fn(),
- };
- const updateText = vi.fn();
- const requestFocus = vi.fn();
- const scheduleDraftPersist = vi.fn();
- expect(
- applyExternalTextInsertRequestToComposer({
- appliedRequestId: null,
- inputApi,
- request: {
- requestId: 8,
- text: mention.markdown,
- mention,
- mode: "prepend-if-missing",
- },
- requestFocus,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBe(8);
- expect(inputApi.prependMentionIfMissing).toHaveBeenCalledWith(mention);
- expect(updateText).toHaveBeenCalledWith(`${mention.markdown} 用户安装期间编辑后的正文`);
- expect(inputApi.setMention).not.toHaveBeenCalled();
- expect(inputApi.setText).not.toHaveBeenCalled();
- });
- it("当前草稿已有同一 Plugin canonical 文本时不重复插入 chip", () => {
- const mention = {
- id: "plugin:demo@mkt",
- category: "plugins" as const,
- label: "Demo",
- value: "demo@mkt",
- markdown: "[@Demo](plugin://demo@mkt)",
- data: { pluginId: "demo@mkt" },
- };
- const inputApi = {
- // label 是 display-only;即使语言切换导致 label 不同,同一 stable ID 也不能重复插入。
- getMarkdown: vi.fn(() => "[@旧名称](plugin://demo@mkt) 当前正文"),
- prependMentionIfMissing: vi.fn(() => false),
- setMention: vi.fn(),
- setText: vi.fn(),
- };
- const updateText = vi.fn();
- expect(
- applyExternalTextInsertRequestToComposer({
- appliedRequestId: null,
- inputApi,
- request: {
- requestId: 9,
- text: mention.markdown,
- mention,
- mode: "prepend-if-missing",
- },
- requestFocus: vi.fn(),
- scheduleDraftPersist: vi.fn(),
- updateText,
- }),
- ).toBe(9);
- expect(inputApi.prependMentionIfMissing).toHaveBeenCalledWith(mention);
- expect(inputApi.setMention).not.toHaveBeenCalled();
- expect(updateText).not.toHaveBeenCalled();
- });
- it("PA158 撤回使用队列项自身权限/Plan/模型,不借用当前 Composer 配置", () => {
- const snapshot = makeSnapshot();
- Object.assign(snapshot.queue.items[0]!, {
- mode: "yolo",
- planEnabled: false,
- modelSelection: {
- providerId: "provider",
- modelId: "model",
- options: { reasoningLevel: "high" },
- },
- });
- expect(resolveQueuedComposerRestore(snapshot, "q-1")?.config).toEqual({
- mode: "yolo",
- planEnabled: false,
- modelSelection: {
- providerId: "provider",
- modelId: "model",
- options: { reasoningLevel: "high" },
- },
- });
- });
- it("提取完整输入并保持附件顺序,供权威删除后恢复 composer", () => {
- expect(resolveQueuedComposerRestore(makeSnapshot(), "q-1")).toEqual({
- baseRevision: 7,
- queueItemId: "q-1",
- sourceCommandId: "cmd-q1",
- inputKind: "sendText",
- text: "排队原文",
- attachments: [
- { ref: "ref-b", fileName: "b.png", mime: "image/png", bytes: 2 },
- { ref: "ref-a", fileName: "a.txt", mime: "text/plain", bytes: 1 },
- ],
- });
- expect(resolveQueuedComposerRestore(makeSnapshot(), "missing")).toBeNull();
- });
- it("goal 恢复可见 slash 文本,compact 明确剪枝", () => {
- const goal = makeSnapshot();
- goal.queue.items[0] = {
- ...goal.queue.items[0]!,
- kind: "sendGoalCommand",
- text: "修复登录",
- };
- expect(resolveQueuedComposerRestore(goal, "q-1")?.text).toBe("/goal 修复登录");
- const compact = makeSnapshot();
- compact.queue.items[0] = {
- ...compact.queue.items[0]!,
- kind: "compact",
- text: "/compact",
- attachments: [],
- };
- expect(resolveQueuedComposerRestore(compact, "q-1")).toBeNull();
- });
- it("只允许 accepted/duplicate 恢复,其他 ACK 分支保持 composer 不变", () => {
- expect(shouldRestoreQueuedComposerFromAck("accepted")).toBe(true);
- expect(shouldRestoreQueuedComposerFromAck("duplicate")).toBe(true);
- expect(shouldRestoreQueuedComposerFromAck("noop")).toBe(false);
- expect(shouldRestoreQueuedComposerFromAck("stale")).toBe(false);
- expect(shouldRestoreQueuedComposerFromAck("rejected")).toBe(false);
- expect(shouldRestoreQueuedComposerFromAck("failed")).toBe(false);
- });
- it("只在绑定 session/workspace 的空 composer 幂等恢复文字与附件", () => {
- const request: ComposerRestoreRequest = {
- requestId: 1,
- sessionId: "session-1",
- workspaceKey: "remote:ssh:host:/workspace",
- inputKind: "sendText",
- text: "排队原文",
- attachments: [{ ref: "ref-1", fileName: "a.png", mime: "image/png", bytes: 1 }],
- config: { mode: "yolo", planEnabled: false },
- };
- const inputApi = {
- focus: vi.fn(),
- setText: vi.fn(),
- };
- const updateText = vi.fn();
- const requestFocus = vi.fn();
- const scheduleDraftPersist = vi.fn();
- const restoreSessionOwnedAttachments = vi.fn(() => true);
- const restoreDraftConfig = vi.fn();
- expect(
- applyComposerRestoreRequestToComposer({
- appliedRequestId: null,
- currentSessionId: "session-1",
- currentWorkspaceKey: "remote:ssh:host:/workspace",
- hasDraftContent: false,
- inputApi,
- request,
- requestFocus,
- restoreSessionOwnedAttachments,
- restoreDraftConfig,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBe(1);
- expect(inputApi.setText).toHaveBeenCalledWith("排队原文");
- expect(updateText).toHaveBeenCalledWith("排队原文");
- expect(requestFocus).toHaveBeenCalledTimes(1);
- expect(scheduleDraftPersist).toHaveBeenCalledTimes(1);
- expect(restoreSessionOwnedAttachments).toHaveBeenCalledWith(request.attachments);
- expect(
- applyComposerRestoreRequestToComposer({
- appliedRequestId: 1,
- currentSessionId: "session-1",
- currentWorkspaceKey: "remote:ssh:host:/workspace",
- hasDraftContent: false,
- inputApi,
- request,
- requestFocus,
- restoreSessionOwnedAttachments,
- restoreDraftConfig,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBe(1);
- expect(inputApi.setText).toHaveBeenCalledTimes(1);
- expect(restoreSessionOwnedAttachments).toHaveBeenCalledTimes(1);
- expect(
- applyComposerRestoreRequestToComposer({
- appliedRequestId: null,
- currentSessionId: "session-other",
- currentWorkspaceKey: "remote:ssh:host:/workspace",
- hasDraftContent: false,
- inputApi,
- request,
- requestFocus,
- restoreSessionOwnedAttachments,
- restoreDraftConfig,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBeNull();
- expect(
- applyComposerRestoreRequestToComposer({
- appliedRequestId: null,
- currentSessionId: "session-1",
- currentWorkspaceKey: "remote:ssh:host:/workspace",
- hasDraftContent: true,
- inputApi,
- request,
- requestFocus,
- restoreSessionOwnedAttachments,
- restoreDraftConfig,
- scheduleDraftPersist,
- updateText,
- }),
- ).toBeNull();
- expect(inputApi.setText).toHaveBeenCalledTimes(1);
- expect(restoreDraftConfig).toHaveBeenCalledExactlyOnceWith(request.config);
- });
- });
|