| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210 |
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
- import {
- BUILTIN_MODEL_PROVIDER_IDS,
- buildCustomSupplierKey,
- type ZCodeError,
- type ZCodeConfigOption,
- type ZCodePermissionRequest,
- type ZCodeTaskMeta,
- } from "@zcode/shared";
- import {
- DEFAULT_TASK_UI_STATE,
- DEFAULT_TASK_RUNTIME_STATE,
- getTaskRuntimeState,
- getTaskUnreadIndicator,
- getTaskUiState,
- getWorkspaceDisplayedTaskState,
- useZCodeSessionStore,
- } from "../src/store/zcodeSessionStore.js";
- import { canGoForward } from "../src/lib/taskNavigationHistory.js";
- import { LAST_SELECTED_AGENT_PROVIDER_STORAGE_KEY } from "../src/lib/zcodeProviderPreference.js";
- import { encodeCustomModelValue } from "../src/lib/zcodeCustomModelValue.js";
- function createStorageMock(initial: Record<string, string> = {}) {
- const data = new Map(Object.entries(initial));
- return {
- getItem(key: string) {
- return data.get(key) ?? null;
- },
- setItem(key: string, value: string) {
- data.set(key, value);
- },
- removeItem(key: string) {
- data.delete(key);
- },
- };
- }
- function createTaskMeta(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
- return {
- taskId: "task-1",
- traceId: "trace-1",
- title: "修复任务删除联动",
- workspacePath: "/tmp/workspace-delete",
- createdAt: 1,
- updatedAt: 1,
- mode: "default",
- provider: "codex",
- ...overrides,
- };
- }
- function createPermissionRequest(
- overrides: Partial<ZCodePermissionRequest> = {},
- ): ZCodePermissionRequest {
- return {
- type: "permission_request",
- taskId: "task-1",
- traceId: "trace-1",
- requestId: "perm-1",
- description: "允许执行命令",
- kind: "execute",
- options: [
- {
- optionId: "allow-once",
- kind: "allow_once",
- name: "允许一次",
- },
- ],
- raw: { toolCallId: "tool-1" },
- ...overrides,
- };
- }
- function createZCodeError(overrides: Partial<ZCodeError> = {}): ZCodeError {
- return {
- code: "SEND_FAILED",
- message: "命令执行失败",
- traceId: "trace-error-1",
- taskId: "task-1",
- ...overrides,
- };
- }
- function createModelConfigOption(currentValue: string): ZCodeConfigOption {
- return {
- id: "model",
- name: "Model",
- category: "model",
- type: "select",
- currentValue,
- options: [{ value: currentValue, name: currentValue }],
- };
- }
- function createModeConfigOption(currentValue: string): ZCodeConfigOption {
- return {
- id: "mode",
- name: "Mode",
- category: "mode",
- type: "select",
- currentValue,
- options: [
- { value: "build", name: "Build" },
- { value: "plan", name: "Plan" },
- { value: "yolo", name: "Yolo" },
- ],
- };
- }
- function createThoughtConfigOption(currentValue: string): ZCodeConfigOption {
- return {
- id: "thought_level",
- name: "Thought Level",
- category: "thought_level",
- type: "select",
- currentValue,
- options: [
- { value: "max", name: "Max" },
- { value: "high", name: "High" },
- { value: "nothink", name: "No Think" },
- ],
- };
- }
- function readModelConfigValue(
- options: readonly ZCodeConfigOption[] | null | undefined,
- ): string | null {
- const modelOption = options?.find(
- (option) => option.category === "model" && option.type === "select",
- );
- return typeof modelOption?.currentValue === "string" ? modelOption.currentValue : null;
- }
- function readModeConfigOption(options: readonly ZCodeConfigOption[] | null | undefined) {
- return options?.find((option) => option.category === "mode" && option.type === "select") ?? null;
- }
- describe("useZCodeSessionStore provider preference", () => {
- beforeEach(() => {
- useZCodeSessionStore.setState((state) => ({
- ...state,
- workspaces: {},
- }));
- });
- afterEach(() => {
- vi.restoreAllMocks();
- vi.unstubAllGlobals();
- });
- it("setConfigOptions 会把空 thought_level 归一到第一个合法选项", () => {
- const workspacePath = "/tmp/workspace-thought-normalize";
- useZCodeSessionStore.getState().setConfigOptions(workspacePath, [
- {
- id: "thought_level",
- name: "Thought Level",
- category: "thought_level",
- type: "select",
- currentValue: "",
- options: [
- { value: "low", name: "Low" },
- { value: "medium", name: "Medium" },
- ],
- },
- ]);
- expect(
- useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath)
- .configOptions?.find((option) => option.id === "thought_level")?.currentValue,
- ).toBe("low");
- });
- it.each([
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, "glm-5.2-pro"],
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan, "GLM-5.2"],
- ["default-glm", "glm-0606[1m]"],
- ])("setConfigOptions 保留 %s/%s 的 high 思考强度", (providerId, modelId) => {
- const workspacePath = `/tmp/workspace-hide-high-${providerId}-${modelId}`;
- useZCodeSessionStore.getState().setConfigOptions(workspacePath, [
- createModelConfigOption(`${providerId}/${modelId}`),
- {
- id: "thought_level",
- name: "Thought Level",
- category: "thought_level",
- type: "select",
- currentValue: "high",
- options: [
- { value: "max", name: "Max" },
- { value: "high", name: "High" },
- { value: "nothink", name: "No Thinking" },
- ],
- },
- ]);
- const thoughtOption = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath)
- .configOptions?.find((option) => option.id === "thought_level");
- expect(thoughtOption?.options?.map((option) => option.value)).toEqual([
- "max",
- "high",
- "nothink",
- ]);
- expect(thoughtOption?.currentValue).toBe("high");
- });
- it("setConfigOptions 保留无 provider 的 glm-date 模型 high 思考强度", () => {
- const workspacePath = "/tmp/workspace-hide-high-plain-glm-date";
- useZCodeSessionStore.getState().setConfigOptions(workspacePath, [
- createModelConfigOption("glm-0606[1m]"),
- {
- id: "thought_level",
- name: "Thought Level",
- category: "thought_level",
- type: "select",
- currentValue: "high",
- options: [
- { value: "max", name: "Max" },
- { value: "high", name: "High" },
- { value: "nothink", name: "No Thinking" },
- ],
- },
- ]);
- const thoughtOption = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath)
- .configOptions?.find((option) => option.id === "thought_level");
- expect(thoughtOption?.options?.map((option) => option.value)).toEqual([
- "max",
- "high",
- "nothink",
- ]);
- expect(thoughtOption?.currentValue).toBe("high");
- });
- it("bindRuntimeProvider 会归一旧 provider,不覆盖 supplier 且不改本地偏好", () => {
- const storage = createStorageMock();
- vi.stubGlobal("window", { localStorage: storage });
- const workspacePath = "/tmp/workspace-runtime-bind";
- const customSupplierKey = buildCustomSupplierKey("provider-a");
- useZCodeSessionStore.getState().startDraft(workspacePath, "codex");
- useZCodeSessionStore.getState().setModelSelectionResolution(workspacePath, {
- selectedSupplierKey: customSupplierKey,
- isGhostSupplier: false,
- supplierMismatchReason: null,
- });
- useZCodeSessionStore.getState().bindRuntimeProvider(workspacePath, "opencode");
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(storage.getItem(LAST_SELECTED_AGENT_PROVIDER_STORAGE_KEY)).toBe("glm");
- expect(workspaceState.selectedProvider).toBe("glm");
- expect(workspaceState.selectedSupplierKey).toBe(customSupplierKey);
- });
- it("composer 文本插入请求按 workspaceIdentity 隔离且只能由对应版本清理", () => {
- const workspacePath = "/tmp/workspace-composer-prefill";
- const workspaceIdentityA = "ssh://user@host-a:/tmp/workspace-composer-prefill";
- const workspaceIdentityB = "ssh://user@host-b:/tmp/workspace-composer-prefill";
- const store = useZCodeSessionStore.getState();
- expect(store.requestComposerTextInsert(workspacePath, "first", workspaceIdentityA)).toBe(1);
- expect(
- store.requestComposerTextInsert(workspacePath, "second", workspaceIdentityA, {
- id: "plugin:demo@mkt",
- category: "plugins",
- label: "Demo",
- value: "demo@mkt",
- markdown: "[@Demo](plugin://demo@mkt)",
- data: {
- pluginId: "demo@mkt",
- icon: "https://cdn.example.com/demo.png",
- },
- }),
- ).toBe(2);
- store.requestComposerTextInsert(workspacePath, "isolated", workspaceIdentityB);
- store.requestComposerTextInsert(
- workspacePath,
- "[@Demo](plugin://demo@mkt)",
- workspaceIdentityA,
- {
- id: "plugin:demo@mkt",
- category: "plugins",
- label: "Demo",
- value: "demo@mkt",
- markdown: "[@Demo](plugin://demo@mkt)",
- data: { pluginId: "demo@mkt" },
- },
- "prepend-if-missing",
- );
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityA)
- .composerTextInsertRequest,
- ).toEqual({
- requestId: 3,
- text: "[@Demo](plugin://demo@mkt)",
- mention: {
- id: "plugin:demo@mkt",
- category: "plugins",
- label: "Demo",
- value: "demo@mkt",
- markdown: "[@Demo](plugin://demo@mkt)",
- data: {
- pluginId: "demo@mkt",
- },
- },
- mode: "prepend-if-missing",
- });
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityB)
- .composerTextInsertRequest,
- ).toEqual({ requestId: 1, text: "isolated" });
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, 1, workspaceIdentityA);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityA)
- .composerTextInsertRequest,
- ).toEqual(expect.objectContaining({ requestId: 3, mode: "prepend-if-missing" }));
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, 2, workspaceIdentityA);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityA)
- .composerTextInsertRequest,
- ).toEqual(expect.objectContaining({ requestId: 3, mode: "prepend-if-missing" }));
- useZCodeSessionStore
- .getState()
- .clearComposerTextInsertRequest(workspacePath, 3, workspaceIdentityA);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityA)
- .composerTextInsertRequest,
- ).toBeNull();
- });
- it("时间线到底请求即使目标 task 未切换也会递增,并且只能由对应版本清理", () => {
- const workspacePath = "/tmp/workspace-timeline-bottom";
- const workspaceIdentity = "ssh://user@host:/tmp/workspace-timeline-bottom";
- const store = useZCodeSessionStore.getState();
- expect(store.requestTimelineBottom(workspacePath, "task-1", workspaceIdentity)).toBe(1);
- expect(store.requestTimelineBottom(workspacePath, "task-1", workspaceIdentity)).toBe(2);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .timelineBottomRequest,
- ).toEqual({ requestId: 2, taskId: "task-1" });
- store.clearTimelineBottomRequest(workspacePath, 1, workspaceIdentity);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .timelineBottomRequest,
- ).toEqual({ requestId: 2, taskId: "task-1" });
- store.clearTimelineBottomRequest(workspacePath, 2, workspaceIdentity);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .timelineBottomRequest,
- ).toBeNull();
- });
- it("bindRuntimeProvider 在同路径不同 workspaceIdentity 下不应互相覆盖 supplier", () => {
- const workspacePath = "/tmp/workspace-runtime-identity";
- const workspaceIdentityA = "ssh://user@host-a:/tmp/workspace-runtime-identity";
- const workspaceIdentityB = "ssh://user@host-b:/tmp/workspace-runtime-identity";
- const customSupplierKeyA = buildCustomSupplierKey("provider-a");
- const customSupplierKeyB = buildCustomSupplierKey("provider-b");
- useZCodeSessionStore.getState().startDraft(workspacePath, "claude", workspaceIdentityA);
- useZCodeSessionStore.getState().setModelSelectionResolution(
- workspacePath,
- {
- selectedSupplierKey: customSupplierKeyA,
- isGhostSupplier: false,
- supplierMismatchReason: null,
- },
- workspaceIdentityA,
- );
- useZCodeSessionStore
- .getState()
- .bindRuntimeProvider(workspacePath, "claude", workspaceIdentityA);
- useZCodeSessionStore.getState().startDraft(workspacePath, "opencode", workspaceIdentityB);
- useZCodeSessionStore.getState().setModelSelectionResolution(
- workspacePath,
- {
- selectedSupplierKey: customSupplierKeyB,
- isGhostSupplier: false,
- supplierMismatchReason: null,
- },
- workspaceIdentityB,
- );
- useZCodeSessionStore
- .getState()
- .bindRuntimeProvider(workspacePath, "opencode", workspaceIdentityB);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityA)
- .selectedSupplierKey,
- ).toBe(customSupplierKeyA);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentityB)
- .selectedSupplierKey,
- ).toBe(customSupplierKeyB);
- });
- it("同一 remote workspace overlay 连续读取时应复用同一个快照引用", () => {
- const workspacePath = "/tmp/workspace-identity-overlay-snapshot";
- const workspaceIdentity = "ssh://user@host:/tmp/workspace-identity-overlay-snapshot";
- // M5②:旧 selectDraftProvider 的"归一后 no-op 不写 identity 桶"路径已随动作删除;
- // 这里保留 identity 读取回落 path 桶时快照引用复用的存活语义。
- useZCodeSessionStore.getState().startDraft(workspacePath, "opencode");
- const firstSnapshot = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- const secondSnapshot = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- expect(firstSnapshot.selectedProvider).toBe("glm");
- expect(firstSnapshot).toBe(secondSnapshot);
- useZCodeSessionStore.getState().setTaskUnreadIndicator(workspacePath, "task-1", true);
- const thirdSnapshot = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- expect(thirdSnapshot).not.toBe(firstSnapshot);
- expect(thirdSnapshot.selectedProvider).toBe("glm");
- expect(thirdSnapshot.taskUnreadByTaskId["task-1"]).toBe(true);
- });
- it("关闭分组草稿只清除当前 identity 的内存分组,不重建草稿或污染同路径", () => {
- const store = useZCodeSessionStore.getState();
- const path = "/group-draft";
- const identity = "remote:server:other:/group-draft";
- store.startDraft(path, undefined, undefined, { projectId: "local", taskGroupId: "local-g" });
- store.startDraft(path, undefined, identity, { projectId: "remote", taskGroupId: "remote-g" });
- const local = useZCodeSessionStore.getState().getWorkspaceState(path);
- const remote = useZCodeSessionStore.getState().getWorkspaceState(path, identity);
- store.clearDraftTaskGroup(path, identity);
- expect(useZCodeSessionStore.getState().getWorkspaceState(path)).toBe(local);
- expect(useZCodeSessionStore.getState().getWorkspaceState(path, identity)).toEqual({
- ...remote,
- draftTaskGroupId: undefined,
- });
- });
- it("同 cwd 切换草稿项目只重建预热实例,已提交任务不改变归属", () => {
- const store = useZCodeSessionStore.getState();
- const path = "/project-draft";
- store.startDraft(path, undefined, undefined, { projectId: "a" });
- const first = useZCodeSessionStore.getState().getWorkspaceState(path);
- expect(first.draftProjectId).toBe("a");
- store.startDraft(path, undefined, undefined, { projectId: "a" });
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(path).draftRuntimeInvalidationVersion,
- ).toBe(first.draftRuntimeInvalidationVersion);
- store.startDraft(path, undefined, undefined, { projectId: "b" });
- expect(useZCodeSessionStore.getState().getWorkspaceState(path)).toMatchObject({
- draftProjectId: "b",
- draftRuntimeInvalidationVersion: first.draftRuntimeInvalidationVersion + 1,
- });
- store.startDraft(path, undefined, undefined, { projectId: null });
- expect(useZCodeSessionStore.getState().getWorkspaceState(path).draftProjectId).toBeNull();
- });
- it("workspaceIdentity overlay 应优先读取 identity 桶的瞬时状态", () => {
- const workspacePath = "/tmp/workspace-remote-transient-state";
- const workspaceIdentity =
- "remote:ssh:localhost:2222:root:/tmp/workspace-remote-transient-state";
- const taskId = "task-remote-transient";
- const identityOptions = [createModelConfigOption("glm-identity")];
- const pathOptions = [createModelConfigOption("glm-path")];
- useZCodeSessionStore.getState().startDraft(workspacePath, undefined, workspaceIdentity);
- useZCodeSessionStore.getState().startDraft(workspacePath, undefined, workspaceIdentity);
- useZCodeSessionStore.getState().bumpTaskListVersion(workspacePath, workspaceIdentity);
- useZCodeSessionStore.getState().bumpTaskListVersion(workspacePath, workspaceIdentity);
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, taskId, identityOptions, workspaceIdentity);
- useZCodeSessionStore
- .getState()
- .setTaskUnreadIndicator(workspacePath, taskId, true, workspaceIdentity);
- useZCodeSessionStore.getState().startDraft(workspacePath);
- useZCodeSessionStore.getState().bumpTaskListVersion(workspacePath);
- useZCodeSessionStore.getState().setTaskConfigOptions(workspacePath, taskId, pathOptions);
- useZCodeSessionStore.getState().setTaskUnreadIndicator(workspacePath, taskId, false);
- const workspaceState = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- expect(readModelConfigValue(workspaceState.taskConfigOptionsByTaskId[taskId])).toBe(
- "glm-identity",
- );
- expect(workspaceState.taskUnreadByTaskId[taskId]).toBe(true);
- expect(workspaceState.taskListVersion).toBe(2);
- expect(workspaceState.draftFocusVersion).toBe(2);
- });
- it("startDraft 传入同 provider 时不应把 custom supplier 重置为 native", () => {
- const workspacePath = "/tmp/workspace-draft-same-provider";
- const customSupplierKey = buildCustomSupplierKey("provider-a");
- useZCodeSessionStore.getState().startDraft(workspacePath, "claude");
- useZCodeSessionStore.getState().setModelSelectionResolution(workspacePath, {
- selectedSupplierKey: customSupplierKey,
- isGhostSupplier: false,
- supplierMismatchReason: null,
- });
- useZCodeSessionStore.getState().startDraft(workspacePath, "claude");
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(workspaceState.selectedProvider).toBe("glm");
- expect(workspaceState.selectedSupplierKey).toBe(customSupplierKey);
- });
- it("startDraft 会递增草稿聚焦版本,供输入框在新建任务后立即聚焦", () => {
- const storage = createStorageMock();
- vi.stubGlobal("window", { localStorage: storage });
- const workspacePath = "/tmp/workspace-focus";
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).draftFocusVersion).toBe(
- 0,
- );
- useZCodeSessionStore.getState().startDraft(workspacePath);
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).draftFocusVersion).toBe(
- 1,
- );
- useZCodeSessionStore.getState().startDraft(workspacePath, "opencode");
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).draftFocusVersion).toBe(
- 2,
- );
- });
- it("startDraft 从 active task 新建且透传同 provider 时会继承输入框完整配置", () => {
- const workspacePath = "/tmp/workspace-new-task-inherit-active-config";
- const taskId = "task-provider-a";
- const taskModel = "provider-a/model-a";
- const workspaceModel = "account:bigmodel-individual-coding-plan/GLM-5.2";
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, [createModelConfigOption(workspaceModel)]);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, taskId);
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, taskId, [
- createModelConfigOption(taskModel),
- createModeConfigOption("yolo"),
- createThoughtConfigOption("max"),
- ]);
- useZCodeSessionStore.getState().startDraft(workspacePath, "glm");
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(workspaceState.activeTaskId).toBeNull();
- expect(readModelConfigValue(workspaceState.configOptions)).toBe(taskModel);
- expect(readModeConfigOption(workspaceState.configOptions)?.currentValue).toBe("yolo");
- expect(
- workspaceState.configOptions?.find((option) => option.category === "thought_level")
- ?.currentValue,
- ).toBe("max");
- });
- it("草稿态重复点击新建任务时不应清空 slashCommands", () => {
- const workspacePath = "/tmp/workspace-draft-slash-commands";
- const slashCommands = [
- { name: "status", description: "查看当前状态" },
- { name: "review", description: "发起代码审查" },
- ];
- useZCodeSessionStore.getState().setSlashCommands(workspacePath, slashCommands);
- useZCodeSessionStore.getState().startDraft(workspacePath);
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).slashCommands).toEqual(
- slashCommands,
- );
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task-from-list");
- useZCodeSessionStore.getState().startDraft(workspacePath);
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).slashCommands).toEqual(
- [],
- );
- });
- it("workspaceIdentity 会隔离 workspace 级命令、初始化与模型切换状态", () => {
- const workspacePath = "/tmp/workspace-same-path-state";
- const identityA = "ssh://host-a/tmp/workspace-same-path-state";
- const identityB = "ssh://host-b/tmp/workspace-same-path-state";
- const slashCommands = [{ name: "review", description: "发起代码审查" }];
- useZCodeSessionStore.getState().setSlashCommands(workspacePath, slashCommands, identityA);
- useZCodeSessionStore.getState().setWorkspaceInitState(workspacePath, "ready", null, identityA);
- useZCodeSessionStore
- .getState()
- .startModelSwitch(workspacePath, "switch-a", "settingModel", identityA);
- const workspaceStateA = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, identityA);
- const workspaceStateB = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, identityB);
- expect(workspaceStateA.slashCommands).toEqual(slashCommands);
- expect(workspaceStateB.slashCommands).toEqual([]);
- expect(workspaceStateA.workspaceInit.status).toBe("ready");
- expect(workspaceStateB.workspaceInit.status).toBe("idle");
- expect(workspaceStateA.modelSwitchRequestId).toBe("switch-a");
- expect(workspaceStateA.modelSwitchPending).toBe(true);
- expect(workspaceStateB.modelSwitchRequestId).toBeNull();
- expect(workspaceStateB.modelSwitchPending).toBe(false);
- });
- it("workspaceIdentity 已存在时不再动态合并 path 桶里的 task config", () => {
- const workspacePath = "/tmp/workspace-same-path-model-config";
- const identityA = "ssh://host-a/tmp/workspace-same-path-model-config";
- const identityB = "ssh://host-b/tmp/workspace-same-path-model-config";
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, "task-1", [createModelConfigOption("glm-path")]);
- useZCodeSessionStore.getState().startDraft(workspacePath, "glm", identityA);
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, [createModelConfigOption("glm-identity-b")], identityB);
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(
- workspacePath,
- "task-1",
- [createModelConfigOption("glm-identity-a")],
- identityA,
- );
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, identityA)
- .taskConfigOptionsByTaskId["task-1"]?.[0]?.currentValue,
- ).toBe("glm-identity-a");
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, identityB)
- .taskConfigOptionsByTaskId["task-1"],
- ).toBeUndefined();
- });
- it("workspaceIdentity 首次写入只迁移明确属于该 identity 的 path task 状态", () => {
- const workspacePath = "/tmp/workspace-same-path-task-cache";
- const workspaceIdentity = "ssh://host-a/tmp/workspace-same-path-task-cache";
- const otherWorkspaceIdentity = "ssh://host-b/tmp/workspace-same-path-task-cache";
- const taskId = "identity-task";
- const otherTaskId = "other-task";
- useZCodeSessionStore.getState().setTaskListCache(workspacePath, [
- createTaskMeta({
- taskId,
- workspacePath,
- workspaceIdentity,
- model: "glm-path",
- }),
- createTaskMeta({
- taskId: otherTaskId,
- workspacePath,
- workspaceIdentity: otherWorkspaceIdentity,
- model: "glm-other",
- }),
- ]);
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, taskId, [createModelConfigOption("glm-path")]);
- useZCodeSessionStore.getState().setTaskRuntimeState(workspacePath, taskId, "streaming");
- useZCodeSessionStore
- .getState()
- .setTaskError(workspacePath, taskId, createZCodeError({ taskId }));
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, otherTaskId, [createModelConfigOption("glm-other")]);
- useZCodeSessionStore
- .getState()
- .setConfigOptions(
- workspacePath,
- [createModelConfigOption("glm-identity")],
- workspaceIdentity,
- );
- const workspaceState = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- expect(workspaceState.configOptions?.[0]?.currentValue).toBe("glm-identity");
- expect(workspaceState.taskListCache?.map((task) => task.taskId)).toEqual([taskId]);
- expect(readModelConfigValue(workspaceState.taskConfigOptionsByTaskId[taskId])).toBe("glm-path");
- expect(getTaskRuntimeState(workspaceState, taskId).status).toBe("streaming");
- expect(getTaskUiState(workspaceState, taskId).error?.message).toBe("命令执行失败");
- expect(workspaceState.taskConfigOptionsByTaskId[otherTaskId]).toBeUndefined();
- });
- it("modelSwitch 状态只允许最新 requestId 生效与收敛", () => {
- const workspacePath = "/tmp/workspace-model-switch";
- const requestId1 = "switch-1";
- const requestId2 = "switch-2";
- useZCodeSessionStore.getState().startModelSwitch(workspacePath, requestId1, "settingModel");
- useZCodeSessionStore
- .getState()
- .updateModelSwitchStage(workspacePath, requestId1, "restartingRuntime");
- useZCodeSessionStore.getState().startModelSwitch(workspacePath, requestId2, "syncingSession");
- // 旧请求结束不应覆盖新请求状态
- useZCodeSessionStore.getState().finishModelSwitch(workspacePath, requestId1);
- let workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(workspaceState.modelSwitchPending).toBe(true);
- expect(workspaceState.modelSwitchRequestId).toBe(requestId2);
- expect(workspaceState.modelSwitchStage).toBe("syncingSession");
- // 当前请求结束后才回到 idle
- useZCodeSessionStore.getState().finishModelSwitch(workspacePath, requestId2);
- workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(workspaceState.modelSwitchPending).toBe(false);
- expect(workspaceState.modelSwitchRequestId).toBeNull();
- expect(workspaceState.modelSwitchStage).toBe("idle");
- });
- it("removeTaskState 删除当前任务时会同步关闭右侧详情态", () => {
- const workspacePath = "/tmp/workspace-delete";
- const taskId = "task-active";
- useZCodeSessionStore
- .getState()
- .upsertOptimisticTaskListItem(workspacePath, createTaskMeta({ taskId, workspacePath }));
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, taskId);
- useZCodeSessionStore.getState().setTaskRuntimeState(workspacePath, taskId, "streaming");
- useZCodeSessionStore.getState().removeTaskState(workspacePath, taskId);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(workspaceState.activeTaskId).toBeNull();
- expect(getWorkspaceDisplayedTaskState(workspaceState).taskStatus).toBe("idle");
- expect(getWorkspaceDisplayedTaskState(workspaceState).taskError).toBeNull();
- expect(workspaceState.optimisticTaskListByTaskId[taskId]).toBeUndefined();
- expect(workspaceState.taskRuntimeByTaskId[taskId]).toBeUndefined();
- });
- it("权限请求会按 task 维度保存,切换任务后仍可恢复", () => {
- const workspacePath = "/tmp/workspace-task-ui";
- const firstTaskId = "task-plan";
- const secondTaskId = "task-empty";
- const permissionRequest = createPermissionRequest({ taskId: firstTaskId });
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, firstTaskId, permissionRequest);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, firstTaskId);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, secondTaskId);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, firstTaskId);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(getTaskUiState(workspaceState, firstTaskId)).toEqual({
- ...DEFAULT_TASK_UI_STATE,
- permissionRequest,
- });
- expect(getTaskUiState(workspaceState, secondTaskId)).toEqual(DEFAULT_TASK_UI_STATE);
- });
- it("问答草稿会按 workspace、task 和 request 隔离,并可在请求收口后清理", () => {
- const workspacePath = "/tmp/workspace-elicitation-draft";
- const firstTaskId = "task-a";
- const secondTaskId = "task-b";
- const requestId = "ask-1";
- const workspaceIdentity = "ssh://host-a/workspace";
- const draft = {
- questionIndex: 1,
- drafts: {
- "0:方案": { selectedValues: ["safe"], customAnswer: "" },
- },
- };
- useZCodeSessionStore
- .getState()
- .setTaskElicitationFormDraft(workspacePath, firstTaskId, requestId, draft, workspaceIdentity);
- let workspaceState = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- expect(
- getTaskUiState(workspaceState, firstTaskId).elicitationFormDraftsByRequestId[requestId],
- ).toEqual(draft);
- expect(
- getTaskUiState(workspaceState, secondTaskId).elicitationFormDraftsByRequestId[requestId],
- ).toBeUndefined();
- const otherWorkspaceState = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, "ssh://host-b/workspace");
- expect(
- getTaskUiState(otherWorkspaceState, firstTaskId).elicitationFormDraftsByRequestId[requestId],
- ).toBeUndefined();
- useZCodeSessionStore
- .getState()
- .removeTaskElicitationFormDraft(workspacePath, firstTaskId, requestId, workspaceIdentity);
- workspaceState = useZCodeSessionStore
- .getState()
- .getWorkspaceState(workspacePath, workspaceIdentity);
- expect(
- getTaskUiState(workspaceState, firstTaskId).elicitationFormDraftsByRequestId[requestId],
- ).toBeUndefined();
- });
- it("连续收到多个权限请求时会按顺序排队,响应当前请求后自动切到下一条", () => {
- const workspacePath = "/tmp/workspace-task-permission-queue";
- const taskId = "task-permission-queue";
- const firstPermissionRequest = createPermissionRequest({
- taskId,
- requestId: "perm-1",
- description: "允许读取 about.txt",
- });
- const secondPermissionRequest = createPermissionRequest({
- taskId,
- requestId: "perm-2",
- description: "允许读取 setting.json",
- });
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, taskId, firstPermissionRequest);
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, taskId, secondPermissionRequest);
- let workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(getTaskUiState(workspaceState, taskId)).toEqual({
- ...DEFAULT_TASK_UI_STATE,
- permissionRequest: firstPermissionRequest,
- pendingPermissionRequests: [secondPermissionRequest],
- });
- useZCodeSessionStore.getState().removeTaskPermissionRequest(workspacePath, taskId, "perm-1");
- workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(getTaskUiState(workspaceState, taskId)).toEqual({
- ...DEFAULT_TASK_UI_STATE,
- permissionRequest: secondPermissionRequest,
- });
- });
- it("重复收到相同权限请求时不会重建 task UI state", () => {
- const workspacePath = "/tmp/workspace-task-permission-duplicate";
- const taskId = "task-permission-duplicate";
- const firstPermissionRequest = createPermissionRequest({ taskId, requestId: "perm-duplicate" });
- const duplicatePermissionRequest = createPermissionRequest({
- taskId,
- requestId: "perm-duplicate",
- });
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, taskId, firstPermissionRequest);
- const firstWorkspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- const firstTaskUiState = getTaskUiState(firstWorkspaceState, taskId);
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, taskId, duplicatePermissionRequest);
- const secondWorkspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(secondWorkspaceState).toBe(firstWorkspaceState);
- expect(getTaskUiState(secondWorkspaceState, taskId)).toBe(firstTaskUiState);
- });
- it("重复清理已移除的权限请求时不会重建 task UI state", () => {
- const workspacePath = "/tmp/workspace-task-permission-response-duplicate";
- const taskId = "task-permission-response-duplicate";
- const permissionRequest = createPermissionRequest({ taskId, requestId: "perm-response" });
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, taskId, permissionRequest);
- useZCodeSessionStore
- .getState()
- .removeTaskPermissionRequest(workspacePath, taskId, "perm-response");
- const firstWorkspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- useZCodeSessionStore
- .getState()
- .removeTaskPermissionRequest(workspacePath, taskId, "perm-response");
- const secondWorkspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(secondWorkspaceState).toBe(firstWorkspaceState);
- });
- it("task 错误会按 task 维度保存在内存里,切换任务后仍可恢复", () => {
- const workspacePath = "/tmp/workspace-task-error";
- const firstTaskId = "task-error";
- const secondTaskId = "task-other";
- const error = createZCodeError({ taskId: firstTaskId });
- useZCodeSessionStore.getState().setTaskError(workspacePath, firstTaskId, error);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, firstTaskId);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, secondTaskId);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, firstTaskId);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(getTaskUiState(workspaceState, firstTaskId).error).toEqual(error);
- expect(getTaskUiState(workspaceState, secondTaskId).error).toBeNull();
- });
- it("草稿态错误会保留在 workspace 内存里,并在重新新建草稿时清空", () => {
- const workspacePath = "/tmp/workspace-draft-error";
- const error = createZCodeError({ taskId: undefined });
- useZCodeSessionStore.getState().setDraftError(workspacePath, error);
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).draftError).toEqual(
- error,
- );
- useZCodeSessionStore.getState().startDraft(workspacePath);
- expect(useZCodeSessionStore.getState().getWorkspaceState(workspacePath).draftError).toBeNull();
- });
- it("当前 task 被右键标记未读后,再次打开会同时清掉 optimistic 蓝点", () => {
- const workspacePath = "/tmp/workspace-task-read";
- const taskId = "task-unread";
- useZCodeSessionStore
- .getState()
- .upsertOptimisticTaskListItem(workspacePath, createTaskMeta({ taskId, workspacePath }));
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, taskId);
- useZCodeSessionStore.getState().setTaskUnreadIndicator(workspacePath, taskId, true);
- useZCodeSessionStore
- .getState()
- .upsertOptimisticTaskListItem(
- workspacePath,
- createTaskMeta({ taskId, workspacePath, unreadAt: 123 }),
- );
- expect(
- getTaskUnreadIndicator(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath),
- taskId,
- ),
- ).toBe(true);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, taskId);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(getTaskUnreadIndicator(workspaceState, taskId)).toBe(false);
- expect(workspaceState.optimisticTaskListByTaskId[taskId]?.unreadAt).toBeUndefined();
- });
- it("重复清理已读 task 时不会重建 workspace state", () => {
- const workspacePath = "/tmp/workspace-task-read-noop";
- const taskId = "task-unread-noop";
- useZCodeSessionStore.getState().setTaskUnreadIndicator(workspacePath, taskId, true);
- useZCodeSessionStore.getState().setTaskUnreadIndicator(workspacePath, taskId, false);
- const firstWorkspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- useZCodeSessionStore.getState().setTaskUnreadIndicator(workspacePath, taskId, false);
- const secondWorkspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(secondWorkspaceState).toBe(firstWorkspaceState);
- });
- it("切到已有 task 时会用 task meta 预热 task 配置,且不覆盖 workspace 默认配置", () => {
- const workspacePath = "/tmp/workspace-task-model-preload";
- const taskId = "task-glm51";
- const staleWorkspaceModel = "glm-0531[1m]";
- const taskModel = "bigmodel-api/GLM-5.1";
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, [createModelConfigOption(staleWorkspaceModel)]);
- useZCodeSessionStore.getState().upsertOptimisticTaskListItem(
- workspacePath,
- createTaskMeta({
- taskId,
- workspacePath,
- provider: "glm",
- model: taskModel,
- }),
- );
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, taskId);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(readModelConfigValue(workspaceState.configOptions)).toBe(staleWorkspaceModel);
- expect(readModelConfigValue(workspaceState.taskConfigOptionsByTaskId[taskId])).toBe(
- encodeCustomModelValue("bigmodel-api", "GLM-5.1"),
- );
- expect(workspaceState.taskConfigOptionsStatusByTaskId[taskId]).toBe("loading");
- });
- it("active task 配置更新只写 task 配置桶,不覆盖 workspace 默认配置", () => {
- const workspacePath = "/tmp/workspace-task-config-isolation";
- const taskId = "task-active-config";
- const workspaceModel = "glm-0531[1m]";
- const taskModel = "bigmodel-api/GLM-5.1";
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, [createModelConfigOption(workspaceModel)]);
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, taskId);
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, taskId, [createModelConfigOption(taskModel)]);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(readModelConfigValue(workspaceState.configOptions)).toBe(workspaceModel);
- expect(readModelConfigValue(workspaceState.taskConfigOptionsByTaskId[taskId])).toBe(taskModel);
- expect(workspaceState.taskConfigOptionsStatusByTaskId[taskId]).toBe("ready");
- });
- it("相同 workspace 配置不会重复通知订阅者", () => {
- const workspacePath = "/tmp/workspace-config-options-dedupe";
- const modelValue = "account:zai-start-plan/GLM-5.2";
- let notificationCount = 0;
- const unsubscribe = useZCodeSessionStore.subscribe(() => {
- notificationCount += 1;
- });
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, [createModelConfigOption(modelValue)]);
- expect(notificationCount).toBe(1);
- useZCodeSessionStore
- .getState()
- .setConfigOptions(workspacePath, [createModelConfigOption(modelValue)]);
- unsubscribe();
- expect(notificationCount).toBe(1);
- });
- it("相同 task 配置不会重复通知订阅者", () => {
- const workspacePath = "/tmp/workspace-task-config-options-dedupe";
- const taskId = "task-config-dedupe";
- const modelValue = "account:zai-start-plan/GLM-5.2";
- let notificationCount = 0;
- const unsubscribe = useZCodeSessionStore.subscribe(() => {
- notificationCount += 1;
- });
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, taskId, [createModelConfigOption(modelValue)]);
- expect(notificationCount).toBe(1);
- useZCodeSessionStore
- .getState()
- .setTaskConfigOptions(workspacePath, taskId, [createModelConfigOption(modelValue)]);
- unsubscribe();
- expect(notificationCount).toBe(1);
- });
- it("任务前进后退选择目标时不会覆盖浏览器式历史位置", () => {
- const workspacePath = "/tmp/workspace-task-nav";
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task-a");
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task-b");
- const backEntry = useZCodeSessionStore.getState().taskNavGoBack();
- expect(backEntry?.taskId).toBe("task-a");
- useZCodeSessionStore.getState().setActiveTaskId(workspacePath, "task-a");
- expect(canGoForward(useZCodeSessionStore.getState().taskNavHistory)).toBe(true);
- expect(useZCodeSessionStore.getState().taskNavGoForward()?.taskId).toBe("task-b");
- });
- it("removeTaskState 会同时清理 task 级权限请求", () => {
- const workspacePath = "/tmp/workspace-remove-task-ui";
- const taskId = "task-remove-ui";
- useZCodeSessionStore
- .getState()
- .setTaskPermissionRequest(workspacePath, taskId, createPermissionRequest({ taskId }));
- useZCodeSessionStore.getState().removeTaskState(workspacePath, taskId);
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(workspaceState.taskUiByTaskId[taskId]).toBeUndefined();
- });
- it("task usage 会按 task 保存,切换运行态时不丢失", () => {
- const workspacePath = "/tmp/workspace-task-usage";
- const taskId = "task-usage";
- useZCodeSessionStore.getState().setTaskUsage(workspacePath, taskId, {
- used: 53000,
- size: 200000,
- cost: { amount: 0.045, currency: "USD" },
- });
- useZCodeSessionStore.getState().setTaskRuntimeState(workspacePath, taskId, "streaming");
- useZCodeSessionStore.getState().setTaskRuntimeState(workspacePath, taskId, "completed");
- const workspaceState = useZCodeSessionStore.getState().getWorkspaceState(workspacePath);
- expect(getTaskRuntimeState(workspaceState, taskId)).toEqual({
- ...DEFAULT_TASK_RUNTIME_STATE,
- status: "completed",
- error: null,
- apiRetry: null,
- usage: {
- used: 53000,
- size: 200000,
- cost: { amount: 0.045, currency: "USD" },
- },
- });
- });
- it("task contextWindow 单独更新,不覆盖已有 usage", () => {
- const workspacePath = "/tmp/workspace-task-context-window";
- const taskId = "task-context-window";
- useZCodeSessionStore.getState().setTaskUsage(workspacePath, taskId, {
- used: 11804,
- size: 200000,
- cost: null,
- cache: {
- inputTokens: 100,
- cacheReadTokens: 40,
- cacheWriteTokens: 0,
- hitRate: 0.4,
- },
- breakdown: [{ source: "messages", chars: 1200 }],
- });
- useZCodeSessionStore.getState().setTaskContextWindow(workspacePath, taskId, 1000000);
- const runtime = getTaskRuntimeState(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath),
- taskId,
- );
- expect(runtime.contextWindow).toBe(1000000);
- expect(runtime.usage).toEqual({
- used: 11804,
- size: 200000,
- cost: null,
- cache: {
- inputTokens: 100,
- cacheReadTokens: 40,
- cacheWriteTokens: 0,
- hitRate: 0.4,
- },
- breakdown: [{ source: "messages", chars: 1200 }],
- });
- });
- it("相同 task usage 不重复通知订阅者", () => {
- const workspacePath = "/tmp/workspace-task-usage-dedupe";
- const taskId = "task-usage-dedupe";
- let notificationCount = 0;
- const unsubscribe = useZCodeSessionStore.subscribe(() => {
- notificationCount += 1;
- });
- useZCodeSessionStore.getState().setTaskUsage(workspacePath, taskId, {
- used: 53000,
- size: 200000,
- cost: { amount: 0.045, currency: "USD" },
- });
- expect(notificationCount).toBe(1);
- useZCodeSessionStore.getState().setTaskUsage(workspacePath, taskId, {
- used: 53000,
- size: 200000,
- cost: { amount: 0.045, currency: "USD" },
- });
- unsubscribe();
- expect(notificationCount).toBe(1);
- });
- it("configOptionsStatus 写入 workspace identity 时不污染 path 桶", () => {
- const workspacePath = "/tmp/workspace-config-status-identity";
- const workspaceIdentity = "ssh://host-a/tmp/workspace-config-status-identity";
- useZCodeSessionStore.getState().setConfigOptionsStatus(workspacePath, "loading");
- useZCodeSessionStore
- .getState()
- .setConfigOptionsStatus(workspacePath, "error", workspaceIdentity);
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath).configOptionsStatus,
- ).toBe("loading");
- expect(
- useZCodeSessionStore.getState().getWorkspaceState(workspacePath, workspaceIdentity)
- .configOptionsStatus,
- ).toBe("error");
- });
- });
|