| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675 |
- import { act, createElement, StrictMode } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import type { ZCodeConfigOption } from "@zcode/shared";
- import type { SessionConfigState } from "@zcode/shared/zcode-protocol-v4";
- import type { IModelSelectionService, ModelSelectionView } from "@zcode/services";
- import { resolveEffectiveModelSelection, type ProviderRegistryView } from "@zcode/provider";
- import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
- import { useDraftConfigControl as useActualDraftConfigControl } from "@/v4/composer/useDraftConfigControl.js";
- import {
- persistV4ComposerDraft as persistScopedDraft,
- readV4ComposerDraft as readScopedDraft,
- resolveComposerScope,
- } from "@/v4/composer/composerDraftStore.js";
- import {
- captureComposerRecentSubmission as captureScopedRecent,
- readComposerRecent,
- } from "@/lib/composerRecent.js";
- function testScope(path: string, identity?: string) {
- return resolveComposerScope(
- useZCodeSessionStore.getState().getWorkspaceState(path, identity).draftProjectId,
- identity,
- );
- }
- function persistV4ComposerDraft(...args: Parameters<typeof persistScopedDraft>) {
- return persistScopedDraft(
- args[0],
- args[1],
- args[2],
- args[3],
- args[4] ?? testScope(args[0], args[1]),
- );
- }
- function readV4ComposerDraft(...args: Parameters<typeof readScopedDraft>) {
- return readScopedDraft(args[0], args[1], args[2], args[3] ?? testScope(args[0], args[1]));
- }
- function captureComposerRecentSubmission(...args: Parameters<typeof captureScopedRecent>) {
- return captureScopedRecent(
- args[0],
- args[1],
- args[2],
- args[3],
- args[4] ?? testScope(args[0], args[2]),
- );
- }
- import { resolveImportedComposerConfig } from "@/v4/composer/newTaskDraft.js";
- function activateImportedSession(result: Parameters<typeof resolveImportedComposerConfig>[0]) {
- useZCodeSessionStore
- .getState()
- .setActiveTaskId(
- result.workspacePath,
- result.sessionId,
- result.workspaceIdentity,
- resolveImportedComposerConfig(result),
- );
- }
- function readImportInitialization(
- workspacePath: string,
- workspaceIdentity: string | undefined,
- taskId: string,
- ) {
- const value = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity).composerInitialization;
- return value?.taskId === taskId ? value.config : undefined;
- }
- const { prepareWorkspaceMock } = vi.hoisted(() => ({
- prepareWorkspaceMock: vi.fn(),
- }));
- const { toastMock } = vi.hoisted(() => ({
- toastMock: vi.fn(),
- }));
- vi.mock("@/hooks/useWorkspacePrepare.js", () => ({
- prepareWorkspaceWithZCodeSessionService: prepareWorkspaceMock,
- }));
- vi.mock("@/hooks/useZCodeSessionService.js", () => ({
- useZCodeSessionService: () => ({ readWorkspacePresentation: vi.fn() }),
- }));
- vi.mock("@/hooks/useSettingService.js", () => ({
- useSettings: () => ({ settings: null }),
- }));
- vi.mock("@/components/ui/toast.js", () => ({
- toast: toastMock,
- }));
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({
- intl: { formatMessage: ({ id }: { id: string }) => id },
- }),
- }));
- vi.mock("@/logger.js", () => ({
- logger: {
- debug: vi.fn(),
- info: vi.fn(),
- warn: vi.fn(),
- },
- }));
- const WORKSPACE_PRESENTATION: ZCodeConfigOption[] = [
- {
- id: "mode",
- name: "Mode",
- category: "mode",
- type: "select",
- currentValue: "build",
- options: [{ value: "build", name: "Build" }],
- },
- ];
- const CUSTOM_COMMANDS = [
- {
- name: "review",
- description: "Review current changes",
- source: "custom" as const,
- },
- ];
- const selectionServices = new Map<string, IModelSelectionService>();
- // 用真实纯解析算法模拟 Host,只省略 RPC;同内容的 fixture 保持同一 Service owner。
- function useDraftConfigControl(
- params: Omit<Parameters<typeof useActualDraftConfigControl>[0], "modelSelectionService"> & {
- modelSelectionView: ModelSelectionView | null;
- },
- ) {
- const view = params.modelSelectionView;
- const key = JSON.stringify(view);
- if (view && !selectionServices.has(key))
- selectionServices.set(key, {
- getView: async (input) => ({
- ...view,
- ...(input
- ? resolveEffectiveModelSelection({
- selection: input.selection,
- registry: view as unknown as ProviderRegistryView,
- classifyProvider: () => "ordinary",
- })
- : {}),
- }),
- onDidChange: () => ({ dispose: () => {} }),
- });
- return useActualDraftConfigControl({
- ...params,
- modelSelectionService: view ? selectionServices.get(key)! : null,
- });
- }
- function modelSelectionView(
- revision: number,
- modelIds: readonly string[],
- preferredModelId: string,
- ): ModelSelectionView {
- return {
- revision,
- preferredSelection: {
- providerId: "provider",
- modelId: preferredModelId,
- },
- providers: [
- {
- providerId: "provider",
- config: {},
- models: modelIds.map((modelId) => ({
- modelId,
- config: {
- optionSpecs: {
- reasoningLevel: { values: ["low", "high"], map: "{}" },
- },
- },
- })),
- },
- ],
- } as ModelSelectionView;
- }
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- addEventListener: () => {},
- appendChild: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- childNodes: [] as unknown[],
- getAttribute: () => null,
- insertBefore: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as unknown,
- removeAttribute: () => {},
- removeChild: (child: { parentNode?: unknown }) => {
- child.parentNode = null;
- return child;
- },
- removeEventListener: () => {},
- setAttribute: () => {},
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as Element;
- }
- function installMinimalDom() {
- const storage = new Map<string, string>();
- const documentMock = {
- addEventListener: () => {},
- createElement: (tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createTextNode: (nodeValue: string) => ({
- nodeType: 3,
- nodeValue,
- ownerDocument: documentMock,
- parentNode: null,
- }),
- nodeType: 9,
- removeEventListener: () => {},
- } as unknown as Document;
- const windowMock = {
- localStorage: {
- getItem: (key: string) => storage.get(key) ?? null,
- setItem: (key: string, value: string) => storage.set(key, value),
- removeItem: (key: string) => storage.delete(key),
- },
- addEventListener: () => {},
- document: documentMock,
- HTMLIFrameElement: function HTMLIFrameElement() {},
- HTMLElement: function HTMLElement() {},
- Node: function Node() {},
- removeEventListener: () => {},
- };
- Object.defineProperty(globalThis, "document", {
- configurable: true,
- value: documentMock,
- });
- Object.defineProperty(globalThis, "window", {
- configurable: true,
- value: windowMock,
- });
- Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
- configurable: true,
- value: true,
- });
- return createMinimalElement(documentMock);
- }
- describe("useDraftConfigControl slash command hydration", () => {
- it("PD03 同目录切项目恢复各自正文;旧 Hook 回调不能写新项目", async () => {
- const workspacePath = "/shared-project-root";
- const root = createRoot(installMinimalDom());
- const view = modelSelectionView(1, ["model"], "model");
- let control: ReturnType<typeof useDraftConfigControl> | undefined;
- function Probe() {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: null,
- modelSelectionView: view,
- agentStartupAllowed: false,
- });
- return null;
- }
- act(() =>
- useZCodeSessionStore
- .getState()
- .startDraft(workspacePath, undefined, undefined, { projectId: "p" }),
- );
- await act(async () => root.render(createElement(Probe)));
- act(() => {
- control!.updateComposerContent({ text: "P" });
- control!.handleDraftSwitchMode("yolo");
- });
- const stale = control!.updateComposerContent;
- act(() =>
- useZCodeSessionStore
- .getState()
- .startDraft(workspacePath, undefined, undefined, { projectId: "q" }),
- );
- expect(control!.composerDraft.text).toBe("");
- act(() => {
- control!.updateComposerContent({ text: "Q" });
- stale({ text: "late P" });
- });
- expect(control!.composerDraft.text).toBe("Q");
- act(() =>
- useZCodeSessionStore
- .getState()
- .startDraft(workspacePath, undefined, undefined, { projectId: "p" }),
- );
- expect(control!.composerDraft.text).toBe("P");
- expect(control!.draftConfig.mode).toBe("yolo");
- act(() => root.unmount());
- });
- it("TD02/04 两次提交后重开空白页继承 B,重复 New Task 不保留空配置", async () => {
- const workspacePath = "/text-only-regression";
- const root = createRoot(installMinimalDom());
- const view = modelSelectionView(1, ["A", "B"], "A");
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "yolo", modelSelection: view.preferredSelection },
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- await act(async () => {
- control!.handleDraftSwitchMode("yolo");
- control!.handleDraftSelectModel("provider", "A");
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
- act(() =>
- captureComposerRecentSubmission(workspacePath, {
- mode: "yolo",
- modelSelection: control!.draftConfig.modelSelection!,
- })(),
- );
- await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
- await act(async () => {
- control!.handleDraftSwitchMode("build");
- control!.handleDraftSelectModel("provider", "B");
- });
- act(() =>
- captureComposerRecentSubmission(workspacePath, {
- mode: "build",
- modelSelection: control!.draftConfig.modelSelection!,
- })(),
- );
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- expect(control!.draftConfig).toMatchObject({ mode: "build", model: "B" });
- act(() => control!.handleDraftSwitchMode("yolo"));
- await act(async () => useZCodeSessionStore.getState().startDraft(workspacePath));
- expect(control!.draftConfig).toMatchObject({ mode: "build", model: "B" });
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
- act(() => root.unmount());
- });
- it.each([undefined, "remote:provider-readiness"])(
- "I24 首个 Provider 就绪只补齐未选择模型的 Root 草稿:%s",
- async (workspaceIdentity) => {
- const workspacePath = "/provider-readiness";
- const root = createRoot(installMinimalDom());
- const emptyView = { ...modelSelectionView(1, [], ""), preferredSelection: null };
- const readyView = {
- ...modelSelectionView(2, ["model"], "model"),
- preferredSelection: {
- providerId: "provider",
- modelId: "model",
- options: { reasoningLevel: "high" },
- },
- };
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ view }: { view: ModelSelectionView }) {
- control = useDraftConfigControl({
- workspacePath,
- workspaceIdentity,
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { view: emptyView })));
- act(() => {
- control!.updateComposerContent({ text: "KEEP DRAFT", editorStateJson: "EDITOR" });
- control!.handleDraftSwitchMode("plan");
- });
- const before = control!.composerDraft;
- expect(before.modelSelection).toBeUndefined();
- await act(async () => root.render(createElement(Probe, { view: readyView })));
- expect(control!.draftConfig.modelSelection).toEqual(readyView.preferredSelection);
- expect(control!.composerDraft).toMatchObject({
- text: before.text,
- editorStateJson: before.editorStateJson,
- mode: before.mode,
- planEnabled: before.planEnabled,
- });
- expect(
- readV4ComposerDraft(workspacePath, workspaceIdentity, "__draft__")?.modelSelection,
- ).toEqual(readyView.preferredSelection);
- // 同一路径的另一远端身份没有被写入;补选不冒充已接纳提交更新 Recent。
- expect(readV4ComposerDraft(workspacePath, "remote:other", "__draft__")).toBeNull();
- expect(
- window.localStorage.getItem(
- `zcode-model-selection-recent-v1:${workspaceIdentity ?? workspacePath}`,
- ),
- ).toBeNull();
- act(() => root.unmount());
- },
- );
- it.each(["chosen", "invalid", "session"])(
- "I24 目录就绪不替换已有意图或 Session:%s",
- async (scenario) => {
- const workspacePath = "/provider-readiness-excluded";
- const root = createRoot(installMinimalDom());
- const sessionId = scenario === "session" ? "existing" : null;
- const selection = {
- providerId: "provider",
- modelId: scenario === "invalid" ? "removed" : "chosen",
- options: { reasoningLevel: "low" },
- };
- persistV4ComposerDraft(workspacePath, undefined, sessionId ?? "__draft__", {
- text: "KEEP INTENT",
- mode: "build",
- planEnabled: true,
- ...(scenario === "session" ? {} : { modelSelection: selection }),
- });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ ready }: { ready: boolean }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- agentStartupAllowed: false,
- sessionConfig: sessionId ? { mode: "build" } : null,
- modelSelectionView: {
- ...modelSelectionView(ready ? 2 : 1, ready ? ["chosen", "default"] : [], "default"),
- preferredSelection: ready
- ? {
- providerId: "provider",
- modelId: "default",
- options: { reasoningLevel: "high" },
- }
- : null,
- },
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { ready: false })));
- await act(async () => root.render(createElement(Probe, { ready: true })));
- expect(control!.composerDraft.modelSelection).toEqual(
- scenario === "session" ? undefined : selection,
- );
- expect(control!.draftConfig.modelSelection).toEqual(
- scenario === "chosen" ? selection : undefined,
- );
- expect(control!.composerDraft.text).toBe("KEEP INTENT");
- act(() => root.unmount());
- },
- );
- it.each(["root", "empty-root", "recent", "default", "unavailable"])(
- "SHARE30 首次导入与普通新任务一致:%s,保留 Root 正文且不更新 Recent",
- async (source) => {
- const root = createRoot(installMinimalDom());
- const workspacePath = "/import";
- const selection = {
- providerId: "provider",
- modelId: "model",
- options: { reasoningLevel: "high" },
- };
- const view = {
- ...modelSelectionView(1, source === "unavailable" ? [] : ["model"], "model"),
- preferredSelection: selection,
- };
- if (source !== "default" && source !== "unavailable")
- captureComposerRecentSubmission(workspacePath, {
- modelSelection: selection,
- mode: "yolo",
- })();
- if (source === "root" || source === "empty-root")
- persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
- text: "KEEP ROOT",
- editorStateJson: "ROOT EDITOR",
- mode: "plan",
- ...(source === "root" ? { modelSelection: selection } : {}),
- });
- const originalRoot = readV4ComposerDraft(workspacePath, undefined, "__draft__");
- const recentBefore = window.localStorage.getItem(
- `zcode-model-selection-recent-v1:${workspacePath}`,
- );
- activateImportedSession({ workspacePath, sessionId: "imported", reused: false });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: "imported" })));
- const imported = control!.draftConfig;
- expect(control!.composerDraft.text).toBe("");
- expect(control!.composerDraft.editorStateJson).toBeUndefined();
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toEqual(originalRoot);
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- // 导入仍保留空选择;回到从未选择模型的 Root 时,I24 允许补齐当前首选模型。
- expect(control!.draftConfig).toEqual(
- source === "empty-root"
- ? {
- ...imported,
- modelSelection: selection,
- provider: selection.providerId,
- model: selection.modelId,
- thought: selection.options.reasoningLevel,
- }
- : imported,
- );
- expect(window.localStorage.getItem(`zcode-model-selection-recent-v1:${workspacePath}`)).toBe(
- recentBefore,
- );
- act(() => root.unmount());
- },
- );
- it.each(["none", "mode", "model"])(
- "SHARE31 View 延迟不被空 snapshot 抢先锁定;用户先编辑=%s",
- async (edited) => {
- const root = createRoot(installMinimalDom());
- const workspacePath = "/delayed-import";
- activateImportedSession({ workspacePath, sessionId: "imported", reused: false });
- let control: ReturnType<typeof useDraftConfigControl>;
- const readyView = {
- ...modelSelectionView(1, ["model"], "model"),
- preferredSelection: {
- providerId: "provider",
- modelId: "model",
- options: { reasoningLevel: "high" },
- },
- };
- function Probe({ ready }: { ready: boolean }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: "imported",
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionView: ready ? readyView : null,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { ready: false })));
- expect(control!.composerDraft.mode).toBeUndefined();
- act(() => control!.updateComposerContent({ text: "content stays" }));
- if (edited === "mode") act(() => control!.handleDraftSwitchMode("plan"));
- if (edited === "model") {
- act(() => control!.handleDraftSelectModel("provider", "model"));
- act(() => control!.handleDraftSelectThought("low"));
- }
- await act(async () => root.render(createElement(Probe, { ready: true })));
- expect(control!.composerDraft.text).toBe("content stays");
- expect(control!.draftConfig.mode).toBe("build");
- expect(control!.draftConfig.planEnabled).toBe(edited === "mode");
- if (edited === "mode") expect(control!.composerDraft.modelSelection).toBeUndefined();
- else expect(control!.draftConfig.modelSelection?.modelId).toBe("model");
- if (edited === "model")
- expect(control!.draftConfig.modelSelection?.options?.reasoningLevel).toBe("low");
- expect(control!.composerDraft).not.toHaveProperty("initializeFromNewTask");
- act(() => root.unmount());
- },
- );
- it("SHARE32 使用导入实际 identity,重复导入/目标已有草稿不覆盖", () => {
- installMinimalDom();
- const workspacePath = "/same";
- persistV4ComposerDraft(workspacePath, "ssh://one/same", "__draft__", {
- text: "remote",
- mode: "yolo",
- });
- persistV4ComposerDraft(workspacePath, undefined, "__draft__", { text: "local", mode: "plan" });
- activateImportedSession({ workspacePath, sessionId: "new", reused: false });
- expect(readV4ComposerDraft(workspacePath, undefined, "new")).toBeNull();
- expect(readImportInitialization(workspacePath, undefined, "new")).toMatchObject({
- mode: "build",
- planEnabled: true,
- });
- expect(readV4ComposerDraft(workspacePath, "ssh://one/same", "new")).toBeNull();
- activateImportedSession({
- workspacePath,
- workspaceIdentity: "ssh://two/same",
- sessionId: "new",
- reused: false,
- });
- expect(readV4ComposerDraft(workspacePath, "ssh://two/same", "new")?.mode).toBeUndefined();
- persistV4ComposerDraft(workspacePath, undefined, "new", {
- text: "already edited",
- mode: "edit",
- });
- activateImportedSession({ workspacePath, sessionId: "new", reused: false });
- expect(readV4ComposerDraft(workspacePath, undefined, "new")).toMatchObject({
- text: "already edited",
- mode: "edit",
- });
- activateImportedSession({ workspacePath, sessionId: "history", reused: true });
- expect(readV4ComposerDraft(workspacePath, undefined, "history")).toBeNull();
- });
- beforeEach(() => {
- selectionServices.clear();
- prepareWorkspaceMock.mockReset();
- toastMock.mockReset();
- useZCodeSessionStore.setState((state) => ({
- ...state,
- workspaces: {},
- }));
- });
- afterEach(() => {
- vi.restoreAllMocks();
- delete (globalThis as { document?: unknown }).document;
- delete (globalThis as { window?: unknown }).window;
- delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
- });
- it("task → draft 继承模型目录但 slashCommands 为空时重新拉取并写入 identity 桶", async () => {
- const workspacePath = "/workspace";
- const workspaceIdentity = "ssh://demo/workspace";
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, WORKSPACE_PRESENTATION, workspaceIdentity);
- prepareWorkspaceMock.mockResolvedValue({
- configOptions: WORKSPACE_PRESENTATION,
- slashCommands: CUSTOM_COMMANDS,
- });
- function Probe() {
- useDraftConfigControl({
- workspacePath,
- workspaceIdentity,
- provider: "glm",
- sessionId: null,
- modelSelectionView: null,
- });
- return null;
- }
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(createElement(Probe));
- });
- await vi.waitFor(() => {
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .slashCommands,
- ).toEqual(CUSTOM_COMMANDS);
- });
- expect(prepareWorkspaceMock).toHaveBeenCalledWith(
- expect.objectContaining({ workspaceIdentity, workspacePath }),
- );
- act(() => root.unmount());
- });
- it("打开已有 session 且 workspace slashCommands 为空时独立补水合", async () => {
- const workspacePath = "/known-workspace";
- useZCodeSessionStore.getState().setConfigOptions(workspacePath, WORKSPACE_PRESENTATION);
- prepareWorkspaceMock.mockResolvedValue({
- configOptions: WORKSPACE_PRESENTATION,
- slashCommands: CUSTOM_COMMANDS,
- });
- function Probe() {
- useDraftConfigControl({
- workspacePath,
- provider: "glm",
- sessionId: "session-known",
- modelSelectionView: null,
- });
- return null;
- }
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(createElement(Probe));
- });
- await vi.waitFor(() => {
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).slashCommands,
- ).toEqual(CUSTOM_COMMANDS);
- });
- expect(prepareWorkspaceMock).toHaveBeenCalledWith(expect.objectContaining({ workspacePath }));
- act(() => root.unmount());
- });
- it("打开已有 session 时保留已经水合的 workspace slashCommands", async () => {
- const workspacePath = "/hydrated-workspace";
- const store = useZCodeSessionStore.getState();
- store.setConfigOptions(workspacePath, WORKSPACE_PRESENTATION);
- store.setSlashCommands(workspacePath, CUSTOM_COMMANDS);
- function Probe() {
- useDraftConfigControl({
- workspacePath,
- provider: "glm",
- sessionId: "session-known",
- modelSelectionView: null,
- });
- return null;
- }
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(createElement(Probe));
- });
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).slashCommands).toEqual(
- CUSTOM_COMMANDS,
- );
- expect(prepareWorkspaceMock).not.toHaveBeenCalled();
- act(() => root.unmount());
- });
- it("从 active task 新建草稿时不再从旧 workspace presentation 继承选择", async () => {
- const workspacePath = "/inherited-draft-workspace";
- const taskId = "task-custom-model";
- const inheritedOptions: ZCodeConfigOption[] = [
- {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue: "custom-provider/gpt-5.6-sol",
- options: [{ value: "custom-provider/gpt-5.6-sol", name: "GPT-5.6 Sol" }],
- },
- {
- id: "mode",
- name: "Mode",
- category: "mode",
- type: "select",
- currentValue: "yolo",
- options: [{ value: "yolo", name: "YOLO" }],
- },
- {
- id: "thought_level",
- name: "Thought",
- category: "thought_level",
- type: "select",
- currentValue: "max",
- options: [{ value: "max", name: "Max" }],
- },
- ];
- const store = useZCodeSessionStore.getState();
- store.setActiveTaskId(workspacePath, taskId);
- store.setTaskConfigOptions(workspacePath, taskId, inheritedOptions);
- store.startDraft(workspacePath, "glm");
- prepareWorkspaceMock.mockResolvedValue({
- configOptions: inheritedOptions,
- slashCommands: CUSTOM_COMMANDS,
- });
- let initialConfig: Partial<SessionConfigState> | undefined;
- function Probe() {
- const control = useDraftConfigControl({
- workspacePath,
- provider: "glm",
- sessionId: null,
- modelSelectionView: null,
- });
- initialConfig = control.resolveInitialDraftConfig();
- return null;
- }
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(createElement(Probe));
- });
- // 旧 task presentation 不再是新 Composer 的选择来源;View 未到达时不初始化。
- expect(initialConfig).toBeUndefined();
- act(() => root.unmount());
- });
- it("active session 切入草稿时,View 未 Ready 不提前用旧 task 配置初始化", async () => {
- const workspacePath = "/inherited-draft-render-transition";
- const taskId = "task-custom-model";
- const inheritedOptions: ZCodeConfigOption[] = [
- {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue: "custom-provider/gpt-5.6-sol",
- options: [{ value: "custom-provider/gpt-5.6-sol", name: "GPT-5.6 Sol" }],
- },
- {
- id: "mode",
- name: "Mode",
- category: "mode",
- type: "select",
- currentValue: "yolo",
- options: [{ value: "yolo", name: "YOLO" }],
- },
- {
- id: "thought_level",
- name: "Thought",
- category: "thought_level",
- type: "select",
- currentValue: "max",
- options: [{ value: "max", name: "Max" }],
- },
- ];
- const store = useZCodeSessionStore.getState();
- store.setConfigOptions(workspacePath, inheritedOptions);
- store.setActiveTaskId(workspacePath, taskId);
- store.setTaskConfigOptions(workspacePath, taskId, inheritedOptions);
- store.setSlashCommands(workspacePath, CUSTOM_COMMANDS);
- prepareWorkspaceMock.mockResolvedValue({
- configOptions: inheritedOptions,
- slashCommands: CUSTOM_COMMANDS,
- });
- const configsSeenDuringDraftRender: Array<Partial<SessionConfigState> | undefined> = [];
- function Probe({ sessionId }: { sessionId: string | null }) {
- const control = useDraftConfigControl({
- workspacePath,
- provider: "glm",
- sessionId,
- modelSelectionView: null,
- });
- if (sessionId === null) {
- configsSeenDuringDraftRender.push(control.resolveInitialDraftConfig());
- }
- return null;
- }
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(createElement(Probe, { sessionId: taskId }));
- });
- act(() => {
- useZCodeSessionStore.getState().startDraft(workspacePath, "glm");
- root.render(createElement(Probe, { sessionId: null }));
- });
- expect(configsSeenDuringDraftRender[0]).toBeUndefined();
- act(() => root.unmount());
- });
- it("Registry revision 只影响有效选择,正文和模式编辑保留原意图,配置恢复后重新有效", async () => {
- const workspacePath = "/live-selection-invalidated";
- const firstView = modelSelectionView(1, ["old", "preferred"], "preferred");
- const nextView = modelSelectionView(2, ["preferred"], "preferred");
- useZCodeSessionStore.getState().setSlashCommands(workspacePath, CUSTOM_COMMANDS);
- useZCodeSessionStore.getState().setConfigOptions(workspacePath, WORKSPACE_PRESENTATION);
- let control: ReturnType<typeof useDraftConfigControl> | undefined;
- function Probe({ view }: { view: ModelSelectionView }) {
- control = useDraftConfigControl({
- workspacePath,
- provider: "glm",
- sessionId: null,
- modelSelectionView: view,
- });
- return null;
- }
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(createElement(Probe, { view: firstView }));
- });
- act(() => control!.handleDraftSelectModel("provider", "old"));
- expect(control!.draftConfig.modelSelection).toEqual({
- providerId: "provider",
- modelId: "old",
- options: { reasoningLevel: "high" },
- });
- await act(async () => {
- root.render(createElement(Probe, { view: nextView }));
- });
- expect(control!.draftConfig.modelSelection).toBeUndefined();
- expect(control!.resolveInitialDraftConfig()?.modelSelection).toBeUndefined();
- act(() => control!.updateComposerContent({ text: "保留原选择" }));
- act(() => control!.handleDraftSwitchMode("plan"));
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toMatchObject({
- mode: "build",
- planEnabled: true,
- text: "保留原选择",
- modelSelection: { providerId: "provider", modelId: "old" },
- });
- await act(async () => {
- root.render(createElement(Probe, { view: { ...firstView, revision: 3 } }));
- });
- expect(control!.draftConfig.modelSelection).toEqual({
- providerId: "provider",
- modelId: "old",
- options: { reasoningLevel: "high" },
- });
- expect(toastMock).not.toHaveBeenCalled();
- act(() => root.unmount());
- });
- it("有效选择只在接纳时写回,迟到接纳不能覆盖新选择或另一个 scope", async () => {
- const workspacePath = "/effective-selection-commit";
- const root = createRoot(installMinimalDom());
- const original = {
- providerId: "old-account",
- modelId: "model",
- options: { reasoningLevel: "high" },
- };
- const effective = { ...original, providerId: "new-account" };
- persistV4ComposerDraft(workspacePath, undefined, "a", {
- text: "hello",
- mode: "build",
- modelSelection: original,
- });
- const service: IModelSelectionService = {
- getView: async (input) => ({
- revision: 1,
- providers: [],
- effectiveSelection:
- input?.selection?.providerId === "old-account" ? effective : (input?.selection ?? null),
- }),
- onDidChange: () => ({ dispose: () => {} }),
- };
- let control: ReturnType<typeof useActualDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string }) {
- control = useActualDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionService: service,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: "a" })));
- expect(control!.draftConfig.modelSelection).toEqual(effective);
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
- act(() => control!.updateComposerContent({ text: "draft" }));
- expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual(original);
- // 对象属性顺序不属于 Selection 语义,协议/序列化重建可能改变顺序。
- const accepted = control!.captureAcceptedModelSelection({
- options: effective.options,
- modelId: effective.modelId,
- providerId: effective.providerId,
- });
- act(() => control!.updateComposerContent({ text: "新正文" }));
- act(() => accepted());
- expect(readV4ComposerDraft(workspacePath, undefined, "a")).toMatchObject({
- text: "新正文",
- modelSelection: effective,
- });
- const recommended = { ...effective, providerId: "account:bigmodel-start-plan" };
- const acceptRecommendation = control!.captureAcceptedModelSelection(recommended, effective);
- expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual(effective);
- act(() => acceptRecommendation());
- expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual(recommended);
- const staleAccepted = control!.captureAcceptedModelSelection(recommended);
- await act(async () => control!.handleDraftSelectModel("personal", "another"));
- act(() => staleAccepted());
- expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual({
- providerId: "personal",
- modelId: "another",
- });
- const oldScopeAccepted = control!.captureAcceptedModelSelection(
- control!.draftConfig.modelSelection!,
- );
- await act(async () => root.render(createElement(Probe, { sessionId: "b" })));
- act(() => oldScopeAccepted());
- expect(readV4ComposerDraft(workspacePath, undefined, "b")?.modelSelection).toBeUndefined();
- act(() => root.unmount());
- });
- it("Session 只初始化一次,用户未发送的 mode/model 不被迟到 Snapshot 覆盖", async () => {
- const workspacePath = "/session-draft-seed";
- const view = modelSelectionView(1, ["old", "new"], "old");
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ config }: { config: Partial<SessionConfigState> | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: "session",
- sessionConfig: config,
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- const config = { mode: "yolo", modelSelection: view.preferredSelection! };
- await act(async () => {
- root.render(createElement(Probe, { config }));
- });
- expect(control!.draftConfig).toMatchObject(config);
- act(() => {
- control!.handleDraftSwitchMode("edit");
- control!.handleDraftSelectModel("provider", "new");
- });
- await act(async () => {
- root.render(createElement(Probe, { config: { ...config } }));
- });
- expect(control!.draftConfig).toMatchObject({
- mode: "edit",
- modelSelection: { modelId: "new" },
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "session")).toBeNull();
- act(() => root.unmount());
- });
- it("旧文本草稿等 Session 到达后只补新字段;Todo70 未绑定结果不会用 preferred 填满", async () => {
- const workspacePath = "/old-text-draft";
- const root = createRoot(installMinimalDom());
- persistV4ComposerDraft(workspacePath, undefined, "session", { text: "未发送正文" });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ config }: { config: Partial<SessionConfigState> | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: "session",
- sessionConfig: config,
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["old"], "old"),
- });
- return null;
- }
- await act(async () => {
- root.render(createElement(Probe, { config: null }));
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "session")?.mode).toBeUndefined();
- await act(async () => {
- root.render(createElement(Probe, { config: { mode: "build" } }));
- });
- expect(control!.draftConfig.modelSelection).toBeUndefined();
- expect(readV4ComposerDraft(workspacePath, undefined, "session")).toMatchObject({
- text: "未发送正文",
- mode: "build",
- });
- await act(async () => {
- root.render(
- createElement(Probe, {
- config: {
- mode: "yolo",
- modelSelection: modelSelectionView(1, ["old"], "old").preferredSelection!,
- },
- }),
- );
- });
- expect(control!.draftConfig).toMatchObject({ mode: "build" });
- expect(control!.draftConfig.modelSelection).toBeUndefined();
- act(() => root.unmount());
- });
- it("新 Root 继承最近接纳的权限,菜单编辑与旧 Snapshot 不覆盖独立草稿", async () => {
- const workspacePath = "/recent-mode";
- const root = createRoot(installMinimalDom());
- const view = modelSelectionView(1, ["model"], "model");
- captureComposerRecentSubmission(workspacePath, {
- modelSelection: view.preferredSelection!,
- mode: "yolo",
- })();
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "build", modelSelection: view.preferredSelection },
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- expect(control!.draftConfig.mode).toBe("yolo");
- act(() => {
- control!.handleDraftSwitchMode("plan");
- control!.updateComposerContent({ text: "root unsent" });
- });
- await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
- expect(control!.draftConfig.mode).toBe("build");
- act(() => {
- control!.handleDraftSwitchMode("edit");
- control!.updateComposerContent({ text: "session unsent" });
- });
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
- await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
- expect(control!.draftConfig.mode).toBe("build");
- expect(
- readComposerRecent(workspacePath, undefined, undefined, testScope(workspacePath)),
- ).toMatchObject({ mode: "yolo" });
- act(() => root.unmount());
- });
- it("目录重解析期间主动选模型仍使用同 Host 目录补齐思考", async () => {
- const workspacePath = "/select-during-reasoning-read";
- const root = createRoot(installMinimalDom());
- persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
- text: "draft",
- mode: "build",
- modelSelection: {
- providerId: "provider",
- modelId: "first",
- options: { reasoningLevel: "high" },
- },
- });
- let delayed = false;
- const service: IModelSelectionService = {
- getView: async (input) => {
- if (delayed) await new Promise(() => {});
- const view = modelSelectionView(1, ["first", "second"], "first");
- return { ...view, effectiveSelection: input?.selection ?? null };
- },
- onDidChange: () => ({ dispose() {} }),
- };
- let control: ReturnType<typeof useActualDraftConfigControl>;
- function Probe() {
- control = useActualDraftConfigControl({
- workspacePath,
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionService: service,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe)));
- delayed = true;
- await act(async () => control!.handleDraftSelectThought("low"));
- expect(control!.modelSelectionRead.state.status).toBe("loading");
- act(() => control!.handleDraftSelectModel("provider", "second"));
- expect(control!.composerDraft.modelSelection).toEqual({
- providerId: "provider",
- modelId: "second",
- options: { reasoningLevel: "high" },
- });
- act(() => root.unmount());
- });
- it("账号连接同步返回的模型和档位原子写入,不依赖旧 API 目录", async () => {
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe() {
- control = useDraftConfigControl({
- workspacePath: "/fresh-connection-selection",
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["api-model"], "api-model"),
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe)));
- act(() =>
- control!.handleDraftSelectModel("account:bigmodel-team-coding-plan", "GLM-5.3", "high"),
- );
- expect(control!.composerDraft.modelSelection).toEqual({
- providerId: "account:bigmodel-team-coding-plan",
- modelId: "GLM-5.3",
- options: { reasoningLevel: "high" },
- });
- act(() => root.unmount());
- });
- it("新草稿先保留 Recent 原意图,再交给当前 View 解析,不能在初始化时丢掉旧账号", async () => {
- const workspacePath = "/recent-account-intent";
- const root = createRoot(installMinimalDom());
- const original = {
- providerId: "old-account",
- modelId: "same",
- options: { reasoningLevel: "high" },
- };
- const effective = { ...original, providerId: "provider" };
- captureComposerRecentSubmission(workspacePath, { modelSelection: original, mode: "build" })();
- const service: IModelSelectionService = {
- getView: async (input) => ({
- ...modelSelectionView(1, ["same"], "same"),
- ...(input ? { effectiveSelection: input.selection ? effective : null } : {}),
- }),
- onDidChange: () => ({ dispose() {} }),
- };
- let control: ReturnType<typeof useActualDraftConfigControl>;
- function Probe() {
- control = useActualDraftConfigControl({
- workspacePath,
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionService: service,
- });
- return null;
- }
- await act(async () => {
- root.render(createElement(Probe));
- });
- expect(control!.draftConfig.modelSelection).toEqual(effective);
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
- act(() => control!.updateComposerContent({ text: "draft" }));
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.modelSelection).toEqual(
- original,
- );
- act(() => root.unmount());
- });
- it("Reasoning 失效只清档位,loading 不清选择,重挂载不补默认", async () => {
- const workspacePath = "/persisted-invalid-reasoning";
- const container = installMinimalDom();
- persistV4ComposerDraft(workspacePath, undefined, "session", {
- text: "draft",
- mode: "plan",
- modelSelection: {
- providerId: "provider",
- modelId: "old",
- options: { reasoningLevel: "deleted" },
- },
- });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ view }: { view: ModelSelectionView | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: "session",
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- let root = createRoot(container);
- await act(async () => {
- root.render(createElement(Probe, { view: null }));
- });
- expect(control!.draftConfig.modelSelection?.options?.reasoningLevel).toBe("deleted");
- const view = modelSelectionView(1, ["old"], "old");
- await act(async () => {
- root.render(createElement(Probe, { view }));
- });
- expect(control!.draftConfig.modelSelection).toEqual({ providerId: "provider", modelId: "old" });
- act(() => root.unmount());
- root = createRoot(container);
- await act(async () => {
- root.render(createElement(Probe, { view }));
- });
- expect(control!.draftConfig.modelSelection).toEqual({ providerId: "provider", modelId: "old" });
- expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
- expect(
- readV4ComposerDraft(workspacePath, undefined, "session")?.modelSelection?.options
- ?.reasoningLevel,
- ).toBe("deleted");
- expect(toastMock).not.toHaveBeenCalled();
- act(() => root.unmount());
- });
- it("延迟 Session seed 也校验档位,正文保存与选模共享完整草稿且 scope 隔离", async () => {
- const workspacePath = "/delayed-session";
- const root = createRoot(installMinimalDom());
- const view = modelSelectionView(1, ["old", "new"], "new");
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({
- sessionId,
- config,
- }: {
- sessionId: string;
- config: Partial<SessionConfigState> | null;
- }) {
- control = useDraftConfigControl({
- workspacePath,
- workspaceIdentity: "ssh://demo/workspace",
- sessionId,
- sessionConfig: config,
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- await act(async () => {
- root.render(createElement(Probe, { sessionId: "a", config: null }));
- });
- await act(async () => {
- root.render(
- createElement(Probe, {
- sessionId: "a",
- config: {
- mode: "yolo",
- modelSelection: {
- providerId: "provider",
- modelId: "old",
- options: { reasoningLevel: "deleted" },
- },
- },
- }),
- );
- });
- expect(control!.draftConfig.modelSelection).toEqual({ providerId: "provider", modelId: "old" });
- act(() => {
- control!.updateComposerContent({ text: "草稿 A" });
- control!.handleDraftSelectModel("provider", "new");
- });
- const staleContentCallback = control!.updateComposerContent;
- await act(async () => {
- root.render(createElement(Probe, { sessionId: "b", config: { mode: "plan" } }));
- });
- act(() => staleContentCallback({ text: "迟到 A" }));
- expect(control!.composerDraft).toMatchObject({ text: "", mode: "build", planEnabled: true });
- await act(async () => {
- root.render(createElement(Probe, { sessionId: "a", config: { mode: "build" } }));
- });
- expect(control!.composerDraft).toMatchObject({
- text: "草稿 A",
- mode: "build",
- modelSelection: {
- providerId: "provider",
- modelId: "new",
- },
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "a")).toBeNull();
- act(() => root.unmount());
- });
- it("新任务创建 Session 时先转移完整 Draft,再删除 Root scope", async () => {
- const workspacePath = "/promote-composer-draft";
- const root = createRoot(installMinimalDom());
- const view = modelSelectionView(1, ["model"], "model");
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe() {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionView: view,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe)));
- act(() => {
- control!.updateComposerContent({ text: "等待接纳的正文" });
- control!.handleDraftSwitchMode("yolo");
- control!.handleDraftSelectModel("provider", "model");
- control!.promoteComposerDraft("created-session");
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
- expect(readV4ComposerDraft(workspacePath, undefined, "created-session")).toMatchObject({
- text: "等待接纳的正文",
- mode: "yolo",
- modelSelection: {
- providerId: "provider",
- modelId: "model",
- },
- });
- act(() => root.unmount());
- });
- it("TD05 首发清空后仅内存交接配置,Snapshot 迟到不覆盖,离开后不遗留空草稿", async () => {
- const workspacePath = "/empty-promotion";
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["model"], "model"),
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- act(() => {
- control!.handleDraftSwitchMode("yolo");
- control!.promoteComposerDraft("created");
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "created")).toBeNull();
- await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
- expect(control!.draftConfig.mode).toBe("yolo");
- expect(readImportInitialization(workspacePath, undefined, "created")).toBeUndefined();
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
- expect(control!.draftConfig.mode).toBe("build");
- act(() => root.unmount());
- });
- it("目标 Session scope 写盘失败时保留 Root Draft", async () => {
- const workspacePath = "/promote-write-failure";
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe() {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["model"], "model"),
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe)));
- act(() => control!.updateComposerContent({ text: "必须保留" }));
- persistV4ComposerDraft(workspacePath, undefined, "__draft__", control!.composerDraft);
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.text).toBe("必须保留");
- const originalSetItem = window.localStorage.setItem.bind(window.localStorage);
- const failedWrites = vi.fn(() => {
- throw new Error("quota");
- });
- window.localStorage.setItem = failedWrites;
- act(() => control!.promoteComposerDraft("created-session"));
- window.localStorage.setItem = originalSetItem;
- expect(failedWrites).toHaveBeenCalledTimes(1);
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.text).toBe("必须保留");
- expect(readV4ComposerDraft(workspacePath, undefined, "created-session")).toBeNull();
- act(() => root.unmount());
- });
- it("PA158 授权按 scope 消费一次,保留 Plan/草稿;撤回编辑不重放旧授权", async () => {
- const root = createRoot(installMinimalDom());
- const workspacePath = "/permission-grant";
- persistV4ComposerDraft(workspacePath, undefined, "s", {
- text: "draft",
- mode: "edit",
- planEnabled: true,
- });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig:
- sessionId === "s"
- ? { mode: "yolo", planEnabled: false, permissionGrant: { interactionId: "p" } }
- : { mode: "build" },
- modelSelectionView: null,
- agentStartupAllowed: false,
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
- expect(control!.composerDraft).toMatchObject({
- text: "draft",
- mode: "yolo",
- planEnabled: false,
- });
- act(() => control!.handleDraftSwitchMode("build"));
- await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
- expect(control!.composerDraft.mode).toBe("build");
- act(() =>
- control!.replaceComposerDraft({ text: "later queue", mode: "edit", planEnabled: false }),
- );
- expect(control!.composerDraft).toMatchObject({
- text: "later queue",
- mode: "edit",
- planEnabled: false,
- });
- await act(async () => root.render(createElement(Probe, { sessionId: "other" })));
- expect(control!.composerDraft.mode).toBe("build");
- await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
- expect(control!.composerDraft.mode).toBe("yolo");
- act(() => root.unmount());
- });
- it.each([false, true])(
- "CS01/02/10 恢复当前权限/Plan,忽略历史授权,有文字=%s",
- async (hasText) => {
- const workspacePath = "/restore-current-config";
- const root = createRoot(installMinimalDom());
- if (hasText)
- persistV4ComposerDraft(workspacePath, undefined, "s", {
- text: "keep text",
- mode: "yolo",
- planEnabled: true,
- modelSelection: { providerId: "provider", modelId: "draft-model" },
- });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- agentStartupAllowed: false,
- modelSelectionView: null,
- sessionConfig: {
- mode: "build",
- planEnabled: false,
- modelSelection: { providerId: "provider", modelId: "session-model" },
- permissionGrant: { interactionId: "old" },
- planTransition: { toolCallId: "old", planEnabled: true },
- },
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
- expect(control!.composerDraft).toMatchObject({
- text: hasText ? "keep text" : "",
- mode: "build",
- planEnabled: false,
- modelSelection: { modelId: hasText ? "draft-model" : "session-model" },
- });
- act(() => {
- control!.handleDraftSwitchMode("yolo");
- control!.handleDraftSwitchMode("plan");
- });
- await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
- expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
- await act(async () => root.render(createElement(Probe, { sessionId: "other" })));
- await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
- expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
- const persisted = readV4ComposerDraft(workspacePath, undefined, "s");
- expect(persisted ?? {}).not.toHaveProperty("lastPermissionGrantId");
- expect(persisted ?? {}).not.toHaveProperty("lastPlanTransitionId");
- act(() => root.unmount());
- },
- );
- it("CS03/04 首帧迟到仍取最新权限,后续只响应新关联并使用最新快照值", async () => {
- const root = createRoot(installMinimalDom());
- const workspacePath = "/observe-config";
- persistV4ComposerDraft(workspacePath, undefined, "s", {
- text: "keep",
- mode: "yolo",
- planEnabled: true,
- });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ config }: { config: Partial<SessionConfigState> | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: "s",
- sessionConfig: config,
- agentStartupAllowed: false,
- modelSelectionView: null,
- });
- return null;
- }
- const config = {
- mode: "build",
- planEnabled: false,
- permissionGrant: { interactionId: "old" },
- planTransition: { toolCallId: "old", planEnabled: true },
- };
- await act(async () => root.render(createElement(Probe, { config: null })));
- await act(async () => root.render(createElement(Probe, { config })));
- expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
- act(() => control!.handleDraftSwitchMode("plan"));
- const granted = { ...config, mode: "yolo", permissionGrant: { interactionId: "new" } };
- await act(async () => root.render(createElement(Probe, { config: granted })));
- expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
- act(() => control!.handleDraftSwitchMode("edit"));
- await act(async () => root.render(createElement(Probe, { config: null })));
- await act(async () => root.render(createElement(Probe, { config: { ...granted } })));
- expect(control!.draftConfig.mode).toBe("edit");
- // 新关联到达时快照可能已包含之后的模式改变,不能硬编码重新执行 yolo/旧 Plan。
- const latest = {
- ...granted,
- mode: "build",
- planEnabled: false,
- permissionGrant: { interactionId: "newer" },
- planTransition: { toolCallId: "newer", planEnabled: true },
- };
- await act(async () => root.render(createElement(Probe, { config: latest })));
- expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
- expect(control!.composerDraft.text).toBe("keep");
- act(() => root.unmount());
- });
- it("CS06 New Task 在 React 提交前也使旧 promotion 失效", async () => {
- const workspacePath = "/promotion-generation";
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe() {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: null,
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["model"], "model"),
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe)));
- const oldPromotion = control!.promoteComposerDraft;
- act(() => {
- useZCodeSessionStore.getState().startDraft(workspacePath);
- persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
- text: "new page text",
- mode: "build",
- });
- oldPromotion("old-target");
- });
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.text).toBe("new page text");
- expect(readV4ComposerDraft(workspacePath, undefined, "old-target")).toBeNull();
- act(() => root.unmount());
- });
- it("CS06 未接收的首发配置不跨 pane 卸载残留", async () => {
- const workspacePath = "/promotion-unmounted";
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["model"], "model"),
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- act(() => {
- control!.handleDraftSwitchMode("yolo");
- control!.promoteComposerDraft("target");
- });
- const oldPromotion = control!.promoteComposerDraft;
- act(() => root.unmount());
- oldPromotion("late-target");
- const reopened = createRoot(installMinimalDom());
- await act(async () => reopened.render(createElement(Probe, { sessionId: "target" })));
- expect(control!.draftConfig.mode).toBe("build");
- act(() => reopened.unmount());
- });
- it("CS04/07 StrictMode 只接收当前导入参数,切走或新建即取消未接收参数", async () => {
- const workspacePath = "/scoped-import";
- const root = createRoot(installMinimalDom());
- const store = useZCodeSessionStore.getState();
- store.setActiveTaskId(workspacePath, "cancelled", undefined, { mode: "yolo" });
- store.setActiveTaskId(workspacePath, "other");
- expect(readImportInitialization(workspacePath, undefined, "cancelled")).toBeUndefined();
- store.setActiveTaskId(workspacePath, "cancelled", undefined, { mode: "yolo" });
- store.startDraft(workspacePath);
- expect(readImportInitialization(workspacePath, undefined, "cancelled")).toBeUndefined();
- store.setActiveTaskId(workspacePath, "imported", undefined, {
- mode: "yolo",
- planEnabled: true,
- });
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe() {
- control = useDraftConfigControl({
- workspacePath,
- sessionId: "imported",
- sessionConfig: {
- mode: "build",
- planEnabled: false,
- permissionGrant: { interactionId: "old" },
- },
- agentStartupAllowed: false,
- modelSelectionView: null,
- });
- return null;
- }
- await act(async () => root.render(createElement(StrictMode, null, createElement(Probe))));
- expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
- expect(readImportInitialization(workspacePath, undefined, "imported")).toBeUndefined();
- expect(readV4ComposerDraft(workspacePath, undefined, "imported")).toBeNull();
- act(() => root.unmount());
- });
- it("CS05 同一 pane 首发绑定保留后续新输入和选择,后续普通恢复才采用会话权限", async () => {
- const workspacePath = "/promote-live-edit";
- const root = createRoot(installMinimalDom());
- let control: ReturnType<typeof useDraftConfigControl>;
- function Probe({ sessionId }: { sessionId: string | null }) {
- control = useDraftConfigControl({
- workspacePath,
- sessionId,
- sessionConfig: { mode: "build" },
- agentStartupAllowed: false,
- modelSelectionView: modelSelectionView(1, ["model"], "model"),
- });
- return null;
- }
- await act(async () => root.render(createElement(Probe, { sessionId: null })));
- act(() => {
- control!.promoteComposerDraft("created");
- control!.updateComposerContent({ text: "typed after acceptance" });
- control!.handleDraftSwitchMode("yolo");
- });
- await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
- expect(control!.composerDraft).toMatchObject({ text: "typed after acceptance", mode: "yolo" });
- expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
- await act(async () => root.render(createElement(Probe, { sessionId: "other" })));
- await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
- expect(control!.composerDraft).toMatchObject({ text: "typed after acceptance", mode: "build" });
- act(() => root.unmount());
- });
- });
|