useDraftConfigControl.test.ts 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. import { act, createElement, StrictMode } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import type { ZCodeConfigOption } from "@zcode/shared";
  5. import type { SessionConfigState } from "@zcode/shared/zcode-protocol-v4";
  6. import type { IModelSelectionService, ModelSelectionView } from "@zcode/services";
  7. import { resolveEffectiveModelSelection, type ProviderRegistryView } from "@zcode/provider";
  8. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  9. import { useDraftConfigControl as useActualDraftConfigControl } from "@/v4/composer/useDraftConfigControl.js";
  10. import {
  11. persistV4ComposerDraft as persistScopedDraft,
  12. readV4ComposerDraft as readScopedDraft,
  13. resolveComposerScope,
  14. } from "@/v4/composer/composerDraftStore.js";
  15. import {
  16. captureComposerRecentSubmission as captureScopedRecent,
  17. readComposerRecent,
  18. } from "@/lib/composerRecent.js";
  19. function testScope(path: string, identity?: string) {
  20. return resolveComposerScope(
  21. useZCodeSessionStore.getState().getWorkspaceState(path, identity).draftProjectId,
  22. identity,
  23. );
  24. }
  25. function persistV4ComposerDraft(...args: Parameters<typeof persistScopedDraft>) {
  26. return persistScopedDraft(
  27. args[0],
  28. args[1],
  29. args[2],
  30. args[3],
  31. args[4] ?? testScope(args[0], args[1]),
  32. );
  33. }
  34. function readV4ComposerDraft(...args: Parameters<typeof readScopedDraft>) {
  35. return readScopedDraft(args[0], args[1], args[2], args[3] ?? testScope(args[0], args[1]));
  36. }
  37. function captureComposerRecentSubmission(...args: Parameters<typeof captureScopedRecent>) {
  38. return captureScopedRecent(
  39. args[0],
  40. args[1],
  41. args[2],
  42. args[3],
  43. args[4] ?? testScope(args[0], args[2]),
  44. );
  45. }
  46. import { resolveImportedComposerConfig } from "@/v4/composer/newTaskDraft.js";
  47. function activateImportedSession(result: Parameters<typeof resolveImportedComposerConfig>[0]) {
  48. useZCodeSessionStore
  49. .getState()
  50. .setActiveTaskId(
  51. result.workspacePath,
  52. result.sessionId,
  53. result.workspaceIdentity,
  54. resolveImportedComposerConfig(result),
  55. );
  56. }
  57. function readImportInitialization(
  58. workspacePath: string,
  59. workspaceIdentity: string | undefined,
  60. taskId: string,
  61. ) {
  62. const value = useZCodeSessionStore
  63. .getState()
  64. .getWorkspaceState(workspacePath, workspaceIdentity).composerInitialization;
  65. return value?.taskId === taskId ? value.config : undefined;
  66. }
  67. const { prepareWorkspaceMock } = vi.hoisted(() => ({
  68. prepareWorkspaceMock: vi.fn(),
  69. }));
  70. const { toastMock } = vi.hoisted(() => ({
  71. toastMock: vi.fn(),
  72. }));
  73. vi.mock("@/hooks/useWorkspacePrepare.js", () => ({
  74. prepareWorkspaceWithZCodeSessionService: prepareWorkspaceMock,
  75. }));
  76. vi.mock("@/hooks/useZCodeSessionService.js", () => ({
  77. useZCodeSessionService: () => ({ readWorkspacePresentation: vi.fn() }),
  78. }));
  79. vi.mock("@/hooks/useSettingService.js", () => ({
  80. useSettings: () => ({ settings: null }),
  81. }));
  82. vi.mock("@/components/ui/toast.js", () => ({
  83. toast: toastMock,
  84. }));
  85. vi.mock("@/i18n/IntlProvider.js", () => ({
  86. useZCodeIntl: () => ({
  87. intl: { formatMessage: ({ id }: { id: string }) => id },
  88. }),
  89. }));
  90. vi.mock("@/logger.js", () => ({
  91. logger: {
  92. debug: vi.fn(),
  93. info: vi.fn(),
  94. warn: vi.fn(),
  95. },
  96. }));
  97. const WORKSPACE_PRESENTATION: ZCodeConfigOption[] = [
  98. {
  99. id: "mode",
  100. name: "Mode",
  101. category: "mode",
  102. type: "select",
  103. currentValue: "build",
  104. options: [{ value: "build", name: "Build" }],
  105. },
  106. ];
  107. const CUSTOM_COMMANDS = [
  108. {
  109. name: "review",
  110. description: "Review current changes",
  111. source: "custom" as const,
  112. },
  113. ];
  114. const selectionServices = new Map<string, IModelSelectionService>();
  115. // 用真实纯解析算法模拟 Host,只省略 RPC;同内容的 fixture 保持同一 Service owner。
  116. function useDraftConfigControl(
  117. params: Omit<Parameters<typeof useActualDraftConfigControl>[0], "modelSelectionService"> & {
  118. modelSelectionView: ModelSelectionView | null;
  119. },
  120. ) {
  121. const view = params.modelSelectionView;
  122. const key = JSON.stringify(view);
  123. if (view && !selectionServices.has(key))
  124. selectionServices.set(key, {
  125. getView: async (input) => ({
  126. ...view,
  127. ...(input
  128. ? resolveEffectiveModelSelection({
  129. selection: input.selection,
  130. registry: view as unknown as ProviderRegistryView,
  131. classifyProvider: () => "ordinary",
  132. })
  133. : {}),
  134. }),
  135. onDidChange: () => ({ dispose: () => {} }),
  136. });
  137. return useActualDraftConfigControl({
  138. ...params,
  139. modelSelectionService: view ? selectionServices.get(key)! : null,
  140. });
  141. }
  142. function modelSelectionView(
  143. revision: number,
  144. modelIds: readonly string[],
  145. preferredModelId: string,
  146. ): ModelSelectionView {
  147. return {
  148. revision,
  149. preferredSelection: {
  150. providerId: "provider",
  151. modelId: preferredModelId,
  152. },
  153. providers: [
  154. {
  155. providerId: "provider",
  156. config: {},
  157. models: modelIds.map((modelId) => ({
  158. modelId,
  159. config: {
  160. optionSpecs: {
  161. reasoningLevel: { values: ["low", "high"], map: "{}" },
  162. },
  163. },
  164. })),
  165. },
  166. ],
  167. } as ModelSelectionView;
  168. }
  169. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  170. const element = {
  171. addEventListener: () => {},
  172. appendChild: (child: { parentNode?: unknown }) => {
  173. child.parentNode = element;
  174. return child;
  175. },
  176. childNodes: [] as unknown[],
  177. getAttribute: () => null,
  178. insertBefore: (child: { parentNode?: unknown }) => {
  179. child.parentNode = element;
  180. return child;
  181. },
  182. nodeName: tagName.toUpperCase(),
  183. nodeType: 1,
  184. ownerDocument,
  185. parentNode: null as unknown,
  186. removeAttribute: () => {},
  187. removeChild: (child: { parentNode?: unknown }) => {
  188. child.parentNode = null;
  189. return child;
  190. },
  191. removeEventListener: () => {},
  192. setAttribute: () => {},
  193. style: {},
  194. tagName: tagName.toUpperCase(),
  195. };
  196. return element as unknown as Element;
  197. }
  198. function installMinimalDom() {
  199. const storage = new Map<string, string>();
  200. const documentMock = {
  201. addEventListener: () => {},
  202. createElement: (tagName: string) =>
  203. createMinimalElement(documentMock as unknown as Document, tagName),
  204. createTextNode: (nodeValue: string) => ({
  205. nodeType: 3,
  206. nodeValue,
  207. ownerDocument: documentMock,
  208. parentNode: null,
  209. }),
  210. nodeType: 9,
  211. removeEventListener: () => {},
  212. } as unknown as Document;
  213. const windowMock = {
  214. localStorage: {
  215. getItem: (key: string) => storage.get(key) ?? null,
  216. setItem: (key: string, value: string) => storage.set(key, value),
  217. removeItem: (key: string) => storage.delete(key),
  218. },
  219. addEventListener: () => {},
  220. document: documentMock,
  221. HTMLIFrameElement: function HTMLIFrameElement() {},
  222. HTMLElement: function HTMLElement() {},
  223. Node: function Node() {},
  224. removeEventListener: () => {},
  225. };
  226. Object.defineProperty(globalThis, "document", {
  227. configurable: true,
  228. value: documentMock,
  229. });
  230. Object.defineProperty(globalThis, "window", {
  231. configurable: true,
  232. value: windowMock,
  233. });
  234. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  235. configurable: true,
  236. value: true,
  237. });
  238. return createMinimalElement(documentMock);
  239. }
  240. describe("useDraftConfigControl slash command hydration", () => {
  241. it("PD03 同目录切项目恢复各自正文;旧 Hook 回调不能写新项目", async () => {
  242. const workspacePath = "/shared-project-root";
  243. const root = createRoot(installMinimalDom());
  244. const view = modelSelectionView(1, ["model"], "model");
  245. let control: ReturnType<typeof useDraftConfigControl> | undefined;
  246. function Probe() {
  247. control = useDraftConfigControl({
  248. workspacePath,
  249. sessionId: null,
  250. modelSelectionView: view,
  251. agentStartupAllowed: false,
  252. });
  253. return null;
  254. }
  255. act(() =>
  256. useZCodeSessionStore
  257. .getState()
  258. .startDraft(workspacePath, undefined, undefined, { projectId: "p" }),
  259. );
  260. await act(async () => root.render(createElement(Probe)));
  261. act(() => {
  262. control!.updateComposerContent({ text: "P" });
  263. control!.handleDraftSwitchMode("yolo");
  264. });
  265. const stale = control!.updateComposerContent;
  266. act(() =>
  267. useZCodeSessionStore
  268. .getState()
  269. .startDraft(workspacePath, undefined, undefined, { projectId: "q" }),
  270. );
  271. expect(control!.composerDraft.text).toBe("");
  272. act(() => {
  273. control!.updateComposerContent({ text: "Q" });
  274. stale({ text: "late P" });
  275. });
  276. expect(control!.composerDraft.text).toBe("Q");
  277. act(() =>
  278. useZCodeSessionStore
  279. .getState()
  280. .startDraft(workspacePath, undefined, undefined, { projectId: "p" }),
  281. );
  282. expect(control!.composerDraft.text).toBe("P");
  283. expect(control!.draftConfig.mode).toBe("yolo");
  284. act(() => root.unmount());
  285. });
  286. it("TD02/04 两次提交后重开空白页继承 B,重复 New Task 不保留空配置", async () => {
  287. const workspacePath = "/text-only-regression";
  288. const root = createRoot(installMinimalDom());
  289. const view = modelSelectionView(1, ["A", "B"], "A");
  290. let control: ReturnType<typeof useDraftConfigControl>;
  291. function Probe({ sessionId }: { sessionId: string | null }) {
  292. control = useDraftConfigControl({
  293. workspacePath,
  294. sessionId,
  295. sessionConfig: { mode: "yolo", modelSelection: view.preferredSelection },
  296. agentStartupAllowed: false,
  297. modelSelectionView: view,
  298. });
  299. return null;
  300. }
  301. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  302. await act(async () => {
  303. control!.handleDraftSwitchMode("yolo");
  304. control!.handleDraftSelectModel("provider", "A");
  305. });
  306. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
  307. act(() =>
  308. captureComposerRecentSubmission(workspacePath, {
  309. mode: "yolo",
  310. modelSelection: control!.draftConfig.modelSelection!,
  311. })(),
  312. );
  313. await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
  314. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  315. await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
  316. await act(async () => {
  317. control!.handleDraftSwitchMode("build");
  318. control!.handleDraftSelectModel("provider", "B");
  319. });
  320. act(() =>
  321. captureComposerRecentSubmission(workspacePath, {
  322. mode: "build",
  323. modelSelection: control!.draftConfig.modelSelection!,
  324. })(),
  325. );
  326. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  327. expect(control!.draftConfig).toMatchObject({ mode: "build", model: "B" });
  328. act(() => control!.handleDraftSwitchMode("yolo"));
  329. await act(async () => useZCodeSessionStore.getState().startDraft(workspacePath));
  330. expect(control!.draftConfig).toMatchObject({ mode: "build", model: "B" });
  331. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
  332. act(() => root.unmount());
  333. });
  334. it.each([undefined, "remote:provider-readiness"])(
  335. "I24 首个 Provider 就绪只补齐未选择模型的 Root 草稿:%s",
  336. async (workspaceIdentity) => {
  337. const workspacePath = "/provider-readiness";
  338. const root = createRoot(installMinimalDom());
  339. const emptyView = { ...modelSelectionView(1, [], ""), preferredSelection: null };
  340. const readyView = {
  341. ...modelSelectionView(2, ["model"], "model"),
  342. preferredSelection: {
  343. providerId: "provider",
  344. modelId: "model",
  345. options: { reasoningLevel: "high" },
  346. },
  347. };
  348. let control: ReturnType<typeof useDraftConfigControl>;
  349. function Probe({ view }: { view: ModelSelectionView }) {
  350. control = useDraftConfigControl({
  351. workspacePath,
  352. workspaceIdentity,
  353. sessionId: null,
  354. agentStartupAllowed: false,
  355. modelSelectionView: view,
  356. });
  357. return null;
  358. }
  359. await act(async () => root.render(createElement(Probe, { view: emptyView })));
  360. act(() => {
  361. control!.updateComposerContent({ text: "KEEP DRAFT", editorStateJson: "EDITOR" });
  362. control!.handleDraftSwitchMode("plan");
  363. });
  364. const before = control!.composerDraft;
  365. expect(before.modelSelection).toBeUndefined();
  366. await act(async () => root.render(createElement(Probe, { view: readyView })));
  367. expect(control!.draftConfig.modelSelection).toEqual(readyView.preferredSelection);
  368. expect(control!.composerDraft).toMatchObject({
  369. text: before.text,
  370. editorStateJson: before.editorStateJson,
  371. mode: before.mode,
  372. planEnabled: before.planEnabled,
  373. });
  374. expect(
  375. readV4ComposerDraft(workspacePath, workspaceIdentity, "__draft__")?.modelSelection,
  376. ).toEqual(readyView.preferredSelection);
  377. // 同一路径的另一远端身份没有被写入;补选不冒充已接纳提交更新 Recent。
  378. expect(readV4ComposerDraft(workspacePath, "remote:other", "__draft__")).toBeNull();
  379. expect(
  380. window.localStorage.getItem(
  381. `zcode-model-selection-recent-v1:${workspaceIdentity ?? workspacePath}`,
  382. ),
  383. ).toBeNull();
  384. act(() => root.unmount());
  385. },
  386. );
  387. it.each(["chosen", "invalid", "session"])(
  388. "I24 目录就绪不替换已有意图或 Session:%s",
  389. async (scenario) => {
  390. const workspacePath = "/provider-readiness-excluded";
  391. const root = createRoot(installMinimalDom());
  392. const sessionId = scenario === "session" ? "existing" : null;
  393. const selection = {
  394. providerId: "provider",
  395. modelId: scenario === "invalid" ? "removed" : "chosen",
  396. options: { reasoningLevel: "low" },
  397. };
  398. persistV4ComposerDraft(workspacePath, undefined, sessionId ?? "__draft__", {
  399. text: "KEEP INTENT",
  400. mode: "build",
  401. planEnabled: true,
  402. ...(scenario === "session" ? {} : { modelSelection: selection }),
  403. });
  404. let control: ReturnType<typeof useDraftConfigControl>;
  405. function Probe({ ready }: { ready: boolean }) {
  406. control = useDraftConfigControl({
  407. workspacePath,
  408. sessionId,
  409. agentStartupAllowed: false,
  410. sessionConfig: sessionId ? { mode: "build" } : null,
  411. modelSelectionView: {
  412. ...modelSelectionView(ready ? 2 : 1, ready ? ["chosen", "default"] : [], "default"),
  413. preferredSelection: ready
  414. ? {
  415. providerId: "provider",
  416. modelId: "default",
  417. options: { reasoningLevel: "high" },
  418. }
  419. : null,
  420. },
  421. });
  422. return null;
  423. }
  424. await act(async () => root.render(createElement(Probe, { ready: false })));
  425. await act(async () => root.render(createElement(Probe, { ready: true })));
  426. expect(control!.composerDraft.modelSelection).toEqual(
  427. scenario === "session" ? undefined : selection,
  428. );
  429. expect(control!.draftConfig.modelSelection).toEqual(
  430. scenario === "chosen" ? selection : undefined,
  431. );
  432. expect(control!.composerDraft.text).toBe("KEEP INTENT");
  433. act(() => root.unmount());
  434. },
  435. );
  436. it.each(["root", "empty-root", "recent", "default", "unavailable"])(
  437. "SHARE30 首次导入与普通新任务一致:%s,保留 Root 正文且不更新 Recent",
  438. async (source) => {
  439. const root = createRoot(installMinimalDom());
  440. const workspacePath = "/import";
  441. const selection = {
  442. providerId: "provider",
  443. modelId: "model",
  444. options: { reasoningLevel: "high" },
  445. };
  446. const view = {
  447. ...modelSelectionView(1, source === "unavailable" ? [] : ["model"], "model"),
  448. preferredSelection: selection,
  449. };
  450. if (source !== "default" && source !== "unavailable")
  451. captureComposerRecentSubmission(workspacePath, {
  452. modelSelection: selection,
  453. mode: "yolo",
  454. })();
  455. if (source === "root" || source === "empty-root")
  456. persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
  457. text: "KEEP ROOT",
  458. editorStateJson: "ROOT EDITOR",
  459. mode: "plan",
  460. ...(source === "root" ? { modelSelection: selection } : {}),
  461. });
  462. const originalRoot = readV4ComposerDraft(workspacePath, undefined, "__draft__");
  463. const recentBefore = window.localStorage.getItem(
  464. `zcode-model-selection-recent-v1:${workspacePath}`,
  465. );
  466. activateImportedSession({ workspacePath, sessionId: "imported", reused: false });
  467. let control: ReturnType<typeof useDraftConfigControl>;
  468. function Probe({ sessionId }: { sessionId: string | null }) {
  469. control = useDraftConfigControl({
  470. workspacePath,
  471. sessionId,
  472. sessionConfig: { mode: "build" },
  473. agentStartupAllowed: false,
  474. modelSelectionView: view,
  475. });
  476. return null;
  477. }
  478. await act(async () => root.render(createElement(Probe, { sessionId: "imported" })));
  479. const imported = control!.draftConfig;
  480. expect(control!.composerDraft.text).toBe("");
  481. expect(control!.composerDraft.editorStateJson).toBeUndefined();
  482. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toEqual(originalRoot);
  483. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  484. // 导入仍保留空选择;回到从未选择模型的 Root 时,I24 允许补齐当前首选模型。
  485. expect(control!.draftConfig).toEqual(
  486. source === "empty-root"
  487. ? {
  488. ...imported,
  489. modelSelection: selection,
  490. provider: selection.providerId,
  491. model: selection.modelId,
  492. thought: selection.options.reasoningLevel,
  493. }
  494. : imported,
  495. );
  496. expect(window.localStorage.getItem(`zcode-model-selection-recent-v1:${workspacePath}`)).toBe(
  497. recentBefore,
  498. );
  499. act(() => root.unmount());
  500. },
  501. );
  502. it.each(["none", "mode", "model"])(
  503. "SHARE31 View 延迟不被空 snapshot 抢先锁定;用户先编辑=%s",
  504. async (edited) => {
  505. const root = createRoot(installMinimalDom());
  506. const workspacePath = "/delayed-import";
  507. activateImportedSession({ workspacePath, sessionId: "imported", reused: false });
  508. let control: ReturnType<typeof useDraftConfigControl>;
  509. const readyView = {
  510. ...modelSelectionView(1, ["model"], "model"),
  511. preferredSelection: {
  512. providerId: "provider",
  513. modelId: "model",
  514. options: { reasoningLevel: "high" },
  515. },
  516. };
  517. function Probe({ ready }: { ready: boolean }) {
  518. control = useDraftConfigControl({
  519. workspacePath,
  520. sessionId: "imported",
  521. sessionConfig: { mode: "build" },
  522. agentStartupAllowed: false,
  523. modelSelectionView: ready ? readyView : null,
  524. });
  525. return null;
  526. }
  527. await act(async () => root.render(createElement(Probe, { ready: false })));
  528. expect(control!.composerDraft.mode).toBeUndefined();
  529. act(() => control!.updateComposerContent({ text: "content stays" }));
  530. if (edited === "mode") act(() => control!.handleDraftSwitchMode("plan"));
  531. if (edited === "model") {
  532. act(() => control!.handleDraftSelectModel("provider", "model"));
  533. act(() => control!.handleDraftSelectThought("low"));
  534. }
  535. await act(async () => root.render(createElement(Probe, { ready: true })));
  536. expect(control!.composerDraft.text).toBe("content stays");
  537. expect(control!.draftConfig.mode).toBe("build");
  538. expect(control!.draftConfig.planEnabled).toBe(edited === "mode");
  539. if (edited === "mode") expect(control!.composerDraft.modelSelection).toBeUndefined();
  540. else expect(control!.draftConfig.modelSelection?.modelId).toBe("model");
  541. if (edited === "model")
  542. expect(control!.draftConfig.modelSelection?.options?.reasoningLevel).toBe("low");
  543. expect(control!.composerDraft).not.toHaveProperty("initializeFromNewTask");
  544. act(() => root.unmount());
  545. },
  546. );
  547. it("SHARE32 使用导入实际 identity,重复导入/目标已有草稿不覆盖", () => {
  548. installMinimalDom();
  549. const workspacePath = "/same";
  550. persistV4ComposerDraft(workspacePath, "ssh://one/same", "__draft__", {
  551. text: "remote",
  552. mode: "yolo",
  553. });
  554. persistV4ComposerDraft(workspacePath, undefined, "__draft__", { text: "local", mode: "plan" });
  555. activateImportedSession({ workspacePath, sessionId: "new", reused: false });
  556. expect(readV4ComposerDraft(workspacePath, undefined, "new")).toBeNull();
  557. expect(readImportInitialization(workspacePath, undefined, "new")).toMatchObject({
  558. mode: "build",
  559. planEnabled: true,
  560. });
  561. expect(readV4ComposerDraft(workspacePath, "ssh://one/same", "new")).toBeNull();
  562. activateImportedSession({
  563. workspacePath,
  564. workspaceIdentity: "ssh://two/same",
  565. sessionId: "new",
  566. reused: false,
  567. });
  568. expect(readV4ComposerDraft(workspacePath, "ssh://two/same", "new")?.mode).toBeUndefined();
  569. persistV4ComposerDraft(workspacePath, undefined, "new", {
  570. text: "already edited",
  571. mode: "edit",
  572. });
  573. activateImportedSession({ workspacePath, sessionId: "new", reused: false });
  574. expect(readV4ComposerDraft(workspacePath, undefined, "new")).toMatchObject({
  575. text: "already edited",
  576. mode: "edit",
  577. });
  578. activateImportedSession({ workspacePath, sessionId: "history", reused: true });
  579. expect(readV4ComposerDraft(workspacePath, undefined, "history")).toBeNull();
  580. });
  581. beforeEach(() => {
  582. selectionServices.clear();
  583. prepareWorkspaceMock.mockReset();
  584. toastMock.mockReset();
  585. useZCodeSessionStore.setState((state) => ({
  586. ...state,
  587. workspaces: {},
  588. }));
  589. });
  590. afterEach(() => {
  591. vi.restoreAllMocks();
  592. delete (globalThis as { document?: unknown }).document;
  593. delete (globalThis as { window?: unknown }).window;
  594. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  595. });
  596. it("task → draft 继承模型目录但 slashCommands 为空时重新拉取并写入 identity 桶", async () => {
  597. const workspacePath = "/workspace";
  598. const workspaceIdentity = "ssh://demo/workspace";
  599. useZCodeSessionStore
  600. .getState()
  601. .setConfigOptions(workspacePath, WORKSPACE_PRESENTATION, workspaceIdentity);
  602. prepareWorkspaceMock.mockResolvedValue({
  603. configOptions: WORKSPACE_PRESENTATION,
  604. slashCommands: CUSTOM_COMMANDS,
  605. });
  606. function Probe() {
  607. useDraftConfigControl({
  608. workspacePath,
  609. workspaceIdentity,
  610. provider: "glm",
  611. sessionId: null,
  612. modelSelectionView: null,
  613. });
  614. return null;
  615. }
  616. const root: Root = createRoot(installMinimalDom());
  617. await act(async () => {
  618. root.render(createElement(Probe));
  619. });
  620. await vi.waitFor(() => {
  621. expect(
  622. useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
  623. .slashCommands,
  624. ).toEqual(CUSTOM_COMMANDS);
  625. });
  626. expect(prepareWorkspaceMock).toHaveBeenCalledWith(
  627. expect.objectContaining({ workspaceIdentity, workspacePath }),
  628. );
  629. act(() => root.unmount());
  630. });
  631. it("打开已有 session 且 workspace slashCommands 为空时独立补水合", async () => {
  632. const workspacePath = "/known-workspace";
  633. useZCodeSessionStore.getState().setConfigOptions(workspacePath, WORKSPACE_PRESENTATION);
  634. prepareWorkspaceMock.mockResolvedValue({
  635. configOptions: WORKSPACE_PRESENTATION,
  636. slashCommands: CUSTOM_COMMANDS,
  637. });
  638. function Probe() {
  639. useDraftConfigControl({
  640. workspacePath,
  641. provider: "glm",
  642. sessionId: "session-known",
  643. modelSelectionView: null,
  644. });
  645. return null;
  646. }
  647. const root: Root = createRoot(installMinimalDom());
  648. await act(async () => {
  649. root.render(createElement(Probe));
  650. });
  651. await vi.waitFor(() => {
  652. expect(
  653. useZCodeSessionStore.getState().getWorkspaceState(workspacePath).slashCommands,
  654. ).toEqual(CUSTOM_COMMANDS);
  655. });
  656. expect(prepareWorkspaceMock).toHaveBeenCalledWith(expect.objectContaining({ workspacePath }));
  657. act(() => root.unmount());
  658. });
  659. it("打开已有 session 时保留已经水合的 workspace slashCommands", async () => {
  660. const workspacePath = "/hydrated-workspace";
  661. const store = useZCodeSessionStore.getState();
  662. store.setConfigOptions(workspacePath, WORKSPACE_PRESENTATION);
  663. store.setSlashCommands(workspacePath, CUSTOM_COMMANDS);
  664. function Probe() {
  665. useDraftConfigControl({
  666. workspacePath,
  667. provider: "glm",
  668. sessionId: "session-known",
  669. modelSelectionView: null,
  670. });
  671. return null;
  672. }
  673. const root: Root = createRoot(installMinimalDom());
  674. await act(async () => {
  675. root.render(createElement(Probe));
  676. });
  677. expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).slashCommands).toEqual(
  678. CUSTOM_COMMANDS,
  679. );
  680. expect(prepareWorkspaceMock).not.toHaveBeenCalled();
  681. act(() => root.unmount());
  682. });
  683. it("从 active task 新建草稿时不再从旧 workspace presentation 继承选择", async () => {
  684. const workspacePath = "/inherited-draft-workspace";
  685. const taskId = "task-custom-model";
  686. const inheritedOptions: ZCodeConfigOption[] = [
  687. {
  688. id: "model",
  689. name: "Model",
  690. category: "model",
  691. type: "select",
  692. currentValue: "custom-provider/gpt-5.6-sol",
  693. options: [{ value: "custom-provider/gpt-5.6-sol", name: "GPT-5.6 Sol" }],
  694. },
  695. {
  696. id: "mode",
  697. name: "Mode",
  698. category: "mode",
  699. type: "select",
  700. currentValue: "yolo",
  701. options: [{ value: "yolo", name: "YOLO" }],
  702. },
  703. {
  704. id: "thought_level",
  705. name: "Thought",
  706. category: "thought_level",
  707. type: "select",
  708. currentValue: "max",
  709. options: [{ value: "max", name: "Max" }],
  710. },
  711. ];
  712. const store = useZCodeSessionStore.getState();
  713. store.setActiveTaskId(workspacePath, taskId);
  714. store.setTaskConfigOptions(workspacePath, taskId, inheritedOptions);
  715. store.startDraft(workspacePath, "glm");
  716. prepareWorkspaceMock.mockResolvedValue({
  717. configOptions: inheritedOptions,
  718. slashCommands: CUSTOM_COMMANDS,
  719. });
  720. let initialConfig: Partial<SessionConfigState> | undefined;
  721. function Probe() {
  722. const control = useDraftConfigControl({
  723. workspacePath,
  724. provider: "glm",
  725. sessionId: null,
  726. modelSelectionView: null,
  727. });
  728. initialConfig = control.resolveInitialDraftConfig();
  729. return null;
  730. }
  731. const root: Root = createRoot(installMinimalDom());
  732. await act(async () => {
  733. root.render(createElement(Probe));
  734. });
  735. // 旧 task presentation 不再是新 Composer 的选择来源;View 未到达时不初始化。
  736. expect(initialConfig).toBeUndefined();
  737. act(() => root.unmount());
  738. });
  739. it("active session 切入草稿时,View 未 Ready 不提前用旧 task 配置初始化", async () => {
  740. const workspacePath = "/inherited-draft-render-transition";
  741. const taskId = "task-custom-model";
  742. const inheritedOptions: ZCodeConfigOption[] = [
  743. {
  744. id: "model",
  745. name: "Model",
  746. category: "model",
  747. type: "select",
  748. currentValue: "custom-provider/gpt-5.6-sol",
  749. options: [{ value: "custom-provider/gpt-5.6-sol", name: "GPT-5.6 Sol" }],
  750. },
  751. {
  752. id: "mode",
  753. name: "Mode",
  754. category: "mode",
  755. type: "select",
  756. currentValue: "yolo",
  757. options: [{ value: "yolo", name: "YOLO" }],
  758. },
  759. {
  760. id: "thought_level",
  761. name: "Thought",
  762. category: "thought_level",
  763. type: "select",
  764. currentValue: "max",
  765. options: [{ value: "max", name: "Max" }],
  766. },
  767. ];
  768. const store = useZCodeSessionStore.getState();
  769. store.setConfigOptions(workspacePath, inheritedOptions);
  770. store.setActiveTaskId(workspacePath, taskId);
  771. store.setTaskConfigOptions(workspacePath, taskId, inheritedOptions);
  772. store.setSlashCommands(workspacePath, CUSTOM_COMMANDS);
  773. prepareWorkspaceMock.mockResolvedValue({
  774. configOptions: inheritedOptions,
  775. slashCommands: CUSTOM_COMMANDS,
  776. });
  777. const configsSeenDuringDraftRender: Array<Partial<SessionConfigState> | undefined> = [];
  778. function Probe({ sessionId }: { sessionId: string | null }) {
  779. const control = useDraftConfigControl({
  780. workspacePath,
  781. provider: "glm",
  782. sessionId,
  783. modelSelectionView: null,
  784. });
  785. if (sessionId === null) {
  786. configsSeenDuringDraftRender.push(control.resolveInitialDraftConfig());
  787. }
  788. return null;
  789. }
  790. const root: Root = createRoot(installMinimalDom());
  791. await act(async () => {
  792. root.render(createElement(Probe, { sessionId: taskId }));
  793. });
  794. act(() => {
  795. useZCodeSessionStore.getState().startDraft(workspacePath, "glm");
  796. root.render(createElement(Probe, { sessionId: null }));
  797. });
  798. expect(configsSeenDuringDraftRender[0]).toBeUndefined();
  799. act(() => root.unmount());
  800. });
  801. it("Registry revision 只影响有效选择,正文和模式编辑保留原意图,配置恢复后重新有效", async () => {
  802. const workspacePath = "/live-selection-invalidated";
  803. const firstView = modelSelectionView(1, ["old", "preferred"], "preferred");
  804. const nextView = modelSelectionView(2, ["preferred"], "preferred");
  805. useZCodeSessionStore.getState().setSlashCommands(workspacePath, CUSTOM_COMMANDS);
  806. useZCodeSessionStore.getState().setConfigOptions(workspacePath, WORKSPACE_PRESENTATION);
  807. let control: ReturnType<typeof useDraftConfigControl> | undefined;
  808. function Probe({ view }: { view: ModelSelectionView }) {
  809. control = useDraftConfigControl({
  810. workspacePath,
  811. provider: "glm",
  812. sessionId: null,
  813. modelSelectionView: view,
  814. });
  815. return null;
  816. }
  817. const root: Root = createRoot(installMinimalDom());
  818. await act(async () => {
  819. root.render(createElement(Probe, { view: firstView }));
  820. });
  821. act(() => control!.handleDraftSelectModel("provider", "old"));
  822. expect(control!.draftConfig.modelSelection).toEqual({
  823. providerId: "provider",
  824. modelId: "old",
  825. options: { reasoningLevel: "high" },
  826. });
  827. await act(async () => {
  828. root.render(createElement(Probe, { view: nextView }));
  829. });
  830. expect(control!.draftConfig.modelSelection).toBeUndefined();
  831. expect(control!.resolveInitialDraftConfig()?.modelSelection).toBeUndefined();
  832. act(() => control!.updateComposerContent({ text: "保留原选择" }));
  833. act(() => control!.handleDraftSwitchMode("plan"));
  834. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toMatchObject({
  835. mode: "build",
  836. planEnabled: true,
  837. text: "保留原选择",
  838. modelSelection: { providerId: "provider", modelId: "old" },
  839. });
  840. await act(async () => {
  841. root.render(createElement(Probe, { view: { ...firstView, revision: 3 } }));
  842. });
  843. expect(control!.draftConfig.modelSelection).toEqual({
  844. providerId: "provider",
  845. modelId: "old",
  846. options: { reasoningLevel: "high" },
  847. });
  848. expect(toastMock).not.toHaveBeenCalled();
  849. act(() => root.unmount());
  850. });
  851. it("有效选择只在接纳时写回,迟到接纳不能覆盖新选择或另一个 scope", async () => {
  852. const workspacePath = "/effective-selection-commit";
  853. const root = createRoot(installMinimalDom());
  854. const original = {
  855. providerId: "old-account",
  856. modelId: "model",
  857. options: { reasoningLevel: "high" },
  858. };
  859. const effective = { ...original, providerId: "new-account" };
  860. persistV4ComposerDraft(workspacePath, undefined, "a", {
  861. text: "hello",
  862. mode: "build",
  863. modelSelection: original,
  864. });
  865. const service: IModelSelectionService = {
  866. getView: async (input) => ({
  867. revision: 1,
  868. providers: [],
  869. effectiveSelection:
  870. input?.selection?.providerId === "old-account" ? effective : (input?.selection ?? null),
  871. }),
  872. onDidChange: () => ({ dispose: () => {} }),
  873. };
  874. let control: ReturnType<typeof useActualDraftConfigControl>;
  875. function Probe({ sessionId }: { sessionId: string }) {
  876. control = useActualDraftConfigControl({
  877. workspacePath,
  878. sessionId,
  879. sessionConfig: { mode: "build" },
  880. agentStartupAllowed: false,
  881. modelSelectionService: service,
  882. });
  883. return null;
  884. }
  885. await act(async () => root.render(createElement(Probe, { sessionId: "a" })));
  886. expect(control!.draftConfig.modelSelection).toEqual(effective);
  887. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
  888. act(() => control!.updateComposerContent({ text: "draft" }));
  889. expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual(original);
  890. // 对象属性顺序不属于 Selection 语义,协议/序列化重建可能改变顺序。
  891. const accepted = control!.captureAcceptedModelSelection({
  892. options: effective.options,
  893. modelId: effective.modelId,
  894. providerId: effective.providerId,
  895. });
  896. act(() => control!.updateComposerContent({ text: "新正文" }));
  897. act(() => accepted());
  898. expect(readV4ComposerDraft(workspacePath, undefined, "a")).toMatchObject({
  899. text: "新正文",
  900. modelSelection: effective,
  901. });
  902. const recommended = { ...effective, providerId: "account:bigmodel-start-plan" };
  903. const acceptRecommendation = control!.captureAcceptedModelSelection(recommended, effective);
  904. expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual(effective);
  905. act(() => acceptRecommendation());
  906. expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual(recommended);
  907. const staleAccepted = control!.captureAcceptedModelSelection(recommended);
  908. await act(async () => control!.handleDraftSelectModel("personal", "another"));
  909. act(() => staleAccepted());
  910. expect(readV4ComposerDraft(workspacePath, undefined, "a")?.modelSelection).toEqual({
  911. providerId: "personal",
  912. modelId: "another",
  913. });
  914. const oldScopeAccepted = control!.captureAcceptedModelSelection(
  915. control!.draftConfig.modelSelection!,
  916. );
  917. await act(async () => root.render(createElement(Probe, { sessionId: "b" })));
  918. act(() => oldScopeAccepted());
  919. expect(readV4ComposerDraft(workspacePath, undefined, "b")?.modelSelection).toBeUndefined();
  920. act(() => root.unmount());
  921. });
  922. it("Session 只初始化一次,用户未发送的 mode/model 不被迟到 Snapshot 覆盖", async () => {
  923. const workspacePath = "/session-draft-seed";
  924. const view = modelSelectionView(1, ["old", "new"], "old");
  925. const root = createRoot(installMinimalDom());
  926. let control: ReturnType<typeof useDraftConfigControl>;
  927. function Probe({ config }: { config: Partial<SessionConfigState> | null }) {
  928. control = useDraftConfigControl({
  929. workspacePath,
  930. sessionId: "session",
  931. sessionConfig: config,
  932. agentStartupAllowed: false,
  933. modelSelectionView: view,
  934. });
  935. return null;
  936. }
  937. const config = { mode: "yolo", modelSelection: view.preferredSelection! };
  938. await act(async () => {
  939. root.render(createElement(Probe, { config }));
  940. });
  941. expect(control!.draftConfig).toMatchObject(config);
  942. act(() => {
  943. control!.handleDraftSwitchMode("edit");
  944. control!.handleDraftSelectModel("provider", "new");
  945. });
  946. await act(async () => {
  947. root.render(createElement(Probe, { config: { ...config } }));
  948. });
  949. expect(control!.draftConfig).toMatchObject({
  950. mode: "edit",
  951. modelSelection: { modelId: "new" },
  952. });
  953. expect(readV4ComposerDraft(workspacePath, undefined, "session")).toBeNull();
  954. act(() => root.unmount());
  955. });
  956. it("旧文本草稿等 Session 到达后只补新字段;Todo70 未绑定结果不会用 preferred 填满", async () => {
  957. const workspacePath = "/old-text-draft";
  958. const root = createRoot(installMinimalDom());
  959. persistV4ComposerDraft(workspacePath, undefined, "session", { text: "未发送正文" });
  960. let control: ReturnType<typeof useDraftConfigControl>;
  961. function Probe({ config }: { config: Partial<SessionConfigState> | null }) {
  962. control = useDraftConfigControl({
  963. workspacePath,
  964. sessionId: "session",
  965. sessionConfig: config,
  966. agentStartupAllowed: false,
  967. modelSelectionView: modelSelectionView(1, ["old"], "old"),
  968. });
  969. return null;
  970. }
  971. await act(async () => {
  972. root.render(createElement(Probe, { config: null }));
  973. });
  974. expect(readV4ComposerDraft(workspacePath, undefined, "session")?.mode).toBeUndefined();
  975. await act(async () => {
  976. root.render(createElement(Probe, { config: { mode: "build" } }));
  977. });
  978. expect(control!.draftConfig.modelSelection).toBeUndefined();
  979. expect(readV4ComposerDraft(workspacePath, undefined, "session")).toMatchObject({
  980. text: "未发送正文",
  981. mode: "build",
  982. });
  983. await act(async () => {
  984. root.render(
  985. createElement(Probe, {
  986. config: {
  987. mode: "yolo",
  988. modelSelection: modelSelectionView(1, ["old"], "old").preferredSelection!,
  989. },
  990. }),
  991. );
  992. });
  993. expect(control!.draftConfig).toMatchObject({ mode: "build" });
  994. expect(control!.draftConfig.modelSelection).toBeUndefined();
  995. act(() => root.unmount());
  996. });
  997. it("新 Root 继承最近接纳的权限,菜单编辑与旧 Snapshot 不覆盖独立草稿", async () => {
  998. const workspacePath = "/recent-mode";
  999. const root = createRoot(installMinimalDom());
  1000. const view = modelSelectionView(1, ["model"], "model");
  1001. captureComposerRecentSubmission(workspacePath, {
  1002. modelSelection: view.preferredSelection!,
  1003. mode: "yolo",
  1004. })();
  1005. let control: ReturnType<typeof useDraftConfigControl>;
  1006. function Probe({ sessionId }: { sessionId: string | null }) {
  1007. control = useDraftConfigControl({
  1008. workspacePath,
  1009. sessionId,
  1010. sessionConfig: { mode: "build", modelSelection: view.preferredSelection },
  1011. agentStartupAllowed: false,
  1012. modelSelectionView: view,
  1013. });
  1014. return null;
  1015. }
  1016. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  1017. expect(control!.draftConfig.mode).toBe("yolo");
  1018. act(() => {
  1019. control!.handleDraftSwitchMode("plan");
  1020. control!.updateComposerContent({ text: "root unsent" });
  1021. });
  1022. await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
  1023. expect(control!.draftConfig.mode).toBe("build");
  1024. act(() => {
  1025. control!.handleDraftSwitchMode("edit");
  1026. control!.updateComposerContent({ text: "session unsent" });
  1027. });
  1028. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  1029. expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
  1030. await act(async () => root.render(createElement(Probe, { sessionId: "session" })));
  1031. expect(control!.draftConfig.mode).toBe("build");
  1032. expect(
  1033. readComposerRecent(workspacePath, undefined, undefined, testScope(workspacePath)),
  1034. ).toMatchObject({ mode: "yolo" });
  1035. act(() => root.unmount());
  1036. });
  1037. it("目录重解析期间主动选模型仍使用同 Host 目录补齐思考", async () => {
  1038. const workspacePath = "/select-during-reasoning-read";
  1039. const root = createRoot(installMinimalDom());
  1040. persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
  1041. text: "draft",
  1042. mode: "build",
  1043. modelSelection: {
  1044. providerId: "provider",
  1045. modelId: "first",
  1046. options: { reasoningLevel: "high" },
  1047. },
  1048. });
  1049. let delayed = false;
  1050. const service: IModelSelectionService = {
  1051. getView: async (input) => {
  1052. if (delayed) await new Promise(() => {});
  1053. const view = modelSelectionView(1, ["first", "second"], "first");
  1054. return { ...view, effectiveSelection: input?.selection ?? null };
  1055. },
  1056. onDidChange: () => ({ dispose() {} }),
  1057. };
  1058. let control: ReturnType<typeof useActualDraftConfigControl>;
  1059. function Probe() {
  1060. control = useActualDraftConfigControl({
  1061. workspacePath,
  1062. sessionId: null,
  1063. agentStartupAllowed: false,
  1064. modelSelectionService: service,
  1065. });
  1066. return null;
  1067. }
  1068. await act(async () => root.render(createElement(Probe)));
  1069. delayed = true;
  1070. await act(async () => control!.handleDraftSelectThought("low"));
  1071. expect(control!.modelSelectionRead.state.status).toBe("loading");
  1072. act(() => control!.handleDraftSelectModel("provider", "second"));
  1073. expect(control!.composerDraft.modelSelection).toEqual({
  1074. providerId: "provider",
  1075. modelId: "second",
  1076. options: { reasoningLevel: "high" },
  1077. });
  1078. act(() => root.unmount());
  1079. });
  1080. it("账号连接同步返回的模型和档位原子写入,不依赖旧 API 目录", async () => {
  1081. const root = createRoot(installMinimalDom());
  1082. let control: ReturnType<typeof useDraftConfigControl>;
  1083. function Probe() {
  1084. control = useDraftConfigControl({
  1085. workspacePath: "/fresh-connection-selection",
  1086. sessionId: null,
  1087. agentStartupAllowed: false,
  1088. modelSelectionView: modelSelectionView(1, ["api-model"], "api-model"),
  1089. });
  1090. return null;
  1091. }
  1092. await act(async () => root.render(createElement(Probe)));
  1093. act(() =>
  1094. control!.handleDraftSelectModel("account:bigmodel-team-coding-plan", "GLM-5.3", "high"),
  1095. );
  1096. expect(control!.composerDraft.modelSelection).toEqual({
  1097. providerId: "account:bigmodel-team-coding-plan",
  1098. modelId: "GLM-5.3",
  1099. options: { reasoningLevel: "high" },
  1100. });
  1101. act(() => root.unmount());
  1102. });
  1103. it("新草稿先保留 Recent 原意图,再交给当前 View 解析,不能在初始化时丢掉旧账号", async () => {
  1104. const workspacePath = "/recent-account-intent";
  1105. const root = createRoot(installMinimalDom());
  1106. const original = {
  1107. providerId: "old-account",
  1108. modelId: "same",
  1109. options: { reasoningLevel: "high" },
  1110. };
  1111. const effective = { ...original, providerId: "provider" };
  1112. captureComposerRecentSubmission(workspacePath, { modelSelection: original, mode: "build" })();
  1113. const service: IModelSelectionService = {
  1114. getView: async (input) => ({
  1115. ...modelSelectionView(1, ["same"], "same"),
  1116. ...(input ? { effectiveSelection: input.selection ? effective : null } : {}),
  1117. }),
  1118. onDidChange: () => ({ dispose() {} }),
  1119. };
  1120. let control: ReturnType<typeof useActualDraftConfigControl>;
  1121. function Probe() {
  1122. control = useActualDraftConfigControl({
  1123. workspacePath,
  1124. sessionId: null,
  1125. agentStartupAllowed: false,
  1126. modelSelectionService: service,
  1127. });
  1128. return null;
  1129. }
  1130. await act(async () => {
  1131. root.render(createElement(Probe));
  1132. });
  1133. expect(control!.draftConfig.modelSelection).toEqual(effective);
  1134. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
  1135. act(() => control!.updateComposerContent({ text: "draft" }));
  1136. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.modelSelection).toEqual(
  1137. original,
  1138. );
  1139. act(() => root.unmount());
  1140. });
  1141. it("Reasoning 失效只清档位,loading 不清选择,重挂载不补默认", async () => {
  1142. const workspacePath = "/persisted-invalid-reasoning";
  1143. const container = installMinimalDom();
  1144. persistV4ComposerDraft(workspacePath, undefined, "session", {
  1145. text: "draft",
  1146. mode: "plan",
  1147. modelSelection: {
  1148. providerId: "provider",
  1149. modelId: "old",
  1150. options: { reasoningLevel: "deleted" },
  1151. },
  1152. });
  1153. let control: ReturnType<typeof useDraftConfigControl>;
  1154. function Probe({ view }: { view: ModelSelectionView | null }) {
  1155. control = useDraftConfigControl({
  1156. workspacePath,
  1157. sessionId: "session",
  1158. sessionConfig: { mode: "build" },
  1159. agentStartupAllowed: false,
  1160. modelSelectionView: view,
  1161. });
  1162. return null;
  1163. }
  1164. let root = createRoot(container);
  1165. await act(async () => {
  1166. root.render(createElement(Probe, { view: null }));
  1167. });
  1168. expect(control!.draftConfig.modelSelection?.options?.reasoningLevel).toBe("deleted");
  1169. const view = modelSelectionView(1, ["old"], "old");
  1170. await act(async () => {
  1171. root.render(createElement(Probe, { view }));
  1172. });
  1173. expect(control!.draftConfig.modelSelection).toEqual({ providerId: "provider", modelId: "old" });
  1174. act(() => root.unmount());
  1175. root = createRoot(container);
  1176. await act(async () => {
  1177. root.render(createElement(Probe, { view }));
  1178. });
  1179. expect(control!.draftConfig.modelSelection).toEqual({ providerId: "provider", modelId: "old" });
  1180. expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
  1181. expect(
  1182. readV4ComposerDraft(workspacePath, undefined, "session")?.modelSelection?.options
  1183. ?.reasoningLevel,
  1184. ).toBe("deleted");
  1185. expect(toastMock).not.toHaveBeenCalled();
  1186. act(() => root.unmount());
  1187. });
  1188. it("延迟 Session seed 也校验档位,正文保存与选模共享完整草稿且 scope 隔离", async () => {
  1189. const workspacePath = "/delayed-session";
  1190. const root = createRoot(installMinimalDom());
  1191. const view = modelSelectionView(1, ["old", "new"], "new");
  1192. let control: ReturnType<typeof useDraftConfigControl>;
  1193. function Probe({
  1194. sessionId,
  1195. config,
  1196. }: {
  1197. sessionId: string;
  1198. config: Partial<SessionConfigState> | null;
  1199. }) {
  1200. control = useDraftConfigControl({
  1201. workspacePath,
  1202. workspaceIdentity: "ssh://demo/workspace",
  1203. sessionId,
  1204. sessionConfig: config,
  1205. agentStartupAllowed: false,
  1206. modelSelectionView: view,
  1207. });
  1208. return null;
  1209. }
  1210. await act(async () => {
  1211. root.render(createElement(Probe, { sessionId: "a", config: null }));
  1212. });
  1213. await act(async () => {
  1214. root.render(
  1215. createElement(Probe, {
  1216. sessionId: "a",
  1217. config: {
  1218. mode: "yolo",
  1219. modelSelection: {
  1220. providerId: "provider",
  1221. modelId: "old",
  1222. options: { reasoningLevel: "deleted" },
  1223. },
  1224. },
  1225. }),
  1226. );
  1227. });
  1228. expect(control!.draftConfig.modelSelection).toEqual({ providerId: "provider", modelId: "old" });
  1229. act(() => {
  1230. control!.updateComposerContent({ text: "草稿 A" });
  1231. control!.handleDraftSelectModel("provider", "new");
  1232. });
  1233. const staleContentCallback = control!.updateComposerContent;
  1234. await act(async () => {
  1235. root.render(createElement(Probe, { sessionId: "b", config: { mode: "plan" } }));
  1236. });
  1237. act(() => staleContentCallback({ text: "迟到 A" }));
  1238. expect(control!.composerDraft).toMatchObject({ text: "", mode: "build", planEnabled: true });
  1239. await act(async () => {
  1240. root.render(createElement(Probe, { sessionId: "a", config: { mode: "build" } }));
  1241. });
  1242. expect(control!.composerDraft).toMatchObject({
  1243. text: "草稿 A",
  1244. mode: "build",
  1245. modelSelection: {
  1246. providerId: "provider",
  1247. modelId: "new",
  1248. },
  1249. });
  1250. expect(readV4ComposerDraft(workspacePath, undefined, "a")).toBeNull();
  1251. act(() => root.unmount());
  1252. });
  1253. it("新任务创建 Session 时先转移完整 Draft,再删除 Root scope", async () => {
  1254. const workspacePath = "/promote-composer-draft";
  1255. const root = createRoot(installMinimalDom());
  1256. const view = modelSelectionView(1, ["model"], "model");
  1257. let control: ReturnType<typeof useDraftConfigControl>;
  1258. function Probe() {
  1259. control = useDraftConfigControl({
  1260. workspacePath,
  1261. sessionId: null,
  1262. agentStartupAllowed: false,
  1263. modelSelectionView: view,
  1264. });
  1265. return null;
  1266. }
  1267. await act(async () => root.render(createElement(Probe)));
  1268. act(() => {
  1269. control!.updateComposerContent({ text: "等待接纳的正文" });
  1270. control!.handleDraftSwitchMode("yolo");
  1271. control!.handleDraftSelectModel("provider", "model");
  1272. control!.promoteComposerDraft("created-session");
  1273. });
  1274. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
  1275. expect(readV4ComposerDraft(workspacePath, undefined, "created-session")).toMatchObject({
  1276. text: "等待接纳的正文",
  1277. mode: "yolo",
  1278. modelSelection: {
  1279. providerId: "provider",
  1280. modelId: "model",
  1281. },
  1282. });
  1283. act(() => root.unmount());
  1284. });
  1285. it("TD05 首发清空后仅内存交接配置,Snapshot 迟到不覆盖,离开后不遗留空草稿", async () => {
  1286. const workspacePath = "/empty-promotion";
  1287. const root = createRoot(installMinimalDom());
  1288. let control: ReturnType<typeof useDraftConfigControl>;
  1289. function Probe({ sessionId }: { sessionId: string | null }) {
  1290. control = useDraftConfigControl({
  1291. workspacePath,
  1292. sessionId,
  1293. sessionConfig: { mode: "build" },
  1294. agentStartupAllowed: false,
  1295. modelSelectionView: modelSelectionView(1, ["model"], "model"),
  1296. });
  1297. return null;
  1298. }
  1299. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  1300. act(() => {
  1301. control!.handleDraftSwitchMode("yolo");
  1302. control!.promoteComposerDraft("created");
  1303. });
  1304. expect(readV4ComposerDraft(workspacePath, undefined, "created")).toBeNull();
  1305. await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
  1306. expect(control!.draftConfig.mode).toBe("yolo");
  1307. expect(readImportInitialization(workspacePath, undefined, "created")).toBeUndefined();
  1308. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  1309. await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
  1310. expect(control!.draftConfig.mode).toBe("build");
  1311. act(() => root.unmount());
  1312. });
  1313. it("目标 Session scope 写盘失败时保留 Root Draft", async () => {
  1314. const workspacePath = "/promote-write-failure";
  1315. const root = createRoot(installMinimalDom());
  1316. let control: ReturnType<typeof useDraftConfigControl>;
  1317. function Probe() {
  1318. control = useDraftConfigControl({
  1319. workspacePath,
  1320. sessionId: null,
  1321. agentStartupAllowed: false,
  1322. modelSelectionView: modelSelectionView(1, ["model"], "model"),
  1323. });
  1324. return null;
  1325. }
  1326. await act(async () => root.render(createElement(Probe)));
  1327. act(() => control!.updateComposerContent({ text: "必须保留" }));
  1328. persistV4ComposerDraft(workspacePath, undefined, "__draft__", control!.composerDraft);
  1329. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.text).toBe("必须保留");
  1330. const originalSetItem = window.localStorage.setItem.bind(window.localStorage);
  1331. const failedWrites = vi.fn(() => {
  1332. throw new Error("quota");
  1333. });
  1334. window.localStorage.setItem = failedWrites;
  1335. act(() => control!.promoteComposerDraft("created-session"));
  1336. window.localStorage.setItem = originalSetItem;
  1337. expect(failedWrites).toHaveBeenCalledTimes(1);
  1338. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.text).toBe("必须保留");
  1339. expect(readV4ComposerDraft(workspacePath, undefined, "created-session")).toBeNull();
  1340. act(() => root.unmount());
  1341. });
  1342. it("PA158 授权按 scope 消费一次,保留 Plan/草稿;撤回编辑不重放旧授权", async () => {
  1343. const root = createRoot(installMinimalDom());
  1344. const workspacePath = "/permission-grant";
  1345. persistV4ComposerDraft(workspacePath, undefined, "s", {
  1346. text: "draft",
  1347. mode: "edit",
  1348. planEnabled: true,
  1349. });
  1350. let control: ReturnType<typeof useDraftConfigControl>;
  1351. function Probe({ sessionId }: { sessionId: string }) {
  1352. control = useDraftConfigControl({
  1353. workspacePath,
  1354. sessionId,
  1355. sessionConfig:
  1356. sessionId === "s"
  1357. ? { mode: "yolo", planEnabled: false, permissionGrant: { interactionId: "p" } }
  1358. : { mode: "build" },
  1359. modelSelectionView: null,
  1360. agentStartupAllowed: false,
  1361. });
  1362. return null;
  1363. }
  1364. await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
  1365. expect(control!.composerDraft).toMatchObject({
  1366. text: "draft",
  1367. mode: "yolo",
  1368. planEnabled: false,
  1369. });
  1370. act(() => control!.handleDraftSwitchMode("build"));
  1371. await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
  1372. expect(control!.composerDraft.mode).toBe("build");
  1373. act(() =>
  1374. control!.replaceComposerDraft({ text: "later queue", mode: "edit", planEnabled: false }),
  1375. );
  1376. expect(control!.composerDraft).toMatchObject({
  1377. text: "later queue",
  1378. mode: "edit",
  1379. planEnabled: false,
  1380. });
  1381. await act(async () => root.render(createElement(Probe, { sessionId: "other" })));
  1382. expect(control!.composerDraft.mode).toBe("build");
  1383. await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
  1384. expect(control!.composerDraft.mode).toBe("yolo");
  1385. act(() => root.unmount());
  1386. });
  1387. it.each([false, true])(
  1388. "CS01/02/10 恢复当前权限/Plan,忽略历史授权,有文字=%s",
  1389. async (hasText) => {
  1390. const workspacePath = "/restore-current-config";
  1391. const root = createRoot(installMinimalDom());
  1392. if (hasText)
  1393. persistV4ComposerDraft(workspacePath, undefined, "s", {
  1394. text: "keep text",
  1395. mode: "yolo",
  1396. planEnabled: true,
  1397. modelSelection: { providerId: "provider", modelId: "draft-model" },
  1398. });
  1399. let control: ReturnType<typeof useDraftConfigControl>;
  1400. function Probe({ sessionId }: { sessionId: string }) {
  1401. control = useDraftConfigControl({
  1402. workspacePath,
  1403. sessionId,
  1404. agentStartupAllowed: false,
  1405. modelSelectionView: null,
  1406. sessionConfig: {
  1407. mode: "build",
  1408. planEnabled: false,
  1409. modelSelection: { providerId: "provider", modelId: "session-model" },
  1410. permissionGrant: { interactionId: "old" },
  1411. planTransition: { toolCallId: "old", planEnabled: true },
  1412. },
  1413. });
  1414. return null;
  1415. }
  1416. await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
  1417. expect(control!.composerDraft).toMatchObject({
  1418. text: hasText ? "keep text" : "",
  1419. mode: "build",
  1420. planEnabled: false,
  1421. modelSelection: { modelId: hasText ? "draft-model" : "session-model" },
  1422. });
  1423. act(() => {
  1424. control!.handleDraftSwitchMode("yolo");
  1425. control!.handleDraftSwitchMode("plan");
  1426. });
  1427. await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
  1428. expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
  1429. await act(async () => root.render(createElement(Probe, { sessionId: "other" })));
  1430. await act(async () => root.render(createElement(Probe, { sessionId: "s" })));
  1431. expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
  1432. const persisted = readV4ComposerDraft(workspacePath, undefined, "s");
  1433. expect(persisted ?? {}).not.toHaveProperty("lastPermissionGrantId");
  1434. expect(persisted ?? {}).not.toHaveProperty("lastPlanTransitionId");
  1435. act(() => root.unmount());
  1436. },
  1437. );
  1438. it("CS03/04 首帧迟到仍取最新权限,后续只响应新关联并使用最新快照值", async () => {
  1439. const root = createRoot(installMinimalDom());
  1440. const workspacePath = "/observe-config";
  1441. persistV4ComposerDraft(workspacePath, undefined, "s", {
  1442. text: "keep",
  1443. mode: "yolo",
  1444. planEnabled: true,
  1445. });
  1446. let control: ReturnType<typeof useDraftConfigControl>;
  1447. function Probe({ config }: { config: Partial<SessionConfigState> | null }) {
  1448. control = useDraftConfigControl({
  1449. workspacePath,
  1450. sessionId: "s",
  1451. sessionConfig: config,
  1452. agentStartupAllowed: false,
  1453. modelSelectionView: null,
  1454. });
  1455. return null;
  1456. }
  1457. const config = {
  1458. mode: "build",
  1459. planEnabled: false,
  1460. permissionGrant: { interactionId: "old" },
  1461. planTransition: { toolCallId: "old", planEnabled: true },
  1462. };
  1463. await act(async () => root.render(createElement(Probe, { config: null })));
  1464. await act(async () => root.render(createElement(Probe, { config })));
  1465. expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
  1466. act(() => control!.handleDraftSwitchMode("plan"));
  1467. const granted = { ...config, mode: "yolo", permissionGrant: { interactionId: "new" } };
  1468. await act(async () => root.render(createElement(Probe, { config: granted })));
  1469. expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
  1470. act(() => control!.handleDraftSwitchMode("edit"));
  1471. await act(async () => root.render(createElement(Probe, { config: null })));
  1472. await act(async () => root.render(createElement(Probe, { config: { ...granted } })));
  1473. expect(control!.draftConfig.mode).toBe("edit");
  1474. // 新关联到达时快照可能已包含之后的模式改变,不能硬编码重新执行 yolo/旧 Plan。
  1475. const latest = {
  1476. ...granted,
  1477. mode: "build",
  1478. planEnabled: false,
  1479. permissionGrant: { interactionId: "newer" },
  1480. planTransition: { toolCallId: "newer", planEnabled: true },
  1481. };
  1482. await act(async () => root.render(createElement(Probe, { config: latest })));
  1483. expect(control!.draftConfig).toMatchObject({ mode: "build", planEnabled: false });
  1484. expect(control!.composerDraft.text).toBe("keep");
  1485. act(() => root.unmount());
  1486. });
  1487. it("CS06 New Task 在 React 提交前也使旧 promotion 失效", async () => {
  1488. const workspacePath = "/promotion-generation";
  1489. const root = createRoot(installMinimalDom());
  1490. let control: ReturnType<typeof useDraftConfigControl>;
  1491. function Probe() {
  1492. control = useDraftConfigControl({
  1493. workspacePath,
  1494. sessionId: null,
  1495. agentStartupAllowed: false,
  1496. modelSelectionView: modelSelectionView(1, ["model"], "model"),
  1497. });
  1498. return null;
  1499. }
  1500. await act(async () => root.render(createElement(Probe)));
  1501. const oldPromotion = control!.promoteComposerDraft;
  1502. act(() => {
  1503. useZCodeSessionStore.getState().startDraft(workspacePath);
  1504. persistV4ComposerDraft(workspacePath, undefined, "__draft__", {
  1505. text: "new page text",
  1506. mode: "build",
  1507. });
  1508. oldPromotion("old-target");
  1509. });
  1510. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")?.text).toBe("new page text");
  1511. expect(readV4ComposerDraft(workspacePath, undefined, "old-target")).toBeNull();
  1512. act(() => root.unmount());
  1513. });
  1514. it("CS06 未接收的首发配置不跨 pane 卸载残留", async () => {
  1515. const workspacePath = "/promotion-unmounted";
  1516. const root = createRoot(installMinimalDom());
  1517. let control: ReturnType<typeof useDraftConfigControl>;
  1518. function Probe({ sessionId }: { sessionId: string | null }) {
  1519. control = useDraftConfigControl({
  1520. workspacePath,
  1521. sessionId,
  1522. sessionConfig: { mode: "build" },
  1523. agentStartupAllowed: false,
  1524. modelSelectionView: modelSelectionView(1, ["model"], "model"),
  1525. });
  1526. return null;
  1527. }
  1528. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  1529. act(() => {
  1530. control!.handleDraftSwitchMode("yolo");
  1531. control!.promoteComposerDraft("target");
  1532. });
  1533. const oldPromotion = control!.promoteComposerDraft;
  1534. act(() => root.unmount());
  1535. oldPromotion("late-target");
  1536. const reopened = createRoot(installMinimalDom());
  1537. await act(async () => reopened.render(createElement(Probe, { sessionId: "target" })));
  1538. expect(control!.draftConfig.mode).toBe("build");
  1539. act(() => reopened.unmount());
  1540. });
  1541. it("CS04/07 StrictMode 只接收当前导入参数,切走或新建即取消未接收参数", async () => {
  1542. const workspacePath = "/scoped-import";
  1543. const root = createRoot(installMinimalDom());
  1544. const store = useZCodeSessionStore.getState();
  1545. store.setActiveTaskId(workspacePath, "cancelled", undefined, { mode: "yolo" });
  1546. store.setActiveTaskId(workspacePath, "other");
  1547. expect(readImportInitialization(workspacePath, undefined, "cancelled")).toBeUndefined();
  1548. store.setActiveTaskId(workspacePath, "cancelled", undefined, { mode: "yolo" });
  1549. store.startDraft(workspacePath);
  1550. expect(readImportInitialization(workspacePath, undefined, "cancelled")).toBeUndefined();
  1551. store.setActiveTaskId(workspacePath, "imported", undefined, {
  1552. mode: "yolo",
  1553. planEnabled: true,
  1554. });
  1555. let control: ReturnType<typeof useDraftConfigControl>;
  1556. function Probe() {
  1557. control = useDraftConfigControl({
  1558. workspacePath,
  1559. sessionId: "imported",
  1560. sessionConfig: {
  1561. mode: "build",
  1562. planEnabled: false,
  1563. permissionGrant: { interactionId: "old" },
  1564. },
  1565. agentStartupAllowed: false,
  1566. modelSelectionView: null,
  1567. });
  1568. return null;
  1569. }
  1570. await act(async () => root.render(createElement(StrictMode, null, createElement(Probe))));
  1571. expect(control!.draftConfig).toMatchObject({ mode: "yolo", planEnabled: true });
  1572. expect(readImportInitialization(workspacePath, undefined, "imported")).toBeUndefined();
  1573. expect(readV4ComposerDraft(workspacePath, undefined, "imported")).toBeNull();
  1574. act(() => root.unmount());
  1575. });
  1576. it("CS05 同一 pane 首发绑定保留后续新输入和选择,后续普通恢复才采用会话权限", async () => {
  1577. const workspacePath = "/promote-live-edit";
  1578. const root = createRoot(installMinimalDom());
  1579. let control: ReturnType<typeof useDraftConfigControl>;
  1580. function Probe({ sessionId }: { sessionId: string | null }) {
  1581. control = useDraftConfigControl({
  1582. workspacePath,
  1583. sessionId,
  1584. sessionConfig: { mode: "build" },
  1585. agentStartupAllowed: false,
  1586. modelSelectionView: modelSelectionView(1, ["model"], "model"),
  1587. });
  1588. return null;
  1589. }
  1590. await act(async () => root.render(createElement(Probe, { sessionId: null })));
  1591. act(() => {
  1592. control!.promoteComposerDraft("created");
  1593. control!.updateComposerContent({ text: "typed after acceptance" });
  1594. control!.handleDraftSwitchMode("yolo");
  1595. });
  1596. await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
  1597. expect(control!.composerDraft).toMatchObject({ text: "typed after acceptance", mode: "yolo" });
  1598. expect(readV4ComposerDraft(workspacePath, undefined, "__draft__")).toBeNull();
  1599. await act(async () => root.render(createElement(Probe, { sessionId: "other" })));
  1600. await act(async () => root.render(createElement(Probe, { sessionId: "created" })));
  1601. expect(control!.composerDraft).toMatchObject({ text: "typed after acceptance", mode: "build" });
  1602. act(() => root.unmount());
  1603. });
  1604. });