v4-native-goal-compact.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // L2:v4 原生 goal/compact 命令组(compact / sendGoalCommand / pauseGoal / resumeGoal)——
  2. // 验证自旧 server-operations compactSession/goalSession 搬运的状态机 barrier 在原生层保真:
  3. // compact 去重幂等、active /goal 入队、重复 set 收敛 replace、goal 续跑的锁生命周期。
  4. import { describe, expect, it, vi } from "vitest";
  5. import type { CommandEnvelope } from "@zcode/shared/zcode-protocol-v4";
  6. import { V4CommandExecutor } from "../src/zcode-protocol-v4/commands/executor.js";
  7. import type {
  8. V4CommandCoreHost,
  9. V4SessionRecordView,
  10. } from "../src/zcode-protocol-v4/commands/types.js";
  11. function makeApp(overrides: Record<string, unknown> = {}) {
  12. return {
  13. sessionId: "s1",
  14. runtime: {
  15. getActiveTurnInfo: vi.fn().mockReturnValue(undefined),
  16. setExecutionState: vi.fn().mockResolvedValue(undefined),
  17. },
  18. getModel: vi.fn().mockReturnValue("primary-provider/default-model"),
  19. getThoughtLevel: vi.fn().mockReturnValue("high"),
  20. getMode: vi.fn().mockReturnValue("auto"),
  21. submitPrompt: vi.fn().mockResolvedValue({}),
  22. steerTurn: vi.fn().mockResolvedValue({ kind: "queued" }),
  23. setQueueAutoDrain: vi.fn().mockResolvedValue(undefined),
  24. readTarget: vi.fn().mockResolvedValue(null),
  25. setTarget: vi.fn().mockImplementation(async (input: { objective: string }) => ({
  26. targetID: "g1",
  27. objective: input.objective,
  28. status: "active",
  29. })),
  30. updateTargetStatus: vi
  31. .fn()
  32. .mockResolvedValue({ targetID: "g1", objective: "旧目标", status: "active" }),
  33. continueActiveTarget: vi.fn().mockResolvedValue(null),
  34. ...overrides,
  35. };
  36. }
  37. function makeRecord(
  38. overrides: Partial<V4SessionRecordView> & { app?: ReturnType<typeof makeApp> } = {},
  39. ): V4SessionRecordView {
  40. return {
  41. app: (overrides.app ?? makeApp()) as unknown as V4SessionRecordView["app"],
  42. workspace: { workspacePath: "/w" },
  43. persistence: "immediate",
  44. ...overrides,
  45. } as V4SessionRecordView;
  46. }
  47. function makeHost(
  48. record: V4SessionRecordView,
  49. overrides: Partial<V4CommandCoreHost> = {},
  50. ): V4CommandCoreHost & {
  51. legacyMutations: string[];
  52. } {
  53. const legacyMutations: string[] = [];
  54. return {
  55. getRecord: (id) => (id === "s1" ? record : undefined),
  56. afterLegacyStateMutation: async (_r, reason) => {
  57. legacyMutations.push(reason);
  58. },
  59. ...overrides,
  60. legacyMutations,
  61. };
  62. }
  63. function envelope(type: string, payload: unknown): CommandEnvelope {
  64. return {
  65. type,
  66. payload,
  67. sessionId: "s1",
  68. commandId: "cmd-1",
  69. baseRevision: 0,
  70. } as unknown as CommandEnvelope;
  71. }
  72. async function settle() {
  73. // 后台 turn 是 void 起跑的微任务链,flush 两轮保证 finally 执行完。
  74. await new Promise((r) => setTimeout(r, 0));
  75. await new Promise((r) => setTimeout(r, 0));
  76. }
  77. describe("v4 原生 compact", () => {
  78. it.each([false, true])("compact 固定提交的 B:busy=%s", async (busy) => {
  79. const modelSelection = { providerId: "provider-b", modelId: "model-b" };
  80. const app = makeApp();
  81. const record = makeRecord({
  82. app,
  83. ...(busy ? { activeAbortController: new AbortController() } : {}),
  84. });
  85. await new V4CommandExecutor(makeHost(record)).execute(envelope("compact", { modelSelection }));
  86. const call = (busy ? app.steerTurn : app.submitPrompt).mock.calls[0];
  87. expect(call?.[0]).toBe("/compact");
  88. expect(call?.[1]).toMatchObject({
  89. intent: { kind: "compact", sourceCommandId: "cmd-1", modelSelection },
  90. });
  91. expect(call?.[1]?.intent).not.toHaveProperty("mode");
  92. expect(call?.[1]?.intent).not.toHaveProperty("planEnabled");
  93. await settle();
  94. });
  95. it("旧 compact 在接收时固定 A,就绪等待期间切到 C 不改写请求", async () => {
  96. let ready!: () => void;
  97. const readyGate = new Promise<void>((resolve) => {
  98. ready = resolve;
  99. });
  100. const app = makeApp();
  101. const record = makeRecord({ app });
  102. const executing = new V4CommandExecutor(
  103. makeHost(record, {
  104. ensureModelReady: vi.fn().mockReturnValue(readyGate),
  105. }),
  106. ).execute(envelope("compact", {}));
  107. app.getModel.mockReturnValue("provider-c/model-c");
  108. ready();
  109. await executing;
  110. expect(app.submitPrompt).toHaveBeenCalledWith(
  111. "/compact",
  112. expect.objectContaining({
  113. intent: expect.objectContaining({
  114. modelSelection: {
  115. providerId: "primary-provider",
  116. modelId: "default-model",
  117. },
  118. }),
  119. }),
  120. );
  121. await settle();
  122. });
  123. it("正路径:后台 submitPrompt('/compact') + 上锁 + ready 边界释放", async () => {
  124. let finishCompact!: () => void;
  125. const compactGate = new Promise<Record<string, never>>((resolve) => {
  126. finishCompact = () => resolve({});
  127. });
  128. const app = makeApp({ submitPrompt: vi.fn().mockReturnValue(compactGate) });
  129. const record = makeRecord({ app });
  130. const host = makeHost(record);
  131. host.recordPersistentCommandFact = vi.fn().mockResolvedValue(undefined);
  132. await new V4CommandExecutor(host).execute(envelope("compact", {}));
  133. // 提交即返:execute 已回,compact 仍挂起 → Stop 可用的锁必须在。
  134. expect(record.activeAbortController).toBeDefined();
  135. expect(app.submitPrompt).toHaveBeenCalledWith(
  136. "/compact",
  137. expect.objectContaining({ inputId: "cmd-1" }),
  138. );
  139. finishCompact();
  140. await settle();
  141. // ready 边界:compact 结束先释放锁再 legacy 广播。
  142. expect(record.activeAbortController).toBeUndefined();
  143. expect(host.recordPersistentCommandFact).toHaveBeenCalledWith(
  144. "s1",
  145. "timeline",
  146. expect.objectContaining({ commandId: "cmd-1", status: "accepted" }),
  147. { lifecycleStatus: "success" },
  148. );
  149. expect(host.legacyMutations).toEqual(["session_compacted"]);
  150. });
  151. it("释放 active lock 后,state mutation 收尾完成前仍持有 residency lease", async () => {
  152. let finishCompact!: () => void;
  153. let finishMutation!: () => void;
  154. let mutationStarted!: () => void;
  155. const compactGate = new Promise<Record<string, never>>((resolve) => {
  156. finishCompact = () => resolve({});
  157. });
  158. const mutationGate = new Promise<void>((resolve) => {
  159. finishMutation = resolve;
  160. });
  161. const mutationStartedGate = new Promise<void>((resolve) => {
  162. mutationStarted = resolve;
  163. });
  164. const record = makeRecord({
  165. app: makeApp({ submitPrompt: vi.fn().mockReturnValue(compactGate) }),
  166. });
  167. const host = makeHost(record, {
  168. afterLegacyStateMutation: async () => {
  169. mutationStarted();
  170. await mutationGate;
  171. },
  172. });
  173. await new V4CommandExecutor(host).execute(envelope("compact", {}));
  174. expect(record.residencyFinalizationCount).toBe(1);
  175. finishCompact();
  176. await mutationStartedGate;
  177. expect(record.activeAbortController).toBeUndefined();
  178. expect(record.residencyFinalizationCount).toBe(1);
  179. finishMutation();
  180. await settle();
  181. expect(record.residencyFinalizationCount).toBe(0);
  182. });
  183. it("compact 进行中:显式拒绝(不再次提交)", async () => {
  184. const app = makeApp({
  185. runtime: {
  186. getActiveTurnInfo: vi
  187. .fn()
  188. .mockReturnValue({ kind: "compact", queueLength: 0, steerable: false }),
  189. },
  190. });
  191. const controller = new AbortController();
  192. const record = makeRecord({ app, activeAbortController: controller });
  193. const host = makeHost(record);
  194. await expect(
  195. new V4CommandExecutor(host).execute(envelope("compact", {})),
  196. ).rejects.toMatchObject({ reasonCode: "compactOperationLock" });
  197. expect(app.submitPrompt).not.toHaveBeenCalled();
  198. // 显式拒绝不得触碰进行中 compact 的锁。
  199. expect(record.activeAbortController).toBe(controller);
  200. });
  201. it("其他 active turn:/compact 以 typed maintenance intent 入队,不立即提交", async () => {
  202. const app = makeApp({
  203. runtime: {
  204. getActiveTurnInfo: vi
  205. .fn()
  206. .mockReturnValue({ kind: "primaryTurn", queueLength: 0, steerable: true }),
  207. },
  208. });
  209. const record = makeRecord({ app, activeAbortController: new AbortController() });
  210. await new V4CommandExecutor(makeHost(record)).execute(envelope("compact", {}));
  211. expect(app.steerTurn).toHaveBeenCalledWith(
  212. "/compact",
  213. expect.objectContaining({
  214. commandKind: "compact",
  215. delivery: "queue",
  216. inputId: "cmd-1",
  217. intent: expect.objectContaining({
  218. kind: "compact",
  219. text: "/compact",
  220. requestedDelivery: "queue",
  221. }),
  222. }),
  223. );
  224. expect(app.submitPrompt).not.toHaveBeenCalled();
  225. });
  226. it("goal verifier busy routing:即使没有 steerable active turn,也进入 deferred FIFO", async () => {
  227. const modelSelection = { providerId: "provider-b", modelId: "model-b" };
  228. const enqueueDeferredInput = vi.fn().mockResolvedValue({ kind: "queued" });
  229. const app = makeApp({ enqueueDeferredInput });
  230. const record = makeRecord({ app });
  231. await new V4CommandExecutor(
  232. makeHost(record, { getInputRoutingMode: vi.fn().mockReturnValue("enqueue") }),
  233. ).execute(envelope("compact", { modelSelection }));
  234. expect(enqueueDeferredInput).toHaveBeenCalledWith(
  235. "/compact",
  236. expect.objectContaining({
  237. commandKind: "compact",
  238. delivery: "queue",
  239. intent: expect.objectContaining({ modelSelection }),
  240. }),
  241. );
  242. expect(app.submitPrompt).not.toHaveBeenCalled();
  243. });
  244. it("已有 queued compact:compactOperationLock 拒绝,避免 FIFO 内重复维护意图", async () => {
  245. const app = makeApp();
  246. const record = makeRecord({ app });
  247. await expect(
  248. new V4CommandExecutor(
  249. makeHost(record, { hasQueueItemKind: vi.fn().mockReturnValue(true) }),
  250. ).execute(envelope("compact", {})),
  251. ).rejects.toMatchObject({ reasonCode: "compactOperationLock" });
  252. expect(app.steerTurn).not.toHaveBeenCalled();
  253. expect(app.submitPrompt).not.toHaveBeenCalled();
  254. });
  255. it("后台 compact 登记 runtime active turn 前重复提交:同步 operation lock 仍拒绝", async () => {
  256. let finishCompact!: () => void;
  257. const compactGate = new Promise<Record<string, never>>((resolve) => {
  258. finishCompact = () => resolve({});
  259. });
  260. const app = makeApp({ submitPrompt: vi.fn().mockReturnValue(compactGate) });
  261. const record = makeRecord({ app });
  262. const host = makeHost(record);
  263. await new V4CommandExecutor(host).execute(envelope("compact", {}));
  264. await expect(
  265. new V4CommandExecutor(host).execute({
  266. ...envelope("compact", {}),
  267. commandId: "cmd-2",
  268. }),
  269. ).rejects.toMatchObject({ reasonCode: "compactOperationLock" });
  270. finishCompact();
  271. await settle();
  272. });
  273. it("compact 失败:finally 仍释放锁,reason=session_compact_failed", async () => {
  274. const app = makeApp({
  275. submitPrompt: vi.fn().mockRejectedValue(new Error("boom")),
  276. });
  277. const record = makeRecord({ app });
  278. const host = makeHost(record);
  279. await new V4CommandExecutor(host).execute(envelope("compact", {}));
  280. await settle();
  281. expect(record.activeAbortController).toBeUndefined();
  282. expect(host.legacyMutations).toEqual(["session_compact_failed"]);
  283. });
  284. it("queued compact 失败且后方仍有输入:关闭 autoDrain,保持 FIFO failure barrier", async () => {
  285. const app = makeApp({
  286. submitPrompt: vi.fn().mockRejectedValue(new Error("boom")),
  287. });
  288. const record = makeRecord({ app });
  289. const host = makeHost(record, { getQueueLength: vi.fn().mockReturnValue(1) });
  290. await new V4CommandExecutor(host).execute(envelope("compact", {}));
  291. await settle();
  292. expect(app.setQueueAutoDrain).toHaveBeenCalledWith(false);
  293. expect(host.legacyMutations).toEqual(["session_compact_failed"]);
  294. });
  295. });
  296. describe("v4 原生 sendGoalCommand", () => {
  297. it("set 正路径:setTarget(active) + 后台续跑 + 锁生命周期", async () => {
  298. let finishContinuation!: () => void;
  299. const continuationGate = new Promise<null>((resolve) => {
  300. finishContinuation = () => resolve(null);
  301. });
  302. const app = makeApp({
  303. continueActiveTarget: vi.fn().mockReturnValue(continuationGate),
  304. });
  305. const record = makeRecord({ app });
  306. const host = makeHost(record);
  307. await new V4CommandExecutor(host).execute(
  308. envelope("sendGoalCommand", {
  309. text: " 完成登录页 ",
  310. displayText: "/TaRgEt replace 完成登录页",
  311. modelSelection: {
  312. providerId: "alternate-provider",
  313. modelId: "goal-model",
  314. options: { reasoningLevel: "max" },
  315. },
  316. mode: "yolo",
  317. }),
  318. );
  319. // objective 落库为 trim 后文本,状态直接 active。
  320. expect(app.setTarget).toHaveBeenCalledWith(
  321. expect.objectContaining({
  322. displayText: "/TaRgEt replace 完成登录页",
  323. objective: "完成登录页",
  324. status: "active",
  325. intent: expect.objectContaining({
  326. sourceCommandId: "cmd-1",
  327. modelSelection: {
  328. providerId: "alternate-provider",
  329. modelId: "goal-model",
  330. options: { reasoningLevel: "max" },
  331. },
  332. mode: "yolo",
  333. }),
  334. }),
  335. );
  336. // 续跑期间持锁(连续 /goal 会被 active turn barrier 拒绝)。
  337. expect(record.activeAbortController).toBeDefined();
  338. expect(host.legacyMutations).toEqual(["goal_set"]);
  339. finishContinuation();
  340. await settle();
  341. expect(app.continueActiveTarget).toHaveBeenCalledWith(
  342. expect.objectContaining({
  343. inputId: "cmd-1",
  344. intent: expect.objectContaining({
  345. modelSelection: {
  346. providerId: "alternate-provider",
  347. modelId: "goal-model",
  348. options: { reasoningLevel: "max" },
  349. },
  350. mode: "yolo",
  351. }),
  352. }),
  353. );
  354. // 续跑结束释放锁(旧实现修过的 bug:占锁会让连续 /goal 误判活跃 turn)。
  355. expect(record.activeAbortController).toBeUndefined();
  356. expect(host.legacyMutations).toEqual(["goal_set", "goal_continuation_completed"]);
  357. });
  358. it("重复 set:已有 target 收敛 replace 语义(reason=goal_replaced)", async () => {
  359. const app = makeApp({
  360. readTarget: vi
  361. .fn()
  362. .mockResolvedValue({ targetID: "g0", objective: "旧目标", status: "active" }),
  363. });
  364. const record = makeRecord({ app });
  365. const host = makeHost(record);
  366. await new V4CommandExecutor(host).execute(envelope("sendGoalCommand", { text: "新目标" }));
  367. await settle();
  368. // 不要求用户先 clear/replace:直接覆盖写入新目标。
  369. expect(app.setTarget).toHaveBeenCalledWith(
  370. expect.objectContaining({
  371. displayText: "/goal 新目标",
  372. objective: "新目标",
  373. status: "active",
  374. intent: expect.objectContaining({ sourceCommandId: "cmd-1" }),
  375. }),
  376. );
  377. expect(host.legacyMutations[0]).toBe("goal_replaced");
  378. });
  379. it("active turn 存在:入队为 goal command,不立即写 target", async () => {
  380. const app = makeApp();
  381. const record = makeRecord({ app, activeAbortController: new AbortController() });
  382. await new V4CommandExecutor(makeHost(record)).execute(
  383. envelope("sendGoalCommand", {
  384. text: "x",
  385. displayText: "/target x",
  386. modelSelection: { providerId: "queued-provider", modelId: "queued-model" },
  387. mode: "edit",
  388. }),
  389. );
  390. expect(app.steerTurn).toHaveBeenCalledWith(
  391. "/target x",
  392. expect.objectContaining({
  393. commandKind: "sendGoalCommand",
  394. inputId: "cmd-1",
  395. queryId: "cmd-1",
  396. intent: expect.objectContaining({
  397. kind: "sendGoalCommand",
  398. text: "x",
  399. modelSelection: { providerId: "queued-provider", modelId: "queued-model" },
  400. mode: "edit",
  401. }),
  402. }),
  403. );
  404. expect(app.setTarget).not.toHaveBeenCalled();
  405. });
  406. it("goal verifier routing 早于 controller:/goal 仍进入 deferred FIFO", async () => {
  407. const enqueueDeferredInput = vi.fn().mockResolvedValue({ kind: "queued" });
  408. const app = makeApp({
  409. enqueueDeferredInput,
  410. runtime: { getActiveTurnInfo: vi.fn().mockReturnValue(undefined) },
  411. });
  412. const record = makeRecord({ app });
  413. await new V4CommandExecutor(
  414. makeHost(record, { getInputRoutingMode: vi.fn().mockReturnValue("enqueue") }),
  415. ).execute(envelope("sendGoalCommand", { text: "验证后继续目标" }));
  416. expect(enqueueDeferredInput).toHaveBeenCalledWith(
  417. "/goal 验证后继续目标",
  418. expect.objectContaining({ commandKind: "sendGoalCommand", delivery: "queue" }),
  419. );
  420. expect(app.setTarget).not.toHaveBeenCalled();
  421. expect(app.continueActiveTarget).not.toHaveBeenCalled();
  422. });
  423. it("active turn queue 拒绝:显式失败,不伪装成已受理", async () => {
  424. const app = makeApp({
  425. steerTurn: vi.fn().mockResolvedValue({ kind: "rejected", reason: "input_too_large" }),
  426. });
  427. const record = makeRecord({ app, activeAbortController: new AbortController() });
  428. await expect(
  429. new V4CommandExecutor(makeHost(record)).execute(
  430. envelope("sendGoalCommand", { text: "过大的目标" }),
  431. ),
  432. ).rejects.toMatchObject({ reasonCode: "proto.payloadTooLarge" });
  433. expect(app.setTarget).not.toHaveBeenCalled();
  434. });
  435. it("Plan 与 Goal 互斥:拒绝同时开启,不先落库目标", async () => {
  436. const app = makeApp({ getMode: vi.fn().mockReturnValue("plan") });
  437. const record = makeRecord({ app });
  438. const host = makeHost(record);
  439. await expect(
  440. new V4CommandExecutor(host).execute(envelope("sendGoalCommand", { text: "目标" })),
  441. ).rejects.toMatchObject({ reasonCode: "guard.planGoalMutuallyExclusive" });
  442. await settle();
  443. expect(app.setTarget).not.toHaveBeenCalled();
  444. expect(app.continueActiveTarget).not.toHaveBeenCalled();
  445. expect(record.activeAbortController).toBeUndefined();
  446. expect(host.legacyMutations).toEqual([]);
  447. });
  448. });
  449. describe("v4 原生 resumeGoal", () => {
  450. it("Plan 中不能把暂停目标恢复为 active", async () => {
  451. const app = makeApp({
  452. runtime: { getPlanEnabled: () => true },
  453. readTarget: vi.fn().mockResolvedValue({ targetID: "g1", status: "paused" }),
  454. });
  455. await expect(
  456. new V4CommandExecutor(makeHost(makeRecord({ app }))).execute(envelope("resumeGoal", {})),
  457. ).rejects.toMatchObject({ reasonCode: "guard.planGoalMutuallyExclusive" });
  458. expect(app.updateTargetStatus).not.toHaveBeenCalled();
  459. expect(app.continueActiveTarget).not.toHaveBeenCalled();
  460. });
  461. it("正路径:updateTargetStatus(active) + 后台续跑", async () => {
  462. const app = makeApp();
  463. const record = makeRecord({ app });
  464. const host = makeHost(record);
  465. await new V4CommandExecutor(host).execute(envelope("resumeGoal", {}));
  466. await settle();
  467. expect(app.updateTargetStatus).toHaveBeenCalledWith("active");
  468. expect(app.continueActiveTarget).toHaveBeenCalledWith(
  469. expect.objectContaining({ inputId: "cmd-1" }),
  470. );
  471. expect(record.activeAbortController).toBeUndefined();
  472. expect(host.legacyMutations).toContain("goal_resumed");
  473. expect(host.legacyMutations).toContain("goal_continuation_completed");
  474. });
  475. it("无 target:幂等成功(不抛错、不续跑、不广播)", async () => {
  476. const app = makeApp({ updateTargetStatus: vi.fn().mockResolvedValue(null) });
  477. const record = makeRecord({ app });
  478. const host = makeHost(record);
  479. await expect(
  480. new V4CommandExecutor(host).execute(envelope("resumeGoal", {})),
  481. ).resolves.toBeUndefined();
  482. expect(app.continueActiveTarget).not.toHaveBeenCalled();
  483. expect(host.legacyMutations).toEqual([]);
  484. });
  485. });
  486. describe("v4 原生 pauseGoal", () => {
  487. it("没有 active work 也会独立暂停 target,不触发 stop queue 语义", async () => {
  488. const app = makeApp({
  489. readTarget: vi
  490. .fn()
  491. .mockResolvedValue({ targetID: "g1", objective: "旧目标", status: "active" }),
  492. updateTargetStatus: vi
  493. .fn()
  494. .mockResolvedValue({ targetID: "g1", objective: "旧目标", status: "paused" }),
  495. });
  496. const record = makeRecord({ app });
  497. const host = makeHost(record);
  498. await new V4CommandExecutor(host).execute(envelope("pauseGoal", {}));
  499. expect(app.updateTargetStatus).toHaveBeenCalledWith("paused");
  500. expect(app.setQueueAutoDrain).not.toHaveBeenCalled();
  501. expect(app.continueActiveTarget).not.toHaveBeenCalled();
  502. expect(host.legacyMutations).toEqual(["goal_paused"]);
  503. });
  504. it("先结算 paused target,再终止当前 goal work", async () => {
  505. const controller = new AbortController();
  506. const app = makeApp({
  507. readTarget: vi
  508. .fn()
  509. .mockResolvedValue({ targetID: "g1", objective: "旧目标", status: "active" }),
  510. updateTargetStatus: vi.fn().mockImplementation(async () => {
  511. expect(controller.signal.aborted).toBe(false);
  512. return { targetID: "g1", objective: "旧目标", status: "paused" };
  513. }),
  514. });
  515. const record = makeRecord({ app, activeAbortController: controller });
  516. await new V4CommandExecutor(makeHost(record)).execute(envelope("pauseGoal", {}));
  517. expect(controller.signal.aborted).toBe(true);
  518. });
  519. it("没有 active target 时幂等 noop", async () => {
  520. const app = makeApp();
  521. const host = makeHost(makeRecord({ app }));
  522. await expect(
  523. new V4CommandExecutor(host).execute(envelope("pauseGoal", {})),
  524. ).resolves.toBeUndefined();
  525. expect(app.updateTargetStatus).not.toHaveBeenCalled();
  526. expect(host.legacyMutations).toEqual([]);
  527. });
  528. });
  529. describe("supports 分流(goal/compact 组)", () => {
  530. it("pauseGoal 已原生", () => {
  531. const executor = new V4CommandExecutor(makeHost(makeRecord()));
  532. expect(executor.supports("pauseGoal")).toBe(true);
  533. });
  534. });