| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- import { describe, expect, it, vi } from "vitest";
- import {
- ZCODE_PROTOCOL_NAME,
- ZCODE_PROTOCOL_VERSION,
- type ZCodeMessageWithParts,
- type ZCodeSessionStateSnapshot,
- type ZCodeTaskSnapshot,
- } from "@zcode/shared";
- import {
- readDesktopContinuousSessionRestoreSnapshot,
- readWebRemoteReplayableSessionConfigOptions,
- } from "../src/lib/zcodeSessionRestore.js";
- const workspace = {
- workspacePath: "/workspace/app",
- workspaceIdentity: "ssh://dev/workspace/app",
- workspaceKey: "ssh://dev/workspace/app",
- };
- const model = { providerId: "glm", modelId: "glm-4.6" };
- function createSnapshot(params: {
- title?: string;
- messages?: ZCodeMessageWithParts[];
- } = {}): ZCodeSessionStateSnapshot {
- return {
- protocol: {
- name: ZCODE_PROTOCOL_NAME,
- version: ZCODE_PROTOCOL_VERSION,
- },
- session: {
- sessionId: "sess_1",
- workspace,
- sessionKind: "interactive",
- title: params.title ?? "Existing session",
- mode: "build",
- status: "idle",
- model,
- createdAt: 1,
- updatedAt: 2,
- },
- settings: {
- model: {
- current: model,
- available: [{ ref: model, label: "GLM 4.6" }],
- },
- thoughtLevel: {
- enabled: false,
- available: [],
- },
- mode: {
- current: "build",
- },
- },
- projection: {
- sessionId: "sess_1",
- status: "idle",
- mode: "build",
- turnCount: 0,
- totalTokenCount: 0,
- contextUsed: 0,
- contextWindow: 128000,
- pendingPermissions: [],
- activeToolCalls: [],
- backgroundJobs: [],
- },
- runtime: {
- eventSeq: 0,
- stateRevision: 1,
- deliveryKind: "desktop-continuous",
- pendingRequestIds: [],
- },
- messages: params.messages ?? [],
- };
- }
- function createLegacyTaskSnapshot(): ZCodeTaskSnapshot {
- return {
- meta: {
- taskId: "sess_1",
- traceId: "trace-legacy",
- title: "Legacy task",
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- createdAt: 1,
- updatedAt: 2,
- mode: "build",
- provider: "glm",
- status: "completed",
- },
- messages: [
- {
- role: "user",
- content: "old prompt",
- timestamp: 1,
- },
- ],
- };
- }
- describe("readDesktopContinuousSessionRestoreSnapshot", () => {
- it("resumes historical desktop session before reading the limited snapshot", async () => {
- const calls: string[] = [];
- let active = false;
- const zcodeSessionService = {
- resumeSession: vi.fn(async () => {
- calls.push("resume");
- active = true;
- return createSnapshot({ title: "Resumed session" });
- }),
- readSession: vi.fn(async () => {
- calls.push("read");
- if (!active) {
- throw new Error("Session is not active: sess_1");
- }
- return createSnapshot({ title: "Read session" });
- }),
- };
- const restoreParams: Parameters<typeof readDesktopContinuousSessionRestoreSnapshot>[0] = {
- zcodeSessionService,
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- messageLimit: 30,
- model: { providerId: "custom-openai", modelId: "deepseek-v4-flash" },
- thoughtLevel: "high",
- };
- const result = await readDesktopContinuousSessionRestoreSnapshot(restoreParams);
- expect(calls).toEqual(["resume", "read"]);
- expect(zcodeSessionService.resumeSession).toHaveBeenCalledWith({
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- model: { providerId: "custom-openai", modelId: "deepseek-v4-flash" },
- thoughtLevel: "high",
- });
- expect(zcodeSessionService.readSession).toHaveBeenCalledWith({
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- deliveryKind: "desktop-continuous",
- messageLimit: 30,
- });
- expect(result.resumedSessionSnapshot?.session.title).toBe("Resumed session");
- expect(result.snapshot.meta.title).toBe("Read session");
- });
- it("falls back to legacy task snapshot when the protocol session is missing", async () => {
- const legacySnapshot = createLegacyTaskSnapshot();
- const zcodeSessionService = {
- resumeSession: vi.fn(async () => {
- throw new Error("Session not found: sess_1");
- }),
- readSession: vi.fn(async () => createSnapshot()),
- };
- const zcodeTaskService = {
- getTaskSnapshot: vi.fn(async () => legacySnapshot),
- };
- const result = await readDesktopContinuousSessionRestoreSnapshot({
- zcodeSessionService,
- zcodeTaskService,
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- messageLimit: 30,
- });
- expect(result.usedLegacySnapshot).toBe(true);
- expect(result.resumedSessionSnapshot).toBeUndefined();
- expect(result.snapshot.meta.title).toBe("Legacy task");
- expect(zcodeSessionService.readSession).not.toHaveBeenCalled();
- expect(zcodeTaskService.getTaskSnapshot).toHaveBeenCalledWith({
- taskId: "sess_1",
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- messageLimit: 30,
- clientMode: "desktop-continuous",
- });
- });
- it("falls back to task snapshot when the historical runtime model is unavailable", async () => {
- const legacySnapshot = createLegacyTaskSnapshot();
- const unavailableModelError = Object.assign(
- new Error("历史任务使用的模型已不可用,请选择当前可用模型后继续。"),
- { code: "ZCODE_RUNTIME_MODEL_UNAVAILABLE" },
- );
- const zcodeSessionService = {
- resumeSession: vi.fn(async () => {
- throw unavailableModelError;
- }),
- readSession: vi.fn(async () => createSnapshot()),
- };
- const zcodeTaskService = {
- getTaskSnapshot: vi.fn(async () => legacySnapshot),
- };
- const result = await readDesktopContinuousSessionRestoreSnapshot({
- zcodeSessionService,
- zcodeTaskService,
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- messageLimit: 30,
- model: { providerId: "removed-provider", modelId: "removed-model" },
- });
- expect(result.usedLegacySnapshot).toBe(true);
- expect(result.resumedSessionSnapshot).toBeUndefined();
- expect(result.restoreWarning).toMatchObject({
- code: "ZCODE_RUNTIME_MODEL_UNAVAILABLE",
- message: "历史任务使用的模型已不可用,请选择当前可用模型后继续。",
- taskId: "sess_1",
- });
- expect(result.snapshot.meta.title).toBe("Legacy task");
- expect(zcodeSessionService.readSession).not.toHaveBeenCalled();
- expect(zcodeTaskService.getTaskSnapshot).toHaveBeenCalledWith({
- taskId: "sess_1",
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- messageLimit: 30,
- clientMode: "desktop-continuous",
- });
- });
- });
- describe("readWebRemoteReplayableSessionConfigOptions", () => {
- it("resumes with the historical model before reading active mobile task toolbar settings", async () => {
- const calls: string[] = [];
- const historicalModel = {
- providerId: "china-llm-zcode-dev:demo",
- modelId: "deepseek-v4-flash",
- };
- const zcodeSessionService = {
- resumeSession: vi.fn(async () => {
- calls.push("resume");
- return createSnapshot({ title: "Mobile resumed session" });
- }),
- readSession: vi.fn(async () => {
- calls.push("read");
- return createSnapshot({
- title: "Mobile session",
- });
- }),
- };
- const restoreParams: Parameters<typeof readWebRemoteReplayableSessionConfigOptions>[0] = {
- zcodeSessionService,
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- model: historicalModel,
- thoughtLevel: "high",
- };
- const configOptions = await readWebRemoteReplayableSessionConfigOptions(restoreParams);
- expect(calls).toEqual(["resume", "read"]);
- expect(zcodeSessionService.resumeSession).toHaveBeenCalledWith({
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- model: historicalModel,
- thoughtLevel: "high",
- });
- expect(zcodeSessionService.readSession).toHaveBeenCalledWith({
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- deliveryKind: "web-remote-replayable",
- messageLimit: 1,
- });
- expect(configOptions?.map((option) => option.category)).toEqual([
- "model",
- "mode",
- ]);
- });
- it("includes thought level from replayable session settings when enabled", async () => {
- const snapshot = createSnapshot();
- const zcodeSessionService = {
- readSession: vi.fn(async () => ({
- ...snapshot,
- settings: {
- ...snapshot.settings,
- thoughtLevel: {
- enabled: true,
- current: "high",
- available: [
- { value: "low", label: "Low" },
- { value: "high", label: "High" },
- ],
- },
- },
- })),
- resumeSession: vi.fn(),
- };
- const configOptions = await readWebRemoteReplayableSessionConfigOptions({
- zcodeSessionService,
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- });
- expect(configOptions?.map((option) => option.category)).toEqual([
- "model",
- "mode",
- "thought_level",
- ]);
- expect(
- configOptions?.find((option) => option.category === "thought_level")
- ?.currentValue,
- ).toBe("high");
- });
- it("returns null for legacy tasks without an active protocol session", async () => {
- const zcodeSessionService = {
- readSession: vi.fn(async () => {
- throw new Error("Session not found: sess_1");
- }),
- resumeSession: vi.fn(),
- };
- await expect(
- readWebRemoteReplayableSessionConfigOptions({
- zcodeSessionService,
- workspacePath: workspace.workspacePath,
- workspaceIdentity: workspace.workspaceIdentity,
- sessionId: "sess_1",
- }),
- ).resolves.toBeNull();
- });
- });
|