feat(web): 角色与音频工作台 - 完善素材选择、生成参数与同步交互

This commit is contained in:
ddcat666 committed 2026-10-01 09:43:02 +08:00
1 parent 5650da6d31
commit 22e7331d04
77 files changed
+3641 -721

No files matched your search

+62 -34
View File
@@ -4,7 +4,17 @@ import { apiClient } from "../src/services/api/request";
import { canvasContentHash } from "../src/lib/canvas/canvas-content";
import { rebaseCanvasProjects, parseCanvasStorageDocument } from "../src/lib/canvas/canvas-storage-revision";
import { readCanvasSyncDrafts } from "../src/services/canvas-sync-drafts";
import { applyAgentCanvasPatches, deleteCanvasProjectsWithRemoteSync, refreshCanvasAfterAgent, initializeRemoteUserDataSession, installRemoteUserDataAutoSync, loadCanvasProjectForEditing, resetRemoteUserDataSync, saveRemoteUserDataNow, syncRemoteUserData } from "../src/services/user-data-sync";
import {
applyAgentCanvasPatches,
deleteCanvasProjectsWithRemoteSync,
refreshCanvasAfterAgent,
initializeRemoteUserDataSession,
installRemoteUserDataAutoSync,
loadCanvasProjectForEditing,
resetRemoteUserDataSync,
saveRemoteUserDataNow,
syncRemoteUserData,
} from "../src/services/user-data-sync";
import { createAgentCanvasSync } from "../src/services/agent-canvas-sync";
import { flushCanvasStorePersistence, useCanvasStore, type CanvasProject } from "../src/stores/canvas/use-canvas-store";
import { flushAssetStorePersistence, useAssetStore } from "../src/stores/use-asset-store";
@@ -93,11 +103,20 @@ beforeEach(async () => {
data = { id };
}
} else if (id === "batch" && method === "post") {
data = { assets: [{
id: "invalid-mime-asset", kind: "image", title: "历史素材", coverUrl: "", tags: [],
createdAt: "2026-09-01", updatedAt: "2026-09-01",
data: { dataUrl: "https://example.com/image.png", width: 100, height: 100, bytes: 1, mimeType: "image/*" },
}] };
data = {
assets: [
{
id: "invalid-mime-asset",
kind: "image",
title: "历史素材",
coverUrl: "",
tags: [],
createdAt: "2026-09-01",
updatedAt: "2026-09-01",
data: { dataUrl: "https://example.com/image.png", width: 100, height: 100, bytes: 1, mimeType: "image/*" },
},
],
};
} else if (id === "restore" && method === "post") {
const current = remote.get("canvas")!;
if (body.revision !== current.revision) status = 409;
@@ -159,7 +178,8 @@ test("deletion skips editing and invalid MIME assets, including an uncached canv
await deleteCanvasProjectsWithRemoteSync([" canvas ", "canvas", "other"]);
expect(requests.map(({ method, id }) => ({ method, id }))).toEqual([
{ method: "delete", id: "canvas" }, { method: "delete", id: "other" },
{ method: "delete", id: "canvas" },
{ method: "delete", id: "other" },
]);
expect(remote.size).toBe(0);
expect(useCanvasStore.getState().projects).toEqual([]);
@@ -210,24 +230,40 @@ test("load latest, save and reload stay synced when Agent history replays an unv
await loadCanvasProjectForEditing("canvas");
let resolve!: () => void;
let reject!: (error: unknown) => void;
const reconciled = new Promise<void>((done, fail) => { resolve = done; reject = fail; });
const reconciled = new Promise<void>((done, fail) => {
resolve = done;
reject = fail;
});
const sync = createAgentCanvasSync({
canvasId: "canvas", batchMs: 0,
canvasId: "canvas",
batchMs: 0,
applyPatches: (patches) => applyAgentCanvasPatches("canvas", patches),
refresh: async () => { await refreshCanvasAfterAgent("canvas"); resolve(); },
refresh: async () => {
await refreshCanvasAfterAgent("canvas");
resolve();
},
onError: reject,
});
try {
sync.receive({ eventId: "historical-event", runId: "completed-run", seq: 1, type: "canvas_updated", createdAt: "2026-09-17", payload: {
canvasId: "canvas",
canvasPatch: { canvasId: "canvas", updatedAt: "2026-09-17", nodes: [{ before: null, after: { ...canvas().nodes[0], id: "stale-node" } }], connections: [] },
} });
sync.receive({
eventId: "historical-event",
runId: "completed-run",
seq: 1,
type: "canvas_updated",
createdAt: "2026-09-17",
payload: {
canvasId: "canvas",
canvasPatch: { canvasId: "canvas", updatedAt: "2026-09-17", nodes: [{ before: null, after: { ...canvas().nodes[0], id: "stale-node" } }], connections: [] },
},
});
await reconciled;
expect(useSyncProgressStore.getState().syncingProjects.canvas.phase).toBe("done");
await saveRemoteUserDataNow("canvas");
expect(useCanvasStore.getState().openProject("canvas")).toMatchObject({ revision: 10, title: "已保存的标题", nodes: canvas().nodes });
expect(await readCanvasSyncDrafts("canvas")).toHaveLength(0);
} finally { sync.dispose(); }
} finally {
sync.dispose();
}
}
expect(requests.filter((request) => request.method === "put")).toHaveLength(1);
});
@@ -246,30 +282,22 @@ test("a stale save auto-reconciles when the cloud already contains the identical
expect(await readCanvasSyncDrafts("canvas")).toHaveLength(0);
});
test("a conflict preserves drafts, stops retries and does not block another canvas", async () => {
test("a stale save rebases disjoint Agent edits and keeps saving", async () => {
remote.set("canvas", { ...addNode(canvas(), "remote-video"), revision: 2 });
useCanvasStore.getState().updateProject("canvas", { nodes: addNode(canvas(), "local-video").nodes });
useCanvasStore.getState().renameProject("other", "other edit");
await expect(saveRemoteUserDataNow()).rejects.toThrow("版本冲突");
expect(remote.get("canvas")!.nodes.map((node) => node.id)).toEqual(["old-image", "remote-video"]);
await expect(saveRemoteUserDataNow()).resolves.toBeUndefined();
expect(remote.get("canvas")!.nodes.map((node) => node.id)).toEqual(["old-image", "local-video", "remote-video"]);
expect(remote.get("canvas")!.revision).toBe(3);
expect(remote.get("other")!.title).toBe("other edit");
expect(useSyncProgressStore.getState().syncingProjects.canvas.phase).toBe("conflict");
expect((await readCanvasSyncDrafts("canvas"))[0].project.nodes.at(-1)!.id).toBe("local-video");
expect(useSyncProgressStore.getState().syncingProjects.canvas.phase).toBe("done");
useCanvasStore.getState().renameProject("canvas", "more local edits");
await expect(saveRemoteUserDataNow()).rejects.toThrow("云端画布已有更新");
useCanvasStore.getState().renameProject("other", "still saves independently");
await saveRemoteUserDataNow("other");
expect(remote.get("other")!.title).toBe("still saves independently");
await expect(saveRemoteUserDataNow(["canvas", "other"])).rejects.toThrow("云端画布已有更新");
expect(requests.filter((request) => request.method === "put" && request.id === "canvas")).toHaveLength(1);
await expect(saveRemoteUserDataNow("canvas")).rejects.toThrow("云端画布已有更新");
await loadCanvasProjectForEditing("canvas", { latest: true });
expect(useCanvasStore.getState().openProject("canvas")!.nodes.at(-1)!.id).toBe("remote-video");
expect((await readCanvasSyncDrafts("canvas")).some((draft) => draft.project.title === "more local edits")).toBe(true);
resetRemoteUserDataSync();
await useCanvasStore.persist.rehydrate();
await syncRemoteUserData(scope);
expect(useSyncProgressStore.getState().syncingProjects.canvas.draftCount).toBe(2);
await expect(saveRemoteUserDataNow("canvas")).resolves.toBeUndefined();
expect(remote.get("canvas")!.title).toBe("more local edits");
expect(useSyncProgressStore.getState().syncingProjects.canvas.phase).toBe("done");
});
test("transient cloud failures schedule a bounded automatic retry", async () => {